File: Unflattener.t

package info (click to toggle)
bioperl 1.7.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,888 kB
  • sloc: perl: 94,151; xml: 14,982; makefile: 20
file content (351 lines) | stat: -rw-r--r-- 9,781 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
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
# -*-Perl-*- Test Harness script for Bioperl
# $Id$

use strict;

BEGIN {
    use Bio::Root::Test;

    test_begin(-tests => 21);

    use_ok('Bio::SeqIO');
    use_ok('Bio::SeqFeature::Tools::Unflattener');
}

my $verbosity = test_debug();

my ($seq, @sfs);
my $unflattener = Bio::SeqFeature::Tools::Unflattener->new;
$unflattener->verbose($verbosity);


if (1) {
    my @path = ("NC_000007-ribosomal-slippage.gb");
    $seq = getseq(@path);

    my @topsfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -use_magic => 1);
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED:%d\n", scalar(@sfs);
    }
    is(@sfs, 1995);
}


if (1) {
    my @path = ("ribosome-slippage.gb");
    $seq     = getseq(@path);

    my @topsfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -use_magic => 1);
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED:%d\n", scalar(@sfs);
    }
    is(@sfs, 3);
}


if (1) {
    my @path = ("AE003644_Adh-genomic.gb");
    $seq     = getseq(@path);

    is ($seq->accession_number, 'AE003644');
    my @topsfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -group_tag => 'locus_tag');
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED:%d\n", scalar(@sfs);
    }
    is(@sfs, 21);
}

# now try again, using a custom subroutine to link together features
$seq = getseq("AE003644_Adh-genomic.gb");
@sfs = $unflattener->unflatten_seq
    (-seq       => $seq,
     -group_tag => 'locus_tag',
     -resolver_method =>
        sub {
             my $self = shift;
             my ($sf, @candidate_container_sfs) = @_;

             if ($sf->has_tag('note')) {
                 my @notes   = $sf->get_tag_values('note');
                 my @trnames = map {/from transcript\s+(.*)/; $1;}
                               @notes;
                 @trnames    = grep {$_} @trnames;

                 my $trname;
                 if (@trnames == 0) {
                     $self->throw("UNRESOLVABLE");
                 }
                 elsif (@trnames == 1) {
                     $trname = $trnames[0];
                 }
                 else {
                     $self->throw("AMBIGUOUS: @trnames");
                 }

                 my @container_sfs =
                     grep {
                           my ($product) =
                                 $_->has_tag('product') ? $_->get_tag_values('product')
                               : ('');
                           $product eq $trname;
                     } @candidate_container_sfs;

                 if (@container_sfs == 0) {
                     $self->throw("UNRESOLVABLE");
                 }
                 elsif (@container_sfs == 1) {
                     # we got it!
                     return ($container_sfs[0]=>0);
                 }
                 else {
                     $self->throw("AMBIGUOUS");
                 }
             }
        });
$unflattener->feature_from_splitloc(-seq => $seq);
if( $verbosity > 0 ) {
    warn "\n\nPOST PROCESSING:\n";
    write_hier(@sfs);
    warn sprintf "PROCESSED2:%d\n", scalar(@sfs);
}
is(@sfs, 21);

# try again; different sequence
# this is an E-Coli seq with no mRNA features;
# we just want to link all features directly with gene

$seq = getseq("D10483.gbk");

# UNFLATTEN
@sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                   -partonomy => {'*'=>'gene'});
if( $verbosity > 0 ) {
    warn "\n\nPOST PROCESSING:\n";
    write_hier(@sfs);
    warn sprintf "PROCESSED:%d\n", scalar(@sfs);
}
is(@sfs, 98);

# this sequence has no locus_tag or or gene tags
$seq = getseq("AY763288.gb");

# UNFLATTEN
@sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                   -use_magic => 1);
if( $verbosity > 0 ) {
    warn "\n\nPOST PROCESSING:\n";
    write_hier(@sfs);
    warn sprintf "PROCESSED:%d\n", scalar(@sfs);
}
is(@sfs, 3);

# try again; different sequence - dicistronic gene, mRNA record

$seq = getseq("X98338_Adh-mRNA.gb");

# UNFLATTEN
@sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                   -partonomy => {'*'=>'gene'});
if( $verbosity > 0 ) {
    warn "\n\nPOST PROCESSING:\n";
    write_hier(@sfs);
    warn sprintf "PROCESSED:%d\n", scalar(@sfs);
}
is(@sfs, 7);

# try again; this sequence has no CDSs but rRNA present

$seq = getseq("no_cds_example.gb");

# UNFLATTEN
@sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                   -use_magic => 1);
if( $verbosity > 0 ) {
    warn "\n\nPOST PROCESSING:\n";
    write_hier(@sfs);
    warn sprintf "PROCESSED:%d\n", scalar(@sfs);
}

my @all_sfs = $seq->get_all_SeqFeatures;

my @exons = grep { $_-> primary_tag eq 'exon' } @all_sfs ;

is(@exons, 2);


if (1) {
    # this is an arabidopsise gbk record. it has no mRNA features.
    # it has explicit exon/intron records
    my @path = ("ATF14F8.gbk");
    $seq     = getseq(@path);

    is ($seq->accession_number, 'AL391144');
    my @topsfs = $seq->get_SeqFeatures;
    my @cdss   = grep {$_->primary_tag eq 'CDS'} @topsfs;
    my $n      = scalar(@topsfs);
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -use_magic => 1);
    @sfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED/TOP:%d\n", scalar(@sfs);
    }
    is(@sfs,28);

    my @allsfs = $seq->get_all_SeqFeatures;
    is(@allsfs,202);

    my @mrnas = grep {$_->primary_tag eq 'mRNA'} @allsfs;
    if( $verbosity > 0 ) {
        warn sprintf "ALL:%d\n",   scalar(@allsfs);
        warn sprintf "mRNAs:%d\n", scalar(@mrnas);
    }
    # relationship between mRNA and CDS should be one-one
    is(@mrnas,@cdss);
}


if (1) {
    # this is a record from FlyBase
    # it has mRNA features, and explicit exon/intron records
    my @path = ("AnnIX-v003.gbk");
    $seq     = getseq(@path);

    my @topsfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -use_magic => 1);
    @sfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED/TOP:%d\n", scalar(@sfs);
    }
    is scalar(@sfs), 1;

    my @exons = grep {$_->primary_tag eq 'exon'} $seq->get_all_SeqFeatures;
    is scalar(@exons), 6;    # total number of exons per splice

    my %numberh = map {$_->get_tag_values("number") => 1} @exons;
    my @numbers = keys %numberh;
    if( $verbosity > 0 ) {
        warn sprintf "DISTINCT EXONS: %d [@numbers]\n", scalar(@numbers);
    }
    is scalar(@numbers), 6;  # distinct exons
}


if (1) {
    # example of a BAD genbank entry
    my @path = ("dmel_2Lchunk.gb");
    $seq     = getseq(@path);

    my @topsfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn sprintf "TOP:%d\n", scalar(@topsfs);
        write_hier(@topsfs);
    }

    # UNFLATTEN
    #
    # we EXPECT problems with this erroneous record
    $unflattener->error_threshold(2);
    @sfs = $unflattener->unflatten_seq(-seq       => $seq,
                                       -use_magic => 1);
    @sfs = $seq->get_SeqFeatures;
    if( $verbosity > 0 ) {
        warn "\n\nPOST PROCESSING:\n";
        write_hier(@sfs);
        warn sprintf "PROCESSED/TOP:%d\n", scalar(@sfs);
    }
    is scalar(@sfs), 2;

    my @exons = grep {$_->primary_tag eq 'exon'} $seq->get_all_SeqFeatures;
    is scalar(@exons), 2;    # total number of exons per splice

    my @probs = $unflattener->get_problems;
    $unflattener->report_problems(\*STDERR) if $verbosity > 0;
    $unflattener->clear_problems;
    if( $verbosity > 0 ) {
        warn sprintf "PROBLEMS ENCOUNTERED: %d (EXPECTED: 6)\n", scalar(@probs);
    }
    is scalar(@probs), 6;
}


sub write_hier {
    my @sfs = @_;
    _write_hier(0, @sfs);
}

sub _write_hier {
    my $indent = shift;
    my @sfs = @_;
    foreach my $sf (@sfs) {
        my $label = '?';
        if ($sf->has_tag('gene')) {
            ($label) = $sf->get_tag_values('gene');
        }
        elsif ($sf->has_tag('product')) {
            ($label) = $sf->get_tag_values('product');
        }
        elsif ($sf->has_tag('number')) {
            $label = join("; ", $sf->get_tag_values('number'));
        }
        warn sprintf "%s%s $label\n", '  ' x $indent, $sf->primary_tag;
        my @sub_sfs = $sf->sub_SeqFeature;
        _write_hier($indent+1, @sub_sfs);
    }
}

sub getseq {
    my @path  = @_;
    my $seqio = Bio::SeqIO->new('-file'   => test_input_file(@path),
                                '-format' => 'GenBank');
    $seqio->verbose($verbosity);

    my $seq = $seqio->next_seq();
    return $seq;
}