#!/usr/bin/python # Copyright (c) 2005-2006 by Mark T. Holder # Florida State University. (see end of file) # New file (post April, 2006, expected to be clean under pylint) '''Reads the output from MRBAYES (version 3.0 or greater). Basically this is a trees block reader that will tolerate unfinished files.''' from PIPRes.exceptions import CipresException from PIPRes.util.server_import import * from PIPRes.wrap.idl import CipresIDL_api1 _LOG = cipresGetLogger('PIPRes.service_impl.mb_tree_reader') class MBTreeSupplier(CipresIDL_api1__POA.TreeSupplier, SimpleServer): OutOfRangeBehavior = CipresIDL_api1.TreeSupplier.OutOfRangeBehavior oorb_error = CipresIDL_api1.TreeSupplier.error oorb_waitForTree = CipresIDL_api1.TreeSupplier.waitForTree oorb_padWithNullTree = CipresIDL_api1.TreeSupplier.padWithNullTree def __init__(self, registry = None): SimpleServer.__init__(self, registry) self.convertTreeFunc = self.orb is None and toTreeBridge or toIDLTree self.pathToTreefile = None self._initVariableAtts() def _initVariableAtts(self): "called to reinitialize attributes (in preparation of reading a new file)." self.taxLabels = None self.taxManager = None # the externalCurr position is controlled by the stream interface (tell, seek, next) # _lastRead actually reflects the current position in the file (these will differ # if the random access interface is used). self.externalCurrTree = 0 self.externalCurrFilePos = None self._lastReadTree = -1 self._lastReadFilePos = None self.numTrees = -1 self.maxTreeSeen = -1 self.firstTreeFilePos = None self.endRead = False self.fileObj = None def getUIXml(self): return ''' InFile true A MRBAYES ".t" file. $nexusFilepath; ''' def execute(self, command): _LOG.debug('execute called with "%s"' % command) self.pathToTreefile = command self._initVariableAtts() return (True, '') def getNumTrees(self): self._triggerRead(MBTreeSupplier.oorb_padWithNullTree, toEnd=True); return self.numTrees def getTree(self, treeIndex, outOfRangeBehavior): self._triggerRead(outOfRangeBehavior); return _exceptionTranslate(toIDLTree, tree) def getTrees(self, fromIndex, toIndex, outOfRangeBehavior): self._triggerRead(outOfRangeBehavior); trees = _exceptionTranslate(self.implRef.getTrees, fromIndex, toIndex, outOfRangeBehavior) return _exceptionTranslate(map, toIDLTree, tree) def tell(self): return self.currTree def seek(self, treeIndex, outOfRangeBehavior): self._triggerRead(outOfRangeBehavior); return _exceptionTranslate(self.implRef.seek, treeIndex, outOfRangeBehavior) def next(self, numTrees, outOfRangeBehavior): return _exceptionTranslate(toIDLTree, tree) def _openFile(self, outOfRangeBehavior): "Tries to open (or reopen) the filepath in pathToTreefile set by the execute call" self._initVariableAtts() if not self.pathToTreefile: raise CipresException(CipresException.MAJOR_API, "filepath must be specified by an execute() call") try: self.fileObj = open(self.pathToTreeFile, 'rU') except: if outOfRangeBehavior == MBTreeSupplier.oorb_error: raise CipresException(CipresException.MAJOR_BAD_ARG, "could not open %s." % self.pathToTreeFile) return False return True def _moveToFirstTree(self, outOfRangeBehavior): assert(self.fileObj is not None) def _triggerRead(self, outOfRangeBehavior, toEnd=False): """Called before any method that requires access to the file as a hook to standardize behavior. returns False if the file could not be accessed and outOfRangeBehavior is wait or pad (raises an exception if outOfRangeBehavior is set to error).""" if self.fileObj is None: if not self._openFile(outOfRangeBehavior): return False self._moveToFirstTree(outOfRangeBehavior) if toEnd and not self.endRead: self._readToEnd(outOfRangeBehavior) return True if __name__ == '__main__': import doctest doctest.testmod() # This file is part of the PIPRes library # # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA