| 1 |
from pyfastaq import sequences, utils |
| 2 |
|
| 3 |
class Error (Exception): pass |
| 4 |
|
| 5 |
def file_reader(fname): |
| 6 |
f = utils.open_file_read(fname) |
| 7 |
c = Caf() |
| 8 |
|
| 9 |
while c.get_next_from_file(f): |
| 10 |
yield c |
| 11 |
|
| 12 |
utils.close(f) |
| 13 |
|
| 14 |
|
| 15 |
class Caf: |
| 16 |
def __init__(self): |
| 17 |
self.id = None |
| 18 |
self.seq = None |
| 19 |
self.insert_min = None |
| 20 |
self.insert_max = None |
| 21 |
self.ligation = None |
| 22 |
self.clone = None |
| 23 |
self.clip_start = None |
| 24 |
self.clip_end = None |
| 25 |
|
| 26 |
|
| 27 |
def __eq__(self, other): |
| 28 |
if type(other) is type(self): |
| 29 |
return self.__dict__ == other.__dict__ |
| 30 |
return False |
| 31 |
|
| 32 |
|
| 33 |
def get_next_from_file(self, f): |
| 34 |
self.__init__() |
| 35 |
line = f.readline() |
| 36 |
if not line: |
| 37 |
return None |
| 38 |
while line == '\n': |
| 39 |
line = f.readline() |
| 40 |
|
| 41 |
if not line.startswith('DNA : '): |
| 42 |
raise Error("Error reading caf file. Expected line starting with 'DNA : ...'") |
| 43 |
|
| 44 |
self.id = line.rstrip().split()[2] |
| 45 |
|
| 46 |
line = f.readline() |
| 47 |
seq = [] |
| 48 |
|
| 49 |
while line != '\n': |
| 50 |
seq.append(line.rstrip()) |
| 51 |
line = f.readline() |
| 52 |
|
| 53 |
self.seq = sequences.Fasta(self.id, ''.join(seq)) |
| 54 |
|
| 55 |
line = f.readline() |
| 56 |
if not line.startswith('BaseQuality : '): |
| 57 |
raise Error("Error reading caf file. Expected line starting with 'BaseQuality : ...'") |
| 58 |
|
| 59 |
quals = [int(x) for x in f.readline().rstrip().split()] |
| 60 |
self.seq = self.seq.to_Fastq(quals) |
| 61 |
|
| 62 |
line = f.readline() |
| 63 |
assert line == '\n' |
| 64 |
line = f.readline() |
| 65 |
|
| 66 |
while line not in ['', '\n']: |
| 67 |
a = line.rstrip().split() |
| 68 |
if a[0] == 'Insert_size': |
| 69 |
self.insert_min, self.insert_max = int(a[1]), int(a[2]) |
| 70 |
elif a[0] == 'Ligation_no': |
| 71 |
self.ligation = a[1] |
| 72 |
elif a[0] == 'Clone': |
| 73 |
self.clone = a[1] |
| 74 |
elif a[0] == 'Clipping' and a[1] == 'QUAL': |
| 75 |
self.clip_start, self.clip_end = int(a[2]) - 1, int(a[3]) - 1 |
| 76 |
|
| 77 |
line = f.readline() |
| 78 |
|
| 79 |
return True |