comparison build_ucsc_custom_track.py @ 0:618e56c3109b draft

Imported from capsule None
author devteam
date Mon, 28 Jul 2014 11:55:41 -0400
parents
children 3d87079756e1
comparison
equal deleted inserted replaced
-1:000000000000 0:618e56c3109b
1 #!/usr/bin/env python
2 """
3 Build a UCSC genome browser custom track file
4 """
5
6 import sys, os
7
8 assert sys.version_info[:2] >= ( 2, 4 )
9
10 def stop_err( msg ):
11 sys.stderr.write( msg )
12 sys.exit()
13
14 FILE_TYPE_TO_TRACK_TYPE = { 'bed': None, 'bedstrict': None, 'bed6': None, 'bed12': None, 'bedgraph':'bedGraph', 'wig':'wiggle_0' }
15 CHUNK_SIZE = 2**20 #1mb
16
17 def get_track_line_is_interval( file_type, name, description, color, visibility ):
18 if file_type in FILE_TYPE_TO_TRACK_TYPE:
19 track_type = FILE_TYPE_TO_TRACK_TYPE[ file_type ]
20 is_interval = False
21 else:
22 track_type = None
23 is_interval = True
24 track_line = 'track '
25 if track_type:
26 track_line += 'type=%s ' % ( track_type )
27 track_line += 'name="%s" description="%s" color=%s visibility=%s\n' % ( name, description, color, visibility )
28 return track_line, is_interval
29
30 args = sys.argv[1:]
31
32 out_fname = args.pop(0)
33 out = open( out_fname, "w" )
34
35 num_tracks = 0
36 skipped_lines = 0
37 first_invalid_line = 0
38 while args:
39 # Suck in one dataset worth of arguments
40 in_fname = args.pop(0)
41 file_type = args.pop(0)
42 colspec = args.pop(0)
43 name = args.pop(0)
44 description = args.pop(0)
45 color = args.pop(0).replace( '-', ',' )
46 visibility = args.pop(0)
47 track_line, is_interval = get_track_line_is_interval( file_type, name, description, color, visibility )
48 # Do the work
49 in_file = open( in_fname )
50 out.write( track_line )
51 if not is_interval:
52 while True:
53 chunk = in_file.read( CHUNK_SIZE )
54 if chunk:
55 out.write( chunk )
56 else:
57 break
58 else:
59 # Assume type is interval (don't pass this script anything else!)
60 try:
61 c, s, e, st = [ int( x ) - 1 for x in colspec.split( "," ) ]
62 except:
63 try:
64 c, s, e = [ int( x ) - 1 for x in colspec.split( "," )[:3] ]
65 st = -1 #strand column is absent
66 except:
67 stop_err( "Columns in interval file invalid for UCSC custom track." )
68
69 i = 0
70 for i, line in enumerate( in_file ):
71 line = line.rstrip( '\r\n' )
72 if line and not line.startswith( '#' ):
73 fields = line.split( "\t" )
74 if st > 0:
75 #strand column is present
76 try:
77 out.write( "%s\t%s\t%s\t%d\t0\t%s\n" % ( fields[c], fields[s], fields[e], i, fields[st] ) )
78 except:
79 skipped_lines += 1
80 if not first_invalid_line:
81 first_invalid_line = i+1
82 else:
83 try:
84 out.write( "%s\t%s\t%s\n" % ( fields[c], fields[s], fields[e] ) )
85 except:
86 skipped_lines += 1
87 if not first_invalid_line:
88 first_invalid_line = i+1
89 out.write( "\n" ) #separating newline
90 num_tracks += 1
91
92 out.close()
93
94 print "Generated a custom track containing %d subtracks." % num_tracks
95 if skipped_lines:
96 print "Skipped %d invalid lines starting at #%d" % ( skipped_lines, first_invalid_line )
97
98
99