def test_write_tabular_distance_matrix(self): """correctly writes tabular data for DistanceMatrix""" data = {(0, 0): 0, (0, 1): 4, (1, 0): 4, (1, 1): 0} matrix = DistanceMatrix(data) loader = io_app.load_tabular(sep="\t") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(matrix, identifier=outpath) new = loader(outpath) # when written to file in tabular form # the loaded table will have dim-1 dim-2 as column labels # and the key-values pairs listed below; in dict form... expected = { 0: { "dim-1": 0, "dim-2": 1, "value": 4 }, 1: { "dim-1": 1, "dim-2": 0, "value": 4 }, } self.assertEqual(expected, new.to_dict())
def test_write_tabular_pssm(self): """correctly writes tabular data for PSSM""" # data from test_profile data = numpy.array([ [0.1, 0.3, 0.5, 0.1], [0.25, 0.25, 0.25, 0.25], [0.05, 0.8, 0.05, 0.1], [0.7, 0.1, 0.1, 0.1], [0.6, 0.15, 0.05, 0.2], ]) pssm = PSSM(data, "ACTG") loader = io_app.load_tabular(sep="\t") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(pssm, identifier=outpath) new = loader(outpath) expected = safe_log(data) - safe_log( numpy.array([0.25, 0.25, 0.25, 0.25])) for i in range(len(expected)): j = i // 4 self.assertTrue( numpy.isclose(new.array[i][2], expected[j][i - j], atol=0.0001))
def test_composes_with_write_tabular(self): """correctly links to tabular""" with TemporaryDirectory(dir=".") as dirname: writer = io.write_tabular(dirname) dist_calc = dist_app.fast_slow_dist(distance="hamming", moltype="protein") _ = dist_calc + writer
def test_load_tabular_table(self): """correctly loads tabular data""" rows = [[1, 2], [3, 4], [5, 6.5]] table = Table(["A", "B"], data=rows) loader = io_app.load_tabular(sep="\t", as_type="table") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(table, identifier=outpath) new = loader(outpath) self.assertEqual(table.to_dict(), new.to_dict())
def test_load_tabular_distance_matrix(self): """correctly loads tabular data for DistanceMatrix""" data = {(0, 0): 0, (0, 1): 4, (1, 0): 4, (1, 1): 0} matrix = DistanceMatrix(data) loader = io_app.load_tabular(sep="\t", as_type="distances") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(matrix, identifier=outpath) new = loader(outpath) self.assertEqual(matrix.to_dict(), new.to_dict())
def test_load_tabular_motif_freqs_array(self): """correctly loads tabular data for MotifFreqsArray""" data = [[0.3333, 0.6667], [0.3750, 0.625], [0.3333, 0.6667]] mfa = MotifFreqsArray(data, "AB") loader = io_app.load_tabular(sep="\t", as_type="motif_freqs") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(mfa, identifier=outpath) new = loader(outpath) self.assertEqual(mfa.to_dict(), new.to_dict())
def test_load_tabular_motif_counts_array(self): """correctly loads tabular data for MotifCountsArray""" data = [[2, 4], [3, 5], [4, 8]] mca = MotifCountsArray(data, "AB") loader = io_app.load_tabular(sep="\t", as_type="motif_counts") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(mca, identifier=outpath) new = loader(outpath) self.assertEqual(mca.to_dict(), new.to_dict())
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_write_tabular_motif_freqs_array(self): """correctly writes tabular data for MotifFreqsArray""" data = [[0.3333, 0.6667], [0.3750, 0.625], [0.3333, 0.6667]] mfa = MotifFreqsArray(data, "AB") loader = io_app.load_tabular(sep="\t") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(mfa, identifier=outpath) new = loader(outpath) # when written to file in tabular form # the loaded table will have dim-1 dim-2 as column labels # and the key-values pairs listed below; in dict form... expected = { 0: { "dim-1": 0, "dim-2": "A", "value": 0.3333 }, 1: { "dim-1": 0, "dim-2": "B", "value": 0.6667 }, 2: { "dim-1": 1, "dim-2": "A", "value": 0.3750 }, 3: { "dim-1": 1, "dim-2": "B", "value": 0.6250 }, 4: { "dim-1": 2, "dim-2": "A", "value": 0.3333 }, 5: { "dim-1": 2, "dim-2": "B", "value": 0.6667 }, } self.assertEqual(expected, new.to_dict())
def test_load_tabular_pssm(self): """correctly loads tabular data for PSSM""" # data from test_profile data = [ [0.1, 0.3, 0.5, 0.1], [0.25, 0.25, 0.25, 0.25], [0.05, 0.8, 0.05, 0.1], [0.7, 0.1, 0.1, 0.1], [0.6, 0.15, 0.05, 0.2], ] pssm = PSSM(data, "ACTG") loader = io_app.load_tabular(sep="\t", as_type="pssm") with TemporaryDirectory(dir=".") as dirname: writer = io_app.write_tabular(data_path=dirname, format="tsv") outpath = join(dirname, "delme.tsv") writer.write(pssm, identifier=outpath) new = loader(outpath) assert_allclose(pssm.array, new.array, atol=0.0001)