forked from biopython/biopython
/
test_Emboss.py
1037 lines (947 loc) · 39.1 KB
/
test_Emboss.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
# Copyright 2009 by Peter Cock. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Runs a few EMBOSS tools to check our wrappers and parsers."""
import os
import sys
import unittest
import subprocess
from io import StringIO
from Bio.Emboss.Applications import WaterCommandline, NeedleCommandline
from Bio.Emboss.Applications import SeqretCommandline, SeqmatchallCommandline
from Bio import SeqIO
from Bio import AlignIO
from Bio import MissingExternalDependencyError
from Bio.Application import _escape_filename
from Bio.Alphabet import generic_protein, generic_dna, generic_nucleotide
from Bio.Seq import Seq, translate
from Bio.SeqRecord import SeqRecord
# from Bio.Data.IUPACData import ambiguous_dna_letters
# ###############################################################
# Try to avoid problems when the OS is in another language
os.environ["LANG"] = "C"
exes_wanted = ["water", "needle", "seqret", "transeq", "seqmatchall", "embossversion"]
exes = {} # Dictionary mapping from names to exe locations
if "EMBOSS_ROOT" in os.environ:
# Windows default installation path is C:\mEMBOSS which contains the exes.
# EMBOSS also sets an environment variable which we will check for.
path = os.environ["EMBOSS_ROOT"]
if os.path.isdir(path):
for name in exes_wanted:
if os.path.isfile(os.path.join(path, name + ".exe")):
exes[name] = os.path.join(path, name + ".exe")
del name
else:
raise MissingExternalDependencyError(
"$EMBOSS_ROOT=%r which does not exist!" % path
)
del path
if sys.platform != "win32":
from subprocess import getoutput
for name in exes_wanted:
# This will "just work" if installed on the path as normal on Unix
output = getoutput("%s -help" % name)
if "not found" not in output and "not recognized" not in output:
exes[name] = name
del output
del name
if len(exes) < len(exes_wanted):
raise MissingExternalDependencyError(
"Install EMBOSS if you want to use Bio.Emboss."
)
def get_emboss_version():
"""Return a tuple of three ints, e.g. (6,1,0)."""
# Windows and Unix versions of EMBOSS seem to differ in
# which lines go to stdout and stderr - so merge them.
child = subprocess.Popen(
_escape_filename(exes["embossversion"]),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
stdout, stderr = child.communicate()
child.stdout.close() # This is both stdout and stderr
del child
assert stderr is None # Send to stdout instead
for line in stdout.split("\n"):
if line.strip() == "Report the current EMBOSS version number":
# e.g.
# $ embossversion
# Report the current EMBOSS version number
# 6.5.7.0
pass
elif line.strip() == "Reports the current EMBOSS version number":
# e.g.
# $ embossversion
# Reports the current EMBOSS version number
# 6.3.1
pass
elif line.startswith("Writes the current EMBOSS version number"):
pass
elif line.count(".") == 2:
return tuple(int(v) for v in line.strip().split("."))
elif line.count(".") == 3:
# e.g. I installed mEMBOSS-6.2.0.1-setup.exe
# which reports 6.2.0.1 - for this return (6,2,0)
return tuple(int(v) for v in line.strip().split("."))[:3]
else:
# Either we can't understand the output, or this is really
# an error message not caught earlier (e.g. not in English)
raise MissingExternalDependencyError(
"Install EMBOSS if you want to use Bio.Emboss (%s)." % line
)
# In case there was no output at all...
raise MissingExternalDependencyError("Could not get EMBOSS version")
# To avoid confusing known errors from old versions of EMBOSS ...
emboss_version = get_emboss_version()
if emboss_version < (6, 1, 0):
raise MissingExternalDependencyError("Test requires EMBOSS 6.1.0 patch 3 or later.")
#################################################################
# Top level function as this makes it easier to use for debugging:
def emboss_convert(filename, old_format, new_format):
"""Run seqret, returns handle."""
# Setup, this assumes for all the format names used
# Biopython and EMBOSS names are consistent!
cline = SeqretCommandline(
exes["seqret"],
sequence=filename,
sformat=old_format,
osformat=new_format,
auto=True, # no prompting
stdout=True,
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
child.stdin.close()
child.stderr.close()
return child.stdout
# Top level function as this makes it easier to use for debugging:
def emboss_piped_SeqIO_convert(records, old_format, new_format):
"""Run seqret, returns records (as a generator)."""
# Setup, this assumes for all the format names used
# Biopython and EMBOSS names are consistent!
cline = SeqretCommandline(
exes["seqret"],
sformat=old_format,
osformat=new_format,
auto=True, # no prompting
filter=True,
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
SeqIO.write(records, child.stdin, old_format)
child.stdin.close()
child.stderr.close()
# TODO - Is there a nice way to return an iterator AND
# automatically close the handle?
records = list(SeqIO.parse(child.stdout, new_format))
child.stdout.close()
return records
# Top level function as this makes it easier to use for debugging:
def emboss_piped_AlignIO_convert(alignments, old_format, new_format):
"""Run seqret, returns alignments (as a generator)."""
# Setup, this assumes for all the format names used
# Biopython and EMBOSS names are consistent!
cline = SeqretCommandline(
exes["seqret"],
sformat=old_format,
osformat=new_format,
auto=True, # no prompting
filter=True,
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
try:
AlignIO.write(alignments, child.stdin, old_format)
except Exception as err:
child.stdin.close()
child.stderr.close()
child.stdout.close()
raise
child.stdin.close()
child.stderr.close()
# TODO - Is there a nice way to return an iterator AND
# automatically close the handle?
try:
aligns = list(AlignIO.parse(child.stdout, new_format))
except Exception as err:
child.stdout.close()
raise
child.stdout.close()
return aligns
# Top level function as this makes it easier to use for debugging:
def compare_records(old_list, new_list):
"""Check two lists of SeqRecords agree, raises a ValueError if mismatch."""
if len(old_list) != len(new_list):
raise ValueError("%i vs %i records" % (len(old_list), len(new_list)))
for old, new in zip(old_list, new_list):
# Note the name matching is a bit fuzzy, e.g. truncation and
# no spaces in PHYLIP files.
if (
old.id != new.id
and old.name != new.name
and (old.id not in new.id)
and (new.id not in old.id)
and (old.id.replace(" ", "_") != new.id.replace(" ", "_"))
):
raise ValueError(
"'%s' or '%s' vs '%s' or '%s' records"
% (old.id, old.name, new.id, new.name)
)
if len(old.seq) != len(new.seq):
raise ValueError("%i vs %i" % (len(old.seq), len(new.seq)))
if str(old.seq).upper() != str(new.seq).upper():
if str(old.seq).replace("X", "N") == str(new.seq):
raise ValueError("X -> N (protein forced into nucleotide?)")
if len(old.seq) < 200:
raise ValueError("'%s' vs '%s'" % (old.seq, new.seq))
else:
raise ValueError(
"'%s...%s' vs '%s...%s'"
% (old.seq[:60], old.seq[-10:], new.seq[:60], new.seq[-10:])
)
if old.features and new.features and len(old.features) != len(new.features):
raise ValueError(
"%i vs %i features" % (len(old.features), len(new.features))
)
# TODO - check annotation
return True
# Top level function as this makes it easier to use for debugging:
def compare_alignments(old_list, new_list):
"""Check two lists of Alignments agree, raises a ValueError if mismatch."""
if len(old_list) != len(new_list):
raise ValueError("%i vs %i alignments" % (len(old_list), len(new_list)))
for old, new in zip(old_list, new_list):
if len(old) != len(new):
raise ValueError("Alignment with %i vs %i records" % (len(old), len(new)))
compare_records(old, new)
return True
class SeqRetSeqIOTests(unittest.TestCase):
"""Check EMBOSS seqret against Bio.SeqIO for converting files."""
def tearDown(self):
clean_up()
def check_SeqIO_to_EMBOSS(
self, in_filename, in_format, skip_formats=(), alphabet=None
):
"""Check SeqIO writes files seqret can read back."""
if alphabet:
records = list(SeqIO.parse(in_filename, in_format, alphabet))
else:
records = list(SeqIO.parse(in_filename, in_format))
for temp_format in ["genbank", "embl", "fasta"]:
if temp_format in skip_formats:
continue
new_records = list(
emboss_piped_SeqIO_convert(records, temp_format, "fasta")
)
try:
self.assertTrue(compare_records(records, new_records))
except ValueError as err:
raise ValueError(
"Disagree on file %s %s in %s format: %s"
% (in_format, in_filename, temp_format, err)
) from None
def check_EMBOSS_to_SeqIO(self, filename, old_format, skip_formats=()):
"""Check SeqIO can read read seqret's conversion output."""
# TODO: Why can't we read EMBOSS's swiss output?
self.assertTrue(os.path.isfile(filename))
old_records = list(SeqIO.parse(filename, old_format))
for new_format in ["genbank", "fasta", "pir", "embl", "ig"]:
if new_format in skip_formats:
continue
handle = emboss_convert(filename, old_format, new_format)
new_records = list(SeqIO.parse(handle, new_format))
handle.close()
try:
self.assertTrue(compare_records(old_records, new_records))
except ValueError as err:
raise ValueError(
"Disagree on %s file %s in %s format: %s"
% (old_format, filename, new_format, err)
) from None
def check_SeqIO_with_EMBOSS(
self, filename, old_format, skip_formats=(), alphabet=None
):
# Check EMBOSS can read Bio.SeqIO output...
self.check_SeqIO_to_EMBOSS(filename, old_format, skip_formats, alphabet)
# Check Bio.SeqIO can read EMBOSS seqret output...
self.check_EMBOSS_to_SeqIO(filename, old_format, skip_formats)
def test_abi(self):
"""Check SeqIO agrees with EMBOSS' Abi to FASTQ conversion."""
# This lets use check the id, sequence, and quality scores
for filename in ["Abi/3730.ab1", "Abi/empty.ab1"]:
old = SeqIO.read(filename, "abi")
handle = emboss_convert(filename, "abi", "fastq-sanger")
new = SeqIO.read(handle, "fastq-sanger")
handle.close()
if emboss_version == (6, 4, 0) and new.id == "EMBOSS_001":
# Avoid bug in EMBOSS 6.4.0 (patch forthcoming)
pass
else:
self.assertEqual(old.id, new.id)
self.assertEqual(str(old.seq), str(new.seq))
if emboss_version < (6, 3, 0) and new.letter_annotations[
"phred_quality"
] == [1] * len(old):
# Apparent bug in EMBOSS 6.2.0.1 on Windows
pass
else:
self.assertEqual(old.letter_annotations, new.letter_annotations)
def test_genbank(self):
"""Check SeqIO & EMBOSS reading each other's conversions of a GenBank file."""
self.check_SeqIO_with_EMBOSS("GenBank/cor6_6.gb", "genbank")
def test_genbank2(self):
"""Check SeqIO & EMBOSS reading each other's conversions of another GenBank file."""
self.check_SeqIO_with_EMBOSS("GenBank/NC_000932.gb", "genbank")
def test_embl(self):
"""Check SeqIO & EMBOSS reading each other's conversions of an EMBL file."""
self.check_SeqIO_with_EMBOSS("EMBL/U87107.embl", "embl")
def test_ig(self):
"""Check SeqIO & EMBOSS reading each other's conversions of an ig file."""
# NOTE - EMBOSS considers "genbank" to be for nucleotides only,
# and will turn "X" into "N" for GenBank output.
self.check_SeqIO_to_EMBOSS(
"IntelliGenetics/VIF_mase-pro.txt",
"ig",
alphabet=generic_protein,
skip_formats=["genbank", "embl"],
)
# TODO - What does a % in an ig sequence mean?
# e.g. "IntelliGenetics/vpu_nucaligned.txt"
# and "IntelliGenetics/TAT_mase_nuc.txt"
# EMBOSS seems to ignore them.
def test_pir(self):
"""Check SeqIO & EMBOSS reading each other's conversions of a PIR file."""
# Skip genbank here, EMBOSS mangles the LOCUS line:
self.check_SeqIO_with_EMBOSS(
"NBRF/clustalw.pir", "pir", skip_formats=["genbank"]
)
# Skip EMBL here, EMBOSS mangles the ID line
# Skip GenBank, EMBOSS 6.0.1 on Windows won't output proteins as GenBank
self.check_SeqIO_with_EMBOSS(
"NBRF/DMB_prot.pir", "pir", skip_formats=["embl", "genbank"]
)
def test_clustalw(self):
"""Check SeqIO & EMBOSS reading each other's conversions of a Clustalw file."""
self.check_SeqIO_with_EMBOSS(
"Clustalw/hedgehog.aln", "clustal", skip_formats=["embl", "genbank"]
)
self.check_SeqIO_with_EMBOSS(
"Clustalw/opuntia.aln", "clustal", skip_formats=["embl", "genbank"]
)
class SeqRetAlignIOTests(unittest.TestCase):
"""Check EMBOSS seqret against Bio.AlignIO for converting files."""
def tearDown(self):
clean_up()
def check_EMBOSS_to_AlignIO(self, filename, old_format, skip_formats=()):
"""Check AlignIO can read seqret's conversion of the file."""
self.assertTrue(os.path.isfile(filename), filename)
old_aligns = list(AlignIO.parse(filename, old_format))
formats = ["clustal", "phylip", "ig", "msf"]
if len(old_aligns) == 1:
formats.extend(["fasta", "nexus"])
for new_format in formats:
if new_format in skip_formats:
continue
handle = emboss_convert(filename, old_format, new_format)
try:
new_aligns = list(AlignIO.parse(handle, new_format))
except Exception: # TODO - Which exceptions?
handle.close()
raise ValueError(
"Can't parse %s file %s in %s format."
% (old_format, filename, new_format)
) from None
handle.close()
try:
self.assertTrue(compare_alignments(old_aligns, new_aligns))
except ValueError as err:
raise ValueError(
"Disagree on %s file %s in %s format: %s"
% (old_format, filename, new_format, err)
) from None
def check_AlignIO_to_EMBOSS(
self, in_filename, in_format, skip_formats=(), alphabet=None
):
"""Check Bio.AlignIO can write files seqret can read."""
if alphabet:
old_aligns = list(AlignIO.parse(in_filename, in_format, alphabet))
else:
old_aligns = list(AlignIO.parse(in_filename, in_format))
formats = ["clustal", "phylip"]
if len(old_aligns) == 1:
formats.extend(["fasta", "nexus"])
for temp_format in formats:
if temp_format in skip_formats:
continue
# PHYLIP is a simple format which explicitly supports
# multiple alignments (unlike FASTA).
try:
new_aligns = list(
emboss_piped_AlignIO_convert(old_aligns, temp_format, "phylip")
)
except ValueError as e:
# e.g. ValueError: Need a DNA, RNA or Protein alphabet
# from writing Nexus files...
continue
try:
self.assertTrue(compare_alignments(old_aligns, new_aligns))
except ValueError as err:
raise ValueError(
"Disagree on file %s %s in %s format: %s"
% (in_format, in_filename, temp_format, err)
) from None
def check_AlignIO_with_EMBOSS(
self, filename, old_format, skip_formats=(), alphabet=None
):
# Check EMBOSS can read Bio.AlignIO output...
self.check_AlignIO_to_EMBOSS(filename, old_format, skip_formats, alphabet)
# Check Bio.AlignIO can read EMBOSS seqret output...
self.check_EMBOSS_to_AlignIO(filename, old_format, skip_formats)
def test_align_clustalw(self):
"""Check AlignIO & EMBOSS reading each other's conversions of a ClustalW file."""
self.check_AlignIO_with_EMBOSS("Clustalw/hedgehog.aln", "clustal")
self.check_AlignIO_with_EMBOSS("Clustalw/opuntia.aln", "clustal")
self.check_AlignIO_with_EMBOSS(
"Clustalw/odd_consensus.aln", "clustal", skip_formats=["nexus"]
) # TODO - why not nexus?
self.check_AlignIO_with_EMBOSS("Clustalw/protein.aln", "clustal")
self.check_AlignIO_with_EMBOSS("Clustalw/promals3d.aln", "clustal")
def test_clustalw(self):
"""Check AlignIO & EMBOSS reading each other's conversions of a PHYLIP file."""
self.check_AlignIO_with_EMBOSS("Phylip/horses.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/hennigian.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/reference_dna.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/reference_dna2.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/interlaced.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/interlaced2.phy", "phylip")
self.check_AlignIO_with_EMBOSS("Phylip/random.phy", "phylip")
class PairwiseAlignmentTests(unittest.TestCase):
"""Run pairwise alignments with water and needle, and parse them."""
def tearDown(self):
clean_up()
def pairwise_alignment_check(self, query_seq, targets, alignments, local=True):
"""Check pairwise alignment data is sane."""
# The datasets should be small, so making iterators into lists is OK
targets = list(targets)
alignments = list(alignments)
self.assertEqual(len(targets), len(alignments))
for target, alignment in zip(targets, alignments):
self.assertEqual(len(alignment), 2)
# self.assertEqual(target.id, alignment[1].id) #too strict
if alignment[1].id not in target.id and alignment[1].id not in target.name:
raise AssertionError(
"%s vs %s or %s" % (alignment[1].id, target.id, target.name)
)
if local:
# Local alignment
self.assertIn(str(alignment[0].seq).replace("-", ""), query_seq)
self.assertIn(
str(alignment[1].seq).replace("-", "").upper(),
str(target.seq).upper(),
)
else:
# Global alignment
self.assertEqual(str(query_seq), str(alignment[0].seq).replace("-", ""))
self.assertEqual(
str(target.seq).upper(),
str(alignment[1].seq).replace("-", "").upper(),
)
return True
def run_water(self, cline):
# Run the tool,
stdout, stderr = cline()
self.assertTrue(
stderr.strip().startswith("Smith-Waterman local alignment"), stderr
)
if cline.outfile:
self.assertEqual(stdout.strip(), "")
self.assertTrue(
os.path.isfile(cline.outfile),
"Missing output file %r from:\n%s" % (cline.outfile, cline),
)
else:
# Don't use this yet... could return stdout handle instead?
return stdout
def test_water_file(self):
"""Run water with the asis trick, output to a file."""
# Setup, try a mixture of keyword arguments and later additions:
cline = WaterCommandline(cmd=exes["water"], gapopen="10", gapextend="0.5")
# Try using both human readable names, and the literal ones:
cline.set_parameter("asequence", "asis:ACCCGGGCGCGGT")
cline.set_parameter("-bsequence", "asis:ACCCGAGCGCGGT")
# Try using a property set here:
cline.outfile = "Emboss/temp with space.water"
self.assertEqual(str(eval(repr(cline))), str(cline))
# Run the tool,
self.run_water(cline)
# Check we can parse the output...
align = AlignIO.read(cline.outfile, "emboss")
self.assertEqual(len(align), 2)
self.assertEqual(str(align[0].seq), "ACCCGGGCGCGGT")
self.assertEqual(str(align[1].seq), "ACCCGAGCGCGGT")
# Clean up,
os.remove(cline.outfile)
def test_water_piped(self):
"""Run water with asis trick, output piped to stdout."""
cline = WaterCommandline(
cmd=exes["water"],
asequence="asis:ACCCGGGCGCGGT",
bsequence="asis:ACCCGAGCGCGGT",
gapopen=10,
gapextend=0.5,
auto=True,
filter=True,
)
self.assertEqual(
str(cline),
exes["water"]
+ " -auto -filter"
+ " -asequence=asis:ACCCGGGCGCGGT"
+ " -bsequence=asis:ACCCGAGCGCGGT"
+ " -gapopen=10 -gapextend=0.5",
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
child.stdin.close()
# Check we could read it's output
align = AlignIO.read(child.stdout, "emboss")
self.assertEqual(len(align), 2)
self.assertEqual(str(align[0].seq), "ACCCGGGCGCGGT")
self.assertEqual(str(align[1].seq), "ACCCGAGCGCGGT")
# Check no error output:
self.assertEqual(child.stderr.read(), "")
self.assertEqual(0, child.wait())
child.stdout.close()
child.stderr.close()
def test_needle_file(self):
"""Run needle with the asis trick, output to a file."""
# Setup,
cline = NeedleCommandline(cmd=exes["needle"])
cline.set_parameter("-asequence", "asis:ACCCGGGCGCGGT")
cline.set_parameter("-bsequence", "asis:ACCCGAGCGCGGT")
cline.set_parameter("-gapopen", "10")
cline.set_parameter("-gapextend", "0.5")
# EMBOSS would guess this, but let's be explicit:
cline.set_parameter("-snucleotide", "True")
cline.set_parameter("-outfile", "Emboss/temp with space.needle")
self.assertEqual(str(eval(repr(cline))), str(cline))
# Run the tool,
stdout, stderr = cline()
# Check it worked,
self.assertTrue(
stderr.strip().startswith("Needleman-Wunsch global alignment"), stderr
)
self.assertEqual(stdout.strip(), "")
filename = cline.outfile
self.assertTrue(
os.path.isfile(filename),
"Missing output file %r from:\n%s" % (filename, cline),
)
# Check we can parse the output...
align = AlignIO.read(filename, "emboss")
self.assertEqual(len(align), 2)
self.assertEqual(str(align[0].seq), "ACCCGGGCGCGGT")
self.assertEqual(str(align[1].seq), "ACCCGAGCGCGGT")
# Clean up,
os.remove(filename)
def test_needle_piped(self):
"""Run needle with asis trick, output piped to stdout."""
cline = NeedleCommandline(
cmd=exes["needle"],
asequence="asis:ACCCGGGCGCGGT",
bsequence="asis:ACCCGAGCGCGGT",
gapopen=10,
gapextend=0.5,
auto=True,
filter=True,
)
self.assertEqual(
str(cline),
exes["needle"]
+ " -auto -filter"
+ " -asequence=asis:ACCCGGGCGCGGT"
+ " -bsequence=asis:ACCCGAGCGCGGT"
+ " -gapopen=10 -gapextend=0.5",
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
child.stdin.close()
# Check we could read it's output
align = AlignIO.read(child.stdout, "emboss")
self.assertEqual(len(align), 2)
self.assertEqual(str(align[0].seq), "ACCCGGGCGCGGT")
self.assertEqual(str(align[1].seq), "ACCCGAGCGCGGT")
# Check no error output:
self.assertEqual(child.stderr.read(), "")
self.assertEqual(0, child.wait())
child.stdout.close()
child.stderr.close()
def test_water_file2(self):
"""Run water with the asis trick and nucleotide FASTA file, output to a file."""
# Setup,
query = "ACACACTCACACACACTTGGTCAGAGATGCTGTGCTTCTTGGAAGCAAGGNCTCAAAGGCAAGGTGCACGCAGAGGGACGTTTGAGTCTGGGATGAAGCATGTNCGTATTATTTATATGATGGAATTTCACGTTTTTATG"
out_file = "Emboss/temp_test2.water"
in_file = "Fasta/f002"
self.assertTrue(os.path.isfile(in_file))
if os.path.isfile(out_file):
os.remove(out_file)
cline = WaterCommandline(cmd=exes["water"])
cline.set_parameter("-asequence", "asis:%s" % query)
cline.set_parameter("-bsequence", in_file)
cline.set_parameter("-gapopen", "10")
cline.set_parameter("-gapextend", "0.5")
cline.set_parameter("-outfile", out_file)
self.assertEqual(str(eval(repr(cline))), str(cline))
# Run the tool,
self.run_water(cline)
# Check we can parse the output and it is sensible...
self.pairwise_alignment_check(
query,
SeqIO.parse(in_file, "fasta"),
AlignIO.parse(out_file, "emboss"),
local=True,
)
# Clean up,
os.remove(out_file)
def test_water_file3(self):
"""Run water with the asis trick and GenBank file, output to a file."""
# Setup,
query = "TGTTGTAATGTTTTAATGTTTCTTCTCCCTTTAGATGTACTACGTTTGGA"
out_file = "Emboss/temp_test3.water"
in_file = "GenBank/cor6_6.gb"
self.assertTrue(os.path.isfile(in_file))
if os.path.isfile(out_file):
os.remove(out_file)
cline = WaterCommandline(cmd=exes["water"])
cline.set_parameter("asequence", "asis:%s" % query)
cline.set_parameter("bsequence", in_file)
# TODO - Tell water this is a GenBank file!
cline.set_parameter("gapopen", "1")
cline.set_parameter("gapextend", "0.5")
cline.set_parameter("outfile", out_file)
self.assertEqual(str(eval(repr(cline))), str(cline))
# Run the tool,
self.run_water(cline)
# Check we can parse the output and it is sensible...
self.pairwise_alignment_check(
query,
SeqIO.parse(in_file, "genbank"),
AlignIO.parse(out_file, "emboss"),
local=True,
)
# Clean up,
os.remove(out_file)
def test_water_file4(self):
"""Run water with the asis trick and SwissProt file, output to a file."""
# Setup,
query = "DVCTGKALCDPVTQNIKTYPVKIENLRVMI"
out_file = "Emboss/temp_test4.water"
in_file = "SwissProt/P0A186.txt"
self.assertTrue(os.path.isfile(in_file))
if os.path.isfile(out_file):
os.remove(out_file)
cline = WaterCommandline(cmd=exes["water"])
cline.set_parameter("-asequence", "asis:%s" % query)
cline.set_parameter("-bsequence", in_file)
# EMBOSS should work this out, but let's be explicit:
cline.set_parameter("-sprotein", True)
# TODO - Tell water this is a SwissProt file!
cline.set_parameter("-gapopen", "20")
cline.set_parameter("-gapextend", "5")
cline.set_parameter("-outfile", out_file)
self.assertEqual(str(eval(repr(cline))), str(cline))
# Run the tool,
self.run_water(cline)
# Check we can parse the output and it is sensible...
self.pairwise_alignment_check(
query,
SeqIO.parse(in_file, "swiss"),
AlignIO.parse(out_file, "emboss"),
local=True,
)
# Clean up,
os.remove(out_file)
def test_needle_piped2(self):
"""Run needle with asis trick, and nucleotide FASTA file, output piped to stdout."""
# TODO - Support needle in Bio.Emboss.Applications
# (ideally with the -auto and -filter arguments)
# Setup,
query = "ACACACTCACACACACTTGGTCAGAGATGCTGTGCTTCTTGGAA"
cline = exes["needle"]
cline += " -asequence asis:" + query
cline += " -bsequence Fasta/f002"
cline += " -auto" # no prompting
cline += " -filter" # use stdout
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
child.stdin.close()
# Check we can parse the output and it is sensible...
self.pairwise_alignment_check(
query,
SeqIO.parse("Fasta/f002", "fasta"),
AlignIO.parse(child.stdout, "emboss"),
local=False,
)
# Check no error output:
self.assertEqual(child.stderr.read(), "")
self.assertEqual(0, child.wait())
child.stdout.close()
child.stderr.close()
def test_water_needs_output(self):
"""Run water without output file or stdout/filter should give error."""
cline = WaterCommandline(
cmd=exes["water"],
asequence="asis:ACCCGGGCGCGGT",
bsequence="asis:ACCCGAGCGCGGT",
gapopen=10,
gapextend=0.5,
auto=True,
)
self.assertTrue(cline.auto)
self.assertTrue(not cline.stdout)
self.assertTrue(not cline.filter)
self.assertEqual(cline.outfile, None)
self.assertRaises(ValueError, str, cline)
def test_needle_needs_output(self):
"""Run needle without output file or stdout/filter should give error."""
cline = NeedleCommandline(
cmd=exes["needle"],
asequence="asis:ACCCGGGCGCGGT",
bsequence="asis:ACCCGAGCGCGGT",
gapopen=10,
gapextend=0.5,
auto=True,
)
self.assertTrue(cline.auto)
self.assertTrue(not cline.stdout)
self.assertTrue(not cline.filter)
self.assertEqual(cline.outfile, None)
self.assertRaises(ValueError, str, cline)
def test_seqtmatchall_piped(self):
"""Run seqmatchall with pair output piped to stdout."""
cline = SeqmatchallCommandline(
cmd=exes["seqmatchall"],
sequence="Fasta/f002",
aformat="pair",
wordsize=9,
auto=True,
stdout=True,
)
self.assertEqual(
str(cline),
exes["seqmatchall"]
+ " -auto -stdout"
+ " -sequence=Fasta/f002"
+ " -wordsize=9 -aformat=pair",
)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
child.stdin.close()
# Check we could read it's output
for align in AlignIO.parse(child.stdout, "emboss"):
self.assertEqual(len(align), 2)
self.assertEqual(align.get_alignment_length(), 9)
# Check no error output:
self.assertEqual(child.stderr.read(), "")
self.assertEqual(0, child.wait())
child.stdout.close()
child.stderr.close()
# Top level function as this makes it easier to use for debugging:
def emboss_translate(sequence, table=None, frame=None):
"""Call transeq, returns protein sequence as string."""
# TODO - Support transeq in Bio.Emboss.Applications?
# (doesn't seem worthwhile as Biopython can do translations)
if not sequence:
raise ValueError(sequence)
# Setup,
cline = exes["transeq"]
if len(sequence) < 100:
filename = None
cline += " -sequence asis:%s" % sequence
else:
# There are limits on command line string lengths...
# use a temp file instead.
filename = "Emboss/temp_transeq.txt"
SeqIO.write(SeqRecord(sequence, id="Test"), filename, "fasta")
cline += " -sequence %s" % filename
cline += " -auto" # no prompting
cline += " -filter" # use stdout
if table is not None:
cline += " -table %s" % str(table)
if frame is not None:
cline += " -frame %s" % str(frame)
# Run the tool,
child = subprocess.Popen(
str(cline),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=(sys.platform != "win32"),
)
out, err = child.communicate()
# Check no error output:
if err != "":
raise ValueError(str(cline) + "\n" + err)
# Check we could read it's output
record = SeqIO.read(StringIO(out), "fasta")
if 0 != child.wait():
raise ValueError(str(cline))
if filename:
os.remove(filename)
if not record.id.startswith("Test"):
raise ValueError(str(cline))
else:
if not record.id.startswith("asis"):
raise ValueError(str(cline))
return str(record.seq)
# Top level function as this makes it easier to use for debugging:
def check_translation(sequence, translation, table=None):
if table is None:
t = 1
else:
t = table
if (
translation != str(sequence.translate(t))
or translation != str(translate(sequence, t))
or translation != translate(str(sequence), t)
):
# More details...
for i, amino in enumerate(translation):
codon = sequence[i * 3 : i * 3 + 3]
if amino != str(codon.translate(t)):
raise ValueError(
"%s -> %s not %s (table %s)" % (codon, amino, codon.translate(t), t)
)
# Shouldn't reach this line:
raise ValueError("%s -> %s (table %s)" % (sequence, translation, t))
return True
class TranslationTests(unittest.TestCase):
"""Run pairwise alignments with water and needle, and parse them."""
def tearDown(self):
clean_up()
def test_simple(self):
"""Run transeq vs Bio.Seq for simple translations (including alt tables)."""
examples = [
Seq("ACGTGACTGACGTAGCATGCCACTAGG"),
# Unamibguous TA? codons:
Seq("TAATACTATTAG", generic_dna),
# Most of the ambiguous TA? codons:
Seq("TANTARTAYTAMTAKTAHTABTADTAV", generic_dna),
# Problem cases,
#
# Seq("TAW", generic_dna),
# W = A or T, but EMBOSS does TAW -> X
# TAA -> Y, TAT ->Y, so in Biopython TAW -> Y
#
# Seq("TAS", generic_dna),
# S = C or G, but EMBOSS does TAS -> Y
# TAG -> *, TAC ->Y, so in Biopython TAS -> X (Y or *)
#
# Seq("AAS", generic_dna),
# On table 9, EMBOSS gives N, we give X.
# S = C or G, so according to my reading of
# table 9 on the NCBI page, AAC=N, AAG=K
# suggesting this is a bug in EMBOSS.
#
Seq("ACGGGGGGGGTAAGTGGTGTGTGTGTAGT", generic_dna),
]
for sequence in examples:
# EMBOSS treats spare residues differently... avoid this issue
if len(sequence) % 3 != 0:
sequence = sequence[: -(len(sequence) % 3)]
self.assertEqual(len(sequence) % 3, 0)
self.assertTrue(len(sequence) > 0)
self.check(sequence)
def check(self, sequence):
"""Compare our translation to EMBOSS's using all tables.
Takes a Seq object (and a filename containing it).
"""
translation = emboss_translate(sequence)
self.assertTrue(check_translation(sequence, translation))
for table in [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14, 15, 16, 21, 22, 23]:
translation = emboss_translate(sequence, table)
self.assertTrue(check_translation(sequence, translation, table))
return True
def translate_all_codons(self, letters):