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_load_tabular(self): """correctly loads tabular data""" rows = [[1, 2], [3, 4], [5, 6.5]] table = Table(["A", "B"], data=rows) load_table = io_app.load_tabular(sep="\t", with_header=True) with TemporaryDirectory(dir=".") as dirname: outpath = join(dirname, "delme.tsv") table.write(outpath) new = load_table(outpath) self.assertEqual(new.title, "") self.assertEqual(type(new[0, "B"]), type(table[0, "B"])) self.assertEqual(type(new[0, "A"]), type(table[0, "A"])) outpath = join(dirname, "delme2.tsv") with open(outpath, "w") as out: out.write("\t".join(table.header[:1]) + "\n") for row in table.tolist(): row = "\t".join(map(str, row)) out.write(row + "\n") result = load_table(outpath) self.assertIsInstance(result, NotCompleted) with TemporaryDirectory(dir=".") as dirname: outpath = join(dirname, "delme.zip") dstore = WritableZippedDataStore(outpath, suffix="tsv", create=True) dstore.write("sample1.tsv", table.to_string("tsv")) new = load_table(dstore[0]) self.assertEqual(type(new[0, "B"]), type(table[0, "B"])) self.assertEqual(type(new[0, "A"]), type(table[0, "A"]))
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_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)