-
Notifications
You must be signed in to change notification settings - Fork 0
/
cram_utils.py
executable file
·263 lines (202 loc) · 8.39 KB
/
cram_utils.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
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
import pysam
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
import edlib
import subprocess
import re
import mappy
UP_seq = "TCTTCAGCGTTCCCGAGA"
UP_seq_revcomp = "TCTCGGGAACGCTGAAGA"
const_3prime_full_forward = "TCAGACGTGTGCTCTTCCGATCT" # right
const_3prime_full_reverse = "AGATCGGAAGAGCACACGTCTGA" # right
const_5prime_full_forward = "CTACACGACGCTCTTCCGATCT" # left
right_const = const_3prime_full_reverse[:20]
left_const = const_5prime_full_forward[-20:]
N_read_extract = 100000
print(N_read_extract)
def cram_fastq_split_by_lines(indir, sample, cores, lines=4e6):
cram_file = f"{indir}/{sample}.cram"
crai_file = f"{indir}/{sample}.cram.crai"
if os.path.isfile(crai_file):
print(f"cram {crai_file} index exists")
else:
print(f"cram {crai_file} index does not exit, will index")
cram_index_command = f"samtools index -@{int(cores)} {cram_file}"
subprocess.call(cram_index_command, shell=True)
splitted_file = f"{indir}/{sample}/split/{sample}.part_000.fastq"
if os.path.isfile(splitted_file):
print(splitted_file, " splitted fastq exists, skip splitting")
else:
print(splitted_file, " splitted fastq does not exist")
split_dir = f"{indir}/{sample}/split"
if not os.path.exists(split_dir):
os.makedirs(split_dir)
print(f"{split_dir} created")
else:
print(f"{split_dir} already exists")
split_name_pattern = f"{split_dir}/{sample}.part_"
# convert to fastq with samtools and split by n=lines (4x number of reads)
# add a suffix with 3 digits and prefix of 'split_name_pattern'
cram_to_fastq_command = (
f"samtools fastq -@{int(cores)} {cram_file} | "
f"split -d -a 3 -l {int(lines)} "
f"--additional-suffix=.fastq - {split_name_pattern}"
)
subprocess.call(cram_to_fastq_command, shell=True)
def seq_counter(seq_dict, seq_instance):
if seq_dict.get(seq_instance) is None:
seq_dict[seq_instance] = 1
else:
seq_dict[seq_instance] += 1
def quad_dict_store(quad_dict, quad_key, quad_items):
if quad_dict.get(quad_key) is None:
quad_dict[quad_key] = [quad_items]
else:
quad_dict[quad_key].extend([quad_items])
def edit_match(input_seq, target_seq, max_dist):
if input_seq == target_seq:
dist = 0
match = True
else:
edit = edlib.align(input_seq, target_seq, "NW", "path", max_dist)
dist = edit["editDistance"]
if dist >= 0 and dist <= max_dist:
cigar = edit["cigar"]
if "D" in cigar or "I" in cigar:
match = False
dist = "indel"
else:
match = True
else:
match = False
return (match, dist)
def quality_calc(seq, quals, bases_dict, quals_dict):
for i in range(len(seq)):
if bases_dict.get(str(i)) is None:
bases_dict[str(i)] = {}
seq_counter(bases_dict[str(i)], seq[i])
else:
seq_counter(bases_dict[str(i)], seq[i])
if quals_dict.get(str(i)) is None:
quals_dict[str(i)] = {}
seq_counter(quals_dict[str(i)], quals[i])
else:
seq_counter(quals_dict[str(i)], quals[i])
def quality_df(quals_dict):
quals_df = pd.DataFrame(quals_dict)
quals_df = quals_df.T
quals_df = quals_df.fillna(0)
quals_df = quals_df.stack()
quals_df = quals_df.reset_index()
quals_df.columns = ["position", "value", "ncount"]
quals_df.position = quals_df.position.astype("int") + 1
counts_df = quals_df.groupby("position").sum()
quals_df["position_cnt"] = quals_df.position.apply(
lambda x: counts_df.loc[x].ncount
)
quals_df["freq"] = quals_df.ncount / quals_df.position_cnt * 100
return quals_df
def extract_trimmed_fastq_pairs(indir, sample, part, limit):
i = 0
max_dist = 3
timmed_length = 55
ultima_fastq = f"{indir}/{sample}/split/{sample}.part_{part}.fastq"
R1_fastq = f"{indir}/{sample}/split/{sample}_R1.part_{part}.fastq"
R2_fastq = f"{indir}/{sample}/split/{sample}_R2.part_{part}.fastq"
r1_qual_dict = {}
r2_qual_dict = {}
r1_base_dict = {}
r2_base_dict = {}
do_qc = True
# R1_quals = R1_fastq.replace(".fastq", "_quals.csv")
# if os.path.isfile(R1_quals):
# print(R1_quals, " exists, skip")
# return
R1 = open(R1_fastq, "w")
R2 = open(R2_fastq, "w")
with pysam.FastxFile(ultima_fastq) as R:
for r in tqdm(R):
i += 1
seq = r.sequence
rlen = len(seq)
# reconstruction libraries are in this range
if rlen > 135 and rlen < 170:
# find TruSeqR1 position in the first 50nt of the read
begin_seq = seq[:50]
accept_r1 = False
pos_con_in_begin = begin_seq.find(left_const)
if pos_con_in_begin >= 0:
accept_r1 = True
pos_con_in_begin += len(left_const)
else:
edit = edlib.align(
left_const, begin_seq, "HW", "locations", max_dist
)
dist = edit["editDistance"]
if dist >= 0:
accept_r1 = True
locs = edit["locations"][0]
pos_con_in_begin = locs[1] + 1
# find TruSeqR2 position in the last 50nt of the read
end_seq = seq[-50:]
accept_r2 = False
pos_con_in_end = end_seq.find(right_const)
if pos_con_in_end >= 0:
accept_r2 = True
dist = 0
else:
edit = edlib.align(
right_const, end_seq, "HW", "locations", max_dist
)
dist = edit["editDistance"]
if dist >= 0:
accept_r2 = True
locs = edit["locations"][0]
pos_con_in_end = locs[0]
if accept_r2 and accept_r1:
qual = r.quality
# Actually trim and split to get R1
trim_begin = pos_con_in_begin
r1_seq = seq[trim_begin : trim_begin + timmed_length]
r1_qual = qual[trim_begin : trim_begin + timmed_length]
# this is a very specfic insertion pattern which I manually replace!!
if r1_seq[8:15] == "TCCTTCA":
r1_seq = r1_seq[:9] + r1_seq[10:]
r1_qual = r1_qual[:9] + r1_qual[10:]
# Actually trim and split to get R2
trim_end = 50 - pos_con_in_end
r2_seq = mappy.revcomp(seq[-trim_end - timmed_length : -trim_end])
r2_qual = qual[-trim_end - timmed_length : -trim_end][::-1]
R1.write(f"@{r.name}_1\n")
R1.write(f"{r1_seq}\n")
R1.write("+\n")
R1.write(f"{r1_qual}\n")
R2.write(f"@{r.name}_2\n")
R2.write(f"{r2_seq}\n")
R2.write("+\n")
R2.write(f"{r2_qual}\n")
if do_qc and i % 200 == 0:
quality_calc(r1_seq, r1_qual, r1_base_dict, r1_qual_dict)
quality_calc(r2_seq, r2_qual, r2_base_dict, r2_qual_dict)
if i > N_read_extract and limit:
break
r1_qual_df = quality_df(r1_qual_dict)
r2_qual_df = quality_df(r2_qual_dict)
r1_base_df = quality_df(r1_base_dict)
r2_base_df = quality_df(r2_base_dict)
r1_qual_df.to_csv(R1_fastq.replace(".fastq", "_quals.csv"))
r2_qual_df.to_csv(R2_fastq.replace(".fastq", "_quals.csv"))
r1_base_df.to_csv(R1_fastq.replace(".fastq", "_bases.csv"))
r2_base_df.to_csv(R2_fastq.replace(".fastq", "_bases.csv"))
R1.close()
R2.close()
def find_sub_fastq_parts(indir, sample):
# pattern = re.compile(r"_R1.part_(.*?)\.fastq")
pattern = re.compile(r"^(?!.*R[12]).*part_\d{3}\.fastq")
all_files = os.listdir(f"{indir}/{sample}/split/")
# part + 3 digits because we did split suffix with 3 digits
all_parts = [f.split(".part_")[1][:3] for f in all_files if pattern.search(f)]
parts = sorted(np.unique(all_parts))
return parts