]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/format_genome
removed variables bulk type
[biopieces.git] / bp_bin / format_genome
1 #!/usr/bin/env perl -w
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 strict;
30 use Maasha::Fasta;
31 use Maasha::Biopieces;
32
33
34 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
35
36
37 my ( $default, $formats, $options, $in, $out, $record, $data_out, $entry,
38      $genome, $dir, $fasta_dir, $phastcons_dir, $fh_out, $vals, $format, $tmp_dir );
39
40 $tmp_dir = Maasha::Biopieces::get_tmpdir();
41 $default = $ENV{ 'BP_DATA' };
42 $formats = 'fasta,blast,vmatch,phastcons';
43
44 $options = Maasha::Biopieces::parse_options(
45     [
46         { long => 'no_stream', short => 'x', type => 'flag',   mandatory => 'no',  default => undef,    allowed => undef,    disallowed => undef },
47         { long => 'dir',       short => 'd', type => 'dir!',   mandatory => 'no',  default => $default, allowed => undef,    disallowed => undef },
48         { long => 'genome',    short => 'g', type => 'string', mandatory => 'yes', default => undef,    allowed => undef,    disallowed => undef },
49         { long => 'formats',   short => 'f', type => 'list',   mandatory => 'yes', default => undef,    allowed => $formats, disallowed => '0' },
50     ]   
51 );
52
53 $in  = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
54 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
55
56 $dir    = $options->{ 'dir' };
57 $genome = $options->{ 'genome' };
58
59 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes" );
60 Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome" );
61
62 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
63 {
64     if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
65     {
66         $fasta_dir = "$dir/genomes/$genome/fasta";
67     }
68     else
69     {
70         Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
71
72         $fasta_dir = "$dir/genomes/$genome/fasta";
73
74         $fh_out = Maasha::Filesys::file_write_open( "$fasta_dir/$genome.fna" );
75     }
76 }
77 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
78 {
79     Maasha::Filesys::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
80
81     $phastcons_dir = "$dir/genomes/$genome/phastcons";
82
83     $fh_out = Maasha::Filesys::file_write_open( "$phastcons_dir/$genome.pp" );
84 }
85
86 while ( $record = Maasha::Biopieces::get_record( $in ) ) 
87 {
88     if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
89     {
90         Maasha::Fasta::put_entry( $entry, $fh_out );
91     }
92     elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
93     {
94         print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
95
96         $vals = $record->{ 'VALS' };
97
98         $vals =~ tr/,/\n/;
99
100         print $fh_out "$vals\n";
101     }
102
103     Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
104 }
105
106 foreach $format ( @{ $options->{ 'formats' } } )
107 {
108     if    ( $format =~ /^fasta$/i )     { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
109     elsif ( $format =~ /^blast$/i )     { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
110     elsif ( $format =~ /^blat$/i )      { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
111     elsif ( $format =~ /^vmatch$/i )    { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $tmp_dir ) }
112     elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
113 }
114
115 close $fh_out if $fh_out;
116
117
118 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
119
120
121 BEGIN
122 {
123     $run_time_beg = Maasha::Biopieces::run_time();
124
125     Maasha::Biopieces::log_biopiece();
126 }
127
128 END
129 {
130     Maasha::Biopieces::close_stream( $in );
131     Maasha::Biopieces::close_stream( $out );
132
133     $run_time_end = Maasha::Biopieces::run_time();
134
135     Maasha::Biopieces::run_time_print( $run_time_beg, $run_time_end, $options );
136 }
137
138
139 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
140
141
142 __END__