Exemplo n.º 1
0
    def fastme(self):
        """Use fastme to make a minimum-evolution tree, which is returned.

        The resulting tree is read in by p4, and is returned.

        We interact with fastme by writing files, but care is taken that
        existing files are not overwritten, because new file names are
        made to be unique. 

        If the branch lengths are less than zero, they are made to be zero.
        """

        gm = ["DistanceMatrix.fastme()"]

        flob_dm, dmFName_fq = func.uniqueFile('tmp.dm')
        flob_tf, treeFName_fq = func.uniqueFile(
            'tmp.tree')  #tempfile.mkstemp(suffix='tree', dir=theDir)
        flob_tf.close()

        # Throw the dir and dirname away.
        dirname, dmFName = os.path.split(dmFName_fq)
        dirname, treeFName = os.path.split(treeFName_fq)

        # Write the files, do the analysis
        self.writePhylipToOpenFile(flob_dm, 6)
        flob_dm.close()

        os.system("fastme -i %s -o %s" % (dmFName, treeFName))

        # This is the result.  The tree, if it exists, is read in by p4.
        oldLen = len(var.trees)
        func.read(treeFName)
        newLen = len(var.trees)
        if newLen == oldLen + 1:
            pass
        else:
            gm.append("I was expecting exactly one tree.  Got %i" %
                      (oldLen - newLen))
            raise Glitch, gm
        t = var.trees.pop()

        # Tidy up.
        os.remove(treeFName)
        os.remove(dmFName)

        for n in t.iterNodesNoRoot():
            if n.br.len < 0.0:
                n.br.len = 0.0
            if not n.isLeaf:
                n.name = None

        return t
Exemplo n.º 2
0
    def fastme(self):
        """Use fastme to make a minimum-evolution tree, which is returned.

        The resulting tree is read in by p4, and is returned.

        We interact with fastme by writing files, but care is taken that
        existing files are not overwritten, because new file names are
        made to be unique. 

        If the branch lengths are less than zero, they are made to be zero.
        """

        gm = ["DistanceMatrix.fastme()"]

        flob_dm, dmFName_fq = func.uniqueFile('tmp.dm')
        # tempfile.mkstemp(suffix='tree', dir=theDir)
        flob_tf, treeFName_fq = func.uniqueFile('tmp.tree')
        flob_tf.close()

        # Throw the dir and dirname away.
        dirname, dmFName = os.path.split(dmFName_fq)
        dirname, treeFName = os.path.split(treeFName_fq)

        # Write the files, do the analysis
        self.writePhylipToOpenFile(flob_dm, 6)
        flob_dm.close()

        os.system("fastme -i %s -o %s" % (dmFName, treeFName))

        # This is the result.  The tree, if it exists, is read in by p4.
        oldLen = len(var.trees)
        func.read(treeFName)
        newLen = len(var.trees)
        if newLen == oldLen + 1:
            pass
        else:
            gm.append("I was expecting exactly one tree.  Got %i" %
                      (oldLen - newLen))
            raise P4Error(gm)
        t = var.trees.pop()

        # Tidy up.
        os.remove(treeFName)
        os.remove(dmFName)

        for n in t.iterNodesNoRoot():
            if n.br.len < 0.0:
                n.br.len = 0.0
            if not n.isLeaf:
                n.name = None

        return t
Exemplo n.º 3
0
    def njUsingPaup(self, paupPath='paup'):
        """Use paup to make a neighbor-joining tree, which is returned.

        The resulting tree is read in by p4, and is returned.

        We interact with paup by writing files, but care is taken that
        existing files are not overwritten, because new file names are
        made to be unique. 

        If this does not work well, try setting the paupPath arg.
        """

        gm = ["DistanceMatrix.njUsingPaup()"]

        #filename    = sha.new(str(os.getpid())).hexdigest()[-10:]
        #dmFName     = os.path.join(pathPrefix, "%s.dmat" % filename)
        #treeFName   = os.path.join(pathPrefix, "%s.tree" % filename)
        #pFName      = os.path.join(pathPrefix, "%s.cmds" % filename)

        #tempfile.mkstemp(suffix='', prefix='tmp', dir=None, text=False)
        #if pathPrefix:
        #    theDir = pathPrefix
        #else:
        #    theDir = None
        flob_dm, dmFName_fq = func.uniqueFile('tmp.dm')
        flob_tf, treeFName_fq = func.uniqueFile('tmp.tree') #tempfile.mkstemp(suffix='tree', dir=theDir)
        flob_tf.close()
        flob_pf, pFName = func.uniqueFile('tmp.cmds') # tempfile.mkstemp(suffix='cmds', dir=theDir)

        # Throw the dir and dirname away. 
        dirname, dmFName = os.path.split(dmFName_fq)
        dirname, treeFName = os.path.split(treeFName_fq)

        # Make the paup commands
        paupCommandString = """#nexus
        begin paup;
          execute %s;
          set crit=dist;
          dset negbrlen=setzero;
          nj;
          savetrees file=%s format=altnex brlens=yes taxablk=yes replace=yes;
          quit;
        end;
        
        """ % (dmFName, treeFName)

        #print paupCommandString

        # Write the files, do the analysis
        #writeNexusToOpenFile(self, flob, writeTaxaBlock, append, digitsAfterDecimal)
        self.writeNexusToOpenFile(flob_dm, True, False, 6)
        flob_dm.close()
        flob_pf.write(paupCommandString)
        flob_pf.close()

        os.system("%s -n %s > /dev/null" % (paupPath, pFName))

        # This is the result.  The tree, if it exists, is read in by p4.
        oldLen = len(var.trees)
        func.read(treeFName)
        newLen = len(var.trees)
        if newLen == oldLen + 1:
            pass
        else:
            gm.append("I was expecting exactly one tree.  Got %i" % (oldLen - newLen))
            raise Glitch, gm
        t = var.trees.pop()

        # Tidy up.
        os.remove(treeFName)
        os.remove(pFName)
        os.remove(dmFName)

        for n in t.iterNodesNoRoot():
            if n.br.len < 0.0:
                n.br.len = 0.0

        return t
Exemplo n.º 4
0
    def njUsingPaup(self, paupPath='paup'):
        """Use paup to make a neighbor-joining tree, which is returned.

        The resulting tree is read in by p4, and is returned.

        We interact with paup by writing files, but care is taken that
        existing files are not overwritten, because new file names are
        made to be unique. 

        If this does not work well, try setting the paupPath arg.
        """

        gm = ["DistanceMatrix.njUsingPaup()"]

        #filename    = sha.new(str(os.getpid())).hexdigest()[-10:]
        #dmFName     = os.path.join(pathPrefix, "%s.dmat" % filename)
        #treeFName   = os.path.join(pathPrefix, "%s.tree" % filename)
        #pFName      = os.path.join(pathPrefix, "%s.cmds" % filename)

        #tempfile.mkstemp(suffix='', prefix='tmp', dir=None, text=False)
        #if pathPrefix:
        #    theDir = pathPrefix
        #else:
        #    theDir = None
        flob_dm, dmFName_fq = func.uniqueFile('tmp.dm')
        flob_tf, treeFName_fq = func.uniqueFile(
            'tmp.tree')  #tempfile.mkstemp(suffix='tree', dir=theDir)
        flob_tf.close()
        flob_pf, pFName = func.uniqueFile(
            'tmp.cmds')  # tempfile.mkstemp(suffix='cmds', dir=theDir)

        # Throw the dir and dirname away.
        dirname, dmFName = os.path.split(dmFName_fq)
        dirname, treeFName = os.path.split(treeFName_fq)

        # Make the paup commands
        paupCommandString = """#nexus
        begin paup;
          execute %s;
          set crit=dist;
          dset negbrlen=setzero;
          nj;
          savetrees file=%s format=altnex brlens=yes taxablk=yes replace=yes;
          quit;
        end;
        
        """ % (dmFName, treeFName)

        #print paupCommandString

        # Write the files, do the analysis
        #writeNexusToOpenFile(self, flob, writeTaxaBlock, append, digitsAfterDecimal)
        self.writeNexusToOpenFile(flob_dm, True, False, 6)
        flob_dm.close()
        flob_pf.write(paupCommandString)
        flob_pf.close()

        os.system("%s -n %s > /dev/null" % (paupPath, pFName))

        # This is the result.  The tree, if it exists, is read in by p4.
        oldLen = len(var.trees)
        func.read(treeFName)
        newLen = len(var.trees)
        if newLen == oldLen + 1:
            pass
        else:
            gm.append("I was expecting exactly one tree.  Got %i" %
                      (oldLen - newLen))
            raise Glitch, gm
        t = var.trees.pop()

        # Tidy up.
        os.remove(treeFName)
        os.remove(pFName)
        os.remove(dmFName)

        for n in t.iterNodesNoRoot():
            if n.br.len < 0.0:
                n.br.len = 0.0

        return t