class Gene(object):
    def __init__(self, FeatureObject):
        """ initializes chromosome, strand, and Ids from FeatureObject so we don't have to call these every time"""
        self.features=[]
        self.exons = RangeFinder()
        self.chr = FeatureObject.chr
        self.strand = FeatureObject.strand
        self.tId = FeatureObject.tId
        self.gId = FeatureObject.gId

        self.features.append(FeatureObject)
    def add(self, FeatureObject):
        """adds FeatureObject to Gene object"""
        self.features.append(FeatureObject)
    def makeExons(self):
        """ take features list and create a list of exonstarts and stops. This means merging utr and exon features"""
        self.exonStarts = []
        self.exonEnds= []
        keepStart =''
        keepStop = 0
        for f in self.features:
            if (f.start - 1 > keepStop):         # if it is a new exon
                if(keepStop > 0):
                    self.exonStarts.append(keepStart)
                    self.exonEnds.append(keepStop)
                keepStart = f.start
                    
            keepStop = f.stop
        # now add the last
        self.exonStarts.append(keepStart)
        self.exonEnds.append(keepStop)
    def addExons(self):           # addExons can not be part of add, since the exons must first be created in makeExons under the Gene class
        """ add exons to Gene object """
        for i in xrange(len(self.exonStarts)):
            self.exons.add(self.chr, self.exonStarts[i], self.exonEnds[i], self, self.strand)
    def isIdentical(self, otherGene):
        """ this should only be used after getTranscripts, because it assumes chromosome and strand are identical """
        if not (len(self.features) == len (otherGene.features)):
            return False
        for f in xrange(len(self.features)):
            if not (self.features[f].descriptor == otherGene.features[f].descriptor and self.features[f].start == otherGene.features[f].start and self.features[f].stop == otherGene.features[f].stop):
                return False
        return True    # if the number of exons is the same and the features are all the same
    def justCDS(self):
        """ tests if the gene has UTR features. Returns False if it does"""
        for f in self.features:
            if (f.descriptor == "5UTR" or f.descriptor == "3UTR"):
                return False
        return True
    def cdsIdentical(self, otherGene):
        selfFeatures = []
        otherFeatures = []
        for f in self.features:
            if f.descriptor == "CDS":
                selfFeatures.append(f)
        for f in otherGene.features:
            if f.descriptor == "CDS":
                otherFeatures.append(f)
        # now compare
        if not (len(selfFeatures) == len (otherFeatures)):
            return False
        for f in xrange(len(selfFeatures)):
            if not (selfFeatures[f].descriptor == otherFeatures[f].descriptor and selfFeatures[f].start == otherFeatures[f].start and selfFeatures[f].stop == otherFeatures[f].stop):
                return False
        return True    # if the number of exons is the same and the features are all the same
예제 #2
0
class Gene(object):
    def __init__(self, FeatureObject):
        """ initializes chromosome, strand, and Ids from FeatureObject so we don't have to call these every time"""
        self.features = []
        self.exons = RangeFinder()
        self.chr = FeatureObject.chr
        self.strand = FeatureObject.strand
        self.tId = FeatureObject.tId
        self.gId = FeatureObject.gId

        self.features.append(FeatureObject)

    def add(self, FeatureObject):
        """adds FeatureObject to Gene object"""
        self.features.append(FeatureObject)

    def makeExons(self):
        """ take features list and create a list of exonstarts and stops. This means merging utr and exon features"""
        self.exonStarts = []
        self.exonEnds = []
        keepStart = ''
        keepStop = 0
        for f in self.features:
            if (f.start - 1 > keepStop):  # if it is a new exon
                if (keepStop > 0):
                    self.exonStarts.append(keepStart)
                    self.exonEnds.append(keepStop)
                keepStart = f.start

            keepStop = f.stop
        # now add the last
        self.exonStarts.append(keepStart)
        self.exonEnds.append(keepStop)

    def addExons(
        self
    ):  # addExons can not be part of add, since the exons must first be created in makeExons under the Gene class
        """ add exons to Gene object """
        for i in xrange(len(self.exonStarts)):
            self.exons.add(self.chr, self.exonStarts[i], self.exonEnds[i],
                           self, self.strand)

    def isIdentical(self, otherGene):
        """ this should only be used after getTranscripts, because it assumes chromosome and strand are identical """
        if not (len(self.features) == len(otherGene.features)):
            return False
        for f in xrange(len(self.features)):
            if not (self.features[f].descriptor
                    == otherGene.features[f].descriptor
                    and self.features[f].start == otherGene.features[f].start
                    and self.features[f].stop == otherGene.features[f].stop):
                return False
        return True  # if the number of exons is the same and the features are all the same

    def justCDS(self):
        """ tests if the gene has UTR features. Returns False if it does"""
        for f in self.features:
            if (f.descriptor == "5UTR" or f.descriptor == "3UTR"):
                return False
        return True

    def cdsIdentical(self, otherGene):
        selfFeatures = []
        otherFeatures = []
        for f in self.features:
            if f.descriptor == "CDS":
                selfFeatures.append(f)
        for f in otherGene.features:
            if f.descriptor == "CDS":
                otherFeatures.append(f)
        # now compare
        if not (len(selfFeatures) == len(otherFeatures)):
            return False
        for f in xrange(len(selfFeatures)):
            if not (selfFeatures[f].descriptor == otherFeatures[f].descriptor
                    and selfFeatures[f].start == otherFeatures[f].start
                    and selfFeatures[f].stop == otherFeatures[f].stop):
                return False
        return True  # if the number of exons is the same and the features are all the same