1#!/usr/local/bin/python3.8
2
3# Integrated tests of esl-shuffle miniapp
4#
5# Usage:   ./esl-shuffle.itest.py <builddir> <srcdir> <tmpfile_prefix>
6# Example: ./esl-shuffle.itest.py .. .. tmpfoo
7#
8# SRE, Sat 07 Jul 2018 [Woodland Lodge, Titusville PA]
9# supersedes esl-shuffle.itest.pl
10#
11import sys
12import os
13import subprocess
14
15if len(sys.argv) != 4:
16    sys.exit('usage: esl-shuffle.itest.py <builddir> <srcdir> <tmppfx>')
17
18builddir = sys.argv[1]
19srcdir   = sys.argv[2]
20tmppfx   = sys.argv[3]
21
22if not os.path.isfile('{0}/miniapps/esl-shuffle'.format(builddir)):
23    sys.exit('FAIL: no esl-shuffle program in {0}'.format(builddir))
24errmsg = 'FAIL: esl-shuffle.itest.py integration test failed'
25
26
27# <tmppfx>.sto is a test alignment file
28#
29with open('{0}.sto'.format(tmppfx), 'w') as f:
30    print(
31"""# STOCKHOLM 1.0
32
33seq1 ACDEFGHIKLMNPQRSTVWY
34seq2 ACDEFGHIKLMNPQRSTVWY
35seq3 ACDEFGHIKLMNPQRSTVWY
36seq4 ACDEFGHIKLMNPQRSTVWY
37seq5 ACDEFGHIKLMNPQRSTVWY
38//
39""", end='', file=f)
40
41# <tmppfx>.fa is a test sequence file
42with open('{0}.fa'.format(tmppfx), 'w') as f:
43    print(
44""">seq1
45ACDEFGHIKLMNPQRSTVWY
46>seq2
47ACACACACACACACACACAC
48>seq3
49WYWYWYWYWYWYWYWYWYWY
50""", end='', file=f)
51
52
53# Use of --seed makes shuffled outputs reproducible, regressable.
54# Until you change the RNG again, anyway. If you do that, all these
55# regressions need to change.
56#
57try:
58    output = subprocess.check_output([ '{}/miniapps/esl-shuffle'.format(builddir), '--seed', '42', '{}.fa'.format(tmppfx) ],
59                                     stderr=subprocess.STDOUT, universal_newlines=True)
60except:
61    sys.exit(errmsg)
62
63lines = output.splitlines()
64if len(lines) != 6:                         sys.exit(errmsg)
65if (lines[0] != '>seq1-shuffled'        or
66    lines[1] != 'TIGEYHFWCKVSALQNPDRM'  or
67    lines[2] != '>seq2-shuffled'        or
68    lines[3] != 'CACAAAACCCACCAACAACC'  or
69    lines[4] != '>seq3-shuffled'        or
70    lines[5] != 'WWYYWWYWWYYWYYWYYWYW'):    sys.exit(errmsg)
71
72# We had bugs in the -N option at one point.  This test exercises the
73# bugs.
74#
75try:
76    output = subprocess.check_output([ '{}/miniapps/esl-shuffle'.format(builddir), '--seed', '42', '-N', '2', '{}.fa'.format(tmppfx) ],
77                                     stderr=subprocess.STDOUT, universal_newlines=True)
78except:
79    sys.exit(errmsg)
80
81lines = output.splitlines()
82if len(lines) != 12:                     sys.exit(errmsg)
83if (lines[2] != '>seq1-shuffled-1'  or
84    lines[3] != 'NTEPDRFIQYKLCMWVHAGS'): sys.exit(errmsg);
85
86# Easel iss #24 was a silly, untested failure of esl-shuffle -A
87#
88try:
89    output = subprocess.check_output([ '{}/miniapps/esl-shuffle'.format(builddir), '--seed', '42', '-A', '{}.sto'.format(tmppfx) ],
90                                     stderr=subprocess.STDOUT, universal_newlines=True)
91except:
92    sys.exit(errmsg)
93
94lines = output.splitlines()
95if len(lines) != 9:                           sys.exit(errmsg)
96if (lines[3] != 'seq1 TIGEYHFWCKVSALQNPDRM'): sys.exit(errmsg)
97
98print('ok')
99
100os.remove('{0}.sto'.format(tmppfx))
101os.remove('{0}.fa'.format(tmppfx))
102sys.exit(0)
103