def main(): skipped = 0 not_pairwise = 0 if int_file == "None": try: maf_reader = bx.align.maf.Reader( open(inp_file, 'r') ) except: stop_err("Your MAF file appears to be malformed.") print >>fout, "#Seq1\tStart1\tEnd1\tSeq2\tStart2\tEnd2\tL\tN\tp" for block in maf_reader: if len(block.components) != 2: not_pairwise += 1 continue try: rateEstimator(block) except: skipped += 1 else: index, index_filename = maf_utilities.build_maf_index( inp_file, species = [dbkey_i] ) if index is None: print >> sys.stderr, "Your MAF file appears to be malformed." sys.exit() win = NiceReaderWrapper( fileinput.FileInput( int_file ), chrom_col=chr_col_i, start_col=start_col_i, end_col=end_col_i, strand_col=strand_col_i, fix_strand=True) species=None mincols = 0 global alignlen, mismatches for interval in win: alignlen = 0 mismatches = 0.0 src = "%s.%s" % ( dbkey_i, interval.chrom ) for block in maf_utilities.get_chopped_blocks_for_region( index, src, interval, species, mincols ): if len(block.components) != 2: not_pairwise += 1 continue try: rateEstimator(block) except: skipped += 1 if alignlen: p = mismatches/alignlen else: p = 'NA' interval.fields.append(str(alignlen)) interval.fields.append(str(mismatches)) interval.fields.append(str(p)) print >>fout, "\t".join(interval.fields) #num_blocks += 1 if not_pairwise: print "Skipped %d non-pairwise blocks" %(not_pairwise) if skipped: print "Skipped %d blocks as invalid" %(skipped)
def main(): skipped = 0 not_pairwise = 0 if int_file == "None": try: maf_reader = bx.align.maf.Reader( open(inp_file, 'r') ) except: stop_err("Your MAF file appears to be malformed.") print >> fout, "#Seq1\tStart1\tEnd1\tSeq2\tStart2\tEnd2\tL\tN\tp" for block in maf_reader: if len(block.components) != 2: not_pairwise += 1 continue try: rateEstimator(block) except: skipped += 1 else: index, index_filename = maf_utilities.build_maf_index( inp_file, species = [dbkey_i] ) if index is None: print >> sys.stderr, "Your MAF file appears to be malformed." sys.exit() win = NiceReaderWrapper( fileinput.FileInput( int_file ), chrom_col=chr_col_i, start_col=start_col_i, end_col=end_col_i, strand_col=strand_col_i, fix_strand=True) species = None mincols = 0 global alignlen, mismatches for interval in win: alignlen = 0 mismatches = 0.0 src = "%s.%s" % ( dbkey_i, interval.chrom ) for block in maf_utilities.get_chopped_blocks_for_region( index, src, interval, species, mincols ): if len(block.components) != 2: not_pairwise += 1 continue try: rateEstimator(block) except: skipped += 1 if alignlen: p = mismatches/alignlen else: p = 'NA' interval.fields.append(str(alignlen)) interval.fields.append(str(mismatches)) interval.fields.append(str(p)) print >> fout, "\t".join(interval.fields) #num_blocks += 1 if not_pairwise: print "Skipped %d non-pairwise blocks" % (not_pairwise) if skipped: print "Skipped %d blocks as invalid" % (skipped)
def __main__(): maf_source_type = sys.argv.pop( 1 ) input_maf_filename = sys.argv[1].strip() input_interval_filename = sys.argv[2].strip() output_filename = sys.argv[3].strip() dbkey = sys.argv[4].strip() try: chr_col = int( sys.argv[5].strip() ) - 1 start_col = int( sys.argv[6].strip() ) - 1 end_col = int( sys.argv[7].strip() ) - 1 except: print >>sys.stderr, "You appear to be missing metadata. You can specify your metadata by clicking on the pencil icon associated with your interval file." sys.exit() summary = sys.argv[8].strip() if summary.lower() == "true": summary = True else: summary = False mafIndexFile = "%s/maf_index.loc" % sys.argv[9] try: maf_index_filename = sys.argv[10].strip() except: maf_index_filename = None index = index_filename = None if maf_source_type == "user": #index maf for use here index, index_filename = maf_utilities.open_or_build_maf_index( input_maf_filename, maf_index_filename, species = [dbkey] ) if index is None: print >>sys.stderr, "Your MAF file appears to be malformed." sys.exit() elif maf_source_type == "cached": #access existing indexes index = maf_utilities.maf_index_by_uid( input_maf_filename, mafIndexFile ) if index is None: print >> sys.stderr, "The MAF source specified (%s) appears to be invalid." % ( input_maf_filename ) sys.exit() else: print >>sys.stdout, 'Invalid source type specified: %s' % maf_source_type sys.exit() out = open(output_filename, 'w') num_region = 0 species_summary = {} total_length = 0 #loop through interval file for num_region, region in enumerate( bx.intervals.io.NiceReaderWrapper( open( input_interval_filename, 'r' ), chrom_col = chr_col, start_col = start_col, end_col = end_col, fix_strand = True, return_header = False, return_comments = False ) ): src = "%s.%s" % ( dbkey, region.chrom ) region_length = region.end - region.start total_length += region_length coverage = { dbkey: BitSet( region_length ) } for block in maf_utilities.get_chopped_blocks_for_region( index, src, region, force_strand='+' ): #make sure all species are known for c in block.components: spec = c.src.split( '.' )[0] if spec not in coverage: coverage[spec] = BitSet( region_length ) start_offset, alignment = maf_utilities.reduce_block_by_primary_genome( block, dbkey, region.chrom, region.start ) for i in range( len( alignment[dbkey] ) ): for spec, text in alignment.items(): if text[i] != '-': coverage[spec].set( start_offset + i ) if summary: #record summary for key in coverage.keys(): if key not in species_summary: species_summary[key] = 0 species_summary[key] = species_summary[key] + coverage[key].count_range() else: #print coverage for interval coverage_sum = coverage[dbkey].count_range() out.write( "%s\t%s\t%s\t%s\n" % ( "\t".join( region.fields ), dbkey, coverage_sum, region_length - coverage_sum ) ) keys = coverage.keys() keys.remove( dbkey ) keys.sort() for key in keys: coverage_sum = coverage[key].count_range() out.write( "%s\t%s\t%s\t%s\n" % ( "\t".join( region.fields ), key, coverage_sum, region_length - coverage_sum ) ) if summary: out.write( "#species\tnucleotides\tcoverage\n" ) for spec in species_summary: out.write( "%s\t%s\t%.4f\n" % ( spec, species_summary[spec], float( species_summary[spec] ) / total_length ) ) out.close() print "%i regions were processed with a total length of %i." % ( num_region, total_length ) maf_utilities.remove_temp_index_file( index_filename )
def __main__(): index = index_filename = None mincols = 0 #Parse Command Line options, args = doc_optparse.parse( __doc__ ) if options.dbkey: dbkey = options.dbkey else: dbkey = None if dbkey in [None, "?"]: print >>sys.stderr, "You must specify a proper build in order to extract alignments. You can specify your genome build by clicking on the pencil icon associated with your interval file." sys.exit() species = maf_utilities.parse_species_option( options.species ) if options.chromCol: chromCol = int( options.chromCol ) - 1 else: print >>sys.stderr, "Chromosome column not set, click the pencil icon in the history item to set the metadata attributes." sys.exit() if options.startCol: startCol = int( options.startCol ) - 1 else: print >>sys.stderr, "Start column not set, click the pencil icon in the history item to set the metadata attributes." sys.exit() if options.endCol: endCol = int( options.endCol ) - 1 else: print >>sys.stderr, "End column not set, click the pencil icon in the history item to set the metadata attributes." sys.exit() if options.strandCol: strandCol = int( options.strandCol ) - 1 else: strandCol = -1 if options.interval_file: interval_file = options.interval_file else: print >>sys.stderr, "Input interval file has not been specified." sys.exit() if options.output_file: output_file = options.output_file else: print >>sys.stderr, "Output file has not been specified." sys.exit() #Finish parsing command line #Open indexed access to MAFs if options.mafType: if options.indexLocation: index = maf_utilities.maf_index_by_uid( options.mafType, options.indexLocation ) else: index = maf_utilities.maf_index_by_uid( options.mafType, options.mafIndexFile ) if index is None: print >> sys.stderr, "The MAF source specified (%s) appears to be invalid." % ( options.mafType ) sys.exit() elif options.mafFile: index, index_filename = maf_utilities.open_or_build_maf_index( options.mafFile, options.mafIndex, species = [dbkey] ) if index is None: print >> sys.stderr, "Your MAF file appears to be malformed." sys.exit() else: print >>sys.stderr, "Desired source MAF type has not been specified." sys.exit() #Create MAF writter out = bx.align.maf.Writer( open(output_file, "w") ) #Iterate over input regions num_blocks = 0 num_regions = None for num_regions, region in enumerate( bx.intervals.io.NiceReaderWrapper( open( interval_file, 'r' ), chrom_col = chromCol, start_col = startCol, end_col = endCol, strand_col = strandCol, fix_strand = True, return_header = False, return_comments = False ) ): src = "%s.%s" % ( dbkey, region.chrom ) for block in maf_utilities.get_chopped_blocks_for_region( index, src, region, species, mincols ): out.write( block ) num_blocks += 1 #Close output MAF out.close() #remove index file if created during run maf_utilities.remove_temp_index_file( index_filename ) if num_blocks: print "%i MAF blocks extracted for %i regions." % ( num_blocks, ( num_regions + 1 ) ) elif num_regions is not None: print "No MAF blocks could be extracted for %i regions." % ( num_regions + 1 ) else: print "No valid regions have been provided."