-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvxtractor_fastq.py
1661 lines (1379 loc) · 67.4 KB
/
vxtractor_fastq.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os
import sys
import glob
import shutil
import argparse
import subprocess
import multiprocessing
import unittest
import logging
import pytest
import re
from collections import namedtuple
from tqdm import tqdm
from Bio.Data import IUPACData
from Bio.SeqUtils import nt_search
from Bio.SearchIO.HmmerIO import Hmmer3TabParser
from Bio.SearchIO._model import QueryResult, Hit, HSP, HSPFragment
import pyfastx
from pyfastxcli import fastx_format_check
__description__ = "A tool for extracting 16S rRNA gene variable regions from FASTQ files guided by alignment positions."
__author__ = "Connor Morgan-Lang, Koonkie Cloud Services"
_DEFAULT_PRIMERS = {"515f": "GTGYCAGCMGCCGCGGTAA",
"806r": "GGACTACNVGGGTWTCTAAT"}
_COORD_RANGE = namedtuple("coord_range", ["start", "stop"])
nuc_alphabet = re.compile(r"^[ACGT]+$")
class FastqExtractorTester(unittest.TestCase):
def setUp(self) -> None:
as_one = AmpliconSample(sample_name="SRR4011269", fwd="CCTACGGGAGGCAGCAG", rev="CCGTCAATTCMTTTRAGT")
as_two = AmpliconSample(sample_name="mock_sample_two", fwd="", rev="")
as_three = AmpliconSample(sample_name="mock_sample_three", fwd="", rev="")
self.mock_pe_amplicon_samples = [as_one, as_two, as_three]
self.se_sample = AmpliconSample(sample_name="ERR3339670", fwd="GTGCCAGCMGCCGCGGTAA", rev="GGACTACHVGGGTWTCTAAT")
self.fail_as = AmpliconSample(sample_name="ERR2105436", fwd="GTGCCAGCMGCCGCGGTAA", rev="GGACTACHVGGGTWTCTAAT")
self.pyro_as = AmpliconSample(sample_name="8A", fwd="ACTCCTACGGGAGGCAGCAG", rev="CCGTCAATTCMTTTGAGTTT")
self.test_data_dir = "test_data" + os.sep
self.output_dir = "./vxtractor_fastq_out"
self.tmp_dir = "./vxtractor_fastq_tmp"
self.trim_out = self.test_data_dir + "primer_test_trimmed.fq"
self.hmms_dir = os.path.join(self.test_data_dir, "HMMs", "bacteria")
for dir_path in [self.output_dir, self.tmp_dir]:
if not os.path.isdir(dir_path):
os.mkdir(dir_path)
# Make the directories
for _sample in self.mock_pe_amplicon_samples: # type: AmpliconSample
if not os.path.isdir(self.test_data_dir + _sample.name):
os.mkdir(self.test_data_dir + _sample.name)
shutil.copyfile(self.test_data_dir + "Chen2016_1_1.fastq",
os.path.join(self.test_data_dir, _sample.name, "mock_R1.fq"))
shutil.copyfile(self.test_data_dir + "Chen2016_1_2.fastq",
os.path.join(self.test_data_dir, _sample.name, "mock_R2.fq"))
for _sample in [self.se_sample, self.fail_as, self.pyro_as]:
if not os.path.isdir(os.path.join(self.test_data_dir, _sample.name)):
os.mkdir(os.path.join(self.test_data_dir, _sample.name))
shutil.copyfile(os.path.join(self.test_data_dir, _sample.name + ".fastq"),
os.path.join(self.test_data_dir, _sample.name, _sample.name + ".fastq"))
return
def tearDown(self) -> None:
for dir_path in [self.output_dir, self.tmp_dir]:
if os.path.isdir(dir_path):
shutil.rmtree(dir_path)
if os.path.isfile(self.trim_out):
os.remove(self.trim_out)
for _sample in self.mock_pe_amplicon_samples: # type: AmpliconSample
if os.path.isdir(self.test_data_dir + _sample.name):
shutil.rmtree(self.test_data_dir + _sample.name)
if os.path.isdir(os.path.join(self.test_data_dir, self.se_sample.name)):
shutil.rmtree(os.path.join(self.test_data_dir, self.se_sample.name))
return
def test_validate_arguments(self):
cli_args = ["--primer_map", self.test_data_dir + "example_primer_map.txt",
"--fastq_path", self.test_data_dir,
"--extraction_guide", "infernal",
"--cmfile", self.test_data_dir + "SSU_rRNA_bacteria.cm",
"--output_dir", self.tmp_dir,
"--threads", str(2)]
args = get_options(cli_args)
validate_arguments(args)
self.assertTrue(os.path.isfile(os.path.join(self.tmp_dir, "515_806_primers.fasta")))
# Test without providing cmfile for Infernal extraction
args.cmfile = None
with pytest.raises(AssertionError):
validate_arguments(args)
# Test bad tool name
args.extraction_guide = "vxtract"
with pytest.raises(ValueError):
validate_arguments(args)
return
def test_rc_primers(self):
mock_sample = self.mock_pe_amplicon_samples[0]
rc_fwd, rc_rev = mock_sample.rc_primers()
self.assertEqual(len(mock_sample.fwd_primer), len(rc_fwd))
self.assertEqual(len(mock_sample.rev_primer), len(rc_rev))
self.assertEqual(("CTGCTGCCTCCCGTAGG", "ACTYAAAKGAATTGACGG"), (rc_fwd, rc_rev))
return
def test_validate_executables(self):
retcode = validate_executables()
self.assertEqual(0, retcode)
retcode = validate_executables(extractor="vxtractor")
self.assertEqual(0, retcode)
return
def test_validate_hmm_dir(self):
with pytest.raises(AssertionError):
validate_hmm_dir(self.test_data_dir + "HMMs")
validate_hmm_dir(self.hmms_dir)
return
def test_read_name_match(self):
self.assertTrue(read_name_match("R0235093:66:000000000-A4C50:1:1101:17880:1893/1",
"R0235093:66:000000000-A4C50:1:1101:17880:1893/2"))
self.assertTrue(read_name_match("SRR12345_1 read_1 N033:1234/1", "SRR12345_1 read_1 N033:1234/2"))
self.assertFalse(read_name_match("SRR12345_1", "SRR12345_11"))
return
def test_align_primers(self):
chen_as = AmpliconSample(sample_name="SRR4011269",
fwd="TGGGGAATATTGCACAA", rev="ACGGCAAGCCAGA")
primer_counts = chen_as.align_primers(self.test_data_dir + "primer_test.fq")
self.assertEqual(2, primer_counts["reverse_rc"])
self.assertEqual(2, primer_counts["forward"])
self.assertTrue(primer_counts["reverse"] == primer_counts["forward_rc"] == 1)
return
def test_fastx_summarize(self):
# Test a FASTQ
fx = Fastx(fastx_path=self.test_data_dir + "primer_test.fq")
fx.summarize()
self.assertEqual(4, fx.num_seqs())
return
def test_check_pairing(self):
mock_sample = self.mock_pe_amplicon_samples[0] # type: AmpliconSample
if not mock_sample.raw_fwd and not mock_sample.raw_rev:
mock_sample.fetch_raw_fastq(self.test_data_dir)
mock_sample.check_pairing(self.tmp_dir)
self.assertFalse(mock_sample.repaired)
return
def test_filter_reads(self):
mock_sample = self.mock_pe_amplicon_samples[0]
if not mock_sample.raw_fwd and not mock_sample.raw_rev:
mock_sample.fetch_raw_fastq(self.test_data_dir)
mock_sample.filtered_fwd = os.path.join(self.tmp_dir, mock_sample.name + "_filtered_R1.fastq")
mock_sample.filtered_rev = os.path.join(self.tmp_dir, mock_sample.name + "_filtered_R1.fastq")
with pytest.raises(AssertionError):
fq1, fq2 = mock_sample.collect_fastq_by_stage("repaired")
filter_reads(fwd_in=fq1, filtered_fwd=mock_sample.filtered_fwd, min_seq_length=100,
rev_in=fq2, filtered_rev=mock_sample.filtered_rev)
fq1, fq2 = mock_sample.collect_fastq_by_stage("raw")
filtered_dict = filter_reads(fwd_in=fq1, filtered_fwd=mock_sample.filtered_fwd, min_seq_length=200,
rev_in=fq2, filtered_rev=mock_sample.filtered_rev)
read_pairs, fastqs = filtered_dict.popitem()
pairs_pass, pairs_fail = read_pairs
self.assertEqual(351, pairs_fail)
self.assertTrue(os.path.isfile(mock_sample.filtered_fwd))
self.assertTrue(os.path.isfile(mock_sample.filtered_rev))
# Ensure the number of lines in both Fastq files are identical
with open(mock_sample.filtered_fwd) as fh:
n_fwd = len(fh.readlines())
with open(mock_sample.filtered_rev) as fh:
n_rev = len(fh.readlines())
self.assertEqual(n_fwd, n_rev)
return
def test_fq2fa(self):
tmp_fa = self.tmp_dir + os.sep + "test_TarA.1.fa"
fq2fa(fastq=self.test_data_dir + "test_TarA.1.fq", fa=tmp_fa)
fx = Fastx(tmp_fa)
fx.summarize()
self.assertEqual(12, fx.num_seqs())
return
def test_cmsearch_wrapper(self):
output_file = self.tmp_dir + os.sep + "test_SSU_cmsearch.tbl"
cmsearch_cmd = cmsearch_command_generator(exec_path=which("cmsearch"),
fasta_file=self.test_data_dir + "test_SSU_rRNA.fa",
output_tbl=output_file,
cov_model=self.test_data_dir + "SSU_rRNA_bacteria.cm",
threads=2,
hmm_only=True)
launch_system_command(cmsearch_cmd)
self.assertTrue(os.path.isfile(output_file))
with open(output_file) as tbl:
self.assertEqual(2511, len(tbl.readlines()))
return
def test_get_model_primer_coords(self):
primer_fa = self.test_data_dir + "515_806_primers.fasta"
primers_range = get_model_primer_coords(cmsearch=which("cmsearch"),
primers_fa=primer_fa,
cov_model=self.test_data_dir + "SSU_rRNA_bacteria.cm")
self.assertEqual(523, primers_range.start)
self.assertEqual(776, primers_range.stop)
return
def test_fetch_raw_fastq(self):
mock_sample = self.mock_pe_amplicon_samples[0]
mock_sample.fetch_raw_fastq(self.test_data_dir)
self.assertEqual(self.test_data_dir + "SRR4011269/mock_R1.fq", mock_sample.raw_fwd)
self.assertEqual(self.test_data_dir + "SRR4011269/mock_R2.fq", mock_sample.raw_rev)
with pytest.raises(AssertionError):
AmpliconSample("SRR3581236", fwd="T", rev="A").fetch_raw_fastq(self.test_data_dir)
return
def test_read_vxtracted(self):
mock_sample = self.mock_pe_amplicon_samples[0]
mock_sample.var_pos_tbl = self.test_data_dir + "SRR3581236_vxtractor.csv"
mock_sample.read_vxtractor_coords(start_name="V3rightlong", end_name="V5leftlong")
mock_sample.var_pos_tbl = self.test_data_dir + "test_V4.csv"
variable_positions = mock_sample.read_vxtractor_coords(start_name="V3rightlong", end_name="V5leftlong")
self.assertEqual(100, len(variable_positions))
self.assertEqual((440, 693), variable_positions["AF418950.1.1453"])
self.assertEqual((501, 753), variable_positions["AY005047.1.1486"])
return
def test_trim_fq(self):
trim_fq(fq_in=self.test_data_dir + "primer_test.fq",
fq_out=self.trim_out,
trim_positions={"Seq1_fwd_rev_pos": (1, 10),
"Seq2_fwd-rc_rev-rc_pos": (11, 20),
"Seq3_fwd_pos": (1, 10),
"Seq4_rev-rc_pos": (1, 10)})
for name, seq, qual in Fastx(self.trim_out).fx_handler:
if name == "Seq1_fwd_rev_pos":
self.assertEqual("GGGGAATAT", seq)
return
def test_read_fastq_seq_names(self):
seq_names = read_fastq_seq_names(os.path.join("test_data", "Chen2016_1_1.fastq"), 50)
self.assertEqual(50, len(seq_names))
seq_names = read_fastq_seq_names(os.path.join("test_data", "Chen2016_1_1.fastq"))
self.assertEqual(2500, len(seq_names))
return
def test_repair_wrapper(self):
sam = self.mock_pe_amplicon_samples[0]
sam.fetch_raw_fastq(self.test_data_dir)
sam.repaired_fwd = os.path.join(self.tmp_dir, sam.name + "_repaired_R1.fastq")
sam.repaired_rev = os.path.join(self.tmp_dir, sam.name + "_repaired_R2.fastq")
repair_wrapper(fwd_reads=sam.raw_fwd,
rev_reads=sam.raw_rev,
repaired_fwd=sam.repaired_fwd,
repaired_rev=sam.repaired_rev)
self.assertTrue(os.path.isfile(sam.repaired_fwd))
self.assertTrue(os.path.isfile(sam.repaired_rev))
self.assertEqual(2500, len(read_fastq_seq_names(sam.repaired_fwd)))
return
def test_vxtractor_command_generator(self):
try:
vxtractor_exe_path = which("vxtractor.pl")
except SystemError:
return
coords_table = os.path.join(self.tmp_dir, "aln_coords.csv")
vx_cmd = vxtractor_command_generator(exec_path=vxtractor_exe_path,
fa_in=self.test_data_dir + "test_SSU_rRNA.fa",
fa_extracted=os.path.join(self.tmp_dir, "test_SSU_rRNA_extracted.fa"),
var_pos_tbl=coords_table,
hmm_dir=self.hmms_dir)
launch_system_command(vx_cmd)
self.assertTrue(os.path.isfile(coords_table))
return
def test_parse_hmmer_dom_table(self):
qseq_coords = parse_hmmer_dom_table(self.test_data_dir + "empty_aln_coords.csv")
self.assertEqual(0, len(qseq_coords))
qseq_coords = parse_hmmer_dom_table(self.test_data_dir + "infernal_aln_coords.csv")
self.assertEqual(6, len(qseq_coords))
return
def test_find_trim_coords_from_infernal_alignments(self):
primers_range = _COORD_RANGE(start=355, stop=540)
mock_sample = self.mock_pe_amplicon_samples[0]
mock_sample.var_pos_tbl = self.test_data_dir + "infernal_aln_coords.csv"
trim_coords = mock_sample.find_trim_coords_from_infernal_alignments(target_coords=primers_range)
self.assertEqual(4, len(trim_coords))
self.assertEqual((1, 188), trim_coords['SRR4011269.1015 1015 length=247'])
self.assertEqual((54, 231), trim_coords['SRR4011269.3 3 length=247'])
self.assertEqual((0, 253), trim_coords['HWI-M00178:39:000000000-A409G:1:1113:4128:17933 1:N:0:AAC>'])
self.assertEqual((1, 253), trim_coords['HWI-M00178:39:000000000-A409G:1:2107:28336:12297 1:N:0:AAC>'])
mock_sample.var_pos_tbl = self.test_data_dir + "v4_infernal_aln_coords.csv"
primers_range = _COORD_RANGE(start=505, stop=796)
trim_coords = mock_sample.find_trim_coords_from_infernal_alignments(target_coords=primers_range)
self.assertEqual(10678, len(trim_coords))
# Test an empty alignment table
mock_sample.var_pos_tbl = self.test_data_dir + "empty_aln_coords.csv"
trim_coords = mock_sample.find_trim_coords_from_infernal_alignments(target_coords=primers_range)
self.assertEqual(0, len(trim_coords))
return
def test_main(self):
# Test with Infernal
cli_args = ["--primer_map", self.test_data_dir + "example_primer_map.txt",
"--fastq_path", self.test_data_dir,
"--extraction_guide", "infernal",
"--cmfile", self.test_data_dir + "SSU_rRNA_bacteria.cm",
"--hmmonly",
"--output_dir", self.output_dir,
"--threads", str(4),
"--min_read_length", str(150),
"--verbose", "--delete"]
main(cli_args)
pyro_sample_extracted = os.path.join(self.output_dir, "8A_extracted.fq")
self.assertTrue(os.path.isfile(pyro_sample_extracted))
pyro_fx = Fastx(pyro_sample_extracted)
pyro_fx.get_seq_lengths()
self.assertEqual(604, pyro_fx.num_seqs())
# Test with V-Xtractor
cli_args = ["--primer_map", self.test_data_dir + "example_primer_map.txt",
"--fastq_path", self.test_data_dir,
"--extraction_guide", "vxtractor",
"--vxtractor_hmms", os.path.join(self.test_data_dir, "HMMs", "bacteria"),
"--output_dir", self.output_dir,
"--min_read_length", str(151),
"--threads", str(2)]
main(cli_args)
return
class InfernalTblParser(Hmmer3TabParser):
"""Parser for the Infernal cmsearch tblout format."""
def hmm_as_hit(self):
return True
def _parse_row(self):
"""Return a dictionary of parsed row values (PRIVATE)."""
cols = [x for x in self.line.strip().split(" ") if x]
if len(cols) < 18:
raise ValueError("Less columns than expected, only %i" % len(cols))
# if len(cols) > 19, we have extra description columns
# combine them all into one string in the 19th column
if len(cols) > 18:
cols[17] = " ".join(cols[17:])
# assign parsed column data into qresult, hit, and hsp dicts
qresult = {}
qresult["id"] = cols[2] # query name
qresult["accession"] = cols[3] # query accession
hit = {}
hit["id"] = cols[0] # target name
hit["accession"] = cols[1] # target accession
hit["description"] = cols[17] # description of target
hsp = {}
hsp["evalue"] = float(cols[15]) # i-evalue
hsp["bitscore"] = float(cols[14]) # score
hsp["bias"] = float(cols[13]) # bias
frag = {}
# strand is always 0, since HMMER now only handles protein
if cols[9] == '-':
frag["hit_strand"] = frag["query_strand"] = -1
frag["hit_start"] = int(cols[8]) # hmm to
frag["hit_end"] = int(cols[7]) - 1 # hmm from
else:
frag["hit_strand"] = frag["query_strand"] = 1
frag["hit_start"] = int(cols[7]) - 1 # hmm from
frag["hit_end"] = int(cols[8]) # hmm to
frag["query_start"] = int(cols[5]) - 1 # ali from
frag["query_end"] = int(cols[6]) # ali to
# Infernal results are always RNA
frag["molecule_type"] = "rna"
# switch hmm<-->ali coordinates if hmm is not hit
if not self.hmm_as_hit:
frag["hit_end"], frag["query_end"] = (frag["query_end"], frag["hit_end"])
frag["hit_start"], frag["query_start"] = (
frag["query_start"],
frag["hit_start"],
)
return {"qresult": qresult, "hit": hit, "hsp": hsp, "frag": frag}
def _parse_qresult(self):
"""Return QueryResult objects (PRIVATE)."""
# state values, determines what to do for each line
state_EOF = 0
state_QRES_NEW = 1
state_QRES_SAME = 3
state_HIT_NEW = 2
state_HIT_SAME = 4
# dummies for initial states
qres_state = None
hit_state = None
file_state = None
# dummies for initial id caches
prev_qid = None
prev_hid = None
# dummies for initial parsed value containers
cur, prev = None, None
hit_list, hsp_list = [], []
hit_ids = set()
cur_qid = None
cur_hid = None
while True:
# store previous line's parsed values, for every line after the 1st
if cur is not None:
prev = cur
prev_qid = cur_qid
prev_hid = cur_hid
# only parse the line if it's not EOF
if self.line and not self.line.startswith("#"):
cur = self._parse_row()
cur_qid = cur["qresult"]["id"]
cur_hid = cur["hit"]["id"]
else:
file_state = state_EOF
# mock ID values since the line is empty
cur_qid, cur_hid = None, None
# get the state of hit and qresult
if prev_qid != cur_qid:
qres_state = state_QRES_NEW
else:
qres_state = state_QRES_SAME
# new hits are hits with different ids or hits in a new qresult
if prev_hid != cur_hid or qres_state == state_QRES_NEW:
hit_state = state_HIT_NEW
else:
hit_state = state_HIT_SAME
# start creating objects after the first line (i.e. prev is filled)
if prev is not None:
# each line is basically an HSP with one HSPFragment
frag = HSPFragment(prev_hid, prev_qid)
for attr, value in prev["frag"].items():
setattr(frag, attr, value)
hsp = HSP([frag])
for attr, value in prev["hsp"].items():
setattr(hsp, attr, value)
hsp_list.append(hsp)
# create hit object when we've finished parsing all its hsps
# i.e. when hit state is state_HIT_NEW
if hit_state == state_HIT_NEW:
hit = Hit(hsp_list)
if hit.id not in hit_ids:
for attr, value in prev["hit"].items():
setattr(hit, attr, value)
hit_list.append(hit)
hit_ids.add(hit.id)
hsp_list = []
# create qresult and yield if we're at a new qresult or EOF
if qres_state == state_QRES_NEW or file_state == state_EOF:
qresult = QueryResult(hit_list, prev_qid)
for attr, value in prev["qresult"].items():
setattr(qresult, attr, value)
yield qresult
# if current line is EOF, break
if file_state == state_EOF:
break
hit_list = []
self.line = self.handle.readline()
@staticmethod
def hsp_name(hsp: HSP) -> str:
if hsp.hit_description != '-':
return hsp.hit_id + ' ' + hsp.hit_description
else:
return hsp.hit_id
class MyFormatter(logging.Formatter):
error_fmt = "%(levelname)s - %(module)s, line %(lineno)d:\n%(message)s"
warning_fmt = "%(levelname)s:\n%(message)s"
debug_fmt = "%(asctime)s\n%(message)s"
info_fmt = "%(message)s"
def __init__(self):
super().__init__(fmt="%(levelname)s: %(message)s",
datefmt="%d/%m %H:%M:%S")
def format(self, record):
# Save the original format configured by the user
# when the logger formatter was instantiated
format_orig = self._style._fmt
# Replace the original format with one customized by logging level
if record.levelno == logging.DEBUG:
self._style._fmt = MyFormatter.debug_fmt
elif record.levelno == logging.INFO:
self._style._fmt = MyFormatter.info_fmt
elif record.levelno == logging.ERROR:
self._style._fmt = MyFormatter.error_fmt
elif record.levelno == logging.WARNING:
self._style._fmt = MyFormatter.warning_fmt
# Call the original formatter class to do the grunt work
result = logging.Formatter.format(self, record)
# Restore the original format configured by the user
self._style._fmt = format_orig
return result
def prep_logging(log_file_name=None, verbosity=False) -> None:
"""
Allows for multiple file handlers to be added to the root logger, but only a single stream handler.
The new file handlers must be removed outside of this function explicitly
:param log_file_name:
:param verbosity:
:return: None
"""
if verbosity:
logging_level = logging.DEBUG
else:
logging_level = logging.INFO
# Detect whether a handlers are already present and return if true
logger = logging.getLogger()
if len(logger.handlers):
return
formatter = MyFormatter()
# Set the console handler normally writing to stdout/stderr
ch = logging.StreamHandler()
ch.setLevel(logging_level)
ch.terminator = ''
ch.setFormatter(formatter)
if log_file_name:
output_dir = os.path.dirname(log_file_name)
try:
if output_dir and not os.path.isdir(output_dir):
os.makedirs(output_dir)
except (IOError, OSError):
sys.stderr.write("ERROR: Unable to make directory '" + output_dir + "'.\n")
sys.exit(3)
logging.basicConfig(level=logging.DEBUG,
filename=log_file_name,
filemode='w',
datefmt="%d/%m %H:%M:%S",
format="%(asctime)s %(levelname)s:\n%(message)s")
logging.getLogger('').addHandler(ch)
logging.getLogger('').propagate = False
else:
logging.basicConfig(level=logging_level,
datefmt="%d/%m %H:%M:%S",
format="%(asctime)s %(levelname)s:\n%(message)s")
return
def _maketrans(complement_mapping):
"""Make a python string translation table (PRIVATE).
Arguments:
- complement_mapping - a dictionary such as ambiguous_dna_complement
and ambiguous_rna_complement from Data.IUPACData.
Returns a translation table (a string of length 256) for use with the
python string's translate method to use in a (reverse) complement.
Compatible with lower case and upper case sequences.
For internal use only.
"""
keys = "".join(complement_mapping.keys()).encode("ASCII")
values = "".join(complement_mapping.values()).encode("ASCII")
return bytes.maketrans(keys + keys.lower(), values + values.lower())
_dna_complement_table = _maketrans(IUPACData.ambiguous_dna_complement)
def reverse_complement(seq: str) -> str:
return seq.translate(_dna_complement_table)[::-1]
class Fastx:
def __init__(self, fastx_path, fx_format=None):
self.file_name = fastx_path
# Ensure the file isn't empty
self.st_size = os.stat(self.file_name).st_size
if self.st_size == 0:
logging.debug("{} is empty.\n".format(self.file_name))
return
# Set the file format to either FASTA or FASTQ
self.format = fx_format
if not self.format:
self.format = fastx_format_check(self.file_name)
self.seq_lengths = []
# Create the pyfastx file handler
if self.format == 'fasta':
self.fx_handler = pyfastx.Fasta(self.file_name, build_index=False)
elif self.format == 'fastq':
self.fx_handler = pyfastx.Fastq(self.file_name, build_index=False)
else:
raise AssertionError("Unknown file format '{}'.".format(self.format))
return
def get_seq_lengths(self) -> None:
for record in self.fx_handler:
self.seq_lengths.append(len(record[-1]))
return
def max_seq_length(self) -> int:
return max(self.seq_lengths)
def mean_seq_length(self) -> float:
return round(sum(self.seq_lengths)/self.num_seqs(), 1)
def num_seqs(self) -> int:
return len(self.seq_lengths)
def summarize(self, verbosity=0) -> None:
if self.st_size == 0:
logging.debug("Unable to summarize {} as file is empty.\n".format(self.file_name))
return
if not self.seq_lengths:
self.get_seq_lengths()
summary_str = "\t".join(str(i) for i in [self.file_name, self.num_seqs(),
self.mean_seq_length(), self.max_seq_length()]) \
+ "\n"
if verbosity:
logging.info(summary_str)
else:
logging.debug(summary_str)
return
class AmpliconSample:
def __init__(self, sample_name: str, fwd: str, rev: str):
self.name = sample_name
self.fwd_primer = fwd
self.rev_primer = rev
self.paired_end = True
self.repaired = False
# Path to FASTQ files
self.raw_fwd = ""
self.raw_rev = ""
self.repaired_fwd = ""
self.repaired_rev = ""
self.filtered_fwd = ""
self.filtered_rev = ""
self.trim_path_fwd = ""
self.trim_path_rev = ""
self.merge_path = ""
self.final_fq = ""
# Path to the target-region coordinates table
self.var_pos_tbl = ""
self.pairs_pass_filter = 0
self.pairs_fail_filter = 0
self.n_frags_aligned = 0
self.n_short_frags = 0
self.n_good_extracts = 0
return
def fetch_raw_fastq(self, input_dir):
sample_fq = sorted(glob.glob(os.path.join(input_dir, self.name + os.sep + "*")))
if len(sample_fq) > 2:
raise AssertionError("More than two FASTQ files found for sample '{}'.\n".format(self.name))
elif len(sample_fq) == 1:
self.raw_fwd = sample_fq.pop(0)
self.paired_end = False
elif len(sample_fq) == 0:
raise AssertionError("Unable to find FASTQ files for sample '{}'.\n".format(self.name))
else:
self.raw_fwd = sample_fq.pop(0)
self.raw_rev = sample_fq.pop(0)
logging.debug("FASTQ files found for '{}': {} {}\n".format(self.name, self.raw_fwd, self.raw_rev))
return
def rc_primers(self) -> (str, str):
return reverse_complement(self.fwd_primer), reverse_complement(self.rev_primer)
def collect_fastq_by_stage(self, stage: str) -> list:
fastq_files = []
if stage == "raw":
fastq_files.append(self.raw_fwd)
if self.paired_end:
fastq_files.append(self.raw_rev)
elif stage == "repair":
if self.paired_end:
fastq_files.append(self.repaired_fwd)
fastq_files.append(self.repaired_rev)
else:
fastq_files.append(self.raw_fwd)
elif stage == "filter":
fastq_files.append(self.filtered_fwd)
if self.paired_end:
fastq_files.append(self.filtered_rev)
elif stage == "trim":
fastq_files.append(self.trim_path_fwd)
if self.paired_end:
fastq_files.append(self.trim_path_rev)
elif stage == "merge":
if self.paired_end:
fastq_files.append(self.merge_path)
else:
# No merging if single-end files
fastq_files.append(self.trim_path_fwd)
elif stage == "final":
fastq_files.append(self.final_fq)
elif stage == "all":
fastq_files += [fq for fq in [self.raw_fwd, self.raw_rev,
self.repaired_fwd, self.repaired_rev,
self.filtered_fwd, self.filtered_rev,
self.trim_path_fwd, self.trim_path_rev,
self.merge_path, self.final_fq]
if fq]
else:
raise AssertionError("Unknown stage specified for fastq file collection.")
return fastq_files
def summarize_reads(self, stage: str, verbosity=0):
if verbosity:
logging.info("File name\tSequences\tMean length\tMax length\n")
else:
logging.debug("File name\tSequences\tMean length\tMax length\n")
for fq in self.collect_fastq_by_stage(stage):
fx = Fastx(fastx_path=fq)
fx.summarize(verbosity)
return
def align_primers(self, fx_path: str) -> dict:
rc_fwd, rc_rev = self.rc_primers()
primer_name_map = {"forward": self.fwd_primer,
"reverse": self.rev_primer,
"forward_rc": rc_fwd,
"reverse_rc": rc_rev}
primer_counts = {"forward": 0, "reverse": 0, "forward_rc": 0, "reverse_rc": 0}
fx = Fastx(fastx_path=fx_path)
if fx.st_size == 0:
logging.debug("{} is empty - no reads to find primer sequences in.\n".format(fx.file_name))
return primer_counts
if fx.format == "fasta":
logging.error("Unable to align primers to FASTA file.\n")
raise AssertionError
for _, seq, _ in fx.fx_handler:
for name in primer_name_map:
if len(nt_search(seq, primer_name_map[name])) > 1:
primer_counts[name] += 1
return primer_counts
def primer_check(self, stage: str):
"""Enumerate the number of times a primer (or reverse complement) was found on the reads."""
fastq_to_check = self.collect_fastq_by_stage(stage)
for fq in fastq_to_check:
primer_counts = self.align_primers(fq)
logging.debug("Primers in '{}':\n"
"{}\t{}\t\t{}\t{}\n"
"{}\t{}\t\t{}\t{}\n".format(fq,
"Forward", primer_counts["forward"],
"Reverse", primer_counts["reverse"],
"Fwd_RC", primer_counts["forward_rc"],
"Rev_RC", primer_counts["reverse_rc"]))
return
def check_pairing(self, temporary_dir: str, rm_suffix=False, n=100) -> None:
"""
Reads n number of read pairs from the forward and reverse fastq files and ensures that the sequence names match.
If the sequence names do not match (i.e. first forward read name is not identical to the first reverse read)
the BBTools' repair.sh script is called on the raw FASTQ files.
Once repair.sh is called, the AmpliconSample attributes 'repaired_fwd' and 'repaired_rev' are set, and the
'repaired' attribute is set to True.
:param n: Number of read pairs to sample.
:param rm_suffix: Boolean indicating whether the string 'length=[0-9]+' should be removed from the read names
:param temporary_dir: Path to write the repaired FASTQ files (if necessary)
:return: None
"""
logging.debug("Validating paired-read ordering for '{}'... ".format(self.name))
rm_suffix_re = re.compile(r" length=\d+$")
fwd_seq_names = [seq_name.split()[0] for seq_name in
read_fastq_seq_names(self.raw_fwd, num_reads=n)]
rev_seq_names = [seq_name.split()[0] for seq_name in
read_fastq_seq_names(self.raw_rev, num_reads=n)]
if rm_suffix:
fwd_seq_names = [rm_suffix_re.sub('', seq_name) for seq_name in fwd_seq_names]
rev_seq_names = [rm_suffix_re.sub('', seq_name) for seq_name in rev_seq_names]
if compare_read_names(fwd_seq_names, rev_seq_names) is False:
logging.debug("Repairing FASTQ files {}, {}.\n".format(self.raw_fwd, self.raw_rev))
self.repaired = True
self.repaired_fwd = os.path.join(temporary_dir, self.name + "_repaired_R1.fastq")
self.repaired_rev = os.path.join(temporary_dir, self.name + "_repaired_R2.fastq")
else:
self.repaired_fwd = self.raw_fwd
self.repaired_rev = self.raw_rev
logging.debug("done.\n")
return
def read_vxtractor_coords(self, start_name: str, end_name: str) -> dict:
variable_positions = {}
with open(self.var_pos_tbl, 'r') as tbl:
cmd_line = tbl.readline()
opt_line = tbl.readline()
fields = tbl.readline().strip().split(',')
# Find the field positions for the start and end data in the CSV
pos_dict = dict(zip(fields, range(len(fields))))
start_field_pos = pos_dict[start_name]
end_field_pos = pos_dict[end_name]
# Read the alignment positions
for line in tbl:
if not line:
continue
self.n_frags_aligned += 1
fields = re.sub(r"['\"]", '', line).strip().split(',')
try:
start = int(fields[start_field_pos].split('-')[1])
end = int(fields[end_field_pos].split('-')[0])
except (IndexError, ValueError):
if fields[start_field_pos] == "notfound" or fields[end_field_pos] == "notfound":
continue
variable_positions[fields[0]] = (start, end)
self.n_good_extracts += 1
return variable_positions
def find_trim_coords_from_infernal_alignments(self, target_coords: namedtuple) -> dict:
trim_coords = {}
for qseq_name, hsp in parse_hmmer_dom_table(self.var_pos_tbl).items(): # type: (str, HSP)
self.n_frags_aligned += 1
# TODO: Handle alignments to the negative strand
# Ensure the extracted sequences would be full-length
if hsp.query_start > target_coords.start or hsp.query_end < target_coords.stop:
self.n_short_frags += 1
continue
# Add the coordinates to the trim_coords dictionary
trim_coords[qseq_name] = (hsp.hit_start + (target_coords.start - hsp.query_start),
hsp.hit_end - (hsp.query_end - target_coords.stop))
self.n_good_extracts += 1
if self.n_frags_aligned > 0:
logging.debug("{} fragments ({}%) failed to cover entire target region.\n"
"".format(self.n_short_frags,
round(100*self.n_short_frags/self.n_frags_aligned, 2)))
return trim_coords
def validate_arguments(args) -> None:
if args.extraction_guide == "infernal":
if not args.cmfile:
raise AssertionError("Reference CM file must be provided with '--cmfile' for Infernal extraction mode.")
if not os.path.isfile(args.cmfile):
raise IOError("CM file '{}' does not exist.".format(args.cmfile))
if not args.guide_primers:
args.guide_primers = os.path.join(args.output_dir, "515_806_primers.fasta")
write_fasta_from_dict(_DEFAULT_PRIMERS, fasta_path=args.guide_primers)
elif args.extraction_guide == "vxtractor":
if not args.hmm_path:
raise AssertionError("Profile HMMs directory must be provided with '--vxtractor_hmms' for V-Xtractor.")
validate_hmm_dir(args.hmm_path)
else:
raise ValueError("Unrecognized extraction mode '{}' specified with '--extraction_guide'."
"".format(args.extraction_guide))
if args.infernal_threads > args.threads:
logging.error("Number of infernal-process threads specified ({}) is more than the number of threads ({}).\n"
"".format(args.infernal_threads, args.threads))
sys.exit(5)
return
def get_options(sys_args):
parser = argparse.ArgumentParser(description="A tool for primer-trimming, merging and extracting 16S rRNA gene "
"variable regions from FASTQ files.",
add_help=False)
req_args = parser.add_argument_group("Required arguments")
opt_args = parser.add_argument_group("Optional arguments")
mis_args = parser.add_argument_group("Miscellaneous arguments")
profile_args = opt_args.add_mutually_exclusive_group()
req_args.add_argument("-p", "--primer_map", dest="primers", required=True,
help="Path to a CSV file listing the forward and reverse primers used for each sample.")
req_args.add_argument("-f", "--fastq_path", dest="fastq_dir", required=True,
help="Path to the directory containing SRA run directories with FASTQ files.")
opt_args.add_argument('-o', '--output_dir', default='./out', required=False,
help="Path to a directory to write the merged, trimmed FASTQ files [ DEFAULT = './out' ]")
opt_args.add_argument("--hmmonly", default=False, action="store_true",
help="Infernal only: alignment refinement with secondary structure is skipped.")
opt_args.add_argument("-g", "--guide_primers", default=None, required=False,
help="Infernal only: a FASTA file containing primer sequences to guide sequence extraction. "
"[ DEFAULT = 515f,806r ]")
opt_args.add_argument("-e", "--extraction_guide", choices=["infernal", "vxtractor"], default="infernal",
help="Select whether to use Infernal's cmsearch or V-Xtractor guide sequence extraction.")
opt_args.add_argument("--infernal_threads", type=int, default=2,
help="Number of threads for each infernal process to use. "
"Must be greater than or equal to --threads. [ DEFAULT = 2 ]")
profile_args.add_argument("-c", "--cmfile", required=False, default=None,
help="Path to the covariance model file for cmsearch.")
profile_args.add_argument("-x", "--vxtractor_hmms", dest="hmm_path", required=False, default=None,
help="Path to a directory containing profile HMMs for V-Xtractor.")
opt_args.add_argument("-l", "--min_read_length", default=100, type=int,
help="The minimum sequence length for an input read. [ DEFAULT = 100 bp ]")
mis_args.add_argument("-n", "--threads", default=4, type=int,
help="The number of threads available for PEAR and cutadapt.")
mis_args.add_argument('-v', '--verbose', action='store_true', default=False,
help='Prints a more verbose runtime log.')
mis_args.add_argument("-d", "--delete", action="store_true", default=False,
help="Remove all intermediate files.")
mis_args.add_argument("-h", "--help",
action="help", help="Show this help message and exit.")
args = parser.parse_args(sys_args)
return args
def read_fastq_seq_names(fastq_file: str, num_reads=-1) -> list:
read_names = []
fx = Fastx(fastx_path=fastq_file, fx_format="fastq")
if fx.st_size == 0:
return read_names
i = 1
for name, _, _ in fx.fx_handler:
read_names.append(name)
if 0 < num_reads <= i:
break
i += 1
return read_names
def read_name_match(fwd_name: str, rev_name: str) -> bool:
def doctor_read_name_fn(read_name: str):
return re.sub(r'/[1-2]$', '', read_name.split()[0])
if doctor_read_name_fn(fwd_name) == doctor_read_name_fn(rev_name):
return True
else:
return False
def compare_read_names(fwd_read_names: list, rev_read_names: list) -> bool:
if len(fwd_read_names) != len(rev_read_names):
logging.error("Unable to compare read name lists of different size!\n")
raise AssertionError
i = 0
while i < len(fwd_read_names):
fn, rn = fwd_read_names[i], rev_read_names[i]
if not read_name_match(fn, rn):
return False
i += 1
return True
def write_fasta_from_dict(fasta_records: dict, fasta_path: str) -> None:
fasta_h = open(fasta_path, 'w')
for name, seq in fasta_records.items():
fasta_h.write(">{}\n{}\n".format(name, seq))
fasta_h.close()
return
def prep_for_analysis(input_dir, output_dir, temporary_dir) -> None: