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->{ 'tBaseInsert' } ) # ---- Dirty addition to allow Affy data from MySQL to be dumped ----
4523 $record = Maasha::UCSC::psl2record( $record );
4525 $new_record = $record;
4526 $new_record->{ "CHR" } = $record->{ "S_ID" };
4527 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4528 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4529 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4530 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4531 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4533 Maasha::UCSC::bed_put_entry( $new_record, $fh, $cols );
4535 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4537 Maasha::UCSC::bed_put_entry( $record, $fh, $cols );
4540 put_record( $record, $out ) if not $options->{ "no_stream" };
4547 sub script_write_psl
4549 # Martin A. Hansen, August 2007.
4551 # Write PSL output from stream.
4553 my ( $in, # handle to in stream
4554 $out, # handle to out stream
4555 $options, # options hash
4560 my ( $fh, $record, @output, $first );
4564 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4566 while ( $record = get_record( $in ) )
4568 put_record( $record, $out ) if not $options->{ "no_stream" };
4570 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4572 Maasha::UCSC::psl_put_header( $fh ) if $first;
4573 Maasha::UCSC::psl_put_entry( $record, $fh );
4582 sub script_write_fixedstep
4584 # Martin A. Hansen, Juli 2008.
4586 # Write fixedStep entries from recrods in the stream.
4588 my ( $in, # handle to in stream
4589 $out, # handle to out stream
4590 $options, # options hash
4595 my ( $fh, $record, $vals );
4597 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4599 while ( $record = get_record( $in ) )
4601 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4603 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4605 $vals = $record->{ 'VALS' };
4609 print $fh "$vals\n";
4612 put_record( $record, $out ) if not $options->{ "no_stream" };
4619 sub script_write_2bit
4621 # Martin A. Hansen, March 2008.
4623 # Write sequence entries from stream in 2bit format.
4625 my ( $in, # handle to in stream
4626 $out, # handle to out stream
4627 $options, # options hash
4632 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4634 $mask = 1 if not $options->{ "no_mask" };
4636 $tmp_file = "$BP_TMP/write_2bit.fna";
4637 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4639 $fh_out = write_stream( $options->{ "data_out" } );
4641 while ( $record = get_record( $in ) )
4643 if ( $entry = record2fasta( $record ) ) {
4644 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4647 put_record( $record, $out ) if not $options->{ "no_stream" };
4652 $fh_in = Maasha::Common::read_open( $tmp_file );
4654 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4663 sub script_write_solid
4665 # Martin A. Hansen, April 2008.
4667 # Write di-base encoded Solid sequence from entries in stream.
4669 my ( $in, # handle to in stream
4670 $out, # handle to out stream
4671 $options, # options hash
4676 my ( $record, $fh, $entry );
4678 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4680 while ( $record = get_record( $in ) )
4682 if ( $entry = record2fasta( $record ) )
4684 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4686 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4689 put_record( $record, $out ) if not $options->{ "no_stream" };
4696 sub script_write_ucsc_config
4698 # Martin A. Hansen, November 2008.
4700 # Write UCSC Genome Broser configuration (.ra file type) from
4701 # records in the stream.
4703 my ( $in, # handle to in stream
4704 $out, # handle to out stream
4705 $options, # options hash
4710 my ( $record, $fh );
4712 $fh = write_stream( $options->{ "data_out" } );
4714 while ( $record = get_record( $in ) )
4716 Maasha::UCSC::ucsc_config_put_entry( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4718 put_record( $record, $out ) if not $options->{ "no_stream" };
4725 sub script_plot_seqlogo
4727 # Martin A. Hansen, August 2007.
4729 # Calculates and writes a sequence logo for alignments.
4731 my ( $in, # handle to in stream
4732 $out, # handle to out stream
4733 $options, # options hash
4738 my ( $record, @entries, $logo, $fh );
4740 while ( $record = get_record( $in ) )
4742 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4743 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4746 put_record( $record, $out ) if not $options->{ "no_stream" };
4749 $logo = Maasha::Plot::seq_logo( \@entries );
4751 $fh = write_stream( $options->{ "data_out" } );
4759 sub script_plot_phastcons_profiles
4761 # Martin A. Hansen, January 2008.
4763 # Plots PhastCons profiles.
4765 my ( $in, # handle to in stream
4766 $out, # handle to out stream
4767 $options, # options hash
4772 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4774 $options->{ "title" } ||= "PhastCons Profiles";
4776 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4777 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4779 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4780 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4782 while ( $record = get_record( $in ) )
4784 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4786 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4787 $record->{ "CHR_BEG" },
4788 $record->{ "CHR_END" },
4789 $options->{ "flank" } );
4791 push @{ $AoA }, [ @{ $scores } ];
4794 put_record( $record, $out ) if not $options->{ "no_stream" };
4797 Maasha::UCSC::phastcons_normalize( $AoA );
4799 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4800 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4802 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4804 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4806 $fh = write_stream( $options->{ "data_out" } );
4808 print $fh "$_\n" foreach @{ $plot };
4814 sub script_analyze_bed
4816 # Martin A. Hansen, March 2008.
4818 # Analyze BED entries in stream.
4820 my ( $in, # handle to in stream
4821 $out, # handle to out stream
4822 $options, # options hash
4829 while ( $record = get_record( $in ) )
4831 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4833 put_record( $record, $out );
4838 sub script_analyze_vals
4840 # Martin A. Hansen, August 2007.
4842 # Analyze values for given keys in stream.
4844 my ( $in, # handle to in stream
4845 $out, # handle to out stream
4846 $options, # options hash
4851 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4853 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4855 while ( $record = get_record( $in ) )
4857 foreach $key ( keys %{ $record } )
4859 next if $options->{ "keys" } and not exists $key_hash{ $key };
4861 $analysis->{ $key }->{ "COUNT" }++;
4863 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4865 $analysis->{ $key }->{ "TYPE" } = "num";
4866 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4867 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4868 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4872 $len = length $record->{ $key };
4874 $analysis->{ $key }->{ "TYPE" } = "alph";
4875 $analysis->{ $key }->{ "SUM" } += $len;
4876 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4877 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4881 put_record( $record, $out ) if not $options->{ "no_stream" };
4884 foreach $key ( keys %{ $analysis } )
4886 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4887 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4890 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4900 if ( $options->{ "keys" } ) {
4901 @keys = @{ $options->{ "keys" } };
4903 @keys = keys %{ $analysis };
4906 foreach $key ( @keys )
4908 $keys .= sprintf "% 15s", $key;
4909 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4910 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4911 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4912 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4913 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4914 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4917 print $out "$keys\n";
4918 print $out "$types\n";
4919 print $out "$counts\n";
4920 print $out "$mins\n";
4921 print $out "$maxs\n";
4922 print $out "$sums\n";
4923 print $out "$means\n";
4927 sub script_head_records
4929 # Martin A. Hansen, August 2007.
4931 # Display the first sequences in stream.
4933 my ( $in, # handle to in stream
4934 $out, # handle to out stream
4935 $options, # options hash
4940 my ( $record, $count );
4942 $options->{ "num" } ||= 10;
4946 while ( $record = get_record( $in ) )
4950 put_record( $record, $out );
4952 last if $count == $options->{ "num" };
4957 sub script_remove_keys
4959 # Martin A. Hansen, August 2007.
4961 # Remove keys from stream.
4963 my ( $in, # handle to in stream
4964 $out, # handle to out stream
4965 $options, # options hash
4970 my ( $record, $new_record );
4972 while ( $record = get_record( $in ) )
4974 if ( $options->{ "keys" } )
4976 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4978 elsif ( $options->{ "save_keys" } )
4980 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4982 $record = $new_record;
4985 put_record( $record, $out ) if keys %{ $record };
4990 sub script_remove_adaptor
4992 # Martin A. Hansen, August 2008.
4994 # Find and remove adaptor from sequences in the stream.
4996 my ( $in, # handle to in stream
4997 $out, # handle to out stream
4998 $options, # options hash
5003 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
5005 $options->{ "remove" } ||= "after";
5007 $max_mismatch = $options->{ "mismatches" } || 0;
5008 $offset = $options->{ "offset" };
5010 if ( not defined $offset ) {
5016 $adaptor = uc $options->{ "adaptor" };
5017 $adaptor_len = length $adaptor;
5019 while ( $record = get_record( $in ) )
5021 if ( $record->{ "SEQ" } )
5023 $seq = uc $record->{ "SEQ" };
5024 $seq_len = length $seq;
5026 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
5028 $record->{ "ADAPTOR_POS" } = $pos;
5030 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
5032 if ( $options->{ "remove" } eq "after" )
5034 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
5035 $record->{ "SEQ_LEN" } = $pos;
5039 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
5040 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
5044 put_record( $record, $out );
5048 put_record( $record, $out );
5054 sub script_remove_mysql_tables
5056 # Martin A. Hansen, November 2008.
5058 # Remove MySQL tables from values in stream.
5060 my ( $in, # handle to in stream
5061 $out, # handle to out stream
5062 $options, # options hash
5067 my ( $record, %table_hash, $dbh, $table );
5069 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
5070 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
5072 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
5074 while ( $record = get_record( $in ) )
5076 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5078 put_record( $record, $out ) if not $options->{ 'no_stream' };
5081 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5083 foreach $table ( sort keys %table_hash )
5085 if ( Maasha::SQL::table_exists( $dbh, $table ) )
5087 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5088 Maasha::SQL::delete_table( $dbh, $table );
5089 print STDERR "done.\n" if $options->{ 'verbose' };
5093 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
5097 Maasha::SQL::disconnect( $dbh );
5101 sub script_remove_ucsc_tracks
5103 # Martin A. Hansen, November 2008.
5105 # Remove track from MySQL tables and config file.
5107 my ( $in, # handle to in stream
5108 $out, # handle to out stream
5109 $options, # options hash
5114 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
5116 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
5117 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
5118 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
5120 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
5122 while ( $record = get_record( $in ) )
5124 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
5126 put_record( $record, $out ) if not $options->{ 'no_stream' };
5129 # ---- locate track in config file ----
5131 $fh_in = Maasha::Common::read_open( $options->{ 'config_file' } );
5133 while ( $track = Maasha::UCSC::ucsc_config_get_entry( $fh_in ) ) {
5134 push @tracks, $track;
5139 map { push @new_tracks, $_ if not exists $track_hash{ $_->{ 'track' } } } @tracks;
5141 print STDERR qq(WARNING: track not found in config file: "$options->{ 'config_file' }"\n) if scalar @tracks == scalar @new_tracks;
5143 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
5145 $fh_out = Maasha::Common::write_open( $options->{ 'config_file' } );
5147 map { Maasha::UCSC::ucsc_config_put_entry( $_, $fh_out ) } @new_tracks;
5151 # ---- locate track in database ----
5153 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
5155 foreach $track ( sort keys %track_hash )
5157 if ( Maasha::SQL::table_exists( $dbh, $track ) )
5159 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
5160 Maasha::SQL::delete_table( $dbh, $track );
5161 print STDERR "done.\n" if $options->{ 'verbose' };
5165 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
5169 Maasha::SQL::disconnect( $dbh );
5171 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
5175 sub script_rename_keys
5177 # Martin A. Hansen, August 2007.
5179 # Rename keys in stream.
5181 my ( $in, # handle to in stream
5182 $out, # handle to out stream
5183 $options, # options hash
5190 while ( $record = get_record( $in ) )
5192 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5194 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
5196 delete $record->{ $options->{ "keys" }->[ 0 ] };
5199 put_record( $record, $out );
5204 sub script_uniq_vals
5206 # Martin A. Hansen, August 2007.
5208 # Find unique values in stream.
5210 my ( $in, # handle to in stream
5211 $out, # handle to out stream
5212 $options, # options hash
5217 my ( %hash, $record );
5219 while ( $record = get_record( $in ) )
5221 if ( $record->{ $options->{ "key" } } )
5223 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
5225 put_record( $record, $out );
5227 $hash{ $record->{ $options->{ "key" } } } = 1;
5229 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
5231 put_record( $record, $out );
5235 $hash{ $record->{ $options->{ "key" } } } = 1;
5240 put_record( $record, $out );
5246 sub script_merge_vals
5248 # Martin A. Hansen, August 2007.
5250 # Rename keys in stream.
5252 my ( $in, # handle to in stream
5253 $out, # handle to out stream
5254 $options, # options hash
5259 my ( $record, @join, $i );
5261 $options->{ "delimit" } ||= '_';
5263 while ( $record = get_record( $in ) )
5265 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5267 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5269 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5270 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5273 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5276 put_record( $record, $out );
5281 sub script_merge_records
5283 # Martin A. Hansen, July 2008.
5285 # Merges records in the stream based on identical values of two given keys.
5287 my ( $in, # handle to in stream
5288 $out, # handle to out stream
5289 $options, # options hash
5294 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5295 $num1, $num2, $num, $cmp, $i );
5297 $merge = $options->{ "merge" } || "AandB";
5299 $file1 = "$BP_TMP/merge_records1.tmp";
5300 $file2 = "$BP_TMP/merge_records2.tmp";
5302 $fh1 = Maasha::Common::write_open( $file1 );
5303 $fh2 = Maasha::Common::write_open( $file2 );
5305 $key1 = $options->{ "keys" }->[ 0 ];
5306 $key2 = $options->{ "keys" }->[ 1 ];
5308 $num = $key2 =~ s/n$//;
5312 while ( $record = get_record( $in ) )
5314 if ( exists $record->{ $key1 } )
5317 @vals1 = $record->{ $key1 };
5319 delete $record->{ $key1 };
5321 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5323 print $fh1 join( "\t", @vals1 ), "\n";
5327 elsif ( exists $record->{ $key2 } )
5330 @vals2 = $record->{ $key2 };
5332 delete $record->{ $key2 };
5334 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5336 print $fh2 join( "\t", @vals2 ), "\n";
5347 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5348 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5352 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5353 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5356 $fh1 = Maasha::Common::read_open( $file1 );
5357 $fh2 = Maasha::Common::read_open( $file2 );
5359 @vals1 = Maasha::Common::get_fields( $fh1 );
5360 @vals2 = Maasha::Common::get_fields( $fh2 );
5362 while ( $num1 > 0 and $num2 > 0 )
5367 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5369 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5374 if ( $merge =~ /^(AorB|AnotB)$/ )
5376 for ( $i = 0; $i < @keys1; $i++ ) {
5377 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5380 put_record( $record, $out );
5383 @vals1 = Maasha::Common::get_fields( $fh1 );
5388 if ( $merge =~ /^(BorA|BnotA)$/ )
5390 for ( $i = 0; $i < @keys2; $i++ ) {
5391 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5394 put_record( $record, $out );
5397 @vals2 = Maasha::Common::get_fields( $fh2 );
5402 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5404 for ( $i = 0; $i < @keys1; $i++ ) {
5405 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5408 for ( $i = 1; $i < @keys2; $i++ ) {
5409 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5412 put_record( $record, $out );
5415 @vals1 = Maasha::Common::get_fields( $fh1 );
5416 @vals2 = Maasha::Common::get_fields( $fh2 );
5428 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5432 for ( $i = 0; $i < @keys1; $i++ ) {
5433 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5436 put_record( $record, $out );
5439 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5443 for ( $i = 0; $i < @keys2; $i++ ) {
5444 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5447 put_record( $record, $out );
5454 # Martin A. Hansen, August 2007.
5456 # Grab for records in stream.
5458 my ( $in, # handle to in stream
5459 $out, # handle to out stream
5460 $options, # options hash
5465 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5467 $keys = $options->{ 'keys' };
5468 $vals_only = $options->{ 'vals_only' };
5469 $keys_only = $options->{ 'keys_only' };
5470 $invert = $options->{ 'invert' };
5472 if ( $options->{ 'patterns' } )
5474 $patterns = [ split ",", $options->{ 'patterns' } ];
5476 elsif ( -f $options->{ 'patterns_in' } )
5478 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5480 elsif ( $options->{ 'regex' } )
5482 if ( $options->{ 'case_insensitive' } ) {
5483 $regex = qr/$options->{ 'regex' }/i;
5485 $regex = qr/$options->{ 'regex' }/;
5488 elsif ( -f $options->{ 'exact_in' } )
5490 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5492 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5496 elsif ( $options->{ 'eval' } )
5498 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5506 while ( $record = get_record( $in ) )
5510 if ( %lookup_hash ) {
5511 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5512 } elsif ( $patterns ) {
5513 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5514 } elsif ( $regex ) {
5515 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5517 $found = grab_eval( $key, $op, $val, $record );
5520 if ( $found and not $invert ) {
5521 put_record( $record, $out );
5522 } elsif ( not $found and $invert ) {
5523 put_record( $record, $out );
5531 # Martin A. Hansen, August 2007.
5533 # Evaluate extression for records in stream.
5535 my ( $in, # handle to in stream
5536 $out, # handle to out stream
5537 $options, # options hash
5542 my ( $record, $eval_key, @keys, $eval_val );
5544 while ( $record = get_record( $in ) )
5546 if ( $options->{ "eval" } )
5548 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
5555 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5557 @keys = grep { exists $record->{ $_ } } @keys;
5560 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5562 $record->{ $eval_key } = eval "$eval_val";
5563 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
5567 warn qq(WARNING: Bad compute expression: "$options->{ 'eval' }"\n);
5571 put_record( $record, $out );
5578 # Martin A. Hansen, June 2008.
5582 my ( $in, # handle to in stream
5583 $out, # handle to out stream
5584 $options, # options hash
5589 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5591 while ( $record = get_record( $in ) )
5595 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5597 push @rows, $record->{ $key };
5601 push @matrix, [ @rows ];
5606 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5608 foreach $row ( @matrix )
5610 for ( $i = 0; $i < @{ $row }; $i++ ) {
5611 $record->{ "V$i" } = $row->[ $i ];
5614 put_record( $record, $out );
5619 sub script_add_ident
5621 # Martin A. Hansen, May 2008.
5623 # Add a unique identifier to each record in stream.
5625 my ( $in, # handle to in stream
5626 $out, # handle to out stream
5627 $options, # options hash
5632 my ( $record, $key, $prefix, $i );
5634 $key = $options->{ "key" } || "ID";
5635 $prefix = $options->{ "prefix" } || "ID";
5639 while ( $record = get_record( $in ) )
5641 $record->{ $key } = sprintf( "$prefix%08d", $i );
5643 put_record( $record, $out );
5650 sub script_count_records
5652 # Martin A. Hansen, August 2007.
5654 # Count records in stream.
5656 my ( $in, # handle to in stream
5657 $out, # handle to out stream
5658 $options, # options hash
5663 my ( $record, $count, $result, $fh, $line );
5667 if ( $options->{ "no_stream" } )
5669 while ( $line = <$in> )
5673 $count++ if $line eq "---";
5678 while ( $record = get_record( $in ) )
5680 put_record( $record, $out );
5686 $result = { "RECORDS_COUNT" => $count };
5688 $fh = write_stream( $options->{ "data_out" } );
5690 put_record( $result, $fh );
5696 sub script_random_records
5698 # Martin A. Hansen, August 2007.
5700 # Pick a number or random records from stream.
5702 my ( $in, # handle to in stream
5703 $out, # handle to out stream
5704 $options, # options hash
5709 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5711 $options->{ "num" } ||= 10;
5713 $tmp_file = "$BP_TMP/random_records.tmp";
5715 $fh_out = Maasha::Common::write_open( $tmp_file );
5719 while ( $record = get_record( $in ) )
5721 put_record( $record, $fh_out );
5731 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5733 while ( $i < $options->{ "num" } )
5735 $rand = int( rand( $count ) );
5737 if ( not exists $rand_hash{ $rand } )
5739 $rand_hash{ $rand } = 1;
5741 $max = $rand if $rand > $max;
5747 $fh_in = Maasha::Common::read_open( $tmp_file );
5751 while ( $record = get_record( $fh_in ) )
5753 put_record( $record, $out ) if exists $rand_hash{ $count };
5755 last if $count == $max;
5766 sub script_sort_records
5768 # Martin A. Hansen, August 2007.
5770 # Sort to sort records according to keys.
5772 my ( $in, # handle to in stream
5773 $out, # handle to out stream
5774 $options, # options hash
5779 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5781 foreach $key ( @{ $options->{ "keys" } } )
5783 if ( $key =~ s/n$// ) {
5784 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5786 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5790 $sort_str = join " or ", @sort_cmd;
5791 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5793 while ( $record = get_record( $in ) ) {
5794 push @records, $record;
5797 @records = sort $sort_sub @records;
5799 if ( $options->{ "reverse" } )
5801 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5802 put_record( $records[ $i ], $out );
5807 for ( $i = 0; $i < scalar @records; $i++ ) {
5808 put_record( $records[ $i ], $out );
5814 sub script_count_vals
5816 # Martin A. Hansen, August 2007.
5818 # Count records in stream.
5820 my ( $in, # handle to in stream
5821 $out, # handle to out stream
5822 $options, # options hash
5827 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5829 $tmp_file = "$BP_TMP/count_cache.tmp";
5831 $fh_out = Maasha::Common::write_open( $tmp_file );
5836 while ( $record = get_record( $in ) )
5838 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5840 push @records, $record;
5842 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5844 map { put_record( $_, $fh_out ) } @records;
5851 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5862 $fh_in = Maasha::Common::read_open( $tmp_file );
5864 while ( $record = get_record( $fh_in ) )
5866 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5868 put_record( $record, $out );
5870 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5878 foreach $record ( @records )
5880 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5882 put_record( $record, $out );
5889 sub script_plot_histogram
5891 # Martin A. Hansen, September 2007.
5893 # Plot a simple histogram for a given key using GNU plot.
5895 my ( $in, # handle to in stream
5896 $out, # handle to out stream
5897 $options, # options hash
5902 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5904 $options->{ "title" } ||= "Histogram";
5905 $options->{ "sort" } ||= "num";
5907 while ( $record = get_record( $in ) )
5909 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5911 put_record( $record, $out ) if not $options->{ "no_stream" };
5914 if ( $options->{ "sort" } eq "num" ) {
5915 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5917 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5920 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5922 $fh = write_stream( $options->{ "data_out" } );
5924 print $fh "$_\n" foreach @{ $result };
5930 sub script_plot_lendist
5932 # Martin A. Hansen, August 2007.
5934 # Plot length distribution using GNU plot.
5936 my ( $in, # handle to in stream
5937 $out, # handle to out stream
5938 $options, # options hash
5943 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5945 $options->{ "title" } ||= "Length Distribution";
5947 while ( $record = get_record( $in ) )
5949 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5951 put_record( $record, $out ) if not $options->{ "no_stream" };
5954 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5956 for ( $i = 0; $i < $max; $i++ ) {
5957 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5960 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5962 $fh = write_stream( $options->{ "data_out" } );
5964 print $fh "$_\n" foreach @{ $result };
5970 sub script_plot_chrdist
5972 # Martin A. Hansen, August 2007.
5974 # Plot chromosome distribution using GNU plot.
5976 my ( $in, # handle to in stream
5977 $out, # handle to out stream
5978 $options, # options hash
5983 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5985 $options->{ "title" } ||= "Chromosome Distribution";
5987 while ( $record = get_record( $in ) )
5989 if ( $record->{ "CHR" } ) { # generic
5990 $data_hash{ $record->{ "CHR" } }++;
5991 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5992 $data_hash{ $record->{ "S_ID" } }++;
5993 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5994 $data_hash{ $record->{ "S_ID" } }++;
5995 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5996 $data_hash{ $record->{ "S_ID" } }++;
5999 put_record( $record, $out ) if not $options->{ "no_stream" };
6002 foreach $elem ( keys %data_hash )
6006 $sort_key =~ s/chr//i;
6008 $sort_key =~ s/^X(.*)/99$1/;
6009 $sort_key =~ s/^Y(.*)/99$1/;
6010 $sort_key =~ s/^Z(.*)/999$1/;
6011 $sort_key =~ s/^M(.*)/9999$1/;
6012 $sort_key =~ s/^U(.*)/99999$1/;
6014 $count = $sort_key =~ tr/_//;
6016 $sort_key =~ s/_.*/"999999" x $count/ex;
6018 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
6021 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
6023 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
6025 $fh = write_stream( $options->{ "data_out" } );
6027 print $fh "$_\n" foreach @{ $result };
6033 sub script_plot_karyogram
6035 # Martin A. Hansen, August 2007.
6037 # Plot hits on karyogram.
6039 my ( $in, # handle to in stream
6040 $out, # handle to out stream
6041 $options, # options hash
6046 my ( %options, $record, @data, $fh, $result, %data_hash );
6048 $options->{ "genome" } ||= "human";
6049 $options->{ "feat_color" } ||= "black";
6051 while ( $record = get_record( $in ) )
6053 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
6055 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
6058 put_record( $record, $out ) if not $options->{ "no_stream" };
6061 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
6063 $fh = write_stream( $options->{ "data_out" } );
6071 sub script_plot_matches
6073 # Martin A. Hansen, August 2007.
6075 # Plot matches in 2D generating a dotplot.
6077 my ( $in, # handle to in stream
6078 $out, # handle to out stream
6079 $options, # options hash
6084 my ( $record, @data, $fh, $result, %data_hash );
6086 $options->{ "direction" } ||= "both";
6088 while ( $record = get_record( $in ) )
6090 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
6091 push @data, $record;
6094 put_record( $record, $out ) if not $options->{ "no_stream" };
6097 $options->{ "title" } ||= "plot_matches";
6098 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
6099 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
6101 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
6103 $fh = write_stream( $options->{ "data_out" } );
6105 print $fh "$_\n" foreach @{ $result };
6111 sub script_length_vals
6113 # Martin A. Hansen, August 2007.
6115 # Determine the length of the value for given keys.
6117 my ( $in, # handle to in stream
6118 $out, # handle to out stream
6119 $options, # options hash
6124 my ( $record, $key );
6126 while ( $record = get_record( $in ) )
6128 foreach $key ( @{ $options->{ "keys" } } )
6130 if ( $record->{ $key } ) {
6131 $record->{ $key . "_LEN" } = length $record->{ $key };
6135 put_record( $record, $out );
6142 # Martin A. Hansen, August 2007.
6144 # Calculates the sums for values of given keys.
6146 my ( $in, # handle to in stream
6147 $out, # handle to out stream
6148 $options, # options hash
6153 my ( $record, $key, %sum_hash, $fh );
6155 while ( $record = get_record( $in ) )
6157 foreach $key ( @{ $options->{ "keys" } } )
6159 if ( $record->{ $key } ) {
6160 $sum_hash{ $key } += $record->{ $key };
6164 put_record( $record, $out ) if not $options->{ "no_stream" };
6167 $fh = write_stream( $options->{ "data_out" } );
6169 foreach $key ( @{ $options->{ "keys" } } ) {
6170 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
6177 sub script_mean_vals
6179 # Martin A. Hansen, August 2007.
6181 # Calculate the mean of values of given keys.
6183 my ( $in, # handle to in stream
6184 $out, # handle to out stream
6185 $options, # options hash
6190 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
6192 while ( $record = get_record( $in ) )
6194 foreach $key ( @{ $options->{ "keys" } } )
6196 if ( $record->{ $key } )
6198 $sum_hash{ $key } += $record->{ $key };
6199 $count_hash{ $key }++;
6203 put_record( $record, $out ) if not $options->{ "no_stream" };
6206 $fh = write_stream( $options->{ "data_out" } );
6208 foreach $key ( @{ $options->{ "keys" } } )
6210 if ( $count_hash{ $key } ) {
6211 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
6216 put_record( { $key . "_MEAN" => $mean } , $fh );
6223 sub script_median_vals
6225 # Martin A. Hansen, March 2008.
6227 # Calculate the median values of given keys.
6229 my ( $in, # handle to in stream
6230 $out, # handle to out stream
6231 $options, # options hash
6236 my ( $record, $key, %median_hash, $median, $fh );
6238 while ( $record = get_record( $in ) )
6240 foreach $key ( @{ $options->{ "keys" } } ) {
6241 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6244 put_record( $record, $out ) if not $options->{ "no_stream" };
6247 $fh = write_stream( $options->{ "data_out" } );
6249 foreach $key ( @{ $options->{ "keys" } } )
6251 if ( $median_hash{ $key } ) {
6252 $median = Maasha::Calc::median( $median_hash{ $key } );
6257 put_record( { $key . "_MEDIAN" => $median } , $fh );
6266 # Martin A. Hansen, February 2008.
6268 # Determine the maximum values of given keys.
6270 my ( $in, # handle to in stream
6271 $out, # handle to out stream
6272 $options, # options hash
6277 my ( $record, $key, $fh, %max_hash, $max_record );
6279 while ( $record = get_record( $in ) )
6281 foreach $key ( @{ $options->{ "keys" } } )
6283 if ( $record->{ $key } )
6285 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6289 put_record( $record, $out ) if not $options->{ "no_stream" };
6292 $fh = write_stream( $options->{ "data_out" } );
6294 foreach $key ( @{ $options->{ "keys" } } )
6296 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6299 put_record( $max_record, $fh );
6307 # Martin A. Hansen, February 2008.
6309 # Determine the minimum values of given keys.
6311 my ( $in, # handle to in stream
6312 $out, # handle to out stream
6313 $options, # options hash
6318 my ( $record, $key, $fh, %min_hash, $min_record );
6320 while ( $record = get_record( $in ) )
6322 foreach $key ( @{ $options->{ "keys" } } )
6324 if ( defined $record->{ $key } )
6326 if ( exists $min_hash{ $key } ) {
6327 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6329 $min_hash{ $key } = $record->{ $key };
6334 put_record( $record, $out ) if not $options->{ "no_stream" };
6337 $fh = write_stream( $options->{ "data_out" } );
6339 foreach $key ( @{ $options->{ "keys" } } )
6341 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6344 put_record( $min_record, $fh );
6350 sub script_upload_to_ucsc
6352 # Martin A. Hansen, August 2007.
6354 # Calculate the mean of values of given keys.
6356 # This routine has developed into the most ugly hack. Do something!
6358 my ( $in, # handle to in stream
6359 $out, # handle to out stream
6360 $options, # options hash
6365 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
6367 $options->{ "short_label" } ||= $options->{ 'table' };
6368 $options->{ "long_label" } ||= $options->{ 'table' };
6369 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6370 $options->{ "priority" } ||= 1;
6371 $options->{ "visibility" } ||= "pack";
6372 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6373 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6375 $file = "$BP_TMP/ucsc_upload.tmp";
6383 $fh_out = Maasha::Common::write_open( $file );
6385 while ( $record = get_record( $in ) )
6387 put_record( $record, $out ) if not $options->{ "no_stream" };
6389 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6391 $vals = $record->{ "VALS" };
6394 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6395 print $fh_out "$vals\n";
6397 $format = "WIGGLE" if not $format;
6399 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6401 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6402 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6406 $format = "PSL" if not $format;
6408 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6410 # chrom chromStart chromEnd name score strand size secStr conf
6412 print $fh_out join ( "\t",
6414 $record->{ "CHR_BEG" },
6415 $record->{ "CHR_END" } + 1,
6416 $record->{ "Q_ID" },
6417 $record->{ "SCORE" },
6418 $record->{ "STRAND" },
6419 $record->{ "SIZE" },
6420 $record->{ "SEC_STRUCT" },
6421 $record->{ "CONF" },
6424 $format = "BED_SS" if not $format;
6426 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6428 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6430 $format = "BED" if not $format;
6431 $columns = $record->{ "BED_COLS" } if not $columns;
6433 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6435 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6437 $format = "BED" if not $format;
6438 $columns = 6 if not $columns;
6440 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6442 $record->{ "CHR" } = $record->{ "S_ID" };
6443 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6444 $record->{ "CHR_END" } = $record->{ "S_END" };
6445 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6447 $format = "BED" if not $format;
6448 $columns = 6 if not $columns;
6450 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6452 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6454 $record->{ "CHR" } = $record->{ "S_ID" };
6455 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6456 $record->{ "CHR_END" } = $record->{ "S_END" };
6457 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6458 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6460 $format = "BED" if not $format;
6461 $columns = 6 if not $columns;
6463 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6466 if ( $i == $options->{ "chunk_size" } )
6470 if ( $format eq "BED" ) {
6471 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6472 } elsif ( $format eq "PSL" ) {
6473 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6482 $fh_out = Maasha::Common::write_open( $file );
6490 if ( exists $options->{ "database" } and $options->{ "table" } )
6492 if ( $format eq "BED" )
6494 $type = "bed $columns";
6496 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6498 elsif ( $format eq "BED_SS" )
6500 $options->{ "sec_struct" } = 1;
6502 $type = "sec_struct";
6504 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6506 elsif ( $format eq "PSL" )
6510 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6512 elsif ( $format eq "WIGGLE" )
6514 $options->{ "visibility" } = "full";
6516 $wig_file = "$options->{ 'table' }.wig";
6517 $wib_file = "$options->{ 'table' }.wib";
6519 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6521 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6523 if ( $options->{ 'verbose' } ) {
6524 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6526 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6529 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6537 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6542 Maasha::UCSC::update_my_tracks( $options, $type );
6547 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6552 # Martin A. Hansen, November 2009.
6554 # Uses keys from a lookup hash to search records. Optionally, a list of
6555 # keys can be given so the lookup is limited to these, also, flags
6556 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6557 # succeeded, else 0.
6559 my ( $lookup_hash, # hashref with patterns
6561 $keys, # list of keys - OPTIONAL
6562 $vals_only, # only vals flag - OPTIONAL
6563 $keys_only, # only keys flag - OPTIONAL
6570 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6574 if ( not $vals_only ) {
6575 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6578 if ( not $keys_only ) {
6579 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6589 # Martin A. Hansen, November 2009.
6591 # Uses patterns to match records containing the pattern as a substring.
6592 # Returns 1 if the record is matched, else 0.
6594 my ( $patterns, # list of patterns
6596 $keys, # list of keys - OPTIONAL
6597 $vals_only, # only vals flag - OPTIONAL
6598 $keys_only, # only keys flag - OPTIONAL
6605 foreach $pattern ( @{ $patterns } )
6609 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6613 if ( not $vals_only ) {
6614 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6617 if ( not $keys_only ) {
6618 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6629 # Martin A. Hansen, November 2009.
6631 # Uses regex to match records.
6632 # Returns 1 if the record is matched, else 0.
6634 my ( $regex, # regex to match
6636 $keys, # list of keys - OPTIONAL
6637 $vals_only, # only vals flag - OPTIONAL
6638 $keys_only, # only keys flag - OPTIONAL
6645 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6649 if ( not $vals_only ) {
6650 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6653 if ( not $keys_only ) {
6654 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6664 # Martin A. Hansen, November 2009.
6666 # Test if the value of a given record key evaluates according
6667 # to a given operator. Returns 1 if eval is OK, else 0.
6669 my ( $key, # record key
6677 if ( defined $record->{ $key } )
6679 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6680 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6681 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6682 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6683 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6684 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6685 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6686 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6695 # Martin A. Hansen, July 2008.
6697 # Given a biopiece record converts it to a FASTA record.
6698 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6699 # tried in that order.
6701 my ( $record, # record
6706 my ( $seq_name, $seq );
6708 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6709 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6711 if ( defined $seq_name and defined $seq ) {
6712 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6721 # Martin A. Hansen, July 2007.
6723 # Opens a stream to STDIN or a file,
6725 my ( $path, # path - OPTIONAL
6728 # Returns filehandle.
6732 if ( not -t STDIN ) {
6733 $fh = Maasha::Common::read_stdin();
6734 } elsif ( not $path ) {
6735 # Maasha::Common::error( qq(no data stream) );
6737 $fh = Maasha::Common::read_open( $path );
6740 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6748 # Martin A. Hansen, August 2007.
6750 # Opens a stream to STDOUT or a file.
6752 my ( $path, # path - OPTIONAL
6753 $gzip, # compress data - OPTIONAL
6756 # Returns filehandle.
6761 $fh = Maasha::Common::write_open( $path, $gzip );
6763 $fh = Maasha::Common::write_stdout();
6772 # Martin A. Hansen, July 2007.
6774 # Reads one record at a time and converts that record
6775 # to a Perl data structure (a hash) which is returned.
6777 my ( $fh, # handle to stream
6782 my ( $block, @lines, $line, $key, $value, %record );
6784 local $/ = "\n---\n";
6790 return if not defined $block;
6792 @lines = split "\n", $block;
6794 foreach $line ( @lines )
6796 ( $key, $value ) = split ": ", $line, 2;
6798 $record{ $key } = $value;
6801 return wantarray ? %record : \%record;
6807 # Martin A. Hansen, July 2007.
6809 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6811 my ( $data, # data structure
6812 $fh, # file handle - OPTIONAL
6817 if ( scalar keys %{ $data } )
6821 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6826 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6837 # Martin A. Hansen, November 2007.
6839 # Extracts files from an explicit GetOpt::Long argument
6840 # allowing for the use of glob. E.g.
6841 # --data_in=test.fna
6842 # --data_in=test.fna,test2.fna
6844 # --data_in=test.fna,/dir/*.fna
6846 my ( $option, # option from GetOpt::Long
6851 my ( $elem, @files );
6853 foreach $elem ( split ",", $option )
6857 } elsif ( $elem =~ /\*/ ) {
6858 push @files, glob( $elem );
6862 return wantarray ? @files : \@files;
6868 # Martin A. Hansen, April 2008.
6870 # Removes temporary directory and exits gracefully.
6871 # This subroutine is meant to be run always as the last
6872 # thing even if a script is dies or is interrupted
6875 my ( $sig, # signal from the %SIG
6878 # print STDERR "signal->$sig<-\n";
6886 if ( $sig =~ /MAASHA_ERROR/ ) {
6887 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6888 } elsif ( $sig eq "INT" ) {
6889 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6890 } elsif ( $sig eq "TERM" ) {
6891 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6893 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6905 # Martin A. Hansen, July 2008.
6907 # Cleans out any unused temporary files and directories in BP_TMP.
6911 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6913 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6915 $curr_pid = Maasha::Common::get_processid();
6917 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6919 foreach $dir ( @dirs )
6921 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6927 if ( $user eq Maasha::Common::get_user() )
6929 if ( not Maasha::Common::process_running( $pid ) )
6931 # print STDERR "Removing stale dir: $dir\n";
6932 Maasha::Common::dir_remove( $dir );
6934 elsif ( $pid == $curr_pid )
6936 # print STDERR "Removing current dir: $dir\n";
6937 # Maasha::Common::dir_remove( $dir );
6951 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<