Exemplo n.º 1
0
def test_species_translation():
    spn = "Mephitis mephitis"
    info = get_ott_taxon_info(spn)
    if info:
        ottid, ottname, ncbi_id = info
    a = ottid == 231602

    tree_of_life.mrca(ott_ids=[ottid], wrap_response=False)

    ott_ids = [770315, 158484]
    ott_mrca = get_mrca_ott(ott_ids)
    b = ott_mrca == 312031

    workdir = "tests/output/tmp"
    configfi = "tests/data/test.config"
    id_to_spn = r"tests/data/tiny_test_example/test_nicespl.csv"
    otu_jsonfi = "{}/otu_dict.json".format(workdir)
    """Tests if your own input files will generate a data object of class AlignTreeTax
	"""

    conf = ConfigObj(configfi, interactive=False)
    ids = IdDicts(conf, workdir=workdir)

    otu_json = OtuJsonDict(id_to_spn, ids)

    c = otu_json == expected_json
    assert a * b * c == 1
def test_species_translation():
    spn = "Mephitis mephitis"
    info = get_ott_taxon_info(spn)
    if info:
        ottid, ottname, ncbi_id = info
    assert ottid == 231602

    tree_of_life.mrca(ott_ids=[ottid], wrap_response=False)

    ott_ids = [770315, 158484]
    ott_mrca = get_mrca_ott(ott_ids)
    assert ott_mrca == 312031
Exemplo n.º 3
0
 def _get_mrca(self):
     if not _study_get:
         _get_study()
     ott_ids = get_subtree_otus(self.nexson, tree_id=self.tree_id) #TODO are these ottids or OTUs?
     self.mrca_node = tree_of_life.mrca(ott_ids=ottids, wrap_response=True)
     sys.stdout.write("mrca_node found, {}\n".format(mrca_node.nearest_taxon.ott_id))
     self._found_mrca = 1
Exemplo n.º 4
0
def get_mrca_ott(ott_ids):
    """finds the mrca of the taxa in the ingroup of the original
    tree. The blast search later is limited to descendents of this
    mrca according to the ncbi taxonomy"""
    if None in ott_ids:
        ott_ids.remove(None)
    try:
        mrca_node = tree_of_life.mrca(ott_ids=list(ott_ids), wrap_response=True)
    except  RuntimeError:
        sys.stderr.write("POST to get MRCA of ingroup failed - check internet connectivity, and or provide ingroup mrca OTT_ID, or check treemachine MRCA call\n")
        sys.exit()
    return mrca_node.nearest_taxon.ott_id
Exemplo n.º 5
0
def fetch_and_write_mrca(id_list, details, subtree, induced_subtree, output, errstream):
    from peyotl.sugar import tree_of_life
    mrca_node = tree_of_life.mrca(ott_ids=id_list, wrap_response=True)
    assert tuple() == mrca_node.invalid_node_ids
    assert tuple() == mrca_node.node_ids_not_in_tree
    if mrca_node.invalid_ott_ids:
        errstream.write(
            'The following OTT IDs were not valid: {}\n'.format(' '.join([str(i) for i in mrca_node.invalid_ott_ids])))
    if mrca_node.ott_ids_not_in_tree:
        f = 'The following OTT IDs are valid identifiers, but not recovered in the synthetic estimate of the tree of life: {}\n'
        errstream.write(f.format(' '.join([str(i) for i in mrca_node.ott_ids_not_in_tree])))
    DOMAIN = 'https://tree.opentreeoflife.org'
    URL_PATH_FMT = 'opentree/argus/otol.draft.22@{i:d}'
    URL_FMT = DOMAIN + '/' + URL_PATH_FMT
    url = URL_FMT.format(i=mrca_node.node_id)
    errstream.write('The (unstable) UFL for seeing this node in the ToL is: {}\n'.format(url))
    errstream.write('The (unstable) ID of the MRCA node in the graph of life is: {}\n'.format(mrca_node.node_id))
    if mrca_node.is_taxon:
        errstream.write('The node in the Graph of Life corresponds to a taxon:\n')
        mrca_node.write_report(errstream)
    else:
        errstream.write(
            'The node in the Graph of Life does not correspond to a taxon.\nThe most recent ancestor which is also a named taxon in OTT is:\n')
        mrca_node.nearest_taxon.write_report(errstream)

    if details:
        # could call mrca_node.fetch_node_info()
        errstream.write(
            'Source(s) that support this node: {}\n'.format(' '.join([str(i) for i in mrca_node.synth_sources])))
        errstream.write('Is in the synthetic tree of life? {}\n'.format(mrca_node.in_synth_tree))
        errstream.write('Correspondences with other taxonomies: {}\n'.format(mrca_node.tax_source))
        errstream.write('Is in the graph of life? {}\n'.format(mrca_node.in_graph))
        errstream.write('# tips below this node = {}\n'.format(mrca_node.num_tips))
        errstream.write('# children of this node = {}\n'.format(mrca_node.num_synth_children))

    if subtree:
        # We could ask for this using: 
        #   newick = tree_of_life.subtree(node_id=mrca_node.node_id)['newick']
        # or we can ask the GoLNodeWrapper object to do the call (as shown below)
        try:
            newick = mrca_node.subtree_newick
        except Exception as x:
            errstream.write('Could not fetch the subtree. Error: {}\n'.format(str(x)))
        else:
            errstream.write('The newick representation of the subtree rooted at this node is:\n')
            output.write('{}\n'.format(newick))

    if induced_subtree:
        induced_newick = tree_of_life.induced_subtree(ott_ids=id_list)['subtree']
        errstream.write('The newick representation of the induced subtree rooted at this node is:\n')
        output.write('{}\n'.format(induced_newick))
Exemplo n.º 6
0
def get_mrca_ott(ott_ids):
    """finds the mrca of the taxa in the ingroup of the original
    tree. The blast search later is limited to descendants of this
    mrca according to the ncbi taxonomy

    Only used in the functions that generate the ATT object.

    :param ott_ids: list of all OToL identifiers for tip labels in phylogeny
    :return: OToL identifier of most recent common ancestor or ott_ids
    """
    debug("get_mrca_ott")
    if None in ott_ids:
        ott_ids.remove(None)
    synth_tree_ott_ids = []
    ott_ids_not_in_synth = []
    for ott in ott_ids:
        r = check_if_ottid_in_synth(ott)
        if r == 1:
            synth_tree_ott_ids.append(ott)
        else:
            ott_ids_not_in_synth.append(ott)
    if len(synth_tree_ott_ids) == 0:
        sys.stderr.write(
            'No sampled taxa were found in the current synthetic tree. '
            'Please find and input and appropriate OTT id as ingroup mrca in generate_ATT_from_files'
        )
        sys.exit(-3)
    mrca_node = tree_of_life.mrca(
        ott_ids=synth_tree_ott_ids,
        wrap_response=False)  # need to fix wrap eventually
    if u'nearest_taxon' in mrca_node.keys():
        tax_id = mrca_node[u'nearest_taxon'].get(u'ott_id')
        if _VERBOSE:
            sys.stdout.write('(v3) MRCA of sampled taxa is {}\n'.format(
                mrca_node[u'nearest_taxon'][u'name']))
    elif u'taxon' in mrca_node['mrca'].keys():
        tax_id = mrca_node['mrca'][u'taxon'][u'ott_id']
        if _VERBOSE:
            sys.stdout.write('(v3) MRCA of sampled taxa is {}\n'.format(
                mrca_node['mrca'][u'taxon'][u'name']))
    else:
        # debug(mrca_node.keys())
        sys.stderr.write(
            '(v3) MRCA of sampled taxa not found. Please find and input an '
            'appropriate OTT id as ingroup mrca in generate_ATT_from_files')
        sys.exit(-4)
    return tax_id
Exemplo n.º 7
0
                             tip_label="ot:ottId"),
                 subtree_id="ingroup")
otu_dict = gen_otu_dict(n)
ottids = []
for oid, o in otu_dict.items():
    try:
        ottid = o[u'^ot:ottId']
        if ("{}:".format(ottid) in m) or ("{})".format(ottid)
                                          in m) or ("{},".format(ottid) in m):
            ottids.append(ottid)
        else:
            print(o)
    except:
        pass

mrca_node = tree_of_life.mrca(ott_ids=ottids, wrap_response=True)

newick = extract_tree(
    n, tree_id,
    PhyloSchema('newick',
                output_nexml2json='1.2.1',
                content="tree",
                tip_label="ot:originalLabel"))
newick = newick.replace(" ", "_")  #UGH

d = DnaCharacterMatrix.get(path=seqaln, schema=mattype)
# make the taxon_namespace immutable, so the tree does not add
#   new labels...
d.taxon_namespace.is_mutable = True
tre = Tree.get(data=newick, schema="newick", taxon_namespace=d.taxon_namespace)
Exemplo n.º 8
0
def test_opentree_service():
    spp_name = "h**o sapiens"
    opentree_helpers.get_ott_taxon_info("h**o sapiens")
    ottids = 515698, 590452, 643717
    mrca = physcraper.get_mrca_ott(['515698', '590452', '643717'])
    assert mrca == 1042120
    resp = tree_of_life.mrca(ott_ids=['515698'], wrap_response=False)
    expected_resp = {
        u'mrca': {
            u'taxon': {
                u'unique_name': u'Barnadesia',
                u'tax_sources':
                [u'ncbi:4237', u'gbif:8141286', u'irmng:1046392'],
                u'name': u'Barnadesia',
                u'rank': u'genus',
                u'ott_id': 515698
            },
            u'supported_by': {
                u'pg_99@tree5885': u'_node1001466 ',
                u'ott3.0draft6': u'ott515698'
            },
            u'was_constrained': True,
            u'node_id': u'ott515698',
            u'was_uncontested': True,
            u'resolves': {
                u'pg_2539@tree6294': u'_node1094881 ',
                u'ot_502@tree1': u'_node910 ',
                u'pg_1944@tree3959': u'_node740947 '
            },
            u'num_tips': 14
        },
        u'synth_id': u'opentree10.4',
        u'source_id_map': {
            u'pg_2539@tree6294': {
                u'git_sha': u'17ef8789bb8c7de13f285d5198c4e6c120695f80',
                u'study_id': u'pg_2539',
                u'tree_id': u'tree6294'
            },
            u'pg_99@tree5885': {
                u'git_sha': u'17ef8789bb8c7de13f285d5198c4e6c120695f80',
                u'study_id': u'pg_99',
                u'tree_id': u'tree5885'
            },
            u'ot_502@tree1': {
                u'git_sha': u'17ef8789bb8c7de13f285d5198c4e6c120695f80',
                u'study_id': u'ot_502',
                u'tree_id': u'tree1'
            },
            u'ott3.0draft6': {
                u'taxonomy': u'ott3.0draft6'
            },
            u'pg_1944@tree3959': {
                u'git_sha': u'17ef8789bb8c7de13f285d5198c4e6c120695f80',
                u'study_id': u'pg_1944',
                u'tree_id': u'tree3959'
            }
        }
    }
    assert resp == expected_resp
    assert opentree_helpers.check_if_ottid_in_synth(1) == 0
    assert opentree_helpers.check_if_ottid_in_synth(878252) == 0
    assert opentree_helpers.check_if_ottid_in_synth(983181) == 1
    opentree_helpers.check_if_ottid_in_synth("10000000000")
Exemplo n.º 9
0
import physcraper
from peyotl.sugar import tree_of_life, taxonomy, taxomachine

spp_name = "h**o sapiens"
physcraper.opentree_helpers.get_ott_taxon_info("h**o sapiens")

ottids = 515698, 590452, 643717
mrca = physcraper.get_mrca_ott(['515698', '590452', '643717'])

assert mrca == 1042120

resp = tree_of_life.mrca(ott_ids=['515698'], wrap_response=False)

expected_resp = {
    u'mrca': {
        u'taxon': {
            u'unique_name': u'Barnadesia',
            u'tax_sources': [u'ncbi:4237', u'gbif:8141286', u'irmng:1046392'],
            u'name': u'Barnadesia',
            u'rank': u'genus',
            u'ott_id': 515698
        },
        u'supported_by': {
            u'pg_99@tree5885': u'_node1001466 ',
            u'ott3.0draft6': u'ott515698'
        },
        u'was_constrained': True,
        u'node_id': u'ott515698',
        u'was_uncontested': True,
        u'resolves': {
            u'pg_2539@tree6294': u'_node1094881 ',