forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·147 lines (111 loc) · 3.99 KB
/
test.py
File metadata and controls
executable file
·147 lines (111 loc) · 3.99 KB
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
#!/usr/bin/env python3
"""tests for moog.py"""
import os
import random
import re
import string
from subprocess import getstatusoutput
from Bio import SeqIO
from Bio.SeqUtils import GC
from numpy import mean
from itertools import chain
prg = './moog.py'
# --------------------------------------------------
def random_string():
"""generate a random string"""
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
# --------------------------------------------------
def test_exists():
"""usage"""
assert os.path.isfile(prg)
# --------------------------------------------------
def test_usage():
"""usage"""
for flag in ['-h', '--help']:
rv, out = getstatusoutput('{} {}'.format(prg, flag))
assert rv == 0
assert re.match("usage", out, re.IGNORECASE)
# --------------------------------------------------
def test_bad_seqtype():
"""die on bad seqtype"""
bad = random_string()
rv, out = getstatusoutput(f'{prg} -t {bad}')
assert rv != 0
assert re.match('usage:', out, re.I)
assert re.search(
f"-t/--seqtype: invalid choice: '{bad}' \(choose from 'dna', 'rna'\)",
out)
# --------------------------------------------------
def test_bad_pctgc():
"""die on bad pctgc"""
bad = random.randint(1, 10)
rv, out = getstatusoutput(f'{prg} -p {bad}')
assert rv != 0
assert re.match('usage:', out, re.I)
assert re.search(f'--pctgc "{float(bad)}" must be between 0 and 1', out)
# --------------------------------------------------
def test_defaults():
"""runs on good input"""
out_file = 'out.fa'
try:
if os.path.isfile(out_file):
os.remove(out_file)
rv, out = getstatusoutput(prg)
assert rv == 0
assert out == f'Done, wrote 10 DNA sequences to "{out_file}".'
assert os.path.isfile(out_file)
# correct number of seqs
seqs = list(SeqIO.parse(out_file, 'fasta'))
assert len(seqs) == 10
# the lengths are in the correct range
seq_lens = list(map(lambda seq: len(seq.seq), seqs))
assert max(seq_lens) <= 75
assert min(seq_lens) >= 50
# bases are correct
bases = ''.join(
sorted(
set(chain(map(lambda seq: ''.join(sorted(set(seq.seq))),
seqs)))))
assert bases == 'ACGT'
# the pct GC is about right
gc = list(map(lambda seq: GC(seq.seq) / 100, seqs))
assert .47 <= mean(gc) <= .53
finally:
if os.path.isfile(out_file):
os.remove(out_file)
# --------------------------------------------------
def test_options():
"""runs on good input"""
out_file = random_string() + '.fasta'
try:
if os.path.isfile(out_file):
os.remove(out_file)
min_len = random.randint(50, 99)
max_len = random.randint(100, 150)
num_seqs = random.randint(100, 150)
pct_gc = random.random()
cmd = (f'{prg} -m {min_len} -x {max_len} -o {out_file} '
f'-n {num_seqs} -t rna -p {pct_gc:.02f} -s 1')
rv, out = getstatusoutput(cmd)
assert rv == 0
assert out == f'Done, wrote {num_seqs} RNA sequences to "{out_file}".'
assert os.path.isfile(out_file)
# correct number of seqs
seqs = list(SeqIO.parse(out_file, 'fasta'))
assert len(seqs) == num_seqs
# the lengths are in the correct range
seq_lens = list(map(lambda seq: len(seq.seq), seqs))
assert max(seq_lens) <= max_len
assert min(seq_lens) >= min_len
# bases are correct
bases = ''.join(
sorted(
set(chain(map(lambda seq: ''.join(sorted(set(seq.seq))),
seqs)))))
assert bases == 'ACGU'
# the pct GC is about right
gc = list(map(lambda seq: GC(seq.seq) / 100, seqs))
assert pct_gc - .3 <= mean(gc) <= pct_gc + .3
finally:
if os.path.isfile(out_file):
os.remove(out_file)