Package Bio :: Package Phylo :: Module _io
[hide private]
[frames] | no frames]

Source Code for Module Bio.Phylo._io

 1  # Copyright (C) 2009 by Eric Talevich (eric.talevich@gmail.com) 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license. Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  """I/O function wrappers for phylogenetic tree formats. 
 7   
 8  This API follows the same semantics as Biopython's `SeqIO` and `AlignIO`. 
 9  """ 
10   
11  from __future__ import print_function 
12   
13  from Bio import File 
14  from Bio.Phylo import ( 
15      BaseTree, 
16      NewickIO, 
17      NexusIO, 
18      PhyloXMLIO, 
19      NeXMLIO, 
20  ) 
21   
22  supported_formats = { 
23      'newick': NewickIO, 
24      'nexus': NexusIO, 
25      'phyloxml': PhyloXMLIO, 
26      'nexml': NeXMLIO, 
27  } 
28   
29  try: 
30      from Bio.Phylo import CDAOIO 
31      supported_formats['cdao'] = CDAOIO 
32  except: 
33      pass 
34   
35   
36 -def parse(file, format, **kwargs):
37 """Iteratively parse a file and return each of the trees it contains. 38 39 If a file only contains one tree, this still returns an iterable object that 40 contains one element. 41 42 Example 43 ------- 44 45 >>> trees = parse('../../Tests/PhyloXML/apaf.xml', 'phyloxml') 46 >>> for tree in trees: 47 ... print(tree.rooted) 48 True 49 """ 50 with File.as_handle(file, 'r') as fp: 51 for tree in getattr(supported_formats[format], 'parse')(fp, **kwargs): 52 yield tree
53 54
55 -def read(file, format, **kwargs):
56 """Parse a file in the given format and return a single tree. 57 58 Raises a `ValueError` if there are zero or multiple trees -- if this occurs, 59 use `parse` instead to get the complete sequence of trees. 60 """ 61 try: 62 tree_gen = parse(file, format, **kwargs) 63 tree = next(tree_gen) 64 except StopIteration: 65 raise ValueError("There are no trees in this file.") 66 try: 67 next(tree_gen) 68 except StopIteration: 69 return tree 70 else: 71 raise ValueError( 72 "There are multiple trees in this file; use parse() instead.")
73 74
75 -def write(trees, file, format, **kwargs):
76 """Write a sequence of trees to file in the given format.""" 77 if isinstance(trees, (BaseTree.Tree, BaseTree.Clade)): 78 # Passed a single tree instead of an iterable -- that's OK 79 trees = [trees] 80 with File.as_handle(file, 'w+') as fp: 81 n = getattr(supported_formats[format], 'write')(trees, fp, **kwargs) 82 return n
83 84
85 -def convert(in_file, in_format, out_file, out_format, parse_args=None, **kwargs):
86 """Convert between two tree file formats.""" 87 if parse_args is None: 88 parse_args = {} 89 trees = parse(in_file, in_format, **parse_args) 90 return write(trees, out_file, out_format, **kwargs)
91