Repository navigation
Expand file tree
/
Copy pathsampleOverlapScoreTry.py
More file actions
267 lines (235 loc) · 9.53 KB
/
Copy pathsampleOverlapScoreTry.py
File metadata and controls
267 lines (235 loc) · 9.53 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 29 14:26:16 2018
@author: samhitapn
@purpose: Overlap score trials
@modified: 12.April.2018
"""
#import biopython
from Bio import pairwise2
from Bio import SeqIO as sio
import numpy as np
import math as mt
import itertools
from time import time
import re
nt = ["A","T","C","G"] # Bases
ntPattern = re.compile("[ATGC]")
gapPattern = re.compile("[-]")
"""
@Definition: For calculating the probaility of the base at a given position
@Input parameters: X -> base A/C/T/G; b -> base in the particular position; q -> quality score at the position
@Output parameters: Probability
"""
def probabilityQ (X, b, p):
if X == b:
qp = 1 - p
else:
qp = p/3
return qp
"""
@Definition: For calculating the probaility from the phred quality scores
@Input parameters:
@Output parameters: Probability
"""
def getProbQuality (q):
#print(q)
#q = int(q)
p = 10**(-q/10)
return p
"""
@Definition:
@Input parameters:
@Output parameters: Probability
"""
def findOverlapRegion (read1, read2):
read1_start = min(read1.find("A"),read1.find("T"),read1.find("G"),read1.find("C"))
read2_start = min(read2.find("A"),read2.find("T"),read2.find("G"),read2.find("C"))
read1_end = max(read1.rfind("A"),read1.rfind("T"),read1.rfind("G"),read1.rfind("C"))
read2_end = max(read2.rfind("A"),read2.rfind("T"),read2.rfind("G"),read2.rfind("C"))
print(read1_start, read1_end, read2_start, read2_end)
overlapStart = max(read1_start, read2_start)
overlapEnd = min(read1_end, read2_end)
return(overlapStart, overlapEnd)
"""
@Definition:
@Input parameters:
@Output parameters: Probability
"""
def reorderScores (read, score):
i = 0
while i < len(read):
if read[i] == "-":
score.insert(i,"-")
i = i + 1
return(score)
"""
@Definition:
@Input parameters:
@Output parameters: Probability
"""
def getGapRegionScore (read, score, pos):
start = pos
end = ntPattern.search(read,start).start()
#print(start,end)
lenGap = end - start
#print(start, end, lenGap)
if lenGap > 1:
probSum = 0
#probProd = 0
while start < end:
#print(start,end,lenGap)
if score[start] != "-":
probSum = probSum + (10/13 * getProbQuality(np.float128(score[start]))) + (3/13 * (1 - getProbQuality(np.float128(score[start]))))
#probProd = probProd + (10/13 * getProbQuality(np.float128(score[start]))) * (3/13 * (1 - getProbQuality(np.float128(score[start]))))
start = start + 1
else:
start = start + 1
probGapSum = probSum/lenGap
#probGapProd = probProd/lenGap
else:
#print("ELSE")
#print(start,end,lenGap)
if score[start] != "-":
probGapSum = (10/13 * getProbQuality(np.float128(score[start]))) + (3/13 * (1 - getProbQuality(np.float128(score[start]))))
#probGapProd = (10/13 * getProbQuality(np.float128(score[start]))) * (3/13 * (1 - getProbQuality(np.float128(score[start]))))
#return(probGapSum, probGapProd, end)
return(probGapSum, end)
"""
@Definition:
@Input parameters:
@Output parameters:
"""
def overalpScoreCalculation(seqDetails):
# Initiating required values
probabilityOverallSum = 1
#probabilityOverallProd = 1
# Getting the sequence and scores
seqRead1 = seqDetails.split(" ")[0]
scoreRead1 = seqDetails.split(" ")[1].split(",")
seqRead2 = seqDetails.split(" ")[2]
scoreRead2 = seqDetails.split(" ")[3].split(",")
# reordering the score positions based on alignment
scoreRead1 = reorderScores(seqRead1,scoreRead1)
scoreRead2 = reorderScores(seqRead2,scoreRead2)
# Finding the overlap region
overlapRegion = findOverlapRegion(seqRead1, seqRead2)
startOverlap = overlapRegion[0]
endOverlap = overlapRegion[1]
L = endOverlap - startOverlap
print(startOverlap, endOverlap, L)
for i in range(startOverlap, endOverlap):
print(1, i, seqRead1[i], scoreRead1[i])
print(2, i, seqRead2[i], scoreRead2[i])
i = i + 1
"""
# Finding the overlap region
overlapRegion = findOverlapRegion(seqRead1, seqRead2)
startOverlap = overlapRegion[0]
endOverlap = overlapRegion[1]
L = endOverlap - startOverlap
print(startOverlap, endOverlap, L)
#outFile = open("scoreData.txt","w+")
while startOverlap <= endOverlap:
probabilityBaseSum = 0
#probabilityBaseProd = 0
#New code -> To include indels Option1
# Gap in first read -> calculation based on read 2
print(seqRead1[startOverlap],scoreRead1[startOverlap],seqRead2[startOverlap],scoreRead2[startOverlap])
if seqRead1[startOverlap] == "-":
pl = 1
#print("BEFORE")
#print(pl,startOverlap)
#print(scoreRead2[startOverlap])
#probabilityBase = (3/13 * getProbQuality(float(scoreRead2[startOverlap]))) + (10/13 * (1 - getProbQuality(float(scoreRead2[startOverlap]))))
#probabilityBase = (10/13 * float(scoreRead2[startOverlap])) + (3/13 * (1 - float(scoreRead2[startOverlap])))
gapDetails = getGapRegionScore(seqRead1,scoreRead2,startOverlap)
probabilityBaseSum = gapDetails[0]
#probabilityBaseProd = gapDetails[1]
startOverlap = gapDetails[1]
# Gap in second read -> calculation based on read 1
elif seqRead2[startOverlap] == "-":
pl = 2
#print("BEFORE")
#print(pl,startOverlap)
#print(scoreRead1[startOverlap])
#probabilityBase = (10/13 * float(scoreRead1[startOverlap])) + (3/13 * (1 - float(scoreRead1[startOverlap])))
#end = ntPattern.search(seqRead2,startOverlap).start()
#print(end)
#gapDetails = getGapRegionScore(seqRead1,scoreRead1,startOverlap)
gapDetails = getGapRegionScore(seqRead2,scoreRead1,startOverlap)
probabilityBaseSum = gapDetails[0]
#probabilityBaseProd = gapDetails[1]
startOverlap = gapDetails[1]
#print("AFTER")
#print(pl, startOverlap)
# Existing score calculation
else:
pl = 3
#print("BEFORE")
#print(pl,startOverlap)
for n in nt:
#print(n)
probabilityBaseSum = probabilityBaseSum + (probabilityQ(n,seqRead1[startOverlap],getProbQuality(np.float128(scoreRead1[startOverlap]))) * probabilityQ(n,seqRead2[startOverlap],getProbQuality(np.float128(scoreRead2[startOverlap]))))
#probabilityBaseProd = probabilityBaseProd + (probabilityQ(n,seqRead1[startOverlap],getProbQuality(np.float128(scoreRead1[startOverlap]))) * probabilityQ(n,seqRead2[startOverlap],getProbQuality(np.float128(scoreRead2[startOverlap]))))
#probabilityBase = probabilityBase + (probabilityQ(n,seqRead1[startOverlap],float(scoreRead1[startOverlap])) * probabilityQ(n,seqRead2[startOverlap],float(scoreRead2[startOverlap])))
startOverlap = startOverlap + 1
#print("AFTER")
#print(pl, startOverlap)
#print("$$$$$$$$$$$$$$$$$$$$$$$$$$")
#print("I am in %f", pl)
#print(probabilityBase)
probabilityOverallSum = probabilityOverallSum * probabilityBaseSum
#probabilityOverallProd = probabilityOverallProd * probabilityBaseProd
print(seqDetails.split(" ")[4], startOverlap-1, probabilityOverallSum, pl)
# Overlap score
overlapScoreSum = probabilityOverallSum ** 1/L
#overlapScoreProd = probabilityOverallProd ** 1/L
#print(overlapScore)
#outFile.close()
return (overlapScoreSum)
"""
# MAIN
sequences = sio.to_dict(sio.parse("data/Sample_AllReads.fastq","fastq"))
print("^^^^^^SEQUENCES READ^^^^^^\n")
overlapCount = 1
alignment = {}
# Get the overlap pairs and details from the PAF files
with open("data/Sample_AllReads_Overlaps.paf","r") as f:
for ovlPair in f.readlines():
if overlapCount <= 5:
#print(ovlPair)
#print("^^^^^^^^^^^^^^^^^^^^")
ovl = ovlPair.split("\t")
if ovl[0] != ovl[5]:
#print(overlapCount)
start = time()
al = pairwise2.align.globalxx(sequences[ovl[0]].seq,sequences[ovl[5]].seq)
stop = time()
print(stop - start)
alignment[ovl[0] + ' ' + ovl[5]] = al[0][0] + " " + str(sequences[ovl[0]].letter_annotations["phred_quality"]).strip('[]').replace(" ","") + " " + al[0][1] + " " + str(sequences[ovl[0]].letter_annotations["phred_quality"]).strip('[]').replace(" ","") + " " + str(overlapCount)
overlapCount = overlapCount + 1
#print(al[0][0])
#print(al[0][1])
#print("$$$$$$$$$$$$$$$$$$")
else:
break
#print(alignment)
# Getting the scores for each overlap pair
for key in alignment:
#print(alignment[key].split(" ")[0])
#print(alignment[key].split(" ")[2])
print(key)
if alignment[key].split(" ")[4] == str(4):
#print(alignment[key].split(" ")[0])
#print(alignment[key].split(" ")[1])
#print(alignment[key].split(" ")[2])
#print(alignment[key].split(" ")[3])
#print(alignment[key])
overlapScore = overalpScoreCalculation(alignment[key])
#alignment[key] = alignment[key] + " " + str(overlapScore[0]) + " " + str(overlapScore[1])
#alignment[key] = alignment[key] + " " + str(overlapScore)
#print(key)
#print(alignment[key].split(" ")[5])
print("############")