annotate tools/maf/maf_stats.py @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 #Dan Blankenberg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Reads a list of intervals and a maf. Outputs a new set of intervals with statistics appended.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import bx.intervals.io
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from bx.bitset import BitSet
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 from galaxy.tools.util import maf_utilities
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 def __main__():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 maf_source_type = sys.argv.pop( 1 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 input_maf_filename = sys.argv[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 input_interval_filename = sys.argv[2].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 output_filename = sys.argv[3].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 dbkey = sys.argv[4].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 chr_col = int( sys.argv[5].strip() ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 start_col = int( sys.argv[6].strip() ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 end_col = int( sys.argv[7].strip() ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 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."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 summary = sys.argv[8].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 if summary.lower() == "true": summary = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 else: summary = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 mafIndexFile = "%s/maf_index.loc" % sys.argv[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 maf_index_filename = sys.argv[10].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 maf_index_filename = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 index = index_filename = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 if maf_source_type == "user":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 #index maf for use here
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 index, index_filename = maf_utilities.open_or_build_maf_index( input_maf_filename, maf_index_filename, species = [dbkey] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 if index is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 print >>sys.stderr, "Your MAF file appears to be malformed."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 elif maf_source_type == "cached":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 #access existing indexes
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 index = maf_utilities.maf_index_by_uid( input_maf_filename, mafIndexFile )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 if index is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 print >> sys.stderr, "The MAF source specified (%s) appears to be invalid." % ( input_maf_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 print >>sys.stdout, 'Invalid source type specified: %s' % maf_source_type
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 out = open(output_filename, 'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 num_region = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 species_summary = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 total_length = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 #loop through interval file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 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 ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 src = "%s.%s" % ( dbkey, region.chrom )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 region_length = region.end - region.start
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 total_length += region_length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 coverage = { dbkey: BitSet( region_length ) }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 for block in index.get_as_iterator( src, region.start, region.end ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 for spec in maf_utilities.get_species_in_block( block ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 if spec not in coverage: coverage[spec] = BitSet( region_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 for block in maf_utilities.iter_blocks_split_by_species( block ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if maf_utilities.component_overlaps_region( block.get_component_by_src( src ), region ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 #need to chop and orient the block
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 block = maf_utilities.orient_block_by_region( maf_utilities.chop_block_by_region( block, src, region ), src, region, force_strand = '+' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 start_offset, alignment = maf_utilities.reduce_block_by_primary_genome( block, dbkey, region.chrom, region.start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 for i in range( len( alignment[dbkey] ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 for spec, text in alignment.items():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if text[i] != '-':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 coverage[spec].set( start_offset + i )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 if summary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 #record summary
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 for key in coverage.keys():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if key not in species_summary: species_summary[key] = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 species_summary[key] = species_summary[key] + coverage[key].count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 #print coverage for interval
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 coverage_sum = coverage[dbkey].count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 out.write( "%s\t%s\t%s\t%s\n" % ( "\t".join( region.fields ), dbkey, coverage_sum, region_length - coverage_sum ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 keys = coverage.keys()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 keys.remove( dbkey )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 keys.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 for key in keys:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 coverage_sum = coverage[key].count_range()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 out.write( "%s\t%s\t%s\t%s\n" % ( "\t".join( region.fields ), key, coverage_sum, region_length - coverage_sum ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if summary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 out.write( "#species\tnucleotides\tcoverage\n" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 for spec in species_summary:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 out.write( "%s\t%s\t%.4f\n" % ( spec, species_summary[spec], float( species_summary[spec] ) / total_length ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 out.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 if num_region is not None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 print "%i regions were processed with a total length of %i." % ( num_region + 1, total_length )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 maf_utilities.remove_temp_index_file( index_filename )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 if __name__ == "__main__": __main__()