Package Bio :: Package SeqIO :: Module IgIO
[hide private]
[frames] | no frames]

Source Code for Module Bio.SeqIO.IgIO

  1  # Copyright 2008-2010 by Peter Cock.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  # 
  6  # This module is for reading and writing IntelliGenetics format files as 
  7  # SeqRecord objects.  This file format appears to be the same as the MASE 
  8  # multiple sequence alignment format. 
  9   
 10  """Bio.SeqIO support for the "ig" (IntelliGenetics or MASE) file format. 
 11   
 12  You are expected to use this module via the Bio.SeqIO functions. 
 13  """ 
 14   
 15  from __future__ import print_function 
 16   
 17  from Bio.Alphabet import single_letter_alphabet 
 18  from Bio.Seq import Seq 
 19  from Bio.SeqRecord import SeqRecord 
 20   
 21   
22 -def IgIterator(handle, alphabet=single_letter_alphabet):
23 """Iterate over IntelliGenetics records (as SeqRecord objects). 24 25 handle - input file 26 alphabet - optional alphabet 27 28 The optional free format file header lines (which start with two 29 semi-colons) are ignored. 30 31 The free format commentary lines at the start of each record (which 32 start with a semi-colon) are recorded as a single string with embedded 33 new line characters in the SeqRecord's annotations dictionary under the 34 key 'comment'. 35 """ 36 #Skip any file header text before the first record (;; lines) 37 while True: 38 line = handle.readline() 39 if not line: 40 break # Premature end of file, or just empty? 41 if not line.startswith(";;"): 42 break 43 44 while line: 45 #Now iterate over the records 46 if line[0] != ";": 47 raise ValueError( 48 "Records should start with ';' and not:\n%s" % repr(line)) 49 50 #Try and agree with SeqRecord convention from the GenBank parser, 51 #(and followed in the SwissProt parser) which stores the comments 52 #as a long string with newlines under annotations key 'comment'. 53 54 #Note some examples use "; ..." and others ";..." 55 comment_lines = [] 56 while line.startswith(";"): 57 #TODO - Extract identifier from lines like "LOCUS\tB_SF2"? 58 comment_lines.append(line[1:].strip()) 59 line = handle.readline() 60 title = line.rstrip() 61 62 seq_lines = [] 63 while True: 64 line = handle.readline() 65 if not line: 66 break 67 if line[0] == ";": 68 break 69 #Remove trailing whitespace, and any internal spaces 70 seq_lines.append(line.rstrip().replace(" ", "")) 71 seq_str = "".join(seq_lines) 72 if seq_str.endswith("1"): 73 #Remove the optional terminator (digit one) 74 seq_str = seq_str[:-1] 75 if "1" in seq_str: 76 raise ValueError( 77 "Potential terminator digit one found within sequence.") 78 79 #Return the record and then continue... 80 record = SeqRecord(Seq(seq_str, alphabet), 81 id=title, name=title) 82 record.annotations['comment'] = "\n".join(comment_lines) 83 yield record 84 85 #We should be at the end of the file now 86 assert not line
87 88 if __name__ == "__main__": 89 print("Running quick self test") 90 91 import os 92 path = "../../Tests/IntelliGenetics/" 93 if os.path.isdir(path): 94 for filename in os.listdir(path): 95 if os.path.splitext(filename)[-1] == ".txt": 96 print("") 97 print(filename) 98 print("-" * len(filename)) 99 with open(os.path.join(path, filename)) as handle: 100 for record in IgIterator(handle): 101 print("%s %i" % (record.id, len(record))) 102 print("Done") 103 else: 104 print("Could not find input files") 105