def write(trees, file, format, **kwargs): """Write a sequence of trees to file in the given format.""" if isinstance(trees, (BaseTree.Tree, 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 write(trees, file, format, **kwargs): """Write a sequence of trees to file in the given format.""" if isinstance(trees, (BaseTree.Tree, 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 treeappend(trees, file, format, **kwargs): """appending version of Phylo._io.write()""" if isinstance(trees, (BaseTree.Tree, BaseTree.Clade)): # Passed a single tree instead of an iterable -- that's OK trees = [trees] with File.as_handle(file, 'a+') as fp: n = getattr(supported_formats[format], 'write')(trees, fp, **kwargs) return n
def test_path(self): "Test as_handle with a path argument" p = self._path('test_file.fasta') mode = 'wb' with File.as_handle(p, mode=mode) as handle: self.assertEqual(p, handle.name) self.assertEqual(mode, handle.mode) self.assertFalse(handle.closed) self.assertTrue(handle.closed)
def test_string_path(self): "Test as_handle with a string path argument" p = self._path('test_file.fasta') mode = 'wb' with File.as_handle(p, mode=mode) as handle: self.assertEqual(p, handle.name) self.assertEqual(mode, handle.mode) self.assertFalse(handle.closed) self.assertTrue(handle.closed)
def test_path_object(self): "Test as_handle with a pathlib.Path object" from pathlib import Path p = Path(self._path('test_file.fasta')) mode = 'wb' with File.as_handle(p, mode=mode) as handle: self.assertEqual(str(p.absolute()), handle.name) self.assertEqual(mode, handle.mode) self.assertFalse(handle.closed) self.assertTrue(handle.closed)
def test_handle(self): "Test as_handle with a file-like object argument" p = self._path('test_file.fasta') with open(p, 'wb') as fp: with File.as_handle(fp) as handle: self.assertEqual(fp, handle, "as_handle should " "return argument when given a file-like object") self.assertFalse(handle.closed) self.assertFalse(handle.closed, "Exiting as_handle given a file-like object should not " "close the file")
def test_custom_path_like_object(self): """Test as_handle with a custom path-like object.""" class CustomPathLike: def __init__(self, path): self.path = path def __fspath__(self): return self.path p = CustomPathLike(self._path("test_file.fasta")) mode = "wb" with File.as_handle(p, mode=mode) as handle: self.assertEqual(p.path, handle.name) self.assertEqual(mode, handle.mode) self.assertFalse(handle.closed) self.assertTrue(handle.closed)
def test_custom_path_like_object(self): "Test as_handle with a custom path-like object" class CustomPathLike: def __init__(self, path): self.path = path def __fspath__(self): return self.path p = CustomPathLike(self._path('test_file.fasta')) mode = 'wb' with File.as_handle(p, mode=mode) as handle: self.assertEqual(p.path, handle.name) self.assertEqual(mode, handle.mode) self.assertFalse(handle.closed) self.assertTrue(handle.closed)
def parse_pdb_header(infile): """Return 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 in ("ATOM ", "HETATM", "MODEL "): break else: header.append(l) return _parse_pdb_header_list(header)
def parse_pdb_header(infile): """Return 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 in ("ATOM ", "HETATM", "MODEL "): break else: header.append(l) return _parse_pdb_header_list(header)
def parse(file, format, **kwargs): """Parse a file iteratively, and yield each of the trees it contains. If a file only contains one tree, this still returns an iterable object that contains one element. Examples -------- >>> import Bio.Phylo >>> trees = Bio.Phylo.parse('PhyloXML/apaf.xml', 'phyloxml') >>> for tree in trees: ... print(tree.rooted) True """ with File.as_handle(file) as fp: yield from getattr(supported_formats[format], "parse")(fp, **kwargs)
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. Examples -------- >>> 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(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 test_handle(self): """Test as_handle with a file-like object argument.""" p = self._path("test_file.fasta") with open(p, "wb") as fp: with File.as_handle(fp) as handle: self.assertEqual( fp, handle, "as_handle should " "return argument when given a " "file-like object", ) self.assertFalse(handle.closed) self.assertFalse( handle.closed, "Exiting as_handle given a file-like object " "should not close the file", )
def read(handle, dtype=float): """Parse the file and return an Array object.""" header = [] with File.as_handle(handle) as fp: for line in fp: if not line.startswith("#"): break header.append(line[1:].strip()) row = line.split() rows = [row] for line in fp: row = line.split() rows.append(row) if len(rows[0]) == len(rows[1]) == 2: alphabet = [key for key, value in rows] for key in alphabet: if len(key) > 1: alphabet = tuple(alphabet) break else: alphabet = "".join(alphabet) matrix = Array(alphabet=alphabet, dims=1, dtype=dtype) matrix.update(rows) else: alphabet = rows.pop(0) for key in alphabet: if len(key) > 1: alphabet = tuple(alphabet) break else: alphabet = "".join(alphabet) matrix = Array(alphabet=alphabet, dims=2, dtype=dtype) for letter1, row in zip(alphabet, rows): assert letter1 == row.pop(0) for letter2, word in zip(alphabet, row): matrix[letter1, letter2] = float(word) matrix.header = header return matrix
def test_stringio(self): s = StringIO() with File.as_handle(s) as handle: self.assertEqual(s, handle)
def test_stringio(self): s = StringIO() with File.as_handle(s) as handle: self.assertIs(s, handle)
def test_stringio(self): """Testing passing StringIO handles.""" s = StringIO() with File.as_handle(s) as handle: self.assertIs(s, handle)