def write(trees, file, format, **kwargs): """Write a sequence of trees to file in the given format.""" if isinstance(trees, BaseTree.Tree) or isinstance(trees, BaseTree.Clade): # Passed a single tree instead of an iterable -- that's OK trees = [trees] with File.as_handle(file, 'w+') as fp: n = getattr(supported_formats[format], 'write')(trees, fp, **kwargs) return n
def parse(file, format, **kwargs): """Iteratively parse a file and return each of the trees it contains. If a file only contains one tree, this still returns an iterable object that contains one element. Example ------- >>> trees = parse('../../Tests/PhyloXML/apaf.xml', 'phyloxml') >>> for tree in trees: ... print(tree.rooted) True """ with File.as_handle(file, 'r') as fp: for tree in getattr(supported_formats[format], 'parse')(fp, **kwargs): yield tree
def parse_pdb_header(infile): """ Returns the header lines of a pdb file as a dictionary. Dictionary keys are: head, deposition_date, release_date, structure_method, resolution, structure_reference, journal_reference, author and compound. """ header = [] with File.as_handle(infile, 'r') as f: for l in f: record_type = l[0:6] if (record_type == 'ATOM ' or record_type == 'HETATM' or record_type == 'MODEL '): break else: header.append(l) return _parse_pdb_header_list(header)
def parse_pdb_header(infile): """ Returns the header lines of a pdb file as a dictionary. Dictionary keys are: head, deposition_date, release_date, structure_method, resolution, structure_reference, journal_reference, author and compound. """ header = [] with File.as_handle(infile, 'r') as f: for l in f: record_type=l[0:6] if (record_type=='ATOM ' or record_type=='HETATM' or record_type=='MODEL '): break else: header.append(l) return _parse_pdb_header_list(header)