def FromFileParser(f, format, dialign_recode=False, **kw): format = format.lower() if format in XML_PARSERS: doctype = format format = "xml" else: doctype = None if format == "xml": source = dom = xml.dom.minidom.parse(f) if doctype is None: doctype = str(dom.doctype.name).lower() if doctype not in XML_PARSERS: raise FileFormatError("Unsupported XML doctype %s" % doctype) parser = XML_PARSERS[doctype] else: if format not in PARSERS: raise FileFormatError("Unsupported file format %s" % format) parser = PARSERS[format] source = f for (name, seq) in parser(source, **kw): if isinstance(seq, str): if dialign_recode: seq = seq.translate(_lc_to_wc) if not seq.isupper(): seq = seq.upper() yield (name, seq) f.close()
def write_alignment_to_file(f, alignment, format, **kw): format = format.lower() if format not in FORMATTERS: raise FileFormatError("Unsupported file format %s" % format) contents = FORMATTERS[format](alignment, **kw) f.write(contents) f.close()
def save_to_filename(alignment, filename, format, **kw): """Arguments: - alignment: to be written - filename: name of the sequence alignment file - format: the multiple sequence file format """ if format is None: raise FileFormatError("format not known") with atomic_write(filename, mode="wt") as f: try: write_alignment_to_file(f, alignment, format, **kw) except Exception: try: os.unlink(filename) except Exception: pass raise
def save_to_filename(alignment, filename, format, **kw): """Arguments: - alignment: to be written - filename: name of the sequence alignment file - format: the multiple sequence file format """ if format is None: raise FileFormatError("format not known") f = open_(filename, "wt") try: write_alignment_to_file(f, alignment, format, **kw) except Exception: try: os.unlink(filename) except Exception: pass raise finally: f.close()