def testMimicPhylesystemExport(self): study_nexson = pathmap.nexson_obj('10/pg_10.json') src_schema = PhyloSchema('nexson', version='1.2.1') out_schema = PhyloSchema(schema='newick', content='tree', content_id='bogusID here') result_data = out_schema.convert(study_nexson, serialize=True, src_schema=src_schema) self.assertFalse(bool(result_data)) out_schema = PhyloSchema(schema='nexus', content='tree', content_id='bogusID here') result_data = out_schema.convert(study_nexson, serialize=True, src_schema=src_schema) self.assertFalse(bool(result_data))
def testNewickConvStudyViaPS(self): o = pathmap.nexson_obj('9/v1.2.json') ps = PhyloSchema(type_ext='.tre') nex = ps.convert(o, serialize=True) self.assertTrue(nex.startswith('(')) # pylint: disable=E1103
def testNexusConvStudyViaPS(self): o = pathmap.nexson_obj('10/pg_10.json') ps = PhyloSchema(type_ext='.nex') nex = ps.convert(o, serialize=True) self.assertTrue(nex.startswith('#')) # pylint: disable=E1103
def testNexusConvViaPS(self): o = pathmap.nexson_obj('10/pg_10.json') ps = PhyloSchema('nexus', content='tree', content_id='tree3') nex = ps.convert(o, serialize=True) self.assertTrue(nex.startswith('#')) # pylint: disable=E1103
def _main(): import sys, codecs, json import argparse _HELP_MESSAGE = '''NexSON (or NeXML) to newick converter''' _EPILOG = '''UTF-8 encoding is used (for input and output). Environmental variables used: NEXSON_LOGGING_LEVEL logging setting: NotSet, Debug, Warn, Info, Error NEXSON_LOGGING_FORMAT format string for logging messages. ''' tip_label_list = PhyloSchema._otu_label_list for tl in tip_label_list: assert(tl.startswith('ot:')) tip_labels_choices = [i[3:] for i in tip_label_list] parser = argparse.ArgumentParser(description=_HELP_MESSAGE, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=_EPILOG) parser.add_argument("input", help="filepath to input") parser.add_argument("-i", "--id", metavar="TREE-ID", required=False, help="The ID tree to emit") parser.add_argument("-o", "--output", metavar="FILE", required=False, help="output filepath. Standard output is used if omitted.") parser.add_argument("-l", "--list", action="store_true", default=False, help="Just list the tree IDs in the nexSON.") parser.add_argument("-x", "--xml", action="store_true", default=False, help="Parse input as NeXML rather than NexSON.") tl_help = 'The field to use to label tips. Should be one of: "{}"' tl_help = tl_help.format('", "'.join(tip_labels_choices)) parser.add_argument("-t", "--tip-label", metavar="STRING", required=False, default='originallabel', help=tl_help) args = parser.parse_args() otu_label = args.tip_label.lower() if not otu_label.startswith('ot:'): otu_label = 'ot:' + otu_label if otu_label not in tip_label_list: sys.exit('Illegal tip label choice "{}"\n'.format(args.tip_label)) inpfn = args.input outfn = args.output try: inp = codecs.open(inpfn, mode='rU', encoding='utf-8') except: sys.exit('nexson_newick: Could not open file "{fn}"\n'.format(fn=inpfn)) if outfn is not None: try: out = codecs.open(outfn, mode='w', encoding='utf-8') except: sys.exit('nexson_newick: Could not open output filepath "{fn}"\n'.format(fn=outfn)) else: out = codecs.getwriter('utf-8')(sys.stdout) if args.xml: src_schema = PhyloSchema('nexml') blob = get_ot_study_info_from_nexml(inp) else: src_schema = None blob = json.load(inp) if args.list: schema = PhyloSchema(content='treelist', output_nexml2json='1.2.1') tl = schema.convert(src=blob, src_schema=src_schema) out.write('{t}\n'.format(t='\n'.join(tl))) else: schema = create_content_spec(content='tree', content_id=args.id, format='newick', otu_label=otu_label) try: schema.convert(src=blob, serialize=True, output_dest=out, src_schema=src_schema) except KeyError: if 'nexml' not in blob and 'nex:nexml' not in blob: blob = blob['data'] schema.convert(src=blob, serialize=True, output_dest=out, src_schema=src_schema) else: raise