comparison bed_to_protein_map.py @ 0:4702e7f629bb draft default tip

planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/bed_to_protein_map commit 38e8d0e983c3aa314e13bdc9ea98f4a728b7772c-dirty
author jjohnson
date Mon, 20 Nov 2017 14:58:18 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4702e7f629bb
1 #!/usr/bin/env python
2 """
3 """
4 import sys
5 import os.path
6 import optparse
7
8 """
9 X 276352 291629 ENST00000430923 20 + 284187 291629 80,80,80 5 42,148,137,129,131 0,7814,12380,14295,15146
10 X 304749 318819 ENST00000326153 20 - 305073 318787 80,80,80 10 448,153,149,209,159,68,131,71,138,381 0,2610,2982,6669,8016,9400,10140,10479,12164,13689
11 grep ENST bed_to_protein_map.py | grep -v grep | ./bed_to_protein_map.py
12 """
13
14 def __main__():
15 #Parse Command Line
16 parser = optparse.OptionParser()
17 #I/O
18 parser.add_option( '-i', '--input', dest='input', default=None, help='Tabular file with peptide_sequence column' )
19 parser.add_option( '-c', '--column', type='int', dest='column', default=1, help='column ordinal with Ensembl transcript ID' )
20 parser.add_option( '-g', '--gtf', dest='gtf', default=None, help='GTF gene model file. Used to annotate NSJ peptide entries.')
21 parser.add_option( '-2', '--twobit', dest='twobit', default=None, help='Reference genome in UCSC twobit format')
22 parser.add_option( '-C', '--coding', dest='coding', action='store_true', default=False, help='Output coding BED line')
23 parser.add_option( '-o', '--output', dest='output', default=None, help='The output bed (else write to stdout)' )
24 parser.add_option( '-s', '--sqlitedb', dest='sqlitedb', default=None, help='The sqlitedb' )
25 parser.add_option( '--debug', dest='debug', action='store_true', default=False, help='Print debugging messages')
26 (options, args) = parser.parse_args()
27 ##INPUTS##
28 if options.input != None:
29 try:
30 inputPath = os.path.abspath(options.input)
31 inputFile = open(inputPath, 'r')
32 except Exception, e:
33 print >> sys.stderr, "failed: %s" % e
34 exit(2)
35 else:
36 inputFile = sys.stdin
37
38 if options.output != None:
39 try:
40 outputPath = os.path.abspath(options.output)
41 outputFile = open(outputPath, 'w')
42 except Exception, e:
43 print >> sys.stderr, "failed: %s" % e
44 exit(3)
45 else:
46 outputFile = sys.stdout
47
48 dbFile = None
49 if options.sqlitedb != None:
50 try:
51 dbPath = os.path.abspath(options.sqlitedb)
52 dbFile = open(dbPath, 'w')
53 except Exception, e:
54 print >> sys.stderr, "failed: %s" % e
55 exit(3)
56
57
58 try:
59 for linenum,line in enumerate(inputFile):
60 if options.debug:
61 print >> sys.stderr, "%d: %s\n" % (linenum,line)
62 if line.startswith('#'):
63 continue
64 if line.strip() == '':
65 continue
66 fields = line.rstrip('\r\n').split('\t')
67 if len(fields) < 12:
68 print >> sys.stderr, "%d: %s\n" % (linenum,line)
69 continue
70 (chrom,_chromStart,_chromEnd,name,score,strand,_thickStart,_thickEnd,itemRgb,_blockCount,blockSizes,blockStarts) = fields[0:12]
71 chromStart = int(_chromStart)
72 chromEnd = int(_chromEnd)
73 thickStart = int(_thickStart)
74 thickEnd = int(_thickEnd)
75 blockCount = int(_blockCount)
76 blockSizes = [int(x) for x in blockSizes.split(',')]
77 blockStarts = [int(x) for x in blockStarts.split(',')]
78 if strand == '+':
79 cds_start = 0
80 cds_end = 0
81 for i in range(blockCount):
82 start = chromStart + blockStarts[i]
83 end = start + blockSizes[i]
84 if end < thickStart:
85 continue
86 if start > thickEnd:
87 break
88 if start < thickStart:
89 start = thickStart
90 if end > thickEnd:
91 end = thickEnd
92 cds_end = cds_start + (end - start)
93 ##dbFile.write('%s\t%s\t%d\t%d\t%s\t%d\t%d\n' % (name,chrom,start,end,strand,cds_start,cds_end))
94 outputFile.write('%s\t%s\t%d\t%d\t%s\t%d\t%d\n' % (name,chrom,start,end,strand,cds_start,cds_end))
95 cds_start = cds_end
96 elif strand == '-':
97 cds_start = 0
98 cds_end = 0
99 for i in reversed(range(blockCount)):
100 start = chromStart + blockStarts[i]
101 end = start + blockSizes[i]
102 if end < thickStart:
103 break
104 if start > thickEnd:
105 continue
106 if start < thickStart:
107 start = thickStart
108 if end > thickEnd:
109 end = thickEnd
110 cds_end = cds_start + (end - start)
111 outputFile.write('%s\t%s\t%d\t%d\t%s\t%d\t%d\n' % (name,chrom,start,end,strand,cds_start,cds_end))
112 cds_start = cds_end
113 pass
114 except Exception, e:
115 print >> sys.stderr, "failed: %s" % e
116 exit(1)
117
118
119 if __name__ == "__main__" : __main__()
120