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

Source Code for Module Bio.SeqIO.TabIO

  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  """Bio.SeqIO support for the "tab" (simple tab separated) file format. 
  7   
  8  You are expected to use this module via the Bio.SeqIO functions. 
  9   
 10  The "tab" format is an ad-hoc plain text file format where each sequence is 
 11  on one (long) line.  Each line contains the identifier/description, followed 
 12  by a tab, followed by the sequence.  For example, consider the following 
 13  short FASTA format file:: 
 14   
 15      >ID123456 possible binding site? 
 16      CATCNAGATGACACTACGACTACGACTCAGACTAC 
 17      >ID123457 random sequence 
 18      ACACTACGACTACGACTCAGACTACAAN 
 19   
 20  Apart from the descriptions, this can be represented in the simple two column 
 21  tab separated format as follows:: 
 22   
 23      ID123456(tab)CATCNAGATGACACTACGACTACGACTCAGACTAC 
 24      ID123457(tab)ACACTACGACTACGACTCAGACTACAAN 
 25   
 26  When reading this file, "ID123456" or "ID123457" will be taken as the record's 
 27  .id and .name property.  There is no other information to record. 
 28   
 29  Similarly, when writing to this format, Biopython will ONLY record the record's 
 30  .id and .seq (and not the description or any other information) as in the 
 31  example above. 
 32  """ 
 33   
 34  from __future__ import print_function 
 35   
 36  from Bio.Alphabet import single_letter_alphabet 
 37  from Bio.Seq import Seq 
 38  from Bio.SeqRecord import SeqRecord 
 39  from Bio.SeqIO.Interfaces import SequentialSequenceWriter 
 40   
 41  __docformat__ = "restructuredtext en" 
 42   
 43   
44 -def TabIterator(handle, alphabet=single_letter_alphabet):
45 """Iterates over tab separated lines (as SeqRecord objects). 46 47 Each line of the file should contain one tab only, dividing the line 48 into an identifier and the full sequence. 49 50 Arguments: 51 - handle - input file 52 - alphabet - optional alphabet 53 54 The first field is taken as the record's .id and .name (regardless of 55 any spaces within the text) and the second field is the sequence. 56 57 Any blank lines are ignored. 58 """ 59 for line in handle: 60 try: 61 title, seq = line.split("\t") # will fail if more than one tab! 62 except: 63 if line.strip() == "": 64 # It's a blank line, ignore it 65 continue 66 raise ValueError("Each line should have one tab separating the" + 67 " title and sequence, this line has %i tabs: %s" 68 % (line.count("\t"), repr(line))) 69 title = title.strip() 70 seq = seq.strip() # removes the trailing new line 71 yield SeqRecord(Seq(seq, alphabet), 72 id=title, name=title, 73 description="")
74 75
76 -class TabWriter(SequentialSequenceWriter):
77 """Class to write simple tab separated format files. 78 79 Each line consists of "id(tab)sequence" only. 80 81 Any description, name or other annotation is not recorded. 82 """
83 - def write_record(self, record):
84 """Write a single tab line to the file.""" 85 assert self._header_written 86 assert not self._footer_written 87 self._record_written = True 88 89 title = self.clean(record.id) 90 seq = self._get_seq_string(record) # Catches sequence being None 91 assert "\t" not in title 92 assert "\n" not in title 93 assert "\r" not in title 94 assert "\t" not in seq 95 assert "\n" not in seq 96 assert "\r" not in seq 97 self.handle.write("%s\t%s\n" % (title, seq))
98 99 100 if __name__ == "__main__": 101 print("Running quick self test") 102 from Bio._py3k import StringIO 103 104 # This example has a trailing blank line which should be ignored 105 handle = StringIO("Alpha\tAAAAAAA\nBeta\tCCCCCCC\n\n") 106 records = list(TabIterator(handle)) 107 assert len(records) == 2 108 109 handle = StringIO("Alpha\tAAAAAAA\tExtra\nBeta\tCCCCCCC\n") 110 try: 111 records = list(TabIterator(handle)) 112 assert False, "Should have reject this invalid example!" 113 except ValueError: 114 # Good! 115 pass 116 117 print("Done") 118