コード例 #1
0
ファイル: __init__.py プロジェクト: pombredanne/peyotl
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
コード例 #2
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
コード例 #3
0
ファイル: __init__.py プロジェクト: pombredanne/peyotl
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
コード例 #4
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
コード例 #5
0
def add_resource_meta(obj, rel, href, version):
    if _is_badgerfish_version(version):
        m = obj.setdefault('meta', [])
        if not isinstance(m, list):
            m = [m]
            obj['meta'] = m
        m.append({'@href': href, '@rel': rel, '@xsi:type': 'nex:ResourceMeta'})
    else:
        k = '^' + rel
        _add_value_to_dict_bf(obj, k, {'@href': href})
コード例 #6
0
ファイル: __init__.py プロジェクト: pombredanne/peyotl
def add_resource_meta(obj, rel, href, version):
    if _is_badgerfish_version(version):
        m = obj.setdefault('meta', [])
        if not isinstance(m, list):
            m = [m]
            obj['meta'] = m
        m.append({'@href':href,
                  '@rel': rel,
                  '@xsi:type': 'nex:ResourceMeta'})
    else:
        k = '^' + rel
        _add_value_to_dict_bf(obj, k, {'@href': href})
コード例 #7
0
ファイル: nexson2nexml.py プロジェクト: pombredanne/peyotl
 def __init__(self, conv_cfg):
     NexsonConverter.__init__(self, conv_cfg)
     self.input_format = conv_cfg.input_format
     self.use_default_root_atts = conv_cfg.get('use_default_root_atts', True)
     self.otu_label = conv_cfg.get('otu_label', 'ot:originalLabel')
     if self.otu_label.startswith('^'):
         self.otu_label = self.otu_label[1:]
     self._migrating_from_bf = _is_badgerfish_version(self.input_format)
     # TreeBase and phylografter trees often lack the tree xsi:type
     self._adding_tree_xsi_type = True
     # we have started using ot:ottTaxonName, ot:originalLabel or ot:ottId
     self._creating_otu_label = True
コード例 #8
0
 def __init__(self, conv_cfg):
     NexsonConverter.__init__(self, conv_cfg)
     self.input_format = conv_cfg.input_format
     self.use_default_root_atts = conv_cfg.get('use_default_root_atts',
                                               True)
     self.otu_label = conv_cfg.get('otu_label', 'ot:originalLabel')
     if self.otu_label.startswith('^'):
         self.otu_label = self.otu_label[1:]
     self._migrating_from_bf = _is_badgerfish_version(self.input_format)
     # TreeBase and phylografter trees often lack the tree xsi:type
     self._adding_tree_xsi_type = True
     # we have started using ot:ottTaxonName, ot:originalLabel or ot:ottId
     self._creating_otu_label = True
コード例 #9
0
ファイル: adaptor.py プロジェクト: mtholder/peyotl
def create_validation_adaptor(obj, logger, **kwargs):
    try:
        nexson_version = detect_nexson_version(obj)
    except:
        return BadgerFishValidationAdaptor(obj, logger, **kwargs)
    if _is_by_id_hbf(nexson_version):
        # _LOG.debug('validating as ById...')
        return ByIdHBFValidationAdaptor(obj, logger, **kwargs)
    elif _is_badgerfish_version(nexson_version):
        # _LOG.debug('validating as BadgerFish...')
        return BadgerFishValidationAdaptor(obj, logger, **kwargs)
    elif _is_direct_hbf(nexson_version):
        # _LOG.debug('validating as DirectHBF...')
        return DirectHBFValidationAdaptor(obj, logger, **kwargs)
    raise NotImplementedError('nexml2json version {v}'.format(v=nexson_version))
コード例 #10
0
def add_schema_attributes(container, nexson_version):
    """Adds several attributes to `container`:
    _using_hbf_meta  - boolean. True for HoneyBadgerFish v1-style meta elements ('^prop': value
                rather than 'meta': {'$':value})
    and the following _SchemaFragment instances:
    _NexmlEl_Schema
    """
    if _is_by_id_hbf(nexson_version):
        _add_by_id_nexson_schema_attributes(container)
    elif _is_badgerfish_version(nexson_version):
        _add_badgerfish_nexson_schema_attributes(container)
    elif _is_direct_hbf(nexson_version):
        _add_direct_nexson_schema_attributes(container)
    else:
        raise NotImplementedError('unrecognized nexson variant {}'.format(nexson_version))
コード例 #11
0
ファイル: __init__.py プロジェクト: pombredanne/peyotl
def _nexson_directly_translatable_to_nexml(vers):
    'TEMP: until we refactor nexml writing code to be more general...'
    return (_is_badgerfish_version(vers)
            or _is_direct_hbf(vers)
            or vers == 'nexml')
コード例 #12
0
def _nexson_directly_translatable_to_nexml(vers):
    'TEMP: until we refactor nexml writing code to be more general...'
    return (_is_badgerfish_version(vers)
            or _is_direct_hbf(vers)
            or vers == 'nexml')
コード例 #13
0
 def __init__(self, conv_cfg):
     NexsonConverter.__init__(self, conv_cfg)
     self.output_format = conv_cfg.output_format
     self._badgerfish_style_conversion = _is_badgerfish_version(
         conv_cfg.output_format)
コード例 #14
0
ファイル: nexml2nexson.py プロジェクト: OpenTreeOfLife/peyotl
 def __init__(self, conv_cfg):
     NexsonConverter.__init__(self, conv_cfg)
     self.output_format = conv_cfg.output_format
     self._badgerfish_style_conversion = _is_badgerfish_version(conv_cfg.output_format)