-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_functions.py
More file actions
113 lines (94 loc) · 3.64 KB
/
Copy pathutil_functions.py
File metadata and controls
113 lines (94 loc) · 3.64 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
from random import random
from re import finditer
from collections import defaultdict
from Config import PEPTIDES_FN, PSEUDO_COUNTS, MC_SAMPLING_DEPTH
from Peptide import Peptide
def getSeqListFromFasta(fn):
seqList = []
seqInProgress = ""
for line in open(fn):
if line.startswith(">"):
if seqInProgress != "":
seqList.append(seqInProgress)
seqInProgress = ""
else:
seqInProgress += line.strip()
if seqInProgress != "":
seqList.append(seqInProgress)
return seqList
def sampleFromFreqDict(freqDict):
aas = list(freqDict.keys())
thresholds = []
threshold = 0
for aa in aas:
threshold += freqDict[aa]
thresholds.append(threshold)
assert thresholds[-1] == sum(freqDict.values())
draw = random() * thresholds[-1]
for index, t in enumerate(thresholds):
if draw < t:
return aas[index]
raise
def getOutputFile(fn):
outFile = open(fn,'w')
outFile.write("Pseudo-count: " + f'{PSEUDO_COUNTS:.2f}' +'\n')
outFile.write("Peptides: " + PEPTIDES_FN + '\n')
outFile.write("Simulation depth: " + str(MC_SAMPLING_DEPTH) + '\n')
outFile.write("spm means seq per million (estimated probability times a million)\n")
return outFile
def get_sampled_seq_counts(mm):
sampledSeqCounts = defaultdict(int)
for i in range(MC_SAMPLING_DEPTH):
sampledSeqCounts[mm.sample()] += 1
sortedSampledSeqCounts = sorted(sampledSeqCounts.items(), key=lambda x: x[1], reverse=True)
return sortedSampledSeqCounts
def modify_to_non_deaminated_version(sortedSampledSeqCounts):
for i, (s, c) in enumerate(sortedSampledSeqCounts):
if s[3] == "E":
sl = list(sortedSampledSeqCounts[i][0])
sl[3] = "Q"
sortedSampledSeqCounts[i] = list(sortedSampledSeqCounts[i])
sortedSampledSeqCounts[i][0] = "".join(sl)
sortedSampledSeqCounts[i] = tuple(sortedSampledSeqCounts[i])
if s[5] == "E":
sl = list(sortedSampledSeqCounts[i][0])
sl[5] = "Q"
sortedSampledSeqCounts[i] = list(sortedSampledSeqCounts[i])
sortedSampledSeqCounts[i][0] = "".join(sl)
sortedSampledSeqCounts[i] = tuple(sortedSampledSeqCounts[i])
sampleSeqCountDict = defaultdict(int)
for s, c in sortedSampledSeqCounts:
sampleSeqCountDict[s] += c
sortedMergedIfBecomeEqualSampledSeqCounts = sorted(sampleSeqCountDict.items(), key=lambda x: x[1], reverse=True)
return sortedMergedIfBecomeEqualSampledSeqCounts
def read_peptides():
peptides = []
deamPositions = defaultdict(int)
for line in open(PEPTIDES_FN):
name, pep = line.strip().split()
peptides.append(Peptide(pep))
deamPositions[tuple([x.start() for x in finditer("[.]", peptides[-1].base_seq)])] += 1
return peptides
def extend_seqs(seq_list, db_seqs, flank_size):
extensions = {}
for seq in seq_list:
extensions[seq] = []
for db_seq in db_seqs:
for hit in finditer(seq, db_seq):
start = max(0, hit.start()-flank_size)
end = min(len(db_seq), hit.end()+flank_size)
extensions[seq].append(db_seq[start:end])
return extensions
def check_alpha_gamma(queries, fasta_db_fn):
entries = {}
for line in open(fasta_db_fn):
if line.startswith(">"):
entries[line.strip()] = ""
lastKey = line.strip()
else:
entries[lastKey] += line.strip()
for query in queries:
chains = set()
for id,seq in entries.items():
if query in seq:
chains.add(id.split(" ")[1])