Ejemplo n.º 1
0
 def get(self, study_id, content=None, schema=None, **kwargs):
     """Syntactic sugar around to make it easier to get fine-grained access
     to the parts of a file without composing a PhyloSchema object.
     Possible invocations include:
         w.get('pg_10')
         w.get('pg_10', 'trees')
         w.get('pg_10', 'trees', format='nexus')
         w.get('pg_10', tree_id='tree3')
     see:
     """
     if isinstance(study_id, TreeRef):
         return self.get(study_id=study_id.study_id,
                         tree_id=study_id.tree_id,
                         content=content,
                         schema=schema,
                         **kwargs)
     if schema is None:
         schema = create_content_spec(content=content,
                                      repo_nexml2json=self.repo_nexml2json,
                                      **kwargs)
     r = self.get_study(study_id, schema)
     if schema.content == 'study' and schema.format_str == 'nexson':
         return r
     if isinstance(r, dict) and ('data' in r):
         return r['data']
     return r
Ejemplo n.º 2
0
 def get(self, study_id, content=None, schema=None, **kwargs):
     """Syntactic sugar around to make it easier to get fine-grained access
     to the parts of a file without composing a PhyloSchema object.
     Possible invocations include:
         w.get('pg_10')
         w.get('pg_10', 'trees')
         w.get('pg_10', 'trees', format='nexus')
         w.get('pg_10', tree_id='tree3')
     see:
     """
     if isinstance(study_id, TreeRef):
         return self.get(study_id=study_id.study_id,
                         tree_id=study_id.tree_id,
                         content=content,
                         schema=schema,
                         **kwargs)
     if schema is None:
         schema = create_content_spec(content=content,
                                      repo_nexml2json=self.repo_nexml2json,
                                      **kwargs)
     r = self.get_study(study_id, schema)
     if schema.content == 'study' and schema.format_str == 'nexson':
         return r
     if isinstance(r, dict) and ('data' in r):
         return r['data']
     return r
Ejemplo n.º 3
0
 def fetch_nexsons(self, tree_list, download=False):
     nc = self.nexson_cache
     pa = self.phyleystem_api
     if download:
         pa.phylesystem_obj.pull()
     schema = create_content_spec(nexson_version='0.0.0')
     for id_obj in tree_list:
         study_id = id_obj['study_id']
         nexson = pa.get_study(study_id, schema=schema)
         path = os.path.join(nc, study_id)
         write_as_json(nexson, path)
Ejemplo n.º 4
0
 def get_study(self, study_id, schema=None):
     if self._src_code == _GET_EXTERNAL:
         url = self.get_external_url(study_id)
         nexson = self.json_http_get(url)
         r = {'data': nexson}
     elif self._src_code == _GET_LOCAL:
         nexson, sha = self.phylesystem_obj.return_study(study_id)  # pylint: disable=W0632
         r = {'data': nexson,
              'sha': sha}
     else:
         assert self._src_code == _GET_API
         if self._trans_code == _TRANS_SERVER:
             if schema is None:
                 schema = create_content_spec(nexson_version=self.repo_nexml2json)
         r = self._remote_get_study(study_id, schema)
     if (isinstance(r, dict) and 'data' in r) and (self._trans_code == _TRANS_CLIENT) and (schema is not None):
         r['data'] = schema.convert(r['data'])
     return r
Ejemplo n.º 5
0
 def get_study(self, study_id, schema=None):
     if self._src_code == _GET_EXTERNAL:
         url = self.get_external_url(study_id)
         nexson = self.json_http_get(url)
         r = {'data': nexson}
     elif self._src_code == _GET_LOCAL:
         nexson, sha = self.phylesystem_obj.return_study(study_id) #pylint: disable=W0632
         r = {'data': nexson,
              'sha': sha}
     else:
         assert self._src_code == _GET_API
         if self._trans_code == _TRANS_SERVER:
             if schema is None:
                 schema = create_content_spec(nexson_version=self.repo_nexml2json)
         r = self._remote_get_study(study_id, schema)
     if (isinstance(r, dict) and 'data' in r) and (self._trans_code == _TRANS_CLIENT) and (schema is not None):
         r['data'] = schema.convert(r['data'])
     return r
Ejemplo n.º 6
0
#!/usr/bin/env python
from peyotl.utility.str_util import UNICODE, is_str_type
from peyotl.api.wrapper import _WSWrapper, APIWrapper
from peyotl.nexson_syntax import create_content_spec
from peyotl.utility import doi2url, get_config_object, get_logger
import anyjson
_LOG = get_logger(__name__)
_OTI_NEXSON_SCHEMA = create_content_spec(format='nexson', nexson_version='0.0.0')


class _OTIWrapper(_WSWrapper):
    '''Wrapper around OTI which is a text-searching index that wraps the findAllStudies
    stored in the phylesystem. You can search for studies, trees, or nodes.

    The primary attributes of interest are:
        node_search_term_set,
        study_search_term_set, and
        tree_search_term_set
    The primary methods of interest are:
        find_all_studies,
        find_nodes,
        find_studies, and
        find_trees

    The find_nodes, find_trees, and find_studies queries will do one of the following:
        * raise an HTTPError (from requests),
        * raise a RuntimeError (if the server returns an error statement), or
        * return the matched_studies list for the query
    search terms can be sent in as keys+values in a dict or using a keyword arguments
        The key in the keyword arg will have "ot:" prepended to it, if it is not
        an already valid search term.
Ejemplo n.º 7
0
#!/usr/bin/env python
from peyotl.utility.str_util import UNICODE, is_str_type, underscored2camel_case
from peyotl.api.wrapper import _WSWrapper, APIWrapper
from peyotl.api.study_ref import TreeRefList
from peyotl.nexson_syntax import create_content_spec
from peyotl.utility import doi2url, get_config_object, get_logger
import anyjson
_LOG = get_logger(__name__)
_OTI_NEXSON_SCHEMA = create_content_spec(format='nexson',
                                         nexson_version='0.0.0')


class _OTIWrapper(_WSWrapper):
    '''Wrapper around OTI which is a text-searching index that wraps the findAllStudies
    stored in the phylesystem. You can search for studies, trees, or nodes.

    The primary attributes of interest are:
        node_search_term_set,
        study_search_term_set, and
        tree_search_term_set
    The primary methods of interest are:
        find_all_studies,
        find_nodes,
        find_studies, and
        find_trees

    The find_nodes, find_trees, and find_studies queries will do one of the following:
        * raise an HTTPError (from requests),
        * raise a RuntimeError (if the server returns an error statement), or
        * return the matched_studies list for the query
    search terms can be sent in as keys+values in a dict or using a keyword arguments
Ejemplo n.º 8
0
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