Coverage for /home/deng/Projects/ete4/hackathon/ete4/ete4/nexml/__init__.py: 34%

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-03-21 09:19 +0100

1from sys import stdout 

2from . import _nexml 

3from ._nexml import * 

4from ._nexml_tree import NexmlTree 

5 

6#_nexml.AbstractTree.subclass = NexmlTree 

7_nexml.FloatTree.subclass = NexmlTree 

8_nexml.IntTree.subclass = NexmlTree 

9 

10class Nexml(_nexml.Nexml): 

11 """ Creates a new nexml project. """ 

12 def __repr__(self): 

13 return "NeXML project <%s>" %hex(hash(self)) 

14 

15 def __init__(self, *args, **kargs): 

16 _nexml.Nexml.__init__(self, *args, **kargs) 

17 

18 def build_from_file(self, fname, index_otus=True): 

19 """ Populate Nexml project with data in a nexml file. """ 

20 doc = _nexml.parsexml_(fname) 

21 rootNode = doc.getroot() 

22 rootTag, rootClass = _nexml.get_root_tag(rootNode) 

23 if rootClass is None: 

24 rootTag = 'Nexml' 

25 rootClass = self.__class__ 

26 #rootObj = rootClass.factory() 

27 self.build(rootNode) 

28 

29 # This keeps a pointer from all trees to the parent nexml 

30 # project. This way I can access other parts, such as otus, 

31 # etc... 

32 if index_otus: 

33 id2taxa = {} 

34 for taxa in self.get_otus(): 

35 id2taxon = {} 

36 for taxon in taxa.otu: 

37 id2taxon[taxon.id] = taxon 

38 id2taxa[taxa.id] = [taxa, id2taxon] 

39 

40 for trees in self.get_trees(): 

41 for t in trees.get_tree(): 

42 t.set_nexml_project(self) 

43 if trees.otus in id2taxa: 

44 t.nexml_otus = id2taxa[trees.otus][0] 

45 

46 def export(self, outfile=stdout, level=0): 

47 namespace='xmlns:nex="http://www.nexml.org/2009"' 

48 return super(Nexml, self).export(outfile=outfile, level=level, namespacedef_=namespace) 

49 

50 

51__all__ = _nexml.__all__ + ["Nexml", "NexmlTree"]