Ejemplo n.º 1
0
def svg(self, fName=None, width=500, putInternalNodeNamesOnBranches=0):
    """Make a basic svg drawing of self.

    The 'width' is in postscript points (??). -- does that work with svg?
    
    By default, internal node names label the node, where the node
    name goes on the right of the node.  You can make the node name
    label the branch by setting 'putInternalNodeNamesOnBranches'.

    """

    gm = ["Tree.svg()"]

    if not self.preAndPostOrderAreValid:
        self.setPreAndPostOrder()

    from TreePicture import TreePicture

    p = TreePicture(self)
    p.addToBrLen = 0.0
    p.width = width
    p.yScale = 17.0
    p.xScale = None
    p.pointsPerLetter = 8.0  # was 6.0
    p.textSize = 11
    p.labelTextSize = 8
    p.putInternalNodeNamesOnBranches = putInternalNodeNamesOnBranches
    p.xOrigin = 5.0
    p.setPos()
    p.svg = True
    s = p.vectorString()

    if not fName:
        if self.name:
            fName = "%s.svg" % self.name
        else:
            fName = "%i.svg" % os.getpid()
    # if not fName.endswith('.svg'):
    #    fName = '%s.svg' % fName
    f = file(fName, "w")
    f.write(s)
    f.close()
Ejemplo n.º 2
0
def svg(self, fName=None, width=500, putInternalNodeNamesOnBranches=0):
    """Make a basic svg drawing of self.

    The 'width' is in postscript points (??). -- does that work with svg?
    
    By default, internal node names label the node, where the node
    name goes on the right of the node.  You can make the node name
    label the branch by setting 'putInternalNodeNamesOnBranches'.

    """

    gm = ['Tree.svg()']

    if not self.preAndPostOrderAreValid:
        self.setPreAndPostOrder()

    from TreePicture import TreePicture
    p = TreePicture(self)
    p.addToBrLen = 0.0
    p.width = width
    p.yScale = 17.0
    p.xScale = None
    p.pointsPerLetter = 8.0 # was 6.0
    p.textSize=11
    p.labelTextSize=8
    p.putInternalNodeNamesOnBranches = putInternalNodeNamesOnBranches
    p.xOrigin = 5.0
    p.setPos()
    p.svg = True
    s = p.vectorString()

    if not fName:
        if self.name:
            fName = '%s.svg' % self.name
        else:
            fName = '%i.svg' % os.getpid()
    #if not fName.endswith('.svg'):
    #    fName = '%s.svg' % fName
    f = file(fName, 'w')
    f.write(s)
    f.close()
Ejemplo n.º 3
0
def textDrawList(
    self,
    showInternalNodeNames=1,
    addToBrLen=0.2,
    width=None,
    autoIncreaseWidth=True,
    showNodeNums=1,
    partNum=0,
    model=False,
):

    if len(self.nodes) == 0:
        return [""]
    elif len(self.nodes) == 1:
        if showNodeNums:
            return ["%i:%s" % (self.nodes[0].nodeNum, self.nodes[0].name)]
        else:
            return ["%s" % self.nodes[0].name]
    gm = ["Tree.textDrawList()"]
    if not self.preAndPostOrderAreValid:
        self.setPreAndPostOrder()

    from TreePicture import TreePicture

    p = TreePicture(self)
    p.fName = None
    p.width = width
    p.xScale = None
    p.yScale = 1
    p.pointsPerLetter = 1
    p.addToBrLen = addToBrLen
    p.textShowNodeNums = showNodeNums
    p.showInternalNodeNames = showInternalNodeNames
    if showNodeNums:
        p.nameOffset = 0
    else:
        p.nameOffset = 1
    p.xOrigin = 0.0
    p.yOrigin = 0.0
    p.textSize = 1
    p.labelTextSize = 1

    # If the width is not specified, make a guess
    if width == None:
        tLen = 0  # Longest number of horizontal sections to draw
        longestNameLen = 0
        for n in self.nodes:
            if n.isLeaf and n != self.root:
                if n.name:  # This assumes short internal node name lengths
                    if len(n.name) > longestNameLen:
                        longestNameLen = len(n.name)
                thisLen = 0
                n1 = n
                # print "x n1 is node %i" % n1.nodeNum
                while n1 != self.root:
                    n1 = n1.parent
                    # print "y n1 is node %i" % n1.nodeNum
                    thisLen += 1
                if thisLen > tLen:
                    tLen = thisLen
        # print "tLen =", tLen
        # print "longestNameLen =", longestNameLen
        rootNameLen = 0
        if self.root.name:
            rootNameLen = len(self.root.name) + 1
        p.width = (10 * tLen) + rootNameLen + longestNameLen
        if p.width > 100:
            p.width = 100

    # print "p.width =", p.width
    # import sys; sys.exit()

    # Make sure the names fit.
    if not autoIncreaseWidth:
        for n in self.nodes:
            if n.isLeaf and n != self.root:
                if n.name and len(n.name) > p.width:
                    gm.append("There are long names, and the given width is not enough.")
                    raise Glitch, gm

    if model:
        try:
            partNum = int(partNum)
        except:
            gm.append("partNum arg should be an integer.")
            raise Glitch, gm
        if not self.model:
            gm.append("If model arg is set, then self.model must exist.")
            raise Glitch, gm
        if partNum < 0 or partNum >= self.model.nParts:
            gm.append("Zero-based partNum %i is out of range of %s parts." % (partNum, self.model.nParts))
            raise Glitch, gm
        p.partNum = partNum
        p.doModel = 1

        doComps = 1
        doRMatrices = 1
        if self.model.parts[partNum].nComps < 2:
            doComps = 0
        if self.model.parts[partNum].nRMatrices < 2:
            doRMatrices = 0

        if not doComps and not doRMatrices:
            p.setPos(autoIncreaseWidth)
            s = p.textString(returnAsList=True)
            s.append("Both the composition of the model and the rate matrix are homogeneous in part %i.\n" % partNum)
            return s

        # First do compositions
        s = []
        if doComps:
            if 0:
                if self.model.nParts > 1:
                    print "Compositions for part %i" % partNum
                else:
                    print "\nCompositions\n------------"
            p.textDrawModelThing = var.TEXTDRAW_COMP
            p.setPos(autoIncreaseWidth)
            s = p.textString(returnAsList=True)
            # print s

        # Then do RMatrices
        if doRMatrices:
            if 0:
                if self.model.nParts > 1:
                    print "RMatrices for part %i" % partNum
                else:
                    print "\nRMatrices\n---------"
            p.textDrawModelThing = var.TEXTDRAW_RMATRIX
            p.setPos(autoIncreaseWidth)
            if s:
                s += p.textString(returnAsList=True)
            else:
                s = p.textString(returnAsList=True)
            if self.model.parts[partNum].nComps == 1:
                s.append("The composition of the model is homogeneous in part %i\n" % partNum)
        elif self.model.parts[partNum].nRMatrices == 1:
            s.append("The rate matrix is homogeneous in part %i\n" % partNum)
        # Don't bother with GDASRV, yet
        return s

    else:
        p.setPos(autoIncreaseWidth)
        s = p.textString(returnAsList=True)
        return s
Ejemplo n.º 4
0
def textDrawList(self, showInternalNodeNames=1, addToBrLen=0.2, width=None, autoIncreaseWidth=True, showNodeNums=1, partNum=0, model=False):
    
    if len(self.nodes) == 0:
        return  ['']
    elif len(self.nodes) == 1:
        if showNodeNums:
            return ['%i:%s' % (self.nodes[0].nodeNum, self.nodes[0].name)]
        else:
            return ['%s' % self.nodes[0].name]
    gm = ['Tree.textDrawList()']
    if not self.preAndPostOrderAreValid:
        self.setPreAndPostOrder()

    from TreePicture import TreePicture
    p = TreePicture(self)
    p.fName = None
    p.width = width
    p.xScale = None
    p.yScale = 1
    p.pointsPerLetter = 1
    p.addToBrLen = addToBrLen
    p.textShowNodeNums = showNodeNums
    p.showInternalNodeNames = showInternalNodeNames
    if showNodeNums:
        p.nameOffset = 0
    else:
        p.nameOffset = 1
    p.xOrigin = 0.0
    p.yOrigin = 0.0
    p.textSize = 1
    p.labelTextSize = 1

    # If the width is not specified, make a guess
    if width == None:
        tLen = 0 # Longest number of horizontal sections to draw
        longestNameLen = 0
        for n in self.nodes:
            if n.isLeaf and n != self.root:
                if n.name: # This assumes short internal node name lengths
                    if len(n.name) > longestNameLen:
                        longestNameLen = len(n.name)
                thisLen = 0
                n1 = n
                #print "x n1 is node %i" % n1.nodeNum
                while n1 != self.root:
                    n1 = n1.parent
                    #print "y n1 is node %i" % n1.nodeNum
                    thisLen += 1
                if thisLen > tLen:
                    tLen = thisLen
        #print "tLen =", tLen
        #print "longestNameLen =", longestNameLen
        rootNameLen = 0
        if self.root.name:
            rootNameLen = len(self.root.name) + 1
        p.width = (10 * tLen) + rootNameLen + longestNameLen
        if p.width > 100:
            p.width = 100

    #print "p.width =", p.width
    #import sys; sys.exit()
    
    # Make sure the names fit.
    if not autoIncreaseWidth:
        for n in self.nodes:
            if n.isLeaf and n != self.root:
                if n.name and len(n.name) > p.width:
                    gm.append("There are long names, and the given width is not enough.")
                    raise Glitch, gm


    if model:
        try:
            partNum = int(partNum)
        except:
            gm.append("partNum arg should be an integer.")
            raise Glitch, gm
        if not self.model:
            gm.append("If model arg is set, then self.model must exist.")
            raise Glitch, gm
        if partNum < 0 or partNum >= self.model.nParts:
            gm.append("Zero-based partNum %i is out of range of %s parts." % (partNum, self.model.nParts))
            raise Glitch, gm
        p.partNum = partNum
        p.doModel = 1

        doComps = 1
        doRMatrices = 1
        if self.model.parts[partNum].nComps < 2:
            doComps = 0
        if self.model.parts[partNum].nRMatrices < 2:
            doRMatrices = 0

        if not doComps and not doRMatrices:
            p.setPos(autoIncreaseWidth)
            s = p.textString(returnAsList=True)
            s.append("Both the composition of the model and the rate matrix are homogeneous in part %i.\n" % partNum)
            return s
        
        # First do compositions
        s = []
        if doComps:
            if 0:
                if self.model.nParts > 1:
                    print "Compositions for part %i" % partNum
                else:
                    print "\nCompositions\n------------"
            p.textDrawModelThing = var.TEXTDRAW_COMP
            p.setPos(autoIncreaseWidth)
            s = p.textString(returnAsList=True)
            #print s

        # Then do RMatrices
        if doRMatrices:
            if 0:
                if self.model.nParts > 1:
                    print "RMatrices for part %i" % partNum
                else:
                    print "\nRMatrices\n---------"
            p.textDrawModelThing = var.TEXTDRAW_RMATRIX
            p.setPos(autoIncreaseWidth)
            if s:
                s += p.textString(returnAsList=True)
            else:
                s = p.textString(returnAsList=True)
            if self.model.parts[partNum].nComps == 1:
                s.append("The composition of the model is homogeneous in part %i\n" % partNum)
        elif self.model.parts[partNum].nRMatrices == 1:
            s.append("The rate matrix is homogeneous in part %i\n" % partNum)
        # Don't bother with GDASRV, yet
        return s

    else:
        p.setPos(autoIncreaseWidth)
        s = p.textString(returnAsList=True)
        return s