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