annotate corebio/seq_io/plain_io.py @ 11:4b38580a8b97

Uploaded
author davidmurphy
date Thu, 16 Feb 2012 21:15:10 -0500
parents c55bdc2fb9fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
1 #!/usr/bin/env python
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
2
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
3 # Copyright (c) 2005 Gavin E. Crooks <gec@threeplusone.com>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
4 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
5 # This software is distributed under the MIT Open Source License.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
6 # <http://www.opensource.org/licenses/mit-license.html>
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
7 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
8 # Permission is hereby granted, free of charge, to any person obtaining a
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
9 # copy of this software and associated documentation files (the "Software"),
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
10 # to deal in the Software without restriction, including without limitation
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
11 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
12 # and/or sell copies of the Software, and to permit persons to whom the
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
13 # Software is furnished to do so, subject to the following conditions:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
14 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
15 # The above copyright notice and this permission notice shall be included
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
16 # in all copies or substantial portions of the Software.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
17 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
24 # THE SOFTWARE.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
25 #
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
26
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
27 """Read and write raw, unformatted sequence data. The whole file is read
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
28 in as a sequence. Whitespace is removed.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
29
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
30
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
31 --- Example Plain/Raw/Text File ---
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
32
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
33 --------------------------LENSTSPYDYGENESD-------FSDSPPCPQDF
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
34 --------------------------LENLEDLF-WELDRLD------NYNDTSLVENH-
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
35 --------------------------MSNITDPQMWDFDDLN-------FTGMPPADEDY
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
36 -----------------------------------YTSDN---------YSGSGDYDSNK
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
37 -SL-------NFDRTFLPALYSLLFLLGLLGNGAVAAVLLSQRTALSSTDTFLLHLAVAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
38 --LC-PATMASFKAVFVPVAYSLIFLLGVIGNVLVLVILERHRQTRSSTETFLFHLAVAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
39 -SPC-MLETETLNKYVVIIAYALVFLLSLLGNSLVMLVILYSRVGRSVTDVYLLNLALAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
40 -EPC-RDENVHFNRIFLPTIYFIIFLTGIVGNGLVILVMGYQKKLRSMTDKYRLHLSVAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
41 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
42
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
43 from corebio.seq import *
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
44 from corebio.utils import remove_whitespace
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
45
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
46 example = """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
47 --------------------------LENSTSPYDYGENESD-------FSDSPPCPQDF
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
48 --------------------------LENLEDLF-WELDRLD------NYNDTSLVENH-
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
49 --------------------------MSNITDPQMWDFDDLN-------FTGMPPADEDY
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
50 -----------------------------------YTSDN---------YSGSGDYDSNK
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
51 -SL-------NFDRTFLPALYSLLFLLGLLGNGAVAAVLLSQRTALSSTDTFLLHLAVAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
52 --LC-PATMASFKAVFVPVAYSLIFLLGVIGNVLVLVILERHRQTRSSTETFLFHLAVAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
53 -SPC-MLETETLNKYVVIIAYALVFLLSLLGNSLVMLVILYSRVGRSVTDVYLLNLALAD
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
54 -EPC-RDENVHFNRIFLPTIYFIIFLTGIV
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
55 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
56
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
57 names = ("plain","raw")
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
58 extensions = ('txt', )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
59
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
60 def read(fin, alphabet=None):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
61 """Read a file of raw sequecne data.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
62
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
63 Args:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
64 fin -- A stream or file to read
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
65 alphabet -- The expected alphabet of the data, if given
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
66 Returns:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
67 SeqList -- A list of sequences
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
68 Raises:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
69 ValueError -- If the file is unparsable
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
70 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
71 seqs = [ s for s in iterseq(fin, alphabet)]
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
72 return SeqList(seqs)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
73
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
74
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
75 def iterseq(fin, alphabet=None) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
76 """ Read the sequence data and yeild one (and only one) sequence.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
77
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
78 Args:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
79 fin -- A stream or file to read
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
80 alphabet -- The expected alphabet of the data, if given
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
81 Yeilds:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
82 Seq -- One alphabetic sequence at a time.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
83 Raises:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
84 ValueError -- If the file is unparsable
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
85 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
86
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
87 alphabet = Alphabet(alphabet)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
88 lines = []
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
89 for linenum, line in enumerate(fin) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
90 if line.isspace(): continue # Blank line
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
91 line = line.strip()
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
92
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
93
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
94 if line[0] == '>' : # probable a fasta file. Fail.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
95 raise ValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
96 "Parse Error on input line: %d " % (linenum) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
97 line = remove_whitespace(line)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
98
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
99 if not alphabet.alphabetic(line) :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
100 raise ValueError(
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
101 "Character on line: %d not in alphabet: %s : %s" % \
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
102 (linenum, alphabet, line) )
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
103 lines.append(line)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
104
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
105 yield Seq(''.join(lines), alphabet)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
106
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
107
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
108
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
109 def write(afile, seqs):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
110 """Write raw sequence data, one line per sequence.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
111
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
112 arguments:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
113 afile -- A writable stream.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
114 seqs -- A list of Seq's
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
115 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
116 for s in seqs :
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
117 writeseq(afile, s)
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
118
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
119
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
120 def writeseq(afile, seq):
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
121 """ Write a single sequence in raw format.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
122
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
123 arguments:
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
124 afile -- A writable stream.
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
125 seq -- A Seq instance
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
126 """
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
127 print >>afile, seq
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
128
c55bdc2fb9fa Uploaded
davidmurphy
parents:
diff changeset
129