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

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 133 CC 9 SCAR 21.1
CC
9
Cov
0%
create_conf_file
12.0
load
12.0
finish_load
12.0
CRAP 8.2
1 package Bio::Graphics::Browser2::DataLoader::bam;
2  
3 # $Id$
4 use strict;
5 use base 'Bio::Graphics::Browser2::DataLoader';
6  
7 my $HASBIGWIG;
8  
9 sub create_conf_file {
10 my $self = shift;
11 my $bam_file = shift;
12 my $has_bigwig = shift;
13  
14 my $conf = $self->conf_fh;
15  
16 my $data_path = $self->data_path;
17 my $loadid = $self->loadid;
18 my $tracklabel = $self->new_track_label;
19 my $filename = $self->track_name;
20  
21 # find a fasta file to use
22 my $fasta = $self->get_fasta_file || '';
23 my $category = $self->category;
24  
25 my $sam_db =<<END;
26 [$loadid:database]
27 db_adaptor = Bio::DB::Sam
28 db_args = -bam "$bam_file"
29 -fasta "$fasta"
30 -split_splices 1
31 search options = none
32 END
33  
34 (my $bigwig = $bam_file) =~ s/\.bam$/.bw/;
35  
36 my $bigwig_db = $has_bigwig ? <<BIGWIG : '';
37 [${loadid}_bw:database]
38 db_adaptor = Bio::DB::BigWig
39 db_args = -bigwig "$bigwig"
40 BIGWIG
41  
42 my $sam_track =<<END;
43 [$tracklabel]
44 feature = match
45 glyph = segments
46 draw_target = 1
47 show_mismatch = 1
48 mismatch_color = red
49 database = $loadid
50 bgcolor = blue
51 fgcolor = blue
52 height = 3
53 label = $filename
54 category = $category
55 label density = 50
56 feature_limit = 500
57 bump = fast
58 stranded = 1
59 key = $filename
60 END
61  
62 my $semantic_track = $has_bigwig ? <<BIGWIG : <<ORDINARY;
63 [$tracklabel:499]
64 database = ${loadid}_bw
65 feature = summary
66 glyph = wiggle_whiskers
67 max_color = lightgrey
68 min_color = lightgrey
69 mean_color = black
70 stdev_color = grey
71 stdev_color_neg = grey
72 height = 20
73 BIGWIG
74 [$tracklabel:499]
75 database = $loadid
76 feature = coverage:2000
77 min_score = 0
78 glyph = wiggle_xyplot
79 height = 20
80 fgcolor = blue
81 bgcolor = blue
82 autoscale = local
83 key = $filename
84 ORDINARY
85  
86  
87 print $conf <<END;
88 $sam_db
89  
90 $bigwig_db
91  
92 #>>>>>>>>>> cut here <<<<<<<<
93  
94 $semantic_track
95  
96 $sam_track
97 END
98  
99 }
100  
101 # slightly different behavior -- never return the .fai file - only the first .fa file
102 sub get_fasta_file {
103 my $self = shift;
104 my @fasta = $self->get_fasta_files;
105 my $fasta = (grep {!/\.fai$/} @fasta)[0];
106 return $fasta;
107 }
108  
109 sub load {
110 my $self = shift;
111 my ($initial_lines,$fh) = @_;
112  
113 $self->flag_busy(1);
114 eval {
115 $self->open_conf;
116 $self->set_status('starting load');
117
118 mkdir $self->sources_path or die $!;
119 my $source_file = IO::File->new(
120 File::Spec->catfile($self->sources_path,$self->track_name),'>');
121  
122 $self->start_load;
123  
124 $self->set_status('load data');
125 my $bytes_loaded = 0;
126 foreach (@$initial_lines) {
127 $source_file->print($_);
128 $bytes_loaded += length $_;
129 }
130  
131 my $buffer;
132 while ((my $bytes = read($fh,$buffer,8192) > 0)) {
133 $source_file->print($buffer);
134 $bytes_loaded += length $ buffer;
135 $self->set_status("loaded $bytes_loaded bytes") if $bytes++ % 10000;
136 }
137 $source_file->close();
138  
139 $self->finish_load;
140 $self->close_conf;
141 $self->set_processing_complete;
142 };
143  
144 $self->flag_busy(0);
145 die $@ if $@;
146 return $self->tracks;
147 }
148  
149 sub finish_load {
150 my $self = shift;
151 eval "require Bio::DB::Sam; 1" or return;
152  
153 # keep original copy in sources directory. Create new sorted and indexed
154 # copy in main level
155 my $source = File::Spec->catfile($self->sources_path,$self->track_name);
156 my $dest = File::Spec->catfile($self->data_path,$self->track_name);
157 $dest =~ s/\.[bs]am$//i; # sorting will add the .bam extension
158  
159 # check whether we need to sort or not
160 if ($self->is_sorted($source)) {
161 # bam is already sorted by coordinate, destination will become source
162 $dest = $source;
163 }
164 else {
165 $self->set_status('sorting BAM file');
166 Bio::DB::Bam->sort_core(0,$source,$dest,250*1e6);
167 $dest .= '.bam';
168 }
169  
170 $self->set_status('indexing BAM file');
171 Bio::DB::Bam->index_build($dest);
172  
173 my $bigwig_exists = 0;
174 if ($self->has_bigwig) {
175 $self->set_status('creating BigWig coverage file');
176 $bigwig_exists = $self->create_big_wig();
177 }
178  
179 $self->set_status('creating conf file');
180 $self->create_conf_file($dest,$bigwig_exists);
181 }
182  
183 sub has_bigwig {
184 my $self = shift;
185 return $HASBIGWIG if defined $HASBIGWIG;
186 my $result = eval "require Bio::DB::Sam::SamToGBrowse;1";
187 $result &&= eval "require Bio::DB::BigWig; 1";
188 return $HASBIGWIG = $result;
189 }
190  
191 sub create_big_wig {
192 my $self = shift;
193 my $dir = $self->data_path;
194 my $fasta = $self->get_fasta_file or return;
195 my $wigout = Bio::DB::Sam::SamToGBrowse->new($dir,$fasta,0);
196 $wigout->bam_to_wig($self->chrom_sizes); # this creates the wig files
197 die $wigout->last_error if $wigout->last_error;
198 1;
199 }
200  
201 sub is_sorted {
202 my $self = shift;
203 my $sam = Bio::DB::Sam->new(-bam => shift, -autoindex => 0);
204 my $header = $sam->bam->header->text;
205 return (split /\n/, $header)[0] =~ /SO:coordinate/i;
206 }
207  
208 1;