Esempio n. 1
0
File: oid.py Progetto: bsmithers/hpf
class OIDTreeParser(object):
    """
    Parse the oid.tre PAUP run file for the given Nexus Tree.
    Uses the oid translation table to rename node taxon correctly.
    @deprecated
    """
    
    def __init__(self):
        assert False, "Dummy, use the NEXUS parser" 
    
    TRANSLATION = "Translate"
    NUMBER = "number"
    TAXON = "taxon"
    TREE = "tree"

    name_line = Group(Word(nums).setResultsName(NUMBER)+
                 Word(alphanums+punctuation).setParseAction(lambda tokens:[tokens[0].replace(",","")]).setResultsName(TAXON)
                 )
    
    tree = (Consume(Literal(";")).setResultsName(TREE)
            )
        
    translation = (OneOrMore(name_line).setResultsName(TRANSLATION)+
                   Literal(";")
                   )
    
    expression = (Consume(Literal(TRANSLATION)).suppress()+
                  translation+
                  Consume(Literal("tree PAUP_1 = [&R]")).suppress()+
                  tree+
                  empty
                  )
    
    
    def parse(self,handle):
        ps = OIDTreeParser
        result = OIDTreeParser.expression.parseFile(handle)
        from Bio.Nexus.Trees import Tree
        self.taxon = {}
        runtime().debug("Found",len(result[ps.TRANSLATION]),"species")
        print result
        for translation in result[ps.TRANSLATION]:
            print translation
            num = translation[ps.NUMBER]
            taxon = translation[ps.TAXON]
            runtime().debug(num,taxon)
            self.taxon[num] = taxon
        
        self.tree = Tree(result[ps.TREE])
        assert self.tree.count_terminals() == len(self.taxon.keys(), 
               "Translation count and # of tree terminals don't match")
        for node in self.tree.get_terminals():
            runtime().debug("Rename",node.get_data().taxon,"to",self.taxon[node.get_data().taxon])
            node.get_data().taxon = self.taxon[node.get_data().taxon]
        self.tree.no
        return self