| 1 | package Bio::DB::SeqFeature::Store::BedLoader; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Carp 'croak'; | ||
| 5 | use Text::ParseWords 'shellwords'; | ||
| 6 | use Bio::DB::SeqFeature::Store::LoadHelper; | ||
| 7 | |||
| 8 | use base 'Bio::DB::SeqFeature::Store::GFF3Loader'; | ||
| 9 | |||
| 10 | sub create_load_data { #overridden | ||
| 11 | my $self = shift; | ||
| 12 | $self->SUPER::create_load_data; | ||
| 13 | $self->{load_data}{IndexSubfeatures} = $self->index_subfeatures(); | ||
| 14 | $self->{load_data}{TemporaryLoadID} ||= "F0000"; | ||
| 15 | $self->{load_data}{track_conf}{name}||= 'Feature0000'; | ||
| 16 | $self->{load_data}{LoadedTypes} = {}; | ||
| 17 | |||
| 18 | |||
| 19 | $self->{load_data}{Helper} = | ||
| 20 | Bio::DB::SeqFeature::Store::LoadHelper->new($self->{tmpdir}); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub load_line { | ||
| 24 | my $self = shift; | ||
| 25 | my $line = shift; | ||
| 26 | chomp $line; | ||
| 27 | |||
| 28 | # make sure these are always defined | ||
| 29 | if ($line =~ /^track/) { | ||
| 30 | $self->handle_track_conf($line); | ||
| 31 | } elsif ($line) { | ||
| 32 | $self->handle_feature($line); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | sub handle_track_conf { | ||
| 37 | my $self = shift; | ||
| 38 | my $line = shift; | ||
| 39 | my $load_data = $self->{load_data}; | ||
| 40 | my %attributes = $self->parse_track_line($line); | ||
| 41 | $load_data->{track_conf} = \%attributes; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub parse_track_line { | ||
| 45 | my $self = shift; | ||
| 46 | my $line = shift; | ||
| 47 | my @tokens = shellwords($line); | ||
| 48 | shift @tokens; | ||
| 49 | return map {split '='} @tokens; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub handle_feature { | ||
| 53 | my $self = shift; | ||
| 54 | my $line = shift; | ||
| 55 | my ($chrom,$chromStart,$chromEnd, # mandatory | ||
| 56 | $name,$score,$Strand, | ||
| 57 | $thickStart,$thickEnd,$itemRGB, | ||
| 58 | $blockCount,$blockSizes,$blockStarts) = split /\s+/,$line; | ||
| 59 | |||
| 60 | my $ld = $self->{load_data}; | ||
| 61 | my $load_id = $ld->{TemporaryLoadID}++; | ||
| 62 | |||
| 63 | # status reporting | ||
| 64 | if (++$ld->{count} % 1000 == 0) { | ||
| 65 | my $now = $self->time(); | ||
| 66 | my $nl = -t STDOUT && !$ENV{EMACS} ? "\r" : "\n"; | ||
| 67 | local $^W = 0; # kill uninit variable warning | ||
| 68 | $self->msg(sprintf("%d features loaded in %5.2fs (%5.2fs/1000 features)...%s$nl", | ||
| 69 | $ld->{count},$now - $ld->{start_time}, | ||
| 70 | $now - $ld->{millenium_time}, | ||
| 71 | ' ' x 80 | ||
| 72 | )); | ||
| 73 | $ld->{millenium_time} = $now; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | my $isGene = $thickStart || $blockCount; | ||
| 78 | my $method = $isGene ? 'mRNA' : 'region'; | ||
| 79 | my $source = $ld->{track_conf}{name}; # bogus, but works | ||
| 80 | |||
| 81 | $ld->{LoadedTypes}{"$method:$source"}++; | ||
| 82 | |||
| 83 | my $strand = $Strand && $Strand eq '-' ? -1 : +1; | ||
| 84 | my $start = $chromStart+1; | ||
| 85 | my $end = $chromEnd; | ||
| 86 | |||
| 87 | my %attributes; | ||
| 88 | %attributes = (RGB => $itemRGB) if $itemRGB && $itemRGB; | ||
| 89 | |||
| 90 | # create parent feature | ||
| 91 | my @args = (-display_name => $name, | ||
| 92 | -seq_id => $chrom, | ||
| 93 | -start => $start, | ||
| 94 | -end => $end, | ||
| 95 | -strand => $strand, | ||
| 96 | -score => $score, | ||
| 97 | -primary_tag => $method, | ||
| 98 | -source => $source, | ||
| 99 | -tag => \%attributes, | ||
| 100 | -attributes => \%attributes, | ||
| 101 | ); | ||
| 102 | |||
| 103 | my $feature = $self->sfclass->new(@args); | ||
| 104 | $feature->object_store($self->store) if $feature->can('object_store'); # for lazy table features | ||
| 105 | |||
| 106 | $ld->{CurrentFeature} = $feature; | ||
| 107 | $ld->{CurrentID} = $load_id; | ||
| 108 | |||
| 109 | my $helper = $ld->{Helper}; | ||
| 110 | $helper->indexit($load_id=>1); # index toplevel features | ||
| 111 | $helper->toplevel($load_id=>1) if !$self->{fast}; | ||
| 112 | |||
| 113 | $self->store_current_feature(); # this will clear out CurrentFeature and CurrentID | ||
| 114 | |||
| 115 | if ($isGene) { | ||
| 116 | my @children; | ||
| 117 | |||
| 118 | # parts is an array of [start,end,method] | ||
| 119 | my ($parts) = $self->split_gene_bits($chromStart,$chromEnd, | ||
| 120 | $thickStart,$thickEnd, | ||
| 121 | $blockCount,$blockSizes,$blockStarts); | ||
| 122 | for my $u (@$parts) { | ||
| 123 | my $f = $self->sfclass->new(-seq_id => $chrom, | ||
| 124 | -start => $u->[0], | ||
| 125 | -end => $u->[1], | ||
| 126 | -strand => $strand, | ||
| 127 | -source => $source, | ||
| 128 | -score => $score, | ||
| 129 | -primary_tag => $u->[2], | ||
| 130 | -attributes => \%attributes, | ||
| 131 | ); | ||
| 132 | my $id = $ld->{TemporaryLoadID}++; | ||
| 133 | $ld->{CurrentFeature} = $f; | ||
| 134 | $ld->{CurrentID} = $id; | ||
| 135 | $self->store_current_feature(); | ||
| 136 | push @children,$id; | ||
| 137 | } | ||
| 138 | |||
| 139 | # remember parentage using the helper | ||
| 140 | for my $child_id (@children) { | ||
| 141 | $helper->add_children($load_id=>$child_id); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub split_gene_bits { | ||
| 147 | my $self = shift; | ||
| 148 | my ($chromStart,$chromEnd, | ||
| 149 | $thickStart,$thickEnd, | ||
| 150 | $numBlocks,$blockSizes,$blockStarts) = @_; | ||
| 151 | |||
| 152 | # no internal structure, so just create UTRs and one CDS in the middle | ||
| 153 | # remember that BED format uses 0-based indexing, hence the +1s | ||
| 154 | unless ($blockSizes) { | ||
| 155 | my @bits = ([$chromStart+1,$thickStart,'UTR'], | ||
| 156 | [$thickStart+1,$thickEnd,'CDS'], | ||
| 157 | [$thickEnd+1,$chromEnd,'UTR']); | ||
| 158 | return \@bits; | ||
| 159 | } | ||
| 160 | |||
| 161 | # harder -- we have internal exons | ||
| 162 | my @block_sizes = split ',',$blockSizes; | ||
| 163 | my @block_starts = split ',',$blockStarts; | ||
| 164 | croak "Invalid BED file: blockSizes != blockStarts" | ||
| 165 | unless @block_sizes == @block_starts && @block_sizes == $numBlocks; | ||
| 166 | |||
| 167 | my @bits; | ||
| 168 | for (my $i=0;$i<@block_starts;$i++) { | ||
| 169 | my $start = $chromStart + $block_starts[$i]; | ||
| 170 | my $end = $chromStart + $block_starts[$i] + $block_sizes[$i]; | ||
| 171 | |||
| 172 | if ($start < $thickStart) { | ||
| 173 | if ($end < $thickStart) { # UTR wholly contained in an exon | ||
| 174 | push @bits,[$start+1,$end,'UTR']; | ||
| 175 | } | ||
| 176 | elsif ($end >= $thickStart) { # UTR partially contained in an exon | ||
| 177 | push @bits,[$start+1,$thickStart,'UTR']; | ||
| 178 | push @bits,[$thickStart+1,$end,'CDS']; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | elsif ($start < $thickEnd) { | ||
| 183 | if ($end <= $thickEnd) { # CDS wholly contained in an exon | ||
| 184 | push @bits,[$start+1,$end,'CDS']; | ||
| 185 | } | ||
| 186 | elsif ($end > $thickEnd) { # CDS partially contained in an exon | ||
| 187 | push @bits,[$start+1,$thickEnd,'CDS']; | ||
| 188 | push @bits,[$thickEnd+1,$end,'UTR']; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | elsif ($start > $thickEnd) { | ||
| 193 | push @bits,[$start+1,$end,'UTR']; # UTR wholly contained in an exon | ||
| 194 | } | ||
| 195 | |||
| 196 | elsif ($start == $thickEnd) { | ||
| 197 | push @bits,[$start+1,$end,'UTR']; # non-coding gene ?! | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | croak "Programmer error when calculating UTR bounds"; | ||
| 201 | } | ||
| 202 | |||
| 203 | } | ||
| 204 | |||
| 205 | return \@bits; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub loaded_types { | ||
| 209 | my $self = shift; | ||
| 210 | my $ld = $self->{load_data}; | ||
| 211 | return keys %{$ld->{LoadedTypes}}; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; |