Example #1
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Display taxonomic info (based on the Open Tree Taxonomy) for a taxon ID',
        common_args=("ott-id", ))
    cli.parser.add_argument(
        '--source-id',
        default=None,
        type=str,
        help=
        'A source taxonomy id for the taxon of interest, in the form prefix:id, for'
        ' example ncbi:9443, irmng:11338. '
        'Valid prefixes are currently ncbi, gbif, worms, if, and irmng. '
        'Either ott_id or source_id must be given, but not both')
    cli.parser.add_argument(
        '--include-lineage',
        action='store_true',
        help='Return the lineage of nodes back to the root of the tree')
    cli.parser.add_argument(
        '--include-children',
        action='store_true',
        help='Return information about all the children of this taxon')
    cli.parser.add_argument(
        '--include-terminal-descendants',
        action='store_true',
        help=
        'Return a list of terminal OTT IDs that are descendants of this taxon')
    OT, args = cli.parse_cli(arg_list)

    kwargs = {
        'include_children': args.include_children,
        'include_lineage': args.include_lineage,
        'include_terminal_descendants': args.include_terminal_descendants,
    }
    if args.ott_id:
        o = args.ott_id if not args.ott_id.startswith(
            'ott') else args.ott_id[3:]
        try:
            ott_id = int(o)
        except:
            raise RuntimeError(
                'Expecting each ott ID to be an integer or a string starting with "ott". '
                'Found "{}"\n'.format(args.ott_id))
        else:
            output = OT.taxon_info(ott_id=ott_id, **kwargs)
    elif args.source_id:
        if ':' not in args.source_id:
            raise RuntimeError(
                'Expecting a source ID to be in prefix:ID form. '
                'Found "{}"\n'.format(args.source_id))
        output = OT.taxon_info(source_id=args.source_id, **kwargs)
    else:
        raise RuntimeError(
            'Expecting either --ott-id or --source-id to be provided\n')
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sp = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sp))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(usage='Look up the OTT IDs for a set of names')
    cli.parser.add_argument("names", nargs="+", help="names to match")
    cli.parser.add_argument(
        "--context-name",
        default=None,
        help=
        'If you know the named Open Tree name searching index, you can supply it here '
        'to limit your search to only a subset of the taxonomny.')
    cli.parser.add_argument("--do-approximate-matching",
                            action="store_true",
                            help='Enables fuzzy matching')
    cli.parser.add_argument(
        "--include-suppressed",
        action="store_true",
        help='Return taxa that are normally suppressed from TNRS '
        'results. See {}'.format(get_suppressed_taxon_flag_expl_url()))
    OT, args = cli.parse_cli(arg_list)
    resp = OT.tnrs_match(args.names,
                         context_name=args.context_name,
                         do_approximate_matching=args.do_approximate_matching,
                         include_suppressed=args.include_suppressed)
    if list_for_results is not None:
        list_for_results.append(resp)
    if out is not None:
        sf = json.dumps(resp.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sf))
    return 0
Example #3
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Look up studies in the "phylesystem" set of phylogenetic studies that are in the Open '
        'system')
    cli.parser.add_argument("value", help="The value of the property to match")
    cli.parser.add_argument("--property",
                            default=None,
                            required=True,
                            help='The name of the field to search through.')
    cli.parser.add_argument("--verbose",
                            action="store_true",
                            help='include meta-data in response')
    cli.parser.add_argument("--exact",
                            action="store_true",
                            help='disables fuzzy matching')
    OT, args = cli.parse_cli(arg_list)

    output = OT.find_studies(args.value,
                             search_property=args.property,
                             verbose=args.verbose,
                             exact=args.exact)

    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sf = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sf))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage='Display node info for the synthetic tree node(s) requested',
        common_args=("ott-ids", "node-ids"))
    cli.parser.add_argument(
        '--include-lineage',
        action='store_true',
        help='Return the lineage of nodes back to the root of the tree')
    OT, args = cli.parse_cli(arg_list)
    ott_id_list, node_id_list = process_ott_and_node_id_list_args(args)
    # use node_id_list if there are multiple. This is an odd call in the API
    if (not node_id_list) and (not ott_id_list):
        raise ValueError('Either --node-ids or --ott-ids must be provided.\n')
    if len(ott_id_list) > 1 or (node_id_list and ott_id_list):
        node_id_list.extend(['ott{}'.format(i) for i in ott_id_list])
        ott_id_list.clear()
    if len(node_id_list) == 1:
        output = OT.synth_node_info(node_id=node_id_list[0],
                                    include_lineage=args.include_lineage)
    elif len(node_id_list) > 1:
        output = OT.synth_node_info(node_ids=node_id_list,
                                    include_lineage=args.include_lineage)
    else:
        assert len(ott_id_list) == 1
        ott_id = ott_id_list[0]
        assert ott_id is not None
        output = OT.synth_node_info(ott_id=ott_id,
                                    include_lineage=args.include_lineage)

    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sp = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sp))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage='Look up a minimal context name of a set of names')
    cli.parser.add_argument("names", nargs="+", help="names to match")
    OT, args = cli.parse_cli(arg_list)

    output = OT.tnrs_infer_context(args.names)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sf = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sf))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Displays the searchable properties for studies and trees in the "phylesystem"'
    )
    OT = cli.parse_cli(arg_list)[0]
    output = OT.studies_properties()
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sf = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sf))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Display node information about the Most Recent Common Ancestor of a set of IDs',
        common_args=("ott-ids", "node-ids"))
    OT, args = cli.parse_cli(arg_list)
    ott_id_list, node_id_list = process_ott_and_node_id_list_args(args)
    output = OT.synth_mrca(node_ids=node_id_list, ott_ids=ott_id_list)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sp = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sp))
    return 0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Display taxonomic information about the Most Recent Common Ancestor of a set of IDs',
        common_args=("ott-ids", ))
    OT, args = cli.parse_cli(arg_list)
    ott_id_list = process_ott_and_node_id_list_args(args)[0]
    if not ott_id_list:
        sys.exit('--ott-ids must be provided.\n')
    output = OT.taxon_mrca(ott_ids=ott_id_list)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        sf = json.dumps(output.response_dict, indent=2, sort_keys=True)
        out.write('{}\n'.format(sf))
    return 0
Example #9
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(usage='Display taxonomy and synthetic tree information '
                                  'returned by the "about" API calls.',
                            common_args=("ott-ids", "node-ids"))
    cli.parser.add_argument('--outfile', nargs='?', type=argparse.FileType('w'),
                            default=None)
    OT, args = cli.parse_cli(arg_list)
    ott_id_list, node_id_list = process_ott_and_node_id_list_args(args)
    output = OT.synth_induced_tree(node_ids=node_id_list, ott_ids=ott_id_list)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
            sp = output.tree.as_ascii_plot()
            out.write('{}\n'.format(sp))
    if args.outfile is not None:
            new = (output.response_dict['newick'])
            args.outfile.write('{}\n'.format(new))
    return 0
Example #10
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage='Display taxonomy and synthetic tree information '
        'returned by the "about" API calls.')
    OT = cli.parse_cli()[0]
    about = OT.about()
    if list_for_results is not None:
        list_for_results.append(about)
    if out is None:
        return 0
    for k in about.keys():
        call_record = about[k]
        if call_record:
            print(k)
            sf = json.dumps(call_record,
                            sort_keys=True,
                            indent=2,
                            separators=(',', ': '),
                            ensure_ascii=True)
            print(sf)
            print('')
    return 0
Example #11
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage='Fetch a tree by its phylesystem study ID and tree ID')
    cli.parser.add_argument(dest='study_id',
                            help="The ID of the study to retrieve")
    cli.parser.add_argument(dest='tree_id',
                            help="The ID of the study to retrieve")
    cli.parser.add_argument(
        '--format',
        default='nexson',
        type=str,
        help='one of: "newick", "nexson", "nexus", or "object"]')
    OT, args = cli.parse_cli(arg_list)
    output = OT.get_tree(args.study_id, args.tree_id, tree_format=args.format)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is None:
        return 0
    if args.format == 'nexson':
        out.write('{}\n'.format(
            json.dumps(output.response_dict, indent=2, sort_keys=True)))
    else:
        out.write('{}\n'.format(output.tree))
    return 0
Example #12
0
#!/usr/bin/env python3
import json

from opentree import OTCommandLineTool, get_suppressed_taxon_flag_expl_url

cli = OTCommandLineTool(usage='Look up the OTT IDs for a set of names')
cli.parser.add_argument("names", nargs="+", help="names to match")
cli.parser.add_argument(
    "--context-name",
    default=None,
    help=
    'If you know the named Open Tree name searching index, you can supply it here '
    'to limit your search to only a subset of the taxonomny.')
cli.parser.add_argument("--do-approximate-matching",
                        action="store_true",
                        help='Enables fuzzy matching')
cli.parser.add_argument(
    "--include-suppressed",
    action="store_true",
    help='Return taxa that are normally suppressed from TNRS '
    'results. See {}'.format(get_suppressed_taxon_flag_expl_url()))
OT, args = cli.parse_cli()

output = OT.tnrs_match(args.names,
                       context_name=args.context_name,
                       do_approximate_matching=args.do_approximate_matching,
                       include_suppressed=args.include_suppressed)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
Example #13
0
#!/usr/bin/env python3

import json

from opentree import OTCommandLineTool

cli = OTCommandLineTool(usage='Display the Open Tree TNRS named contexts')
OT = cli.parse_cli()[0]

output = OT.tnrs_contexts()

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
#!/usr/bin/env python3
import json
import sys

from opentree import OTCommandLineTool

cli = OTCommandLineTool(usage='Fetch a study by its phylesystem study ID')
cli.parser.add_argument(dest='study_id',
                        help="The ID of the study to retrieve")
OT, args = cli.parse_cli()
output = OT.get_study(args.study_id)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
sys.exit(0 if output else 1)
Example #15
0
def main(arg_list, out, list_for_results=None):
    cli = OTCommandLineTool(
        usage=
        'Gets a subtree of the synthetic tree rooted at the node requested',
        common_args=("ott-id", "node-id"))
    cli.parser.add_argument('--format',
                            default='newick',
                            help='"newick" or "arguson" tree format')
    cli.parser.add_argument(
        '--label-format',
        default='name_and_id',
        help='"name_and_id", "name", or "id" style of labeling newick nodes')
    cli.parser.add_argument(
        '--height-limit',
        default=None,
        type=int,
        help='number of levels to return. -1 for unlimited (newick only)')
    cli.parser.add_argument('--outfile',
                            nargs='?',
                            type=argparse.FileType('w'),
                            default=None)
    OT, args = cli.parse_cli(arg_list)

    tree_format = args.format.strip().lower()
    if tree_format not in ["newick", "arguson"]:
        raise RuntimeError(
            "Did not recognize --format={}\n".format(tree_format))
    if args.height_limit is None:
        if tree_format == "newick":
            height_limit = -1
        else:
            height_limit = 3
    else:
        height_limit = args.height_limit
        if height_limit < -1:
            raise RuntimeError('Expecting height limit to be >= -1.')
    label_format = args.label_format.strip().lower()
    if label_format not in ["name_and_id", "name", "id"]:
        raise RuntimeError(
            "Did not recognize --label-format={}\n".format(label_format))

    ott_id, node_id = process_ott_or_node_id_arg(args)
    # use node_id_list if there are multiple. This is an odd call in the API
    if (ott_id is None) and (node_id is None):
        raise RuntimeError(
            'Either --node-ids or --ott-ids must be provided.\n')
    if ott_id is not None:
        output = OT.synth_subtree(ott_id=ott_id,
                                  tree_format=tree_format,
                                  label_format=label_format,
                                  height_limit=height_limit)
    else:
        output = OT.synth_subtree(node_id=node_id,
                                  tree_format=tree_format,
                                  label_format=label_format,
                                  height_limit=height_limit)
    if list_for_results is not None:
        list_for_results.append(output)
    if out is not None:
        if tree_format == 'newick':
            sp = output.tree.as_ascii_plot()
            out.write('{}\n'.format(sp))
            new = (output.response_dict['newick'])
            args.outfile.write('{}\n'.format(new))
        else:
            sf = json.dumps(output.response_dict, indent=2, sort_keys=True)
            out.write('{}\n'.format(sp))
            args.outfile.write('{}\n'.format(sf))
    return 0
#!/usr/bin/env python3
from opentree import OTCommandLineTool, process_ott_and_node_id_list_args

cli = OTCommandLineTool(usage='Display taxonomy and synthetic tree information '
                              'returned by the "about" API calls.',
                        common_args=("ott-ids", "node-ids"))
OT, args = cli.parse_cli()
ott_id_list, node_id_list = process_ott_and_node_id_list_args(args)
output = OT.synth_induced_tree(node_ids=node_id_list, ott_ids=ott_id_list)
print(output.tree.as_ascii_plot())
Example #17
0
#!/usr/bin/env python3
import sys

from opentree import OTCommandLineTool, process_ott_or_node_id_arg

cli = OTCommandLineTool(
    usage='Gets a subtree of the synthetic tree rooted at the node requested',
    common_args=("ott-id", "node-id"))
cli.parser.add_argument('--format',
                        default='newick',
                        help='"newick" or "arguson" tree format')
cli.parser.add_argument(
    '--label-format',
    default='name_and_id',
    help='"name_and_id", "name", or "id" style of labeling newick nodes')
cli.parser.add_argument(
    '--height-limit',
    default=None,
    type=int,
    help='number of levels to return. -1 for unlimited (newick only)')
OT, args = cli.parse_cli()

tree_format = args.format.strip().lower()
if tree_format not in ["newick", "arguson"]:
    sys.exit("Did not recognize --format={}\n".format(tree_format))
if args.height_limit is None:
    if tree_format == "newick":
        height_limit = -1
    else:
        height_limit = 3
else:
Example #18
0
#!/usr/bin/env python3

import json
from opentree import OTCommandLineTool

cli = OTCommandLineTool(
    usage=
    'Displays the searchable properties for studies and trees in the "phylesystem"'
)
OT = cli.parse_cli()[0]
output = OT.studies_properties()
print(json.dumps(output.response_dict, indent=2, sort_keys=True))
Example #19
0
#!/usr/bin/env python3

import sys
from opentree import OTCommandLineTool

cli = OTCommandLineTool(
    usage='Display taxonomy and synthetic tree information '
    'returned by the "about" API calls.')
OT = cli.parse_cli()[0]

about = OT.about()
for k in about.keys():
    call_record = about[k]
    if call_record:
        print(k)
        call_record.write_response(sys.stdout)
        print('')
Example #20
0
    about_info = OT.about()
    synth_tree_about = about_info['synth_tree_about']
    synth_id = synth_tree_about['synth_id']
    subproblem_scaffold = OT.get_subproblem_scaffold_tree(synth_id)
    scaf_newick = subproblem_scaffold.response.text
    subprob_size_dict = OT.get_subproblem_size_info(synth_id).response_dict
    tree = OT.ws.to_object_converter.tree_from_newick(scaf_newick)
    augment_nodes_with_ot_properties(tree)
    scaffold_id_set = scaffold_tree_to_ott_id_set(tree)
    subproblem_list = []
    for synth_nd in tip_synth_node_info["lineage"]:
        snid = synth_nd["node_id"]
        if snid.startswith('ott'):
            sn_ott_id = ott_str_as_int(snid)
            if sn_ott_id in scaffold_id_set:
                m = "subproblem: OTT={} unique_name={}".format(
                    sn_ott_id, synth_nd["taxon"]["unique_name"])
                subproblem_list.append((m, sn_ott_id, synth_nd))
    subproblem_list.reverse()
    prompt_for_subproblem_exploration(synth_id, subproblem_list,
                                      subprob_size_dict)


if __name__ == '__main__':
    cli = OTCommandLineTool(
        usage='Display node info for the synthetic tree node(s) requested',
        common_args=("ott-id", ))

    OT, parsed_args = cli.parse_cli()
    main(parsed_args)
Example #21
0
#!/usr/bin/env python3
import json
import sys

from opentree import OTCommandLineTool, process_ott_and_node_id_list_args

cli = OTCommandLineTool(
    usage=
    'Display taxonomic information about the Most Recent Common Ancestor of a set of IDs',
    common_args=("ott-ids", ))
OT, args = cli.parse_cli()
ott_id_list = process_ott_and_node_id_list_args(args)[0]
if not ott_id_list:
    sys.exit('--ott-ids must be provided.\n')
output = OT.taxon_mrca(ott_ids=ott_id_list)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
Example #22
0
#!/usr/bin/env python3
import json

from opentree import OTCommandLineTool

cli = OTCommandLineTool(
    usage=
    'Look up trees in the "phylesystem" set of phylogenetic studies that are in the Open '
    'system')
cli.parser.add_argument("value", help="The value of the property to match")
cli.parser.add_argument("--property",
                        default=None,
                        required=True,
                        help='The name of the field to search through.')
cli.parser.add_argument("--verbose",
                        action="store_true",
                        help='include meta-data in response')
cli.parser.add_argument("--exact",
                        action="store_true",
                        help='disables fuzzy matching')
OT, args = cli.parse_cli()

output = OT.find_trees(args.value,
                       search_property=args.property,
                       verbose=args.verbose,
                       exact=args.exact)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
#!/usr/bin/env python3
import json

from opentree import OTCommandLineTool, get_suppressed_taxon_flag_expl_url

cli = OTCommandLineTool(
    usage='Return some names that start with the provided name.')
cli.parser.add_argument("name", help="name string to complete")
cli.parser.add_argument(
    "--context-name",
    default=None,
    help=
    'If you know the named Open Tree name searching index, you can supply it here '
    'to limit your search to only a subset of the taxonomny.')
cli.parser.add_argument(
    "--include-suppressed",
    action="store_true",
    help='Return taxa that are normally suppressed from TNRS '
    'results. See {}'.format(get_suppressed_taxon_flag_expl_url()))
OT, args = cli.parse_cli()

output = OT.tnrs_autocomplete(args.name,
                              context_name=args.context_name,
                              include_suppressed=args.include_suppressed)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
Example #24
0
#!/usr/bin/env python3
import json

from opentree import OTCommandLineTool

cli = OTCommandLineTool(usage='Look up a minimal context name of a set of names')
cli.parser.add_argument("names", nargs="+", help="names to match")
OT, args = cli.parse_cli()

output = OT.tnrs_infer_context(args.names)

print(json.dumps(output.response_dict, indent=2, sort_keys=True))
Example #25
0
#!/usr/bin/env python3
import sys
import json
from opentree import OTCommandLineTool


cli = OTCommandLineTool(usage='Display taxonomic info (based on the Open Tree Taxonomy) for a taxon ID',
                        common_args=("ott-id", ))
cli.parser.add_argument('--source-id', default=None, type=str,
                        help='A source taxonomy id for the taxon of interest, in the form prefix:id, for'
                             ' example ncbi:9443, irmng:11338. '
                             'Valid prefixes are currently ncbi, gbif, worms, if, and irmng. '
                             'Either ott_id or source_id must be given, but not both')
cli.parser.add_argument('--include-lineage', action='store_true',
                        help='Return the lineage of nodes back to the root of the tree')
cli.parser.add_argument('--include-children', action='store_true',
                        help='Return information about all the children of this taxon')
cli.parser.add_argument('--include-terminal-descendants', action='store_true',
                        help='Return a list of terminal OTT IDs that are descendants of this taxon')
OT, args = cli.parse_cli()

kwargs = {'include_children': args.include_children,
          'include_lineage': args.include_lineage,
          'include_terminal_descendants': args.include_terminal_descendants,
          }
if args.ott_id:
    o = args.ott_id if not args.ott_id.startswith('ott') else args.ott_id[3:]
    try:
        ott_id = int(o)
    except:
        sys.exit('Expecting each ott ID to be an integer or a string starting with "ott". '