views:

131

answers:

3

Hi, I need to create a Bed module with these functions:

readBed(file)
–
read 
a 
BED 
format 
file 
and
 constructs 
a 
list 
of 
gene 
model 
objects 
from 
the
 data
 it 
contains.



writeBed(models=models,fname=file)
 – 
writes 
the 
given 
list
 of
 gene 
model 
objects
 and 
 writes
 them 
to 
a
 file 
named 
fname.

For the readBed, I was thinking about readline function that I have wrote before, and add the codes for it to return a result as a list. For writeBed, I really am clueless. Here is my codes, please guide me everyone:

def ReadBed(file):
  result = []
  line = fh.readline()
  if not line:
      fh.close()
  else:
      return result

def writeBed(models=models, fname=file):
  if file.ReadBed = result
  return result in fname

Also, I had a Range class like this, and I want to raise TypeError and ValueError for my class, but not sure how to do it, can everyone please help me too. Thank you so much everyone:

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
+1  A: 

I'll start with the Range class. Firstly, you shouldn't use get/set methods, instead just use the variable. Get/set methods in python are almost always bad practice. Even if you need validation, you can use properties.

If you're using python 2.x, you need to inherit from object to get new-style classes. If you're using py3k, you don't need to declare it.

Method and function names in python should be like_this rather than likeThis (by convention).

Doing something like if bool: return True else: return False can always be simplified to just return bool, so that makes your overlap method a lot simpler. If you think about the logic of it a little bit too, your comparison becomes a lot easier: for two ranges to overlap, one must start before the other ends, and must also end after the other one starts.

For your BED functions, have you tried running them? What happened? Make sure to look at what variables you are using in your functions and where you define them. You should also have a look at the with statement, which is commonly used when opening files. It provides hooks for setup and tear-down, and filehandles are made so that they .close() on tear-down. Try using that and it should also make the logic a little more clear.

class Range(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end

    def __len__(self):
        """This allows you to do len(Range object)."""
        return self.end - self.start + 1

    def overlaps(self, other):
        if self.start < other.end:
            return self.end > other.start
        if other.start < self.end:
            return other.end > self.start
Daenyth
Hi, actually the getters and setters was what my professor want me to included in our assignment and that many experts have told me about shouldnt put that in python, but I have no idea why my prof. wants to. Anyways, even with the getters and setters like that, can we still raise the error exception? Thanks Daenyth.
pmt0512
@pmt0512: Your professor does not have experience with python in the real world then. Look into using the `@property` decorator for the start and end bits, you can raise an exception with that however you'd like
Daenyth
I think so too, she mostly using different language on the purpose of biology research as my major is bioinformatics, so I would say she doesnt have much experience compare to most of programming expert....
pmt0512
Also, I remember @omt0512 mentioning (in one of his earlier questions on the same assignment) that this assignment is the same as another that had been assigned in the previous years, which was in Java. The prof just changed "Java" to "python", which I find ridiculous
inspectorG4dget
+1  A: 

Without knowing the format in which the data is saved to file, I can only assume that it is being pickled. With that assumption in mind, I can give you the following code:

import cPickle

def readBed(filepath):
    with open(filepath, 'r') as f:
        data = cPickle.load(f)
        return data

def writeBed(models, filepath):
    with open(filepath, 'w') as f:
        cPickle.dump(models, f)
inspectorG4dget
I see, can you help me with the other part, to raise the Error too inspector? Thanks
pmt0512
I can certainly raise exceptions in Range, but I just wrote them my solution to DNAFeature. You should be able to read the code I wrote in DNAFeature and write similar code for Range. Make an attempt and I'll help you through it. This is homework and I don't want to just do it for you - if I do, you won't learn and you'll be in trouble at exam time.
inspectorG4dget
yeah, actually i was working on it with the codes in DNAFeature, I will post it after I'm done with the codes though. Thanks inspector
pmt0512
hey inspector, I got this detail about the BED format from my prof, so can you take a look and see if we still need to import cPickle? Thanks inspector
pmt0512
http://genome.ucsc.edu/FAQ/FAQformat.html#format1
pmt0512
Dude, I read the literature, but the last time I took any biology was about 4 years ago (and even that was pretty basic). I don't really understand the document too well, but I can see that pickle is not the way to go. Your prof wants you to write lines to the file, but it's still pretty ambiguous to me and I can't make much of it unless you explain it to me.
inspectorG4dget
Also, you can edit your comments by clicking on the "edit" link at the end of your comment
inspectorG4dget
I just got the file by last night so I have to ask my prof what she want me to extract exactly first, sorry for the confusion, I will let you know right after I get what my prof. want. Thanks inspector
pmt0512
+1  A: 

I am having the same issue with trying to write this bed.py file. Did you guys ever figure it out?

helpme
hey helpme, I'm kind of just figure out part of the readBed commands, not sure yet for the writeBed....
pmt0512
This project is ridiculous!
helpme
I agree, took me days to know what I need to do w/out any clear explanation >.<
pmt0512