annotate tools/encode/split_by_partitions.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -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 #Original script from /home/james/work/encode/feature_partitions/split_by_partitions.py
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 #Usage: python(2.4) split_by_partitions.py partition_index in_file out_file chrCol startCol endCol strandCol
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 from __future__ import division
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 from bx.bitset import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 from bx.bitset_builders import *
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 stop_err( msg ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 sys.stderr.write( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 GALAXY_DATA_INDEX_DIR = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 partition_index = '%s/encode_feature_partitions/partition_list.txt' % GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 partition_offset = "%s/encode_feature_partitions/" % GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 warnings = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 # Load up the partitions
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 partitions = list()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 for line in open( partition_index ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 name, score, filename = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 partitions.append( ( name, score, binned_bitsets_from_file( open( partition_offset+filename ) ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 stop_err( "Error loading partitioning dataset." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 in_file = open( sys.argv[2] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 stop_err( "Bad input data." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 out_file = open( sys.argv[3], "w" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 stop_err( "Bad output file." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 chrCol = int( sys.argv[4] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 stop_err( "Bad chr column: %s" % ( str( sys.argv[4] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 startCol = int( sys.argv[5] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 stop_err( "Bad start column: %s" % ( str( sys.argv[5] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 endCol = int( sys.argv[6] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 stop_err( "Bad end column: %s" % ( str( sys.argv[6] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 strandCol = int( sys.argv[7] )-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 strandCol = -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 line_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 first_invalid_line = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 invalid_line = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 for line in in_file:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 line_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 chr, start, end = fields[chrCol], int( fields[startCol] ), int( fields[endCol] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 if first_invalid_line is None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 first_invalid_line = line_count
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 invalid_line = line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 label = "input_line_" + str( line_count ) #if input file type was known to be bed, then could guess at label column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if strandCol < 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 strand = "+"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 strand = fields[strandCol]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 strand = "+"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 # Find which partition it overlaps
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 overlap = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 for name, score, bb in partitions:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 # Is there at least 1bp overlap?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if chr in bb:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 overlap = bb[chr].count_range( start, end-start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if overlap > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 # No overlap with any partition? For now throw this since the
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 # partitions tile the encode regions completely, indicate an interval
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 # that does not even overlap an encode region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 warning = "warning: Interval (%s, %d, %d) does not overlap any partition" % ( chr, start, end ) + ", line[" + str( line_count ) + "]. "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 warnings.append( warning )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 name = "no_overlap"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 score = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 # Annotate with the name of the partition
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 frac_overlap = overlap / ( end-start )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 # BED6 plus?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 print >>out_file, "%s\t%d\t%d\t%s\t%s\t%s\t%s\t%0.4f" % ( chr, start, end, label, score, strand, name, frac_overlap )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 in_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 stop_err( "Unknown error while processing line # %d: %s" % ( line_count, line ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 out_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 in_file.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 if warnings:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 warn_msg = "This tool is useful on ENCODE regions only, %d warnings, 1st is: " % len( warnings )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 warn_msg += warnings[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 print warn_msg
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 if skipped_lines:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 print "Skipped %d invalid lines starting at line # %d: %s" % ( skipped_lines, first_invalid_line, invalid_line )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 if __name__ == "__main__": main()