def test_write_json_no_info(self): """correctly writes an object with out an info attribute from json""" # create a mock object that pretends like it's been derived from # something with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme") mock = patch("data.source", autospec=True) mock.to_rich_dict = DNA.to_rich_dict mock.source = join("blah", "delme.json") writer = io_app.write_json(outdir, create=True) _ = writer(mock) reader = io_app.load_json() got = reader(writer.data_store[0]) self.assertEqual(got, DNA) # now with a zipped archive with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme.zip") mock = patch("data.source", autospec=True) mock.to_rich_dict = DNA.to_rich_dict mock.source = join("blah", "delme.json") writer = io_app.write_json(outdir, create=True) identifier = writer(mock) reader = io_app.load_json() # checking loadable from a data store member too got = reader(writer.data_store[0]) self.assertEqual(got, DNA) expect = join(outdir.replace(".zip", ""), "delme.json") if expect.startswith("." + os.sep): expect = expect[2:] self.assertEqual(identifier, expect)
def test_load_json(self): """correctly loads an object from json""" from cogent3.app.data_store import make_record_for_json data = make_record_for_json("delme", DNA, True) data = json.dumps(data) # straight directory with TemporaryDirectory(dir=".") as dirname: outpath = join(dirname, "delme.json") with open(outpath, "w") as outfile: outfile.write(data) reader = io_app.load_json() got = reader(outpath) self.assertIsInstance(got, DNA.__class__) self.assertEqual(got, DNA) # zipped directory with TemporaryDirectory(dir=".") as dirname: zip_path = join(dirname, "delme.zip") outpath = "delme/delme.json" with zipfile.ZipFile(zip_path, "a") as out: out.writestr(outpath, data) dstore = io_app.get_data_store(zip_path, suffix="json") member = dstore.get_member("delme.json") reader = io_app.load_json() got = reader(member) self.assertIsInstance(got, DNA.__class__) self.assertEqual(got, DNA)
def test_write_json_with_info(self): """correctly writes an object with info attribute from json""" # create a mock object that pretends like it's been derived from # something from cogent3.app.result import generic_result with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme") obj = generic_result(source=join("blah", "delme.json")) obj["dna"] = DNA writer = io_app.write_json(outdir, create=True) _ = writer(obj) reader = io_app.load_json() got = reader(join(outdir, "delme.json")) got.deserialised_values() self.assertEqual(got["dna"], DNA) # now with a zipped archive with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme.zip") obj = generic_result(source=join("blah", "delme.json")) obj["dna"] = DNA writer = io_app.write_json(outdir, create=True) identifier = writer(obj) reader = io_app.load_json() got = reader(writer.data_store[0]) got.deserialised_values() self.assertEqual(got["dna"], DNA) expect = join(outdir.replace(".zip", ""), "delme.json") if expect.startswith("." + os.sep): expect = expect[2:] self.assertEqual(identifier, expect)
def test_write_json_with_info(self): """correctly writes an object with info attribute from json""" # create a mock object that pretends like it's been derived from # something from cogent3.util.union_dict import UnionDict with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme") mock = Mock() mock.to_rich_dict = DNA.to_rich_dict mock.info = UnionDict(source=join("blah", "delme.json")) writer = io_app.write_json(outdir, create=True) _ = writer(mock) reader = io_app.load_json() got = reader(join(outdir, "delme.json")) self.assertEqual(got, DNA) # now with a zipped archive with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme.zip") mock = Mock() mock.to_rich_dict = DNA.to_rich_dict mock.info = UnionDict(source=join("blah", "delme.json")) writer = io_app.write_json(outdir, create=True) identifier = writer(mock) reader = io_app.load_json() got = reader(writer.data_store[0]) self.assertEqual(got, DNA) expect = join(outdir.replace(".zip", ""), "delme.json") if expect.startswith("." + os.sep): expect = expect[2:] self.assertEqual(identifier, expect)
def test_write_json_no_info(self): """correctly writes an object with out an info attribute from json""" # create a mock object that pretends like it's been derived from # something with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme") gr = _get_generic_result(join("blah", "delme.json")) writer = io_app.write_json(outdir, create=True) _ = writer(gr) reader = io_app.load_json() got = reader(writer.data_store[0]) got.deserialised_values() self.assertEqual(got["dna"], DNA)
def test_write_json_with_info(self): """correctly writes an object with info attribute from json""" # create a mock object that pretends like it's been derived from # something from cogent3.app.result import generic_result with TemporaryDirectory(dir=".") as dirname: outdir = join(dirname, "delme") obj = generic_result(source=join("blah", "delme.json")) obj["dna"] = DNA writer = io_app.write_json(outdir, create=True) _ = writer(obj) reader = io_app.load_json() got = reader(join(outdir, "delme.json")) got.deserialised_values() self.assertEqual(got["dna"], DNA)
""" Display a Phylogenetic Tree Showing Bootstrap Support ===================================================== We use a tree saved in `json` format from a 100 replicate bootstrap resamplings. The `show_support=True` argument controls whether or not to display support. The `threshold=0.8` argument indicates only nodes with a support level ≤0.8 will have support text displayed. """ # %% from cogent3.app import io reader = io.load_json() tree = reader("../../data/tree-with-support.json") fig = tree.get_figure(show_support=True, threshold=0.8) fig.scale_bar = None fig.show(width=500, height=400) #%% # Change the placement of support text # #################################### # # The support text is positioned relative to the `x`, `y` coordinates of the tree node. Control over support text placement is achieved using the `support_xshift` and `support_yshift` attributes. These are expressed in terms of pixels. # # To place the support text internal to the node, we set the yshift=0 (so at the same y-value of the node) and xshift it to the right using a positive integer. fig.support_xshift = 15 fig.support_yshift = 0 fig.show(width=500, height=400)
""" Dotplot with annotated sequences ================================ If sequences in a dotplot have been annotated, the `dotplot()` method returns an `AnnotatedDrawable`. """ #%% # Reloading from json # ################### # # The data file, `tp53.json`, was created from a query of ensembl for one-to-one orthologs of human TP53 between Human, Macaque, Orangutan and Marmoset. The resulting sequences were annotated with the location of the CDS for the canonical transcript, then the `SequenceCollection` was saved as json using `cogent3.app.write_json`. from cogent3.app.io import get_data_store, load_json loader = load_json() seqs = loader("../../data/tp53.json") dp = seqs.dotplot(name1="Macaque", name2="Marmoset", width=600) dp.show() #%% # Removing annotation tracks # ########################## help(dp.remove_track) #%% # Thus we could remove the left annotation track, for instance with # # ```python # dp.remove_track(left_track=True) # ```