Пример #1
0
def strip_to_meta_only(blob, nexson_version):
    if nexson_version is None:
        nexson_version = detect_nexson_version(blob)
    nex = get_nexml_el(blob)
    if _is_by_id_hbf(nexson_version):
        for otus_group in nex.get('otusById', {}).values():
            if 'otuById' in otus_group:
                del otus_group['otuById']
        for trees_group in nex.get('treesById', {}).values():
            tree_group = trees_group['treeById']
            key_list = tree_group.keys()
            for k in key_list:
                tree_group[k] = None
    else:
        otus = nex['otus']
        if not isinstance(otus, list):
            otus = [otus]
        for otus_group in otus:
            if 'otu' in otus_group:
                del otus_group['otu']
        trees = nex['trees']
        if not isinstance(trees, list):
            trees = [trees]
        for trees_group in trees:
            tree_list = trees_group.get('tree')
            if not isinstance(tree_list, list):
                tree_list = [tree_list]
            t = [{'id': i.get('@id')} for i in tree_list]
            trees_group['tree'] = t
Пример #2
0
def cull_nonmatching_trees(nexson, tree_id, curr_version=None):
    '''Modifies `nexson` and returns it in version 1.2.1 
    with any tree that does not match the ID removed.

    Note that this does not search through the NexSON for
    every node, edge, tree that was deleted. So the resulting
    NexSON may have broken references !
    '''
    if curr_version is None:
        curr_version = detect_nexson_version(nexson)
    if not _is_by_id_hbf(curr_version):
        nexson = convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH)

    nexml_el = get_nexml_el(nexson)
    tree_groups = nexml_el['treesById']
    tree_groups_to_del = []
    for tgi, tree_group in tree_groups.items():
        tbi = tree_group['treeById']
        if tree_id in tbi:
            trees_to_del = [i for i in tbi.keys() if i != tree_id]
            for tid in trees_to_del:
                tree_group['^ot:treeElementOrder'].remove(tid)
                del tbi[tid]
        else:
            tree_groups_to_del.append(tgi)
    for tgid in tree_groups_to_del:
        nexml_el['^ot:treesElementOrder'].remove(tgid)
        del tree_groups[tgid]
    return nexson
Пример #3
0
def extract_tree_nexson(nexson, tree_id, curr_version=None):
    '''Returns a list of (id, tree, otus_group) tuples for the
    specified tree_id (all trees if tree_id is None)
    '''
    if curr_version is None:
        curr_version = detect_nexson_version(nexson)
    if not _is_by_id_hbf(curr_version):
        nexson = convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH)

    nexml_el = get_nexml_el(nexson)
    tree_groups = nexml_el['treesById']
    tree_obj_otus_group_list = []
    for tree_group in tree_groups.values():
        if tree_id:
            tree_list = [(tree_id, tree_group['treeById'].get(tree_id))]
        else:
            tree_list = tree_group['treeById'].items()
        for tid, tree in tree_list:
            if tree is not None:
                otu_groups = nexml_el['otusById']
                ogi = tree_group['@otus']
                otu_group = otu_groups[ogi]['otuById']
                tree_obj_otus_group_list.append((tid, tree, otu_group))
                if tree_id is not None:
                    return tree_obj_otus_group_list
    return tree_obj_otus_group_list
Пример #4
0
def test_phylesystem_api_for_study(test_case_instance, phylesystem_wrapper, study_id='pg_10'):
    from peyotl.nexson_syntax.helper import detect_nexson_version, find_val_literal_meta_first
    x = phylesystem_wrapper.get(study_id)['data']
    sid = find_val_literal_meta_first(x['nexml'], 'ot:studyId', detect_nexson_version(x))
    test_case_instance.assertTrue(sid in [study_id])
    y = phylesystem_wrapper.get(study_id, tree_id='tree3', format='newick')
    test_case_instance.assertTrue(y.startswith('('))
Пример #5
0
 def testFetchStudy(self):
     try:
         x = self.phylografter.fetch_study('252')
     except:
         _LOG.exception('GET from phylografter failed!')
     else:
         sid = find_val_literal_meta_first(x['nexml'], 'ot:studyId', detect_nexson_version(x))
         self.assertTrue(sid in ['252', 'pg_252'])
Пример #6
0
 def testExternalURL(self):
     # N.B. that the URL for this API call is an odd one, e.g.
     #    http://devapi.opentreeoflife.org/phylesystem/external_url/pg_10
     pa = PhylesystemAPI(self.domains, get_from='api')
     u = pa.get_external_url('pg_10')
     re = requests.get(u).json()
     sid = find_val_literal_meta_first(re['nexml'], 'ot:studyId', detect_nexson_version(re))
     self.assertTrue(sid in ['10', 'pg_10'])
Пример #7
0
def iter_otu(nexson, nexson_version=None):
    if nexson_version is None:
        nexson_version = detect_nexson_version(nexson)
    if not _is_by_id_hbf(nexson_version):
        raise NotImplementedError('iter_otu is only supported for nexson 1.2 at this point')
    nexml = get_nexml_el(nexson)
    for og in nexml.get('otusById', {}).values():
        for otu_id, otu in og.get('otuById', {}).items():
            yield otu_id, otu
Пример #8
0
def sort_meta_elements(blob):
    '''For v0.0 (which has meta values in a list), this
    function recursively walks through the object
    and sorts each meta by @property or @rel values.
    '''
    v = detect_nexson_version(blob)
    if _is_badgerfish_version(v):
        _recursive_sort_meta(blob, '')
    return blob
Пример #9
0
def convert_nexson_format(blob,
                          out_nexson_format,
                          current_format=None,
                          remove_old_structs=True,
                          pristine_if_invalid=False,
                          sort_arbitrary=False):
    '''Take a dict form of NexSON and converts its datastructures to
    those needed to serialize as out_nexson_format.
    If current_format is not specified, it will be inferred.
    If `remove_old_structs` is False and different honeybadgerfish varieties
        are selected, the `blob` will be 'fat" containing both types
        of lookup structures.
    If pristine_if_invalid is False, then the object may be corrupted if it
        is an invalid nexson struct. Setting this to False can result in
        faster translation, but if an exception is raised the object may
        be polluted with partially constructed fields for the out_nexson_format.
    '''
    if not current_format:
        current_format = detect_nexson_version(blob)
    out_nexson_format = resolve_nexson_format(out_nexson_format)
    if current_format == out_nexson_format:
        if sort_arbitrary:
            sort_arbitrarily_ordered_nexson(blob)
        return blob
    two2zero = _is_by_id_hbf(out_nexson_format) and _is_badgerfish_version(current_format)
    zero2two = _is_by_id_hbf(current_format) and _is_badgerfish_version(out_nexson_format)
    if two2zero or zero2two:
        # go from 0.0 -> 1.0 then the 1.0->1.2 should succeed without nexml...
        blob = convert_nexson_format(blob,
                                     DIRECT_HONEY_BADGERFISH,
                                     current_format=current_format,
                                     remove_old_structs=remove_old_structs,
                                     pristine_if_invalid=pristine_if_invalid)
        current_format = DIRECT_HONEY_BADGERFISH
    ccdict = {'output_format':out_nexson_format,
              'input_format':current_format,
              'remove_old_structs': remove_old_structs,
              'pristine_if_invalid': pristine_if_invalid}
    ccfg = ConversionConfig(ccdict)
    if _is_badgerfish_version(current_format):
        converter = Badgerfish2DirectNexson(ccfg)
    elif _is_badgerfish_version(out_nexson_format):
        assert _is_direct_hbf(current_format)
        converter = Direct2BadgerfishNexson(ccfg)
    elif _is_direct_hbf(current_format) and (out_nexson_format == BY_ID_HONEY_BADGERFISH):
        converter = Direct2OptimalNexson(ccfg)
    elif _is_direct_hbf(out_nexson_format) and (current_format == BY_ID_HONEY_BADGERFISH):
        converter = Optimal2DirectNexson(ccfg)
    else:
        raise NotImplementedError('Conversion from {i} to {o}'.format(i=current_format, o=out_nexson_format))
    blob = converter.convert(blob)
    if sort_arbitrary:
        sort_arbitrarily_ordered_nexson(blob)
    return blob
Пример #10
0
def extract_supporting_file_messages(nexson):
    curr_version = detect_nexson_version(nexson)
    if not _is_by_id_hbf(curr_version):
        nexson = convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH)
    nex = nexson['nexml']
    m_list = []
    m_list.extend(_get_supporting_file_messages_for_this_obj(nex))
    for otus in nex.get('otusById', {}).values():
        m_list.extend(_get_supporting_file_messages_for_this_obj(otus))
        for otu in otus.get('otuById', {}).values():
            m_list.extend(_get_supporting_file_messages_for_this_obj(otu))
    for tree_group in nex.get('treesById', {}).values():
        m_list.extend(_get_supporting_file_messages_for_this_obj(tree_group))
        for tree in tree_group.get('treeById', {}).values():
            m_list.extend(_get_supporting_file_messages_for_this_obj(tree))
    return m_list
Пример #11
0
def sort_arbitrarily_ordered_nexson(blob):
    '''Primarily used for testing (getting nice diffs). Calls
    sort_meta_elements and then sorts otu, node and edge list by id
    '''
    # otu, node and edge elements have no necessary orger in v0.0 or v1.0
    v = detect_nexson_version(blob)
    nex = get_nexml_el(blob)
    if _is_by_id_hbf(v):
        return blob
    sort_meta_elements(blob)
    for ob in _get_index_list_of_values(nex, 'otus'):
        _inplace_sort_by_id(ob.get('otu', []))
    for tb in _get_index_list_of_values(nex, 'trees'):
        for tree in _get_index_list_of_values(tb, 'tree'):
            _inplace_sort_by_id(tree.get('node', []))
            _inplace_sort_by_id(tree.get('edge', []))
    return blob
Пример #12
0
def write_obj_as_nexml(obj_dict,
                       file_obj,
                       addindent='',
                       newl='',
                       use_default_root_atts=True,
                       otu_label='ot:originalLabel'):
    nsv = detect_nexson_version(obj_dict)
    if not _nexson_directly_translatable_to_nexml(nsv):
        convert_nexson_format(obj_dict, DIRECT_HONEY_BADGERFISH)
        nsv = DIRECT_HONEY_BADGERFISH
    ccfg = ConversionConfig(NEXML_NEXSON_VERSION,
                            input_format=nsv,
                            use_default_root_atts=use_default_root_atts,
                            otu_label=otu_label)
    converter = Nexson2Nexml(ccfg)
    doc = converter.convert(obj_dict)
    doc.writexml(file_obj, addindent=addindent, newl=newl, encoding='utf-8')
Пример #13
0
def _move_otu_at_label_properties(obj):
    nex = obj['nexml']
    if 'otus' in nex:
        syntax = SYNTAX_VERSION

        ogl = nex['otus']
        if not isinstance(ogl, list):
            ogl = [ogl]
        for og in ogl:
            for otu in og['otu']:
                _move_label_properties_for_otu(otu, syntax)
    else:
        syntax = detect_nexson_version(obj)
        assert 'otusById' in nex
        for otus_group in nex.get('otusById', {}).values():
            assert 'otuById' in otus_group
            for otu in otus_group.get('otuById', {}).values():
                _move_label_properties_for_otu(otu, syntax)
Пример #14
0
 def testExternalURL(self):
     pa = PhylesystemAPI(self.domains, get_from='api')
     u = pa.get_external_url('pg_10')
     re = requests.get(u).json()
     sid = find_val_literal_meta_first(re['nexml'], 'ot:studyId', detect_nexson_version(re))
     self.assertTrue(sid in ['10', 'pg_10'])
Пример #15
0
def nexml_el_of_by_id(nexson, curr_version=None):
    if curr_version is None:
        curr_version = detect_nexson_version(nexson)
    if not _is_by_id_hbf(curr_version):
        nexson = convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH)
    return get_nexml_el(nexson)
Пример #16
0
 def testStudy(self):
     x = self.ot.study.get('pg_10')['data']
     sid = find_val_literal_meta_first(x['nexml'], 'ot:studyId', detect_nexson_version(x))
     self.assertTrue(sid in ['10', 'pg_10'])
     y = self.ot.study.get('pg_10', tree_id='tree3', format='newick')
     self.assertTrue(y.startswith('('))
Пример #17
0
 def testFetchStudyRemote(self):
     pa = PhylesystemAPI(self.domains, get_from='api')
     x = pa.get_study('pg_10')['data']
     sid = find_val_literal_meta_first(x['nexml'], 'ot:studyId', detect_nexson_version(x))
     self.assertTrue(sid in ['10', 'pg_10'])