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, basestring): if dialign_recode: seq = seq.translate(_lc_to_wc) if not seq.isupper(): seq = seq.upper() yield (name, seq)
def FromFileStructureParser(f, format, dialign_recode=False, **kw): """ Returns a structure parser for a specified format for given filename. Arguments: - filename: name of the structure file - format: the structure file format """ if not type(f) is file: raise TypeError('%s is not a file' % f) 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 return parser(source, **kw)
def write_to_file(f, entities, format, **kw): """Saves a structure in a specified format into a given file handle. Arguments: - entities: structure or entities to be written - filename: name of the structure file - format: structure file format """ format = format.lower() if format not in WRITERS: raise FileFormatError("Unsupported file format %s" % format) writer = WRITERS[format] writer(f, entities, **kw)
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, 'w') try: write_alignment_to_file(f, alignment, format, **kw) except Exception: try: os.unlink(filename) except Exception: pass raise f.close()
def write_alignment_to_file(f, alignment, format, **kw): format = format.lower() if format not in WRITERS: raise FileFormatError("Unsupported file format %s" % format) writer = WRITERS[format](f) writer.writealignment(alignment, **kw)