-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
executable file
·179 lines (149 loc) · 7.48 KB
/
testing.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
import unittest
from abundance import annotation
from args import Args
import translate_search
from quantify_hmm import quantify_hmm
from annotation import blast_search
from pathmanager import PathManager
from output import create_html, remove_temps
import PlasticTools
import os
import database_operations
data_folder = "/home/jasper/Thesis/"
args1 = Args(
output = f'{data_folder}Test_data/output',
contigs = f'{data_folder}Test_data/contigs-fixed.fa',
plastic = 'all',
mappings=f'{data_folder}Test_data/GC125633.bam,{data_folder}Test_data/GC125648.bam,{data_folder}Test_data/GC125657.bam,{data_folder}Test_data/GC125668.bam')
args2 = Args(
output = f'{data_folder}Test_data2/output',
contigs = f'{data_folder}Test_data2/Citadell.contigs.fa',
plastic = 'all',
mappings=f'{data_folder}Test_data2/GC125618.sorted.bam,{data_folder}Test_data2/GC127864.sorted.bam')
class TestPlasticTools(unittest.TestCase):
def setUp(self):
self.args = args2
self.p = PathManager(self.args)
#database_operations.database_fetch(self.args.plastic, self.args.output)
def test1_run_prodigal(self):
# Try to run the function
try:
translate_search.run_prodigal(self.p)
except Exception as e:
print(e)
# Check that the output directories were created
self.assertTrue(os.path.exists(self.p.temps))
# Check that the .faa and .ffn files were created
contigs_base = os.path.basename(self.p.contigs).split(".")[0]
aa_file = os.path.join(self.p.output, "temps", f"{contigs_base}.faa")
nt_file = os.path.join(self.p.output, "temps", f"{contigs_base}.ffn")
self.assertTrue(os.path.exists(aa_file))
self.assertTrue(os.path.exists(nt_file))
# Check that the prodigal log file was created
prodigal_log_file = os.path.join(self.p.output, "temps", "prodigal.log")
self.assertTrue(os.path.exists(prodigal_log_file))
def test2_run_hmmer(self):
# Try to run the function
try:
translate_search.run_hmmer(self.p)
except Exception as e:
print(e)
# Define contigs_base
contigs_base = os.path.basename(self.p.contigs).split(".")[0]
# Check that the hmmsearch output and log files were created for each plastic type
if self.p.plastic == "all":
plastic_names = self.p.all_plastics
else:
plastic_names = self.p.plastic.split(',')
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
hmm_output = os.path.join(temp_dir, f"{contigs_base}_{plastic_name}_HMMER.out")
log_file = os.path.join(temp_dir, f"{plastic_name}_hmmsearch.log")
self.assertTrue(os.path.exists(hmm_output), f"{plastic_name}: {hmm_output}")
self.assertTrue(os.path.exists(log_file), f"{plastic_name}: {log_file}")
# Check that the hmmsearch output fasta file was created for each plastic type
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
output = os.path.join(temp_dir, f"{contigs_base}_{plastic_name}_hmm_output.fasta")
self.assertTrue(os.path.exists(output), f"{plastic_name}: {output}")
def test4_blast_search(self):
# Try to run the function
try:
blast_search.blast_search(self.p)
except Exception as e:
print(e)
# Check that the output directories were created
self.assertTrue(os.path.exists(self.p.temps))
# Check that the annotation log file was created
log_file = os.path.join(self.p.temps, "annotation.log")
self.assertTrue(os.path.exists(log_file), log_file)
# Check that the annotation Excel files were created for each plastic type
if self.p.plastic == "all":
plastic_names = self.p.all_plastics
else:
plastic_names = self.p.plastic.split(',')
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
output_file = os.path.join(temp_dir, f"{self.p.contigs_base}_{plastic_name}_hmm_output_annotation.xlsx")
self.assertTrue(os.path.exists(output_file), f"{plastic_name}: {output_file}")
def test5_annotation(self):
# Try to run the function
try:
annotation(self.p)
except Exception as e:
print(e)
# Check that the output directories were created
self.assertTrue(os.path.exists(self.p.temps))
# Define contigs_base
contigs_base = os.path.basename(self.p.contigs).split(".")[0]
# Check that the corrected .ffn and .saf files were created for each plastic type
if self.p.plastic == "all":
plastic_names = self.p.all_plastics
else:
plastic_names = self.p.plastic.split(',')
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
corrected_ffn_file = os.path.join(temp_dir, f"{contigs_base}_{plastic_name}_corrected.ffn")
saf_file = os.path.join(temp_dir, f"{contigs_base}_{plastic_name}.saf")
self.assertTrue(os.path.exists(corrected_ffn_file))
self.assertTrue(os.path.exists(saf_file))
# Check that the featureCounts log and output files were created for each plastic type
if self.p.plastic == "all":
plastic_names = self.p.all_plastics
else:
plastic_names = self.p.plastic.split(',')
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
mapping_files = self.p.mappings.split(',')
for mapping_file in mapping_files:
mapping_file = mapping_file.strip() # Remove any leading/trailing whitespace
log_file = os.path.join(temp_dir, f"{os.path.basename(mapping_file)}_featureCounts.log")
program_output_file = os.path.join(temp_dir, f"{os.path.basename(mapping_file)}_featureCounts.out")
self.assertTrue(os.path.exists(log_file), log_file)
self.assertTrue(os.path.exists(program_output_file), program_output_file)
# Check that the mapping_summary.tsv file was created for each plastic type
if self.p.plastic == "all":
plastic_names = self.p.all_plastics
else:
plastic_names = self.p.plastic.split(',')
for plastic_name in plastic_names:
temp_dir = os.path.join(self.p.temps, plastic_name.lower())
tsv_file = os.path.join(temp_dir, 'mapping_summary.tsv')
self.assertTrue(os.path.exists(tsv_file), tsv_file)
def test6_html(self):
# Try to run the function
try:
create_html(self.p)
remove_temps(self.p)
except Exception as e:
print(e)
html_files = [file for file in os.listdir(self.p.output) if file.endswith('.html')]
self.assertTrue(html_files != [], html_files)
def run_tests():
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestPlasticTools)
suite._tests.sort(key=lambda x: x._testMethodName)
unittest.TextTestRunner().run(suite)
if __name__ == '__main__':
run_tests()
#PlasticTools.main(args1, debug=True, blast=False)
#PlasticTools.main(args2, debug=True, blast=False)