-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpgx_bed.py
More file actions
executable file
·167 lines (150 loc) · 7.01 KB
/
pgx_bed.py
File metadata and controls
executable file
·167 lines (150 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sys
import time
def load_mapping(bed_full_path):
f = open(bed_full_path)
for l in f:
(chrom, start, stop, protein, dunno1, strand, dunno2, dunno3, dunno4, exons_count, exon_lengths, exon_starts) = l.strip().split()
if exon_lengths.endswith(","):
exon_lengths = exon_lengths[:-1]
if exon_starts.endswith(","):
exon_starts = exon_starts[:-1]
lengths = list(map(int, exon_lengths.split(",")))
starts = list(map(int, exon_starts.split(",")))
mapping[protein] = (chrom, int(start), int(stop), strand, protein, int(exons_count), lengths, starts)
f.close()
def process(peptide, mapping_entry, aa_start):
(chrom, start, stop, strand, protein, exon_count, lengths, starts) = mapping_entry
if strand == "+":
start_target = (aa_start - 1) * 3
tot_start = 0
feature_start = -1
#Otherwise IDE complains about curr_exon being potentially undefined...
curr_exon = -1
for curr_exon in range(exon_count):
tot_start = starts[curr_exon]
if lengths[curr_exon] > start_target:
feature_start = tot_start + start_target
break
else:
start_target -= lengths[curr_exon]
if feature_start < 0:
print("couldn't place %s in %s starting at #AA %d" % (peptide, protein, aa_start), file=sys.stderr)
return ""
end_target = len(peptide) * 3
block_count = 0
block_lengths = []
block_starts = []
overall_feature_start = start + feature_start
current_start = overall_feature_start
while end_target > (start + starts[curr_exon] + lengths[curr_exon] - current_start):
block_count += 1
block_starts.append(current_start-overall_feature_start)
# There will be iterations where this seems like a futile cycle...
block_lengths.append(start + starts[curr_exon] + lengths[curr_exon] - current_start)
end_target -= (start + starts[curr_exon] + lengths[curr_exon] - current_start)
curr_exon += 1
if curr_exon == exon_count:
print("couldn't place %s in %s starting at #AA %d" % (peptide, protein, aa_start), file=sys.stderr)
return ""
#This is the start of the next exon... i.e. starts[curr_exon] which looks redundant but wasn't
#on the first iteration... I'm sure this whole loop structure can be simplified...
current_start = start + starts[curr_exon]
block_count += 1
block_starts.append(current_start - overall_feature_start)
block_lengths.append(end_target)
feature_end = current_start+end_target-1
return "\t".join([chrom,
repr(overall_feature_start),
repr(feature_end+1),
peptide,
'1000',
strand,
repr(overall_feature_start),
repr(feature_end+1),
'0,255,0', repr(block_count),
",".join(map(str, block_lengths)),
",".join(map(str, block_starts))])
else:
start_target = (aa_start-1) * 3
tot_start = 0
feature_start = -1
#Otherwise IDE complains about curr_exon being potentially undefined...
curr_exon = -1
for curr_exon in range(exon_count-1, -1, -1):
tot_start = starts[curr_exon] + lengths[curr_exon]
if lengths[curr_exon] > start_target:
feature_start = tot_start - start_target
break
else:
start_target -= lengths[curr_exon]
if feature_start < 0:
print("couldn't place %s in %s starting at #AA %d" % (peptide, protein, aa_start), file=sys.stderr)
return ""
end_target = len(peptide) * 3
block_count = 0
block_lengths = []
block_starts = []
overall_feature_start = start + feature_start
current_start = overall_feature_start
while end_target > (current_start - start - starts[curr_exon]):
block_count += 1
block_starts = [starts[curr_exon]] + block_starts
# There will be iterations where this seems like a futile cycle...
block_lengths = [(current_start - start - starts[curr_exon])] + block_lengths
end_target -= (current_start - start - starts[curr_exon])
curr_exon -= 1
if curr_exon == -1:
print("couldn't place %s in %s starting at #AA %d" % (peptide, protein, aa_start), file=sys.stderr)
return ""
#This is the start of the next exon... i.e. starts[curr_exon] which looks redundant but wasn't
#on the first iteration... I'm sure this whole loop structure can be simplified...
current_start = start + starts[curr_exon] + lengths[curr_exon]
block_count += 1
feature_end = current_start-end_target
block_starts = [0] + list(map(lambda x: x-(feature_end-start), block_starts))
block_lengths = [end_target] + block_lengths
return "\t".join([chrom, repr(feature_end),
repr(overall_feature_start),
peptide,
'1000',
strand,
repr(feature_end),
repr(overall_feature_start),
'0,255,0',
repr(block_count),
",".join(map(str, block_lengths)),
",".join(map(str, block_starts))])
def batch_process(f):
print('track name=peptides description="Peptides identified by Mass Spectrometry" useScore=0 itemRgb="On"', file=sys.stdout)
for l in f:
vals = l.strip().split("\t")
peptide = vals[0]
protein = vals[1]
aa_start = int(vals[2])
lines = set()
if not protein in mapping:
print("Hit in a protein (%s) which is not mapped by the reference bed file of the '%s' proteome." % (protein, proteome[:-1]), file=sys.stderr)
continue
else:
retval = process(peptide, mapping[protein], aa_start)
if (retval == "") or (retval in lines):
continue
else:
lines.add(retval)
print(retval, file=sys.stdout)
if __name__ == "__main__":
mapping = {}
start_time = time.time()
proteome = sys.argv[1]
if len(sys.argv) == 3:
infile = open(sys.argv[2])
else:
infile = sys.stdin
if not proteome.endswith("/"):
proteome += "/"
load_mapping(proteome + "proteome.bed")
batch_process(infile)
# there is no harm in closing stdin... http://effbot.org/pyfaq/why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it.htm
infile.close()
end_time = time.time()
print("peptides mapped in %.3f seconds" % (end_time-start_time), file=sys.stderr)