]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/format_genome
added verbose stuff to format_genome
[biopieces.git] / bp_bin / format_genome
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2007-2009 Martin A. Hansen.
4
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.
9
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.
14
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.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # Format a genome creating specified indexes.
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 use warnings;
30 use strict;
31 use Maasha::Fasta;
32 use Maasha::Biopieces;
33 use Maasha::NCBI;
34 use Maasha::Match;
35 use Maasha::UCSC;
36
37
38 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
39
40
41 my ( $default, $formats, $options, $in, $out, $record, $data_out, $entry,
42      $genome, $dir, $fasta_dir, $phastcons_dir, $fh_out, $vals, $format, $tmp_dir );
43
44 $tmp_dir = Maasha::Biopieces::get_tmpdir();
45 $default = $ENV{ 'BP_DATA' };
46 $formats = 'fasta,blast,vmatch,bowtie,phastcons';
47
48 $options = Maasha::Biopieces::parse_options(
49     [
50         { long => 'no_stream', short => 'x', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,    disallowed => undef },
51         { long => 'dir',       short => 'd', type => 'dir!',   mandatory => 'no',  default => $default, allowed => undef,    disallowed => undef },
52         { long => 'genome',    short => 'g', type => 'string', mandatory => 'yes', default => undef,    allowed => undef,    disallowed => undef },
53         { long => 'formats',   short => 'f', type => 'list',   mandatory => 'yes', default => undef,    allowed => $formats, disallowed => '0' },
54     ]   
55 );
56
57 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
58 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
59
60 $dir    = $options->{ 'dir' };
61 $genome = $options->{ 'genome' };
62
63 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes" );
64 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome" );
65
66 if ( grep { $_ =~ /fasta|blast|vmatch|bowtie/i } @{ $options->{ "formats" } } )
67 {
68     if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
69     {
70         $fasta_dir = "$dir/genomes/$genome/fasta";
71     }
72     else
73     {
74         Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
75
76         $fasta_dir = "$dir/genomes/$genome/fasta";
77
78         $fh_out = Maasha::Filesys::file_write_open( "$fasta_dir/$genome.fna" );
79     }
80 }
81 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
82 {
83     Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
84
85     $phastcons_dir = "$dir/genomes/$genome/phastcons";
86
87     $fh_out = Maasha::Filesys::file_write_open( "$phastcons_dir/$genome.pp" );
88 }
89
90 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
91 {
92     if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
93     {
94         Maasha::Fasta::put_entry( $entry, $fh_out );
95     }
96     elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
97     {
98         print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
99
100         $vals = $record->{ 'VALS' };
101
102         $vals =~ tr/,/\n/;
103
104         print $fh_out "$vals\n";
105     }
106
107     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
108 }
109
110 foreach $format ( @{ $options->{ 'formats' } } )
111 {
112     print STDERR qq(Creating format: $format for $genome ... ) if $options->{ 'verbose' };
113
114     if    ( $format =~ /^fasta$/i )     { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
115     elsif ( $format =~ /^blast$/i )     { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
116     elsif ( $format =~ /^blat$/i )      { warn "BLAT FORMAT NOT IMPLEMENTED" }
117     elsif ( $format =~ /^vmatch$/i )    { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $tmp_dir ) }
118     elsif ( $format =~ /^bowtie$/i )    { bowtie_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/bowtie", $genome, $options->{ 'verbose' } ) }
119     elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
120
121     print STDERR qq(done.\n) if $options->{ 'verbose' };
122 }
123
124 close $fh_out if $fh_out;
125
126 Maasha::Biopieces::close_stream( $in );
127 Maasha::Biopieces::close_stream( $out );
128
129
130 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
131
132
133 sub bowtie_index
134 {
135     # Martin A. Hansen, July 2009.
136     
137     # Create a bowtie index for fast sequence mapping.
138
139     my ( $src_file,   # filename of source file
140          $dst_dir,    # destination dir to store index
141          $base_name,  # base name of index
142          $verbose,    # verbose flag
143        ) = @_;
144
145     Maasha::Filesys::dir_create_if_not_exists( $dst_dir );
146
147     if ( $verbose ) {
148         Maasha::Common::run( "bowtie-build", "$src_file $dst_dir/$base_name" );
149     } else {
150         Maasha::Common::run( "bowtie-build", "$src_file $dst_dir/$base_name > /dev/null 2>&1" );
151     }
152 }
153
154
155 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
156
157
158 BEGIN
159 {
160     Maasha::Biopieces::status_set();
161 }
162
163
164 END
165 {
166     Maasha::Biopieces::status_log();
167 }
168
169
170 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
171
172
173 __END__