|
a |
|
b/tests/test_taps.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
import unittest |
|
|
4 |
import pysam |
|
|
5 |
import os |
|
|
6 |
|
|
|
7 |
from singlecellmultiomics.molecule import TAPSNlaIIIMolecule, TAPS |
|
|
8 |
from singlecellmultiomics.fragment import NlaIIIFragment |
|
|
9 |
from singlecellmultiomics.utils import create_MD_tag |
|
|
10 |
|
|
|
11 |
from singlecellmultiomics.utils import complement |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
class TestTAPs(unittest.TestCase): |
|
|
15 |
|
|
|
16 |
def test_all(self): |
|
|
17 |
temp_folder = 'data' |
|
|
18 |
|
|
|
19 |
enable_ref_write=True |
|
|
20 |
|
|
|
21 |
|
|
|
22 |
ref_path = f'{temp_folder}/ref.fa' |
|
|
23 |
alignments_path = f'{temp_folder}/alignments.bam' |
|
|
24 |
|
|
|
25 |
if not os.path.exists(temp_folder): |
|
|
26 |
os.makedirs(temp_folder) |
|
|
27 |
# Create reference bam file |
|
|
28 |
|
|
|
29 |
refseq = 'TTAATCATGAAACCGTGGAGGCAAATCGGAGTGTAAGGCTTGACTGGATTCCTACGTTGCGTAGGTTCATGGGGGG' |
|
|
30 |
if enable_ref_write: |
|
|
31 |
with open(ref_path, 'w') as f: |
|
|
32 |
f.write(f">chr1\n{refseq}\n>chr2\n{complement(refseq)}\n""") |
|
|
33 |
|
|
|
34 |
# This command needs to finish, which is not working properly during testing |
|
|
35 |
pysam.faidx(ref_path) |
|
|
36 |
|
|
|
37 |
# CATG at base 5 |
|
|
38 |
# Create BAM file with NLA fragment: |
|
|
39 |
|
|
|
40 |
alignments_path_unsorted = f'{alignments_path}.unsorted.bam' |
|
|
41 |
with pysam.AlignmentFile(alignments_path_unsorted,'wb',reference_names=['chr1'],reference_lengths=[len(refseq)]) as bam: |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
### Nla III mate pair example, containing 2 CpGs and 1 call on the wrong strand |
|
|
45 |
read_A = pysam.AlignedSegment(bam.header) |
|
|
46 |
read_A.reference_name = 'chr1' |
|
|
47 |
read_A.reference_start = 5 |
|
|
48 |
# Before last A is a bogus G>A conversion to test strandness: |
|
|
49 |
read_A.query_sequence = 'CATGAAACCGTGGAGGCAAATTGGAGTAT' |
|
|
50 |
read_A.cigarstring = f'{len(read_A.query_sequence)}M' |
|
|
51 |
read_A.qual = 'A'*len(read_A.query_sequence) |
|
|
52 |
read_A.mapping_quality = 60 |
|
|
53 |
read_A.query_name = 'EX1_GA_CONV_2x_CpG_TAPS' |
|
|
54 |
read_A.set_tag('SM', 'Cell_A') |
|
|
55 |
read_A.is_read1 = True |
|
|
56 |
read_A.is_read2 = False |
|
|
57 |
read_A.set_tag('lh','TG') |
|
|
58 |
# Set substitution tag: |
|
|
59 |
read_A.set_tag('MD', |
|
|
60 |
create_MD_tag( |
|
|
61 |
refseq[read_A.reference_start:read_A.reference_end], read_A.query_sequence)) |
|
|
62 |
read_A.is_paired = True |
|
|
63 |
read_A.is_proper_pair = True |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
# Create a second read which is a mate of the previous |
|
|
67 |
read_B = pysam.AlignedSegment(bam.header) |
|
|
68 |
read_B.reference_name = 'chr1' |
|
|
69 |
read_B.reference_start = 25 |
|
|
70 |
read_B.query_sequence = refseq[25:60].replace('TGT','TAT').replace('CG', 'TG') |
|
|
71 |
read_B.cigarstring = f'{len(read_B.query_sequence)}M' |
|
|
72 |
read_B.qual = 'A'*len(read_B.query_sequence) |
|
|
73 |
read_B.mapping_quality = 60 |
|
|
74 |
read_B.is_read2 = True |
|
|
75 |
read_B.is_read1 = False |
|
|
76 |
read_B.is_reverse = True |
|
|
77 |
read_B.query_name = 'EX1_GA_CONV_2x_CpG_TAPS' |
|
|
78 |
read_B.set_tag('SM', 'Cell_A') |
|
|
79 |
read_B.set_tag('lh','TG') |
|
|
80 |
read_B.set_tag('MD', |
|
|
81 |
create_MD_tag(refseq[read_B.reference_start:read_B.reference_end], |
|
|
82 |
read_B.query_sequence, |
|
|
83 |
)) |
|
|
84 |
read_B.is_paired = True |
|
|
85 |
read_B.is_proper_pair = True |
|
|
86 |
|
|
|
87 |
read_A.next_reference_id = read_B.reference_id |
|
|
88 |
read_A.next_reference_start = read_B.reference_start |
|
|
89 |
read_B.next_reference_id = read_A.reference_id |
|
|
90 |
read_B.next_reference_start = read_A.reference_start |
|
|
91 |
|
|
|
92 |
read_A.mate_is_reverse = read_B.is_reverse |
|
|
93 |
read_B.mate_is_reverse = read_A.is_reverse |
|
|
94 |
|
|
|
95 |
bam.write(read_A) |
|
|
96 |
bam.write(read_B) |
|
|
97 |
|
|
|
98 |
### Nla III mate pair example, dove tailed over random primer |
|
|
99 |
# , containing 1 CpGs and one 1 call in the dove tail which should not be called |
|
|
100 |
read_C = pysam.AlignedSegment(bam.header) |
|
|
101 |
read_C.reference_name = 'chr1' |
|
|
102 |
read_C.reference_start = 5 |
|
|
103 |
read_C.query_sequence = 'CATGAAACCGTGGAGGC'.replace('ACC','ATC').replace('AGGC','CGGT') |
|
|
104 |
read_C.cigarstring = f'{len(read_C.query_sequence)}M' |
|
|
105 |
read_C.qual = 'A'*len(read_C.query_sequence) |
|
|
106 |
read_C.mapping_quality = 60 |
|
|
107 |
read_C.query_name = 'EX2_GA_DOVE' |
|
|
108 |
read_C.set_tag('SM', 'Cell_A') |
|
|
109 |
read_C.is_read1 = True |
|
|
110 |
read_C.set_tag('lh','TG') |
|
|
111 |
# Set substitution tag: |
|
|
112 |
read_C.set_tag('MD', |
|
|
113 |
create_MD_tag( |
|
|
114 |
refseq[read_C.reference_start:read_C.reference_end], |
|
|
115 |
read_C.query_sequence)) |
|
|
116 |
|
|
|
117 |
read_C.is_paired = True |
|
|
118 |
read_C.is_proper_pair = True |
|
|
119 |
|
|
|
120 |
|
|
|
121 |
# Create a second read which is a mate of the previous |
|
|
122 |
read_D = pysam.AlignedSegment(bam.header) |
|
|
123 |
read_D.reference_name = 'chr1' |
|
|
124 |
read_D.reference_start = 10 |
|
|
125 |
read_D.query_sequence = refseq[10:15].replace('ACC','GTC') |
|
|
126 |
read_D.cigarstring = f'{len(read_D.query_sequence)}M' |
|
|
127 |
read_D.qual = 'A'*len(read_D.query_sequence) |
|
|
128 |
read_D.mapping_quality = 60 |
|
|
129 |
read_D.is_read2 = True |
|
|
130 |
read_D.is_read1 = False |
|
|
131 |
read_D.is_reverse = True |
|
|
132 |
read_D.query_name = 'EX2_GA_DOVE' |
|
|
133 |
read_D.set_tag('SM', 'Cell_A') |
|
|
134 |
read_D.set_tag('lh','TG') |
|
|
135 |
read_D.set_tag('MD', |
|
|
136 |
create_MD_tag(refseq[read_D.reference_start:read_D.reference_end], |
|
|
137 |
read_D.query_sequence, |
|
|
138 |
)) |
|
|
139 |
read_D.is_paired = True |
|
|
140 |
read_D.is_proper_pair = True |
|
|
141 |
|
|
|
142 |
read_C.next_reference_id = read_D.reference_id |
|
|
143 |
read_C.next_reference_start = read_D.reference_start |
|
|
144 |
read_D.next_reference_id = read_C.reference_id |
|
|
145 |
read_D.next_reference_start = read_C.reference_start |
|
|
146 |
|
|
|
147 |
read_C.mate_is_reverse = read_D.is_reverse |
|
|
148 |
read_D.mate_is_reverse = read_C.is_reverse |
|
|
149 |
|
|
|
150 |
bam.write(read_C) |
|
|
151 |
bam.write(read_D) |
|
|
152 |
|
|
|
153 |
######################################## |
|
|
154 |
# Reverse dovetailed (2 way) alignment # |
|
|
155 |
######################################## |
|
|
156 |
|
|
|
157 |
read_E = pysam.AlignedSegment(bam.header) |
|
|
158 |
read_E.reference_name = 'chr1' |
|
|
159 |
read_E.query_sequence = refseq[2:71].replace('CATGAA','CATAAA').replace('CGG','CAG') |
|
|
160 |
read_E.reference_start = 71 - len(read_E.query_sequence) |
|
|
161 |
read_E.cigarstring = f'{len(read_E.query_sequence)}M' |
|
|
162 |
read_E.qual = 'A'*len(read_E.query_sequence) |
|
|
163 |
read_E.mapping_quality = 60 |
|
|
164 |
read_E.query_name = 'EX2_GA_2xDOVE_rev' |
|
|
165 |
read_E.set_tag('SM', 'Cell_A') |
|
|
166 |
read_E.is_read2 = False |
|
|
167 |
read_E.is_read1 = True |
|
|
168 |
read_E.set_tag('lh','TG') |
|
|
169 |
read_E.is_reverse = True |
|
|
170 |
# Set substitution tag: |
|
|
171 |
read_E.set_tag('MD', |
|
|
172 |
create_MD_tag( |
|
|
173 |
refseq[read_E.reference_start:read_E.reference_end], |
|
|
174 |
read_E.query_sequence)) |
|
|
175 |
read_E.set_tag('ri','read_E') |
|
|
176 |
read_E.is_paired = True |
|
|
177 |
read_E.is_proper_pair = True |
|
|
178 |
|
|
|
179 |
|
|
|
180 |
# Create a second read which is a mate of the previous |
|
|
181 |
read_F = pysam.AlignedSegment(bam.header) |
|
|
182 |
read_F.reference_name = 'chr1' |
|
|
183 |
read_F.reference_start = 10 |
|
|
184 |
read_F.query_sequence = refseq[10:74].replace('CGG','CAG').replace('GGGG','GAGG') |
|
|
185 |
read_F.cigarstring = f'{len(read_F.query_sequence)}M' |
|
|
186 |
read_F.qual = 'A'*len(read_F.query_sequence) |
|
|
187 |
read_F.mapping_quality = 60 |
|
|
188 |
read_F.is_read1 = False |
|
|
189 |
read_F.is_read2 = True |
|
|
190 |
read_F.is_reverse = False |
|
|
191 |
read_F.query_name = 'EX2_GA_2xDOVE_rev' |
|
|
192 |
read_F.set_tag('ri','read_F') |
|
|
193 |
read_F.set_tag('SM', 'Cell_A') |
|
|
194 |
read_F.set_tag('lh','TG') |
|
|
195 |
read_F.set_tag('MD', |
|
|
196 |
create_MD_tag(refseq[read_F.reference_start:read_F.reference_end], |
|
|
197 |
read_F.query_sequence, |
|
|
198 |
)) |
|
|
199 |
read_F.is_paired = True |
|
|
200 |
read_F.is_proper_pair = True |
|
|
201 |
|
|
|
202 |
|
|
|
203 |
read_F.mate_is_reverse = read_E.is_reverse |
|
|
204 |
read_E.mate_is_reverse = read_F.is_reverse |
|
|
205 |
|
|
|
206 |
read_E.next_reference_id = read_F.reference_id |
|
|
207 |
read_E.next_reference_start = read_F.reference_start |
|
|
208 |
read_F.next_reference_id = read_E.reference_id |
|
|
209 |
read_F.next_reference_start = read_E.reference_start |
|
|
210 |
|
|
|
211 |
bam.write(read_E) |
|
|
212 |
bam.write(read_F) |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
pysam.sort(alignments_path_unsorted, '-o', alignments_path) |
|
|
216 |
pysam.index(alignments_path) |
|
|
217 |
|
|
|
218 |
taps = TAPS() |
|
|
219 |
with pysam.FastaFile(ref_path) as reference: |
|
|
220 |
|
|
|
221 |
self.assertEqual(reference.fetch('chr1', 26, 26 + 3),'CGG') |
|
|
222 |
molecule = TAPSNlaIIIMolecule( |
|
|
223 |
NlaIIIFragment([read_A, read_B]), |
|
|
224 |
reference=reference, |
|
|
225 |
taps=taps, |
|
|
226 |
taps_strand='F' |
|
|
227 |
) |
|
|
228 |
molecule.__finalise__() |
|
|
229 |
|
|
|
230 |
calls = molecule.methylation_call_dict |
|
|
231 |
print(calls) |
|
|
232 |
print(calls[('chr1', 54)]) |
|
|
233 |
self.assertEqual( calls['chr1', 54]['context'], 'Z') |
|
|
234 |
self.assertEqual( calls['chr1', 26]['context'], 'Z') |
|
|
235 |
self.assertNotIn( ('chr1', 26 + 6), calls) |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
molecule = TAPSNlaIIIMolecule( |
|
|
239 |
NlaIIIFragment([read_E, read_F]), |
|
|
240 |
reference =reference, |
|
|
241 |
taps = taps, |
|
|
242 |
taps_strand='F' |
|
|
243 |
) |
|
|
244 |
molecule.__finalise__() |
|
|
245 |
|
|
|
246 |
# Test dove-tail detection: |
|
|
247 |
self.assertNotIn( ('chr1', 71) , molecule.methylation_call_dict) |
|
|
248 |
self.assertNotIn(('chr1', 8) , molecule.methylation_call_dict) |
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
molecule = TAPSNlaIIIMolecule( |
|
|
253 |
NlaIIIFragment([read_C, read_D]), |
|
|
254 |
reference=reference, |
|
|
255 |
taps=taps, |
|
|
256 |
taps_strand='F' |
|
|
257 |
) |
|
|
258 |
molecule.__finalise__() |
|
|
259 |
|
|
|
260 |
calls = molecule.methylation_call_dict |
|
|
261 |
|
|
|
262 |
self.assertEqual(calls['chr1', 12]['context'], 'X') |
|
|
263 |
|
|
|
264 |
# Check that dove tail is not included: |
|
|
265 |
self.assertNotIn(('chr1', 21), calls) |
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
if __name__ == '__main__': |
|
|
270 |
unittest.main() |