]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/test_sam.rb
rewrite of FASTQ internals
[biopieces.git] / code_ruby / test / maasha / test_sam.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__), '..', '..')
3
4 # Copyright (C) 2011 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # This software is part of the Biopieces framework (www.biopieces.org).
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28 require 'test/unit'
29 require 'test/helper'
30 require 'maasha/sam'
31 require 'stringio'
32
33 SAM_DATA =
34 %{@HD\tVN:1.3\tSO:coordinate
35 @SQ\tSN:ref\tLN:45
36 @CO\tMyComment
37 r001\t163\tref\t7\t30\t8M2I4M1D3M\t=\t37\t39\tTTAGATAAAGGATACTG\t*
38 r002\t0\tref\t9\t30\t3S6M1P1I4M\t*\t0\t0\tAAAAGATAAGGATA\t*
39 r003\t0\tref\t9\t30\t5H6M\t*\t0\t0\tAGCTAA\t*\tNM:i:1
40 r004\t0\tref\t16\t30\t6M14N5M\t*\t0\t0\tATAGCTTCAGC\t*
41 r003\t16\tref\t29\t30\t6H5M\t*\t0\t0\tTAGGC\t*\tNM:i:0
42 r001\t83\tref\t37\t30\t9M\t=\t7\t-39\tCAGCGCCAT\t*
43 }
44
45 class SamTest < Test::Unit::TestCase
46   def setup
47     @sam = Sam.new(StringIO.new(SAM_DATA))
48   end
49
50   test "#new with missing version number raises" do
51     assert_raise(SamError) { Sam.new(StringIO.new("@HD")) }
52   end
53
54   test "#new with bad version number raises" do
55     assert_raise(SamError) { Sam.new(StringIO.new("@HD\tXN:1.3")) }
56   end
57
58   test "#new with ok version number returns correctly" do
59     sam = Sam.new(StringIO.new("@HD\tVN:1.3"))
60     assert_equal(1.3, sam.header[:HD][:VN])
61   end
62
63   test "#new with bad sort order raises" do
64     assert_raise(SamError) { Sam.new(StringIO.new("@HD\tVN:1.3\tSO:fish")) }
65   end
66
67   test "#new with ok sort order returns correctly" do
68     %w{unknown unsorted queryname coordinate}.each do |order|
69       sam = Sam.new(StringIO.new("@HD\tVN:1.3\tSO:#{order}"))
70       assert_equal(order, sam.header[:HD][:SO])
71     end
72   end
73
74   test "#new with missing sequence name raises" do
75     assert_raise(SamError) { Sam.new(StringIO.new("@SQ")) }
76   end
77
78   test "#new with bad sequence name raises" do
79     assert_raise(SamError) { Sam.new(StringIO.new("@SQ\tSN:")) }
80   end
81
82   test "#new with ok sequence name returns correctly" do
83     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45"))
84     assert_equal({:LN=>45}, sam.header[:SQ][:SN][:ref])
85   end
86
87   test "#new with duplicate sequence name raises" do
88     assert_raise(SamError) { Sam.new(StringIO.new("@SQ\tSN:ref\n@SQ\tSN:ref")) }
89   end
90
91   test "#new with missing sequence length raises" do
92     assert_raise(SamError) { Sam.new(StringIO.new("@SQ\tSN:ref")) }
93   end
94
95   test "#new with bad sequence length raises" do
96     assert_raise(SamError) { Sam.new(StringIO.new("@SQ\tSN:scaffold17_1_MH0083\tLN:x")) }
97   end
98
99   test "#new with ok sequence length returns correctly" do
100     sam = Sam.new(StringIO.new("@SQ\tSN:scaffold17_1_MH0083\tLN:995"))
101     assert_equal(995, sam.header[:SQ][:SN][:scaffold17_1_MH0083][:LN])
102   end
103
104   test "#new with full SQ dont raise" do
105     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\tAS:ident\tM5:87e6b2aedf51b1f9c89becfab9267f41\tSP:E.coli\tUR:http://www.biopieces.org"))
106     assert_nothing_raised { sam.header }
107   end
108
109   test "#new with bad read group identifier raises" do
110     assert_raise(SamError) { Sam.new(StringIO.new("@RG\tID:")) }
111   end
112
113   test "#new with missing read group identifier raises" do
114     assert_raise(SamError) { Sam.new(StringIO.new("@RG")) }
115   end
116
117   test "#new with duplicate read group identifier raises" do
118     assert_raise(SamError) { Sam.new(StringIO.new("@RG\tID:123\n@RG\tID:123")) }
119   end
120
121   test "#new with ok read group identifier dont raise" do
122     sam = Sam.new(StringIO.new("@RG\tID:123\n@RG\tID:124"))
123     assert_nothing_raised { sam.header }
124   end
125
126   test "#new with bad flow order raises" do
127     assert_raise(SamError) { Sam.new(StringIO.new("@RG\tID:123\tFO:3")) }
128   end
129
130   test "#new with ok flow order dont raise" do
131     sam = Sam.new(StringIO.new("@RG\tID:123\tFO:*"))
132     assert_nothing_raised { sam.header }
133     sam = Sam.new(StringIO.new("@RG\tID:123\tFO:ACMGRSVTWYHKDBN"))
134     assert_nothing_raised { sam.header }
135   end
136
137   test "#new with bad platform raises" do
138     assert_raise(SamError) { Sam.new(StringIO.new("@RG\tID:123\tPL:maersk")) }
139   end
140
141   test "#new with ok platform dont raise" do
142     sam = Sam.new(StringIO.new("@RG\tID:123\tPL:ILLUMINA"))
143     assert_nothing_raised { sam.header }
144   end
145
146   test "#new with bad program identifier raises" do
147     assert_raise(SamError) { Sam.new(StringIO.new("@PG\tID:")) }
148   end
149
150   test "#new with missing program identifier raises" do
151     assert_raise(SamError) { Sam.new(StringIO.new("@PG")) }
152   end
153
154   test "#new with duplicate program identifier raises" do
155     assert_raise(SamError) { Sam.new(StringIO.new("@PG\tID:123\n@PG\tID:123")) }
156   end
157
158   test "#new with bad comment raises" do
159     assert_raise(SamError) { Sam.new(StringIO.new("@CO\t")) }
160   end 
161
162   test "#new with ok comment dont raise" do
163     sam = Sam.new(StringIO.new("@CO\tfubar"))
164     assert_nothing_raised { sam.header }
165   end
166
167   test "#each with bad field count raises" do
168     fields = []
169
170     (0 ... 11).each do |i|
171       sam = Sam.new(StringIO.new(fields.join("\t") + $/))
172       assert_raise(SamError) { sam.each }
173       fields << "*"
174     end
175   end
176
177   test "#each with ok field count dont raise" do
178     sam = Sam.new(StringIO.new(SAM_DATA))
179     assert_nothing_raised { sam.each }
180   end
181
182   test "#each with bad qname raises" do
183     sam = Sam.new(StringIO.new(" \t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
184     assert_raise(SamError) { sam.each }
185   end
186
187   test "#each with ok qname dont raise" do
188     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
189     assert_nothing_raised(SamError) { sam.each }
190   end
191
192   test "#each with bad flag raises" do
193     sam = Sam.new(StringIO.new("*\t-1\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
194     assert_raise(SamError) { sam.each }
195
196     sam = Sam.new(StringIO.new("*\t65536\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
197     assert_raise(SamError) { sam.each }
198   end
199
200   test "#each with ok flag dont raise" do
201     sam = Sam.new(StringIO.new("*\t0\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
202     assert_nothing_raised { sam.each }
203
204     sam = Sam.new(StringIO.new("*\t65535\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
205     assert_nothing_raised { sam.each }
206   end
207
208   test "#each with bad rname raises" do
209     sam = Sam.new(StringIO.new("*\t*\t \t*\t*\t*\t*\t*\t*\t*\t*\n"))
210     assert_raise(SamError) { sam.each }
211   end
212
213   test "#each with ok rname dont raise" do
214     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
215     assert_nothing_raised { sam.each }
216   end
217
218   test "#each with bad pos raises" do
219     sam = Sam.new(StringIO.new("*\t*\t*\t-1\t*\t*\t*\t*\t*\t*\t*\n"))
220     assert_raise(SamError) { sam.each }
221
222     sam = Sam.new(StringIO.new("*\t*\t*\t536870912\t*\t*\t*\t*\t*\t*\t*\n"))
223     assert_raise(SamError) { sam.each }
224   end
225
226   test "#each with ok pos dont raise" do
227     sam = Sam.new(StringIO.new("*\t*\t*\t0\t*\t*\t*\t*\t*\t*\t*\n"))
228     assert_nothing_raised { sam.each }
229
230     sam = Sam.new(StringIO.new("*\t*\t*\t536870911\t*\t*\t*\t*\t*\t*\t*\n"))
231     assert_nothing_raised { sam.each }
232   end
233
234   test "#each with bad mapq raises" do
235     sam = Sam.new(StringIO.new("*\t*\t*\t*\t-1\t*\t*\t*\t*\t*\t*\n"))
236     assert_raise(SamError) { sam.each }
237
238     sam = Sam.new(StringIO.new("*\t*\t*\t*\t256\t*\t*\t*\t*\t*\t*\n"))
239     assert_raise(SamError) { sam.each }
240   end
241
242   test "#each with ok mapq dont raise" do
243     sam = Sam.new(StringIO.new("*\t*\t*\t*\t0\t*\t*\t*\t*\t*\t*\n"))
244     assert_nothing_raised { sam.each }
245
246     sam = Sam.new(StringIO.new("*\t*\t*\t*\t255\t*\t*\t*\t*\t*\t*\n"))
247     assert_nothing_raised { sam.each }
248   end
249
250   test "#each with bad rnext raises" do
251     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t \t*\t*\t*\t*\n"))
252     assert_raise(SamError) { sam.each }
253   end
254
255   test "#each with ok rnext dont raise" do
256     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\n"))
257     assert_nothing_raised { sam.each }
258
259     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t=\t*\t*\t*\t*\n"))
260     assert_nothing_raised { sam.each }
261
262     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t!\t*\t*\t*\t*\n"))
263     assert_nothing_raised { sam.each }
264   end
265
266   test "#each with bad pnext raises" do
267     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t-1\t*\t*\t*\n"))
268     assert_raise(SamError) { sam.each }
269
270     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t536870912\t*\t*\t*\n"))
271     assert_raise(SamError) { sam.each }
272   end
273
274   test "#each with ok pnext dont raise" do
275     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t0\t*\t*\t*\n"))
276     assert_nothing_raised { sam.each }
277
278     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t536870911\t*\t*\t*\t*\n"))
279     assert_nothing_raised { sam.each }
280   end
281
282   test "#each with bad tlen raises" do
283     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t-536870912\t*\t*\n"))
284     assert_raise(SamError) { sam.each }
285
286     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t536870912\t*\t*\n"))
287     assert_raise(SamError) { sam.each }
288   end
289
290   test "#each with ok tlen dont raise" do
291     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t-536870911\t*\t*\n"))
292     assert_nothing_raised { sam.each }
293
294     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t536870911\t*\t*\n"))
295     assert_nothing_raised { sam.each }
296   end
297
298   test "#each with bad seq raises" do
299     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t\*\t \t*\n"))
300     assert_raise(SamError) { sam.each }
301   end
302
303   test "#each with ok seq dont raise" do
304     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t\*\t*\t*\n"))
305     assert_nothing_raised { sam.each }
306
307     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t\*\tATCGatcg=.\t*\n"))
308     assert_nothing_raised { sam.each }
309   end
310
311   test "#each with bad qual raises" do
312     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t\*\t*\t \n"))
313     assert_raise(SamError) { sam.each }
314   end
315
316   test "#each with ok qual dont raise" do
317     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t*\t*\t*\t\*\t*\t@\n"))
318     assert_nothing_raised { sam.each }
319   end
320
321   test "#each with rname missing from header raises" do
322     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\tMIS\t*\t*\t*\t*\t*\t\*\t*\t*\n"))
323     assert_raise(SamError) { sam.each }
324   end
325
326   test "#each with rname present in header dont raise" do
327     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\tref\t*\t*\t*\t*\t*\t\*\t*\t*\n"))
328     assert_nothing_raised { sam.each }
329
330     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\t*\t*\t*\t*\t*\t*\t\*\t*\t*\n"))
331     assert_nothing_raised { sam.each }
332   end
333
334   test "#each with rnext missing from header raises" do
335     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\t*\t*\t*\t*\tMIS\t*\t\*\t*\t*\n"))
336     assert_raise(SamError) { sam.each }
337   end
338
339   test "#each with rnext present in header dont raise" do
340     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\t*\t*\t*\t*\t*\t*\t\*\t*\t*\n"))
341     assert_nothing_raised { sam.each }
342
343     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\t*\t*\t*\t*\t=\t*\t\*\t*\t*\n"))
344     assert_nothing_raised { sam.each }
345
346     sam = Sam.new(StringIO.new("@SQ\tSN:ref\tLN:45\n*\t*\t*\t*\t*\t*\tref\t*\t\*\t*\t*\n"))
347     assert_nothing_raised { sam.each }
348   end
349
350   test "#to_bp returns correctly" do
351     string = "ID00036734\t0\tgi48994873\t366089\t37\t37M1I62M\t*\t0\t0\tGTTCCGCTATCGGCTGAATTTGATTGCGAGTGAGATATTTTATGCCAGCCAGCCAGACGCAGACGCGCCGAGACAGAACTTAATGGGCCCGCTAACAGCG\t*\tXT:A:U\tNM:i:1\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:99\n"
352
353     sam = Sam.new(StringIO.new(string))
354
355     sam.each do |s|
356       assert_equal("SAM", Sam.to_bp(s)[:REC_TYPE])
357       assert_equal("ID00036734", Sam.to_bp(s)[:Q_ID])
358       assert_equal("-", Sam.to_bp(s)[:STRAND])
359       assert_equal("gi48994873", Sam.to_bp(s)[:S_ID])
360       assert_equal(366089, Sam.to_bp(s)[:S_BEG])
361       assert_equal(37, Sam.to_bp(s)[:MAPQ])
362       assert_equal("37M1I62M", Sam.to_bp(s)[:CIGAR])
363       assert_equal("GTTCCGCTATCGGCTGAATTTGATTGCGAGTGAGATATTTTATGCCAGCCAGCCAGACGCAGACGCGCCGAGACAGAACTTAATGGGCCCGCTAACAGCG", Sam.to_bp(s)[:SEQ])
364       assert_equal("37:->T", Sam.to_bp(s)[:ALIGN])
365     end
366   end
367
368   test "#to_bp alignment descriptor without mismatch or indel returns correctly" do
369     string = "q_id\t0\ts_id\t1000\t40\t10M\t*\t0\t0\tGTTCCGCTAT\t*\tXT:A:U\tNM:i:0\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:10\n"
370
371     sam = Sam.new(StringIO.new(string))
372
373     sam.each do |s|
374       assert_equal(nil, Sam.to_bp(s)[:ALIGN])
375     end
376   end
377
378   test "#to_bp alignment descriptor with mismatches returns correctly" do
379     string = "q_id\t0\ts_id\t1000\t40\t10M\t*\t0\t0\tgTTCCGCTAt\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:0C8A\n"
380
381     sam = Sam.new(StringIO.new(string))
382
383     sam.each do |s|
384       assert_equal("0:C>g,9:A>t", Sam.to_bp(s)[:ALIGN])
385     end
386   end
387
388   test "#to_bp alignment descriptor with insertions returns correctly" do
389     string = "q_id\t0\ts_id\t1000\t40\t1I10M1I\t*\t0\t0\taGTTCCGCTATc\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:12\n"
390
391     sam = Sam.new(StringIO.new(string))
392
393     sam.each do |s|
394       assert_equal("0:->a,11:->c", Sam.to_bp(s)[:ALIGN])
395     end
396   end
397
398   test "#to_bp alignment descriptor with deletions returns correctly" do
399     string = "q_id\t0\ts_id\t1000\t40\t2D10M\t*\t0\t0\tGTTCCGCTAT\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:^AC10\n"
400
401     sam = Sam.new(StringIO.new(string))
402
403     sam.each do |s|
404       assert_equal("0:A>-,1:C>-", Sam.to_bp(s)[:ALIGN])
405     end
406   end
407 end
408