开发者

Python Bed module

开发者 https://www.devze.com 2023-01-20 03:39 出处:网络
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.

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.g开发者_JS百科etEnd() >= 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


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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消