from PIPRes.corba.api1 import CipresIDL_api1 from PIPRes.corba.api1 import CipresIDL_api1__POA import re,sys def getNO_SCORE(): return CipresIDL_api1.TreeScore(CipresIDL_api1.NO_SCORE_TYPE, 0.0) def getTreeScore(self): try: return self.m_score.intScore except: try: return self.m_score.doubleScore except: return None def hasEdgeLengths(tree): try: return tree.hasEdgeLengths except AttributeError: # TEMP @ should check all edges instead of just looking for on ':' return ':' in tree.m_newick def returnMNewick(self): return self.m_newick CipresIDL_api1.Tree.__str__ = returnMNewick CipresIDL_api1.Tree.getScore = getTreeScore def stringifyEntry(entry): return '%s %s %s %s' % (entry.registryInstanceID, entry.repositoryID, entry.applicationName, entry.description) def entryInfo(entry, interfaceInfo = False): if interfaceInfo: return stringifyEntry(entry) descStr = entry.description and '(%s)' % entry.description or '' return '%s@%s%s' % (entry.applicationName, entry.registryInstanceID, descStr) def briefEntryInfo(entry): return '%s@%s' % (entry.applicationName, entry.registryInstanceID) CipresIDL_api1.RegistryEntryInfo.__str__ = stringifyEntry CipresIDL_api1.RegistryEntryInfo.entryInfo = entryInfo CipresIDL_api1.RegistryEntryInfo.briefEntryInfo = briefEntryInfo class UnknownIDLInterfaceError(ValueError): def __init__(self, msg, interfaceName = None): ValueError.__init__(self, msg) self.interfaceName = interfaceName class CipresInterfaceEnum: names = [ 'CharacterDB', 'LifeCycle', 'MatrixAlign', 'ReadNexus', 'DelegatingTreeImprove', 'Registry', 'Rid3TreeImprove', 'Scriptable', 'TreeDB', 'TreeDecompose', 'TreeImprove', 'TreeInfer', 'TreeMerge', 'TreePrue', 'TreeSupplier', 'TreeRefine', ] reposID_RE = re.compile(r'IDL:\w+\/(\w+):[0-9.]+') def getName(i): for k, v in CipresInterfaceEnum.__dict__.iteritems(): if v == i: return k raise UnknownIDLInterfaceError, 'The enum facet %d was not found in the CipresInterfaceEnum enumeration' % i def getCode(i): if not i in CipresInterfaceEnum.__dict__: match = CipresInterfaceEnum.reposID_RE.match(i) if match: return CipresInterfaceEnum.getCode(match.group(1)) else: raise UnknownIDLInterfaceError, 'The interface name %s was not found in the CipresInterfaceEnum enumeration' % i return CipresInterfaceEnum.__dict__[i] getName = staticmethod(getName) getCode = staticmethod(getCode) #add class level attributes for each interface for n, name in enumerate(CipresInterfaceEnum.names): CipresInterfaceEnum.__dict__[name] = n def cipresInterfaceStrToEnum(strForm): '''Converts from 'IDL:CipresIDL_api1/InterfaceName/Number' to the value of 'InterfaceName' in the CipresInterfaceEnum enum.''' prefix = 'IDL:CipresIDL_api1/' if not strForm.startswith(prefix): raise ValueError, 'Expecting the repository ID to start with %s found("%s")' % (prefix, strForm) lenPrefix = len('IDL:CipresIDL_api1/') suffix = strForm[lenPrefix:] interfaceName = suffix.split(':')[0] if not interfaceName in CipresInterfaceEnum.__dict__: raise UnknownIDLInterfaceError('The interface %s (parsed from %s) is not valid.' % (interfaceName, strForm), interfaceName) return CipresInterfaceEnum.getCode(interfaceName) def buildMLeafSet(newick): '''Leaves are assumed to be numbered.''' leafPattern = re.compile(r'[(,]\s*(\d+)\s*(?=[),:])') taxNumbers = [int(i) for i in leafPattern.findall(newick)] taxNumbers.sort() return taxNumbers def _idlMatrixEq(self, other): if not other: return False if self is other: return True from PIPRes.cipres_types import CipresDiscreteMatrix if isinstance(other, CipresDiscreteMatrix): return other == self tmp = CipresDiscreteMatrix(self, shallowCopy=True) return tmp == other def _getLeafSet(tree): '''returns m_leafSet (initializing it if necessary).''' if not tree.m_leafSet: tree.m_LeafSet = buildMLeafSet(tree.m_newick) return tree.m_leafSet def _getNLeaves(tree): return len(tree.getLeafSet()) CipresIDL_api1.DataMatrix.__eq__ = _idlMatrixEq CipresIDL_api1.DataMatrix.__ne__ = lambda self, other : not _idlMatrixEq(self, other) # method to construct the leaf set from m_newick if m_leafSet is empty CipresIDL_api1.Tree.getLeafSet = _getLeafSet CipresIDL_api1.Tree.getNLeaves = _getNLeaves class CorbaError(ValueError): '''raised by RichCIpresRegistry functions if there is a corba-related difficulty.''' def __init__(self, msg = ''): self.msg = msg def __str__(self): return 'CorbaError: %s' % self.msg