Exemple #1
0
#!/usr/bin/env python
'''Reads OTT files and produces a set of files with preorder
numbering that allow of minimal memory footprint in python scripts

Note: this script is a memory hog
'''

if __name__ == '__main__':
    from peyotl.ott import OTT
    ott = OTT()
    ott.create_pickle_files()

Exemple #2
0
            out = codecs.open(outfn, mode='w', encoding='utf-8')
        except:
            sys.exit('validate_ot_nexson: Could not open output filepath "{fn}"\n'.format(fn=outfn))
    else:
        out = codecs.getwriter('utf-8')(sys.stdout)
    try:
        nexson = read_as_json(inp_filepath)
    except ValueError as vx:
        _LOG.error('Not valid JSON.')
        if args.verbose:
            raise vx
        else:
            sys.exit(1)
    except Exception as nx:
        _LOG.error(nx.value)
        sys.exit(1)
    convert_nexson_format(nexson, BY_ID_HONEY_BADGERFISH)
    trees = extract_tree_nexson(nexson, tree_id=args.tree_id)
    if len(trees) == 0:
        trees = extract_tree_nexson(nexson, tree_id=None)
        if trees:
            v = '", "'.join([i[0] for i in trees])
            sys.exit('Tree ID {i} not found. Valid IDs for this file are "{l}"\n'.format(i=args.tree_id, l=v))
        else:
            sys.exit('This NexSON has not trees.\n')
    ott = OTT()
    for tree_id, tree, otus in trees:
        tree_proxy = NexsonTreeProxy(tree=tree, tree_id=tree_id, otus=otus)
        evaluate_tree_rooting(nexson, ott, tree_proxy)

                        required=False,
                        help='Optional comma-separated list of flags to prune. '
                             'If omitted, the treemachine flags are used.')
    parser.add_argument('--root',
                        default=None,
                        type=int,
                        required=False,
                        help='Optional taxonomy root argument.')
    args = parser.parse_args(sys.argv[1:])
    ott_dir, output, log_filename, root = args.ott_dir, args.output, args.log, args.root
    flags_str = args.flags
    try:
        assert os.path.isdir(args.ott_dir)
    except:
        sys.exit('Expecting ott-dir argument to be a directory. Got "{}"'.format(args.ott_dir))
    ott = OTT(ott_dir=args.ott_dir)
    if flags_str is None:
        flags = ott.TREEMACHINE_SUPPRESS_FLAGS
    else:
        flags = flags_str.split(',')
    create_log = log_filename is not None
    with codecs.open(args.output, 'w', encoding='utf-8') as outp:
        log = ott.write_newick(outp,
                               label_style=OTULabelStyleEnum.CURRENT_LABEL_OTT_ID,
                               root_ott_id=root,
                               prune_flags=flags,
                               create_log_dict=create_log)
        outp.write('\n')
    if create_log:
        write_as_json(log, log_filename)
             inp_files = [i.strip() for i in tf if i.strip()]
     else:
         error(
             'nexson file must be specified as a positional argument or via the --nexson-file-tags or --input-files-list argument.')
         sys.exit(1)
 if not inp_files:
     error('No input files specified.')
 in_dir = args.input_dir
 if in_dir:
     in_dir = os.path.expanduser(in_dir)
     inp_files = [os.path.join(in_dir, i) for i in inp_files]
 if flags_str is None:
     flags = OTT.TREEMACHINE_SUPPRESS_FLAGS
 else:
     flags = flags_str.split(',')
 ott = OTT(ott_dir=args.ott_dir)
 to_prune_fsi_set = ott.convert_flag_string_set_to_union(flags)
 for inp in inp_files:
     _LOG.debug('{}'.format(inp))
     log_obj = {}
     inp_fn = os.path.split(inp)[-1]
     study_tree = '.'.join(inp_fn.split('.')[:-1])  # strip extension
     study_id, tree_id = propinquity_fn_to_study_tree(inp_fn)
     nexson_blob = read_as_json(inp)
     ntw = NexsonTreeWrapper(nexson_blob, tree_id, log_obj=log_obj)
     assert ntw.root_node_id
     taxonomy_treefile = os.path.join(args.out_dir, study_tree + '-taxonomy.tre')
     try:
         ntw.prune_tree_for_supertree(ott=ott,
                                      to_prune_fsi_set=to_prune_fsi_set,
                                      root_ott_id=root,
Exemple #5
0
     'If omitted, the treemachine flags are used.')
 parser.add_argument('--root',
                     default=None,
                     type=int,
                     required=False,
                     help='Optional taxonomy root argument.')
 args = parser.parse_args(sys.argv[1:])
 ott_dir, output, log_filename, root = args.ott_dir, args.output, args.log, args.root
 flags_str = args.flags
 try:
     assert os.path.isdir(args.ott_dir)
 except:
     sys.exit(
         'Expecting ott-dir argument to be a directory. Got "{}"'.format(
             args.ott_dir))
 ott = OTT(ott_dir=args.ott_dir)
 if flags_str is None:
     flags = ott.TREEMACHINE_SUPPRESS_FLAGS
 else:
     flags = flags_str.split(',')
 create_log = log_filename is not None
 with codecs.open(args.output, 'w', encoding='utf-8') as outp:
     log = ott.write_newick(
         outp,
         label_style=OTULabelStyleEnum.CURRENT_LABEL_OTT_ID,
         root_ott_id=root,
         prune_flags=flags,
         create_log_dict=create_log)
     outp.write('\n')
 if create_log:
     write_as_json(log, log_filename)
Exemple #6
0
def ott_shell_command(args):
    ott = OTT()
    _LOG.info('launching bash in your OTT dir...')
    if subprocess.Popen('bash', cwd=ott.ott_dir).wait() != 0:
        raise RuntimeError('bash in ott dir failed.')
Exemple #7
0
def ott_clear_command(args):
    ott = OTT()
    ott.remove_caches()
Exemple #8
0
     else:
         error(
             'nexson file must be specified as a positional argument or via the --nexson-file-tags or --input-files-list argument.'
         )
         sys.exit(1)
 if not inp_files:
     error('No input files specified.')
 in_dir = args.input_dir
 if in_dir:
     in_dir = os.path.expanduser(in_dir)
     inp_files = [os.path.join(in_dir, i) for i in inp_files]
 if flags_str is None:
     flags = OTT.TREEMACHINE_SUPPRESS_FLAGS
 else:
     flags = flags_str.split(',')
 ott = OTT(ott_dir=args.ott_dir)
 to_prune_fsi_set = ott.convert_flag_string_set_to_union(flags)
 for inp in inp_files:
     _LOG.debug('{}'.format(inp))
     log_obj = {}
     inp_fn = os.path.split(inp)[-1]
     study_tree = '.'.join(inp_fn.split('.')[:-1])  # strip extension
     study_id, tree_id = propinquity_fn_to_study_tree(inp_fn)
     nexson_blob = read_as_json(inp)
     ntw = NexsonTreeWrapper(nexson_blob, tree_id, log_obj=log_obj)
     assert ntw.root_node_id
     taxonomy_treefile = os.path.join(args.out_dir,
                                      study_tree + '-taxonomy.tre')
     try:
         ntw.prune_tree_for_supertree(
             ott=ott,