Source code for tools.parsers.FastaTools

import os

[docs]class FastaFile(object) : """ Represents a whole Fasta file:: #reading f = FastaFile() f.parseFile('hop.fasta') for line in f : print line #writing f = FastaFile() f.add(">prot1", "MLPADEV")'myFasta.fasta') """ def __init__(self, fil = None) : self.reset() if fil != None : self.parseFile(fil)
[docs] def reset(self) : """Erases everything""" = [] self.currentPos = 0
[docs] def parseStr(self, st) : """Parses a string""" = st.split('>')[1:]
[docs] def parseFile(self, fil) : """Opens a file and parses it""" f = open(fil) self.parseStr( f.close()
def __splitLine(self, li) : if len([li]) != 2 :[li] =[li].replace('\r', '\n')[li] =[li].replace('\n\n', '\n') l =[li].split('\n') header = '>'+l[0] data = ''.join(l[1:])[li] = (header, data)
[docs] def get(self, i) : """returns the ith entry""" self.__splitLine(i) return[i]
[docs] def add(self, header, data) : """appends a new entry to the file""" if header[0] != '>' :'>'+header, data)) else :, data))
[docs] def save(self, filePath) : """saves the file into filePath""" f = open(filePath, 'w') f.write(self.make()) f.close()
[docs] def toStr(self) : """returns a string version of self""" st = "" for d in : st += "%s\n%s\n" % (d[0], d[1]) return st
def __iter__(self) : self.currentPos = 0 return self def next(self) : #self to call getitem, and split he line if necessary return self[self.currentPos] def __getitem__(self, i) : """returns the ith entry""" return self.get(i) def __setitem__(self, i, v) : """sets the value of the ith entry""" if len(v) != 2: raise TypeError("v must have a len of 2 : (header, data)")[i] = v def __len__(self) : """returns the number of entries""" return len(