def plotPrimers(self): """ Plot Primer Sets """ if self.primerfile is None: sys.stderr.write( "You cannot plot the primers without specifying the primer file location\n" ) sys.exit(-1) primer = Primer(self.primerfile) primergenes = primer.unique_genes() # Filter out the primer genes not in the names genelist = set() for gene in primergenes: for name in self.names: # If the gene is in any of the names then # add it to the genelist and break the loop if gene in name: genelist.add(gene) break if len(genelist) == 0: raise ValueError( "None of the genes in %s were found to match any of the identifiers names in %s" % (str(primergenes), str(self.names))) genecount = 0 labelf = 'Forward Primer' labelr = 'Reverse Primer' yaxislabels = [] for gene in genelist: genecount -= 1 primer_regions = primer.get_merged_primer_regions(gene) for region in primer_regions: if region.direction == 'R': marker = '<' self.ax.plot((region.start, region.end), (genecount, genecount), color=self.linestyle['Primer']['color'], lw=self.linestyle['Primer']['width'], label=labelr, marker=marker) labelr = '' else: marker = '>' self.ax.plot((region.start, region.end), (genecount, genecount), color=self.linestyle['Primer']['color'], lw=self.linestyle['Primer']['width'], label=labelf, marker=marker) labelf = '' self.xmax = region.end yaxislabels.insert(0, (genecount, gene)) self.yaxislabels = yaxislabels + [(0, '')] + self.yaxislabels
def ref_coverage( reffile, primerfile, pattern ): ''' >>> from pprint import pprint >>> ref_match_pattern = '(?P<name>(?P<accession>.*?)_(?P<gene>.*?)_(?P<virus>.*))' >>> reffile = 'Examples/Ref/H1N1_boston.fasta' >>> primerfile = 'Examples/Primer/sH1N1.fasta' >>> rc = ref_coverage( reffile, primerfile, ref_match_pattern ) >>> isinstance( rc, dict ) True ''' ref = refs( reffile, pattern ) primer = Primer( primerfile ) genes = {} count = 0 # Labels for legend labelf = 'Forward Primer' labelr = 'Reverse Primer' # Ensure iteration in sorted order to ensure consistent iteration everywhere for gene in sorted( ref.keys() ): logging.debug( "Creating Line2D for %s" % gene ) regions = primer.get_merged_primer_regions( gene ) reflen = ref.get( gene, False ) if not regions: raise ValueError( "%s had no regions for gene %s. You may need to refine the match pattern." % (primerfile, gene) ) # Create a line for the reference segment refline = Line2D( [0, reflen], [count, count], linewidth=5, color='green', alpha=0.5 ) logging.debug( "Reference Line: (0,%s), (%s,%s)" % (reflen, count, count) ) primerlines = [] for region in regions: primerlines.append( Line2D( [region.start, region.end], [count, count], linewidth=2, color='black' ) ) if region.direction == 'R': primerlines[-1].set_marker( '<' ) primerlines[-1].set_label( labelr ) # Set reverse label to None so that only 1 line gets an actual label # otherwise the legend tries to use all of them making it way to big labelr = '' else: primerlines[-1].set_marker( '>' ) primerlines[-1].set_label( labelf ) # Same principal as above labelf = '' logging.debug( "Primer Line: (%s,%s), (%s,%s)" % (region.start, region.end, count, count) ) genes[gene] = {'Reference': refline, 'Primers': primerlines} count += 1 # Now create just one labeled refrence line for the legend first = genes[genes.keys()[0]] first['Reference'].set_label( 'Reference' ) return genes
def plotPrimers( self ): """ Plot Primer Sets """ if self.primerfile is None: sys.stderr.write( "You cannot plot the primers without specifying the primer file location\n" ) sys.exit( -1 ) primer = Primer( self.primerfile ) primergenes = primer.unique_genes() # Filter out the primer genes not in the names genelist = set() for gene in primergenes: for name in self.names: # If the gene is in any of the names then # add it to the genelist and break the loop if gene in name: genelist.add( gene ) break if len( genelist ) == 0: raise ValueError( "None of the genes in %s were found to match any of the identifiers names in %s" % (str( primergenes ), str( self.names )) ) genecount = 0 labelf='Forward Primer' labelr='Reverse Primer' yaxislabels = [] for gene in genelist: genecount -= 1 primer_regions = primer.get_merged_primer_regions( gene ) for region in primer_regions: if region.direction == 'R': marker = '<' self.ax.plot( (region.start, region.end), (genecount, genecount), color=self.linestyle['Primer']['color'], lw=self.linestyle['Primer']['width'], label=labelr, marker=marker ) labelr = '' else: marker = '>' self.ax.plot( (region.start, region.end), (genecount, genecount), color=self.linestyle['Primer']['color'], lw=self.linestyle['Primer']['width'], label=labelf, marker=marker ) labelf = '' self.xmax = region.end yaxislabels.insert( 0, (genecount, gene) ) self.yaxislabels = yaxislabels + [(0, '')] + self.yaxislabels