3 # Copyright (C) 2007-2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # Format a genome creating specified indexes.
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use Maasha::Biopieces;
40 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
43 my ( $default, $formats, $options, $in, $out, $record, $data_out, $entry,
44 $genome, $dir, $fasta_dir, $phastcons_dir, $fh_out, $vals, $format, $tmp_dir );
46 $tmp_dir = Maasha::Biopieces::get_tmpdir();
47 $default = $ENV{ 'BP_DATA' };
48 $formats = 'fasta,blast,vmatch,bowtie,bwa,phastcons';
50 $options = Maasha::Biopieces::parse_options(
52 { long => 'no_stream', short => 'x', type => 'flag', mandatory => 'no', default => undef, allowed => undef, disallowed => undef },
53 { long => 'dir', short => 'd', type => 'dir!', mandatory => 'no', default => $default, allowed => undef, disallowed => undef },
54 { long => 'genome', short => 'g', type => 'string', mandatory => 'yes', default => undef, allowed => undef, disallowed => undef },
55 { long => 'formats', short => 'f', type => 'list', mandatory => 'yes', default => undef, allowed => $formats, disallowed => '0' },
59 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
60 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
62 $dir = $options->{ 'dir' };
63 $genome = $options->{ 'genome' };
65 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes" );
66 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome" );
68 if ( grep { $_ =~ /fasta|blast|vmatch|bowtie|bwa/i } @{ $options->{ "formats" } } )
70 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
72 $fasta_dir = "$dir/genomes/$genome/fasta";
76 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
78 $fasta_dir = "$dir/genomes/$genome/fasta";
80 $fh_out = Maasha::Filesys::file_write_open( "$fasta_dir/$genome.fna" );
83 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
85 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
87 $phastcons_dir = "$dir/genomes/$genome/phastcons";
89 $fh_out = Maasha::Filesys::file_write_open( "$phastcons_dir/$genome.pp" );
92 while ( $record = Maasha::Biopieces::get_record( $in ) )
94 if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
96 Maasha::Fasta::put_entry( $entry, $fh_out );
98 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } ) # TODO: clean this!
100 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
102 $vals = $record->{ 'VALS' };
106 print $fh_out "$vals\n";
109 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
112 close $fh_out if $fh_out;
114 foreach $format ( @{ $options->{ 'formats' } } )
116 print STDERR qq(Creating format: $format for $genome ... ) if $options->{ 'verbose' };
118 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", $fasta_dir, "$genome.index" ) }
119 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
120 elsif ( $format =~ /^blat$/i ) { warn "BLAT FORMAT NOT IMPLEMENTED\n" }
121 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $tmp_dir ) }
122 elsif ( $format =~ /^bowtie$/i ) { Maasha::Bowtie::bowtie_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/bowtie", $genome, $options->{ 'verbose' } ) }
123 elsif ( $format =~ /^bwa$/i ) { Maasha::BWA::bwa_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/bwa", $genome, $options->{ 'verbose' } ) }
124 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
126 print STDERR qq(done.\n) if $options->{ 'verbose' };
129 Maasha::Biopieces::close_stream( $in );
130 Maasha::Biopieces::close_stream( $out );
133 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
138 Maasha::Biopieces::status_set();
144 Maasha::Biopieces::status_log();
148 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<