views:

67

answers:

2

Hi, I had this class and subclass :

class Range:

def __init__(self, start, end):
    self.setStart(start)
    self.setEnd(end)
def getStart(self):
    return self.start

def setStart(self, s):
    self.start = s
def getEnd(self):
    return self.end
def setEnd(self, e):
    self.end = e
def getLength(self):
    return len(range(self.start, self.end))
def overlaps(self, r):
    if (r.getStart() < self.getEnd() and r.getEnd() >= self.getEnd()) or \
       (self.getStart() < r.getEnd() and self.getEnd() >= r.getEnd()) or \
       (self.getStart() >= r.getStart() and self.getEnd() <= r.getEnd()) or \
       (r.getStart() >= self.getStart() and r.getEnd() <= self.getEnd()):
        return True
    else:
        return False

class DNAFeature(Range):

   def __init__(self, start, end):
            self.setStart(start)
            self.setEnd(end)
            self.strand = none
            self.sequencename = none
   def getSeqName(self, s):
            return self.SeqName
   def setSeqName(self, s):
            self.sequencename = s
   def getStrand(self):
            if self.SeqName == 'plus':
                    return 1
            elif self.SeqName == 'minus':
                    return -1
            else:
                    return 0
   def setStrand(self, s):
            self.strand = s

And here is what I have to do: Create
 a 
new 
class
– 
GeneModel
‐
 that 
contains 
a 
group
 of 
DNAFeature 
objects
 representing
 exons
 and
 is 
a 
child 
class 
of 
DNAFeature. 
It
 should 
implement 
the
 following 
methods:

 
 getFeats() 
–
returns 
a 
list
 of
 DNAFeature 
objects,
sorted
 by
 start 
position
 addFeat(feat)
–
 accepts 
a 
DNAFeature 
feat 
and
 adds 
it 
to 
its 
internal 
group
 of
 DNAFeature 
objects
 setTranslStart(i)
– 
accepts 
a 
non‐negative 
int,
sets 
the
 start
 position 
of
 the
 initiating 
ATG
 codon
 getTranslStart()
–
returns 
an 
int, 
the
 start 
position 
of 
the
 initiating 
ATG
 codon
 setTranslStop(i)
– 
accepts
 a 
positive 
int,
sets
 the
 end
 position 
for 
the
 stop
 codon
 getTranslStop()
–
 returns
 an 
int,
the
 end 
position 
for
 the
 stop 
codon
 setDisplayId(s) 
–
sets 
the
 name
 of
 the
 gene
 model; 
s 
is 
a 
string
 getDisplayId()
– 
return
 the
 name 
of 
the 
gene
 model,
 returns 
a 
string,
e.g.,
AT1G10555.1
 
 GeneModel
 should 
raise
 appropriate 
ValueError 
and 
TypeError 
exceptions 
when
 users
 pass
 incorrect 
types 
and
 values 
to 
constructors 
and 
“set” 
methods.


I have tried to write whatever comes to my mind, and read the books as well as searching the way to put codes together, but I am so new to programming and hardly can understand how to write the codes correctly. To be honest, this is the first time I ever do a programming class. So if I make any funny mistake in my codes, please forgive me. I haven't finish my codes yet and still reading the books to see where I am doing wrong and right with my codes. However, I really need your help to guide me to the right path. Thank you guys very much. Below is my codes:

class GeneModel(DNAFeature):

   def __init__(self, translstart, translend, displayid):
            self.setTranslStart(translstart)
            self.setTranslStop(translend)
            setDisplayId(displayid)
   def getFeats():
            result = []
            sort.self.getStart()
            return result
   def addFeat(feat):
            self.addFeat = feat
            return self.getStart+self.getEnd
   def setTranslStart(i):
            self.translstart = self.setStart
            self.translstart = non-negative int
   def getTranslStart():
            return self.translstart
   def setTranslStop(i):
            self.translend = self.setEnd
            self.translend = "+" int
   def getTranslStop():
            return self.translend
   def setDisplayId(s):
            self.displayid = re.compile('r'\AT1G[0-9]{5,5}\.[0-9]{,1}, IGNORECASE')
   def getDisplayId():
            return self.displayid
+1  A: 

First, a little bit of cleanup. I'm not completely convinced that your original class, DNAFeature, is actually correct. DNAFeature seems to be inheriting from some other class, named Range, that we're missing here so if you have that code please offer it as well. In that original class, you need to define the variable SeqName (also, its preferable to keep variables lower-cased) since otherwise self.SeqName will be meaningless. Additionally, unless they're inherited from the Range class, you should also define the methods "setStart" and "setEnd". You're getter should not any additional variables, so feel free to change it to "def getSeqName(self)" instead of adding "s". I'm not sure what else your code is really supposed to do, so I'll hold any further comment.

Additionally, though you stated otherwise in your comment, I have to believe from the naming conventions (and what little I remember from bio) that you actually want GeneModel to be a container for a set of DNAFeature instances. That's different from GeneModel subclassing DNAFeature. If I'm right, then you can try:

class GeneModel(object):

    def __init__(dnafeatures):
        self.dnafeatures = dnafeatures

    def get_features(self):
        return self.dnafeatures

    def add_feature(self, feature):
        self.dnafeatures.append(feature)

Here dnafeatures would just be a list of dnafeature instances. This would then allow you to write methods to access these features and do whatever fun stuff you need to do.

My advice would be to make sure your DNAFeature class is correct and that your model of how you want your problem solved (in terms of what your classes do) and try asking again when its a little clearer. Hope this helps!

jlv
I have update my original class Range, for some reason not sure why its doesn't look perfectly between the class Range line and the next line. I am not sure if the subclass DNAFeature is correct as I dont have any test file to test it. But hope you can help me to figure out with the original class. Thank you so much
pmt0512
Thanks for updating when your Range class. It still looks like you need to add SeqName to DNAFeatures. Otherwise, I believe that you can still take my skeleton GeneModel class and add methods as you see fit. As you learn python, you'll figure out the more pythonic ways of doing things (like doing (self.end-self.start) instead of len(range(self.start, self.end)) but hopefully that'll start you off.
jlv
I see, I'll tried it, thanks jlv a lot, your guide help me a lot. Thanks again
pmt0512
+1  A: 

I don't understand what the name of the gene model is. I think it's subject specific, but I think this will work for you:

class GenoModel(DNAFeature):

    def __init__(self, start, end):
        self.setStart(start)
        self.setEnd(end)
        self.strand = None
        self.sequencename = None
        self.exons = []
        self.translStart = None
        self.translStop = None
        self.displayId = None

    def getFeats(self):
        self.exons.sort(cmp=self.start)
        return self.exons

    def addFeat(self, f):

        if type(f) == DNAFeature:
            self.exons.append(f)
        else:
            raise TypeError("Cannot add feature as it is not of type DNAFeature")

    def setTranslStart(self, i):

        if type(i) != int:
            raise TypeError("Cannot set translStart as it is not of type int")
        elif i < 0:
            raise ValueError("Cannot set tanslStart to a negative int")
        else:
            self.translStart = i

    def getTranslStart(self):
        return self.translStart

    def setTranslStop(self, i):

        if type(i) != int:
            raise TypeError("Cannot set translStop as it is not of type int")
        elif i <= 0:
            raise ValueError("Cannot set tanslStop to anything less than 1")
        else:
            self.translStop = i

    def getTranslStop(self):
        return self.translStop

    def setDisplayId(self, s):

        if type(s) != str:
            raise TypeError("Cannot set desiplayId as it is not of type string")
        else:
            self.displayId = s

    def getDisplayId(self):
        return self.displayId

Hope this helps.

inspectorG4dget
Is it because GeneModel is subclass of DNAFeature which was a subclass of Range, that in the def __init__ it will have to be the 3 argument that we define in Range which is self, start and end? Is that right? I just curious, thanks inspector
pmt0512
Yes, but more importantly, if you didn't define an init here, GeneModel by default will take DNAFeature's init.
inspectorG4dget
I see, one more question inspector, what is it mean by using != for ex: if type(s) !=str: or type(i) !=int: ? Thanks
pmt0512
I'm sure that you are aware that "==" tests for equality. "!=" is the opposite of that. It tests for inequality. It's like saying "not ==". Does that make sense?
inspectorG4dget