Package Bio :: Package Sequencing :: Module Phd
[hide private]
[frames] | no frames]

Source Code for Module Bio.Sequencing.Phd

  1  # Copyright 2004 by Cymon J. Cox and Frank Kauff.  All rights reserved. 
  2  # Copyright 2008 by Michiel de Hoon.  All rights reserved. 
  3  # Revisions copyright 2009 by Cymon J. Cox.  All rights reserved. 
  4  # Revisions copyright 2009 by Peter Cock.  All rights reserved. 
  5  # 
  6  # This code is part of the Biopython distribution and governed by its 
  7  # license.  Please see the LICENSE file that should have been included 
  8  # as part of this package. 
  9  """ 
 10  Parser for PHD files output by PHRED and used by PHRAP and CONSED. 
 11   
 12  This module can be used directly which will return Record objects 
 13  which should contain all the original data in the file. 
 14   
 15  Alternatively, using Bio.SeqIO with the "phd" format will call this module 
 16  internally.  This will give SeqRecord objects for each contig sequence. 
 17  """ 
 18   
 19  from Bio import Seq 
 20  from Bio.Alphabet import generic_dna 
 21   
 22   
 23  CKEYWORDS = ['CHROMAT_FILE', 'ABI_THUMBPRINT', 'PHRED_VERSION', 'CALL_METHOD', 
 24          'QUALITY_LEVELS', 'TIME', 'TRACE_ARRAY_MIN_INDEX', 'TRACE_ARRAY_MAX_INDEX', 
 25          'TRIM', 'TRACE_PEAK_AREA_RATIO', 'CHEM', 'DYE'] 
 26   
 27   
28 -class Record(object):
29 """Hold information from a PHD file."""
30 - def __init__(self):
31 self.file_name = '' 32 self.comments = {} 33 for kw in CKEYWORDS: 34 self.comments[kw.lower()] = None 35 self.sites = [] 36 self.seq = '' 37 self.seq_trimmed = ''
38 39
40 -def read(handle):
41 """Reads the next PHD record from the file, returning it as a Record object. 42 43 This function reads PHD file data line by line from the handle, 44 and returns a single Record object. 45 """ 46 for line in handle: 47 if line.startswith("BEGIN_SEQUENCE"): 48 record = Record() 49 record.file_name = line[15:].rstrip() 50 break 51 else: 52 return # No record found 53 54 for line in handle: 55 if line.startswith("BEGIN_COMMENT"): 56 break 57 else: 58 raise ValueError("Failed to find BEGIN_COMMENT line") 59 60 for line in handle: 61 line = line.strip() 62 if not line: 63 continue 64 if line == "END_COMMENT": 65 break 66 keyword, value = line.split(":", 1) 67 keyword = keyword.lower() 68 value = value.strip() 69 if keyword in ('chromat_file', 70 'phred_version', 71 'call_method', 72 'chem', 73 'dye', 74 'time', 75 'basecaller_version', 76 'trace_processor_version'): 77 record.comments[keyword] = value 78 elif keyword in ('abi_thumbprint', 79 'quality_levels', 80 'trace_array_min_index', 81 'trace_array_max_index'): 82 record.comments[keyword] = int(value) 83 elif keyword == 'trace_peak_area_ratio': 84 record.comments[keyword] = float(value) 85 elif keyword == 'trim': 86 first, last, prob = value.split() 87 record.comments[keyword] = (int(first), int(last), float(prob)) 88 else: 89 raise ValueError("Failed to find END_COMMENT line") 90 91 for line in handle: 92 if line.startswith('BEGIN_DNA'): 93 break 94 else: 95 raise ValueError("Failed to find BEGIN_DNA line") 96 97 for line in handle: 98 if line.startswith('END_DNA'): 99 break 100 else: 101 # Line is: "site quality peak_location" 102 # Peak location is optional according to 103 # David Gordon (the Consed author) 104 parts = line.split() 105 if len(parts) in [2, 3]: 106 record.sites.append(tuple(parts)) 107 else: 108 raise ValueError("DNA line must contain a base and quality " 109 "score, and optionally a peak location.") 110 111 for line in handle: 112 if line.startswith("END_SEQUENCE"): 113 break 114 else: 115 raise ValueError("Failed to find END_SEQUENCE line") 116 117 record.seq = Seq.Seq(''.join(n[0] for n in record.sites), generic_dna) 118 if record.comments['trim'] is not None: 119 first, last = record.comments['trim'][:2] 120 record.seq_trimmed = record.seq[first:last] 121 122 return record
123 124
125 -def parse(handle):
126 """Iterates over a file returning multiple PHD records. 127 128 The data is read line by line from the handle. The handle can be a list 129 of lines, an open file, or similar; the only requirement is that we can 130 iterate over the handle to retrieve lines from it. 131 132 Typical usage:: 133 134 records = parse(handle) 135 for record in records: 136 # do something with the record object 137 """ 138 while True: 139 record = read(handle) 140 if not record: 141 return 142 yield record
143