| 1 |
import unittest |
| 2 |
from busco import run_BUSCO |
| 3 |
import sys |
| 4 |
import io |
| 5 |
from unittest.mock import Mock, patch, call |
| 6 |
|
| 7 |
|
| 8 |
class TestParams(unittest.TestCase): |
| 9 |
def setUp(self): |
| 10 |
self.maxDiff = None |
| 11 |
pass |
| 12 |
|
| 13 |
def test_help_short(self): |
| 14 |
args = ["-h"] |
| 15 |
sys.argv[1:] = args |
| 16 |
with self.assertRaises(SystemExit) as cm: |
| 17 |
captured_output = io.StringIO() |
| 18 |
sys.stdout = captured_output |
| 19 |
try: |
| 20 |
run_BUSCO._parse_args() |
| 21 |
finally: |
| 22 |
sys.stdout = sys.__stdout__ |
| 23 |
self.assertEqual(cm.exception.code, 0) |
| 24 |
|
| 25 |
def test_help_long(self): |
| 26 |
args = ["--help"] |
| 27 |
sys.argv[1:] = args |
| 28 |
with self.assertRaises(SystemExit) as cm: |
| 29 |
captured_output = io.StringIO() |
| 30 |
sys.stdout = captured_output |
| 31 |
try: |
| 32 |
run_BUSCO._parse_args() |
| 33 |
finally: |
| 34 |
sys.stdout = sys.__stdout__ |
| 35 |
self.assertEqual(cm.exception.code, 0) |
| 36 |
|
| 37 |
def test_empty_args_calls_help(self): |
| 38 |
sys.argv[1:] = [] |
| 39 |
with self.assertRaises(SystemExit) as cm: |
| 40 |
captured_output = io.StringIO() |
| 41 |
sys.stdout = captured_output |
| 42 |
try: |
| 43 |
run_BUSCO._parse_args() |
| 44 |
finally: |
| 45 |
sys.stdout = sys.__stdout__ |
| 46 |
self.assertEqual(cm.exception.code, 0) |
| 47 |
|
| 48 |
def test_version_short(self): |
| 49 |
args = ["-v"] |
| 50 |
sys.argv[1:] = args |
| 51 |
with self.assertRaises(SystemExit) as cm: |
| 52 |
captured_output = io.StringIO() |
| 53 |
sys.stdout = captured_output |
| 54 |
try: |
| 55 |
run_BUSCO._parse_args() |
| 56 |
finally: |
| 57 |
sys.stdout = sys.__stdout__ |
| 58 |
self.assertEqual(cm.exception.code, 0) |
| 59 |
|
| 60 |
def test_version_long(self): |
| 61 |
args = ["--version"] |
| 62 |
sys.argv[1:] = args |
| 63 |
with self.assertRaises(SystemExit) as cm: |
| 64 |
captured_output = io.StringIO() |
| 65 |
sys.stdout = captured_output |
| 66 |
try: |
| 67 |
run_BUSCO._parse_args() |
| 68 |
finally: |
| 69 |
sys.stdout = sys.__stdout__ |
| 70 |
self.assertEqual(cm.exception.code, 0) |
| 71 |
|
| 72 |
def test_list_lineages(self): |
| 73 |
args = ["--list-datasets"] |
| 74 |
sys.argv[1:] = args |
| 75 |
with self.assertRaises(SystemExit) as cm: |
| 76 |
captured_output = io.StringIO() |
| 77 |
sys.stdout = captured_output |
| 78 |
try: |
| 79 |
run_BUSCO._parse_args() |
| 80 |
finally: |
| 81 |
sys.stdout = sys.__stdout__ |
| 82 |
self.assertEqual(cm.exception.code, 0) |
| 83 |
|
| 84 |
def test_direct_download_action_called(self, *args): |
| 85 |
sys.argv = ["busco", "--download", "archaea_odb10"] |
| 86 |
with patch( |
| 87 |
"busco.run_BUSCO.DirectDownload", autospec=True |
| 88 |
) as MockDirectDownload: |
| 89 |
MockDirectDownload.return_value.configure_mock( |
| 90 |
type=str, |
| 91 |
option_strings=["--download"], |
| 92 |
dest="download", |
| 93 |
default=None, |
| 94 |
nargs=None, |
| 95 |
choices=None, |
| 96 |
) |
| 97 |
params = run_BUSCO._parse_args() |
| 98 |
mock_action = Mock() |
| 99 |
params.direct_download_action = mock_action |
| 100 |
mock_action.execute = Mock() |
| 101 |
|
| 102 |
with patch("busco.run_BUSCO._parse_args", return_value=params): |
| 103 |
try: |
| 104 |
run_BUSCO.main() |
| 105 |
except SystemExit: |
| 106 |
pass |
| 107 |
|
| 108 |
mock_action.execute.assert_called_once_with(params) |
| 109 |
|
| 110 |
def test_cmdline_options_short_minimum(self): |
| 111 |
args = ["-i", None] # required to avoid calling --help |
| 112 |
sys.argv[1:] = args |
| 113 |
params = vars(run_BUSCO._parse_args()) |
| 114 |
correct_parse = { |
| 115 |
"auto-lineage": False, |
| 116 |
"auto-lineage-euk": False, |
| 117 |
"auto-lineage-prok": False, |
| 118 |
"config_file": None, |
| 119 |
"contig_break": None, |
| 120 |
"cpu": None, |
| 121 |
"evalue": None, |
| 122 |
"force": False, |
| 123 |
"help": "==SUPPRESS==", |
| 124 |
"in": None, |
| 125 |
"limit": None, |
| 126 |
"lineage_dataset": None, |
| 127 |
"list_datasets": "==SUPPRESS==", |
| 128 |
"mode": None, |
| 129 |
"offline": False, |
| 130 |
"opt-out-run-stats": False, |
| 131 |
"out": None, |
| 132 |
"out_path": None, |
| 133 |
"quiet": False, |
| 134 |
"restart": False, |
| 135 |
"metaeuk_parameters": None, |
| 136 |
"metaeuk_rerun_parameters": None, |
| 137 |
"use_augustus": False, |
| 138 |
"use_metaeuk": False, |
| 139 |
"use_miniprot": False, |
| 140 |
"augustus_parameters": None, |
| 141 |
"augustus_species": None, |
| 142 |
"long": False, |
| 143 |
"datasets_version": None, |
| 144 |
"download": "==SUPPRESS==", |
| 145 |
"download_base_url": None, |
| 146 |
"download_path": None, |
| 147 |
"version": "==SUPPRESS==", |
| 148 |
"tar": False, |
| 149 |
"scaffold_composition": False, |
| 150 |
"skip_bbtools": False, |
| 151 |
"plot_percentages": False, |
| 152 |
"plot": "==SUPPRESS==", |
| 153 |
} |
| 154 |
self.assertDictEqual(params, correct_parse) |
| 155 |
|
| 156 |
def test_cmdline_options_all_short(self): |
| 157 |
input_file = "input_file" |
| 158 |
output_file = "output_file" |
| 159 |
mode = "mode" |
| 160 |
lineage_dataset = "lineage_dataset" |
| 161 |
cpus = 10 |
| 162 |
evalue = 0.1 |
| 163 |
|
| 164 |
arg_values = { |
| 165 |
"-i": input_file, |
| 166 |
"-o": output_file, |
| 167 |
"-m": mode, |
| 168 |
"-l": lineage_dataset, |
| 169 |
"-c": cpus, |
| 170 |
"-e": evalue, |
| 171 |
} |
| 172 |
flag_options = ["-f", "-q", "-r"] |
| 173 |
command_str = " ".join( |
| 174 |
[" ".join([key, str(value)]) for key, value in arg_values.items()] |
| 175 |
+ flag_options |
| 176 |
) |
| 177 |
sys.argv[1:] = command_str.split(" ") |
| 178 |
params = vars(run_BUSCO._parse_args()) |
| 179 |
correct_parse = { |
| 180 |
"auto-lineage": False, |
| 181 |
"auto-lineage-euk": False, |
| 182 |
"auto-lineage-prok": False, |
| 183 |
"config_file": None, |
| 184 |
"contig_break": None, |
| 185 |
"cpu": cpus, |
| 186 |
"evalue": evalue, |
| 187 |
"force": True, |
| 188 |
"help": "==SUPPRESS==", |
| 189 |
"in": input_file, |
| 190 |
"limit": None, |
| 191 |
"lineage_dataset": lineage_dataset, |
| 192 |
"list_datasets": "==SUPPRESS==", |
| 193 |
"mode": mode, |
| 194 |
"offline": False, |
| 195 |
"opt-out-run-stats": False, |
| 196 |
"out": output_file, |
| 197 |
"out_path": None, |
| 198 |
"quiet": True, |
| 199 |
"restart": True, |
| 200 |
"scaffold_composition": False, |
| 201 |
"metaeuk_parameters": None, |
| 202 |
"metaeuk_rerun_parameters": None, |
| 203 |
"use_augustus": False, |
| 204 |
"use_metaeuk": False, |
| 205 |
"use_miniprot": False, |
| 206 |
"augustus_parameters": None, |
| 207 |
"augustus_species": None, |
| 208 |
"long": False, |
| 209 |
"datasets_version": None, |
| 210 |
"download": "==SUPPRESS==", |
| 211 |
"download_base_url": None, |
| 212 |
"download_path": None, |
| 213 |
"version": "==SUPPRESS==", |
| 214 |
"tar": False, |
| 215 |
"skip_bbtools": False, |
| 216 |
"plot_percentages": False, |
| 217 |
"plot": "==SUPPRESS==", |
| 218 |
} |
| 219 |
self.assertDictEqual(params, correct_parse) |
| 220 |
|
| 221 |
def test_cmdline_options_all_long(self): |
| 222 |
input_file = "input_file" |
| 223 |
output_file = "output_file" |
| 224 |
mode = "mode" |
| 225 |
lineage_dataset = "lineage_dataset" |
| 226 |
cpus = 10 |
| 227 |
evalue = 0.1 |
| 228 |
limit = 1 |
| 229 |
contig_break = 5 |
| 230 |
augustus_parameters = "augustus_parameters" |
| 231 |
augustus_species = "augustus_species" |
| 232 |
config = "config" |
| 233 |
out_path = "out_path" |
| 234 |
download_path = "download_path" |
| 235 |
datasets_version = "datasets_version" |
| 236 |
download_base_url = "download_base_url" |
| 237 |
metaeuk_parameters = "metaeuk_parameters" |
| 238 |
metaeuk_rerun_parameters = "metaeuk_rerun_parameters" |
| 239 |
|
| 240 |
arg_values = { |
| 241 |
"--in": input_file, |
| 242 |
"--cpu": cpus, |
| 243 |
"--out": output_file, |
| 244 |
"--evalue": evalue, |
| 245 |
"--mode": mode, |
| 246 |
"--lineage_dataset": lineage_dataset, |
| 247 |
"--limit": limit, |
| 248 |
"--augustus_parameters": augustus_parameters, |
| 249 |
"--augustus_species": augustus_species, |
| 250 |
"--config": config, |
| 251 |
"--contig_break": contig_break, |
| 252 |
"--out_path": out_path, |
| 253 |
"--download_path": download_path, |
| 254 |
"--datasets_version": datasets_version, |
| 255 |
"--download_base_url": download_base_url, |
| 256 |
"--metaeuk_parameters": metaeuk_parameters, |
| 257 |
"--metaeuk_rerun_parameters": metaeuk_rerun_parameters, |
| 258 |
} |
| 259 |
flag_options = [ |
| 260 |
"--force", |
| 261 |
"--restart", |
| 262 |
"--quiet", |
| 263 |
"--long", |
| 264 |
"--auto-lineage", |
| 265 |
"--auto-lineage-prok", |
| 266 |
"--auto-lineage-euk", |
| 267 |
"--augustus", |
| 268 |
"--offline", |
| 269 |
"--tar", |
| 270 |
"--scaffold_composition", |
| 271 |
"--skip_bbtools", |
| 272 |
"--opt-out-run-stats", |
| 273 |
] |
| 274 |
command_str = " ".join( |
| 275 |
[" ".join([key, str(value)]) for key, value in arg_values.items()] |
| 276 |
+ flag_options |
| 277 |
) |
| 278 |
sys.argv[1:] = command_str.split(" ") |
| 279 |
params = vars(run_BUSCO._parse_args()) |
| 280 |
correct_parse = { |
| 281 |
"augustus_parameters": augustus_parameters, |
| 282 |
"augustus_species": augustus_species, |
| 283 |
"metaeuk_parameters": metaeuk_parameters, |
| 284 |
"metaeuk_rerun_parameters": metaeuk_rerun_parameters, |
| 285 |
"datasets_version": datasets_version, |
| 286 |
"download": "==SUPPRESS==", |
| 287 |
"download_base_url": download_base_url, |
| 288 |
"download_path": download_path, |
| 289 |
"auto-lineage": True, |
| 290 |
"auto-lineage-euk": True, |
| 291 |
"auto-lineage-prok": True, |
| 292 |
"config_file": config, |
| 293 |
"contig_break": contig_break, |
| 294 |
"cpu": cpus, |
| 295 |
"evalue": evalue, |
| 296 |
"force": True, |
| 297 |
"restart": True, |
| 298 |
"use_augustus": True, |
| 299 |
"use_metaeuk": False, |
| 300 |
"use_miniprot": False, |
| 301 |
"help": "==SUPPRESS==", |
| 302 |
"in": input_file, |
| 303 |
"limit": limit, |
| 304 |
"lineage_dataset": lineage_dataset, |
| 305 |
"list_datasets": "==SUPPRESS==", |
| 306 |
"long": True, |
| 307 |
"mode": mode, |
| 308 |
"offline": True, |
| 309 |
"opt-out-run-stats": True, |
| 310 |
"out": output_file, |
| 311 |
"out_path": out_path, |
| 312 |
"quiet": True, |
| 313 |
"version": "==SUPPRESS==", |
| 314 |
"tar": True, |
| 315 |
"scaffold_composition": True, |
| 316 |
"skip_bbtools": True, |
| 317 |
"plot_percentages": False, |
| 318 |
"plot": "==SUPPRESS==", |
| 319 |
} |
| 320 |
self.assertDictEqual(params, correct_parse) |
| 321 |
|
| 322 |
def test_command_line_cpu_type(self): |
| 323 |
bad_args_cpu = ["cpus", "1.5", None] |
| 324 |
|
| 325 |
for arg in bad_args_cpu: |
| 326 |
sys.argv[1:] = ["--cpu", arg] |
| 327 |
with self.assertRaises(SystemExit) as cm: |
| 328 |
captured_output = io.StringIO() |
| 329 |
sys.stderr = captured_output |
| 330 |
try: |
| 331 |
run_BUSCO._parse_args() |
| 332 |
finally: |
| 333 |
sys.stderr = sys.__stderr__ |
| 334 |
self.assertEqual(cm.exception.code, 2) |
| 335 |
|
| 336 |
def test_command_line_evalue_type(self): |
| 337 |
bad_args_evalue = ["evalue", None] |
| 338 |
|
| 339 |
for arg in bad_args_evalue: |
| 340 |
sys.argv[1:] = ["--evalue", arg] |
| 341 |
with self.assertRaises(SystemExit) as cm: |
| 342 |
captured_output = io.StringIO() |
| 343 |
sys.stderr = captured_output |
| 344 |
try: |
| 345 |
run_BUSCO._parse_args() |
| 346 |
finally: |
| 347 |
sys.stderr = sys.__stderr__ |
| 348 |
self.assertEqual(cm.exception.code, 2) |
| 349 |
|
| 350 |
def test_command_line_limit_type(self): |
| 351 |
bad_args_limit = ["limit", "1.5", None] |
| 352 |
for arg in bad_args_limit: |
| 353 |
sys.argv[1:] = ["--limit", arg] |
| 354 |
with self.assertRaises(SystemExit) as cm: |
| 355 |
captured_output = io.StringIO() |
| 356 |
sys.stderr = captured_output |
| 357 |
try: |
| 358 |
run_BUSCO._parse_args() |
| 359 |
finally: |
| 360 |
sys.stderr = sys.__stderr__ |
| 361 |
self.assertEqual(cm.exception.code, 2) |
| 362 |
|
| 363 |
def tearDown(self): |
| 364 |
sys.argv = [sys.argv[0]] |
| 365 |
|
| 366 |
|
| 367 |
class TestMaster(unittest.TestCase): |
| 368 |
def setUp(self): |
| 369 |
self.maxDiff = None |
| 370 |
self.params = {} |
| 371 |
pass |
| 372 |
|
| 373 |
def tearDown(self): |
| 374 |
pass |