Beispiel #1
0
def dendropy_read_treefile(treefiles, quiet=False, preserve_underscores=False, **kwargs):
    out_stream = kwargs.pop('writer', sys.stderr)
    intrees = TreeList()
    if not treefiles:
        if not quiet:
            sys.stderr.write('NOTE: reading trees from stdin\n')
        trees = sys.stdin.read()
        #try two input formats
        try:
            intrees.extend(TreeList.get_from_string(trees, "nexus", case_sensitive_taxon_labels=True, preserve_underscores=preserve_underscores, **kwargs))
        except (DataParseError, NexusReader.NotNexusFileError) as e:
            sys.stderr.write('%s\n' % e.message)
            intrees.extend(TreeList.get_from_string(trees, "newick", case_sensitive_taxon_labels=True, preserve_underscores=preserve_underscores, **kwargs))
        except (DataParseError, Tokenizer.UnexpectedEndOfStreamError, AttributeError)  as e:
            if not quiet:
                sys.stderr.write('%s\n' % e.message)
                sys.exit('Could not read file %s in nexus or newick  format ...\n' % tf)
    else:
        for tf in treefiles:
            if not os.path.isfile(tf):
                out_stream.write('TreeFile %s  does not exist' % tf)
                sys.exit()

            #try two input formats
            try:
                if not quiet:
                    out_stream.write('Reading file %s in nexus format ...\n' % tf)
                intrees.extend(TreeList.get_from_path(tf, "nexus", case_sensitive_taxon_labels=True, preserve_underscores=preserve_underscores, **kwargs))

            #except (DataParseError, dendropy.dataio.nexusreader.NotNexusFileError) as e:
            except (DataParseError, NexusReader.NotNexusFileError, AttributeError) as e:
                try:
                    if not quiet:
                        out_stream.write('Reading file %s in newick format ...\n' % tf)
                    intrees.extend(TreeList.get_from_path(tf, "newick", case_sensitive_taxon_labels=True, preserve_underscores=preserve_underscores, **kwargs))
                except (DataParseError, Tokenizer.UnexpectedEndOfStreamError, AttributeError)  as e:
                    if not quiet:
                        sys.stderr.write('%s\n' % e.message)
                        sys.exit('Could not read file %s in nexus or newick  format ...\n' % tf)
    return intrees