File: Variation_IO.t

package info (click to toggle)
bioperl 1.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,784 kB
  • ctags: 4,962
  • sloc: perl: 70,732; xml: 3,279; lisp: 107; makefile: 53
file content (152 lines) | stat: -rw-r--r-- 3,442 bytes parent folder | download | duplicates (2)
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
# -*-Perl-*-
## Bioperl Test Harness Script for Modules
## $Id: Variation_IO.t,v 1.13 2001/12/14 17:52:44 jason Exp $

use strict;
use vars qw($NUMTESTS);
BEGIN { 
    eval { require Test; };
    if( $@ ) {
	use lib 't';
    }
    use Test;

    eval { require 'Text/Wrap.pm' };
    if( $@ || $Text::Wrap::VERSION < 98 ) {
	print STDERR "Must have at least Text::Wrap 98 installed\n";
	plan tests => 1;
	ok(1);
	exit(0);
    }
    $NUMTESTS = 25;
    plan tests => $NUMTESTS;
}

use Text::Wrap 98;
use Bio::Variation::IO;
use Bio::Root::IO;

sub fileformat ($) {
    my ($file) = shift;
    my $format;
    if ($file =~ /.*dat$/) {
	$format = 'flat';
    }
    elsif ($file =~ /.*xml$/ ) {
	$format = 'xml';
    } else {
	print "Wrong extension! [$file]";
	exit;
    }
    return $format;
}

sub ext ($) {
    my ($file) = @_;
    my ($name) = $file =~ /.*.(...)$/;
    return $name;
}

sub filename ($) {
    my ($file) = @_;
    my ($name) = $file =~ /(.*)....$/;
    return $name;
}

sub io {
    my ($t_file, $o_file) = @_; 
    my $res;

    my ($t_ext) = ext ($t_file);
    my ($o_ext) = ext ($o_file);
    my ($t_format) = fileformat ($t_file);
    my ($o_format) = fileformat ($o_file);
    my ($t_name) = filename($t_file);
    my ($o_name) = filename($o_file);

    my( $before );
    {
        local $/ = undef;
        local *BEFORE;
        open BEFORE, "$t_name.$o_ext";
        $before = <BEFORE>;
        close BEFORE;
    }

    ok $before;#"Error in reading input file [$t_name.$o_ext]";

    my $in = Bio::Variation::IO->new( -file => $t_file);
    my  @entries ;
    while (my $e = $in->next) {
        push @entries, $e;
    }
    my $count = scalar @entries;
    ok @entries > 0;# "No SeqDiff objects [$count]";
    
    my $out = Bio::Variation::IO->new( -FILE => "> $o_file", 
				       -FORMAT => $o_format);
    my $out_ok = 1;
    foreach my $e (@entries) {
        $out->write($e) or $out_ok = 0;
    }
    undef($out);  # Flush to disk
    ok $out_ok;#  "error writing variants";

    my( $after );
    {
        local $/ = undef;
        local *AFTER;
        open AFTER, $o_file;
        $after = <AFTER>;
        close AFTER;
    }

    ok $after;# "Error in reading in again the output file [$o_file]";

    ok $before, $after, "test output file differs from input";
    print STDERR `diff $t_file $o_file` if $before ne $after;
    unlink($o_file); 
}




io  (Bio::Root::IO->catfile("t","data","mutations.dat"), 
   Bio::Root::IO->catfile("t","data","mutations.out.dat")); #1..5

io  (Bio::Root::IO->catfile("t","data","polymorphism.dat"), 
   Bio::Root::IO->catfile("t","data","polymorphism.out.dat")); #6..10


eval {
    require Bio::Variation::IO::xml;
};

if( $@ ) {
    print STDERR
	 "\nThe XML-format conversion requires the CPAN modules ",
	 "XML::Twig, XML::Writer, and IO::String to be installed ",
	 "on your system, which they probably aren't. Skipping these tests.\n";
    for( $Test::ntest..$NUMTESTS) {
	 skip("No XML::Twig installed", 1);
    }
    exit(0);
}

eval {
    io  (Bio::Root::IO->catfile("t","data","mutations.xml"), 
	Bio::Root::IO->catfile("t","data","mutations.out.xml")); #10..12
};

eval {
    io  (Bio::Root::IO->catfile("t","data","polymorphism.xml"), 
	Bio::Root::IO->catfile("t","data","polymorphism.out.xml")); #13..14
};


eval { 
	 io  (Bio::Root::IO->catfile("t","data","mutations.dat"), 
	    Bio::Root::IO->catfile("t","data","mutations.out.xml")); #15..25
};