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" )
683 elsif ( $script eq "write_psl" )
691 elsif ( $script eq "write_fixedstep" )
699 elsif ( $script eq "write_2bit" )
707 elsif ( $script eq "write_solid" )
716 elsif ( $script eq "write_ucsc_config" )
723 elsif ( $script eq "plot_seqlogo" )
730 elsif ( $script eq "plot_phastcons_profiles" )
745 elsif ( $script eq "analyze_vals" )
752 elsif ( $script eq "head_records" )
758 elsif ( $script eq "remove_keys" )
765 elsif ( $script eq "remove_adaptor" )
774 elsif ( $script eq "remove_mysql_tables" )
785 elsif ( $script eq "remove_ucsc_tracks" )
797 elsif ( $script eq "rename_keys" )
803 elsif ( $script eq "uniq_vals" )
810 elsif ( $script eq "merge_vals" )
817 elsif ( $script eq "merge_records" )
824 elsif ( $script eq "grab" )
839 elsif ( $script eq "compute" )
845 elsif ( $script eq "add_ident" )
852 elsif ( $script eq "count_records" )
859 elsif ( $script eq "random_records" )
865 elsif ( $script eq "sort_records" )
872 elsif ( $script eq "count_vals" )
878 elsif ( $script eq "plot_histogram" )
891 elsif ( $script eq "plot_lendist" )
903 elsif ( $script eq "plot_chrdist" )
914 elsif ( $script eq "plot_karyogram" )
923 elsif ( $script eq "plot_matches" )
935 elsif ( $script eq "length_vals" )
941 elsif ( $script eq "sum_vals" )
949 elsif ( $script eq "mean_vals" )
957 elsif ( $script eq "median_vals" )
965 elsif ( $script eq "max_vals" )
973 elsif ( $script eq "min_vals" )
981 elsif ( $script eq "upload_to_ucsc" )
1005 # print STDERR Dumper( \@options );
1012 # print STDERR Dumper( \%options );
1014 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
1015 return wantarray ? %options : \%options;
1018 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
1019 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
1020 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
1021 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
1022 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
1023 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
1024 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
1025 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
1026 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
1027 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
1028 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
1030 # ---- check arguments ----
1032 if ( $options{ 'data_in' } )
1034 $options{ "files" } = getopt_files( $options{ 'data_in' } );
1036 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
1039 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
1041 # print STDERR Dumper( \%options );
1043 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1045 foreach $opt ( keys %options )
1047 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1049 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1051 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1053 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1055 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1057 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1059 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1061 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1063 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1065 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1067 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1069 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1071 elsif ( $opt eq "genome" and $script ne "format_genome" )
1073 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1074 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1076 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1077 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1080 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1082 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1084 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1086 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1088 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1090 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1092 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1094 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1096 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1098 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1102 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1103 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1104 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1105 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1106 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1107 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1108 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1109 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1110 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" };
1111 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1112 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" };
1114 if ( $script eq "upload_to_ucsc" )
1116 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1117 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1120 return wantarray ? %options : \%options;
1124 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1127 sub script_print_usage
1129 # Martin A. Hansen, January 2008.
1131 # Retrieves usage information from file and
1132 # prints this nicely formatted.
1134 my ( $in, # handle to in stream
1135 $out, # handle to out stream
1136 $options, # options hash
1141 my ( $file, $wiki, $lines );
1143 if ( $options->{ 'data_in' } ) {
1144 $file = $options->{ 'data_in' };
1146 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1149 $wiki = Maasha::Gwiki::gwiki_read( $file );
1151 if ( not $options->{ "help" } ) {
1152 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1155 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1157 print STDERR "$_\n" foreach @{ $lines };
1163 sub script_list_biopieces
1165 # Martin A. Hansen, January 2008.
1167 # Prints the synopsis from the usage for each of the biopieces.
1169 my ( $in, # handle to in stream
1170 $out, # handle to out stream
1171 $options, # options hash
1176 my ( @files, $file, $wiki, $program, $synopsis );
1178 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1180 foreach $file ( sort @files )
1182 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1186 $wiki = Maasha::Gwiki::gwiki_read( $file );
1188 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1189 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1191 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1192 $synopsis =~ s/!(\w)/$1/g;
1194 printf( "%-30s%s\n", $program, $synopsis );
1202 sub script_list_genomes
1204 # Martin A. Hansen, January 2008.
1206 # Prints the synopsis from the usage for each of the biopieces.
1208 my ( $in, # handle to in stream
1209 $out, # handle to out stream
1210 $options, # options hash
1215 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1217 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1219 foreach $genome ( @genomes )
1221 next if $genome =~ /\.$/;
1223 @formats = Maasha::Common::ls_dirs( $genome );
1225 foreach $format ( @formats )
1227 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1229 $hash{ $1 }{ $2 } = 1;
1238 map { push @row, $_ } sort keys %found;
1240 print join( "\t", @row ), "\n";
1242 foreach $genome ( sort keys %hash )
1246 foreach $format ( sort keys %found )
1248 if ( exists $hash{ $genome }{ $format } ) {
1255 print join( "\t", @row ), "\n";
1260 sub script_read_fasta
1262 # Martin A. Hansen, August 2007.
1264 # Read sequences from FASTA file.
1266 my ( $in, # handle to in stream
1267 $out, # handle to out stream
1268 $options, # options hash
1273 my ( $record, $file, $data_in, $entry, $num );
1275 while ( $record = get_record( $in ) ) {
1276 put_record( $record, $out );
1281 foreach $file ( @{ $options->{ "files" } } )
1283 $data_in = Maasha::Common::read_open( $file );
1285 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1287 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1290 SEQ_NAME => $entry->[ SEQ_NAME ],
1291 SEQ => $entry->[ SEQ ],
1292 SEQ_LEN => length $entry->[ SEQ ],
1295 put_record( $record, $out );
1298 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1308 close $data_in if $data_in;
1314 # Martin A. Hansen, August 2007.
1316 # Read table or table columns from stream or file.
1318 my ( $in, # handle to in stream
1319 $out, # handle to out stream
1320 $options, # options hash
1325 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1327 $options->{ 'delimit' } ||= '\s+';
1329 while ( $record = get_record( $in ) ) {
1330 put_record( $record, $out );
1333 $skip = $options->{ 'skip' } ||= 0;
1336 foreach $file ( @{ $options->{ "files" } } )
1338 $data_in = Maasha::Common::read_open( $file );
1340 while ( $line = <$data_in> )
1348 next if $line =~ /^#|^$/;
1355 @fields = split /$options->{'delimit'}/, $line;
1357 if ( $options->{ "cols" } ) {
1358 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1363 for ( $i = 0; $i < @fields2; $i++ )
1365 if ( $options->{ "keys" }->[ $i ] ) {
1366 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1368 $record->{ "V" . $i } = $fields2[ $i ];
1372 put_record( $record, $out );
1374 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1384 close $data_in if $data_in;
1390 # Martin A. Hansen, August 2007.
1392 # Read psl table from stream or file.
1394 my ( $in, # handle to in stream
1395 $out, # handle to out stream
1396 $options, # options hash
1401 my ( $record, $file, $data_in, $num );
1403 while ( $record = get_record( $in ) ) {
1404 put_record( $record, $out );
1409 foreach $file ( @{ $options->{ "files" } } )
1411 $data_in = Maasha::Common::read_open( $file );
1413 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1415 put_record( $record, $out );
1417 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1429 # Martin A. Hansen, August 2007.
1431 # Read bed table from stream or file.
1433 my ( $in, # handle to in stream
1434 $out, # handle to out stream
1435 $options, # options hash
1440 my ( $file, $record, $entry, $data_in, $num );
1442 while ( $record = get_record( $in ) ) {
1443 put_record( $record, $out );
1448 foreach $file ( @{ $options->{ "files" } } )
1450 $data_in = Maasha::Common::read_open( $file );
1452 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1454 put_record( $entry, $out );
1456 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1466 close $data_in if $data_in;
1470 sub script_read_fixedstep
1472 # Martin A. Hansen, Juli 2008.
1474 # Read fixedstep wiggle format from stream or file.
1476 my ( $in, # handle to in stream
1477 $out, # handle to out stream
1478 $options, # options hash
1483 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1485 while ( $record = get_record( $in ) ) {
1486 put_record( $record, $out );
1491 foreach $file ( @{ $options->{ "files" } } )
1493 $data_in = Maasha::Common::read_open( $file );
1495 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1497 $head = shift @{ $entry };
1499 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1501 $record->{ "REC_TYPE" } = "fixed_step";
1502 $record->{ "CHR" } = $1;
1503 $record->{ "CHR_BEG" } = $2;
1504 $record->{ "STEP" } = $3;
1505 $record->{ "VALS" } = join ";", @{ $entry };
1508 put_record( $record, $out );
1510 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1520 close $data_in if $data_in;
1524 sub script_read_blast_tab
1526 # Martin A. Hansen, September 2007.
1528 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1530 my ( $in, # handle to in stream
1531 $out, # handle to out stream
1532 $options, # options hash
1537 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1539 while ( $record = get_record( $in ) ) {
1540 put_record( $record, $out );
1545 foreach $file ( @{ $options->{ "files" } } )
1547 $data_in = Maasha::Common::read_open( $file );
1549 while ( $line = <$data_in> )
1553 next if $line =~ /^#/;
1555 @fields = split /\t/, $line;
1557 $record->{ "REC_TYPE" } = "BLAST";
1558 $record->{ "Q_ID" } = $fields[ 0 ];
1559 $record->{ "S_ID" } = $fields[ 1 ];
1560 $record->{ "IDENT" } = $fields[ 2 ];
1561 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1562 $record->{ "MISMATCHES" } = $fields[ 4 ];
1563 $record->{ "GAPS" } = $fields[ 5 ];
1564 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1565 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1566 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1567 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1568 $record->{ "E_VAL" } = $fields[ 10 ];
1569 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1571 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1573 $record->{ "STRAND" } = '-';
1575 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1579 $record->{ "STRAND" } = '+';
1582 put_record( $record, $out );
1584 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1594 close $data_in if $data_in;
1598 sub script_read_embl
1600 # Martin A. Hansen, August 2007.
1604 my ( $in, # handle to in stream
1605 $out, # handle to out stream
1606 $options, # options hash
1611 my ( %options2, $file, $data_in, $num, $entry, $record );
1613 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1614 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1615 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1617 while ( $record = get_record( $in ) ) {
1618 put_record( $record, $out );
1623 foreach $file ( @{ $options->{ "files" } } )
1625 $data_in = Maasha::Common::read_open( $file );
1627 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1629 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1631 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1633 $record_copy = dclone $record;
1635 delete $record_copy->{ "FT" };
1637 put_record( $record_copy, $out );
1639 delete $record_copy->{ "SEQ" };
1641 foreach $feat ( keys %{ $record->{ "FT" } } )
1643 $record_copy->{ "FEAT_TYPE" } = $feat;
1645 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1647 foreach $qual ( keys %{ $feat2 } )
1649 $qual_val = join "; ", @{ $feat2->{ $qual } };
1654 $record_copy->{ $qual } = $qual_val;
1657 put_record( $record_copy, $out );
1661 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1671 close $data_in if $data_in;
1675 sub script_read_stockholm
1677 # Martin A. Hansen, August 2007.
1679 # Read Stockholm format.
1681 my ( $in, # handle to in stream
1682 $out, # handle to out stream
1683 $options, # options hash
1688 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1690 while ( $record = get_record( $in ) ) {
1691 put_record( $record, $out );
1696 foreach $file ( @{ $options->{ "files" } } )
1698 $data_in = Maasha::Common::read_open( $file );
1700 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1702 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1706 foreach $key ( keys %{ $record->{ "GF" } } ) {
1707 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1710 $record_anno->{ "ALIGN" } = $num;
1712 put_record( $record_anno, $out );
1714 foreach $seq ( @{ $record->{ "ALIGN" } } )
1716 undef $record_align;
1719 SEQ_NAME => $seq->[ 0 ],
1723 put_record( $record_align, $out );
1726 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1736 close $data_in if $data_in;
1740 sub script_read_phastcons
1742 # Martin A. Hansen, December 2007.
1744 # Read PhastCons format.
1746 my ( $in, # handle to in stream
1747 $out, # handle to out stream
1748 $options, # options hash
1753 my ( $data_in, $file, $num, $entry, @records, $record );
1755 $options->{ "min" } ||= 10;
1756 $options->{ "dist" } ||= 25;
1757 $options->{ "threshold" } ||= 0.8;
1758 $options->{ "gap" } ||= 5;
1760 while ( $record = get_record( $in ) ) {
1761 put_record( $record, $out );
1766 foreach $file ( @{ $options->{ "files" } } )
1768 $data_in = Maasha::Common::read_open( $file );
1770 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1772 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1774 foreach $record ( @records )
1776 $record->{ "REC_TYPE" } = "BED";
1777 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1779 put_record( $record, $out );
1781 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1792 close $data_in if $data_in;
1796 sub script_read_soft
1798 # Martin A. Hansen, December 2007.
1801 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1803 my ( $in, # handle to in stream
1804 $out, # handle to out stream
1805 $options, # options hash
1810 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1812 while ( $record = get_record( $in ) ) {
1813 put_record( $record, $out );
1818 foreach $file ( @{ $options->{ "files" } } )
1820 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1822 $soft_index = Maasha::NCBI::soft_index_file( $file );
1824 $fh = Maasha::Common::read_open( $file );
1826 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1828 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1830 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1832 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1834 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1836 foreach $sample ( @samples )
1839 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1841 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1843 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1845 foreach $record ( @{ $records } )
1847 put_record( $record, $out );
1849 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1854 $old_end = $sample->{ "LINE_END" };
1862 close $data_in if $data_in;
1869 # Martin A. Hansen, February 2008.
1872 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1874 my ( $in, # handle to in stream
1875 $out, # handle to out stream
1876 $options, # options hash
1881 my ( $data_in, $file, $fh, $num, $record, $entry );
1883 while ( $record = get_record( $in ) ) {
1884 put_record( $record, $out );
1889 foreach $file ( @{ $options->{ "files" } } )
1891 $fh = Maasha::Common::read_open( $file );
1893 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1895 put_record( $entry, $out );
1897 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1907 close $data_in if $data_in;
1911 sub script_read_2bit
1913 # Martin A. Hansen, March 2008.
1915 # Read sequences from 2bit file.
1917 my ( $in, # handle to in stream
1918 $out, # handle to out stream
1919 $options, # options hash
1924 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1926 $mask = 1 if not $options->{ "no_mask" };
1928 while ( $record = get_record( $in ) ) {
1929 put_record( $record, $out );
1934 foreach $file ( @{ $options->{ "files" } } )
1936 $data_in = Maasha::Common::read_open( $file );
1938 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1940 foreach $line ( @{ $toc } )
1942 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1943 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1944 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1946 put_record( $record, $out );
1948 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1958 close $data_in if $data_in;
1962 sub script_read_solexa
1964 # Martin A. Hansen, March 2008.
1966 # Read Solexa sequence reads from file.
1968 my ( $in, # handle to in stream
1969 $out, # handle to out stream
1970 $options, # options hash
1975 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1977 $options->{ "format" } ||= "octal";
1978 $options->{ "quality" } ||= 20;
1980 while ( $record = get_record( $in ) ) {
1981 put_record( $record, $out );
1986 foreach $file ( @{ $options->{ "files" } } )
1988 $data_in = Maasha::Common::read_open( $file );
1990 if ( $options->{ "format" } eq "octal" )
1992 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1994 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1996 put_record( $record, $out );
1998 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2005 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
2007 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2009 put_record( $record, $out );
2011 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2022 close $data_in if $data_in;
2026 sub script_read_solid
2028 # Martin A. Hansen, April 2008.
2030 # Read Solid sequence from file.
2032 my ( $in, # handle to in stream
2033 $out, # handle to out stream
2034 $options, # options hash
2039 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
2041 $options->{ "quality" } ||= 15;
2043 while ( $record = get_record( $in ) ) {
2044 put_record( $record, $out );
2049 foreach $file ( @{ $options->{ "files" } } )
2051 $data_in = Maasha::Common::read_open( $file );
2053 while ( $line = <$data_in> )
2057 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2059 @scores = split /,/, $seq_qual;
2060 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2062 for ( $i = 0; $i < @seqs; $i++ ) {
2063 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2067 REC_TYPE => 'SOLID',
2068 SEQ_NAME => $seq_name,
2070 SEQ_QUAL => join( ";", @scores ),
2071 SEQ_LEN => length $seq_cs,
2072 SEQ => join( "", @seqs ),
2073 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2076 put_record( $record, $out );
2078 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2088 close $data_in if $data_in;
2092 sub script_read_mysql
2094 # Martin A. Hansen, May 2008.
2096 # Read a MySQL query into stream.
2098 my ( $in, # handle to in stream
2099 $out, # handle to out stream
2100 $options, # options hash
2105 my ( $record, $dbh, $results );
2107 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2108 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2110 while ( $record = get_record( $in ) ) {
2111 put_record( $record, $out );
2114 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2116 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2118 Maasha::SQL::disconnect( $dbh );
2120 map { put_record( $_ ) } @{ $results };
2124 sub script_read_ucsc_config
2126 # Martin A. Hansen, November 2008.
2128 # Read track entries from UCSC Genome Browser '.ra' files.
2130 my ( $in, # handle to in stream
2131 $out, # handle to out stream
2132 $options, # options hash
2137 my ( $record, $file, $data_in, $entry, $num );
2139 while ( $record = get_record( $in ) ) {
2140 put_record( $record, $out );
2145 foreach $file ( @{ $options->{ "files" } } )
2147 $data_in = Maasha::Common::read_open( $file );
2149 while ( $record = Maasha::UCSC::ucsc_config_get_entry( $data_in ) )
2151 $record->{ 'REC_TYPE' } = "UCSC Config";
2153 put_record( $record, $out );
2155 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2165 close $data_in if $data_in;
2169 sub script_assemble_tag_contigs
2171 # Martin A. Hansen, November 2008.
2173 # Assemble tags from the stream into
2176 my ( $in, # handle to in stream
2177 $out, # handle to out stream
2178 $options, # options hash
2183 my ( $record, $new_record, %fh_hash, $fh_out, $chr, $array, $pos, $beg, $end, $score, $file, $id );
2185 while ( $record = get_record( $in ) )
2187 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2188 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2189 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2191 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2193 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2195 $fh_out = $fh_hash{ $record->{ "CHR" } };
2197 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2201 map { close $_ } keys %fh_hash;
2203 foreach $chr ( sort keys %fh_hash )
2205 $array = tag_contigs_assemble( "$BP_TMP/$chr" );
2210 while ( ( $beg, $end, $score ) = tag_contigs_scan( $array, $pos ) and $beg )
2215 CHR_END => $end - 1,
2216 Q_ID => sprintf( "TC%06d", $id ),
2218 STRAND => $record->{ 'STRAND' } || '+',
2221 put_record( $new_record, $out );
2227 unlink "$BP_TMP/$chr";
2232 sub tag_contigs_assemble
2234 # Martin A. Hansen, November 2008.
2236 # Given a BED file with entries from only one
2237 # chromosome assembles tag contigs from these
2238 # ignoring strand information. Only tags with
2239 # a score higher than the clone count over
2240 # genomic loci (the SCORE field) is included
2241 # in the tag contigs.
2247 # ======================== tag contig
2250 my ( $path, # full path to BED file
2253 # Returns an arrayref.
2255 my ( $fh, $entry, $clones, $score, @array );
2257 $fh = Maasha::Common::read_open( $path );
2259 while ( $entry = Maasha::UCSC::bed_get_entry( $fh ) )
2261 if ( $entry->{ 'Q_ID' } =~ /(\d+)$/ )
2265 $score = int( $clones / $entry->{ 'SCORE' } );
2267 map { $array[ $_ ] += $score } $entry->{ 'CHR_BEG' } .. $entry->{ 'CHR_END' } if $score >= 1;
2273 return wantarray ? @array : \@array;
2277 sub tag_contigs_scan
2279 # Martin A. Hansen, November 2008.
2281 # Scans an array with tag contigs and locates
2282 # the next contig from a given position. The
2283 # score of the tag contig is determined as the
2284 # maximum value of the tag contig. If a tag contig
2285 # is found a triple is returned with beg, end and score
2286 # otherwise an empty triple is returned.
2288 my ( $array, # array to scan
2289 $beg, # position to start scanning from
2292 # Returns an arrayref.
2294 my ( $end, $score );
2298 while ( $beg < scalar @{ $array } and not $array->[ $beg ] ) { $beg++ }
2302 while ( $array->[ $end ] )
2304 $score = Maasha::Calc::max( $score, $array->[ $end ] );
2310 return wantarray ? ( $beg, $end, $score ) : [ $beg, $end, $score ];
2312 return wantarray ? () : [];
2317 sub script_format_genome
2319 # Martin A. Hansen, Juli 2008.
2321 # Format a genome to speficed formats.
2323 my ( $in, # handle to in stream
2324 $out, # handle to out stream
2325 $options, # options hash
2330 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2332 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2333 $genome = $options->{ 'genome' };
2335 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2336 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2337 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2339 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2341 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2343 $fasta_dir = "$dir/genomes/$genome/fasta";
2347 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2349 $fasta_dir = "$dir/genomes/$genome/fasta";
2351 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2354 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2356 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2358 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2360 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2363 while ( $record = get_record( $in ) )
2365 if ( $fh_out and $entry = record2fasta( $record ) )
2367 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2369 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2371 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2373 $vals = $record->{ 'VALS' };
2377 print $fh_out "$vals\n";
2380 put_record( $record, $out ) if not $options->{ "no_stream" };
2383 foreach $format ( @{ $options->{ 'formats' } } )
2385 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2386 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2387 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2388 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2389 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2392 close $fh_out if $fh_out;
2396 sub script_length_seq
2398 # Martin A. Hansen, August 2007.
2400 # Determine the length of sequences in stream.
2402 my ( $in, # handle to in stream
2403 $out, # handle to out stream
2404 $options, # options hash
2409 my ( $record, $total );
2411 while ( $record = get_record( $in ) )
2413 if ( $record->{ "SEQ" } )
2415 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2416 $total += $record->{ "SEQ_LEN" };
2419 put_record( $record, $out ) if not $options->{ "no_stream" };
2422 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2426 sub script_uppercase_seq
2428 # Martin A. Hansen, August 2007.
2430 # Uppercases sequences in stream.
2432 my ( $in, # handle to in stream
2433 $out, # handle to out stream
2440 while ( $record = get_record( $in ) )
2442 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2444 put_record( $record, $out );
2449 sub script_shuffle_seq
2451 # Martin A. Hansen, December 2007.
2453 # Shuffle sequences in stream.
2455 my ( $in, # handle to in stream
2456 $out, # handle to out stream
2463 while ( $record = get_record( $in ) )
2465 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2467 put_record( $record, $out );
2472 sub script_analyze_seq
2474 # Martin A. Hansen, August 2007.
2476 # Analyze sequence composition of sequences in stream.
2478 my ( $in, # handle to in stream
2479 $out, # handle to out stream
2484 my ( $record, $analysis );
2486 while ( $record = get_record( $in ) )
2488 if ( $record->{ "SEQ" } )
2490 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2492 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2495 put_record( $record, $out );
2500 sub script_analyze_tags
2502 # Martin A. Hansen, August 2008.
2504 # Analyze sequence tags in stream.
2506 my ( $in, # handle to in stream
2507 $out, # handle to out stream
2512 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2514 while ( $record = get_record( $in ) )
2516 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2518 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2522 $len_hash{ length( $record->{ "SEQ" } ) }++;
2523 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2526 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2528 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2532 $len_hash{ $record->{ "BED_LEN" } }++;
2533 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2538 foreach $key ( sort { $a <=> $b } keys %len_hash )
2540 $tag_record->{ "TAG_LEN" } = $key;
2541 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2542 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2544 put_record( $tag_record, $out );
2549 sub script_complexity_seq
2551 # Martin A. Hansen, May 2008.
2553 # Generates an index calculated as the most common di-residue over
2554 # the sequence length for all sequences in stream.
2556 my ( $in, # handle to in stream
2557 $out, # handle to out stream
2562 my ( $record, $index );
2564 while ( $record = get_record( $in ) )
2566 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2568 put_record( $record, $out );
2573 sub script_oligo_freq
2575 # Martin A. Hansen, August 2007.
2577 # Determine the length of sequences in stream.
2579 my ( $in, # handle to in stream
2580 $out, # handle to out stream
2581 $options, # options hash
2586 my ( $record, %oligos, @freq_table );
2588 $options->{ "word_size" } ||= 7;
2590 while ( $record = get_record( $in ) )
2592 if ( $record->{ "SEQ" } )
2594 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2596 if ( not $options->{ "all" } )
2598 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2600 map { put_record( $_, $out ) } @freq_table;
2606 put_record( $record, $out );
2609 if ( $options->{ "all" } )
2611 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2613 map { put_record( $_, $out ) } @freq_table;
2618 sub script_create_weight_matrix
2620 # Martin A. Hansen, August 2007.
2622 # Creates a weight matrix from an alignmnet.
2624 my ( $in, # handle to in stream
2625 $out, # handle to out stream
2626 $options, # options hash
2631 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2635 while ( $record = get_record( $in ) )
2637 if ( $record->{ "SEQ" } )
2639 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2641 $res = substr $record->{ "SEQ" }, $i, 1;
2643 $freq_hash{ $i }{ $res }++;
2644 $res_hash{ $res } = 1;
2651 put_record( $record, $out );
2655 foreach $res ( sort keys %res_hash )
2659 $record->{ "V0" } = $res;
2661 for ( $i = 0; $i < keys %freq_hash; $i++ )
2663 $freq = $freq_hash{ $i }{ $res } || 0;
2665 if ( $options->{ "percent" } ) {
2666 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2669 $record->{ "V" . ( $i + 1 ) } = $freq;
2672 put_record( $record, $out );
2677 sub script_calc_bit_scores
2679 # Martin A. Hansen, March 2007.
2681 # Calculates the bit scores for each position from an alignmnet in the stream.
2683 my ( $in, # handle to in stream
2684 $out, # handle to out stream
2689 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2693 while ( $record = get_record( $in ) )
2695 if ( $record->{ "SEQ" } )
2697 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2699 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2701 $res = substr $record->{ "SEQ" }, $i, 1;
2703 next if $res =~ /-|_|~|\./;
2705 $freq_hash{ $i }{ $res }++;
2712 put_record( $record, $out );
2718 if ( $type eq "protein" ) {
2724 for ( $i = 0; $i < keys %freq_hash; $i++ )
2726 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2728 $bit_diff = $bit_max - $bit_height;
2730 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2733 put_record( $record, $out );
2737 sub script_calc_fixedstep
2739 # Martin A. Hansen, September 2008.
2741 # Calculates fixedstep entries from data in the stream.
2743 my ( $in, # handle to in stream
2744 $out, # handle to out stream
2745 $options, # options hash
2750 my ( $record, %fh_hash, $fh_in, $fh_out, $chr, $chr, $beg, $end, $q_id, $block, $entry, $clones, $beg_block, $max, $i );
2752 while ( $record = get_record( $in ) )
2754 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2755 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2756 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2758 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2760 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2762 $fh_out = $fh_hash{ $record->{ "CHR" } };
2764 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2768 map { close $_ } keys %fh_hash;
2770 foreach $chr ( sort keys %fh_hash )
2772 #Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
2773 Maasha::Common::run( "bed_sort", "--sort 3 --cols 5 $BP_TMP/$chr > $BP_TMP/$chr.sort" );
2775 rename "$BP_TMP/$chr.sort", "$BP_TMP/$chr";
2777 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
2781 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
2783 $chr = $entry->{ 'CHR' };
2784 $beg = $entry->{ 'CHR_BEG' };
2785 $end = $entry->{ 'CHR_END' };
2786 $q_id = $entry->{ 'Q_ID' };
2788 if ( $options->{ "score" } ) {
2789 $clones = $entry->{ 'SCORE' };
2790 } elsif ( $q_id =~ /_(\d+)$/ ) {
2800 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2802 $record->{ "CHR" } = $chr;
2803 $record->{ "CHR_BEG" } = $beg_block;
2804 $record->{ "STEP" } = 1;
2805 $record->{ "VALS" } = join ";", @{ $block };
2806 $record->{ "REC_TYPE" } = "fixed_step";
2808 put_record( $record, $out );
2814 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
2815 $block->[ $i ] += $clones;
2818 $max = Maasha::Calc::max( $max, $end );
2827 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
2828 $block->[ $i ] += $clones;
2835 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2837 $record->{ "CHR" } = $chr;
2838 $record->{ "CHR_BEG" } = $beg_block;
2839 $record->{ "STEP" } = 1;
2840 $record->{ "VALS" } = join ";", @{ $block };
2841 $record->{ "REC_TYPE" } = "fixed_step";
2843 put_record( $record, $out );
2845 unlink "$BP_TMP/$chr";
2850 sub script_reverse_seq
2852 # Martin A. Hansen, August 2007.
2854 # Reverse sequence in record.
2856 my ( $in, # handle to in stream
2857 $out, # handle to out stream
2864 while ( $record = get_record( $in ) )
2866 if ( $record->{ "SEQ" } ) {
2867 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2870 put_record( $record, $out );
2875 sub script_complement_seq
2877 # Martin A. Hansen, August 2007.
2879 # Complement sequence in record.
2881 my ( $in, # handle to in stream
2882 $out, # handle to out stream
2887 my ( $record, $type );
2889 while ( $record = get_record( $in ) )
2891 if ( $record->{ "SEQ" } )
2894 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2897 if ( $type eq "rna" ) {
2898 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2899 } elsif ( $type eq "dna" ) {
2900 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2904 put_record( $record, $out );
2909 sub script_remove_indels
2911 # Martin A. Hansen, August 2007.
2913 # Remove indels from sequences in stream.
2915 my ( $in, # handle to in stream
2916 $out, # handle to out stream
2923 while ( $record = get_record( $in ) )
2925 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2927 put_record( $record, $out );
2932 sub script_transliterate_seq
2934 # Martin A. Hansen, August 2007.
2936 # Transliterate chars from sequence in record.
2938 my ( $in, # handle to in stream
2939 $out, # handle to out stream
2940 $options, # options hash
2945 my ( $record, $search, $replace, $delete );
2947 $search = $options->{ "search" } || "";
2948 $replace = $options->{ "replace" } || "";
2949 $delete = $options->{ "delete" } || "";
2951 while ( $record = get_record( $in ) )
2953 if ( $record->{ "SEQ" } )
2955 if ( $search and $replace ) {
2956 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2957 } elsif ( $delete ) {
2958 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2962 put_record( $record, $out );
2967 sub script_transliterate_vals
2969 # Martin A. Hansen, April 2008.
2971 # Transliterate chars from values in record.
2973 my ( $in, # handle to in stream
2974 $out, # handle to out stream
2975 $options, # options hash
2980 my ( $record, $search, $replace, $delete, $key );
2982 $search = $options->{ "search" } || "";
2983 $replace = $options->{ "replace" } || "";
2984 $delete = $options->{ "delete" } || "";
2986 while ( $record = get_record( $in ) )
2988 foreach $key ( @{ $options->{ "keys" } } )
2990 if ( exists $record->{ $key } )
2992 if ( $search and $replace ) {
2993 eval "\$record->{ $key } =~ tr/$search/$replace/";
2994 } elsif ( $delete ) {
2995 eval "\$record->{ $key } =~ tr/$delete//d";
3000 put_record( $record, $out );
3005 sub script_translate_seq
3007 # Martin A. Hansen, February 2008.
3009 # Translate DNA sequence into protein sequence.
3011 my ( $in, # handle to in stream
3012 $out, # handle to out stream
3013 $options, # options hash
3018 my ( $record, $frame, %new_record );
3020 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
3022 while ( $record = get_record( $in ) )
3024 if ( $record->{ "SEQ" } )
3026 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
3028 foreach $frame ( @{ $options->{ "frames" } } )
3030 %new_record = %{ $record };
3032 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
3033 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
3034 $new_record{ "FRAME" } = $frame;
3036 put_record( \%new_record, $out );
3042 put_record( $record, $out );
3048 sub script_extract_seq
3050 # Martin A. Hansen, August 2007.
3052 # Extract subsequences from sequences in record.
3054 my ( $in, # handle to in stream
3055 $out, # handle to out stream
3056 $options, # options hash
3061 my ( $beg, $end, $len, $record );
3063 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
3066 $beg = $options->{ "beg" } - 1; # correcting for start offset
3069 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
3071 } elsif ( defined $options->{ "end" } ) {
3072 $end = $options->{ "end" } - 1; # correcting for start offset
3075 $len = $options->{ "len" };
3077 # print "beg->$beg, end->$end, len->$len\n";
3079 while ( $record = get_record( $in ) )
3081 if ( $record->{ "SEQ" } )
3083 if ( defined $beg and defined $end )
3085 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
3086 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3088 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
3091 elsif ( defined $beg and defined $len )
3093 if ( $len > length $record->{ "SEQ" } ) {
3094 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3096 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
3099 elsif ( defined $beg )
3101 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
3105 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3107 put_record( $record, $out );
3112 sub script_get_genome_seq
3114 # Martin A. Hansen, December 2007.
3116 # Gets a subsequence from a genome.
3118 my ( $in, # handle to in stream
3119 $out, # handle to out stream
3120 $options, # options hash
3125 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
3127 $options->{ "flank" } ||= 0;
3129 if ( $options->{ "genome" } )
3131 $genome = $options->{ "genome" };
3133 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
3134 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
3136 $fh = Maasha::Common::read_open( $genome_file );
3137 $index = Maasha::Fasta::index_retrieve( $index_file );
3139 shift @{ $index }; # Get rid of the file size info
3141 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
3143 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3145 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
3147 $beg = $index_beg + $options->{ "beg" } - 1;
3149 if ( $options->{ "len" } ) {
3150 $len = $options->{ "len" };
3151 } elsif ( $options->{ "end" } ) {
3152 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3155 $beg -= $options->{ "flank" };
3156 $len += 2 * $options->{ "flank" };
3158 if ( $beg <= $index_beg )
3160 $len -= $index_beg - $beg;
3164 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3166 next if $beg > $index_beg + $index_len;
3168 $record->{ "CHR" } = $options->{ "chr" };
3169 $record->{ "CHR_BEG" } = $beg - $index_beg;
3170 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3172 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3173 $record->{ "SEQ_LEN" } = $len;
3175 put_record( $record, $out );
3179 while ( $record = get_record( $in ) )
3181 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3183 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3185 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3187 $beg = $record->{ "CHR_BEG" } + $index_beg;
3188 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3190 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3192 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3194 $beg = $record->{ "S_BEG" } + $index_beg;
3195 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3197 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3199 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3201 $beg = $record->{ "S_BEG" } + $index_beg;
3202 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3205 $beg -= $options->{ "flank" };
3206 $len += 2 * $options->{ "flank" };
3208 if ( $beg <= $index_beg )
3210 $len -= $index_beg - $beg;
3214 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3216 next if $beg > $index_beg + $index_len;
3218 $record->{ "CHR_BEG" } = $beg - $index_beg;
3219 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3221 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3223 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3225 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3226 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3229 if ( $options->{ "mask" } )
3231 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3233 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3235 @begs = split ",", $record->{ "Q_BEGS" };
3236 @lens = split ",", $record->{ "BLOCKSIZES" };
3238 for ( $i = 0; $i < @begs; $i++ ) {
3239 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3245 put_record( $record, $out );
3252 sub script_get_genome_align
3254 # Martin A. Hansen, April 2008.
3256 # Gets a subalignment from a multiple genome alignment.
3258 my ( $in, # handle to in stream
3259 $out, # handle to out stream
3260 $options, # options hash
3265 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3267 $options->{ "strand" } ||= "+";
3271 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3273 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3275 $beg = $options->{ "beg" } - 1;
3277 if ( $options->{ "end" } ) {
3278 $end = $options->{ "end" };
3279 } elsif ( $options->{ "len" } ) {
3280 $end = $beg + $options->{ "len" };
3283 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3285 foreach $entry ( @{ $align } )
3287 $entry->{ "CHR" } = $record->{ "CHR" };
3288 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3289 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3290 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3291 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3292 $entry->{ "SCORE" } = $record->{ "SCORE" };
3294 put_record( $entry, $out );
3298 while ( $record = get_record( $in ) )
3300 if ( $record->{ "REC_TYPE" } eq "BED" )
3302 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3304 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3306 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3308 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3310 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3313 foreach $entry ( @{ $align } )
3315 $entry->{ "CHR" } = $record->{ "CHR" };
3316 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3317 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3318 $entry->{ "STRAND" } = $record->{ "STRAND" };
3319 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3320 $entry->{ "SCORE" } = $record->{ "SCORE" };
3322 put_record( $entry, $out );
3330 sub script_get_genome_phastcons
3332 # Martin A. Hansen, February 2008.
3334 # Get phastcons scores from genome intervals.
3336 my ( $in, # handle to in stream
3337 $out, # handle to out stream
3338 $options, # options hash
3343 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3345 $options->{ "flank" } ||= 0;
3347 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3348 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3350 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3351 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3353 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3355 $options->{ "beg" } -= 1; # request is 1-based
3356 $options->{ "end" } -= 1; # request is 1-based
3358 if ( $options->{ "len" } ) {
3359 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3362 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3364 $record->{ "CHR" } = $options->{ "chr" };
3365 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3366 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3368 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3369 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3371 put_record( $record, $out );
3374 while ( $record = get_record( $in ) )
3376 if ( $record->{ "REC_TYPE" } eq "BED" )
3378 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3380 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3382 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3384 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3386 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3389 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3390 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3392 put_record( $record, $out );
3395 close $fh_phastcons if $fh_phastcons;
3401 # Martin A. Hansen, December 2007.
3403 # Folds sequences in stream into secondary structures.
3405 my ( $in, # handle to in stream
3406 $out, # handle to out stream
3411 my ( $record, $type, $struct, $index );
3413 while ( $record = get_record( $in ) )
3415 if ( $record->{ "SEQ" } )
3418 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3421 if ( $type ne "protein" )
3423 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3424 $record->{ "SEC_STRUCT" } = $struct;
3425 $record->{ "FREE_ENERGY" } = $index;
3426 $record->{ "SCORE" } = abs int $index;
3427 $record->{ "SIZE" } = length $struct;
3428 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3432 put_record( $record, $out );
3437 sub script_split_seq
3439 # Martin A. Hansen, August 2007.
3441 # Split a sequence in stream into words.
3443 my ( $in, # handle to in stream
3444 $out, # handle to out stream
3445 $options, # options hash
3450 my ( $record, $new_record, $i, $subseq, %lookup );
3452 $options->{ "word_size" } ||= 7;
3454 while ( $record = get_record( $in ) )
3456 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3458 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3460 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3462 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3464 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3465 $new_record->{ "SEQ" } = $subseq;
3467 put_record( $new_record, $out );
3469 $lookup{ $subseq } = 1;
3473 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3474 $new_record->{ "SEQ" } = $subseq;
3476 put_record( $new_record, $out );
3482 put_record( $record, $out );
3488 sub script_split_bed
3490 # Martin A. Hansen, June 2008.
3492 # Split a BED record into overlapping windows.
3494 my ( $in, # handle to in stream
3495 $out, # handle to out stream
3496 $options, # options hash
3501 my ( $record, $new_record, $i );
3503 $options->{ "window_size" } ||= 20;
3504 $options->{ "step_size" } ||= 1;
3506 while ( $record = get_record( $in ) )
3508 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3510 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3512 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3514 $new_record->{ "REC_TYPE" } = "BED";
3515 $new_record->{ "CHR" } = $record->{ "CHR" };
3516 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3517 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3518 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3519 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3520 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3521 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3523 put_record( $new_record, $out );
3528 put_record( $record, $out );
3534 sub script_align_seq
3536 # Martin A. Hansen, August 2007.
3538 # Align sequences in stream.
3540 my ( $in, # handle to in stream
3541 $out, # handle to out stream
3546 my ( $record, @entries, $entry );
3548 while ( $record = get_record( $in ) )
3550 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3551 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3552 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3553 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3555 put_record( $record, $out );
3559 @entries = Maasha::Align::align( \@entries );
3561 foreach $entry ( @entries )
3563 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3566 SEQ_NAME => $entry->[ SEQ_NAME ],
3567 SEQ => $entry->[ SEQ ],
3570 put_record( $record, $out );
3578 # Martin A. Hansen, February 2008.
3580 # Using the first sequence in stream as reference, tile
3581 # all subsequent sequences based on pairwise alignments.
3583 my ( $in, # handle to in stream
3584 $out, # handle to out stream
3585 $options, # options hash
3590 my ( $record, $first, $ref_entry, @entries );
3594 while ( $record = get_record( $in ) )
3596 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3600 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3606 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3611 put_record( $record, $out );
3615 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3617 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3621 sub script_invert_align
3623 # Martin A. Hansen, February 2008.
3625 # Inverts an alignment showing only non-mathing residues
3626 # using the first sequence as reference.
3628 my ( $in, # handle to in stream
3629 $out, # handle to out stream
3630 $options, # options hash
3635 my ( $record, @entries );
3637 while ( $record = get_record( $in ) )
3639 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3641 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3645 put_record( $record, $out );
3649 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3651 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3655 sub script_patscan_seq
3657 # Martin A. Hansen, August 2007.
3659 # Locates patterns in sequences using scan_for_matches.
3661 my ( $in, # handle to in stream
3662 $out, # handle to out stream
3663 $options, # options hash
3668 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3670 if ( $options->{ "patterns" } ) {
3671 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3672 } elsif ( -f $options->{ "patterns_in" } ) {
3673 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3676 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3678 push @args, "-c" if $options->{ "comp" };
3679 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3680 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3682 $seq_file = "$BP_TMP/patscan.seq";
3683 $pat_file = "$BP_TMP/patscan.pat";
3684 $out_file = "$BP_TMP/patscan.out";
3686 $fh_out = Maasha::Common::write_open( $seq_file );
3690 while ( $record = get_record( $in ) )
3692 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3694 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3696 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3698 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3706 $arg = join " ", @args;
3707 $arg .= " -p" if $type eq "protein";
3709 foreach $pattern ( @{ $patterns } )
3711 $fh_out = Maasha::Common::write_open( $pat_file );
3713 print $fh_out "$pattern\n";
3717 if ( $options->{ 'genome' } ) {
3718 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3719 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3721 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3722 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3725 $fh_in = Maasha::Common::read_open( $out_file );
3727 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3729 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3731 if ( $options->{ 'genome' } )
3733 $result->{ "CHR" } = $result->{ "S_ID" };
3734 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3735 $result->{ "CHR_END" } = $result->{ "S_END" };
3737 delete $result->{ "S_ID" };
3738 delete $result->{ "S_BEG" };
3739 delete $result->{ "S_END" };
3743 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3746 put_record( $result, $out );
3758 sub script_create_blast_db
3760 # Martin A. Hansen, September 2007.
3762 # Creates a NCBI BLAST database with formatdb
3764 my ( $in, # handle to in stream
3765 $out, # handle to out stream
3766 $options, # options hash
3771 my ( $fh, $seq_type, $path, $record, $entry );
3773 $path = $options->{ "database" };
3775 $fh = Maasha::Common::write_open( $path );
3777 while ( $record = get_record( $in ) )
3779 put_record( $record, $out ) if not $options->{ "no_stream" };
3781 if ( $entry = record2fasta( $record ) )
3783 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3785 Maasha::Fasta::put_entry( $entry, $fh );
3791 if ( $seq_type eq "protein" ) {
3792 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3794 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3801 sub script_blast_seq
3803 # Martin A. Hansen, September 2007.
3805 # BLASTs sequences in stream against a given database.
3807 my ( $in, # handle to in stream
3808 $out, # handle to out stream
3809 $options, # options hash
3814 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3816 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3817 $options->{ "filter" } = "F";
3818 $options->{ "filter" } = "T" if $options->{ "filter" };
3819 $options->{ "cpus" } ||= 1;
3821 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3823 $tmp_in = "$BP_TMP/blast_query.seq";
3824 $tmp_out = "$BP_TMP/blast.result";
3826 $fh_out = Maasha::Common::write_open( $tmp_in );
3828 while ( $record = get_record( $in ) )
3830 if ( $entry = record2fasta( $record ) )
3832 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3834 Maasha::Fasta::put_entry( $entry, $fh_out );
3837 put_record( $record, $out );
3842 if ( -f $options->{ 'database' } . ".phr" ) {
3843 $s_type = "protein";
3845 $s_type = "nucleotide";
3848 if ( not $options->{ 'program' } )
3850 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3851 $options->{ 'program' } = "blastn";
3852 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3853 $options->{ 'program' } = "blastp";
3854 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3855 $options->{ 'program' } = "blastx";
3856 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3857 $options->{ 'program' } = "tblastn";
3861 if ( $options->{ 'verbose' } )
3863 Maasha::Common::run(
3866 "-p $options->{ 'program' }",
3867 "-e $options->{ 'e_val' }",
3868 "-a $options->{ 'cpus' }",
3871 "-d $options->{ 'database' }",
3872 "-F $options->{ 'filter' }",
3880 Maasha::Common::run(
3883 "-p $options->{ 'program' }",
3884 "-e $options->{ 'e_val' }",
3885 "-a $options->{ 'cpus' }",
3888 "-d $options->{ 'database' }",
3889 "-F $options->{ 'filter' }",
3899 $fh_out = Maasha::Common::read_open( $tmp_out );
3903 while ( $line = <$fh_out> )
3907 next if $line =~ /^#/;
3909 @fields = split /\s+/, $line;
3911 $record->{ "REC_TYPE" } = "BLAST";
3912 $record->{ "Q_ID" } = $fields[ 0 ];
3913 $record->{ "S_ID" } = $fields[ 1 ];
3914 $record->{ "IDENT" } = $fields[ 2 ];
3915 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3916 $record->{ "MISMATCHES" } = $fields[ 4 ];
3917 $record->{ "GAPS" } = $fields[ 5 ];
3918 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3919 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3920 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3921 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3922 $record->{ "E_VAL" } = $fields[ 10 ];
3923 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3925 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3927 $record->{ "STRAND" } = '-';
3929 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3933 $record->{ "STRAND" } = '+';
3936 put_record( $record, $out );
3947 # Martin A. Hansen, August 2007.
3949 # BLATs sequences in stream against a given genome.
3951 my ( $in, # handle to in stream
3952 $out, # handle to out stream
3953 $options, # options hash
3958 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3960 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3962 $options->{ 'tile_size' } ||= 11;
3963 $options->{ 'one_off' } ||= 0;
3964 $options->{ 'min_identity' } ||= 90;
3965 $options->{ 'min_score' } ||= 0;
3966 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3968 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3969 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3970 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3971 $blat_args .= " -minScore=$options->{ 'min_score' }";
3972 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3973 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3975 $query_file = "$BP_TMP/blat.seq";
3977 $fh_out = Maasha::Common::write_open( $query_file );
3979 while ( $record = get_record( $in ) )
3981 if ( $entry = record2fasta( $record ) )
3983 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3984 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3987 put_record( $record, $out );
3992 $blat_args .= " -t=dnax" if $type eq "protein";
3993 $blat_args .= " -q=$type";
3995 $result_file = "$BP_TMP/blat.psl";
3997 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
4001 $entries = Maasha::UCSC::psl_get_entries( $result_file );
4003 map { put_record( $_, $out ) } @{ $entries };
4005 unlink $result_file;
4011 # Martin A. Hansen, July 2008.
4013 # soap sequences in stream against a given file or genome.
4015 my ( $in, # handle to in stream
4016 $out, # handle to out stream
4017 $options, # options hash
4022 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
4024 $options->{ "seed_size" } ||= 10;
4025 $options->{ "mismatches" } ||= 2;
4026 $options->{ "gap_size" } ||= 0;
4027 $options->{ "cpus" } ||= 1;
4029 if ( $options->{ "genome" } ) {
4030 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
4033 $tmp_in = "$BP_TMP/soap_query.seq";
4034 $tmp_out = "$BP_TMP/soap.result";
4036 $fh_out = Maasha::Common::write_open( $tmp_in );
4040 while ( $record = get_record( $in ) )
4042 if ( $entry = record2fasta( $record ) )
4044 Maasha::Fasta::put_entry( $entry, $fh_out );
4049 put_record( $record, $out );
4057 "-s $options->{ 'seed_size' }",
4060 "-v $options->{ 'mismatches' }",
4061 "-g $options->{ 'gap_size' }",
4062 "-p $options->{ 'cpus' }",
4063 "-d $options->{ 'in_file' }",
4067 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
4069 Maasha::Common::run( "soap", $args, 1 );
4073 $fh_out = Maasha::Common::read_open( $tmp_out );
4077 while ( $line = <$fh_out> )
4081 @fields = split /\t/, $line;
4083 $record->{ "REC_TYPE" } = "SOAP";
4084 $record->{ "Q_ID" } = $fields[ 0 ];
4085 $record->{ "SCORE" } = $fields[ 3 ];
4086 $record->{ "STRAND" } = $fields[ 6 ];
4087 $record->{ "S_ID" } = $fields[ 7 ];
4088 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
4089 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
4091 put_record( $record, $out );
4101 sub script_match_seq
4103 # Martin A. Hansen, August 2007.
4105 # BLATs sequences in stream against a given genome.
4107 my ( $in, # handle to in stream
4108 $out, # handle to out stream
4109 $options, # options hash
4114 my ( $record, @entries, $results );
4116 $options->{ "word_size" } ||= 20;
4117 $options->{ "direction" } ||= "both";
4119 while ( $record = get_record( $in ) )
4121 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4122 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4125 put_record( $record, $out );
4128 if ( @entries == 1 )
4130 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
4132 map { put_record( $_, $out ) } @{ $results };
4134 elsif ( @entries == 2 )
4136 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
4138 map { put_record( $_, $out ) } @{ $results };
4143 sub script_create_vmatch_index
4145 # Martin A. Hansen, January 2008.
4147 # Create a vmatch index from sequences in the stream.
4149 my ( $in, # handle to in stream
4150 $out, # handle to out stream
4151 $options, # options hash
4156 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4158 if ( $options->{ "index_name" } )
4160 $file_tmp = $options->{ 'index_name' };
4161 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4164 while ( $record = get_record( $in ) )
4166 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
4168 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4170 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4173 put_record( $record, $out ) if not $options->{ "no_stream" };
4176 if ( $options->{ "index_name" } )
4180 if ( $type eq "protein" ) {
4181 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4183 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4191 sub script_vmatch_seq
4193 # Martin A. Hansen, August 2007.
4195 # Vmatches sequences in stream against a given genome.
4197 my ( $in, # handle to in stream
4198 $out, # handle to out stream
4199 $options, # options hash
4204 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4206 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4208 if ( $options->{ "index_name" } )
4210 @index_files = $options->{ "index_name" };
4214 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4216 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4218 @index_files = sort keys %hash;
4221 while ( $record = get_record( $in ) )
4223 push @records, $record;
4225 put_record( $record, $out );
4228 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4232 $fh_in = Maasha::Common::read_open( $result_file );
4234 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4235 put_record( $record, $out );
4240 unlink $result_file;
4244 sub script_write_fasta
4246 # Martin A. Hansen, August 2007.
4248 # Write FASTA entries from sequences in stream.
4250 my ( $in, # handle to in stream
4251 $out, # handle to out stream
4252 $options, # options hash
4257 my ( $record, $fh, $entry );
4259 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4261 while ( $record = get_record( $in ) )
4263 if ( $entry = record2fasta( $record ) ) {
4264 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4267 put_record( $record, $out ) if not $options->{ "no_stream" };
4274 sub script_write_align
4276 # Martin A. Hansen, August 2007.
4278 # Write pretty alignments aligned sequences in stream.
4280 my ( $in, # handle to in stream
4281 $out, # handle to out stream
4282 $options, # options hash
4287 my ( $fh, $record, @entries );
4289 $fh = write_stream( $options->{ "data_out" } ) ;
4291 while ( $record = get_record( $in ) )
4293 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4294 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4297 put_record( $record, $out ) if not $options->{ "no_stream" };
4300 if ( scalar( @entries ) == 2 ) {
4301 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4302 } elsif ( scalar ( @entries ) > 2 ) {
4303 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4310 sub script_write_blast
4312 # Martin A. Hansen, November 2007.
4314 # Write data in blast table format (-m8 and 9).
4316 my ( $in, # handle to in stream
4317 $out, # handle to out stream
4318 $options, # options hash
4323 my ( $fh, $record, $first );
4325 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4329 while ( $record = get_record( $in ) )
4331 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4333 if ( $options->{ "comment" } and $first )
4335 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";
4340 if ( $record->{ "STRAND" } eq "-" ) {
4341 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4344 print $fh join( "\t",
4345 $record->{ "Q_ID" },
4346 $record->{ "S_ID" },
4347 $record->{ "IDENT" },
4348 $record->{ "ALIGN_LEN" },
4349 $record->{ "MISMATCHES" },
4350 $record->{ "GAPS" },
4351 $record->{ "Q_BEG" } + 1,
4352 $record->{ "Q_END" } + 1,
4353 $record->{ "S_BEG" } + 1,
4354 $record->{ "S_END" } + 1,
4355 $record->{ "E_VAL" },
4356 $record->{ "BIT_SCORE" }
4360 put_record( $record, $out ) if not $options->{ "no_stream" };
4367 sub script_write_tab
4369 # Martin A. Hansen, August 2007.
4371 # Write data as table.
4373 my ( $in, # handle to in stream
4374 $out, # handle to out stream
4375 $options, # options hash
4380 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4382 $options->{ "delimit" } ||= "\t";
4384 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4386 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4388 while ( $record = get_record( $in ) )
4393 if ( $options->{ "keys" } )
4395 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4399 foreach $key ( @{ $options->{ "keys" } } )
4401 if ( exists $record->{ $key } )
4403 push @keys, $key if $options->{ "comment" };
4404 push @vals, $record->{ $key };
4411 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4413 next if exists $no_keys{ $key };
4415 push @keys, $key if $options->{ "comment" };
4416 push @vals, $record->{ $key };
4420 if ( @keys and $options->{ "comment" } )
4422 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4424 delete $options->{ "comment" };
4427 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4429 put_record( $record, $out ) if not $options->{ "no_stream" };
4436 sub script_write_bed
4438 # Martin A. Hansen, August 2007.
4440 # Write BED format for the UCSC genome browser using records in stream.
4442 my ( $in, # handle to in stream
4443 $out, # handle to out stream
4444 $options, # options hash
4449 my ( $cols, $fh, $record, $new_record );
4451 $cols = $options->{ 'cols' }->[ 0 ];
4453 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4455 while ( $record = get_record( $in ) )
4457 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4459 $cols ||= $record->{ "BED_COLS" };
4461 Maasha::UCSC::bed_put_entry( $record, $fh, $cols );
4463 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4465 $new_record->{ "CHR" } = $record->{ "S_ID" };
4466 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4467 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4468 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4469 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4470 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4474 Maasha::UCSC::bed_put_entry( $new_record, $fh, $cols );
4476 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4480 Maasha::UCSC::bed_put_entry( $record, $fh, $cols );
4482 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4484 $new_record->{ "CHR" } = $record->{ "S_ID" };
4485 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4486 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4487 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4488 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4489 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4493 Maasha::UCSC::bed_put_entry( $new_record, $fh, $cols );
4495 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" 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; # or use E_VAL somehow
4502 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4506 Maasha::UCSC::bed_put_entry( $new_record, $fh, $cols );
4508 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Soap ----
4510 $new_record->{ "CHR" } = $record->{ "S_ID" };
4511 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4512 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4513 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4514 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4515 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4519 Maasha::UCSC::bed_put_entry( $new_record, $fh, $cols );
4521 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4523 Maasha::UCSC::bed_put_entry( $record, $fh, $cols );
4526 put_record( $record, $out ) if not $options->{ "no_stream" };
4533 sub script_write_psl
4535 # Martin A. Hansen, August 2007.
4537 # Write PSL output from stream.
4539 my ( $in, # handle to in stream
4540 $out, # handle to out stream
4541 $options, # options hash
4546 my ( $fh, $record, @output, $first );
4550 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4552 while ( $record = get_record( $in ) )
4554 put_record( $record, $out ) if not $options->{ "no_stream" };
4556 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4558 Maasha::UCSC::psl_put_header( $fh ) if $first;
4559 Maasha::UCSC::psl_put_entry( $record, $fh );
4568 sub script_write_fixedstep
4570 # Martin A. Hansen, Juli 2008.
4572 # Write fixedStep entries from recrods in the stream.
4574 my ( $in, # handle to in stream
4575 $out, # handle to out stream
4576 $options, # options hash
4581 my ( $fh, $record, $vals );
4583 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4585 while ( $record = get_record( $in ) )
4587 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4589 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4591 $vals = $record->{ 'VALS' };
4595 print $fh "$vals\n";
4598 put_record( $record, $out ) if not $options->{ "no_stream" };
4605 sub script_write_2bit
4607 # Martin A. Hansen, March 2008.
4609 # Write sequence entries from stream in 2bit format.
4611 my ( $in, # handle to in stream
4612 $out, # handle to out stream
4613 $options, # options hash
4618 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4620 $mask = 1 if not $options->{ "no_mask" };
4622 $tmp_file = "$BP_TMP/write_2bit.fna";
4623 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4625 $fh_out = write_stream( $options->{ "data_out" } );
4627 while ( $record = get_record( $in ) )
4629 if ( $entry = record2fasta( $record ) ) {
4630 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4633 put_record( $record, $out ) if not $options->{ "no_stream" };
4638 $fh_in = Maasha::Common::read_open( $tmp_file );
4640 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4649 sub script_write_solid
4651 # Martin A. Hansen, April 2008.
4653 # Write di-base encoded Solid sequence from entries in stream.
4655 my ( $in, # handle to in stream
4656 $out, # handle to out stream
4657 $options, # options hash
4662 my ( $record, $fh, $entry );
4664 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4666 while ( $record = get_record( $in ) )
4668 if ( $entry = record2fasta( $record ) )
4670 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4672 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4675 put_record( $record, $out ) if not $options->{ "no_stream" };
4682 sub script_write_ucsc_config
4684 # Martin A. Hansen, November 2008.
4686 # Write UCSC Genome Broser configuration (.ra file type) from
4687 # records in the stream.
4689 my ( $in, # handle to in stream
4690 $out, # handle to out stream
4691 $options, # options hash
4696 my ( $record, $fh );
4698 $fh = write_stream( $options->{ "data_out" } );
4700 while ( $record = get_record( $in ) )
4702 Maasha::UCSC::ucsc_config_put_entry( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4704 put_record( $record, $out ) if not $options->{ "no_stream" };
4711 sub script_plot_seqlogo
4713 # Martin A. Hansen, August 2007.
4715 # Calculates and writes a sequence logo for alignments.
4717 my ( $in, # handle to in stream
4718 $out, # handle to out stream
4719 $options, # options hash
4724 my ( $record, @entries, $logo, $fh );
4726 while ( $record = get_record( $in ) )
4728 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4729 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4732 put_record( $record, $out ) if not $options->{ "no_stream" };
4735 $logo = Maasha::Plot::seq_logo( \@entries );
4737 $fh = write_stream( $options->{ "data_out" } );
4745 sub script_plot_phastcons_profiles
4747 # Martin A. Hansen, January 2008.
4749 # Plots PhastCons profiles.
4751 my ( $in, # handle to in stream
4752 $out, # handle to out stream
4753 $options, # options hash
4758 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4760 $options->{ "title" } ||= "PhastCons Profiles";
4762 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4763 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4765 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4766 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4768 while ( $record = get_record( $in ) )
4770 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4772 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4773 $record->{ "CHR_BEG" },
4774 $record->{ "CHR_END" },
4775 $options->{ "flank" } );
4777 push @{ $AoA }, [ @{ $scores } ];
4780 put_record( $record, $out ) if not $options->{ "no_stream" };
4783 Maasha::UCSC::phastcons_normalize( $AoA );
4785 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4786 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4788 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4790 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4792 $fh = write_stream( $options->{ "data_out" } );
4794 print $fh "$_\n" foreach @{ $plot };
4800 sub script_analyze_bed
4802 # Martin A. Hansen, March 2008.
4804 # Analyze BED entries in stream.
4806 my ( $in, # handle to in stream
4807 $out, # handle to out stream
4808 $options, # options hash
4815 while ( $record = get_record( $in ) )
4817 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4819 put_record( $record, $out );
4824 sub script_analyze_vals
4826 # Martin A. Hansen, August 2007.
4828 # Analyze values for given keys in stream.
4830 my ( $in, # handle to in stream
4831 $out, # handle to out stream
4832 $options, # options hash
4837 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4839 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4841 while ( $record = get_record( $in ) )
4843 foreach $key ( keys %{ $record } )
4845 next if $options->{ "keys" } and not exists $key_hash{ $key };
4847 $analysis->{ $key }->{ "COUNT" }++;
4849 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4851 $analysis->{ $key }->{ "TYPE" } = "num";
4852 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4853 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4854 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4858 $len = length $record->{ $key };
4860 $analysis->{ $key }->{ "TYPE" } = "alph";
4861 $analysis->{ $key }->{ "SUM" } += $len;
4862 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4863 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4867 put_record( $record, $out ) if not $options->{ "no_stream" };
4870 foreach $key ( keys %{ $analysis } )
4872 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4873 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4876 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4886 if ( $options->{ "keys" } ) {
4887 @keys = @{ $options->{ "keys" } };
4889 @keys = keys %{ $analysis };
4892 foreach $key ( @keys )
4894 $keys .= sprintf "% 15s", $key;
4895 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4896 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4897 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4898 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4899 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4900 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4903 print $out "$keys\n";
4904 print $out "$types\n";
4905 print $out "$counts\n";
4906 print $out "$mins\n";
4907 print $out "$maxs\n";
4908 print $out "$sums\n";
4909 print $out "$means\n";
4913 sub script_head_records
4915 # Martin A. Hansen, August 2007.
4917 # Display the first sequences in stream.
4919 my ( $in, # handle to in stream
4920 $out, # handle to out stream
4921 $options, # options hash
4926 my ( $record, $count );
4928 $options->{ "num" } ||= 10;
4932 while ( $record = get_record( $in ) )
4936 put_record( $record, $out );
4938 last if $count == $options->{ "num" };
4943 sub script_remove_keys
4945 # Martin A. Hansen, August 2007.
4947 # Remove keys from stream.
4949 my ( $in, # handle to in stream
4950 $out, # handle to out stream
4951 $options, # options hash
4956 my ( $record, $new_record );
4958 while ( $record = get_record( $in ) )
4960 if ( $options->{ "keys" } )
4962 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4964 elsif ( $options->{ "save_keys" } )
4966 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4968 $record = $new_record;
4971 put_record( $record, $out ) if keys %{ $record };
4976 sub script_remove_adaptor
4978 # Martin A. Hansen, August 2008.
4980 # Find and remove adaptor from sequences in the stream.
4982 my ( $in, # handle to in stream
4983 $out, # handle to out stream
4984 $options, # options hash
4989 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4991 $options->{ "remove" } ||= "after";
4993 $max_mismatch = $options->{ "mismatches" } || 0;
4994 $offset = $options->{ "offset" };
4996 if ( not defined $offset ) {
5002 $adaptor = uc $options->{ "adaptor" };
5003 $adaptor_len = length $adaptor;
5005 while ( $record = get_record( $in ) )
5007 if ( $record->{ "SEQ" } )
5009 $seq = uc $record->{ "SEQ" };
5010 $seq_len = length $seq;
5012 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
5014 $record->{ "ADAPTOR_POS" } = $pos;
5016 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
5018 if ( $options->{ "remove" } eq "after" )
5020 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
5021 $record->{ "SEQ_LEN" } = $pos;
5025 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
5026 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
5030 put_record( $record, $out );
5034 put_record( $record, $out );
5040 sub script_remove_mysql_tables
5042 # Martin A. Hansen, November 2008.
5044 # Remove MySQL tables from values in stream.
5046 my ( $in, # handle to in stream
5047 $out, # handle to out stream
5048 $options, # options hash
5053 my ( $record, %table_hash, $dbh, $table );
5055 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
5056 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
5058 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
5060 while ( $record = get_record( $in ) )
5062 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5064 put_record( $record, $out ) if not $options->{ 'no_stream' };
5067 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5069 foreach $table ( sort keys %table_hash )
5071 if ( Maasha::SQL::table_exists( $dbh, $table ) )
5073 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5074 Maasha::SQL::delete_table( $dbh, $table );
5075 print STDERR "done.\n" if $options->{ 'verbose' };
5079 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
5083 Maasha::SQL::disconnect( $dbh );
5087 sub script_remove_ucsc_tracks
5089 # Martin A. Hansen, November 2008.
5091 # Remove track from MySQL tables and config file.
5093 my ( $in, # handle to in stream
5094 $out, # handle to out stream
5095 $options, # options hash
5100 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
5102 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
5103 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
5104 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
5106 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
5108 while ( $record = get_record( $in ) )
5110 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5112 put_record( $record, $out ) if not $options->{ 'no_stream' };
5115 # ---- locate track in config file ----
5117 $fh_in = Maasha::Common::read_open( $options->{ 'config_file' } );
5119 while ( $track = Maasha::UCSC::ucsc_config_get_entry( $fh_in ) ) {
5120 push @tracks, $track;
5125 map { push @new_tracks, $_ if not exists $track_hash{ $_->{ 'track' } } } @tracks;
5127 print STDERR qq(WARNING: track not found in config file: "$options->{ 'config_file' }"\n) if scalar @tracks == scalar @new_tracks;
5129 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
5131 $fh_out = Maasha::Common::write_open( $options->{ 'config_file' } );
5133 map { Maasha::UCSC::ucsc_config_put_entry( $_, $fh_out ) } @new_tracks;
5137 # ---- locate track in database ----
5139 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5141 foreach $track ( sort keys %track_hash )
5143 if ( Maasha::SQL::table_exists( $dbh, $track ) )
5145 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5146 Maasha::SQL::delete_table( $dbh, $track );
5147 print STDERR "done.\n" if $options->{ 'verbose' };
5151 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
5155 Maasha::SQL::disconnect( $dbh );
5157 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
5161 sub script_rename_keys
5163 # Martin A. Hansen, August 2007.
5165 # Rename keys in stream.
5167 my ( $in, # handle to in stream
5168 $out, # handle to out stream
5169 $options, # options hash
5176 while ( $record = get_record( $in ) )
5178 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5180 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
5182 delete $record->{ $options->{ "keys" }->[ 0 ] };
5185 put_record( $record, $out );
5190 sub script_uniq_vals
5192 # Martin A. Hansen, August 2007.
5194 # Find unique values in stream.
5196 my ( $in, # handle to in stream
5197 $out, # handle to out stream
5198 $options, # options hash
5203 my ( %hash, $record );
5205 while ( $record = get_record( $in ) )
5207 if ( $record->{ $options->{ "key" } } )
5209 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5211 put_record( $record, $out );
5213 $hash{ $record->{ $options->{ "key" } } } = 1;
5215 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5217 put_record( $record, $out );
5221 $hash{ $record->{ $options->{ "key" } } } = 1;
5226 put_record( $record, $out );
5232 sub script_merge_vals
5234 # Martin A. Hansen, August 2007.
5236 # Rename keys in stream.
5238 my ( $in, # handle to in stream
5239 $out, # handle to out stream
5240 $options, # options hash
5245 my ( $record, @join, $i );
5247 $options->{ "delimit" } ||= '_';
5249 while ( $record = get_record( $in ) )
5251 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5253 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5255 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5256 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5259 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5262 put_record( $record, $out );
5267 sub script_merge_records
5269 # Martin A. Hansen, July 2008.
5271 # Merges records in the stream based on identical values of two given keys.
5273 my ( $in, # handle to in stream
5274 $out, # handle to out stream
5275 $options, # options hash
5280 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5281 $num1, $num2, $num, $cmp, $i );
5283 $merge = $options->{ "merge" } || "AandB";
5285 $file1 = "$BP_TMP/merge_records1.tmp";
5286 $file2 = "$BP_TMP/merge_records2.tmp";
5288 $fh1 = Maasha::Common::write_open( $file1 );
5289 $fh2 = Maasha::Common::write_open( $file2 );
5291 $key1 = $options->{ "keys" }->[ 0 ];
5292 $key2 = $options->{ "keys" }->[ 1 ];
5294 $num = $key2 =~ s/n$//;
5298 while ( $record = get_record( $in ) )
5300 if ( exists $record->{ $key1 } )
5303 @vals1 = $record->{ $key1 };
5305 delete $record->{ $key1 };
5307 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5309 print $fh1 join( "\t", @vals1 ), "\n";
5313 elsif ( exists $record->{ $key2 } )
5316 @vals2 = $record->{ $key2 };
5318 delete $record->{ $key2 };
5320 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5322 print $fh2 join( "\t", @vals2 ), "\n";
5333 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5334 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5338 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5339 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5342 $fh1 = Maasha::Common::read_open( $file1 );
5343 $fh2 = Maasha::Common::read_open( $file2 );
5345 @vals1 = Maasha::Common::get_fields( $fh1 );
5346 @vals2 = Maasha::Common::get_fields( $fh2 );
5348 while ( $num1 > 0 and $num2 > 0 )
5353 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5355 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5360 if ( $merge =~ /^(AorB|AnotB)$/ )
5362 for ( $i = 0; $i < @keys1; $i++ ) {
5363 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5366 put_record( $record, $out );
5369 @vals1 = Maasha::Common::get_fields( $fh1 );
5374 if ( $merge =~ /^(BorA|BnotA)$/ )
5376 for ( $i = 0; $i < @keys2; $i++ ) {
5377 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5380 put_record( $record, $out );
5383 @vals2 = Maasha::Common::get_fields( $fh2 );
5388 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5390 for ( $i = 0; $i < @keys1; $i++ ) {
5391 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5394 for ( $i = 1; $i < @keys2; $i++ ) {
5395 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5398 put_record( $record, $out );
5401 @vals1 = Maasha::Common::get_fields( $fh1 );
5402 @vals2 = Maasha::Common::get_fields( $fh2 );
5414 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5418 for ( $i = 0; $i < @keys1; $i++ ) {
5419 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5422 put_record( $record, $out );
5425 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5429 for ( $i = 0; $i < @keys2; $i++ ) {
5430 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5433 put_record( $record, $out );
5440 # Martin A. Hansen, August 2007.
5442 # Grab for records in stream.
5444 my ( $in, # handle to in stream
5445 $out, # handle to out stream
5446 $options, # options hash
5451 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5453 $keys = $options->{ 'keys' };
5454 $vals_only = $options->{ 'vals_only' };
5455 $keys_only = $options->{ 'keys_only' };
5456 $invert = $options->{ 'invert' };
5458 if ( $options->{ 'patterns' } )
5460 $patterns = [ split ",", $options->{ 'patterns' } ];
5462 elsif ( -f $options->{ 'patterns_in' } )
5464 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5466 elsif ( $options->{ 'regex' } )
5468 if ( $options->{ 'case_insensitive' } ) {
5469 $regex = qr/$options->{ 'regex' }/i;
5471 $regex = qr/$options->{ 'regex' }/;
5474 elsif ( -f $options->{ 'exact_in' } )
5476 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5478 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5482 elsif ( $options->{ 'eval' } )
5484 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5492 while ( $record = get_record( $in ) )
5496 if ( %lookup_hash ) {
5497 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5498 } elsif ( $patterns ) {
5499 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5500 } elsif ( $regex ) {
5501 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5503 $found = grab_eval( $key, $op, $val, $record );
5506 if ( $found and not $invert ) {
5507 put_record( $record, $out );
5508 } elsif ( not $found and $invert ) {
5509 put_record( $record, $out );
5517 # Martin A. Hansen, August 2007.
5519 # Evaluate extression for records in stream.
5521 my ( $in, # handle to in stream
5522 $out, # handle to out stream
5523 $options, # options hash
5528 my ( $record, $eval_key, @keys, $eval_val );
5530 while ( $record = get_record( $in ) )
5532 if ( $options->{ "eval" } )
5534 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5541 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5543 @keys = grep { exists $record->{ $_ } } @keys;
5546 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5548 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5552 warn qq(WARNING: Bad compute expression: "$options->{ 'eval' }"\n);
5556 put_record( $record, $out );
5563 # Martin A. Hansen, June 2008.
5567 my ( $in, # handle to in stream
5568 $out, # handle to out stream
5569 $options, # options hash
5574 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5576 while ( $record = get_record( $in ) )
5580 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5582 push @rows, $record->{ $key };
5586 push @matrix, [ @rows ];
5591 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5593 foreach $row ( @matrix )
5595 for ( $i = 0; $i < @{ $row }; $i++ ) {
5596 $record->{ "V$i" } = $row->[ $i ];
5599 put_record( $record, $out );
5604 sub script_add_ident
5606 # Martin A. Hansen, May 2008.
5608 # Add a unique identifier to each record in stream.
5610 my ( $in, # handle to in stream
5611 $out, # handle to out stream
5612 $options, # options hash
5617 my ( $record, $key, $prefix, $i );
5619 $key = $options->{ "key" } || "ID";
5620 $prefix = $options->{ "prefix" } || "ID";
5624 while ( $record = get_record( $in ) )
5626 $record->{ $key } = sprintf( "$prefix%08d", $i );
5628 put_record( $record, $out );
5635 sub script_count_records
5637 # Martin A. Hansen, August 2007.
5639 # Count records in stream.
5641 my ( $in, # handle to in stream
5642 $out, # handle to out stream
5643 $options, # options hash
5648 my ( $record, $count, $result, $fh, $line );
5652 if ( $options->{ "no_stream" } )
5654 while ( $line = <$in> )
5658 $count++ if $line eq "---";
5663 while ( $record = get_record( $in ) )
5665 put_record( $record, $out );
5671 $result = { "RECORDS_COUNT" => $count };
5673 $fh = write_stream( $options->{ "data_out" } );
5675 put_record( $result, $fh );
5681 sub script_random_records
5683 # Martin A. Hansen, August 2007.
5685 # Pick a number or random records from stream.
5687 my ( $in, # handle to in stream
5688 $out, # handle to out stream
5689 $options, # options hash
5694 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5696 $options->{ "num" } ||= 10;
5698 $tmp_file = "$BP_TMP/random_records.tmp";
5700 $fh_out = Maasha::Common::write_open( $tmp_file );
5704 while ( $record = get_record( $in ) )
5706 put_record( $record, $fh_out );
5716 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5718 while ( $i < $options->{ "num" } )
5720 $rand = int( rand( $count ) );
5722 if ( not exists $rand_hash{ $rand } )
5724 $rand_hash{ $rand } = 1;
5726 $max = $rand if $rand > $max;
5732 $fh_in = Maasha::Common::read_open( $tmp_file );
5736 while ( $record = get_record( $fh_in ) )
5738 put_record( $record, $out ) if exists $rand_hash{ $count };
5740 last if $count == $max;
5751 sub script_sort_records
5753 # Martin A. Hansen, August 2007.
5755 # Sort to sort records according to keys.
5757 my ( $in, # handle to in stream
5758 $out, # handle to out stream
5759 $options, # options hash
5764 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5766 foreach $key ( @{ $options->{ "keys" } } )
5768 if ( $key =~ s/n$// ) {
5769 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5771 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5775 $sort_str = join " or ", @sort_cmd;
5776 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5778 while ( $record = get_record( $in ) ) {
5779 push @records, $record;
5782 @records = sort $sort_sub @records;
5784 if ( $options->{ "reverse" } )
5786 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5787 put_record( $records[ $i ], $out );
5792 for ( $i = 0; $i < scalar @records; $i++ ) {
5793 put_record( $records[ $i ], $out );
5799 sub script_count_vals
5801 # Martin A. Hansen, August 2007.
5803 # Count records in stream.
5805 my ( $in, # handle to in stream
5806 $out, # handle to out stream
5807 $options, # options hash
5812 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5814 $tmp_file = "$BP_TMP/count_cache.tmp";
5816 $fh_out = Maasha::Common::write_open( $tmp_file );
5821 while ( $record = get_record( $in ) )
5823 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5825 push @records, $record;
5827 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5829 map { put_record( $_, $fh_out ) } @records;
5836 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5847 $fh_in = Maasha::Common::read_open( $tmp_file );
5849 while ( $record = get_record( $fh_in ) )
5851 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5853 put_record( $record, $out );
5855 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5863 foreach $record ( @records )
5865 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5867 put_record( $record, $out );
5874 sub script_plot_histogram
5876 # Martin A. Hansen, September 2007.
5878 # Plot a simple histogram for a given key using GNU plot.
5880 my ( $in, # handle to in stream
5881 $out, # handle to out stream
5882 $options, # options hash
5887 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5889 $options->{ "title" } ||= "Histogram";
5890 $options->{ "sort" } ||= "num";
5892 while ( $record = get_record( $in ) )
5894 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5896 put_record( $record, $out ) if not $options->{ "no_stream" };
5899 if ( $options->{ "sort" } eq "num" ) {
5900 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5902 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5905 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5907 $fh = write_stream( $options->{ "data_out" } );
5909 print $fh "$_\n" foreach @{ $result };
5915 sub script_plot_lendist
5917 # Martin A. Hansen, August 2007.
5919 # Plot length distribution using GNU plot.
5921 my ( $in, # handle to in stream
5922 $out, # handle to out stream
5923 $options, # options hash
5928 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5930 $options->{ "title" } ||= "Length Distribution";
5932 while ( $record = get_record( $in ) )
5934 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5936 put_record( $record, $out ) if not $options->{ "no_stream" };
5939 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5941 for ( $i = 0; $i < $max; $i++ ) {
5942 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5945 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5947 $fh = write_stream( $options->{ "data_out" } );
5949 print $fh "$_\n" foreach @{ $result };
5955 sub script_plot_chrdist
5957 # Martin A. Hansen, August 2007.
5959 # Plot chromosome distribution using GNU plot.
5961 my ( $in, # handle to in stream
5962 $out, # handle to out stream
5963 $options, # options hash
5968 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5970 $options->{ "title" } ||= "Chromosome Distribution";
5972 while ( $record = get_record( $in ) )
5974 if ( $record->{ "CHR" } ) { # generic
5975 $data_hash{ $record->{ "CHR" } }++;
5976 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5977 $data_hash{ $record->{ "S_ID" } }++;
5978 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5979 $data_hash{ $record->{ "S_ID" } }++;
5980 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5981 $data_hash{ $record->{ "S_ID" } }++;
5984 put_record( $record, $out ) if not $options->{ "no_stream" };
5987 foreach $elem ( keys %data_hash )
5991 $sort_key =~ s/chr//i;
5993 $sort_key =~ s/^X(.*)/99$1/;
5994 $sort_key =~ s/^Y(.*)/99$1/;
5995 $sort_key =~ s/^Z(.*)/999$1/;
5996 $sort_key =~ s/^M(.*)/9999$1/;
5997 $sort_key =~ s/^U(.*)/99999$1/;
5999 $count = $sort_key =~ tr/_//;
6001 $sort_key =~ s/_.*/"999999" x $count/ex;
6003 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
6006 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
6008 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
6010 $fh = write_stream( $options->{ "data_out" } );
6012 print $fh "$_\n" foreach @{ $result };
6018 sub script_plot_karyogram
6020 # Martin A. Hansen, August 2007.
6022 # Plot hits on karyogram.
6024 my ( $in, # handle to in stream
6025 $out, # handle to out stream
6026 $options, # options hash
6031 my ( %options, $record, @data, $fh, $result, %data_hash );
6033 $options->{ "genome" } ||= "human";
6034 $options->{ "feat_color" } ||= "black";
6036 while ( $record = get_record( $in ) )
6038 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
6040 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
6043 put_record( $record, $out ) if not $options->{ "no_stream" };
6046 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
6048 $fh = write_stream( $options->{ "data_out" } );
6056 sub script_plot_matches
6058 # Martin A. Hansen, August 2007.
6060 # Plot matches in 2D generating a dotplot.
6062 my ( $in, # handle to in stream
6063 $out, # handle to out stream
6064 $options, # options hash
6069 my ( $record, @data, $fh, $result, %data_hash );
6071 $options->{ "direction" } ||= "both";
6073 while ( $record = get_record( $in ) )
6075 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
6076 push @data, $record;
6079 put_record( $record, $out ) if not $options->{ "no_stream" };
6082 $options->{ "title" } ||= "plot_matches";
6083 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
6084 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
6086 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
6088 $fh = write_stream( $options->{ "data_out" } );
6090 print $fh "$_\n" foreach @{ $result };
6096 sub script_length_vals
6098 # Martin A. Hansen, August 2007.
6100 # Determine the length of the value for given keys.
6102 my ( $in, # handle to in stream
6103 $out, # handle to out stream
6104 $options, # options hash
6109 my ( $record, $key );
6111 while ( $record = get_record( $in ) )
6113 foreach $key ( @{ $options->{ "keys" } } )
6115 if ( $record->{ $key } ) {
6116 $record->{ $key . "_LEN" } = length $record->{ $key };
6120 put_record( $record, $out );
6127 # Martin A. Hansen, August 2007.
6129 # Calculates the sums for values of given keys.
6131 my ( $in, # handle to in stream
6132 $out, # handle to out stream
6133 $options, # options hash
6138 my ( $record, $key, %sum_hash, $fh );
6140 while ( $record = get_record( $in ) )
6142 foreach $key ( @{ $options->{ "keys" } } )
6144 if ( $record->{ $key } ) {
6145 $sum_hash{ $key } += $record->{ $key };
6149 put_record( $record, $out ) if not $options->{ "no_stream" };
6152 $fh = write_stream( $options->{ "data_out" } );
6154 foreach $key ( @{ $options->{ "keys" } } ) {
6155 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
6162 sub script_mean_vals
6164 # Martin A. Hansen, August 2007.
6166 # Calculate the mean of values of given keys.
6168 my ( $in, # handle to in stream
6169 $out, # handle to out stream
6170 $options, # options hash
6175 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
6177 while ( $record = get_record( $in ) )
6179 foreach $key ( @{ $options->{ "keys" } } )
6181 if ( $record->{ $key } )
6183 $sum_hash{ $key } += $record->{ $key };
6184 $count_hash{ $key }++;
6188 put_record( $record, $out ) if not $options->{ "no_stream" };
6191 $fh = write_stream( $options->{ "data_out" } );
6193 foreach $key ( @{ $options->{ "keys" } } )
6195 if ( $count_hash{ $key } ) {
6196 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6201 put_record( { $key . "_MEAN" => $mean } , $fh );
6208 sub script_median_vals
6210 # Martin A. Hansen, March 2008.
6212 # Calculate the median values of given keys.
6214 my ( $in, # handle to in stream
6215 $out, # handle to out stream
6216 $options, # options hash
6221 my ( $record, $key, %median_hash, $median, $fh );
6223 while ( $record = get_record( $in ) )
6225 foreach $key ( @{ $options->{ "keys" } } ) {
6226 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6229 put_record( $record, $out ) if not $options->{ "no_stream" };
6232 $fh = write_stream( $options->{ "data_out" } );
6234 foreach $key ( @{ $options->{ "keys" } } )
6236 if ( $median_hash{ $key } ) {
6237 $median = Maasha::Calc::median( $median_hash{ $key } );
6242 put_record( { $key . "_MEDIAN" => $median } , $fh );
6251 # Martin A. Hansen, February 2008.
6253 # Determine the maximum values of given keys.
6255 my ( $in, # handle to in stream
6256 $out, # handle to out stream
6257 $options, # options hash
6262 my ( $record, $key, $fh, %max_hash, $max_record );
6264 while ( $record = get_record( $in ) )
6266 foreach $key ( @{ $options->{ "keys" } } )
6268 if ( $record->{ $key } )
6270 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6274 put_record( $record, $out ) if not $options->{ "no_stream" };
6277 $fh = write_stream( $options->{ "data_out" } );
6279 foreach $key ( @{ $options->{ "keys" } } )
6281 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6284 put_record( $max_record, $fh );
6292 # Martin A. Hansen, February 2008.
6294 # Determine the minimum values of given keys.
6296 my ( $in, # handle to in stream
6297 $out, # handle to out stream
6298 $options, # options hash
6303 my ( $record, $key, $fh, %min_hash, $min_record );
6305 while ( $record = get_record( $in ) )
6307 foreach $key ( @{ $options->{ "keys" } } )
6309 if ( defined $record->{ $key } )
6311 if ( exists $min_hash{ $key } ) {
6312 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6314 $min_hash{ $key } = $record->{ $key };
6319 put_record( $record, $out ) if not $options->{ "no_stream" };
6322 $fh = write_stream( $options->{ "data_out" } );
6324 foreach $key ( @{ $options->{ "keys" } } )
6326 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6329 put_record( $min_record, $fh );
6335 sub script_upload_to_ucsc
6337 # Martin A. Hansen, August 2007.
6339 # Calculate the mean of values of given keys.
6341 # This routine has developed into the most ugly hack. Do something!
6343 my ( $in, # handle to in stream
6344 $out, # handle to out stream
6345 $options, # options hash
6350 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
6352 $options->{ "short_label" } ||= $options->{ 'table' };
6353 $options->{ "long_label" } ||= $options->{ 'table' };
6354 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6355 $options->{ "priority" } ||= 1;
6356 $options->{ "visibility" } ||= "pack";
6357 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6358 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6360 $file = "$BP_TMP/ucsc_upload.tmp";
6368 $fh_out = Maasha::Common::write_open( $file );
6370 while ( $record = get_record( $in ) )
6372 put_record( $record, $out ) if not $options->{ "no_stream" };
6374 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6376 $vals = $record->{ "VALS" };
6379 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6380 print $fh_out "$vals\n";
6382 $format = "WIGGLE" if not $format;
6384 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6386 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6387 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6391 $format = "PSL" if not $format;
6393 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6395 # chrom chromStart chromEnd name score strand size secStr conf
6397 print $fh_out join ( "\t",
6399 $record->{ "CHR_BEG" },
6400 $record->{ "CHR_END" } + 1,
6401 $record->{ "Q_ID" },
6402 $record->{ "SCORE" },
6403 $record->{ "STRAND" },
6404 $record->{ "SIZE" },
6405 $record->{ "SEC_STRUCT" },
6406 $record->{ "CONF" },
6409 $format = "BED_SS" if not $format;
6411 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6413 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6415 $format = "BED" if not $format;
6416 $columns = $record->{ "BED_COLS" } if not $columns;
6418 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6420 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6422 $format = "BED" if not $format;
6423 $columns = 6 if not $columns;
6425 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6427 $record->{ "CHR" } = $record->{ "S_ID" };
6428 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6429 $record->{ "CHR_END" } = $record->{ "S_END" };
6430 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6432 $format = "BED" if not $format;
6433 $columns = 6 if not $columns;
6435 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6437 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6439 $record->{ "CHR" } = $record->{ "S_ID" };
6440 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6441 $record->{ "CHR_END" } = $record->{ "S_END" };
6442 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6443 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6445 $format = "BED" if not $format;
6446 $columns = 6 if not $columns;
6448 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6451 if ( $i == $options->{ "chunk_size" } )
6455 if ( $format eq "BED" ) {
6456 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6457 } elsif ( $format eq "PSL" ) {
6458 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6467 $fh_out = Maasha::Common::write_open( $file );
6475 if ( exists $options->{ "database" } and $options->{ "table" } )
6477 if ( $format eq "BED" )
6479 $type = "bed $columns";
6481 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6483 elsif ( $format eq "BED_SS" )
6485 $options->{ "sec_struct" } = 1;
6487 $type = "sec_struct";
6489 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6491 elsif ( $format eq "PSL" )
6495 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6497 elsif ( $format eq "WIGGLE" )
6499 $options->{ "visibility" } = "full";
6501 $wig_file = "$options->{ 'table' }.wig";
6502 $wib_file = "$options->{ 'table' }.wib";
6504 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6506 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6508 if ( $options->{ 'verbose' } ) {
6509 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6511 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6514 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6522 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6527 Maasha::UCSC::update_my_tracks( $options, $type );
6532 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6537 # Martin A. Hansen, November 2009.
6539 # Uses keys from a lookup hash to search records. Optionally, a list of
6540 # keys can be given so the lookup is limited to these, also, flags
6541 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6542 # succeeded, else 0.
6544 my ( $lookup_hash, # hashref with patterns
6546 $keys, # list of keys - OPTIONAL
6547 $vals_only, # only vals flag - OPTIONAL
6548 $keys_only, # only keys flag - OPTIONAL
6555 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6559 if ( not $vals_only ) {
6560 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6563 if ( not $keys_only ) {
6564 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6574 # Martin A. Hansen, November 2009.
6576 # Uses patterns to match records containing the pattern as a substring.
6577 # Returns 1 if the record is matched, else 0.
6579 my ( $patterns, # list of patterns
6581 $keys, # list of keys - OPTIONAL
6582 $vals_only, # only vals flag - OPTIONAL
6583 $keys_only, # only keys flag - OPTIONAL
6590 foreach $pattern ( @{ $patterns } )
6594 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6598 if ( not $vals_only ) {
6599 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6602 if ( not $keys_only ) {
6603 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6614 # Martin A. Hansen, November 2009.
6616 # Uses regex to match records.
6617 # Returns 1 if the record is matched, else 0.
6619 my ( $regex, # regex to match
6621 $keys, # list of keys - OPTIONAL
6622 $vals_only, # only vals flag - OPTIONAL
6623 $keys_only, # only keys flag - OPTIONAL
6630 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6634 if ( not $vals_only ) {
6635 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6638 if ( not $keys_only ) {
6639 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6649 # Martin A. Hansen, November 2009.
6651 # Test if the value of a given record key evaluates according
6652 # to a given operator. Returns 1 if eval is OK, else 0.
6654 my ( $key, # record key
6662 if ( defined $record->{ $key } )
6664 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6665 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6666 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6667 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6668 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6669 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6670 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6671 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6680 # Martin A. Hansen, July 2008.
6682 # Given a biopiece record converts it to a FASTA record.
6683 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6684 # tried in that order.
6686 my ( $record, # record
6691 my ( $seq_name, $seq );
6693 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6694 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6696 if ( defined $seq_name and defined $seq ) {
6697 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6706 # Martin A. Hansen, July 2007.
6708 # Opens a stream to STDIN or a file,
6710 my ( $path, # path - OPTIONAL
6713 # Returns filehandle.
6717 if ( not -t STDIN ) {
6718 $fh = Maasha::Common::read_stdin();
6719 } elsif ( not $path ) {
6720 # Maasha::Common::error( qq(no data stream) );
6722 $fh = Maasha::Common::read_open( $path );
6725 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6733 # Martin A. Hansen, August 2007.
6735 # Opens a stream to STDOUT or a file.
6737 my ( $path, # path - OPTIONAL
6738 $gzip, # compress data - OPTIONAL
6741 # Returns filehandle.
6746 $fh = Maasha::Common::write_open( $path, $gzip );
6748 $fh = Maasha::Common::write_stdout();
6757 # Martin A. Hansen, July 2007.
6759 # Reads one record at a time and converts that record
6760 # to a Perl data structure (a hash) which is returned.
6762 my ( $fh, # handle to stream
6767 my ( $block, @lines, $line, $key, $value, %record );
6769 local $/ = "\n---\n";
6775 return if not defined $block;
6777 @lines = split "\n", $block;
6779 foreach $line ( @lines )
6781 ( $key, $value ) = split ": ", $line, 2;
6783 $record{ $key } = $value;
6786 return wantarray ? %record : \%record;
6792 # Martin A. Hansen, July 2007.
6794 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6796 my ( $data, # data structure
6797 $fh, # file handle - OPTIONAL
6802 if ( scalar keys %{ $data } )
6806 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6811 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6822 # Martin A. Hansen, November 2007.
6824 # Extracts files from an explicit GetOpt::Long argument
6825 # allowing for the use of glob. E.g.
6826 # --data_in=test.fna
6827 # --data_in=test.fna,test2.fna
6829 # --data_in=test.fna,/dir/*.fna
6831 my ( $option, # option from GetOpt::Long
6836 my ( $elem, @files );
6838 foreach $elem ( split ",", $option )
6842 } elsif ( $elem =~ /\*/ ) {
6843 push @files, glob( $elem );
6847 return wantarray ? @files : \@files;
6853 # Martin A. Hansen, April 2008.
6855 # Removes temporary directory and exits gracefully.
6856 # This subroutine is meant to be run always as the last
6857 # thing even if a script is dies or is interrupted
6860 my ( $sig, # signal from the %SIG
6863 # print STDERR "signal->$sig<-\n";
6871 if ( $sig =~ /MAASHA_ERROR/ ) {
6872 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6873 } elsif ( $sig eq "INT" ) {
6874 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6875 } elsif ( $sig eq "TERM" ) {
6876 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6878 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6890 # Martin A. Hansen, July 2008.
6892 # Cleans out any unused temporary files and directories in BP_TMP.
6896 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6898 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6900 $curr_pid = Maasha::Common::get_processid();
6902 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6904 foreach $dir ( @dirs )
6906 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6912 if ( $user eq Maasha::Common::get_user() )
6914 if ( not Maasha::Common::process_running( $pid ) )
6916 # print STDERR "Removing stale dir: $dir\n";
6917 Maasha::Common::dir_remove( $dir );
6919 elsif ( $pid == $curr_pid )
6921 # print STDERR "Removing current dir: $dir\n";
6922 # Maasha::Common::dir_remove( $dir );
6936 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<