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 feed(self, handle, consumer): """S.feed(handle, consumer) Feed in a BLAST report for scanning. handle is a file-like object that contains the BLAST report. consumer is a Consumer object that will receive events as the report is scanned. """ from SAP.Bio import File # This stuff appears in 2.0.12. # <p><!-- # QBlastInfoBegin # Status=READY # QBlastInfoEnd # --><p> # <HTML> # <HEAD> # <TITLE>BLAST Search Results </TITLE> # </HEAD> # <BODY BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#660099" ALINK="#660099 # <A HREF="http://www.ncbi.nlm.nih.gov/BLAST/blast_form.map"> <IMG SRC= # <BR><BR><PRE> # BLAST Formatted information # # </BODY> # </HTML> # </BODY> # </HTML> if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) # Read HTML formatting up to the "BLAST" version line. read_and_call_until(uhandle, consumer.noevent, has_re=re.compile(r'<b>.?BLAST')) self._scan_header(uhandle, consumer) self._scan_rounds(uhandle, consumer) self._scan_database_report(uhandle, consumer) self._scan_parameters(uhandle, consumer) # Read HTML footer information. while uhandle.peekline(): read_and_call(uhandle, consumer.noevent)
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)