1 """
2 Parser for PHD files output by PHRED and used by PHRAP and
3 CONSED.
4
5 Works fine with PHRED 0.020425.c
6
7 Version 1.1, 03/09/2004
8 written by Cymon J. Cox (cymon@duke.edu) and Frank Kauff (fkauff@duke.edu)
9 Comments, bugs, problems, suggestions to one uf us are welcome!
10
11 Uses the Biopython Parser interface for parsing: ParserSupport.py
12
13 """
14
15 import os
16 from types import *
17
18 from Bio import File
19 from Bio import Index
20 from Bio import Seq
21 from Bio import SeqRecord
22 from Bio.ParserSupport import *
23 from Bio.Alphabet import IUPAC
24
25 CKEYWORDS=['CHROMAT_FILE','ABI_THUMBPRINT','PHRED_VERSION','CALL_METHOD',\
26 'QUALITY_LEVELS','TIME','TRACE_ARRAY_MIN_INDEX','TRACE_ARRAY_MAX_INDEX',\
27 'TRIM','TRACE_PEAK_AREA_RATIO','CHEM','DYE']
28
30 """Hold information from a PHD file
31
32 """
34 self.file_name = ''
35 self.comments={}
36 for kw in CKEYWORDS:
37 self.comments[kw.lower()]=None
38 self.sites = []
39 self.seq = ''
40 self.seq_trimmed = ''
41
42
44 """Iterates over a file of multiple PHD records
45
46 Methods:
47 next Return the next record from the stream, or None.
48 """
49
50 - def __init__(self, handle, parser=None):
51 """__init__(self, handle, parser=None)
52
53 Create a new iterator. handle is a file-like object. parser
54 is an optional Parser object to change the results into another form.
55 If set to None, then the raw contents of the file will be returned.
56 """
57
58 if type(handle) is not FileType and type(handle) is not InstanceType:
59 raise ValueError, "I expected a file handle or file-like object"
60 self._uhandle = File.UndoHandle(handle)
61 self._parser = parser
62
64 """next(self) -> object
65
66 Return the next PHD record from the file. If no more records
67 return None.
68 """
69
70 lines = []
71 while 1:
72 line = self._uhandle.readline()
73 if not line:
74 break
75
76 if lines and line[:14] == 'BEGIN_SEQUENCE':
77 self._uhandle.saveline(line)
78 break
79 lines.append(line)
80
81 if not lines:
82 return None
83
84 data = ''.join(lines)
85 if self._parser is not None:
86 return self._parser.parse(File.StringHandle(data))
87 return data
88
90 return iter(self.next, None)
91
93 """Parses PHD file data into a Record object
94
95 """
99
100 - def parse(self, handle):
107
108
110 """Scans a PHD-formatted file
111
112 Methods:
113 feed - Feed one PHD record.
114 """
115 - def feed(self, handle, consumer):
116 """feed(self, handle, consumer)
117
118 Feed in PHD data for scanning. handle is a file-like object
119 containing PHD data. consumer is a Consumer object that will
120 receive events as the PHD data is scanned.
121 """
122 assert isinstance(handle, File.UndoHandle), \
123 "handle must be an UndoHandle"
124 if handle.peekline():
125 self._scan_record(handle, consumer)
126
132
135
151
153 while 1:
154 line = uhandle.readline()
155 if is_blank_line(line) or line == 'BEGIN_DNA\n':
156 continue
157 elif line == 'END_DNA\n':
158 break
159 consumer.read_dna(line)
160
161
163 """Consumer that converts a PHD record to a Record object
164
165 """
168
172
178
181
184
187
190
193
194 - def time(self, line):
196
199
202
203 - def trim(self, line):
204 first, last, prob = line[5:-1].split()
205 self.data.comments['trim'] = (int(first), int(last), float(prob))
206
209
210 - def chem(self, line):
212
213 - def dye(self, line):
215
219