.. _tutorial: ######## TUTORIAL ######## To be completed... *********** Basic usage *********** Initialize panel :: panel=Panel(fig_width=900,) Create a track :: test_track = tracks.BaseTrack(name = 'test', sort_by = None) Create a feature :: feat1=features.Simple(name = 'feat1', start = 100, end = 356, ) Add a features to a track :: test_track.append(feat1) :: test_track.extend([feat1, feat2, feat3]) or directly: :: test_track = tracks.BaseTrack(features.Simple(name = 'feat1', start = 100, end = 356, ), features.Simple(name = 'feat2', start = 30, end = 856, ), features.Simple(name = 'feat3', start = 400, end = 700, ), sort_by = 'None',) Add a track to the panel: :: panel.add_track(test_track) Save image. This supports all format supported by matplotlib including: * png * jpeg * pdf * svg :: panel.save('test_image.png') Complete basic example: :: from biograpy import Panel, tracks, features panel=Panel(fig_width=900, padding = 25)#initialize a drawer, and add some padding test_track = tracks.BaseTrack(features.Simple(name = 'feat1', start = 100, end = 356, ), features.Simple(name = 'feat2', start = 30, end = 856, ), features.Simple(name = 'feat3', start = 400, end = 700, ), sort_by = 'None',) panel.add_track(test_track) panel.save('basic_test.png') And this is the resulting image: .. image:: images/basic_test.png ********* The Panel ********* The panel object is the "canvas" to which all the objects will be drawn. **panel.fig** is the matplotlib Figure object **panel.tracks** is the collector of all the tracks. It is a Python list object **panel.save(filepath)** calling this method will draw all the tracks and features to the panel.fig object, and will save the figure to the filepath passed **panel.htmlmap** after ``panel.save(filepath)`` has been called the image htmlmap is accessible as this panel property if the specified format was png or jpg. `TODO` ****** Tracks ****** Handling Tracks --------------- Tracks can be seen as ordered collection of features. For convenience *track.append()* and *track.extend* works as the common list methods. `TODO` Ordering features in track -------------------------- collapse (default) order as they come = user defined order by lenght order by score `TODO` Color scales ------------ colormaps ^^^^^^^^^ `TODO` max and min ^^^^^^^^^^^ `TODO` colorbars ^^^^^^^^^ `TODO` ******** Features ******** Simple features --------------- `TODO` Draw SeqFeatures ---------------- Generic ^^^^^^^ `TODO` Gene ^^^^ `TODO` Segmented ^^^^^^^^^ `TODO` Single position ^^^^^^^^^^^^^^^ `TODO` Draw complex features --------------------- mRNA and CDS ^^^^^^^^^^^^ `TODO` Transmembrane regions ^^^^^^^^^^^^^^^^^^^^^ `TODO` Secondary structures ^^^^^^^^^^^^^^^^^^^^ `TODO` Domains ^^^^^^^ :: from biograpy import Panel, tracks, features from Bio import SeqFeature panel = Panel(fig_width=900) test_track = tracks.BaseTrack(name = 'domains', sort_by = None, cm = 'Set3') feat = SeqFeature.SeqFeature() feat.location = SeqFeature.FeatureLocation(100, 300) dfeat = features.DomainFeature([feat], name = 'test domain 1', seq_line = (1, 766)) test_track.append(dfeat) dfeat = features.DomainFeature([feat],name = 'test domain 1', height = 1.5, seq_line = (1, 766)) test_track.append(dfeat) dfeat = features.DomainFeature([feat],name = 'test domain 1', height = 1.5, boxstyle = 'round4, rounding_size=1.4', seq_line = (1, 766)) test_track.append(dfeat) dfeat = features.DomainFeature([feat],name = 'test domain 1',height = 1.5, boxstyle = 'larrow, pad = 0', seq_line = (1, 766)) test_track.append(dfeat) dfeat = features.DomainFeature([feat], name = 'test domain 1',height = 1.5, boxstyle = 'round ', seq_line = (1, 766)) test_track.append(dfeat) feat2 = SeqFeature.SeqFeature() feat2.location = SeqFeature.FeatureLocation(500, 700) dfeat = features.DomainFeature([feat,feat2], name = 'test domain 1',height = 1.5, boxstyle = 'roundtooth, pad = 0.1, tooth_size=1.2', seq_line = (1, 766), alpha = 0.7) test_track.append(dfeat) dfeat = features.DomainFeature([feat,feat2],name = ['test domain 1', 'test domain 2'], height = 1.5, boxstyle = ['sawtooth, tooth_size=1.4', 'rarrow, pad = 0.1'], seq_line = (1, 766), ec = 'None') test_track.append(dfeat) dfeat = features.DomainFeature([feat,feat2],name = ['', 'test domain 2'],height = 1.5, boxstyle = ['round4, rounding_size=1.4', 'larrow, pad = 0.1'], ec = 'None', fc = ['y', 'c'], color_by_cm = False) test_track.append(dfeat) panel.add_track(test_track) panel.save('domain_test.png') resulting image: .. image:: images/domain_test.png Draw lineplots -------------- Plot feature requires plot track `TODO` **************** Draw a SeqRecord **************** Starting from this_ annotated entry in EMBL format .. _this: https://github.com/apierleoni/BioGraPy/blob/master/src/biograpy/tests/factor7.embl Generate a plot :: from biograpy.seqrecord import SeqRecordDrawer from biograpy import Panel, tracks, features from Bio import SeqIO seqrecord = SeqIO.parse(open("factor7.embl"), "embl").next() SeqRecordDrawer(seqrecord, fig_width=900).save("factor7.png") .. image:: images/factor7.png Check out the similar plot done with BioPerl here_. .. _here: http://www.bioperl.org/wiki/HOWTO:Graphics#Rendering_Features_from_a_GenBank_or_EMBL_File ******************* Draw a Blast Output ******************* :: from biograpy import Panel, tracks, features from Bio import SeqIO, AlignIO, SeqFeature from Bio.Blast import NCBIXML '''Parse BLAST XML output''' aligndata = [] blasthits=NCBIXML.parse(open('BLASTalignment.xml')) id_filter = .3 #show only alignments with sequence identity higher than 30% for hit in blasthits: for alignment in hit.alignments: hsps = [] for hsp in alignment.hsps: if (hsp.identities/float(hsp.align_length) >= id_filter): hit_id = alignment.hit_id hit_name = alignment.hit_def [:40] align=dict(hit_id = hit_id, hit_name = hit_name, hit_start = hsp.sbjct_start, hit_stop = hsp.sbjct_end, query_start = hsp.query_start, query_stop = hsp.query_end, align_lenght = hsp.align_length, identity_percent = hsp.identities/float(hsp.align_length)*100, similarity_percent = hsp.positives/float(hsp.align_length)*100, evalue = hsp.expect, blast_score = hsp.score, query_seq = hsp.query, match_seq = hsp.match, hit_seq = hsp.sbjct, aligns = hsp.num_alignments, other = dir(hsp)) hsps.append(align) aligndata.append(dict(hsps= hsps, hit_def = alignment.hit_def, hit_id = alignment.hit_id, length = alignment.length, title = alignment.title,)) '''Draw Alignment''' panel=Panel(fig_width=900,)#initialize a drawer ref_track = tracks.BaseTrack(features.Simple(1,#draw a reference object hit.query_length, name = hit.query, height = 1.5, fc='grey', ec='k',), name = 'Query') panel.append(ref_track) aligns_track = tracks.BaseTrack(name = 'Hits', cm ='Blues', ) for align in aligndata: for hsp in align['hsps']: location = SeqFeature.FeatureLocation(hsp['query_start'], hsp['query_stop']) feat = SeqFeature.SeqFeature() feat.type = 'Hit' feat.id = hsp['hit_id'] feat.location = location feat.qualifiers['ft_description'] = ['Hit'] feat.qualifiers['ft_type'] = ['Hit'] feat.qualifiers['score'] = float(hsp['identity_percent'])/100. feat.qualifiers['hit_name'] = [hsp['hit_id']] aligns_track.append(features.GenericSeqFeature(feat, score = feat.qualifiers['score'], name = hsp['hit_name'], use_score_for_color = True, url = "http://www.ncbi.nlm.nih.gov/protein/%s"%hsp['hit_id'].split('|')[-2],)) panel.append(aligns_track) panel.save('blast_output.png') print panel.htmlmap Generated image: .. image:: images/blast_output.png to be coupled with the generated html map available calling :: panel.htmlmap :: sp|P04626|ERBB2_HUMAN Receptor tyrosine-protein kinase erbB-2 OS=Homo sapiens GN=ERBB2 PE=1 SV=1 receptor tyrosine-protein kinase erbB-2 receptor tyrosine-protein kinase erbB-2 epidermal growth factor receptor isoform receptor tyrosine-protein kinase erbB-4 receptor tyrosine-protein kinase erbB-4 receptor tyrosine-protein kinase erbB-3 epidermal growth factor receptor isoform epidermal growth factor receptor isoform epidermal growth factor receptor isoform ephrin type-B receptor 1 precursor [Homo ephrin type-A receptor 2 precursor [Homo tyrosine-protein kinase ABL2 isoform e [ ephrin type-A receptor 5 isoform b precu ephrin type-B receptor 4 precursor [Homo ephrin type-A receptor 5 isoform a precu protein-tyrosine kinase 2-beta isoform a protein-tyrosine kinase 2-beta isoform b ephrin type-B receptor 2 isoform 2 precu ephrin type-B receptor 2 isoform 1 precu tyrosine-protein kinase ABL2 isoform d [ tyrosine-protein kinase ABL2 isoform i [ tyrosine-protein kinase ABL2 isoform c [ tyrosine-protein kinase ABL2 isoform f [ tyrosine-protein kinase ABL2 isoform b [ tyrosine-protein kinase ABL2 isoform g [ tyrosine-protein kinase ABL2 isoform h [ focal adhesion kinase 1 isoform a [Homo activated CDC42 kinase 1 isoform 1 [Homo focal adhesion kinase 1 isoform b [Homo tyrosine-protein kinase ZAP-70 isoform 2 focal adhesion kinase 1 isoform c [Homo activated CDC42 kinase 1 isoform 2 [Homo ephrin type-A receptor 4 precursor [Homo ephrin type-A receptor 3 isoform a precu tyrosine-protein kinase ABL1 isoform a [ tyrosine-protein kinase ABL1 isoform b [ ephrin type-B receptor 3 precursor [Homo proto-oncogene tyrosine-protein kinase r proto-oncogene tyrosine-protein kinase r tyrosine-protein kinase ZAP-70 isoform 1 tyrosine-protein kinase SYK isoform 2 [H tyrosine-protein kinase SYK isoform 1 [H hepatocyte growth factor receptor isofor ephrin type-A receptor 7 precursor [Homo hepatocyte growth factor receptor isofor macrophage-stimulating protein receptor ephrin type-A receptor 1 precursor [Homo ephrin type-A receptor 8 isoform 1 precu tyrosine-protein kinase ITK/TSK [Homo sa tyrosine-protein kinase TXK [Homo sapien fibroblast growth factor receptor 3 isof fibroblast growth factor receptor 3 isof fibroblast growth factor receptor 3 isof tyrosine-protein kinase Tec [Homo sapien basic fibroblast growth factor receptor basic fibroblast growth factor receptor tyrosine-protein kinase FRK [Homo sapien basic fibroblast growth factor receptor basic fibroblast growth factor receptor basic fibroblast growth factor receptor fibroblast growth factor receptor 2 isof basic fibroblast growth factor receptor fibroblast growth factor receptor 2 isof fibroblast growth factor receptor 2 isof basic fibroblast growth factor receptor fibroblast growth factor receptor 2 isof fibroblast growth factor receptor 2 isof fibroblast growth factor receptor 2 isof tyrosine-protein kinase Fyn isoform a [H fibroblast growth factor receptor 2 isof fibroblast growth factor receptor 2 isof fibroblast growth factor receptor 2 isof tyrosine-protein kinase Mer precursor [H tyrosine-protein kinase Srms [Homo sapie tyrosine-protein kinase Fyn isoform d [H tyrosine-protein kinase BTK [Homo sapien tyrosine-protein kinase Blk [Homo sapien tyrosine-protein kinase CSK [Homo sapien tyrosine-protein kinase Fyn isoform b [H fibroblast growth factor receptor 4 isof fibroblast growth factor receptor 4 isof proto-oncogene tyrosine-protein kinase R proto-oncogene tyrosine-protein kinase S angiopoietin-1 receptor precursor [Homo tyrosine-protein kinase JAK3 [Homo sapie tyrosine-protein kinase Fgr [Homo sapien megakaryocyte-associated tyrosine-protei leukocyte tyrosine kinase receptor isofo leukocyte tyrosine kinase receptor isofo insulin receptor-related protein precurs tyrosine-protein kinase Fyn isoform c [H megakaryocyte-associated tyrosine-protei megakaryocyte-associated tyrosine-protei tyrosine-protein kinase receptor TYRO3 [ cytoplasmic tyrosine-protein kinase BMX tyrosine-protein kinase receptor UFO iso tyrosine-protein kinase receptor UFO iso insulin receptor isoform Long preproprot insulin receptor isoform Short prepropro or just save as an SVG file to get href attributes automatically added:: panel.save('blast_output.svg') .. image:: images/blast_output.svg :height: 500px :width: 1100 px ********************* Extending the library ********************* Creating a custom GraphicFeature -------------------------------- Subclassing from ``biograpy.features.BaseGraphicFeature`` `TODO`