« blib/lib/Bio/Graphics/Browser2/CachedTrack.pm
↑ Summary
blib/lib/Bio/Graphics/Browser2/Render/Slave/Status.pm »
| 1 | package Bio::Graphics::Browser2::DataLoader::sam; | ||
| 2 | |||
| 3 | # $Id: bam.pm 22336 2009-12-07 22:18:43Z lstein $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base 'Bio::Graphics::Browser2::DataLoader::bam'; | ||
| 7 | use Bio::DB::Sam; | ||
| 8 | |||
| 9 | # go back to line-at-a-time loading | ||
| 10 | sub load { | ||
| 11 | my $self = shift; | ||
| 12 | $self->Bio::Graphics::Browser2::DataLoader::load(@_); | ||
| 13 | } | ||
| 14 | |||
| 15 | # We do nothing here; the base class | ||
| 16 | # makes a copy of the data file in SOURCES | ||
| 17 | sub load_line { } | ||
| 18 | |||
| 19 | sub finish_load { | ||
| 20 | my $self = shift; | ||
| 21 | my $source_file = File::Spec->catfile($self->sources_path,$self->track_name); | ||
| 22 | |||
| 23 | # sort out the file names | ||
| 24 | my $track_name = $self->track_name; | ||
| 25 | $track_name =~ s/\..+$//; # get rid of any extension | ||
| 26 | |||
| 27 | my $base = File::Spec->catfile($self->data_path,$track_name); | ||
| 28 | my $bam = $base . '.bam'; | ||
| 29 | my $sorted = $base . '_sorted'; | ||
| 30 | |||
| 31 | # turn SAM file into a BAM file | ||
| 32 | $self->set_status('converting SAM to BAM'); | ||
| 33 | $self->do_strip_prefix($source_file); | ||
| 34 | |||
| 35 | $self->sam2bam($source_file,$bam); | ||
| 36 | |||
| 37 | # sort it | ||
| 38 | $self->set_status('sorting BAM file'); | ||
| 39 | Bio::DB::Bam->sort_core(0,$bam,$sorted,250*1e6); # use 200 meg core maximum | ||
| 40 | |||
| 41 | # no need to keep sorted and unsorted copies | ||
| 42 | rename "$sorted.bam",$bam; | ||
| 43 | |||
| 44 | $self->set_status('indexing BAM file'); | ||
| 45 | |||
| 46 | Bio::DB::Bam->index_build($bam); | ||
| 47 | |||
| 48 | my $bigwig_exists = 0; | ||
| 49 | |||
| 50 | if ($self->has_bigwig) { | ||
| 51 | $self->set_status('creating BigWig coverage file'); | ||
| 52 | $bigwig_exists = $self->create_big_wig(); | ||
| 53 | } | ||
| 54 | |||
| 55 | $self->set_status('creating conf file'); | ||
| 56 | $self->create_conf_file($bam,$bigwig_exists); | ||
| 57 | |||
| 58 | $self->set_status('recompressing source'); | ||
| 59 | system ("gzip $source_file"); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub do_strip_prefix { | ||
| 63 | my $self = shift; | ||
| 64 | my $source_file = shift; | ||
| 65 | my $prefix = $self->strip_prefix or return; | ||
| 66 | my $dest = "$source_file.new"; | ||
| 67 | open my $i,'<',$source_file or die "Can't open $source_file: $!"; | ||
| 68 | open my $o,">",$dest or die "Can't open $dest: $!"; | ||
| 69 | while (<$i>) { | ||
| 70 | next if /^\@/; | ||
| 71 | s/^([^\t]+)\t([^\t]+)\t$prefix([^\t]+)/$1\t$2\t$3/; | ||
| 72 | } continue { | ||
| 73 | print $o $_; | ||
| 74 | } | ||
| 75 | close $o; | ||
| 76 | close $i; | ||
| 77 | rename $dest,$source_file; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub sam2bam { | ||
| 81 | my $self = shift; | ||
| 82 | my ($sampath,$bampath) = @_; | ||
| 83 | |||
| 84 | my $tam = Bio::DB::Tam->open($sampath) | ||
| 85 | or die "Could not open SAM file for reading: $!"; | ||
| 86 | |||
| 87 | # get chromosome names | ||
| 88 | my $chroms = $self->chromosome_list; | ||
| 89 | |||
| 90 | my $header = eval {$tam->header_read}; | ||
| 91 | unless ($header) { | ||
| 92 | warn "Bio::DB::Sam version 1.20 or greater required for SAM conversion to work properly"; | ||
| 93 | } | ||
| 94 | |||
| 95 | unless ($header && $header->n_targets > 0) { | ||
| 96 | my $fasta = $self->get_fasta_file; | ||
| 97 | $fasta | ||
| 98 | or die "Could not find a suitable reference FASTA file for indexing this SAM file"; | ||
| 99 | |||
| 100 | my $fai = Bio::DB::Sam::Fai->load($fasta) | ||
| 101 | or die "Could not load reference FASTA file for indexing this SAM file: $!"; | ||
| 102 | |||
| 103 | $header = $tam->header_read2($fasta.".fai"); | ||
| 104 | } | ||
| 105 | |||
| 106 | my $targets = $header->target_name; | ||
| 107 | |||
| 108 | my $bam = Bio::DB::Bam->open($bampath,'w') | ||
| 109 | or die "Could not open BAM file for writing: $!"; | ||
| 110 | |||
| 111 | $bam->header_write($header); | ||
| 112 | my $alignment = Bio::DB::Bam::Alignment->new(); | ||
| 113 | my $lines = 0; | ||
| 114 | |||
| 115 | while ($tam->read1($header,$alignment) > 0) { | ||
| 116 | next if $chroms && !$chroms->{$targets->[$alignment->tid]}; | ||
| 117 | $bam->write1($alignment); | ||
| 118 | $self->set_status("converted $lines lines...") if $lines++%1000 == 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | undef $tam; | ||
| 122 | undef $bam; | ||
| 123 | } | ||
| 124 | |||
| 125 | 1; |