blib/lib/Bio/Graphics/Browser2/DataLoader/generic.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 241 CC 33 SCAR 47.4
CC
33
Cov
0%
finish_load
210.0
_state_transition
182.0
load_line
30.0
CRAP 114.8
1 package Bio::Graphics::Browser2::DataLoader::generic;
2  
3 # $Id$
4 use strict;
5 use Bio::DB::SeqFeature::Store;
6 use Carp 'croak';
7 use File::Basename 'basename';
8 use base 'Bio::Graphics::Browser2::DataLoader';
9  
10 use constant TOO_SMALL_FOR_SUMMARY_MODE => 10000; # don't go into summary mode
11  
12 my @COLORS = qw(blue red orange brown mauve peach green cyan
13 black yellow cyan papayawhip coral);
14  
15 my $DUMPING_FIXED; # flag that we patched Bio::SeqFeature::Lite
16  
17 sub start_load {
18 my $self = shift;
19 my $track_name = $self->track_name;
20 my $data_path = $self->data_path;
21  
22 my $db = $self->create_database($data_path);
23 my $loader_class = $self->Loader;
24 my $fast = $self->do_fast;
25 eval "require $loader_class" unless $loader_class->can('new');
26 my $loader = $loader_class->new(-store=> $db,
27 -fast => $fast,
28 -summary_stats => 1,
29 -verbose => 0,
30 -index_subfeatures=> 0,
31 );
32 $loader->start_load();
33 $self->{loader} = $loader;
34 $self->{conflines} = [];
35 $self->state('starting');
36 }
37  
38 sub do_fast { 1 };
39  
40 sub Loader {
41 croak "The Loader() class method must be implemented in a subclass";
42 }
43  
44 sub finish_load {
45 my $self = shift;
46 my $line_count = shift;
47  
48 $self->set_status('creating database');
49 $self->loader->finish_load();
50 my $db = $self->loader->store;
51 my $conf = $self->conf_fh;
52 my $trackname = $self->track_name;
53 my $dsn = $self->dsn;
54 my $backend = $self->backend;
55  
56 $self->write_gff3($db,$dsn) if $backend eq 'memory';
57  
58 my $trackno = 0;
59 my $loadid = $self->loadid;
60 my $inhibit_summary = $line_count < TOO_SMALL_FOR_SUMMARY_MODE;
61 eval {
62 $self->set_status('calculating summary statistics');
63 $self->loader->build_summary;
64 } unless $inhibit_summary;
65 warn $@ if $@;
66  
67 $self->set_status('creating configuration');
68
69 print $conf <<END;
70 [$loadid:database]
71 db_adaptor = Bio::DB::SeqFeature::Store
72 db_args = -adaptor $backend
73 -dsn $dsn
74 search options = default +wildcard +stem
75  
76 #>>>>>>>>>> cut here <<<<<<<<
77 END
78  
79 if (my @lines = @{$self->{conflines}}) { # good! user has provided some config hints
80 my ($old_trackname,$seen_feature);
81 my $category = $self->category;
82 for my $line (@lines) {
83 chomp $line;
84 if ($line =~ /^\s*database/) {
85 next; # disallowed
86 }
87 elsif ($line =~ /^\[([^\]]+)\]/) { # overwrite track names to avoid collisions
88 $old_trackname = $1;
89 undef $seen_feature;
90 my $trackname = $self->new_track_label;
91 print $conf "[$trackname]\n";
92 print $conf "database = $loadid\n" ;
93 print $conf "category = $category\n";
94 print $conf "show summary = 0\n" if $inhibit_summary;
95 } elsif ($line =~ /^feature/) {
96 $seen_feature++;
97 print $conf $line,"\n";
98 } elsif ($line !~ /\S/ && $old_trackname && !$seen_feature) {
99 print $conf "feature = $old_trackname\n\n";
100 undef $old_trackname;
101 undef $seen_feature;
102 } else {
103 print $conf $line,"\n";
104 }
105 }
106 if ($old_trackname && !$seen_feature) {
107 print $conf "feature = $old_trackname\n\n";
108 }
109 } else { # make something up
110 my @types = eval {$db->toplevel_types};
111 @types = $db->types unless @types;
112  
113 my $filename = $self->track_name;
114 for my $t (@types) {
115 my $trackname = $self->new_track_label($t);
116  
117 my ($glyph,$stranded);
118 # start of a big heuristic section
119 if ($t =~ /gene|cds|exon|mRNA|transcript/i) {
120 $glyph = 'gene';
121 $stranded = 0;
122 } else {
123 $glyph = 'segments';
124 $stranded = 1;
125 }
126  
127 my $color = $COLORS[rand @COLORS];
128 my $category = $self->category;
129 my $summary = $inhibit_summary ? 'show summary = 0' : '';
130 print $conf <<END;
131 [$trackname]
132 database = $loadid
133 feature = $t
134 glyph = $glyph
135 bgcolor = $color
136 fgcolor = $color
137 label = 1
138 stranded = $stranded
139 connector = solid
140 balloon hover = \$description
141 category = $category
142 key = $t
143 $summary
144  
145 END
146 }
147 }
148 }
149  
150 sub loader {shift->{loader}}
151 sub state {
152 my $self = shift;
153 my $d = $self->{state};
154 $self->{state} = shift if @_;
155 $d;
156 }
157 sub load_line {
158 my $self = shift;
159 my $line = shift;
160  
161 my $old_state = $self->state;
162 my $state = $self->_state_transition($old_state,$line);
163 my $prefix = $self->strip_prefix;
164  
165 if ($state eq 'data') {
166 $line =~ s/^$prefix// if $prefix;
167 $self->loader->load_line($line);
168 } elsif ($state eq 'config') {
169 push @{$self->{conflines}},$line;
170 } else {
171 # ignore it
172 }
173 $self->state($state) if $state ne $old_state;
174 }
175  
176 # This is called to save out the feature file in GFF3 format,
177 # which is needed when using the memory backend.
178 sub write_gff3 {
179 my $self = shift;
180 my ($db,$path) = @_;
181 $self->fix_memory_dumping;
182 $self->set_status('writing GFF3 file');
183 $path .= "/data.gff3";
184 open my $f,">",$path or die "Can't open $path: $!";
185 print $f "##gff-version 3\n";
186 print $f "##Index-subfeatures 0\n\n";
187 my $i = $db->get_seq_stream;
188 while (my $feature = $i->next_seq) {
189 print $f $feature->gff3_string(1),"\n";
190 }
191 close $f;
192 }
193  
194 # This is a hack to patch errors in bioperl feature file loading
195 sub fix_memory_dumping {
196 my $self = shift;
197 return if $DUMPING_FIXED++;
198 *Bio::SeqFeature::Lite::gff3_string = eval <<'END';
199 sub {
200 my ($self,$recurse,$parent_tree,$seenit,$force_id) = @_;
201 $parent_tree ||= {};
202 $seenit ||= {};
203 my @rsf = ();
204 my @parent_ids;
205  
206 if ($recurse) {
207 $self->_traverse($parent_tree) unless %$parent_tree; # this will record parents of all children
208 my $primary_id = defined $force_id ? $force_id : $self->_real_or_dummy_id;
209  
210 @rsf = $self->get_SeqFeatures;
211 return if $seenit->{$primary_id}++;
212 @parent_ids = keys %{$parent_tree->{$primary_id}};
213 }
214  
215 my $group = $self->format_attributes(\@parent_ids,$force_id);
216 my $name = $self->name;
217  
218 my $class = $self->class;
219 my $strand = ('-','.','+')[$self->strand+1];
220 my $p = join("\t",
221 $self->seq_id||'.',
222 $self->source||'.',
223 $self->method||'.',
224 $self->start||'.',
225 $self->stop||'.',
226 defined($self->score) ? $self->score : '.',
227 $strand||'.',
228 defined($self->phase) ? $self->phase : '.',
229 $group||'');
230 return join("\n",
231 $p,
232 map {$_->gff3_string(1,$parent_tree,$seenit)} @rsf);
233 }
234 END
235 }
236  
237 # shamelessly copied from Bio::Graphics::FeatureFile.
238 sub _state_transition {
239 my $self = shift;
240 my ($current_state,$line) = @_;
241  
242 if ($current_state eq 'starting') {
243 return $current_state unless /\S/;
244 $current_state = 'config';
245 }
246  
247 if ($current_state eq 'data') {
248 return 'config' if $line =~ m/^\s*\[([^\]]+)\]/; # start of a configuration section
249 }
250 elsif ($current_state eq 'config') {
251 return 'data' if $line =~ /^\#\#(\w+)/; # GFF3 meta instruction
252 return 'data' if $line =~ /^reference\s*=/; # feature-file reference sequence directive
253
254 return 'config' if $line =~ /^\s*$/; #empty line
255 return 'config' if $line =~ m/^\[([^\]]+)\]/; # section beginning
256 return 'config' if $line =~ m/^[\w\s]+=/; # arg = value
257 return 'config' if $line =~ m/^\s+(.+)/; # continuation line
258 return 'config' if $line =~ /^\#/; # comment -not a meta
259 return 'data';
260 }
261 return $current_state;
262 }
263 1;