def test_load_aligned_nexus(self): """should handle nexus too""" nexus_paths = io_app.get_data_store(self.basedir, suffix="nex") loader = io_app.load_aligned(format="nexus") results = [loader(m) for m in nexus_paths] for result in results: self.assertIsInstance(result, ArrayAlignment)
def test_triggers_bugcatcher(self): """a composable that does not trap failures returns NotCompletedResult requesting bug report""" from cogent3.app import io, sample, evo, tree, translate, align read = io.load_aligned(moltype="dna") read.func = lambda x: None got = read("somepath.fasta") self.assertIsInstance(got, NotCompleted) self.assertEqual(got.type, "BUG")
def test_write_seqs(self): """correctly writes sequences out""" fasta_paths = list(io_app.findall(self.basedir, suffix=".fasta", limit=2)) fasta_loader = io_app.load_aligned(format="fasta") alns = list(map(fasta_loader, fasta_paths)) with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_seqs(dirname, if_exists="ignore") wrote = list(map(writer, alns)) written = list(io_app.findall(dirname, suffix="fasta")) for i, wrote in enumerate(written): self.assertEqual(alns[i].info.stored, join(dirname, wrote))
def test_apply_to_not_completed(self): """correctly creates notcompleted""" dstore = io_app.get_data_store("data", suffix="fasta", limit=3) with TemporaryDirectory(dir=".") as dirname: reader = io_app.load_aligned(format="fasta", moltype="dna") # trigger creation of notcompleted min_length = sample_app.min_length(3000) outpath = os.path.join(os.getcwd(), dirname, "delme.tinydb") writer = io_app.write_db(outpath) process = reader + min_length + writer r = process.apply_to(dstore, show_progress=False) self.assertEqual(len(process.data_store.incomplete), 3) process.data_store.close()
def test_load_aligned(self): """correctly loads aligned seqs""" def validate(paths, loader): loaded = list(map(loader, paths)) for i, aln in enumerate(loaded): self.assertTrue(len(aln) > 10) self.assertIsInstance(aln, ArrayAlignment) self.assertEqual(aln.info.source, paths[i]) fasta_paths = io_app.get_data_store(self.basedir, suffix=".fasta", limit=2) fasta_loader = io_app.load_aligned(format="fasta") validate(fasta_paths, fasta_loader)
def test_functions_as_composable(self): """works as a composable app""" from pathlib import Path loader = io.load_aligned(moltype="dna", format="paml") dist = dist_app.fast_slow_dist("hamming", moltype="dna") with TemporaryDirectory(dir=".") as dirname: dirname = Path(dirname) writer = io.write_tabular(dirname) proc = loader + dist + writer _ = proc("data/brca1_5.250.paml") output = dirname / "brca1_5.250.tsv" self.assertTrue(output.exists())
def test_apply_to_strings(self): """apply_to handles strings as paths""" dstore = io_app.get_data_store("data", suffix="fasta", limit=3) dstore = [str(m) for m in dstore] with TemporaryDirectory(dir=".") as dirname: reader = io_app.load_aligned(format="fasta", moltype="dna") min_length = sample_app.min_length(10) outpath = os.path.join(os.getcwd(), dirname, "delme.tinydb") writer = io_app.write_db(outpath) process = reader + min_length + writer # create paths as strings r = process.apply_to(dstore, show_progress=False) self.assertEqual(len(process.data_store.logs), 1) process.data_store.close()
def test_user_function_str(self): u_function_1 = user_function(self.foo, "aligned", "aligned") u_function_2 = user_function(self.bar, "aligned", "pairwise_distances") self.assertEqual( str(u_function_1), "user_function(name='foo', module='test_composable')") self.assertEqual( str(u_function_2), "user_function(name='bar', module='test_composable')") # added into a composable func loader = io_app.load_aligned() proc = loader + u_function_1 got = str(proc) self.assertTrue(got.startswith("load_aligned"))
def test_apply_to_not_partially_done(self): """correctly applies process when result already partially done""" dstore = io_app.get_data_store("data", suffix="fasta") num_records = len(dstore) with TemporaryDirectory(dir=".") as dirname: dirname = pathlib.Path(dirname) reader = io_app.load_aligned(format="fasta", moltype="dna") outpath = dirname / "delme.tinydb" writer = io_app.write_db(outpath) _ = writer(reader(dstore[0])) writer.data_store.close() writer = io_app.write_db(outpath, if_exists="ignore") process = reader + writer _ = process.apply_to(dstore, show_progress=False) writer.data_store.close() dstore = io_app.get_data_store(outpath) self.assertEqual(len(dstore), num_records)
def test_checkpointable(self): """chained funcs should be be able to apply a checkpoint""" path = "data" + os.sep + "brca1.fasta" reader = io_app.load_aligned(moltype="dna") omit_degens = sample_app.omit_degenerates(moltype="dna") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_seqs(dirname) aln = reader(path) outpath = writer(aln) read_write = reader + writer got = read_write(path) # should skip reading and return path self.assertEqual(got, outpath) read_write.disconnect() # allows us to reuse bits read_write_degen = reader + writer + omit_degens # should return an alignment instance got = read_write_degen(path) self.assertIsInstance(got, ArrayAlignment) self.assertTrue(len(got) > 1000)
def test_composite_pickleable(self): """composable functions should be pickleable""" from pickle import dumps from cogent3.app import io, sample, evo, tree, translate, align read = io.load_aligned(moltype="dna") dumps(read) trans = translate.select_translatable() dumps(trans) aln = align.progressive_align("nucleotide") dumps(aln) just_nucs = sample.omit_degenerates(moltype="dna") dumps(just_nucs) limit = sample.fixed_length(1000, random=True) dumps(limit) mod = evo.model("HKY85") dumps(mod) qt = tree.quick_tree() dumps(qt) proc = read + trans + aln + just_nucs + limit + mod dumps(proc)
def test_load_aligned_from_zip(self): """correctly loads aligned seqs from a zip archive""" def validate(paths, loader): loaded = list(map(loader, paths)) for i, aln in enumerate(loaded): self.assertTrue(len(aln) > 10) self.assertIsInstance(aln, ArrayAlignment) # paths is only the basename when workjing with zip archives # whereas the inpath will have full path of zip archive self.assertEqual(aln.info.source, paths[i]) self.assertEqual(aln.info.source, paths[i]) with TemporaryDirectory(dir=".") as dirname: zip_path = join(dirname, self.basedir.replace(".zip", "")) shutil.make_archive( base_name=zip_path, root_dir=".", format="zip", base_dir=self.basedir ) zip_path = zip_path + ".zip" # because shutil adds the suffix fasta_paths = list(io_app.findall(zip_path, suffix=".fasta", limit=2)) fasta_loader = io_app.load_aligned(format="fasta") validate(fasta_paths, fasta_loader)