annotate build_ucsc_custom_track.py @ 1:760f588e8a26 draft

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