Exemple #1
0
class Wrapper(object):
    '''
    This class contains wrappers for all the tools used in this project
    '''


    def __init__(self, workDir, resultsFile, inFile, coreId, seqType, seedNum, bootNum, method, interLeaved):
        '''
        Data Fields:
        work_dir   = temproray directory
        inFile     = data file,
        id         = core id (int), 
        seqType    = d (dna); p (protein); r (rna), 
        bootNum    = number of replicates,
        seedNum    = Random number seed between 1 and 32767
        method     =b (Bootstrap) Default, 
                    j (Jackknife)
                    c (Permute species for each character)
                    o (Permute character order)
                    s (Permute within species)
                    r (Rewrite data)),
        interLeaved=True if sequence data is interleaved otherwise False
        '''
        self.work_dir       = workDir
        self.resultsFile    = resultsFile
        self.inFile         = inFile 
        self.coreId         = coreId
        self.seqType        = seqType
        self.seedNum        = seedNum
        self.bootNum        = bootNum
        self.method         = method
        self.n              = bootNum
        self.nt             = False
        self.interLeaved    = interLeaved
        self.outFile        = "bootstrap_"+str(coreId)+".out"
        self.newSpeciesTree = Tree()
        self.leafLabelStree = []
        self.internalExRootSpeceLabels= []
        
        if self.seqType in ['r', 'd']:
            self.nt = True
    
    def getSpeciesTree(self):
        return self.newSpeciesTree
    
    def setSpeceistree(self, stree):
        self.newSpeciesTree= stree

    def writeSpeciesTree(self):
        self.newSpeciesTree.write_to_path(self.resultsFile+'.speciestree.'+str(self.coreId), 'newick', suppress_internal_node_labels=False,annotations_as_nhx=True, extract_comment_metadata=True , suppress_annotations=False)
    
    def allLeafLabels(self, myTree):
        '''
        returns the list containing all the leaf nodes' label of this tree and the internal nodes without the root label
        '''
        leafLabels=[]
        internalLabelsExRoot=[]
        for i in myTree.leaf_nodes():
            leafLabels.append(i.get_node_str().replace("'", ""))
        for i in myTree.internal_nodes():
            if i.level() != 0:
                internalLabelsExRoot.append(i.label)
        return leafLabels, internalLabelsExRoot
    def onlyLeafLabels(self, myTree):
        '''
            returns the list containing labels of leaf nodes for this tree
        '''
        leafLabels=[]
        for i in myTree.leaf_nodes():
            leafLabels.append(i.get_node_str().replace("'", ""))
        return leafLabels
        
    def readTreeFromFile(self, treePath):
        '''
        input: path to the file containing newick tree
        return Tree object 
        '''
        print treePath
        myTree= Tree()
        myTree= Tree.get_from_path(treePath, 'newick', suppress_edge_lengths=False, annotations_as_nhx=True, extract_comment_metadata=True , suppress_annotations=False)
        return myTree

    def readTreeFromString(self, treeString):
        '''
        input: string containing newick tree
        return Tree object 
        '''
        myTree= Tree()
        myTree= Tree.get_from_string( treeString, 'newick', annotations_as_nhx=True, extract_comment_metadata=True , suppress_annotations=False)
        return myTree
           
    def checkExe(self, exePath):
        return os.path.isfile(exePath) and os.access(exePath, os.X_OK)
    
    def Where(self, program):
        '''
        input: name of executable
        output: path to executable
        '''
        fpath, fname = os.path.split(program)
        if fpath:
            if self.checkExe(program):
                return program
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                path = path.strip('"')
                pathToProgram = os.path.join(path, program)
                if self.checkExe(pathToProgram):
                    return pathToProgram
        return None
    
    def checkNotung(self, program):
        for path in os.environ["PATH"].split(os.pathsep):
            path = path.strip('"')
            pathToProgram = os.path.join(path, program)
            if os.path.isfile(pathToProgram):
                return pathToProgram
        return None
    
        
    def WrapFseqboot(self):
        '''
        WrapFseqboot() function is a wrapper over the fseqboot tool.
        input:  inFile     = data file,
                id         = core id (int), 
                seqType    = d (dna); p (protein); r (rna), 
                bootNum    = number of replicates,
                seedNum    = Random number seed between 1 and 32767
                method    = b (Bootstrap) Default, 
                            j (Jackknife)
                            c (Permute species for each character)
                            o (Permute character order)
                            s (Permute within species)
                            r (Rewrite data)),
     
        output: bootstrap alignments to bootstrap_(id).out
        '''
        
        # check if path exists
        cmd= self.Where('fseqboot')
        
        if cmd != None:
            
            bootSeqOutFile= os.path.join(self.work_dir, self.outFile)
            print "bootSeqOutFile: %s"  % bootSeqOutFile
            null = open("/dev/null")
            print "--> Bootstrap begins..."
            try:
                cline = FSeqBootCommandline(sequence = self.inFile, outfile = bootSeqOutFile, seqtype = self.seqType, test= self.method,  seed= self.seedNum, reps = self.bootNum, filter = True)
                stdout, stderr= cline()
                
            except IOError, e:
                print ("Class: Wrapper, Function: WrapFseqboot(): %s " % e)
            print "--> Bootstrap done..."
        else: