def __init__(self, seqrecord, draw_type='simple', gene_to_draw=None, feature_class_qualifier='ft_description', feature_name_qualifier='hit_name', **kwargs): self.seqrecord = seqrecord self.panel = Panel(**kwargs) self.feature_class_qualifier = feature_class_qualifier self.feature_name_qualifier = feature_name_qualifier if draw_type == 'simple': ref_obj_track = tracks.BaseTrack( features.Simple( 1, len(self.seqrecord), height=1.5, name=seqrecord.name, color_by_cm=True, track_lines=3, alpha=1, )) self.panel.add_track(ref_obj_track) self.draw_features() elif draw_type == 'gene structure': self.draw_features_ordered_by_gene_struct(gene_code=gene_to_draw)
def drawFeatures2(tks, highlight=[], filename=None, color=None, **kwargs): from biograpy import Panel, tracks, features panel = Panel(fig_width=500, fig_height=150, fig_dpi=100, padding=10, grid='both') for feats in tks: test_track = tracks.BaseTrack(name='X', height=.2) for feat in feats: if feat.location.strand == -1: arr = "larrow,pad=0.6" else: arr = "rarrow,pad=0.6" q = feat.qualifiers tag = q['locus_tag'][0] test_track.append( features.GenericSeqFeature( feat, name=tag, boxstyle=arr, )) panel.add_track(test_track) if filename == None: filename = 'genediagram' panel.save(filename + '.png') panel.close() return filename + '.png'
def doWork(args): panel = Panel(fig_width=900, padding=25, grid=None, xmin=0) seq_length = 0 for gff in args.gffs: seqrecord = GFF.parse(gff).next() if len(seqrecord) > seq_length: seq_length = len(seqrecord) #seqrecord = SeqIO.parse(args.infile, "genbank").next() cds_track = tracks.BaseTrack(sort_by='collapse') for feature in seqrecord.features: if feature.type == 'CDS': #print feature.qualifiers['product'] if feature.qualifiers['product'][0] == 'hypothetical protein': col = '#BDBDBD' else: col = '#2B8CBE' feat = features.GenericSeqFeature(feature, color_by_cm=False, fc=col) cds_track.append(feat) elif feature.type == 'source': cds_track.append( features.GenericSeqFeature(feature, color_by_cm=False, alpha=0.0, fc='1.0', ec='1.0')) else: cds_track.append( features.GenericSeqFeature(feature, color_by_cm=False, fc='0.0', ec='0.0')) panel.add_track(cds_track) panel.save(args.outfile, xmin=0, xmax=seq_length)
def test_simple_features(self): panel=Panel(fig_width=1000)#initialize a drawer test_track = tracks.BaseTrack(features.Simple(name='feat1',start=100,end=756,), features.Simple(name='feat2',start=300,end=1056,), features.Simple(name='feat3',start=600,end=1356,), features.Simple(name='feat4',start=800,end=1356,), features.Simple(name='feat5',start= 1357,end=1806,), name = 'test') panel.add_track(test_track) fh = tempfile.TemporaryFile() panel.save(fh, format='png') fh.seek(0) img = Image.open(fh) #self.assertEqual(img.size, (1000, 220)) self.assertEqual(img.format, 'PNG')
if is_gene_name_replace_protein_name: m = re.search('([^.]+)', seq_title) seq_title = m.group(1) else: pass print seq_title for domain_obj in domain_objs: print domain_obj.start # draw domain structure using BioGraPy panel = Panel(fig_dpi=100) #if not test_track: test_track = tracks.BaseTrack(name=seq_title, sort_by=None) for index, domain_obj in enumerate(domain_objs): dfeat = None feat = SeqFeature.SeqFeature() start, stop = domain_obj.start, domain_obj.stop #feat.location = SeqFeature.FeatureLocation(start, stop) feat.location = SeqFeature.FeatureLocation(1, 100) feats.append(feat) domain_names.append(domain_obj.short_name) if is_domain_on_same_line: continue if is_domain_on_diff_line: colors_4_this_domain = [domain_colors[domain_obj.short_name]] colors_4_this_domain = 'y' if index == 0:
def draw_features(self): tracks2draw = dict() feat_collector = { } #store complex feature grouping. drawing for witch it is called later for feature in self.seqrecord.features: if feature.type not in tracks2draw: tracks2draw[feature.type] = tracks.BaseTrack(name=feature.type) feature_name = '' if feature.id != '<unknown id>': feature_name = feature.id if self.feature_name_qualifier in feature.qualifiers: if isinstance(feature.qualifiers[self.feature_name_qualifier], list): feature_name = feature.qualifiers[ self.feature_name_qualifier][0] elif isinstance( feature.qualifiers[self.feature_name_qualifier], str): feature_name = feature.qualifiers[ self.feature_name_qualifier] '''draw segmented features ''' drawn = False if feature.sub_features: for sub_feature in feature.sub_features: #detect 'join subfeature and calls the appropriate feature if sub_feature.location_operator == 'join': grfeat = features.SegmentedSeqFeature( feature, name=feature_name, ) tracks2draw[feature.type].add_feature(grfeat) drawn = True break else: if not drawn: '''Add here feature specific visualizations ''' if feature.type == 'gene': #'gene' specific visualization grfeat = features.GeneSeqFeature( feature, name=feature_name, ) tracks2draw[feature.type].add_feature(grfeat) elif feature.location.start.position == feature.location.end.position: #single point feature grfeat = features.SinglePositionFeature( feature, name=feature_name, ) tracks2draw[feature.type].add_feature(grfeat) elif 'transmembrane' in feature.type.lower( ): #add TM handling, cannot handle multiple TM annotations if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'transmembrane' in feat_collector: feat_collector['transmembrane'] = dict( TM=[], cyto=[], non_cyto=[], ) feat_collector['transmembrane']['TM'].append(feature) if 'name' not in feat_collector['transmembrane']: feat_collector['transmembrane'][ 'name'] = feature_name elif 'topological domain' in feature.type.lower( ): #handles topological domains from uniprot if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'transmembrane' in feat_collector: feat_collector['transmembrane'] = dict(TM=[], cyto=[], non_cyto=[]) if 'description' in feature.qualifiers: cyto = False if isinstance(feature.qualifiers['description'], list): for d in feature.qualifiers['description']: if d.lower() == 'cytoplasmic': cyto = True else: if feature.qualifiers['description'].lower( ) == 'cytoplasmic': cyto = True if cyto: feat_collector['transmembrane']['cyto'].append( feature) else: feat_collector['transmembrane'][ 'non_cyto'].append(feature) elif 'cytoplasm' in feature.type.lower(): #risky!! if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'transmembrane' in feat_collector: feat_collector['transmembrane'] = dict(TM=[], cyto=[], non_cyto=[]) if ('not' in feature.type.lower()) or ( 'non' in feature.type.lower()): feat_collector['transmembrane']['non_cyto'].append( feature) else: feat_collector['transmembrane']['cyto'].append( feature) elif feature.type == 'beta_strand': #add secondary structure handling if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'SecStructFeature' in feat_collector: feat_collector['secondary structure'] = dict( betas=[], alphah=[], coil=[]) feat_collector['secondary structure']['betas'].append( feature) elif feature.type == 'alpha_helix': #add secondary structure handling if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'secondary structure' in feat_collector: feat_collector['secondary structure'] = dict( betas=[], alphah=[], coil=[]) feat_collector['secondary structure']['alphah'].append( feature) elif feature.type == 'peptide_coil': #add secondary structure handling if feature.type in tracks2draw: del tracks2draw[feature.type] if not 'secondary structure' in feat_collector: feat_collector['secondary structure'] = dict( betas=[], alphah=[], coil=[]) feat_collector['secondary structure']['coil'].append( feature) else: #generic feature if 'score' in feature.qualifiers: #color by score try: color_by_score = False if isinstance(feature.qualifiers['score'], list): score = float( feature.qualifiers['score'][0]) if 0 <= score <= 1: feat_score = score color_by_score = True else: score = float(feature.qualifiers['score']) if 0 <= score <= 1: feat_score = score color_by_score = True if color_by_score: grfeat = features.GenericSeqFeature( feature, name=feature_name, score=feat_score, use_score_for_color=True, cm='RdYlGn') else: raise ValueError( 'Score value cannot be handled, should be a float between 0 and 1. Actual value: %f' % score) except: #draw normal feature grfeat = features.GenericSeqFeature( feature, name=feature_name) else: grfeat = features.GenericSeqFeature( feature, name=feature_name) tracks2draw[feature.type].add_feature(grfeat) '''draw complex features ''' for feat_group in feat_collector: if feat_group not in tracks2draw: tracks2draw[feat_group] = tracks.BaseTrack(name=feat_group) if feat_group == 'transmembrane': grfeat = features.TMFeature( fill_color='k', cyto_color='r', #cyto_label = 'in', non_cyto_color='g', non_cyto_linestyle='-', #non_cyto_label = 'out', TM_ec='orange', TM_fc='orange', TM_label='', **feat_collector[feat_group]) elif feat_group == 'secondary structure': grfeat = features.SecStructFeature( **feat_collector[feat_group]) tracks2draw[feat_group].add_feature(grfeat) '''add tracks to panel''' for key in sorted(tracks2draw.keys()): self.panel.add_track(tracks2draw[key]) if self.seqrecord.letter_annotations: per_letter_feats = [] ymax = ymin = 0 for quality in self.seqrecord.letter_annotations: if isinstance(self.seqrecord.letter_annotations[quality][0], (int, float)): '''draw numeric per letter annotation as line plot feature ''' feat = features.PlotFeature( self.seqrecord.letter_annotations[quality], range( 1, len(self.seqrecord.letter_annotations[quality]) + 1), label=str(quality), ) per_letter_feats.append(feat) if min(self.seqrecord.letter_annotations[quality]) <= ymin: ymin = min(self.seqrecord.letter_annotations[quality]) if max(self.seqrecord.letter_annotations[quality]) >= ymax: ymax = max(self.seqrecord.letter_annotations[quality]) per_letter_track = tracks.PlotTrack(ymin=ymin, ymax=ymax) for feat in per_letter_feats: per_letter_track.append(feat) self.panel.add_track(per_letter_track)
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)
from biograpy import Panel, tracks, features panel = Panel(fig_width=1000, ) #initialize a drawer test_track = tracks.BaseTrack(features.Simple( name='feat1', start=100, end=756, ), features.Simple( name='feat2', start=300, end=1056, ), features.Simple( name='feat3', start=600, end=1356, ), features.Simple( name='feat4', start=800, end=1356, ), features.Simple( name='feat5', start=1357, end=1806, ), name='test') panel.append(test_track) panel.save('test.png')