1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
77 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
80 $SIG{ '__DIE__' } = \&sig_handler;
81 $SIG{ 'INT' } = \&sig_handler;
82 $SIG{ 'TERM' } = \&sig_handler;
85 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
88 my ( $script, $BP_TMP );
90 $script = Maasha::Common::get_scriptname();
91 $BP_TMP = Maasha::Common::get_tmpdir();
94 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
97 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
98 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
100 $log_global->autoflush( 1 );
101 $log_local->autoflush( 1 );
103 &log( $log_global, $script, \@ARGV );
104 &log( $log_local, $script, \@ARGV );
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
113 run_script( $script );
116 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121 # Martin A. Hansen, January 2008.
123 # Log messages to logfile.
125 my ( $fh, # filehandle to logfile
126 $script, # script name
127 $argv, # reference to @ARGV
132 my ( $time_stamp, $user );
134 $time_stamp = Maasha::Common::time_stamp();
136 $user = $ENV{ 'USER' };
138 $script = "biopieces" if $script eq "-e";
140 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
146 # Martin A. Hansen, August 2007.
148 # Run a specific script.
150 my ( $script, # script name
155 my ( $t0, $t1, $options, $in, $out );
157 $t0 = gettimeofday();
159 $options = get_options( $script );
161 $options->{ "SCRIPT" } = $script;
163 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
164 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
167 $in = read_stream( $options->{ "stream_in" } );
168 $out = write_stream( $options->{ "stream_out" } );
170 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
171 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
172 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
173 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
174 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
175 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
176 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
177 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
178 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
179 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
180 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
181 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
182 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
183 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
184 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
185 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
186 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
187 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
188 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
189 elsif ( $script eq "assemble_tag_contigs" ) { script_assemble_tag_contigs( $in, $out, $options ) }
190 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
191 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
192 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
193 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
194 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
195 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
196 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
197 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
198 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
199 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
200 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
201 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
202 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
203 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
204 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
205 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
206 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
207 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
209 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
211 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
212 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
213 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
214 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
215 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
216 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
217 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
218 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
219 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
220 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
221 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
222 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
223 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
224 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
225 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
226 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
227 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
228 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
229 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
230 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
231 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
232 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
233 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
234 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
235 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
236 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
237 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
238 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
239 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
240 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
241 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
242 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
243 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
244 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
245 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
246 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
247 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
248 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
249 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
250 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
251 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
252 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
253 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
254 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
255 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
256 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
257 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
258 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
259 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
260 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
261 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
262 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
263 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
264 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
265 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
266 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
267 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
269 close $in if defined $in;
272 $t1 = gettimeofday();
274 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
280 # Martin A. Hansen, February 2008.
282 # Gets options from commandline and checks these vigerously.
284 my ( $script, # name of script
289 my ( %options, @options, $opt, @genomes, $real );
291 if ( $script eq "print_usage" )
297 elsif ( $script eq "read_fasta" )
304 elsif ( $script eq "read_tab" )
315 elsif ( $script eq "read_psl" )
322 elsif ( $script eq "read_bed" )
329 elsif ( $script eq "read_fixedstep" )
336 elsif ( $script eq "read_blast_tab" )
343 elsif ( $script eq "read_embl" )
353 elsif ( $script eq "read_stockholm" )
360 elsif ( $script eq "read_phastcons" )
371 elsif ( $script eq "read_soft" )
379 elsif ( $script eq "read_gff" )
386 elsif ( $script eq "read_2bit" )
394 elsif ( $script eq "read_solexa" )
403 elsif ( $script eq "read_solid" )
411 elsif ( $script eq "read_mysql" )
420 elsif ( $script eq "read_ucsc_config" )
427 elsif ( $script eq "format_genome" )
436 elsif ( $script eq "length_seq" )
443 elsif ( $script eq "oligo_freq" )
450 elsif ( $script eq "create_weight_matrix" )
456 elsif ( $script eq "calc_fixedstep" )
463 elsif ( $script eq "transliterate_seq" )
471 elsif ( $script eq "transliterate_vals" )
480 elsif ( $script eq "translate_seq" )
486 elsif ( $script eq "extract_seq" )
494 elsif ( $script eq "get_genome_seq" )
506 elsif ( $script eq "get_genome_align" )
517 elsif ( $script eq "get_genome_phastcons" )
528 elsif ( $script eq "split_seq" )
535 elsif ( $script eq "split_bed" )
542 elsif ( $script eq "tile_seq" )
549 elsif ( $script eq "invert_align" )
555 elsif ( $script eq "patscan_seq" )
566 elsif ( $script eq "create_blast_db" )
573 elsif ( $script eq "blast_seq" )
585 elsif ( $script eq "blat_seq" )
597 elsif ( $script eq "soap_seq" )
608 elsif ( $script eq "match_seq" )
615 elsif ( $script eq "create_vmatch_index" )
623 elsif ( $script eq "vmatch_seq" )
634 elsif ( $script eq "write_fasta" )
643 elsif ( $script eq "write_align" )
653 elsif ( $script eq "write_blast" )
662 elsif ( $script eq "write_tab" )
674 elsif ( $script eq "write_bed" )
682 elsif ( $script eq "write_psl" )
690 elsif ( $script eq "write_fixedstep" )
698 elsif ( $script eq "write_2bit" )
706 elsif ( $script eq "write_solid" )
715 elsif ( $script eq "write_ucsc_config" )
722 elsif ( $script eq "plot_seqlogo" )
729 elsif ( $script eq "plot_phastcons_profiles" )
744 elsif ( $script eq "analyze_vals" )
751 elsif ( $script eq "head_records" )
757 elsif ( $script eq "remove_keys" )
764 elsif ( $script eq "remove_adaptor" )
773 elsif ( $script eq "remove_mysql_tables" )
784 elsif ( $script eq "remove_ucsc_tracks" )
796 elsif ( $script eq "rename_keys" )
802 elsif ( $script eq "uniq_vals" )
809 elsif ( $script eq "merge_vals" )
816 elsif ( $script eq "merge_records" )
823 elsif ( $script eq "grab" )
838 elsif ( $script eq "compute" )
844 elsif ( $script eq "add_ident" )
851 elsif ( $script eq "count_records" )
858 elsif ( $script eq "random_records" )
864 elsif ( $script eq "sort_records" )
871 elsif ( $script eq "count_vals" )
877 elsif ( $script eq "plot_histogram" )
890 elsif ( $script eq "plot_lendist" )
902 elsif ( $script eq "plot_chrdist" )
913 elsif ( $script eq "plot_karyogram" )
922 elsif ( $script eq "plot_matches" )
934 elsif ( $script eq "length_vals" )
940 elsif ( $script eq "sum_vals" )
948 elsif ( $script eq "mean_vals" )
956 elsif ( $script eq "median_vals" )
964 elsif ( $script eq "max_vals" )
972 elsif ( $script eq "min_vals" )
980 elsif ( $script eq "upload_to_ucsc" )
1004 # print STDERR Dumper( \@options );
1011 # print STDERR Dumper( \%options );
1013 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
1014 return wantarray ? %options : \%options;
1017 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
1018 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
1019 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
1020 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
1021 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
1022 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
1023 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
1024 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
1025 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
1026 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
1027 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
1029 # ---- check arguments ----
1031 if ( $options{ 'data_in' } )
1033 $options{ "files" } = getopt_files( $options{ 'data_in' } );
1035 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
1038 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
1040 # print STDERR Dumper( \%options );
1042 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1044 foreach $opt ( keys %options )
1046 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1048 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1050 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1052 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1054 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1056 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1058 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1060 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1062 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1064 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1066 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1068 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1070 elsif ( $opt eq "genome" and $script ne "format_genome" )
1072 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1073 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1075 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1076 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1079 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1081 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1083 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1085 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1087 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1089 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1091 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1093 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1095 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1097 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1101 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1102 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1103 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1104 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1105 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1106 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1107 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1108 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1109 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /format_genome|get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
1110 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1111 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
1113 if ( $script eq "upload_to_ucsc" )
1115 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1116 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1119 return wantarray ? %options : \%options;
1123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1126 sub script_print_usage
1128 # Martin A. Hansen, January 2008.
1130 # Retrieves usage information from file and
1131 # prints this nicely formatted.
1133 my ( $in, # handle to in stream
1134 $out, # handle to out stream
1135 $options, # options hash
1140 my ( $file, $wiki, $lines );
1142 if ( $options->{ 'data_in' } ) {
1143 $file = $options->{ 'data_in' };
1145 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1148 $wiki = Maasha::Gwiki::gwiki_read( $file );
1150 if ( not $options->{ "help" } ) {
1151 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1154 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1156 print STDERR "$_\n" foreach @{ $lines };
1162 sub script_list_biopieces
1164 # Martin A. Hansen, January 2008.
1166 # Prints the synopsis from the usage for each of the biopieces.
1168 my ( $in, # handle to in stream
1169 $out, # handle to out stream
1170 $options, # options hash
1175 my ( @files, $file, $wiki, $program, $synopsis );
1177 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1179 foreach $file ( sort @files )
1181 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1185 $wiki = Maasha::Gwiki::gwiki_read( $file );
1187 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1188 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1190 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1191 $synopsis =~ s/!(\w)/$1/g;
1193 printf( "%-30s%s\n", $program, $synopsis );
1201 sub script_list_genomes
1203 # Martin A. Hansen, January 2008.
1205 # Prints the synopsis from the usage for each of the biopieces.
1207 my ( $in, # handle to in stream
1208 $out, # handle to out stream
1209 $options, # options hash
1214 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1216 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1218 foreach $genome ( @genomes )
1220 next if $genome =~ /\.$/;
1222 @formats = Maasha::Common::ls_dirs( $genome );
1224 foreach $format ( @formats )
1226 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1228 $hash{ $1 }{ $2 } = 1;
1237 map { push @row, $_ } sort keys %found;
1239 print join( "\t", @row ), "\n";
1241 foreach $genome ( sort keys %hash )
1245 foreach $format ( sort keys %found )
1247 if ( exists $hash{ $genome }{ $format } ) {
1254 print join( "\t", @row ), "\n";
1259 sub script_read_fasta
1261 # Martin A. Hansen, August 2007.
1263 # Read sequences from FASTA file.
1265 my ( $in, # handle to in stream
1266 $out, # handle to out stream
1267 $options, # options hash
1272 my ( $record, $file, $data_in, $entry, $num );
1274 while ( $record = get_record( $in ) ) {
1275 put_record( $record, $out );
1280 foreach $file ( @{ $options->{ "files" } } )
1282 $data_in = Maasha::Common::read_open( $file );
1284 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1286 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1289 SEQ_NAME => $entry->[ SEQ_NAME ],
1290 SEQ => $entry->[ SEQ ],
1291 SEQ_LEN => length $entry->[ SEQ ],
1294 put_record( $record, $out );
1297 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1307 close $data_in if $data_in;
1313 # Martin A. Hansen, August 2007.
1315 # Read table or table columns from stream or file.
1317 my ( $in, # handle to in stream
1318 $out, # handle to out stream
1319 $options, # options hash
1324 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1326 $options->{ 'delimit' } ||= '\s+';
1328 while ( $record = get_record( $in ) ) {
1329 put_record( $record, $out );
1332 $skip = $options->{ 'skip' } ||= 0;
1335 foreach $file ( @{ $options->{ "files" } } )
1337 $data_in = Maasha::Common::read_open( $file );
1339 while ( $line = <$data_in> )
1347 next if $line =~ /^#|^$/;
1354 @fields = split /$options->{'delimit'}/, $line;
1356 if ( $options->{ "cols" } ) {
1357 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1362 for ( $i = 0; $i < @fields2; $i++ )
1364 if ( $options->{ "keys" }->[ $i ] ) {
1365 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1367 $record->{ "V" . $i } = $fields2[ $i ];
1371 put_record( $record, $out );
1373 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1383 close $data_in if $data_in;
1389 # Martin A. Hansen, August 2007.
1391 # Read psl table from stream or file.
1393 my ( $in, # handle to in stream
1394 $out, # handle to out stream
1395 $options, # options hash
1400 my ( $record, $file, $data_in, $num );
1402 while ( $record = get_record( $in ) ) {
1403 put_record( $record, $out );
1408 foreach $file ( @{ $options->{ "files" } } )
1410 $data_in = Maasha::Common::read_open( $file );
1412 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1414 put_record( $record, $out );
1416 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1428 # Martin A. Hansen, August 2007.
1430 # Read bed table from stream or file.
1432 my ( $in, # handle to in stream
1433 $out, # handle to out stream
1434 $options, # options hash
1439 my ( $file, $record, $entry, $data_in, $num );
1441 while ( $record = get_record( $in ) ) {
1442 put_record( $record, $out );
1447 foreach $file ( @{ $options->{ "files" } } )
1449 $data_in = Maasha::Common::read_open( $file );
1451 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1453 put_record( $entry, $out );
1455 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1465 close $data_in if $data_in;
1469 sub script_read_fixedstep
1471 # Martin A. Hansen, Juli 2008.
1473 # Read fixedstep wiggle format from stream or file.
1475 my ( $in, # handle to in stream
1476 $out, # handle to out stream
1477 $options, # options hash
1482 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1484 while ( $record = get_record( $in ) ) {
1485 put_record( $record, $out );
1490 foreach $file ( @{ $options->{ "files" } } )
1492 $data_in = Maasha::Common::read_open( $file );
1494 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1496 $head = shift @{ $entry };
1498 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1500 $record->{ "REC_TYPE" } = "fixed_step";
1501 $record->{ "CHR" } = $1;
1502 $record->{ "CHR_BEG" } = $2;
1503 $record->{ "STEP" } = $3;
1504 $record->{ "VALS" } = join ";", @{ $entry };
1507 put_record( $record, $out );
1509 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1519 close $data_in if $data_in;
1523 sub script_read_blast_tab
1525 # Martin A. Hansen, September 2007.
1527 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1529 my ( $in, # handle to in stream
1530 $out, # handle to out stream
1531 $options, # options hash
1536 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1538 while ( $record = get_record( $in ) ) {
1539 put_record( $record, $out );
1544 foreach $file ( @{ $options->{ "files" } } )
1546 $data_in = Maasha::Common::read_open( $file );
1548 while ( $line = <$data_in> )
1552 next if $line =~ /^#/;
1554 @fields = split /\t/, $line;
1556 $record->{ "REC_TYPE" } = "BLAST";
1557 $record->{ "Q_ID" } = $fields[ 0 ];
1558 $record->{ "S_ID" } = $fields[ 1 ];
1559 $record->{ "IDENT" } = $fields[ 2 ];
1560 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1561 $record->{ "MISMATCHES" } = $fields[ 4 ];
1562 $record->{ "GAPS" } = $fields[ 5 ];
1563 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1564 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1565 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1566 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1567 $record->{ "E_VAL" } = $fields[ 10 ];
1568 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1570 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1572 $record->{ "STRAND" } = '-';
1574 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1578 $record->{ "STRAND" } = '+';
1581 put_record( $record, $out );
1583 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1593 close $data_in if $data_in;
1597 sub script_read_embl
1599 # Martin A. Hansen, August 2007.
1603 my ( $in, # handle to in stream
1604 $out, # handle to out stream
1605 $options, # options hash
1610 my ( %options2, $file, $data_in, $num, $entry, $record );
1612 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1613 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1614 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1616 while ( $record = get_record( $in ) ) {
1617 put_record( $record, $out );
1622 foreach $file ( @{ $options->{ "files" } } )
1624 $data_in = Maasha::Common::read_open( $file );
1626 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1628 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1630 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1632 $record_copy = dclone $record;
1634 delete $record_copy->{ "FT" };
1636 put_record( $record_copy, $out );
1638 delete $record_copy->{ "SEQ" };
1640 foreach $feat ( keys %{ $record->{ "FT" } } )
1642 $record_copy->{ "FEAT_TYPE" } = $feat;
1644 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1646 foreach $qual ( keys %{ $feat2 } )
1648 $qual_val = join "; ", @{ $feat2->{ $qual } };
1653 $record_copy->{ $qual } = $qual_val;
1656 put_record( $record_copy, $out );
1660 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1670 close $data_in if $data_in;
1674 sub script_read_stockholm
1676 # Martin A. Hansen, August 2007.
1678 # Read Stockholm format.
1680 my ( $in, # handle to in stream
1681 $out, # handle to out stream
1682 $options, # options hash
1687 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1689 while ( $record = get_record( $in ) ) {
1690 put_record( $record, $out );
1695 foreach $file ( @{ $options->{ "files" } } )
1697 $data_in = Maasha::Common::read_open( $file );
1699 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1701 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1705 foreach $key ( keys %{ $record->{ "GF" } } ) {
1706 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1709 $record_anno->{ "ALIGN" } = $num;
1711 put_record( $record_anno, $out );
1713 foreach $seq ( @{ $record->{ "ALIGN" } } )
1715 undef $record_align;
1718 SEQ_NAME => $seq->[ 0 ],
1722 put_record( $record_align, $out );
1725 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1735 close $data_in if $data_in;
1739 sub script_read_phastcons
1741 # Martin A. Hansen, December 2007.
1743 # Read PhastCons format.
1745 my ( $in, # handle to in stream
1746 $out, # handle to out stream
1747 $options, # options hash
1752 my ( $data_in, $file, $num, $entry, @records, $record );
1754 $options->{ "min" } ||= 10;
1755 $options->{ "dist" } ||= 25;
1756 $options->{ "threshold" } ||= 0.8;
1757 $options->{ "gap" } ||= 5;
1759 while ( $record = get_record( $in ) ) {
1760 put_record( $record, $out );
1765 foreach $file ( @{ $options->{ "files" } } )
1767 $data_in = Maasha::Common::read_open( $file );
1769 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1771 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1773 foreach $record ( @records )
1775 $record->{ "REC_TYPE" } = "BED";
1776 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1778 put_record( $record, $out );
1780 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1791 close $data_in if $data_in;
1795 sub script_read_soft
1797 # Martin A. Hansen, December 2007.
1800 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1802 my ( $in, # handle to in stream
1803 $out, # handle to out stream
1804 $options, # options hash
1809 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1811 while ( $record = get_record( $in ) ) {
1812 put_record( $record, $out );
1817 foreach $file ( @{ $options->{ "files" } } )
1819 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1821 $soft_index = Maasha::NCBI::soft_index_file( $file );
1823 $fh = Maasha::Common::read_open( $file );
1825 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1827 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1829 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1831 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1833 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1835 foreach $sample ( @samples )
1838 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1840 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1842 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1844 foreach $record ( @{ $records } )
1846 put_record( $record, $out );
1848 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1853 $old_end = $sample->{ "LINE_END" };
1861 close $data_in if $data_in;
1868 # Martin A. Hansen, February 2008.
1871 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1873 my ( $in, # handle to in stream
1874 $out, # handle to out stream
1875 $options, # options hash
1880 my ( $data_in, $file, $fh, $num, $record, $entry );
1882 while ( $record = get_record( $in ) ) {
1883 put_record( $record, $out );
1888 foreach $file ( @{ $options->{ "files" } } )
1890 $fh = Maasha::Common::read_open( $file );
1892 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1894 put_record( $entry, $out );
1896 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1906 close $data_in if $data_in;
1910 sub script_read_2bit
1912 # Martin A. Hansen, March 2008.
1914 # Read sequences from 2bit file.
1916 my ( $in, # handle to in stream
1917 $out, # handle to out stream
1918 $options, # options hash
1923 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1925 $mask = 1 if not $options->{ "no_mask" };
1927 while ( $record = get_record( $in ) ) {
1928 put_record( $record, $out );
1933 foreach $file ( @{ $options->{ "files" } } )
1935 $data_in = Maasha::Common::read_open( $file );
1937 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1939 foreach $line ( @{ $toc } )
1941 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1942 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1943 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1945 put_record( $record, $out );
1947 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1957 close $data_in if $data_in;
1961 sub script_read_solexa
1963 # Martin A. Hansen, March 2008.
1965 # Read Solexa sequence reads from file.
1967 my ( $in, # handle to in stream
1968 $out, # handle to out stream
1969 $options, # options hash
1974 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1976 $options->{ "format" } ||= "octal";
1977 $options->{ "quality" } ||= 20;
1979 while ( $record = get_record( $in ) ) {
1980 put_record( $record, $out );
1985 foreach $file ( @{ $options->{ "files" } } )
1987 $data_in = Maasha::Common::read_open( $file );
1989 if ( $options->{ "format" } eq "octal" )
1991 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1993 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1995 put_record( $record, $out );
1997 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2004 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
2006 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2008 put_record( $record, $out );
2010 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2021 close $data_in if $data_in;
2025 sub script_read_solid
2027 # Martin A. Hansen, April 2008.
2029 # Read Solid sequence from file.
2031 my ( $in, # handle to in stream
2032 $out, # handle to out stream
2033 $options, # options hash
2038 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
2040 $options->{ "quality" } ||= 15;
2042 while ( $record = get_record( $in ) ) {
2043 put_record( $record, $out );
2048 foreach $file ( @{ $options->{ "files" } } )
2050 $data_in = Maasha::Common::read_open( $file );
2052 while ( $line = <$data_in> )
2056 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2058 @scores = split /,/, $seq_qual;
2059 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2061 for ( $i = 0; $i < @seqs; $i++ ) {
2062 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2066 REC_TYPE => 'SOLID',
2067 SEQ_NAME => $seq_name,
2069 SEQ_QUAL => join( ";", @scores ),
2070 SEQ_LEN => length $seq_cs,
2071 SEQ => join( "", @seqs ),
2072 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2075 put_record( $record, $out );
2077 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2087 close $data_in if $data_in;
2091 sub script_read_mysql
2093 # Martin A. Hansen, May 2008.
2095 # Read a MySQL query into stream.
2097 my ( $in, # handle to in stream
2098 $out, # handle to out stream
2099 $options, # options hash
2104 my ( $record, $dbh, $results );
2106 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2107 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2109 while ( $record = get_record( $in ) ) {
2110 put_record( $record, $out );
2113 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2115 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2117 Maasha::SQL::disconnect( $dbh );
2119 map { put_record( $_ ) } @{ $results };
2123 sub script_read_ucsc_config
2125 # Martin A. Hansen, November 2008.
2127 # Read track entries from UCSC Genome Browser '.ra' files.
2129 my ( $in, # handle to in stream
2130 $out, # handle to out stream
2131 $options, # options hash
2136 my ( $record, $file, $data_in, $entry, $num );
2138 while ( $record = get_record( $in ) ) {
2139 put_record( $record, $out );
2144 foreach $file ( @{ $options->{ "files" } } )
2146 $data_in = Maasha::Common::read_open( $file );
2148 while ( $record = Maasha::UCSC::ucsc_config_get_entry( $data_in ) )
2150 $record->{ 'REC_TYPE' } = "UCSC Config";
2152 put_record( $record, $out );
2154 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2164 close $data_in if $data_in;
2168 sub script_assemble_tag_contigs
2170 # Martin A. Hansen, November 2008.
2172 # Assemble tags from the stream into
2175 my ( $in, # handle to in stream
2176 $out, # handle to out stream
2177 $options, # options hash
2182 my ( $record, $new_record, %fh_hash, $fh_out, $chr, $array, $pos, $beg, $end, $score, $file, $id );
2184 while ( $record = get_record( $in ) )
2186 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2187 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2188 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2190 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2192 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2194 $fh_out = $fh_hash{ $record->{ "CHR" } };
2196 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2200 map { close $_ } keys %fh_hash;
2202 foreach $chr ( sort keys %fh_hash )
2204 $array = tag_contigs_assemble( "$BP_TMP/$chr" );
2209 while ( ( $beg, $end, $score ) = tag_contigs_scan( $array, $pos ) and $beg )
2214 CHR_END => $end - 1,
2215 Q_ID => sprintf( "TC%06d", $id ),
2217 STRAND => $record->{ 'STRAND' } || '+',
2220 put_record( $new_record, $out );
2226 unlink "$BP_TMP/$chr";
2231 sub tag_contigs_assemble
2233 # Martin A. Hansen, November 2008.
2235 # Given a BED file with entries from only one
2236 # chromosome assembles tag contigs from these
2237 # ignoring strand information. Only tags with
2238 # a score higher than the clone count over
2239 # genomic loci (the SCORE field) is included
2240 # in the tag contigs.
2246 # ======================== tag contig
2249 my ( $path, # full path to BED file
2252 # Returns an arrayref.
2254 my ( $fh, $entry, $clones, $score, @array );
2256 $fh = Maasha::Common::read_open( $path );
2258 while ( $entry = Maasha::UCSC::bed_get_entry( $fh ) )
2260 if ( $entry->{ 'Q_ID' } =~ /(\d+)$/ )
2264 $score = int( $clones / $entry->{ 'SCORE' } );
2266 map { $array[ $_ ] += $score } $entry->{ 'CHR_BEG' } .. $entry->{ 'CHR_END' } if $score >= 1;
2272 return wantarray ? @array : \@array;
2276 sub tag_contigs_scan
2278 # Martin A. Hansen, November 2008.
2280 # Scans an array with tag contigs and locates
2281 # the next contig from a given position. The
2282 # score of the tag contig is determined as the
2283 # maximum value of the tag contig. If a tag contig
2284 # is found a triple is returned with beg, end and score
2285 # otherwise an empty triple is returned.
2287 my ( $array, # array to scan
2288 $beg, # position to start scanning from
2291 # Returns an arrayref.
2293 my ( $end, $score );
2297 while ( $beg < scalar @{ $array } and not $array->[ $beg ] ) { $beg++ }
2301 while ( $array->[ $end ] )
2303 $score = Maasha::Calc::max( $score, $array->[ $end ] );
2309 return wantarray ? ( $beg, $end, $score ) : [ $beg, $end, $score ];
2311 return wantarray ? () : [];
2316 sub script_format_genome
2318 # Martin A. Hansen, Juli 2008.
2320 # Format a genome to speficed formats.
2322 my ( $in, # handle to in stream
2323 $out, # handle to out stream
2324 $options, # options hash
2329 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2331 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2332 $genome = $options->{ 'genome' };
2334 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2335 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2336 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2338 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2340 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2342 $fasta_dir = "$dir/genomes/$genome/fasta";
2346 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2348 $fasta_dir = "$dir/genomes/$genome/fasta";
2350 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2353 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2355 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2357 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2359 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2362 while ( $record = get_record( $in ) )
2364 if ( $fh_out and $entry = record2fasta( $record ) )
2366 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2368 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2370 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2372 $vals = $record->{ 'VALS' };
2376 print $fh_out "$vals\n";
2379 put_record( $record, $out ) if not $options->{ "no_stream" };
2382 foreach $format ( @{ $options->{ 'formats' } } )
2384 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2385 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2386 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2387 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2388 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2391 close $fh_out if $fh_out;
2395 sub script_length_seq
2397 # Martin A. Hansen, August 2007.
2399 # Determine the length of sequences in stream.
2401 my ( $in, # handle to in stream
2402 $out, # handle to out stream
2403 $options, # options hash
2408 my ( $record, $total );
2410 while ( $record = get_record( $in ) )
2412 if ( $record->{ "SEQ" } )
2414 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2415 $total += $record->{ "SEQ_LEN" };
2418 put_record( $record, $out ) if not $options->{ "no_stream" };
2421 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2425 sub script_uppercase_seq
2427 # Martin A. Hansen, August 2007.
2429 # Uppercases sequences in stream.
2431 my ( $in, # handle to in stream
2432 $out, # handle to out stream
2439 while ( $record = get_record( $in ) )
2441 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2443 put_record( $record, $out );
2448 sub script_shuffle_seq
2450 # Martin A. Hansen, December 2007.
2452 # Shuffle sequences in stream.
2454 my ( $in, # handle to in stream
2455 $out, # handle to out stream
2462 while ( $record = get_record( $in ) )
2464 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2466 put_record( $record, $out );
2471 sub script_analyze_seq
2473 # Martin A. Hansen, August 2007.
2475 # Analyze sequence composition of sequences in stream.
2477 my ( $in, # handle to in stream
2478 $out, # handle to out stream
2483 my ( $record, $analysis );
2485 while ( $record = get_record( $in ) )
2487 if ( $record->{ "SEQ" } )
2489 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2491 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2494 put_record( $record, $out );
2499 sub script_analyze_tags
2501 # Martin A. Hansen, August 2008.
2503 # Analyze sequence tags in stream.
2505 my ( $in, # handle to in stream
2506 $out, # handle to out stream
2511 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2513 while ( $record = get_record( $in ) )
2515 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2517 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2521 $len_hash{ length( $record->{ "SEQ" } ) }++;
2522 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2525 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2527 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2531 $len_hash{ $record->{ "BED_LEN" } }++;
2532 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2537 foreach $key ( sort { $a <=> $b } keys %len_hash )
2539 $tag_record->{ "TAG_LEN" } = $key;
2540 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2541 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2543 put_record( $tag_record, $out );
2548 sub script_complexity_seq
2550 # Martin A. Hansen, May 2008.
2552 # Generates an index calculated as the most common di-residue over
2553 # the sequence length for all sequences in stream.
2555 my ( $in, # handle to in stream
2556 $out, # handle to out stream
2561 my ( $record, $index );
2563 while ( $record = get_record( $in ) )
2565 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2567 put_record( $record, $out );
2572 sub script_oligo_freq
2574 # Martin A. Hansen, August 2007.
2576 # Determine the length of sequences in stream.
2578 my ( $in, # handle to in stream
2579 $out, # handle to out stream
2580 $options, # options hash
2585 my ( $record, %oligos, @freq_table );
2587 $options->{ "word_size" } ||= 7;
2589 while ( $record = get_record( $in ) )
2591 if ( $record->{ "SEQ" } )
2593 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2595 if ( not $options->{ "all" } )
2597 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2599 map { put_record( $_, $out ) } @freq_table;
2605 put_record( $record, $out );
2608 if ( $options->{ "all" } )
2610 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2612 map { put_record( $_, $out ) } @freq_table;
2617 sub script_create_weight_matrix
2619 # Martin A. Hansen, August 2007.
2621 # Creates a weight matrix from an alignmnet.
2623 my ( $in, # handle to in stream
2624 $out, # handle to out stream
2625 $options, # options hash
2630 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2634 while ( $record = get_record( $in ) )
2636 if ( $record->{ "SEQ" } )
2638 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2640 $res = substr $record->{ "SEQ" }, $i, 1;
2642 $freq_hash{ $i }{ $res }++;
2643 $res_hash{ $res } = 1;
2650 put_record( $record, $out );
2654 foreach $res ( sort keys %res_hash )
2658 $record->{ "V0" } = $res;
2660 for ( $i = 0; $i < keys %freq_hash; $i++ )
2662 $freq = $freq_hash{ $i }{ $res } || 0;
2664 if ( $options->{ "percent" } ) {
2665 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2668 $record->{ "V" . ( $i + 1 ) } = $freq;
2671 put_record( $record, $out );
2676 sub script_calc_bit_scores
2678 # Martin A. Hansen, March 2007.
2680 # Calculates the bit scores for each position from an alignmnet in the stream.
2682 my ( $in, # handle to in stream
2683 $out, # handle to out stream
2688 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2692 while ( $record = get_record( $in ) )
2694 if ( $record->{ "SEQ" } )
2696 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2698 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2700 $res = substr $record->{ "SEQ" }, $i, 1;
2702 next if $res =~ /-|_|~|\./;
2704 $freq_hash{ $i }{ $res }++;
2711 put_record( $record, $out );
2717 if ( $type eq "protein" ) {
2723 for ( $i = 0; $i < keys %freq_hash; $i++ )
2725 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2727 $bit_diff = $bit_max - $bit_height;
2729 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2732 put_record( $record, $out );
2736 sub script_calc_fixedstep
2738 # Martin A. Hansen, September 2008.
2740 # Calculates fixedstep entries from data in the stream.
2742 my ( $in, # handle to in stream
2743 $out, # handle to out stream
2744 $options, # options hash
2749 my ( $record, %fh_hash, $fh_in, $fh_out, $chr, $chr, $beg, $end, $q_id, $block, $entry, $clones, $beg_block, $max, $i );
2751 while ( $record = get_record( $in ) )
2753 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2754 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2755 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2757 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2759 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2761 $fh_out = $fh_hash{ $record->{ "CHR" } };
2763 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2767 map { close $_ } keys %fh_hash;
2769 foreach $chr ( sort keys %fh_hash )
2771 #Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
2772 Maasha::Common::run( "bed_sort", "--sort 3 --cols 5 $BP_TMP/$chr > $BP_TMP/$chr.sort" );
2774 rename "$BP_TMP/$chr.sort", "$BP_TMP/$chr";
2776 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
2780 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
2782 $chr = $entry->{ 'CHR' };
2783 $beg = $entry->{ 'CHR_BEG' };
2784 $end = $entry->{ 'CHR_END' };
2785 $q_id = $entry->{ 'Q_ID' };
2787 if ( $options->{ "score" } ) {
2788 $clones = $entry->{ 'SCORE' };
2789 } elsif ( $q_id =~ /_(\d+)$/ ) {
2799 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2801 $record->{ "CHR" } = $chr;
2802 $record->{ "CHR_BEG" } = $beg_block;
2803 $record->{ "STEP" } = 1;
2804 $record->{ "VALS" } = join ";", @{ $block };
2805 $record->{ "REC_TYPE" } = "fixed_step";
2807 put_record( $record, $out );
2813 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
2814 $block->[ $i ] += $clones;
2817 $max = Maasha::Calc::max( $max, $end );
2826 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
2827 $block->[ $i ] += $clones;
2834 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2836 $record->{ "CHR" } = $chr;
2837 $record->{ "CHR_BEG" } = $beg_block;
2838 $record->{ "STEP" } = 1;
2839 $record->{ "VALS" } = join ";", @{ $block };
2840 $record->{ "REC_TYPE" } = "fixed_step";
2842 put_record( $record, $out );
2844 unlink "$BP_TMP/$chr";
2849 sub script_reverse_seq
2851 # Martin A. Hansen, August 2007.
2853 # Reverse sequence in record.
2855 my ( $in, # handle to in stream
2856 $out, # handle to out stream
2863 while ( $record = get_record( $in ) )
2865 if ( $record->{ "SEQ" } ) {
2866 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2869 put_record( $record, $out );
2874 sub script_complement_seq
2876 # Martin A. Hansen, August 2007.
2878 # Complement sequence in record.
2880 my ( $in, # handle to in stream
2881 $out, # handle to out stream
2886 my ( $record, $type );
2888 while ( $record = get_record( $in ) )
2890 if ( $record->{ "SEQ" } )
2893 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2896 if ( $type eq "rna" ) {
2897 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2898 } elsif ( $type eq "dna" ) {
2899 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2903 put_record( $record, $out );
2908 sub script_remove_indels
2910 # Martin A. Hansen, August 2007.
2912 # Remove indels from sequences in stream.
2914 my ( $in, # handle to in stream
2915 $out, # handle to out stream
2922 while ( $record = get_record( $in ) )
2924 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2926 put_record( $record, $out );
2931 sub script_transliterate_seq
2933 # Martin A. Hansen, August 2007.
2935 # Transliterate chars from sequence in record.
2937 my ( $in, # handle to in stream
2938 $out, # handle to out stream
2939 $options, # options hash
2944 my ( $record, $search, $replace, $delete );
2946 $search = $options->{ "search" } || "";
2947 $replace = $options->{ "replace" } || "";
2948 $delete = $options->{ "delete" } || "";
2950 while ( $record = get_record( $in ) )
2952 if ( $record->{ "SEQ" } )
2954 if ( $search and $replace ) {
2955 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2956 } elsif ( $delete ) {
2957 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2961 put_record( $record, $out );
2966 sub script_transliterate_vals
2968 # Martin A. Hansen, April 2008.
2970 # Transliterate chars from values in record.
2972 my ( $in, # handle to in stream
2973 $out, # handle to out stream
2974 $options, # options hash
2979 my ( $record, $search, $replace, $delete, $key );
2981 $search = $options->{ "search" } || "";
2982 $replace = $options->{ "replace" } || "";
2983 $delete = $options->{ "delete" } || "";
2985 while ( $record = get_record( $in ) )
2987 foreach $key ( @{ $options->{ "keys" } } )
2989 if ( exists $record->{ $key } )
2991 if ( $search and $replace ) {
2992 eval "\$record->{ $key } =~ tr/$search/$replace/";
2993 } elsif ( $delete ) {
2994 eval "\$record->{ $key } =~ tr/$delete//d";
2999 put_record( $record, $out );
3004 sub script_translate_seq
3006 # Martin A. Hansen, February 2008.
3008 # Translate DNA sequence into protein sequence.
3010 my ( $in, # handle to in stream
3011 $out, # handle to out stream
3012 $options, # options hash
3017 my ( $record, $frame, %new_record );
3019 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
3021 while ( $record = get_record( $in ) )
3023 if ( $record->{ "SEQ" } )
3025 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
3027 foreach $frame ( @{ $options->{ "frames" } } )
3029 %new_record = %{ $record };
3031 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
3032 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
3033 $new_record{ "FRAME" } = $frame;
3035 put_record( \%new_record, $out );
3041 put_record( $record, $out );
3047 sub script_extract_seq
3049 # Martin A. Hansen, August 2007.
3051 # Extract subsequences from sequences in record.
3053 my ( $in, # handle to in stream
3054 $out, # handle to out stream
3055 $options, # options hash
3060 my ( $beg, $end, $len, $record );
3062 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
3065 $beg = $options->{ "beg" } - 1; # correcting for start offset
3068 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
3070 } elsif ( defined $options->{ "end" } ) {
3071 $end = $options->{ "end" } - 1; # correcting for start offset
3074 $len = $options->{ "len" };
3076 # print "beg->$beg, end->$end, len->$len\n";
3078 while ( $record = get_record( $in ) )
3080 if ( $record->{ "SEQ" } )
3082 if ( defined $beg and defined $end )
3084 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
3085 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3087 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
3090 elsif ( defined $beg and defined $len )
3092 if ( $len > length $record->{ "SEQ" } ) {
3093 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3095 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
3098 elsif ( defined $beg )
3100 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3104 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3106 put_record( $record, $out );
3111 sub script_get_genome_seq
3113 # Martin A. Hansen, December 2007.
3115 # Gets a subsequence from a genome.
3117 my ( $in, # handle to in stream
3118 $out, # handle to out stream
3119 $options, # options hash
3124 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
3126 $options->{ "flank" } ||= 0;
3128 if ( $options->{ "genome" } )
3130 $genome = $options->{ "genome" };
3132 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
3133 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
3135 $fh = Maasha::Common::read_open( $genome_file );
3136 $index = Maasha::Fasta::index_retrieve( $index_file );
3138 shift @{ $index }; # Get rid of the file size info
3140 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
3142 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3144 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
3146 $beg = $index_beg + $options->{ "beg" } - 1;
3148 if ( $options->{ "len" } ) {
3149 $len = $options->{ "len" };
3150 } elsif ( $options->{ "end" } ) {
3151 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3154 $beg -= $options->{ "flank" };
3155 $len += 2 * $options->{ "flank" };
3157 if ( $beg <= $index_beg )
3159 $len -= $index_beg - $beg;
3163 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3165 next if $beg > $index_beg + $index_len;
3167 $record->{ "CHR" } = $options->{ "chr" };
3168 $record->{ "CHR_BEG" } = $beg - $index_beg;
3169 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3171 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3172 $record->{ "SEQ_LEN" } = $len;
3174 put_record( $record, $out );
3178 while ( $record = get_record( $in ) )
3180 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3182 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3184 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3186 $beg = $record->{ "CHR_BEG" } + $index_beg;
3187 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3189 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3191 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3193 $beg = $record->{ "S_BEG" } + $index_beg;
3194 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3196 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3198 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3200 $beg = $record->{ "S_BEG" } + $index_beg;
3201 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3204 $beg -= $options->{ "flank" };
3205 $len += 2 * $options->{ "flank" };
3207 if ( $beg <= $index_beg )
3209 $len -= $index_beg - $beg;
3213 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3215 next if $beg > $index_beg + $index_len;
3217 $record->{ "CHR_BEG" } = $beg - $index_beg;
3218 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3220 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3222 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3224 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3225 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3228 if ( $options->{ "mask" } )
3230 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3232 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3234 @begs = split ",", $record->{ "Q_BEGS" };
3235 @lens = split ",", $record->{ "BLOCKSIZES" };
3237 for ( $i = 0; $i < @begs; $i++ ) {
3238 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3244 put_record( $record, $out );
3251 sub script_get_genome_align
3253 # Martin A. Hansen, April 2008.
3255 # Gets a subalignment from a multiple genome alignment.
3257 my ( $in, # handle to in stream
3258 $out, # handle to out stream
3259 $options, # options hash
3264 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3266 $options->{ "strand" } ||= "+";
3270 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3272 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3274 $beg = $options->{ "beg" } - 1;
3276 if ( $options->{ "end" } ) {
3277 $end = $options->{ "end" };
3278 } elsif ( $options->{ "len" } ) {
3279 $end = $beg + $options->{ "len" };
3282 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3284 foreach $entry ( @{ $align } )
3286 $entry->{ "CHR" } = $record->{ "CHR" };
3287 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3288 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3289 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3290 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3291 $entry->{ "SCORE" } = $record->{ "SCORE" };
3293 put_record( $entry, $out );
3297 while ( $record = get_record( $in ) )
3299 if ( $record->{ "REC_TYPE" } eq "BED" )
3301 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3303 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3305 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3307 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3309 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3312 foreach $entry ( @{ $align } )
3314 $entry->{ "CHR" } = $record->{ "CHR" };
3315 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3316 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3317 $entry->{ "STRAND" } = $record->{ "STRAND" };
3318 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3319 $entry->{ "SCORE" } = $record->{ "SCORE" };
3321 put_record( $entry, $out );
3329 sub script_get_genome_phastcons
3331 # Martin A. Hansen, February 2008.
3333 # Get phastcons scores from genome intervals.
3335 my ( $in, # handle to in stream
3336 $out, # handle to out stream
3337 $options, # options hash
3342 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3344 $options->{ "flank" } ||= 0;
3346 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3347 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3349 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3350 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3352 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3354 $options->{ "beg" } -= 1; # request is 1-based
3355 $options->{ "end" } -= 1; # request is 1-based
3357 if ( $options->{ "len" } ) {
3358 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3361 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3363 $record->{ "CHR" } = $options->{ "chr" };
3364 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3365 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3367 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3368 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3370 put_record( $record, $out );
3373 while ( $record = get_record( $in ) )
3375 if ( $record->{ "REC_TYPE" } eq "BED" )
3377 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3379 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3381 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3383 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3385 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3388 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3389 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3391 put_record( $record, $out );
3394 close $fh_phastcons if $fh_phastcons;
3400 # Martin A. Hansen, December 2007.
3402 # Folds sequences in stream into secondary structures.
3404 my ( $in, # handle to in stream
3405 $out, # handle to out stream
3410 my ( $record, $type, $struct, $index );
3412 while ( $record = get_record( $in ) )
3414 if ( $record->{ "SEQ" } )
3417 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3420 if ( $type ne "protein" )
3422 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3423 $record->{ "SEC_STRUCT" } = $struct;
3424 $record->{ "FREE_ENERGY" } = $index;
3425 $record->{ "SCORE" } = abs int $index;
3426 $record->{ "SIZE" } = length $struct;
3427 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3431 put_record( $record, $out );
3436 sub script_split_seq
3438 # Martin A. Hansen, August 2007.
3440 # Split a sequence in stream into words.
3442 my ( $in, # handle to in stream
3443 $out, # handle to out stream
3444 $options, # options hash
3449 my ( $record, $new_record, $i, $subseq, %lookup );
3451 $options->{ "word_size" } ||= 7;
3453 while ( $record = get_record( $in ) )
3455 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3457 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3459 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3461 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3463 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3464 $new_record->{ "SEQ" } = $subseq;
3466 put_record( $new_record, $out );
3468 $lookup{ $subseq } = 1;
3472 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3473 $new_record->{ "SEQ" } = $subseq;
3475 put_record( $new_record, $out );
3481 put_record( $record, $out );
3487 sub script_split_bed
3489 # Martin A. Hansen, June 2008.
3491 # Split a BED record into overlapping windows.
3493 my ( $in, # handle to in stream
3494 $out, # handle to out stream
3495 $options, # options hash
3500 my ( $record, $new_record, $i );
3502 $options->{ "window_size" } ||= 20;
3503 $options->{ "step_size" } ||= 1;
3505 while ( $record = get_record( $in ) )
3507 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3509 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3511 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3513 $new_record->{ "REC_TYPE" } = "BED";
3514 $new_record->{ "CHR" } = $record->{ "CHR" };
3515 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3516 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3517 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3518 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3519 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3520 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3522 put_record( $new_record, $out );
3527 put_record( $record, $out );
3533 sub script_align_seq
3535 # Martin A. Hansen, August 2007.
3537 # Align sequences in stream.
3539 my ( $in, # handle to in stream
3540 $out, # handle to out stream
3545 my ( $record, @entries, $entry );
3547 while ( $record = get_record( $in ) )
3549 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3550 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3551 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3552 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3554 put_record( $record, $out );
3558 @entries = Maasha::Align::align( \@entries );
3560 foreach $entry ( @entries )
3562 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3565 SEQ_NAME => $entry->[ SEQ_NAME ],
3566 SEQ => $entry->[ SEQ ],
3569 put_record( $record, $out );
3577 # Martin A. Hansen, February 2008.
3579 # Using the first sequence in stream as reference, tile
3580 # all subsequent sequences based on pairwise alignments.
3582 my ( $in, # handle to in stream
3583 $out, # handle to out stream
3584 $options, # options hash
3589 my ( $record, $first, $ref_entry, @entries );
3593 while ( $record = get_record( $in ) )
3595 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3599 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3605 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3610 put_record( $record, $out );
3614 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3616 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3620 sub script_invert_align
3622 # Martin A. Hansen, February 2008.
3624 # Inverts an alignment showing only non-mathing residues
3625 # using the first sequence as reference.
3627 my ( $in, # handle to in stream
3628 $out, # handle to out stream
3629 $options, # options hash
3634 my ( $record, @entries );
3636 while ( $record = get_record( $in ) )
3638 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3640 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3644 put_record( $record, $out );
3648 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3650 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3654 sub script_patscan_seq
3656 # Martin A. Hansen, August 2007.
3658 # Locates patterns in sequences using scan_for_matches.
3660 my ( $in, # handle to in stream
3661 $out, # handle to out stream
3662 $options, # options hash
3667 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3669 if ( $options->{ "patterns" } ) {
3670 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3671 } elsif ( -f $options->{ "patterns_in" } ) {
3672 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3675 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3677 push @args, "-c" if $options->{ "comp" };
3678 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3679 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3681 $seq_file = "$BP_TMP/patscan.seq";
3682 $pat_file = "$BP_TMP/patscan.pat";
3683 $out_file = "$BP_TMP/patscan.out";
3685 $fh_out = Maasha::Common::write_open( $seq_file );
3689 while ( $record = get_record( $in ) )
3691 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3693 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3695 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3697 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3705 $arg = join " ", @args;
3706 $arg .= " -p" if $type eq "protein";
3708 foreach $pattern ( @{ $patterns } )
3710 $fh_out = Maasha::Common::write_open( $pat_file );
3712 print $fh_out "$pattern\n";
3716 if ( $options->{ 'genome' } ) {
3717 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3718 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3720 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3721 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3724 $fh_in = Maasha::Common::read_open( $out_file );
3726 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3728 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3730 if ( $options->{ 'genome' } )
3732 $result->{ "CHR" } = $result->{ "S_ID" };
3733 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3734 $result->{ "CHR_END" } = $result->{ "S_END" };
3736 delete $result->{ "S_ID" };
3737 delete $result->{ "S_BEG" };
3738 delete $result->{ "S_END" };
3742 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3745 put_record( $result, $out );
3757 sub script_create_blast_db
3759 # Martin A. Hansen, September 2007.
3761 # Creates a NCBI BLAST database with formatdb
3763 my ( $in, # handle to in stream
3764 $out, # handle to out stream
3765 $options, # options hash
3770 my ( $fh, $seq_type, $path, $record, $entry );
3772 $path = $options->{ "database" };
3774 $fh = Maasha::Common::write_open( $path );
3776 while ( $record = get_record( $in ) )
3778 put_record( $record, $out ) if not $options->{ "no_stream" };
3780 if ( $entry = record2fasta( $record ) )
3782 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3784 Maasha::Fasta::put_entry( $entry, $fh );
3790 if ( $seq_type eq "protein" ) {
3791 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3793 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3800 sub script_blast_seq
3802 # Martin A. Hansen, September 2007.
3804 # BLASTs sequences in stream against a given database.
3806 my ( $in, # handle to in stream
3807 $out, # handle to out stream
3808 $options, # options hash
3813 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3815 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3816 $options->{ "filter" } = "F";
3817 $options->{ "filter" } = "T" if $options->{ "filter" };
3818 $options->{ "cpus" } ||= 1;
3820 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3822 $tmp_in = "$BP_TMP/blast_query.seq";
3823 $tmp_out = "$BP_TMP/blast.result";
3825 $fh_out = Maasha::Common::write_open( $tmp_in );
3827 while ( $record = get_record( $in ) )
3829 if ( $entry = record2fasta( $record ) )
3831 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3833 Maasha::Fasta::put_entry( $entry, $fh_out );
3836 put_record( $record, $out );
3841 if ( -f $options->{ 'database' } . ".phr" ) {
3842 $s_type = "protein";
3844 $s_type = "nucleotide";
3847 if ( not $options->{ 'program' } )
3849 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3850 $options->{ 'program' } = "blastn";
3851 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3852 $options->{ 'program' } = "blastp";
3853 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3854 $options->{ 'program' } = "blastx";
3855 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3856 $options->{ 'program' } = "tblastn";
3860 if ( $options->{ 'verbose' } )
3862 Maasha::Common::run(
3865 "-p $options->{ 'program' }",
3866 "-e $options->{ 'e_val' }",
3867 "-a $options->{ 'cpus' }",
3870 "-d $options->{ 'database' }",
3871 "-F $options->{ 'filter' }",
3879 Maasha::Common::run(
3882 "-p $options->{ 'program' }",
3883 "-e $options->{ 'e_val' }",
3884 "-a $options->{ 'cpus' }",
3887 "-d $options->{ 'database' }",
3888 "-F $options->{ 'filter' }",
3898 $fh_out = Maasha::Common::read_open( $tmp_out );
3902 while ( $line = <$fh_out> )
3906 next if $line =~ /^#/;
3908 @fields = split /\s+/, $line;
3910 $record->{ "REC_TYPE" } = "BLAST";
3911 $record->{ "Q_ID" } = $fields[ 0 ];
3912 $record->{ "S_ID" } = $fields[ 1 ];
3913 $record->{ "IDENT" } = $fields[ 2 ];
3914 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3915 $record->{ "MISMATCHES" } = $fields[ 4 ];
3916 $record->{ "GAPS" } = $fields[ 5 ];
3917 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3918 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3919 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3920 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3921 $record->{ "E_VAL" } = $fields[ 10 ];
3922 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3924 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3926 $record->{ "STRAND" } = '-';
3928 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3932 $record->{ "STRAND" } = '+';
3935 put_record( $record, $out );
3946 # Martin A. Hansen, August 2007.
3948 # BLATs sequences in stream against a given genome.
3950 my ( $in, # handle to in stream
3951 $out, # handle to out stream
3952 $options, # options hash
3957 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3959 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3961 $options->{ 'tile_size' } ||= 11;
3962 $options->{ 'one_off' } ||= 0;
3963 $options->{ 'min_identity' } ||= 90;
3964 $options->{ 'min_score' } ||= 0;
3965 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3967 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3968 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3969 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3970 $blat_args .= " -minScore=$options->{ 'min_score' }";
3971 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3972 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3974 $query_file = "$BP_TMP/blat.seq";
3976 $fh_out = Maasha::Common::write_open( $query_file );
3978 while ( $record = get_record( $in ) )
3980 if ( $entry = record2fasta( $record ) )
3982 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3983 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3986 put_record( $record, $out );
3991 $blat_args .= " -t=dnax" if $type eq "protein";
3992 $blat_args .= " -q=$type";
3994 $result_file = "$BP_TMP/blat.psl";
3996 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
4000 $entries = Maasha::UCSC::psl_get_entries( $result_file );
4002 map { put_record( $_, $out ) } @{ $entries };
4004 unlink $result_file;
4010 # Martin A. Hansen, July 2008.
4012 # soap sequences in stream against a given file or genome.
4014 my ( $in, # handle to in stream
4015 $out, # handle to out stream
4016 $options, # options hash
4021 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
4023 $options->{ "seed_size" } ||= 10;
4024 $options->{ "mismatches" } ||= 2;
4025 $options->{ "gap_size" } ||= 0;
4026 $options->{ "cpus" } ||= 1;
4028 if ( $options->{ "genome" } ) {
4029 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
4032 $tmp_in = "$BP_TMP/soap_query.seq";
4033 $tmp_out = "$BP_TMP/soap.result";
4035 $fh_out = Maasha::Common::write_open( $tmp_in );
4039 while ( $record = get_record( $in ) )
4041 if ( $entry = record2fasta( $record ) )
4043 Maasha::Fasta::put_entry( $entry, $fh_out );
4048 put_record( $record, $out );
4056 "-s $options->{ 'seed_size' }",
4059 "-v $options->{ 'mismatches' }",
4060 "-g $options->{ 'gap_size' }",
4061 "-p $options->{ 'cpus' }",
4062 "-d $options->{ 'in_file' }",
4066 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
4068 Maasha::Common::run( "soap", $args, 1 );
4072 $fh_out = Maasha::Common::read_open( $tmp_out );
4076 while ( $line = <$fh_out> )
4080 @fields = split /\t/, $line;
4082 $record->{ "REC_TYPE" } = "SOAP";
4083 $record->{ "Q_ID" } = $fields[ 0 ];
4084 $record->{ "SCORE" } = $fields[ 3 ];
4085 $record->{ "STRAND" } = $fields[ 6 ];
4086 $record->{ "S_ID" } = $fields[ 7 ];
4087 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
4088 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
4090 put_record( $record, $out );
4100 sub script_match_seq
4102 # Martin A. Hansen, August 2007.
4104 # BLATs sequences in stream against a given genome.
4106 my ( $in, # handle to in stream
4107 $out, # handle to out stream
4108 $options, # options hash
4113 my ( $record, @entries, $results );
4115 $options->{ "word_size" } ||= 20;
4116 $options->{ "direction" } ||= "both";
4118 while ( $record = get_record( $in ) )
4120 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4121 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4124 put_record( $record, $out );
4127 if ( @entries == 1 )
4129 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
4131 map { put_record( $_, $out ) } @{ $results };
4133 elsif ( @entries == 2 )
4135 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
4137 map { put_record( $_, $out ) } @{ $results };
4142 sub script_create_vmatch_index
4144 # Martin A. Hansen, January 2008.
4146 # Create a vmatch index from sequences in the stream.
4148 my ( $in, # handle to in stream
4149 $out, # handle to out stream
4150 $options, # options hash
4155 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4157 if ( $options->{ "index_name" } )
4159 $file_tmp = $options->{ 'index_name' };
4160 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4163 while ( $record = get_record( $in ) )
4165 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
4167 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4169 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4172 put_record( $record, $out ) if not $options->{ "no_stream" };
4175 if ( $options->{ "index_name" } )
4179 if ( $type eq "protein" ) {
4180 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4182 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4190 sub script_vmatch_seq
4192 # Martin A. Hansen, August 2007.
4194 # Vmatches sequences in stream against a given genome.
4196 my ( $in, # handle to in stream
4197 $out, # handle to out stream
4198 $options, # options hash
4203 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4205 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4207 if ( $options->{ "index_name" } )
4209 @index_files = $options->{ "index_name" };
4213 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4215 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4217 @index_files = sort keys %hash;
4220 while ( $record = get_record( $in ) )
4222 push @records, $record;
4224 put_record( $record, $out );
4227 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4231 $fh_in = Maasha::Common::read_open( $result_file );
4233 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4234 put_record( $record, $out );
4239 unlink $result_file;
4243 sub script_write_fasta
4245 # Martin A. Hansen, August 2007.
4247 # Write FASTA entries from sequences in stream.
4249 my ( $in, # handle to in stream
4250 $out, # handle to out stream
4251 $options, # options hash
4256 my ( $record, $fh, $entry );
4258 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4260 while ( $record = get_record( $in ) )
4262 if ( $entry = record2fasta( $record ) ) {
4263 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4266 put_record( $record, $out ) if not $options->{ "no_stream" };
4273 sub script_write_align
4275 # Martin A. Hansen, August 2007.
4277 # Write pretty alignments aligned sequences in stream.
4279 my ( $in, # handle to in stream
4280 $out, # handle to out stream
4281 $options, # options hash
4286 my ( $fh, $record, @entries );
4288 $fh = write_stream( $options->{ "data_out" } ) ;
4290 while ( $record = get_record( $in ) )
4292 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4293 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4296 put_record( $record, $out ) if not $options->{ "no_stream" };
4299 if ( scalar( @entries ) == 2 ) {
4300 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4301 } elsif ( scalar ( @entries ) > 2 ) {
4302 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4309 sub script_write_blast
4311 # Martin A. Hansen, November 2007.
4313 # Write data in blast table format (-m8 and 9).
4315 my ( $in, # handle to in stream
4316 $out, # handle to out stream
4317 $options, # options hash
4322 my ( $fh, $record, $first );
4324 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4328 while ( $record = get_record( $in ) )
4330 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4332 if ( $options->{ "comment" } and $first )
4334 print "# Fields: Query id, Subject id, % identity, alignment length, mismatches, gap openings, q. start, q. end, s. start, s. end, e-value, bit score\n";
4339 if ( $record->{ "STRAND" } eq "-" ) {
4340 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4343 print $fh join( "\t",
4344 $record->{ "Q_ID" },
4345 $record->{ "S_ID" },
4346 $record->{ "IDENT" },
4347 $record->{ "ALIGN_LEN" },
4348 $record->{ "MISMATCHES" },
4349 $record->{ "GAPS" },
4350 $record->{ "Q_BEG" } + 1,
4351 $record->{ "Q_END" } + 1,
4352 $record->{ "S_BEG" } + 1,
4353 $record->{ "S_END" } + 1,
4354 $record->{ "E_VAL" },
4355 $record->{ "BIT_SCORE" }
4359 put_record( $record, $out ) if not $options->{ "no_stream" };
4366 sub script_write_tab
4368 # Martin A. Hansen, August 2007.
4370 # Write data as table.
4372 my ( $in, # handle to in stream
4373 $out, # handle to out stream
4374 $options, # options hash
4379 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4381 $options->{ "delimit" } ||= "\t";
4383 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4385 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4387 while ( $record = get_record( $in ) )
4392 if ( $options->{ "keys" } )
4394 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4398 foreach $key ( @{ $options->{ "keys" } } )
4400 if ( exists $record->{ $key } )
4402 push @keys, $key if $options->{ "comment" };
4403 push @vals, $record->{ $key };
4410 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4412 next if exists $no_keys{ $key };
4414 push @keys, $key if $options->{ "comment" };
4415 push @vals, $record->{ $key };
4419 if ( @keys and $options->{ "comment" } )
4421 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4423 delete $options->{ "comment" };
4426 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4428 put_record( $record, $out ) if not $options->{ "no_stream" };
4435 sub script_write_bed
4437 # Martin A. Hansen, August 2007.
4439 # Write BED format for the UCSC genome browser using records in stream.
4441 my ( $in, # handle to in stream
4442 $out, # handle to out stream
4443 $options, # options hash
4448 my ( $fh, $record, $new_record );
4450 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4452 while ( $record = get_record( $in ) )
4454 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4456 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4458 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4460 $new_record->{ "CHR" } = $record->{ "S_ID" };
4461 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4462 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4463 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4464 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4465 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4467 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4469 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4471 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4473 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4475 $new_record->{ "CHR" } = $record->{ "S_ID" };
4476 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4477 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4478 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4479 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4480 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4482 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4484 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4486 $new_record->{ "CHR" } = $record->{ "S_ID" };
4487 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4488 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4489 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4490 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4491 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4493 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4495 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4497 $new_record->{ "CHR" } = $record->{ "S_ID" };
4498 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4499 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4500 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4501 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4502 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4504 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4506 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4508 Maasha::UCSC::bed_put_entry( $record, $fh );
4511 put_record( $record, $out ) if not $options->{ "no_stream" };
4518 sub script_write_psl
4520 # Martin A. Hansen, August 2007.
4522 # Write PSL output from stream.
4524 my ( $in, # handle to in stream
4525 $out, # handle to out stream
4526 $options, # options hash
4531 my ( $fh, $record, @output, $first );
4535 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4537 while ( $record = get_record( $in ) )
4539 put_record( $record, $out ) if not $options->{ "no_stream" };
4541 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4543 Maasha::UCSC::psl_put_header( $fh ) if $first;
4544 Maasha::UCSC::psl_put_entry( $record, $fh );
4553 sub script_write_fixedstep
4555 # Martin A. Hansen, Juli 2008.
4557 # Write fixedStep entries from recrods in the stream.
4559 my ( $in, # handle to in stream
4560 $out, # handle to out stream
4561 $options, # options hash
4566 my ( $fh, $record, $vals );
4568 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4570 while ( $record = get_record( $in ) )
4572 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4574 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4576 $vals = $record->{ 'VALS' };
4580 print $fh "$vals\n";
4583 put_record( $record, $out ) if not $options->{ "no_stream" };
4590 sub script_write_2bit
4592 # Martin A. Hansen, March 2008.
4594 # Write sequence entries from stream in 2bit format.
4596 my ( $in, # handle to in stream
4597 $out, # handle to out stream
4598 $options, # options hash
4603 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4605 $mask = 1 if not $options->{ "no_mask" };
4607 $tmp_file = "$BP_TMP/write_2bit.fna";
4608 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4610 $fh_out = write_stream( $options->{ "data_out" } );
4612 while ( $record = get_record( $in ) )
4614 if ( $entry = record2fasta( $record ) ) {
4615 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4618 put_record( $record, $out ) if not $options->{ "no_stream" };
4623 $fh_in = Maasha::Common::read_open( $tmp_file );
4625 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4634 sub script_write_solid
4636 # Martin A. Hansen, April 2008.
4638 # Write di-base encoded Solid sequence from entries in stream.
4640 my ( $in, # handle to in stream
4641 $out, # handle to out stream
4642 $options, # options hash
4647 my ( $record, $fh, $entry );
4649 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4651 while ( $record = get_record( $in ) )
4653 if ( $entry = record2fasta( $record ) )
4655 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4657 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4660 put_record( $record, $out ) if not $options->{ "no_stream" };
4667 sub script_write_ucsc_config
4669 # Martin A. Hansen, November 2008.
4671 # Write UCSC Genome Broser configuration (.ra file type) from
4672 # records in the stream.
4674 my ( $in, # handle to in stream
4675 $out, # handle to out stream
4676 $options, # options hash
4681 my ( $record, $fh );
4683 $fh = write_stream( $options->{ "data_out" } );
4685 while ( $record = get_record( $in ) )
4687 Maasha::UCSC::ucsc_config_put_entry( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4689 put_record( $record, $out ) if not $options->{ "no_stream" };
4696 sub script_plot_seqlogo
4698 # Martin A. Hansen, August 2007.
4700 # Calculates and writes a sequence logo for alignments.
4702 my ( $in, # handle to in stream
4703 $out, # handle to out stream
4704 $options, # options hash
4709 my ( $record, @entries, $logo, $fh );
4711 while ( $record = get_record( $in ) )
4713 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4714 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4717 put_record( $record, $out ) if not $options->{ "no_stream" };
4720 $logo = Maasha::Plot::seq_logo( \@entries );
4722 $fh = write_stream( $options->{ "data_out" } );
4730 sub script_plot_phastcons_profiles
4732 # Martin A. Hansen, January 2008.
4734 # Plots PhastCons profiles.
4736 my ( $in, # handle to in stream
4737 $out, # handle to out stream
4738 $options, # options hash
4743 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4745 $options->{ "title" } ||= "PhastCons Profiles";
4747 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4748 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4750 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4751 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4753 while ( $record = get_record( $in ) )
4755 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4757 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4758 $record->{ "CHR_BEG" },
4759 $record->{ "CHR_END" },
4760 $options->{ "flank" } );
4762 push @{ $AoA }, [ @{ $scores } ];
4765 put_record( $record, $out ) if not $options->{ "no_stream" };
4768 Maasha::UCSC::phastcons_normalize( $AoA );
4770 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4771 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4773 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4775 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4777 $fh = write_stream( $options->{ "data_out" } );
4779 print $fh "$_\n" foreach @{ $plot };
4785 sub script_analyze_bed
4787 # Martin A. Hansen, March 2008.
4789 # Analyze BED entries in stream.
4791 my ( $in, # handle to in stream
4792 $out, # handle to out stream
4793 $options, # options hash
4800 while ( $record = get_record( $in ) )
4802 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4804 put_record( $record, $out );
4809 sub script_analyze_vals
4811 # Martin A. Hansen, August 2007.
4813 # Analyze values for given keys in stream.
4815 my ( $in, # handle to in stream
4816 $out, # handle to out stream
4817 $options, # options hash
4822 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4824 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4826 while ( $record = get_record( $in ) )
4828 foreach $key ( keys %{ $record } )
4830 next if $options->{ "keys" } and not exists $key_hash{ $key };
4832 $analysis->{ $key }->{ "COUNT" }++;
4834 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4836 $analysis->{ $key }->{ "TYPE" } = "num";
4837 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4838 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4839 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4843 $len = length $record->{ $key };
4845 $analysis->{ $key }->{ "TYPE" } = "alph";
4846 $analysis->{ $key }->{ "SUM" } += $len;
4847 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4848 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4852 put_record( $record, $out ) if not $options->{ "no_stream" };
4855 foreach $key ( keys %{ $analysis } )
4857 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4858 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4861 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4871 if ( $options->{ "keys" } ) {
4872 @keys = @{ $options->{ "keys" } };
4874 @keys = keys %{ $analysis };
4877 foreach $key ( @keys )
4879 $keys .= sprintf "% 15s", $key;
4880 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4881 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4882 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4883 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4884 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4885 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4888 print $out "$keys\n";
4889 print $out "$types\n";
4890 print $out "$counts\n";
4891 print $out "$mins\n";
4892 print $out "$maxs\n";
4893 print $out "$sums\n";
4894 print $out "$means\n";
4898 sub script_head_records
4900 # Martin A. Hansen, August 2007.
4902 # Display the first sequences in stream.
4904 my ( $in, # handle to in stream
4905 $out, # handle to out stream
4906 $options, # options hash
4911 my ( $record, $count );
4913 $options->{ "num" } ||= 10;
4917 while ( $record = get_record( $in ) )
4921 put_record( $record, $out );
4923 last if $count == $options->{ "num" };
4928 sub script_remove_keys
4930 # Martin A. Hansen, August 2007.
4932 # Remove keys from stream.
4934 my ( $in, # handle to in stream
4935 $out, # handle to out stream
4936 $options, # options hash
4941 my ( $record, $new_record );
4943 while ( $record = get_record( $in ) )
4945 if ( $options->{ "keys" } )
4947 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4949 elsif ( $options->{ "save_keys" } )
4951 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4953 $record = $new_record;
4956 put_record( $record, $out ) if keys %{ $record };
4961 sub script_remove_adaptor
4963 # Martin A. Hansen, August 2008.
4965 # Find and remove adaptor from sequences in the stream.
4967 my ( $in, # handle to in stream
4968 $out, # handle to out stream
4969 $options, # options hash
4974 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4976 $options->{ "remove" } ||= "after";
4978 $max_mismatch = $options->{ "mismatches" } || 0;
4979 $offset = $options->{ "offset" };
4981 if ( not defined $offset ) {
4987 $adaptor = uc $options->{ "adaptor" };
4988 $adaptor_len = length $adaptor;
4990 while ( $record = get_record( $in ) )
4992 if ( $record->{ "SEQ" } )
4994 $seq = uc $record->{ "SEQ" };
4995 $seq_len = length $seq;
4997 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4999 $record->{ "ADAPTOR_POS" } = $pos;
5001 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
5003 if ( $options->{ "remove" } eq "after" )
5005 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
5006 $record->{ "SEQ_LEN" } = $pos;
5010 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
5011 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
5015 put_record( $record, $out );
5019 put_record( $record, $out );
5025 sub script_remove_mysql_tables
5027 # Martin A. Hansen, November 2008.
5029 # Remove MySQL tables from values in stream.
5031 my ( $in, # handle to in stream
5032 $out, # handle to out stream
5033 $options, # options hash
5038 my ( $record, %table_hash, $dbh, $table );
5040 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
5041 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
5043 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
5045 while ( $record = get_record( $in ) )
5047 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5049 put_record( $record, $out ) if not $options->{ 'no_stream' };
5052 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5054 foreach $table ( sort keys %table_hash )
5056 if ( Maasha::SQL::table_exists( $dbh, $table ) )
5058 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5059 Maasha::SQL::delete_table( $dbh, $table );
5060 print STDERR "done.\n" if $options->{ 'verbose' };
5064 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
5068 Maasha::SQL::disconnect( $dbh );
5072 sub script_remove_ucsc_tracks
5074 # Martin A. Hansen, November 2008.
5076 # Remove track from MySQL tables and config file.
5078 my ( $in, # handle to in stream
5079 $out, # handle to out stream
5080 $options, # options hash
5085 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
5087 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
5088 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
5089 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
5091 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
5093 while ( $record = get_record( $in ) )
5095 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5097 put_record( $record, $out ) if not $options->{ 'no_stream' };
5100 # ---- locate track in config file ----
5102 $fh_in = Maasha::Common::read_open( $options->{ 'config_file' } );
5104 while ( $track = Maasha::UCSC::ucsc_config_get_entry( $fh_in ) ) {
5105 push @tracks, $track;
5110 map { push @new_tracks, $_ if not exists $track_hash{ $_->{ 'track' } } } @tracks;
5112 print STDERR qq(WARNING: track not found in config file: "$options->{ 'config_file' }"\n) if scalar @tracks == scalar @new_tracks;
5114 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
5116 $fh_out = Maasha::Common::write_open( $options->{ 'config_file' } );
5118 map { Maasha::UCSC::ucsc_config_put_entry( $_, $fh_out ) } @new_tracks;
5122 # ---- locate track in database ----
5124 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5126 foreach $track ( sort keys %track_hash )
5128 if ( Maasha::SQL::table_exists( $dbh, $track ) )
5130 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5131 Maasha::SQL::delete_table( $dbh, $track );
5132 print STDERR "done.\n" if $options->{ 'verbose' };
5136 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
5140 Maasha::SQL::disconnect( $dbh );
5142 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
5146 sub script_rename_keys
5148 # Martin A. Hansen, August 2007.
5150 # Rename keys in stream.
5152 my ( $in, # handle to in stream
5153 $out, # handle to out stream
5154 $options, # options hash
5161 while ( $record = get_record( $in ) )
5163 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5165 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
5167 delete $record->{ $options->{ "keys" }->[ 0 ] };
5170 put_record( $record, $out );
5175 sub script_uniq_vals
5177 # Martin A. Hansen, August 2007.
5179 # Find unique values in stream.
5181 my ( $in, # handle to in stream
5182 $out, # handle to out stream
5183 $options, # options hash
5188 my ( %hash, $record );
5190 while ( $record = get_record( $in ) )
5192 if ( $record->{ $options->{ "key" } } )
5194 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5196 put_record( $record, $out );
5198 $hash{ $record->{ $options->{ "key" } } } = 1;
5200 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5202 put_record( $record, $out );
5206 $hash{ $record->{ $options->{ "key" } } } = 1;
5211 put_record( $record, $out );
5217 sub script_merge_vals
5219 # Martin A. Hansen, August 2007.
5221 # Rename keys in stream.
5223 my ( $in, # handle to in stream
5224 $out, # handle to out stream
5225 $options, # options hash
5230 my ( $record, @join, $i );
5232 $options->{ "delimit" } ||= '_';
5234 while ( $record = get_record( $in ) )
5236 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5238 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5240 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5241 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5244 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5247 put_record( $record, $out );
5252 sub script_merge_records
5254 # Martin A. Hansen, July 2008.
5256 # Merges records in the stream based on identical values of two given keys.
5258 my ( $in, # handle to in stream
5259 $out, # handle to out stream
5260 $options, # options hash
5265 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5266 $num1, $num2, $num, $cmp, $i );
5268 $merge = $options->{ "merge" } || "AandB";
5270 $file1 = "$BP_TMP/merge_records1.tmp";
5271 $file2 = "$BP_TMP/merge_records2.tmp";
5273 $fh1 = Maasha::Common::write_open( $file1 );
5274 $fh2 = Maasha::Common::write_open( $file2 );
5276 $key1 = $options->{ "keys" }->[ 0 ];
5277 $key2 = $options->{ "keys" }->[ 1 ];
5279 $num = $key2 =~ s/n$//;
5283 while ( $record = get_record( $in ) )
5285 if ( exists $record->{ $key1 } )
5288 @vals1 = $record->{ $key1 };
5290 delete $record->{ $key1 };
5292 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5294 print $fh1 join( "\t", @vals1 ), "\n";
5298 elsif ( exists $record->{ $key2 } )
5301 @vals2 = $record->{ $key2 };
5303 delete $record->{ $key2 };
5305 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5307 print $fh2 join( "\t", @vals2 ), "\n";
5318 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5319 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5323 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5324 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5327 $fh1 = Maasha::Common::read_open( $file1 );
5328 $fh2 = Maasha::Common::read_open( $file2 );
5330 @vals1 = Maasha::Common::get_fields( $fh1 );
5331 @vals2 = Maasha::Common::get_fields( $fh2 );
5333 while ( $num1 > 0 and $num2 > 0 )
5338 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5340 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5345 if ( $merge =~ /^(AorB|AnotB)$/ )
5347 for ( $i = 0; $i < @keys1; $i++ ) {
5348 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5351 put_record( $record, $out );
5354 @vals1 = Maasha::Common::get_fields( $fh1 );
5359 if ( $merge =~ /^(BorA|BnotA)$/ )
5361 for ( $i = 0; $i < @keys2; $i++ ) {
5362 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5365 put_record( $record, $out );
5368 @vals2 = Maasha::Common::get_fields( $fh2 );
5373 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5375 for ( $i = 0; $i < @keys1; $i++ ) {
5376 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5379 for ( $i = 1; $i < @keys2; $i++ ) {
5380 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5383 put_record( $record, $out );
5386 @vals1 = Maasha::Common::get_fields( $fh1 );
5387 @vals2 = Maasha::Common::get_fields( $fh2 );
5399 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5403 for ( $i = 0; $i < @keys1; $i++ ) {
5404 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5407 put_record( $record, $out );
5410 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5414 for ( $i = 0; $i < @keys2; $i++ ) {
5415 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5418 put_record( $record, $out );
5425 # Martin A. Hansen, August 2007.
5427 # Grab for records in stream.
5429 my ( $in, # handle to in stream
5430 $out, # handle to out stream
5431 $options, # options hash
5436 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5438 $keys = $options->{ 'keys' };
5439 $vals_only = $options->{ 'vals_only' };
5440 $keys_only = $options->{ 'keys_only' };
5441 $invert = $options->{ 'invert' };
5443 if ( $options->{ 'patterns' } )
5445 $patterns = [ split ",", $options->{ 'patterns' } ];
5447 elsif ( -f $options->{ 'patterns_in' } )
5449 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5451 elsif ( $options->{ 'regex' } )
5453 if ( $options->{ 'case_insensitive' } ) {
5454 $regex = qr/$options->{ 'regex' }/i;
5456 $regex = qr/$options->{ 'regex' }/;
5459 elsif ( -f $options->{ 'exact_in' } )
5461 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5463 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5467 elsif ( $options->{ 'eval' } )
5469 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5477 while ( $record = get_record( $in ) )
5481 if ( %lookup_hash ) {
5482 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5483 } elsif ( $patterns ) {
5484 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5485 } elsif ( $regex ) {
5486 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5488 $found = grab_eval( $key, $op, $val, $record );
5491 if ( $found and not $invert ) {
5492 put_record( $record, $out );
5493 } elsif ( not $found and $invert ) {
5494 put_record( $record, $out );
5502 # Martin A. Hansen, August 2007.
5504 # Evaluate extression for records in stream.
5506 my ( $in, # handle to in stream
5507 $out, # handle to out stream
5508 $options, # options hash
5513 my ( $record, $eval_key, @keys, $eval_val );
5515 while ( $record = get_record( $in ) )
5517 if ( $options->{ "eval" } )
5519 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5526 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5528 @keys = grep { exists $record->{ $_ } } @keys;
5531 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5533 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5537 warn qq(WARNING: Bad compute expression: "$options->{ 'eval' }"\n);
5541 put_record( $record, $out );
5548 # Martin A. Hansen, June 2008.
5552 my ( $in, # handle to in stream
5553 $out, # handle to out stream
5554 $options, # options hash
5559 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5561 while ( $record = get_record( $in ) )
5565 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5567 push @rows, $record->{ $key };
5571 push @matrix, [ @rows ];
5576 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5578 foreach $row ( @matrix )
5580 for ( $i = 0; $i < @{ $row }; $i++ ) {
5581 $record->{ "V$i" } = $row->[ $i ];
5584 put_record( $record, $out );
5589 sub script_add_ident
5591 # Martin A. Hansen, May 2008.
5593 # Add a unique identifier to each record in stream.
5595 my ( $in, # handle to in stream
5596 $out, # handle to out stream
5597 $options, # options hash
5602 my ( $record, $key, $prefix, $i );
5604 $key = $options->{ "key" } || "ID";
5605 $prefix = $options->{ "prefix" } || "ID";
5609 while ( $record = get_record( $in ) )
5611 $record->{ $key } = sprintf( "$prefix%08d", $i );
5613 put_record( $record, $out );
5620 sub script_count_records
5622 # Martin A. Hansen, August 2007.
5624 # Count records in stream.
5626 my ( $in, # handle to in stream
5627 $out, # handle to out stream
5628 $options, # options hash
5633 my ( $record, $count, $result, $fh, $line );
5637 if ( $options->{ "no_stream" } )
5639 while ( $line = <$in> )
5643 $count++ if $line eq "---";
5648 while ( $record = get_record( $in ) )
5650 put_record( $record, $out );
5656 $result = { "RECORDS_COUNT" => $count };
5658 $fh = write_stream( $options->{ "data_out" } );
5660 put_record( $result, $fh );
5666 sub script_random_records
5668 # Martin A. Hansen, August 2007.
5670 # Pick a number or random records from stream.
5672 my ( $in, # handle to in stream
5673 $out, # handle to out stream
5674 $options, # options hash
5679 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5681 $options->{ "num" } ||= 10;
5683 $tmp_file = "$BP_TMP/random_records.tmp";
5685 $fh_out = Maasha::Common::write_open( $tmp_file );
5689 while ( $record = get_record( $in ) )
5691 put_record( $record, $fh_out );
5701 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5703 while ( $i < $options->{ "num" } )
5705 $rand = int( rand( $count ) );
5707 if ( not exists $rand_hash{ $rand } )
5709 $rand_hash{ $rand } = 1;
5711 $max = $rand if $rand > $max;
5717 $fh_in = Maasha::Common::read_open( $tmp_file );
5721 while ( $record = get_record( $fh_in ) )
5723 put_record( $record, $out ) if exists $rand_hash{ $count };
5725 last if $count == $max;
5736 sub script_sort_records
5738 # Martin A. Hansen, August 2007.
5740 # Sort to sort records according to keys.
5742 my ( $in, # handle to in stream
5743 $out, # handle to out stream
5744 $options, # options hash
5749 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5751 foreach $key ( @{ $options->{ "keys" } } )
5753 if ( $key =~ s/n$// ) {
5754 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5756 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5760 $sort_str = join " or ", @sort_cmd;
5761 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5763 while ( $record = get_record( $in ) ) {
5764 push @records, $record;
5767 @records = sort $sort_sub @records;
5769 if ( $options->{ "reverse" } )
5771 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5772 put_record( $records[ $i ], $out );
5777 for ( $i = 0; $i < scalar @records; $i++ ) {
5778 put_record( $records[ $i ], $out );
5784 sub script_count_vals
5786 # Martin A. Hansen, August 2007.
5788 # Count records in stream.
5790 my ( $in, # handle to in stream
5791 $out, # handle to out stream
5792 $options, # options hash
5797 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5799 $tmp_file = "$BP_TMP/count_cache.tmp";
5801 $fh_out = Maasha::Common::write_open( $tmp_file );
5806 while ( $record = get_record( $in ) )
5808 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5810 push @records, $record;
5812 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5814 map { put_record( $_, $fh_out ) } @records;
5821 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5832 $fh_in = Maasha::Common::read_open( $tmp_file );
5834 while ( $record = get_record( $fh_in ) )
5836 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5838 put_record( $record, $out );
5840 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5848 foreach $record ( @records )
5850 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5852 put_record( $record, $out );
5859 sub script_plot_histogram
5861 # Martin A. Hansen, September 2007.
5863 # Plot a simple histogram for a given key using GNU plot.
5865 my ( $in, # handle to in stream
5866 $out, # handle to out stream
5867 $options, # options hash
5872 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5874 $options->{ "title" } ||= "Histogram";
5875 $options->{ "sort" } ||= "num";
5877 while ( $record = get_record( $in ) )
5879 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5881 put_record( $record, $out ) if not $options->{ "no_stream" };
5884 if ( $options->{ "sort" } eq "num" ) {
5885 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5887 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5890 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5892 $fh = write_stream( $options->{ "data_out" } );
5894 print $fh "$_\n" foreach @{ $result };
5900 sub script_plot_lendist
5902 # Martin A. Hansen, August 2007.
5904 # Plot length distribution using GNU plot.
5906 my ( $in, # handle to in stream
5907 $out, # handle to out stream
5908 $options, # options hash
5913 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5915 $options->{ "title" } ||= "Length Distribution";
5917 while ( $record = get_record( $in ) )
5919 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5921 put_record( $record, $out ) if not $options->{ "no_stream" };
5924 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5926 for ( $i = 0; $i < $max; $i++ ) {
5927 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5930 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5932 $fh = write_stream( $options->{ "data_out" } );
5934 print $fh "$_\n" foreach @{ $result };
5940 sub script_plot_chrdist
5942 # Martin A. Hansen, August 2007.
5944 # Plot chromosome distribution using GNU plot.
5946 my ( $in, # handle to in stream
5947 $out, # handle to out stream
5948 $options, # options hash
5953 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5955 $options->{ "title" } ||= "Chromosome Distribution";
5957 while ( $record = get_record( $in ) )
5959 if ( $record->{ "CHR" } ) { # generic
5960 $data_hash{ $record->{ "CHR" } }++;
5961 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5962 $data_hash{ $record->{ "S_ID" } }++;
5963 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5964 $data_hash{ $record->{ "S_ID" } }++;
5965 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5966 $data_hash{ $record->{ "S_ID" } }++;
5969 put_record( $record, $out ) if not $options->{ "no_stream" };
5972 foreach $elem ( keys %data_hash )
5976 $sort_key =~ s/chr//i;
5978 $sort_key =~ s/^X(.*)/99$1/;
5979 $sort_key =~ s/^Y(.*)/99$1/;
5980 $sort_key =~ s/^Z(.*)/999$1/;
5981 $sort_key =~ s/^M(.*)/9999$1/;
5982 $sort_key =~ s/^U(.*)/99999$1/;
5984 $count = $sort_key =~ tr/_//;
5986 $sort_key =~ s/_.*/"999999" x $count/ex;
5988 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5991 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5993 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5995 $fh = write_stream( $options->{ "data_out" } );
5997 print $fh "$_\n" foreach @{ $result };
6003 sub script_plot_karyogram
6005 # Martin A. Hansen, August 2007.
6007 # Plot hits on karyogram.
6009 my ( $in, # handle to in stream
6010 $out, # handle to out stream
6011 $options, # options hash
6016 my ( %options, $record, @data, $fh, $result, %data_hash );
6018 $options->{ "genome" } ||= "human";
6019 $options->{ "feat_color" } ||= "black";
6021 while ( $record = get_record( $in ) )
6023 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
6025 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
6028 put_record( $record, $out ) if not $options->{ "no_stream" };
6031 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
6033 $fh = write_stream( $options->{ "data_out" } );
6041 sub script_plot_matches
6043 # Martin A. Hansen, August 2007.
6045 # Plot matches in 2D generating a dotplot.
6047 my ( $in, # handle to in stream
6048 $out, # handle to out stream
6049 $options, # options hash
6054 my ( $record, @data, $fh, $result, %data_hash );
6056 $options->{ "direction" } ||= "both";
6058 while ( $record = get_record( $in ) )
6060 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
6061 push @data, $record;
6064 put_record( $record, $out ) if not $options->{ "no_stream" };
6067 $options->{ "title" } ||= "plot_matches";
6068 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
6069 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
6071 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
6073 $fh = write_stream( $options->{ "data_out" } );
6075 print $fh "$_\n" foreach @{ $result };
6081 sub script_length_vals
6083 # Martin A. Hansen, August 2007.
6085 # Determine the length of the value for given keys.
6087 my ( $in, # handle to in stream
6088 $out, # handle to out stream
6089 $options, # options hash
6094 my ( $record, $key );
6096 while ( $record = get_record( $in ) )
6098 foreach $key ( @{ $options->{ "keys" } } )
6100 if ( $record->{ $key } ) {
6101 $record->{ $key . "_LEN" } = length $record->{ $key };
6105 put_record( $record, $out );
6112 # Martin A. Hansen, August 2007.
6114 # Calculates the sums for values of given keys.
6116 my ( $in, # handle to in stream
6117 $out, # handle to out stream
6118 $options, # options hash
6123 my ( $record, $key, %sum_hash, $fh );
6125 while ( $record = get_record( $in ) )
6127 foreach $key ( @{ $options->{ "keys" } } )
6129 if ( $record->{ $key } ) {
6130 $sum_hash{ $key } += $record->{ $key };
6134 put_record( $record, $out ) if not $options->{ "no_stream" };
6137 $fh = write_stream( $options->{ "data_out" } );
6139 foreach $key ( @{ $options->{ "keys" } } ) {
6140 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
6147 sub script_mean_vals
6149 # Martin A. Hansen, August 2007.
6151 # Calculate the mean of values of given keys.
6153 my ( $in, # handle to in stream
6154 $out, # handle to out stream
6155 $options, # options hash
6160 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
6162 while ( $record = get_record( $in ) )
6164 foreach $key ( @{ $options->{ "keys" } } )
6166 if ( $record->{ $key } )
6168 $sum_hash{ $key } += $record->{ $key };
6169 $count_hash{ $key }++;
6173 put_record( $record, $out ) if not $options->{ "no_stream" };
6176 $fh = write_stream( $options->{ "data_out" } );
6178 foreach $key ( @{ $options->{ "keys" } } )
6180 if ( $count_hash{ $key } ) {
6181 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6186 put_record( { $key . "_MEAN" => $mean } , $fh );
6193 sub script_median_vals
6195 # Martin A. Hansen, March 2008.
6197 # Calculate the median values of given keys.
6199 my ( $in, # handle to in stream
6200 $out, # handle to out stream
6201 $options, # options hash
6206 my ( $record, $key, %median_hash, $median, $fh );
6208 while ( $record = get_record( $in ) )
6210 foreach $key ( @{ $options->{ "keys" } } ) {
6211 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6214 put_record( $record, $out ) if not $options->{ "no_stream" };
6217 $fh = write_stream( $options->{ "data_out" } );
6219 foreach $key ( @{ $options->{ "keys" } } )
6221 if ( $median_hash{ $key } ) {
6222 $median = Maasha::Calc::median( $median_hash{ $key } );
6227 put_record( { $key . "_MEDIAN" => $median } , $fh );
6236 # Martin A. Hansen, February 2008.
6238 # Determine the maximum values of given keys.
6240 my ( $in, # handle to in stream
6241 $out, # handle to out stream
6242 $options, # options hash
6247 my ( $record, $key, $fh, %max_hash, $max_record );
6249 while ( $record = get_record( $in ) )
6251 foreach $key ( @{ $options->{ "keys" } } )
6253 if ( $record->{ $key } )
6255 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6259 put_record( $record, $out ) if not $options->{ "no_stream" };
6262 $fh = write_stream( $options->{ "data_out" } );
6264 foreach $key ( @{ $options->{ "keys" } } )
6266 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6269 put_record( $max_record, $fh );
6277 # Martin A. Hansen, February 2008.
6279 # Determine the minimum values of given keys.
6281 my ( $in, # handle to in stream
6282 $out, # handle to out stream
6283 $options, # options hash
6288 my ( $record, $key, $fh, %min_hash, $min_record );
6290 while ( $record = get_record( $in ) )
6292 foreach $key ( @{ $options->{ "keys" } } )
6294 if ( defined $record->{ $key } )
6296 if ( exists $min_hash{ $key } ) {
6297 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6299 $min_hash{ $key } = $record->{ $key };
6304 put_record( $record, $out ) if not $options->{ "no_stream" };
6307 $fh = write_stream( $options->{ "data_out" } );
6309 foreach $key ( @{ $options->{ "keys" } } )
6311 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6314 put_record( $min_record, $fh );
6320 sub script_upload_to_ucsc
6322 # Martin A. Hansen, August 2007.
6324 # Calculate the mean of values of given keys.
6326 # This routine has developed into the most ugly hack. Do something!
6328 my ( $in, # handle to in stream
6329 $out, # handle to out stream
6330 $options, # options hash
6335 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
6337 $options->{ "short_label" } ||= $options->{ 'table' };
6338 $options->{ "long_label" } ||= $options->{ 'table' };
6339 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6340 $options->{ "priority" } ||= 1;
6341 $options->{ "visibility" } ||= "pack";
6342 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6343 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6345 $file = "$BP_TMP/ucsc_upload.tmp";
6353 $fh_out = Maasha::Common::write_open( $file );
6355 while ( $record = get_record( $in ) )
6357 put_record( $record, $out ) if not $options->{ "no_stream" };
6359 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6361 $vals = $record->{ "VALS" };
6364 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6365 print $fh_out "$vals\n";
6367 $format = "WIGGLE" if not $format;
6369 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6371 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6372 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6376 $format = "PSL" if not $format;
6378 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6380 # chrom chromStart chromEnd name score strand size secStr conf
6382 print $fh_out join ( "\t",
6384 $record->{ "CHR_BEG" },
6385 $record->{ "CHR_END" } + 1,
6386 $record->{ "Q_ID" },
6387 $record->{ "SCORE" },
6388 $record->{ "STRAND" },
6389 $record->{ "SIZE" },
6390 $record->{ "SEC_STRUCT" },
6391 $record->{ "CONF" },
6394 $format = "BED_SS" if not $format;
6396 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6398 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6400 $format = "BED" if not $format;
6401 $columns = $record->{ "BED_COLS" } if not $columns;
6403 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6405 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6407 $format = "BED" if not $format;
6408 $columns = 6 if not $columns;
6410 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6412 $record->{ "CHR" } = $record->{ "S_ID" };
6413 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6414 $record->{ "CHR_END" } = $record->{ "S_END" };
6415 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6417 $format = "BED" if not $format;
6418 $columns = 6 if not $columns;
6420 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6422 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6424 $record->{ "CHR" } = $record->{ "S_ID" };
6425 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6426 $record->{ "CHR_END" } = $record->{ "S_END" };
6427 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6428 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6430 $format = "BED" if not $format;
6431 $columns = 6 if not $columns;
6433 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6436 if ( $i == $options->{ "chunk_size" } )
6440 if ( $format eq "BED" ) {
6441 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6442 } elsif ( $format eq "PSL" ) {
6443 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6452 $fh_out = Maasha::Common::write_open( $file );
6460 if ( exists $options->{ "database" } and $options->{ "table" } )
6462 if ( $format eq "BED" )
6464 $type = "bed $columns";
6466 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6468 elsif ( $format eq "BED_SS" )
6470 $options->{ "sec_struct" } = 1;
6472 $type = "sec_struct";
6474 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6476 elsif ( $format eq "PSL" )
6480 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6482 elsif ( $format eq "WIGGLE" )
6484 $options->{ "visibility" } = "full";
6486 $wig_file = "$options->{ 'table' }.wig";
6487 $wib_file = "$options->{ 'table' }.wib";
6489 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6491 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6493 if ( $options->{ 'verbose' } ) {
6494 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6496 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6499 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6507 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6512 Maasha::UCSC::update_my_tracks( $options, $type );
6517 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6522 # Martin A. Hansen, November 2009.
6524 # Uses keys from a lookup hash to search records. Optionally, a list of
6525 # keys can be given so the lookup is limited to these, also, flags
6526 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6527 # succeeded, else 0.
6529 my ( $lookup_hash, # hashref with patterns
6531 $keys, # list of keys - OPTIONAL
6532 $vals_only, # only vals flag - OPTIONAL
6533 $keys_only, # only keys flag - OPTIONAL
6540 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6544 if ( not $vals_only ) {
6545 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6548 if ( not $keys_only ) {
6549 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6559 # Martin A. Hansen, November 2009.
6561 # Uses patterns to match records containing the pattern as a substring.
6562 # Returns 1 if the record is matched, else 0.
6564 my ( $patterns, # list of patterns
6566 $keys, # list of keys - OPTIONAL
6567 $vals_only, # only vals flag - OPTIONAL
6568 $keys_only, # only keys flag - OPTIONAL
6575 foreach $pattern ( @{ $patterns } )
6579 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6583 if ( not $vals_only ) {
6584 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6587 if ( not $keys_only ) {
6588 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6599 # Martin A. Hansen, November 2009.
6601 # Uses regex to match records.
6602 # Returns 1 if the record is matched, else 0.
6604 my ( $regex, # regex to match
6606 $keys, # list of keys - OPTIONAL
6607 $vals_only, # only vals flag - OPTIONAL
6608 $keys_only, # only keys flag - OPTIONAL
6615 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6619 if ( not $vals_only ) {
6620 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6623 if ( not $keys_only ) {
6624 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6634 # Martin A. Hansen, November 2009.
6636 # Test if the value of a given record key evaluates according
6637 # to a given operator. Returns 1 if eval is OK, else 0.
6639 my ( $key, # record key
6647 if ( defined $record->{ $key } )
6649 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6650 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6651 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6652 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6653 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6654 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6655 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6656 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6665 # Martin A. Hansen, July 2008.
6667 # Given a biopiece record converts it to a FASTA record.
6668 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6669 # tried in that order.
6671 my ( $record, # record
6676 my ( $seq_name, $seq );
6678 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6679 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6681 if ( defined $seq_name and defined $seq ) {
6682 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6691 # Martin A. Hansen, July 2007.
6693 # Opens a stream to STDIN or a file,
6695 my ( $path, # path - OPTIONAL
6698 # Returns filehandle.
6702 if ( not -t STDIN ) {
6703 $fh = Maasha::Common::read_stdin();
6704 } elsif ( not $path ) {
6705 # Maasha::Common::error( qq(no data stream) );
6707 $fh = Maasha::Common::read_open( $path );
6710 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6718 # Martin A. Hansen, August 2007.
6720 # Opens a stream to STDOUT or a file.
6722 my ( $path, # path - OPTIONAL
6723 $gzip, # compress data - OPTIONAL
6726 # Returns filehandle.
6731 $fh = Maasha::Common::write_open( $path, $gzip );
6733 $fh = Maasha::Common::write_stdout();
6742 # Martin A. Hansen, July 2007.
6744 # Reads one record at a time and converts that record
6745 # to a Perl data structure (a hash) which is returned.
6747 my ( $fh, # handle to stream
6752 my ( $block, @lines, $line, $key, $value, %record );
6754 local $/ = "\n---\n";
6760 return if not defined $block;
6762 @lines = split "\n", $block;
6764 foreach $line ( @lines )
6766 ( $key, $value ) = split ": ", $line, 2;
6768 $record{ $key } = $value;
6771 return wantarray ? %record : \%record;
6777 # Martin A. Hansen, July 2007.
6779 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6781 my ( $data, # data structure
6782 $fh, # file handle - OPTIONAL
6787 if ( scalar keys %{ $data } )
6791 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6796 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6807 # Martin A. Hansen, November 2007.
6809 # Extracts files from an explicit GetOpt::Long argument
6810 # allowing for the use of glob. E.g.
6811 # --data_in=test.fna
6812 # --data_in=test.fna,test2.fna
6814 # --data_in=test.fna,/dir/*.fna
6816 my ( $option, # option from GetOpt::Long
6821 my ( $elem, @files );
6823 foreach $elem ( split ",", $option )
6827 } elsif ( $elem =~ /\*/ ) {
6828 push @files, glob( $elem );
6832 return wantarray ? @files : \@files;
6838 # Martin A. Hansen, April 2008.
6840 # Removes temporary directory and exits gracefully.
6841 # This subroutine is meant to be run always as the last
6842 # thing even if a script is dies or is interrupted
6845 my ( $sig, # signal from the %SIG
6848 # print STDERR "signal->$sig<-\n";
6856 if ( $sig =~ /MAASHA_ERROR/ ) {
6857 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6858 } elsif ( $sig eq "INT" ) {
6859 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6860 } elsif ( $sig eq "TERM" ) {
6861 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6863 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6875 # Martin A. Hansen, July 2008.
6877 # Cleans out any unused temporary files and directories in BP_TMP.
6881 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6883 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6885 $curr_pid = Maasha::Common::get_processid();
6887 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6889 foreach $dir ( @dirs )
6891 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6897 if ( $user eq Maasha::Common::get_user() )
6899 if ( not Maasha::Common::process_running( $pid ) )
6901 # print STDERR "Removing stale dir: $dir\n";
6902 Maasha::Common::dir_remove( $dir );
6904 elsif ( $pid == $curr_pid )
6906 # print STDERR "Removing current dir: $dir\n";
6907 # Maasha::Common::dir_remove( $dir );
6921 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<