-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull.py
56 lines (45 loc) · 1.61 KB
/
full.py
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
# Inferring Peptide from Full Spectrum
from math import isclose
from collections import defaultdict
from .helpers import Parser
from .spec import match_mass
# Return a dictionary that let's us look up complementary ion pairs
# e.g. "PRO" and "TEIN" for full peptide "PROTEIN"
# We can find pairs, because their weights approximately sum to that
# of the peptide.
def find_pairs(peptide, ions):
pairs = {}
for w in ions:
for w2 in ions:
if isclose(w2 + w, peptide):
pairs[w] = w2
return pairs
# This builds a graph of possibly adjacent ions.
# ions can be considered adjacent in a graph if the difference in
# their masses is close to the mass of a known amino acid.
def weight_graph(ions):
graph = defaultdict(list)
for i in range(len(ions)):
for j in range(i + 1, len(ions)):
aa = match_mass(ions[j] - ions[i])
if aa:
graph[ions[i]] += [[ions[j], aa]]
return graph
def full(peptide, ions):
"""Inferring Peptide from Full Spectrum"""
def infer_peptide(w, seq, rm):
for w2, aa in graph[w]:
if w2 in rm:
continue
if len(seq) + 1 == target_len:
yield seq + aa
else:
yield from infer_peptide(w2, seq + aa, rm + [w, pairs[w]])
graph = weight_graph(ions)
pairs = find_pairs(peptide, ions)
target_len = int(len(ions) / 2 - 1)
w = min(ions)
return list(infer_peptide(w, "", [w, pairs[w]]))
def main(file):
weights = [float(x) for x in Parser(file).lines()]
print(*full(weights[0], weights[1:]), sep="\n")