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