-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnick_detect_write.py
More file actions
192 lines (181 loc) · 9.31 KB
/
nick_detect_write.py
File metadata and controls
192 lines (181 loc) · 9.31 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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
This file is for nick identification for the writing experiment on "ILLINOIS". The sequencing files are too large
to be included here. The link to those files will be provided if requested. Different decision rules can be
explored to improve the identification performance. The rule here is just an example.
Usage:
python nick_detect_write.py
"""
import time
from Bio import SeqIO
# find the positions of all substrings
def find_all(sub, s):
index_list = []
index = s.find(sub)
while index != -1:
index_list.append(index)
index = s.find(sub, index + 1)
if len(index_list) > 0:
return index_list
else:
return -1
# count the mismatched positions
def mismatch_count(a, b):
return sum(1 for x, y in zip(a, b) if x != y)
if __name__ == '__main__':
start = time.time()
for pool_num in [1, 2, 3, 4, 5, 6, 7, 8]:
# Sequencing results for erasure experiment
if pool_num == 1:
filePath = '../sequencing_results/write_pool/I_1_CGATGTAT_L001_R1_001.fastq'
elif pool_num == 2:
filePath = '../sequencing_results/write_pool/L_2_TGACCAAT_L001_R1_001.fastq'
elif pool_num == 3:
filePath = '../sequencing_results/write_pool/L_3_ACAGTGAT_L001_R1_001.fastq'
elif pool_num == 4:
filePath = '../sequencing_results/write_pool/I_4_GCCAATAT_L001_R1_001.fastq'
elif pool_num == 5:
filePath = '../sequencing_results/write_pool/N_5_CAGATCAT_L001_R1_001.fastq'
elif pool_num == 6:
filePath = '../sequencing_results/write_pool/O_6_CTTGTAAT_L001_R1_001.fastq'
elif pool_num == 7:
filePath = '../sequencing_results/write_pool/I_7_AGTCAACA_L001_R1_001.fastq'
elif pool_num == 8:
filePath = '../sequencing_results/write_pool/S_8_AGTTCCGT_L001_R1_001.fastq'
# This is the path to groundtruth oligos in that pool
poolPath = '../oligo_pools/Level ' + str(pool_num - 1) + '.txt'
with open(poolPath, 'r') as f:
ori_lines = f.read().splitlines()
count = [0] * 7
nicked_lines = []
line_count = 1
for rec in SeqIO.parse(filePath, 'fastq'):
nicked_lines.append(rec.seq._data)
eps_1 = 9
eps_2 = 9
for line in nicked_lines:
break_flag = False
# enzyme 1 Nb.BtsI
if line.startswith('CACTGC'):
for gt_line in ori_lines:
pattern_pos = find_all('CACTGC', gt_line)
if pattern_pos != -1:
for pos in pattern_pos:
if mismatch_count(line, gt_line[pos: min(pos + len(line), len(gt_line))]) <= eps_1 \
and len(line) <= len(gt_line) - pos:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[max(0, pos - len(tmp_line)): pos]) <= len(tmp_line)/3 \
and len(tmp_line) <= pos:
count[0] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 2 Nt.BstNBI
elif line.endswith('GAGTC'):
for gt_line in ori_lines:
pattern_pos = find_all('GAGTC', gt_line)
if pattern_pos != -1:
for pos in pattern_pos:
if mismatch_count(line, gt_line[max(0, pos + 5 - len(line)): pos + 5]) <= eps_1 \
and len(line) <= pos + 5:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[pos + 5: min(pos + 5 + len(tmp_line), len(gt_line))]) <= len(tmp_line)/3 \
and len(tmp_line) <= len(gt_line) - pos - 5:
count[1] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 3 Nb.BssSI
elif line.startswith('TCGTG'):
for gt_line in ori_lines:
pattern_pos = find_all('CTCGTG', gt_line)
if pattern_pos != -1:
sub_pos = [i + 1 for i in pattern_pos]
for pos in sub_pos:
if mismatch_count(line, gt_line[pos: min(pos + len(line), len(gt_line))]) <= eps_1 \
and len(line) <= len(gt_line) - pos:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[max(0, pos - len(tmp_line)): pos]) <= len(tmp_line)/3 \
and len(tmp_line) <= pos:
count[2] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 4 Nt.AIwI
elif line[-9:-4] == 'GGATC':
for gt_line in ori_lines:
pattern_pos = find_all('GGATC', gt_line)
if pattern_pos != -1:
for pos in pattern_pos:
if mismatch_count(line, gt_line[max(0, pos + 9 - len(line)): pos + 9]) <= eps_1 \
and len(line) <= pos + 9:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[pos + 9: min(pos + 9 + len(tmp_line), len(gt_line))]) <= len(tmp_line)/3 \
and len(tmp_line) <= len(gt_line) - pos - 9:
count[3] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 5 Nt.BsmAI
elif line[-6:-1] == 'GTCTC':
for gt_line in ori_lines:
pattern_pos = find_all('GTCTC', gt_line)
if pattern_pos != -1:
for pos in pattern_pos:
if mismatch_count(line, gt_line[max(0, pos + 6 - len(line)): pos + 6]) <= eps_1 \
and len(line) <= pos + 6:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[pos + 6: min(pos + 6 + len(tmp_line), len(gt_line))]) <= len(tmp_line)/2.5 \
and len(tmp_line) <= len(gt_line) - pos - 6:
count[4] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 6 Nb.BsmI
elif line.startswith('CATTC'):
for gt_line in ori_lines:
pattern_pos = find_all('GCATTC', gt_line)
if pattern_pos != -1:
sub_pos = [i + 1 for i in pattern_pos]
for pos in sub_pos:
if mismatch_count(line, gt_line[pos: min(pos + len(line), len(gt_line))]) <= eps_1 \
and len(line) <= len(gt_line) - pos:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[max(0, pos - len(tmp_line)): pos]) <= len(tmp_line)/3 \
and len(tmp_line) <= pos:
count[5] += 1
break_flag = True
break
break
if break_flag:
break
# enzyme 7 Nb.BsrDI
elif line.startswith('CATTGC'):
for gt_line in ori_lines:
pattern_pos = find_all('CATTGC', gt_line)
if pattern_pos != -1:
for pos in pattern_pos:
if mismatch_count(line, gt_line[pos: min(pos + len(line), len(gt_line))]) <= eps_1 \
and len(line) <= len(gt_line) - pos:
for tmp_line in nicked_lines:
if mismatch_count(tmp_line, gt_line[max(0, pos - len(tmp_line)): pos]) <= len(tmp_line)/3 \
and len(tmp_line) <= pos:
count[6] += 1
break_flag = True
break
break
if break_flag:
break
print(pool_num, ':', count)
print('Time used:', time.time() - start)