File: test_gff.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (1260 lines) | stat: -rw-r--r-- 44,712 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
#
# test/unit/bio/db/test_gff.rb - Unit test for Bio::GFF
#
# Copyright::   Copyright (C) 2005, 2008
#               Mitsuteru Nakao <n@bioruby.org>
#               Naohisa Goto <ng@bioruby.org>
# License::     The Ruby License
#
#  $Id:$
#

# loading helper routine for testing bioruby
require 'pathname'
load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 3,
                            'bioruby_test_helper.rb')).cleanpath.to_s

# libraries needed for the tests
require 'test/unit'
require 'digest/sha1'
require 'bio/db/gff'

module Bio
  class TestGFF < Test::Unit::TestCase
    
    def setup
      data = <<END_OF_DATA
I	sgd	CEN	151453	151591	.	+	.	CEN "CEN1" ; Note "CEN1\; Chromosome I Centromere"
I	sgd	gene	147591	151163	.	-	.	Gene "TFC3" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene "FUN24" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene "TSV115" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	ORF	147591	151163	.	-	.	ORF "YAL001C" ; Note "TFC3\; transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	143998	147528	.	+	.	Gene "VPS8" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene "FUN15" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene "VPT8" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
END_OF_DATA
      @obj = Bio::GFF.new(data)
    end

    def test_records
      assert_equal(8, @obj.records.size)
    end

    def test_record_class
      assert_equal(Bio::GFF::Record, @obj.records[0].class)
    end

  end # class TestGFF

  class TestGFFRecord < Test::Unit::TestCase
    
    def setup
      data =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.	Gene "CEN1" ; Note "Chromosome I Centromere"
END_OF_DATA
      @obj = Bio::GFF::Record.new(data)
    end

    def test_seqname
      assert_equal('I', @obj.seqname)
    end

    def test_source
      assert_equal('sgd', @obj.source)
    end

    def test_feature
      assert_equal('gene', @obj.feature)
    end

    def test_start
      assert_equal('151453', @obj.start)
    end

    def test_end
      assert_equal('151591', @obj.end)
    end

    def test_score
      assert_equal('.', @obj.score)
    end

    def test_strand
      assert_equal('+', @obj.strand)
    end

    def test_frame
      assert_equal('.', @obj.frame)
    end

    def test_attributes
      at = {"Note"=>'"Chromosome I Centromere"', "Gene"=>'"CEN1"'}
      assert_equal(at, @obj.attributes)
    end

    def test_comment
      assert_equal(nil, @obj.comment)
    end

  end # class TestGFFRecord
  
  class TestGFFRecordConstruct < Test::Unit::TestCase

    def setup
      @obj = Bio::GFF.new
    end

    def test_add_seqname
      name = "test"
      record = Bio::GFF::Record.new("")
      record.seqname = name
      @obj.records << record
      assert_equal(name, @obj.records[0].seqname)
    end

  end # class TestGFFRecordConstruct

  class TestGFF2 < Test::Unit::TestCase
    def setup
      data = <<END_OF_DATA
##gff-version 2
##date 2008-09-22
I	sgd	CEN	151453	151591	.	+	.	CEN "CEN1" ; Note "CEN1; Chromosome I Centromere"
I	sgd	gene	147591	151163	.	-	.	Gene "TFC3" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene "FUN24" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene "TSV115" ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	ORF	147591	151163	.	-	.	ORF "YAL001C" ; Note "TFC3; transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	143998	147528	.	+	.	Gene "VPS8" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene "FUN15" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene "VPT8" ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
END_OF_DATA
      @obj = Bio::GFF::GFF2.new(data)
    end

    def test_const_version
      assert_equal(2, Bio::GFF::GFF2::VERSION)
    end

    def test_gff_version
      assert_equal('2', @obj.gff_version)
    end

    def test_metadata_size
      assert_equal(1, @obj.metadata.size)
    end

    def test_metadata
      assert_equal(Bio::GFF::GFF2::MetaData.new('date', '2008-09-22'),
                   @obj.metadata[0])
    end

    def test_records_size
      assert_equal(8, @obj.records.size)
    end

    def test_to_s
      str = <<END_OF_DATA
##gff-version 2
##date 2008-09-22
I	sgd	CEN	151453	151591	.	+	.	CEN CEN1 ; Note "CEN1; Chromosome I Centromere"
I	sgd	gene	147591	151163	.	-	.	Gene TFC3 ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene FUN24 ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	147591	151163	.	-	.	Gene TSV115 ; Note "transcription factor tau (TFIIIC) subunit 138"
I	sgd	ORF	147591	151163	.	-	.	ORF YAL001C ; Note "TFC3; transcription factor tau (TFIIIC) subunit 138"
I	sgd	gene	143998	147528	.	+	.	Gene VPS8 ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene FUN15 ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
I	sgd	gene	143998	147528	.	+	.	Gene VPT8 ; Note "Vps8p is a membrane-associated hydrophilic protein which contains a C-terminal cysteine-rich region that conforms to the H2 variant of the RING finger Zn2+ binding motif."
END_OF_DATA
      assert_equal(str, @obj.to_s)
    end
  end #class TestGFF2

  class TestGFF2Record < Test::Unit::TestCase
    def setup
      str = "seq1\tBLASTX\tsimilarity\t101\t235\t87.1\t+\t0\tTarget \"HBA_HUMAN\" 11 55 ; E_value 0.0003 ; Align 101 11 ; Align 179 36 ; Comment \"Please ignore this \\\"Comment\\\" attribute; Escape \\x1a\\037 and \\\\\\t\\r\\n\\f\\b\\a\\e\\v; This is test.\" 123 4.56e-34 \"Test for freetext\" ; Note \"\"; Misc IdString; Misc \"free text\"; Misc 5678 "

      @obj = Bio::GFF::GFF2::Record.new(str)
    end

    def test_to_s
      str = "seq1\tBLASTX\tsimilarity\t101\t235\t87.1\t+\t0\tTarget HBA_HUMAN 11 55 ; E_value 0.0003 ; Align 101 11 ; Align 179 36 ; Comment \"Please ignore this \\\"Comment\\\" attribute; Escape \\032\\037 and \\\\\\t\\r\\n\\f\\b\\a\\e\\v; This is test.\" 123 4.56e-34 \"Test for freetext\" ; Note \"\" ; Misc IdString ; Misc \"free text\" ; Misc 5678\n"

      assert_equal(str, @obj.to_s)
    end

    def test_self_parse
      obj2 = Bio::GFF::GFF2::Record.parse(@obj.to_s)
      assert_equal(@obj, obj2)
    end

    def test_eqeq
      obj2 = Bio::GFF::GFF2::Record.new(@obj.to_s)
      assert_equal(true, @obj == obj2)
    end

    def test_eqeq_false
      obj2 = Bio::GFF::GFF2::Record.new(@obj.to_s)
      obj2.seqname = 'seq2'
      assert_equal(false, @obj == obj2)
    end

    def test_comment_only?
      assert_equal(false, @obj.comment_only?)
    end

    def test_seqname
      assert_equal('seq1', @obj.seqname)
    end

    def test_source
      assert_equal('BLASTX', @obj.source)
    end

    def test_feature
      assert_equal('similarity', @obj.feature)
    end

    def test_start
      assert_equal(101, @obj.start)
    end

    def test_end
      assert_equal(235, @obj.end)
    end

    def test_score
      assert_equal(87.1, @obj.score)
    end

    def test_strand
      assert_equal('+', @obj.strand)
    end

    def test_frame
      assert_equal(0, @obj.frame)
    end

    def test_attributes_to_hash
      hash = {
        'Target' =>
        Bio::GFF::GFF2::Record::Value.new(['HBA_HUMAN', '11', '55']),
        'E_value' => '0.0003',
        'Align' =>
        Bio::GFF::GFF2::Record::Value.new(['101', '11']),
        'Comment' =>
        Bio::GFF::GFF2::Record::Value.new(["Please ignore this \"Comment\" attribute; Escape \x1a\037 and \\\t\r\n\f\b\a\e\v; This is test.", "123", "4.56e-34", "Test for freetext"]),
        'Note' => '',
        'Misc' => 'IdString'
      }
      assert_equal(hash, @obj.attributes_to_hash)
    end

    def test_attributes
      attributes =
        [ [ 'Target',
            Bio::GFF::GFF2::Record::Value.new(['HBA_HUMAN', '11', '55']) ],
          [ 'E_value', '0.0003' ],
          [ 'Align',
            Bio::GFF::GFF2::Record::Value.new(['101', '11']) ],
          [ 'Align',
            Bio::GFF::GFF2::Record::Value.new(['179', '36']) ],
          [ 'Comment',
            Bio::GFF::GFF2::Record::Value.new(["Please ignore this \"Comment\" attribute; Escape \x1a\037 and \\\t\r\n\f\b\a\e\v; This is test.", "123", "4.56e-34", "Test for freetext"]) ],
          [ 'Note', '' ],
          [ 'Misc', 'IdString' ],
          [ 'Misc', 'free text' ],
          [ 'Misc', '5678' ]
        ]
      assert_equal(attributes, @obj.attributes)
    end

    def test_attribute
      val_Target = Bio::GFF::GFF2::Record::Value.new(['HBA_HUMAN', '11', '55'])
      assert_equal(val_Target, @obj.attribute('Target'))
      assert_equal('0.0003', @obj.attribute('E_value'))
      val_Align0 = Bio::GFF::GFF2::Record::Value.new(['101', '11'])
      #val_Align1 = Bio::GFF::GFF2::Record::Value.new(['179', '36'])
      assert_equal(val_Align0, @obj.attribute('Align'))
      val_Comment = Bio::GFF::GFF2::Record::Value.new(["Please ignore this \"Comment\" attribute; Escape \x1a\037 and \\\t\r\n\f\b\a\e\v; This is test.", "123", "4.56e-34", "Test for freetext"])
      assert_equal(val_Comment, @obj.attribute('Comment'))
      assert_equal('', @obj.attribute('Note'))
      assert_equal('IdString', @obj.attribute('Misc'))
    end

    def test_attribute_nonexistent
      assert_equal(nil, @obj.attribute('NonExistent'))
    end

    def test_get_attribute
      val_Target = Bio::GFF::GFF2::Record::Value.new(['HBA_HUMAN', '11', '55'])
      assert_equal(val_Target, @obj.get_attribute('Target'))
      assert_equal('0.0003', @obj.get_attribute('E_value'))
      val_Align0 = Bio::GFF::GFF2::Record::Value.new(['101', '11'])
      #val_Align1 = Bio::GFF::GFF2::Record::Value.new(['179', '36'])
      assert_equal(val_Align0, @obj.get_attribute('Align'))
      val_Comment = Bio::GFF::GFF2::Record::Value.new(["Please ignore this \"Comment\" attribute; Escape \x1a\037 and \\\t\r\n\f\b\a\e\v; This is test.", "123", "4.56e-34", "Test for freetext"])
      assert_equal(val_Comment, @obj.get_attribute('Comment'))
      assert_equal('', @obj.get_attribute('Note'))
      assert_equal('IdString', @obj.get_attribute('Misc'))
    end

    def test_get_attribute_nonexistent
      assert_equal(nil, @obj.get_attribute('NonExistent'))
    end

    def test_get_attributes
      val_Target = Bio::GFF::GFF2::Record::Value.new(['HBA_HUMAN', '11', '55'])
      assert_equal([ val_Target ], @obj.get_attributes('Target'))
      assert_equal([ '0.0003' ], @obj.get_attributes('E_value'))
      val_Align0 = Bio::GFF::GFF2::Record::Value.new(['101', '11'])
      val_Align1 = Bio::GFF::GFF2::Record::Value.new(['179', '36'])
      assert_equal([ val_Align0, val_Align1 ],
                   @obj.get_attributes('Align'))
      val_Comment = Bio::GFF::GFF2::Record::Value.new(["Please ignore this \"Comment\" attribute; Escape \x1a\037 and \\\t\r\n\f\b\a\e\v; This is test.", "123", "4.56e-34", "Test for freetext"])
      assert_equal([ val_Comment ], @obj.get_attributes('Comment'))
      assert_equal([ '' ], @obj.get_attributes('Note'))
      assert_equal([ 'IdString', 'free text', '5678' ], 
                   @obj.get_attributes('Misc'))
    end

    def test_get_attributes_nonexistent
      assert_equal([], @obj.get_attributes('NonExistent'))
    end

    def test_set_attribute
      assert_equal('0.0003', @obj.attribute('E_value'))
      assert_equal('1e-10', @obj.set_attribute('E_value', '1e-10'))
      assert_equal('1e-10', @obj.attribute('E_value'))
    end

    def test_set_attribute_multiple
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal('Replaced',
                   @obj.set_attribute('Misc', 'Replaced'))
      assert_equal([ 'Replaced', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
    end

    def test_set_attribute_nonexistent
      assert_equal(nil, @obj.attribute('NonExistent'))
      assert_equal('test', @obj.set_attribute('NonExistent', 'test'))
      assert_equal('test', @obj.attribute('NonExistent'))
    end

    def test_replace_attributes
      assert_equal([ '0.0003' ], @obj.get_attributes('E_value'))
      assert_equal(@obj, @obj.replace_attributes('E_value', '1e-10'))
      assert_equal([ '1e-10' ], @obj.get_attributes('E_value'))
    end

    def test_replace_attributes_single_multiple
      assert_equal([ '0.0003' ], @obj.get_attributes('E_value'))
      assert_equal(@obj, @obj.replace_attributes('E_value',
                                                 '1e-10', '3.14', '2.718'))
      assert_equal([ '1e-10', '3.14', '2.718' ],
                   @obj.get_attributes('E_value'))
    end

    def test_replace_attributes_multiple_single
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(@obj,
                   @obj.replace_attributes('Misc', 'Replaced_All'))
      assert_equal([ 'Replaced_All' ],
                   @obj.get_attributes('Misc'))
    end

    def test_replace_attributes_multiple_multiple_two
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(@obj,
                   @obj.replace_attributes('Misc',
                                           'Replaced', 'test2'))
      assert_equal([ 'Replaced', 'test2' ],
                   @obj.get_attributes('Misc'))
    end

    def test_replace_attributes_multiple_multiple_same
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(@obj,
                   @obj.replace_attributes('Misc',
                                           'Replaced', 'test2', 'test3'))
      assert_equal([ 'Replaced', 'test2', 'test3' ],
                   @obj.get_attributes('Misc'))
    end

    def test_replace_attributes_multiple_multiple_over
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(@obj,
                   @obj.replace_attributes('Misc',
                                           'Replaced', 'test2', 'test3', '4'))
      assert_equal([ 'Replaced', 'test2', 'test3', '4' ],
                   @obj.get_attributes('Misc'))
    end

    def test_replace_attributes_nonexistent
      assert_equal(nil, @obj.attribute('NonExistent'))
      assert_equal(@obj, @obj.replace_attributes('NonExistent', 'test'))
      assert_equal([ 'test' ], @obj.get_attributes('NonExistent'))
    end

    def test_replace_attributes_nonexistent_multiple
      assert_equal(nil, @obj.attribute('NonExistent'))
      assert_equal(@obj,
                   @obj.replace_attributes('NonExistent',
                                           'test', 'gff2', 'attr'))
      assert_equal([ 'test', 'gff2', 'attr' ],
                   @obj.get_attributes('NonExistent'))
    end

    def test_delete_attribute
      assert_equal('0.0003', @obj.attribute('E_value'))
      assert_equal('0.0003', @obj.delete_attribute('E_value', '0.0003'))
      assert_equal(nil, @obj.attribute('E_value'))
    end

    def test_delete_attribute_nil
      assert_equal('0.0003', @obj.attribute('E_value'))
      assert_equal(nil, @obj.delete_attribute('E_value', '3'))
      assert_equal('0.0003', @obj.attribute('E_value'))
    end

    def test_delete_attribute_multiple
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal('free text',
                   @obj.delete_attribute('Misc', 'free text'))
      assert_equal([ 'IdString', '5678' ],
                   @obj.get_attributes('Misc'))
    end

    def test_delete_attribute_multiple2
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal('IdString',
                   @obj.delete_attribute('Misc', 'IdString'))
      assert_equal([ 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal('5678',
                   @obj.delete_attribute('Misc', '5678'))
      assert_equal([ 'free text' ],
                   @obj.get_attributes('Misc'))
    end

    def test_delete_attribute_multiple_nil
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(nil,
                   @obj.delete_attribute('Misc', 'test'))
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
    end

    def test_delete_attribute_nonexistent
      assert_equal(nil, @obj.attribute('NonExistent'))
      assert_equal(nil, @obj.delete_attribute('NonExistent', 'test'))
      assert_equal([], @obj.get_attributes('NonExistent'))
    end

    def test_delete_attributes
      assert_equal('0.0003', @obj.attribute('E_value'))
      assert_equal(@obj, @obj.delete_attributes('E_value'))
      assert_equal(nil, @obj.attribute('E_value'))
    end

    def test_delete_attributes_multiple
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
      assert_equal(@obj, @obj.delete_attributes('Misc'))
      assert_equal([], @obj.get_attributes('Misc'))
    end

    def test_delete_attributes_nonexistent
      assert_equal(nil, @obj.attribute('NonExistent'))
      assert_equal(nil, @obj.delete_attributes('NonExistent'))
      assert_equal([], @obj.get_attributes('NonExistent'))
    end

    def test_sort_attributes_by_tag!
      tags = %w( Comment Align E_value Note )
      assert_equal(@obj, @obj.sort_attributes_by_tag!(tags))
      assert_equal(%w( Comment Align Align E_value Note Target
                       Misc Misc Misc ),
                   @obj.attributes.collect { |x| x[0] })
      # check if the order of 'Misc' is not changed
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
    end

    def test_sort_attributes_by_tag_bang_test2
      tags = %w( E_value Misc Note Target )
      assert_equal(@obj, @obj.sort_attributes_by_tag!(tags))
      assert_equal(%w( E_value Misc Misc Misc Note Target
                       Align Align Comment ),
                   @obj.attributes.collect { |x| x[0] })
      # check if the order of 'Misc' is not changed
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
    end

    def test_sort_attributes_by_tag_bang_with_block
      assert_equal(@obj,
                   @obj.sort_attributes_by_tag! { |x, y|
                     x <=> y
                   })
      assert_equal(%w( Align Align Comment E_value Misc Misc Misc
                       Note Target ),
                   @obj.attributes.collect { |x| x[0] })
      # check if the order of 'Misc' is not changed
      assert_equal([ 'IdString', 'free text', '5678' ],
                   @obj.get_attributes('Misc'))
    end
  end #class TestGFF2Record

  class TestGFF2RecordEmpty < Test::Unit::TestCase
    def setup
      @obj = Bio::GFF::GFF2::Record.new('# test comment')
    end

    def test_comment_only?
      assert_equal(true, @obj.comment_only?)
    end

    def test_comment_only_false
      @obj.seqname = 'test'
      assert_equal(false, @obj.comment_only?)
    end

    def test_to_s
      assert_equal("# test comment\n", @obj.to_s)
    end

    def test_to_s_not_empty
      @obj.seqname = 'test'
      @obj.feature = 'region'
      @obj.start = 1
      @obj.end = 100
      assert_equal("test\t.\tregion\t1\t100\t.\t.\t.\t\t# test comment\n",
                   @obj.to_s)
      @obj.add_attribute('Gene', 'unknown')
      assert_equal("test\t.\tregion\t1\t100\t.\t.\t.\tGene unknown\t# test comment\n",
                   @obj.to_s)
    end

    def test_comment
      assert_equal(' test comment', @obj.comment)
    end

    def test_comment_eq
      assert_equal('changed the comment',
                   @obj.comment = 'changed the comment')
    end
  end #class TestGFF2RecordEmpty

  class TestGFF2ComplexAttributes < Test::Unit::TestCase

    # The test string comes from the Popular genome annotation from the JGI.
    # ftp://ftp.jgi-psf.org/pub/JGI_data/Poplar/annotation/v1.1/Poptr1_1.JamboreeModels.gff.gz
    # Thanks to Tomoaki NISHIYAMA who picks up the example line.
    def test_attributes_case1
      str = "LG_I\tJGI\tCDS\t11052\t11064\t.\t-\t0\tname \"grail3.0116000101\"; proteinId 639579; exonNumber 3\n"

      attributes = [
        [ "name",       "grail3.0116000101" ],
        [ "proteinId",  "639579" ],
        [ "exonNumber", "3" ]
      ]
      record = Bio::GFF::GFF2::Record.new(str)
      assert_equal(attributes, record.attributes)
    end

    # The test string is modified from that of test_attributes_case1.
    def test_attributes_case2
      str = "LG_I\tJGI\tCDS\t11052\t11064\t.\t-\t0\tname \"grail3.0116000101\"; proteinId 639579; exonNumber 3; Note \"Semicolons ; and \;, and quote \\\" can be OK\"; Comment \"This is the \\\"comment\\\"\"\n"

      attributes = [
        [ "name", "grail3.0116000101" ],
        [ "proteinId", "639579" ],
        [ "exonNumber", "3" ],
        [ "Note", "Semicolons ; and ;, and quote \" can be OK" ],
        [ "Comment", "This is the \"comment\"" ]
      ]
      record = Bio::GFF::GFF2::Record.new(str)
      assert_equal(attributes, record.attributes)
    end

    def test_attributes_incompatible_backslash_semicolon
      # No special treatments for backslash-semicolon outside the free text.
      str =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.	Gene "CEN1" ; Note "Chromosome I Centromere"; Semicolon a "b;c" d "e;f;g" h; Illegal a\\;b c d; Comment "a ; b"
END_OF_DATA

      attributes = [
        [ 'Gene',      'CEN1' ],
        [ 'Note',      'Chromosome I Centromere' ],
        [ 'Semicolon',
          Bio::GFF::GFF2::Record::Value.new(['a', 'b;c', 'd', 'e;f;g', 'h']) ],
        [ 'Illegal',   "a\\" ],
        [  'b', Bio::GFF::GFF2::Record::Value.new(['c', 'd']) ],
        [ 'Comment',   'a ; b' ]
      ]
      record = Bio::GFF::GFF2::Record.new(str)
      assert_equal(attributes, record.attributes)
    end

  end #class TestGFF2ComplexAttributes

  class TestGFF2MetaData < Test::Unit::TestCase
    def setup
      @data =
        Bio::GFF::GFF2::MetaData.new('date', '2008-09-22')
    end

    def test_parse
      assert_equal(@data,
                   Bio::GFF::GFF2::MetaData.parse('##date 2008-09-22'))
    end

    def test_directive
      assert_equal('date', @data.directive)
    end

    def test_data
      assert_equal('2008-09-22', @data.data)
    end
  end #class TestGFF2MetaData

  class TestGFF3 < Test::Unit::TestCase
    def setup
      @data =<<END_OF_DATA
##gff-version 3
##sequence-region test01 1 400
test01	RANDOM	contig	1	400	.	+	.	ID=test01;Note=this is test
test01	.	mRNA	101	230	.	+	.	ID=mrna01;Name=testmRNA;Note=this is test mRNA
test01	.	mRNA	101	280	.	+	.	ID=mrna01a;Name=testmRNAalterative;Note=test of alternative splicing variant
test01	.	exon	101	160	.	+	.	ID=exon01;Name=exon01;Alias=exon 1;Parent=mrna01,mrna01a
test01	.	exon	201	230	.	+	.	ID=exon02;Name=exon02;Alias=exon 2;Parent=mrna01
test01	.	exon	251	280	.	+	.	ID=exon02a;Name=exon02a;Alias=exon 2a;Parent=mrna01a
test01	.	Match	101	123	.	.	.	ID=match01;Name=match01;Target=EST101 1 21;Gap=M8 D3 M6 I1 M6
##FASTA
>test01
ACGAAGATTTGTATGACTGATTTATCCTGGACAGGCATTGGTCAGATGTCTCCTTCCGTATCGTCGTTTA
GTTGCAAATCCGAGTGTTCGGGGGTATTGCTATTTGCCACCTAGAAGCGCAACATGCCCAGCTTCACACA
CCATAGCGAACACGCCGCCCCGGTGGCGACTATCGGTCGAAGTTAAGACAATTCATGGGCGAAACGAGAT
AATGGGTACTGCACCCCTCGTCCTGTAGAGACGTCACAGCCAACGTGCCTTCTTATCTTGATACATTAGT
GCCCAAGAATGCGATCCCAGAAGTCTTGGTTCTAAAGTCGTCGGAAAGATTTGAGGAACTGCCATACAGC
CCGTGGGTGAAACTGTCGACATCCATTGTGCGAATAGGCCTGCTAGTGAC
END_OF_DATA
      @gff3 = Bio::GFF::GFF3.new(@data)
    end

    def test_const_version
      assert_equal(3, Bio::GFF::GFF3::VERSION)
    end

    def test_sequence_regions
      region = Bio::GFF::GFF3::SequenceRegion.new('test01', 1, 400)
      assert_equal([ region ], @gff3.sequence_regions)
    end

    def test_gff_version
      assert_equal('3', @gff3.gff_version)
    end

    def test_records
      assert_equal(7, @gff3.records.size)
      r_test01 = Bio::GFF::GFF3::Record.new('test01',
                                            'RANDOM',
                                            'contig',
                                            1, 400, nil, '+', nil,
                                            [ ['ID', 'test01'],
                                              ['Note', 'this is test'] ])
      r_mrna01 = Bio::GFF::GFF3::Record.new('test01',
                                            nil,
                                            'mRNA',
                                            101, 230, nil, '+', nil,
                                            [ ['ID', 'mrna01'],
                                              ['Name', 'testmRNA'],
                                              ['Note', 'this is test mRNA'] ])
      r_exon01 = Bio::GFF::GFF3::Record.new('test01',
                                            nil,
                                            'exon',
                                            101, 160, nil, '+', nil,
                                            [ ['ID', 'exon01'],
                                              ['Name', 'exon01'],
                                              ['Alias', 'exon 1'],
                                              ['Parent', 'mrna01'],
                                              ['Parent', 'mrna01a'] ])

      target = Bio::GFF::GFF3::Record::Target.new('EST101', 1, 21)
      gap = Bio::GFF::GFF3::Record::Gap.new('M8 D3 M6 I1 M6')
      r_match01 =Bio::GFF::GFF3::Record.new('test01',
                                            nil,
                                            'Match',
                                            101, 123, nil, nil, nil,
                                            [ ['ID', 'match01'],
                                              ['Name', 'match01'],
                                              ['Target', target],
                                              ['Gap', gap] ])
      assert_equal(r_test01, @gff3.records[0])
      assert_equal(r_mrna01, @gff3.records[1])
      assert_equal(r_exon01, @gff3.records[3])
      assert_equal(r_match01, @gff3.records[6])
    end

    def test_sequences
      assert_equal(1, @gff3.sequences.size)
      assert_equal('test01', @gff3.sequences[0].entry_id)
      assert_equal('3510a3c4f66f9c2ab8d4d97446490aced7ed1fa4',
                   Digest::SHA1.hexdigest(@gff3.sequences[0].seq.to_s))
    end

    def test_to_s
      assert_equal(@data, @gff3.to_s)
    end

  end #class TestGFF3

  class TestGFF3Record < Test::Unit::TestCase
    
    def setup
      data =<<END_OF_DATA
chrI	SGD	centromere	151467	151584	.	+	.	ID=CEN1;Name=CEN1;gene=CEN1;Alias=CEN1,test%3B0001;Note=Chromosome%20I%20centromere;dbxref=SGD:S000006463;Target=test%2002 123 456 -,test%2C03 159 314;memo%3Dtest%3Battr=99.9%25%09match
END_OF_DATA
      @obj = Bio::GFF::GFF3::Record.new(data)
    end

    def test_seqname
      assert_equal('chrI', @obj.seqname)
    end

    def test_source
      assert_equal('SGD', @obj.source)
    end

    def test_feature
      assert_equal('centromere', @obj.feature)
    end

    def test_start
      assert_equal(151467, @obj.start)
    end

    def test_end
      assert_equal(151584, @obj.end)
    end

    def test_score
      assert_equal(nil, @obj.score)
    end

    def test_strand
      assert_equal('+', @obj.strand)
    end

    def test_frame
      assert_equal(nil, @obj.frame)
    end

    def test_attributes
      attr = [
        ['ID',     'CEN1'],
        ['Name',   'CEN1'],
        ['gene',   'CEN1'],
        ['Alias',  'CEN1'],
        ['Alias',  'test;0001'],
        ['Note',   'Chromosome I centromere'],
        ['dbxref', 'SGD:S000006463'],
        ['Target',
         Bio::GFF::GFF3::Record::Target.new('test 02', 123, 456, '-')],
        ['Target',
         Bio::GFF::GFF3::Record::Target.new('test,03', 159, 314)],
        ['memo=test;attr', "99.9%\tmatch"]
      ]
      assert_equal(attr, @obj.attributes)
    end

    def test_id
      assert_equal('CEN1', @obj.id)
    end

    def test_to_s
      str =<<END_OF_DATA
chrI	SGD	centromere	151467	151584	.	+	.	ID=CEN1;Name=CEN1;gene=CEN1;Alias=CEN1,test%3B0001;Note=Chromosome I centromere;dbxref=SGD:S000006463;Target=test%2002 123 456 -,test%2C03 159 314;memo%3Dtest%3Battr=99.9%25%09match
END_OF_DATA
      assert_equal(str, @obj.to_s)
    end

    def test_to_s_attr_order_changed
      str = <<END_OF_STR
chrI	SGD	centromere	151467	151584	.	+	.	ID=CEN1;Name=CEN1;Alias=CEN1,test%3B0001;Target=test%2002 123 456 -,test%2C03 159 314;Note=Chromosome I centromere;dbxref=SGD:S000006463;gene=CEN1;memo%3Dtest%3Battr=99.9%25%09match
END_OF_STR

      keys = [ 'ID', 'Name', 'Alias', 'Target', 'Note', 'dbxref', 'gene' ]
      @obj.sort_attributes_by_tag!(keys)
      assert_equal(str, @obj.to_s)
    end
  end #class TestGFF3Record

  class TestGFF3RecordMisc < Test::Unit::TestCase
    def test_attributes_none
      # test blank with tab
      data =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.	
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal([], obj.attributes)
      
      # test blank with no tab at end
      data =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal([], obj.attributes)
    end
    
    def test_attributes_one
      data =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.	ID=CEN1
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      at = [ ["ID", 'CEN1'] ]
      assert_equal(at, obj.attributes)
    end
    
    def test_attributes_with_escaping
      data =<<END_OF_DATA
I	sgd	gene	151453	151591	.	+	.	ID=CEN1;gene=CEN1%3Boh;Note=Chromosome I Centromere
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      at = [ ['ID',   'CEN1'],
             ["gene", 'CEN1;oh'],
             ["Note", 'Chromosome I Centromere']
           ]
      assert_equal(at, obj.attributes)      
    end
    
    def test_score
      data =<<END_OF_DATA
ctg123	src	match	456	788	1e-10	-	.	ID=test01
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal(1e-10, obj.score)
      obj.score = 0.5
      assert_equal(0.5, obj.score)
    end

    def test_phase
      data =<<END_OF_DATA
ctg123	src	CDS	456	788	.	-	2	ID=test02
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal(2, obj.phase)
      assert_equal(2, obj.frame)
      obj.phase = 1
      assert_equal(1, obj.phase)
      assert_equal(1, obj.frame)
    end

    def test_id_replace
      data =<<END_OF_DATA
ctg123	src	CDS	456	788	1e-10	-	2	ID=test03
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal('test03', obj.id)
      assert_equal('test_id', obj.id = 'test_id')
      assert_equal('test_id', obj.id)
    end

    def test_id_set
      data =<<END_OF_DATA
ctg123	src	CDS	456	788	1e-10	-	2	NAME=test03
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_nil(obj.id)
      assert_equal('test_id', obj.id = 'test_id')
      assert_equal('test_id', obj.id)
      assert_equal('next_test', obj.id = 'next_test')
      assert_equal('next_test', obj.id)
    end

    def test_id_multiple
      # Note: Two ID attributes in a record is illegal in GFF3.
      data =<<END_OF_DATA
ctg123	src	CDS	456	788	.	-	2	ID=test03,test04
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal([ [ 'ID', 'test03' ], [ 'ID', 'test04' ] ],
                   obj.attributes)
      assert_equal('test03', obj.id)
      assert_equal('test_id', obj.id = 'test_id')
      assert_equal('test_id', obj.id)
      assert_equal([ [ 'ID', 'test_id' ], [ 'ID', 'test04' ] ],
                   obj.attributes)
      str = "ctg123\tsrc\tCDS\t456\t788\t.\t-\t2\tID=test_id,test04\n"
      assert_equal(str, obj.to_s)
    end

    def test_id_multiple2
      # Note: Two ID attributes in a record is illegal in GFF3.
      data =<<END_OF_DATA
ctg123	src	CDS	456	788	.	-	2	ID=test03;ID=test04
END_OF_DATA
      obj = Bio::GFF::GFF3::Record.new(data)
      assert_equal([ [ 'ID', 'test03' ], [ 'ID', 'test04' ] ],
                   obj.attributes)
      assert_equal('test03', obj.id)
      assert_equal('test_id', obj.id = 'test_id')
      assert_equal('test_id', obj.id)
      assert_equal([ [ 'ID', 'test_id' ], [ 'ID', 'test04' ] ],
                   obj.attributes)

      # The "XXX=test03;XXX=test04" is automatically changed to
      # "XXX=test03,test04", as defined in the GFF3 spec.
      str = "ctg123\tsrc\tCDS\t456\t788\t.\t-\t2\tID=test_id,test04\n"
      assert_equal(str, obj.to_s)
    end

    def test_initialize_9
      obj = Bio::GFF::GFF3::Record.new('test01',
                                       'testsrc',
                                       'exon',
                                       1, 400, nil, '+', nil,
                                       [ ['ID',   'test01'],
                                         ['Note', 'this is test'] ])
      assert_equal('test01', obj.seqid)
    end

    def test_to_s_void
      obj = Bio::GFF::GFF3::Record.new
      assert_equal(".\t.\t.\t.\t.\t.\t.\t.\t.\n", obj.to_s)
    end

  end #class TestGFF3RecordMisc

  class TestGFF3RecordEscape < Test::Unit::TestCase
    def setup
      @obj = Object.new.extend(Bio::GFF::GFF3::Escape)
      @str = "A>B\tC=100%;d=e,f,g h"
    end

    def test_escape
      str = @str
      assert_equal('A>B%09C=100%25;d=e,f,g h',
                   @obj.instance_eval { escape(str) })
    end

    def test_escape_attribute
      str = @str
      assert_equal('A>B%09C%3D100%25%3Bd%3De%2Cf%2Cg h',
                   @obj.instance_eval { escape_attribute(str) })
    end

    def test_escape_seqid
      str = @str
      assert_equal('A%3EB%09C%3D100%25%3Bd%3De%2Cf%2Cg%20h',
                   @obj.instance_eval { escape_seqid(str) })
    end

    def test_unescape
      escaped_str = 'A%3EB%09C%3D100%25%3Bd%3De%2Cf%2Cg%20h'
      assert_equal(@str,
                   @obj.instance_eval {
                     unescape(escaped_str) })
    end
  end #class TestGFF3RecordEscape

  class TestGFF3RecordTarget < Test::Unit::TestCase

    def setup
      @target =
        [ Bio::GFF::GFF3::Record::Target.new('ABCD1234', 123, 456, '+'),
          Bio::GFF::GFF3::Record::Target.new(">X Y=Z;P%,Q\tR", 78, 90),
          Bio::GFF::GFF3::Record::Target.new(nil, nil, nil),
        ]
    end

    def test_parse
      strings = 
        [ 'ABCD1234 123 456 +',
          '%3EX%20Y%3DZ%3BP%25%2CQ%09R 78 90',
          ''
        ]
      @target.each do |target|
        str = strings.shift
        assert_equal(target, Bio::GFF::GFF3::Record::Target.parse(str))
      end
    end

    def test_target_id
      assert_equal('ABCD1234', @target[0].target_id)
      assert_equal(">X Y=Z;P%,Q\tR", @target[1].target_id)
      assert_equal(nil, @target[2].target_id)
    end

    def test_start
      assert_equal(123, @target[0].start)
      assert_equal(78, @target[1].start)
      assert_nil(@target[2].start)
    end

    def test_end
      assert_equal(456, @target[0].end)
      assert_equal(90, @target[1].end)
      assert_nil(@target[2].end)
    end

    def test_strand
      assert_equal('+', @target[0].strand)
      assert_nil(@target[1].strand)
      assert_nil(@target[2].strand)
    end

    def test_to_s
      assert_equal('ABCD1234 123 456 +', @target[0].to_s)
      assert_equal('%3EX%20Y%3DZ%3BP%25%2CQ%09R 78 90', @target[1].to_s)
      assert_equal('. . .', @target[2].to_s)
    end

  end #class TestGFF3RecordTarget

  class TestGFF3RecordGap < Test::Unit::TestCase
    def setup
      # examples taken from http://song.sourceforge.net/gff3.shtml
      @gaps_src = [ 'M8 D3 M6 I1 M6',
                    'M3 I1 M2 F1 M4',
                    'M3 I1 M2 R1 M4' ]
      @gaps = @gaps_src.collect { |x| Bio::GFF::GFF3::Record::Gap.new(x) }
    end

    def test_to_s
      @gaps_src.each do |src|
        assert_equal(src, @gaps.shift.to_s)
      end
    end

    def test_eqeq
      gap = Bio::GFF::GFF3::Record::Gap.new('M8 D3 M6 I1 M6')
      assert(gap == @gaps[0])
      assert_equal(false, gap == @gaps[1])
    end

    def test_process_sequences_na
      ref = 'CAAGACCTAAACTGGATTCCAAT'
      tgt = 'CAAGACCTCTGGATATCCAAT'
      ref_aligned = 'CAAGACCTAAACTGGAT-TCCAAT'
      tgt_aligned = 'CAAGACCT---CTGGATATCCAAT'
      assert_equal([ ref_aligned, tgt_aligned ],
                   @gaps[0].process_sequences_na(ref, tgt))
    end

    def test_process_sequences_na_tooshort
      ref = 'CAAGACCTAAACTGGATTCCAA'
      tgt = 'CAAGACCTCTGGATATCCAA'
      assert_raise(RuntimeError) { @gaps[0].process_sequences_na(ref, tgt) }
      ref = 'c'
      tgt = 'c'
      assert_raise(RuntimeError) { @gaps[0].process_sequences_na(ref, tgt) }
    end

    def test_process_sequences_na_aa
      ref1 = 'atgaaggaggttattgaatgtcggcggt'
      tgt1 = 'MKEVVINVGG'
      ref1_aligned = 'atgaaggag---gttattgaatgtcggcggt'
      tgt1_aligned = 'M  K  E  V  V  I  >N  V  G  G  '
      assert_equal([ ref1_aligned, tgt1_aligned ],
                   @gaps[1].process_sequences_na_aa(ref1, tgt1))
    end

    def test_process_sequences_na_aa_reverse_frameshift
      ref2 = 'atgaaggaggttataatgtcggcggt'
      tgt2 = 'MKEVVINVGG'
      ref2_aligned = 'atgaaggag---gttat<aatgtcggcggt'
      tgt2_aligned = 'M  K  E  V  V  I  N  V  G  G  '
      assert_equal([ ref2_aligned, tgt2_aligned ],
                   @gaps[2].process_sequences_na_aa(ref2, tgt2))
    end

    def test_process_sequences_na_aa_reverse_frameshift_more
      gap = Bio::GFF::GFF3::Record::Gap.new("M3 R3 M3")
      ref = 'atgaagattaatgtc'
      tgt = 'MKIINV'
      ref_aligned = 'atgaag<<<attaatgtc'
      tgt_aligned = 'M  K  I  I  N  V  '
      assert_equal([ ref_aligned, tgt_aligned ],
                   gap.process_sequences_na_aa(ref, tgt))
    end

    def test_process_sequences_na_aa_tooshort
      ref2 = 'atgaaggaggttataatgtcggcgg'
      tgt2 = 'MKEVVINVG'
      assert_raise(RuntimeError) do
        @gaps[2].process_sequences_na_aa(ref2, tgt2)
      end

      ref2 = 'atg'
      tgt2 = 'M'
      assert_raise(RuntimeError) do
        @gaps[2].process_sequences_na_aa(ref2, tgt2)
      end
    end

    def test___scan_gap
      str1 = 'CAAGACCT---CTGGATATCCAAT'
      str2 = '-aaaaaaa-a-a---ggag--'
      c = Bio::GFF::GFF3::Record::Gap::Code
      data1 = [ c.new(:M, 8), c.new(:I, 3), c.new(:M, 13) ]
      data2 = [ c.new(:I, 1), c.new(:M, 7), c.new(:I, 1), 
                c.new(:M, 1), c.new(:I, 1), c.new(:M, 1),
                c.new(:I, 3), c.new(:M, 4), c.new(:I, 2) ]

      assert_equal(data1, @gaps[0].instance_eval { __scan_gap(str1) })
      assert_equal(data2, @gaps[0].instance_eval { __scan_gap(str2) })
    end

    def test_new_from_sequences_na
      ref_aligned = 'CAAGACCTAAACTGGAT-TCCAAT'
      tgt_aligned = 'CAAGACCT---CTGGATATCCAAT'
      
      assert_equal(@gaps[0], Bio::GFF::GFF3::Record::Gap.new_from_sequences_na(ref_aligned, tgt_aligned))
    end

    def test_new_from_sequences_na_aa
      ref = 'atgaaggag---gttattgaatgtcggcggt'
      tgt = 'M  K  E  V  V  I  >N  V  G  G  '
      assert_equal(@gaps[1],
                   Bio::GFF::GFF3::Record::Gap.new_from_sequences_na_aa(ref,
                                                                        tgt))
    end

    def test_new_from_sequences_na_aa_reverse_frameshift
      ref = 'atgaaggag---gttat<aatgtcggcggt'
      tgt = 'M  K  E  V  V  I  N  V  G  G  '
      assert_equal(@gaps[2],
                   Bio::GFF::GFF3::Record::Gap.new_from_sequences_na_aa(ref,
                                                                        tgt))
    end

    def test_new_from_sequences_na_aa_reverse_frameshift_more
      gap = Bio::GFF::GFF3::Record::Gap.new("M3 R3 M3")
      ref = 'atgaag<<<attaatgtc'
      tgt = 'M  K  I  I  N  V  '
      assert_equal(gap,
                   Bio::GFF::GFF3::Record::Gap.new_from_sequences_na_aa(ref,
                                                                        tgt))
    end

    def test_new_from_sequences_na_aa_boundary_gap
      g = Bio::GFF::GFF3::Record::Gap

      ref = '---atgatg'
      tgt = 'K  M  M  '
      assert_equal(Bio::GFF::GFF3::Record::Gap.new('I1 M2'),
                   g.new_from_sequences_na_aa(ref, tgt))

      ref = 'atgatg---'
      tgt = 'M  M  K  '
      assert_equal(Bio::GFF::GFF3::Record::Gap.new('M2 I1'),
                   g.new_from_sequences_na_aa(ref, tgt))

      ref = 'atgatgatg'
      tgt = '-  M  M  '
      assert_equal(Bio::GFF::GFF3::Record::Gap.new('D1 M2'),
                   g.new_from_sequences_na_aa(ref, tgt))

      ref = 'atgatgatg'
      tgt = 'M  M  -  '
      assert_equal(Bio::GFF::GFF3::Record::Gap.new('M2 D1'),
                   g.new_from_sequences_na_aa(ref, tgt))
    end

    def test_new_from_sequences_na_aa_example
      gap = Bio::GFF::GFF3::Record::Gap.new('M2 R1 M1 F2 M1')
      ref1 = 'atgg-taagac-att'
      tgt1 = 'M  V  K  -  I  '
      ref2 = 'atggt<aagacatt'
      tgt2 = 'M  V  K  >>I  '
      gap1 = Bio::GFF::GFF3::Record::Gap.new_from_sequences_na_aa(ref1, tgt1)
      assert_equal(gap, gap1)
      gap2 = Bio::GFF::GFF3::Record::Gap.new_from_sequences_na_aa(ref2, tgt2)
      assert_equal(gap, gap2)
    end
  end #class TestGFF3RecordGap

  class TestGFF3SequenceRegion < Test::Unit::TestCase

    def setup
      @data =
        [ Bio::GFF::GFF3::SequenceRegion.new('ABCD1234', 123, 456),
          Bio::GFF::GFF3::SequenceRegion.new(">X Y=Z;P%,Q\tR", 78, 90),
          Bio::GFF::GFF3::SequenceRegion.new(nil, nil, nil),
        ]
    end

    def test_parse
      strings = 
        [ '##sequence-region ABCD1234 123 456',
          '##sequence-region %3EX%20Y%3DZ%3BP%25%2CQ%09R 78 90',
          '##sequence-region'
        ]
      @data.each do |reg|
        str = strings.shift
        assert_equal(reg, Bio::GFF::GFF3::SequenceRegion.parse(str))
      end
    end

    def test_seqid
      assert_equal('ABCD1234', @data[0].seqid)
      assert_equal(">X Y=Z;P%,Q\tR", @data[1].seqid)
      assert_equal(nil, @data[2].seqid)
    end

    def test_start
      assert_equal(123, @data[0].start)
      assert_equal(78, @data[1].start)
      assert_nil(@data[2].start)
    end

    def test_end
      assert_equal(456, @data[0].end)
      assert_equal(90, @data[1].end)
      assert_nil(@data[2].end)
    end

    def test_to_s
      assert_equal("##sequence-region ABCD1234 123 456\n", @data[0].to_s)
      assert_equal("##sequence-region %3EX%20Y%3DZ%3BP%25%2CQ%09R 78 90\n",
                   @data[1].to_s)
      assert_equal("##sequence-region . . .\n", @data[2].to_s)
    end

  end #class TestGFF3SequenceRegion

  class TestGFF3MetaData < Test::Unit::TestCase

    def setup
      @data =
        Bio::GFF::GFF3::MetaData.new('feature-ontology',
                                     'http://song.cvs.sourceforge.net/*checkout*/song/ontology/sofa.obo?revision=1.12')
    end

    def test_parse
      assert_equal(@data,
                   Bio::GFF::GFF3::MetaData.parse('##feature-ontology http://song.cvs.sourceforge.net/*checkout*/song/ontology/sofa.obo?revision=1.12'))
    end

    def test_directive
      assert_equal('feature-ontology', @data.directive)
    end

    def test_data
      assert_equal('http://song.cvs.sourceforge.net/*checkout*/song/ontology/sofa.obo?revision=1.12', @data.data)
    end
  end #class TestGFF3MetaData

end #module Bio