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 );
45 use Maasha::Stockholm;
51 use Maasha::UCSC::BED;
52 use Maasha::UCSC::Wiggle;
61 use vars qw( @ISA @EXPORT_OK );
65 @ISA = qw( Exporter );
80 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
83 $SIG{ '__DIE__' } = \&sig_handler;
84 $SIG{ 'INT' } = \&sig_handler;
85 $SIG{ 'TERM' } = \&sig_handler;
88 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
91 my ( $script, $BP_TMP );
93 $script = Maasha::Common::get_scriptname();
94 $BP_TMP = Maasha::Common::get_tmpdir();
97 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
100 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
101 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
103 $log_global->autoflush( 1 );
104 $log_local->autoflush( 1 );
106 &log( $log_global, $script, \@ARGV );
107 &log( $log_local, $script, \@ARGV );
113 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
116 run_script( $script );
119 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
124 # Martin A. Hansen, January 2008.
126 # Log messages to logfile.
128 my ( $fh, # filehandle to logfile
129 $script, # script name
130 $argv, # reference to @ARGV
135 my ( $time_stamp, $user );
137 $time_stamp = Maasha::Common::time_stamp();
139 $user = $ENV{ 'USER' };
141 $script = "biopieces" if $script eq "-e";
143 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
149 # Martin A. Hansen, August 2007.
151 # Run a specific script.
153 my ( $script, # script name
158 my ( $t0, $t1, $options, $in, $out );
160 $t0 = gettimeofday();
162 $options = get_options( $script );
164 $options->{ "SCRIPT" } = $script;
166 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
167 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
170 $in = read_stream( $options->{ "stream_in" } );
171 $out = write_stream( $options->{ "stream_out" } );
173 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
174 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
175 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
176 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
177 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
178 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
179 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
180 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
181 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
182 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
183 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
184 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
185 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
186 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
187 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
188 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
189 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
190 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
191 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
192 elsif ( $script eq "assemble_tag_contigs" ) { script_assemble_tag_contigs( $in, $out, $options ) }
193 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
194 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
195 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
196 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
198 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
199 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
200 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
201 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
202 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
203 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
204 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
205 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
206 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
208 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
209 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
210 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
213 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
214 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
216 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
217 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
218 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
219 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
220 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
221 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
222 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
223 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
224 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
225 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
226 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
227 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
228 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
229 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
230 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
231 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
232 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
233 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
234 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
235 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
236 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
237 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
238 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
239 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
240 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
241 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
242 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
243 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
244 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
245 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
246 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
247 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
248 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
249 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
250 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
251 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
252 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
253 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
254 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
255 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
256 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
257 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
258 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
259 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
260 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
261 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
262 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
263 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
264 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
265 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
266 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
267 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
268 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
269 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
270 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
272 close $in if defined $in;
275 $t1 = gettimeofday();
277 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
283 # Martin A. Hansen, February 2008.
285 # Gets options from commandline and checks these vigerously.
287 my ( $script, # name of script
292 my ( %options, @options, $opt, @genomes, $real );
294 if ( $script eq "print_usage" )
300 elsif ( $script eq "read_fasta" )
307 elsif ( $script eq "read_tab" )
318 elsif ( $script eq "read_psl" )
325 elsif ( $script eq "read_bed" )
333 elsif ( $script eq "read_fixedstep" )
340 elsif ( $script eq "read_blast_tab" )
347 elsif ( $script eq "read_embl" )
357 elsif ( $script eq "read_stockholm" )
364 elsif ( $script eq "read_phastcons" )
375 elsif ( $script eq "read_soft" )
383 elsif ( $script eq "read_gff" )
390 elsif ( $script eq "read_2bit" )
398 elsif ( $script eq "read_solexa" )
407 elsif ( $script eq "read_solid" )
415 elsif ( $script eq "read_mysql" )
424 elsif ( $script eq "read_ucsc_config" )
431 elsif ( $script eq "format_genome" )
440 elsif ( $script eq "length_seq" )
447 elsif ( $script eq "oligo_freq" )
454 elsif ( $script eq "create_weight_matrix" )
460 elsif ( $script eq "calc_fixedstep" )
467 elsif ( $script eq "transliterate_seq" )
475 elsif ( $script eq "transliterate_vals" )
484 elsif ( $script eq "translate_seq" )
490 elsif ( $script eq "extract_seq" )
498 elsif ( $script eq "get_genome_seq" )
510 elsif ( $script eq "get_genome_align" )
521 elsif ( $script eq "get_genome_phastcons" )
532 elsif ( $script eq "split_seq" )
539 elsif ( $script eq "split_bed" )
546 elsif ( $script eq "tile_seq" )
553 elsif ( $script eq "invert_align" )
559 elsif ( $script eq "patscan_seq" )
570 elsif ( $script eq "create_blast_db" )
577 elsif ( $script eq "blast_seq" )
589 elsif ( $script eq "blat_seq" )
601 elsif ( $script eq "soap_seq" )
612 elsif ( $script eq "match_seq" )
619 elsif ( $script eq "create_vmatch_index" )
627 elsif ( $script eq "vmatch_seq" )
638 elsif ( $script eq "write_fasta" )
647 elsif ( $script eq "write_align" )
657 elsif ( $script eq "write_blast" )
666 elsif ( $script eq "write_tab" )
678 elsif ( $script eq "write_bed" )
687 elsif ( $script eq "write_psl" )
695 elsif ( $script eq "write_fixedstep" )
703 elsif ( $script eq "write_2bit" )
711 elsif ( $script eq "write_solid" )
720 elsif ( $script eq "write_ucsc_config" )
727 elsif ( $script eq "plot_seqlogo" )
734 elsif ( $script eq "plot_phastcons_profiles" )
749 elsif ( $script eq "analyze_vals" )
756 elsif ( $script eq "head_records" )
762 elsif ( $script eq "remove_keys" )
769 elsif ( $script eq "remove_adaptor" )
778 elsif ( $script eq "remove_mysql_tables" )
789 elsif ( $script eq "remove_ucsc_tracks" )
801 elsif ( $script eq "rename_keys" )
807 elsif ( $script eq "uniq_vals" )
814 elsif ( $script eq "merge_vals" )
821 elsif ( $script eq "merge_records" )
828 elsif ( $script eq "grab" )
843 elsif ( $script eq "compute" )
849 elsif ( $script eq "add_ident" )
856 elsif ( $script eq "count_records" )
863 elsif ( $script eq "random_records" )
869 elsif ( $script eq "sort_records" )
876 elsif ( $script eq "count_vals" )
882 elsif ( $script eq "plot_histogram" )
895 elsif ( $script eq "plot_lendist" )
907 elsif ( $script eq "plot_chrdist" )
918 elsif ( $script eq "plot_karyogram" )
927 elsif ( $script eq "plot_matches" )
939 elsif ( $script eq "length_vals" )
945 elsif ( $script eq "sum_vals" )
953 elsif ( $script eq "mean_vals" )
961 elsif ( $script eq "median_vals" )
969 elsif ( $script eq "max_vals" )
977 elsif ( $script eq "min_vals" )
985 elsif ( $script eq "upload_to_ucsc" )
1009 # print STDERR Dumper( \@options );
1016 # print STDERR Dumper( \%options );
1018 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
1019 return wantarray ? %options : \%options;
1022 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
1023 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
1024 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
1025 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
1026 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
1027 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
1028 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
1029 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
1030 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
1031 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
1032 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
1034 # ---- check arguments ----
1036 if ( $options{ 'data_in' } )
1038 $options{ "files" } = getopt_files( $options{ 'data_in' } );
1040 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
1043 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
1045 # print STDERR Dumper( \%options );
1047 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1049 foreach $opt ( keys %options )
1051 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1053 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1055 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1057 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1059 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1061 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1063 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1065 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1067 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1069 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1071 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1073 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1075 elsif ( $opt eq "genome" and $script ne "format_genome" )
1077 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1078 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1080 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1081 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1084 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1086 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1088 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1090 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1092 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1094 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1096 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1098 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1100 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1102 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1106 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1107 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1108 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1109 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1110 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1111 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1112 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1113 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1114 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" };
1115 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1116 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" };
1118 if ( $script eq "upload_to_ucsc" )
1120 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1121 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1124 return wantarray ? %options : \%options;
1128 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1131 sub script_print_usage
1133 # Martin A. Hansen, January 2008.
1135 # Retrieves usage information from file and
1136 # prints this nicely formatted.
1138 my ( $in, # handle to in stream
1139 $out, # handle to out stream
1140 $options, # options hash
1145 my ( $file, $wiki, $lines );
1147 if ( $options->{ 'data_in' } ) {
1148 $file = $options->{ 'data_in' };
1150 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1153 $wiki = Maasha::Gwiki::gwiki_read( $file );
1155 if ( not $options->{ "help" } ) {
1156 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1159 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1161 print STDERR "$_\n" foreach @{ $lines };
1167 sub script_list_biopieces
1169 # Martin A. Hansen, January 2008.
1171 # Prints the synopsis from the usage for each of the biopieces.
1173 my ( $in, # handle to in stream
1174 $out, # handle to out stream
1175 $options, # options hash
1180 my ( @files, $file, $wiki, $program, $synopsis );
1182 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1184 foreach $file ( sort @files )
1186 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1190 $wiki = Maasha::Gwiki::gwiki_read( $file );
1192 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1193 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1195 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1196 $synopsis =~ s/!(\w)/$1/g;
1198 printf( "%-30s%s\n", $program, $synopsis );
1206 sub script_list_genomes
1208 # Martin A. Hansen, January 2008.
1210 # Prints the synopsis from the usage for each of the biopieces.
1212 my ( $in, # handle to in stream
1213 $out, # handle to out stream
1214 $options, # options hash
1219 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1221 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1223 foreach $genome ( @genomes )
1225 next if $genome =~ /\.$/;
1227 @formats = Maasha::Common::ls_dirs( $genome );
1229 foreach $format ( @formats )
1231 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1233 $hash{ $1 }{ $2 } = 1;
1242 map { push @row, $_ } sort keys %found;
1244 print join( "\t", @row ), "\n";
1246 foreach $genome ( sort keys %hash )
1250 foreach $format ( sort keys %found )
1252 if ( exists $hash{ $genome }{ $format } ) {
1259 print join( "\t", @row ), "\n";
1264 sub script_read_fasta
1266 # Martin A. Hansen, August 2007.
1268 # Read sequences from FASTA file.
1270 my ( $in, # handle to in stream
1271 $out, # handle to out stream
1272 $options, # options hash
1277 my ( $record, $file, $data_in, $entry, $num );
1279 while ( $record = get_record( $in ) ) {
1280 put_record( $record, $out );
1285 foreach $file ( @{ $options->{ "files" } } )
1287 $data_in = Maasha::Common::read_open( $file );
1289 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1291 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1294 SEQ_NAME => $entry->[ SEQ_NAME ],
1295 SEQ => $entry->[ SEQ ],
1296 SEQ_LEN => length $entry->[ SEQ ],
1299 put_record( $record, $out );
1302 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1312 close $data_in if $data_in;
1318 # Martin A. Hansen, August 2007.
1320 # Read table or table columns from stream or file.
1322 my ( $in, # handle to in stream
1323 $out, # handle to out stream
1324 $options, # options hash
1329 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1331 $options->{ 'delimit' } ||= '\s+';
1333 while ( $record = get_record( $in ) ) {
1334 put_record( $record, $out );
1337 $skip = $options->{ 'skip' } ||= 0;
1340 foreach $file ( @{ $options->{ "files" } } )
1342 $data_in = Maasha::Common::read_open( $file );
1344 while ( $line = <$data_in> )
1352 next if $line =~ /^#|^$/;
1359 @fields = split /$options->{'delimit'}/, $line;
1361 if ( $options->{ "cols" } ) {
1362 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1367 for ( $i = 0; $i < @fields2; $i++ )
1369 if ( $options->{ "keys" }->[ $i ] ) {
1370 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1372 $record->{ "V" . $i } = $fields2[ $i ];
1376 put_record( $record, $out );
1378 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1388 close $data_in if $data_in;
1394 # Martin A. Hansen, August 2007.
1396 # Read psl table from stream or file.
1398 my ( $in, # handle to in stream
1399 $out, # handle to out stream
1400 $options, # options hash
1405 my ( $record, $file, $data_in, $num );
1407 while ( $record = get_record( $in ) ) {
1408 put_record( $record, $out );
1413 foreach $file ( @{ $options->{ "files" } } )
1415 $data_in = Maasha::Common::read_open( $file );
1417 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1419 put_record( $record, $out );
1421 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1433 # Martin A. Hansen, August 2007.
1435 # Read bed table from stream or file.
1437 my ( $in, # handle to in stream
1438 $out, # handle to out stream
1439 $options, # options hash
1444 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1446 $cols = $options->{ 'cols' }->[ 0 ];
1448 while ( $record = get_record( $in ) ) {
1449 put_record( $record, $out );
1454 foreach $file ( @{ $options->{ "files" } } )
1456 $data_in = Maasha::Common::read_open( $file );
1458 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols ) )
1460 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1462 put_record( $record, $out );
1464 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1474 close $data_in if $data_in;
1478 sub script_read_fixedstep
1480 # Martin A. Hansen, Juli 2008.
1482 # Read fixedstep wiggle format from stream or file.
1484 my ( $in, # handle to in stream
1485 $out, # handle to out stream
1486 $options, # options hash
1491 my ( $file, $record, $entry, $data_in, $num );
1493 while ( $record = get_record( $in ) ) {
1494 put_record( $record, $out );
1499 foreach $file ( @{ $options->{ "files" } } )
1501 $data_in = Maasha::Common::read_open( $file );
1503 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1505 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1507 put_record( $record, $out );
1509 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1519 close $data_in if $data_in;
1523 sub script_read_blast_tab
1525 # Martin A. Hansen, September 2007.
1527 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1529 my ( $in, # handle to in stream
1530 $out, # handle to out stream
1531 $options, # options hash
1536 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1538 while ( $record = get_record( $in ) ) {
1539 put_record( $record, $out );
1544 foreach $file ( @{ $options->{ "files" } } )
1546 $data_in = Maasha::Common::read_open( $file );
1548 while ( $line = <$data_in> )
1552 next if $line =~ /^#/;
1554 @fields = split /\t/, $line;
1556 $record->{ "REC_TYPE" } = "BLAST";
1557 $record->{ "Q_ID" } = $fields[ 0 ];
1558 $record->{ "S_ID" } = $fields[ 1 ];
1559 $record->{ "IDENT" } = $fields[ 2 ];
1560 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1561 $record->{ "MISMATCHES" } = $fields[ 4 ];
1562 $record->{ "GAPS" } = $fields[ 5 ];
1563 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1564 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1565 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1566 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1567 $record->{ "E_VAL" } = $fields[ 10 ];
1568 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1570 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1572 $record->{ "STRAND" } = '-';
1574 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1578 $record->{ "STRAND" } = '+';
1581 put_record( $record, $out );
1583 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1593 close $data_in if $data_in;
1597 sub script_read_embl
1599 # Martin A. Hansen, August 2007.
1603 my ( $in, # handle to in stream
1604 $out, # handle to out stream
1605 $options, # options hash
1610 my ( %options2, $file, $data_in, $num, $entry, $record );
1612 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1613 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1614 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1616 while ( $record = get_record( $in ) ) {
1617 put_record( $record, $out );
1622 foreach $file ( @{ $options->{ "files" } } )
1624 $data_in = Maasha::Common::read_open( $file );
1626 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1628 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1630 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1632 $record_copy = dclone $record;
1634 delete $record_copy->{ "FT" };
1636 put_record( $record_copy, $out );
1638 delete $record_copy->{ "SEQ" };
1640 foreach $feat ( keys %{ $record->{ "FT" } } )
1642 $record_copy->{ "FEAT_TYPE" } = $feat;
1644 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1646 foreach $qual ( keys %{ $feat2 } )
1648 $qual_val = join "; ", @{ $feat2->{ $qual } };
1653 $record_copy->{ $qual } = $qual_val;
1656 put_record( $record_copy, $out );
1660 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1670 close $data_in if $data_in;
1674 sub script_read_stockholm
1676 # Martin A. Hansen, August 2007.
1678 # Read Stockholm format.
1680 my ( $in, # handle to in stream
1681 $out, # handle to out stream
1682 $options, # options hash
1687 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1689 while ( $record = get_record( $in ) ) {
1690 put_record( $record, $out );
1695 foreach $file ( @{ $options->{ "files" } } )
1697 $data_in = Maasha::Common::read_open( $file );
1699 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1701 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1705 foreach $key ( keys %{ $record->{ "GF" } } ) {
1706 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1709 $record_anno->{ "ALIGN" } = $num;
1711 put_record( $record_anno, $out );
1713 foreach $seq ( @{ $record->{ "ALIGN" } } )
1715 undef $record_align;
1718 SEQ_NAME => $seq->[ 0 ],
1722 put_record( $record_align, $out );
1725 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1735 close $data_in if $data_in;
1739 sub script_read_phastcons
1741 # Martin A. Hansen, December 2007.
1743 # Read PhastCons format.
1745 my ( $in, # handle to in stream
1746 $out, # handle to out stream
1747 $options, # options hash
1752 my ( $data_in, $file, $num, $entry, @records, $record );
1754 $options->{ "min" } ||= 10;
1755 $options->{ "dist" } ||= 25;
1756 $options->{ "threshold" } ||= 0.8;
1757 $options->{ "gap" } ||= 5;
1759 while ( $record = get_record( $in ) ) {
1760 put_record( $record, $out );
1765 foreach $file ( @{ $options->{ "files" } } )
1767 $data_in = Maasha::Common::read_open( $file );
1769 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1771 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1773 foreach $record ( @records )
1775 $record->{ "REC_TYPE" } = "BED";
1776 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1778 put_record( $record, $out );
1780 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1791 close $data_in if $data_in;
1795 sub script_read_soft
1797 # Martin A. Hansen, December 2007.
1800 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1802 my ( $in, # handle to in stream
1803 $out, # handle to out stream
1804 $options, # options hash
1809 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1811 while ( $record = get_record( $in ) ) {
1812 put_record( $record, $out );
1817 foreach $file ( @{ $options->{ "files" } } )
1819 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1821 $soft_index = Maasha::NCBI::soft_index_file( $file );
1823 $fh = Maasha::Common::read_open( $file );
1825 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1827 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1829 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1831 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1833 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1835 foreach $sample ( @samples )
1838 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1840 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1842 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1844 foreach $record ( @{ $records } )
1846 put_record( $record, $out );
1848 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1853 $old_end = $sample->{ "LINE_END" };
1861 close $data_in if $data_in;
1868 # Martin A. Hansen, February 2008.
1871 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1873 my ( $in, # handle to in stream
1874 $out, # handle to out stream
1875 $options, # options hash
1880 my ( $data_in, $file, $fh, $num, $record, $entry );
1882 while ( $record = get_record( $in ) ) {
1883 put_record( $record, $out );
1888 foreach $file ( @{ $options->{ "files" } } )
1890 $fh = Maasha::Common::read_open( $file );
1892 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1894 put_record( $entry, $out );
1896 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1906 close $data_in if $data_in;
1910 sub script_read_2bit
1912 # Martin A. Hansen, March 2008.
1914 # Read sequences from 2bit file.
1916 my ( $in, # handle to in stream
1917 $out, # handle to out stream
1918 $options, # options hash
1923 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1925 $mask = 1 if not $options->{ "no_mask" };
1927 while ( $record = get_record( $in ) ) {
1928 put_record( $record, $out );
1933 foreach $file ( @{ $options->{ "files" } } )
1935 $data_in = Maasha::Common::read_open( $file );
1937 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1939 foreach $line ( @{ $toc } )
1941 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1942 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1943 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1945 put_record( $record, $out );
1947 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1957 close $data_in if $data_in;
1961 sub script_read_solexa
1963 # Martin A. Hansen, March 2008.
1965 # Read Solexa sequence reads from file.
1967 my ( $in, # handle to in stream
1968 $out, # handle to out stream
1969 $options, # options hash
1974 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1976 $options->{ "format" } ||= "octal";
1977 $options->{ "quality" } ||= 20;
1979 while ( $record = get_record( $in ) ) {
1980 put_record( $record, $out );
1985 foreach $file ( @{ $options->{ "files" } } )
1987 $data_in = Maasha::Common::read_open( $file );
1989 if ( $options->{ "format" } eq "octal" )
1991 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1993 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1995 put_record( $record, $out );
1997 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2004 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
2006 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
2008 put_record( $record, $out );
2010 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2021 close $data_in if $data_in;
2025 sub script_read_solid
2027 # Martin A. Hansen, April 2008.
2029 # Read Solid sequence from file.
2031 my ( $in, # handle to in stream
2032 $out, # handle to out stream
2033 $options, # options hash
2038 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
2040 $options->{ "quality" } ||= 15;
2042 while ( $record = get_record( $in ) ) {
2043 put_record( $record, $out );
2048 foreach $file ( @{ $options->{ "files" } } )
2050 $data_in = Maasha::Common::read_open( $file );
2052 while ( $line = <$data_in> )
2056 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2058 @scores = split /,/, $seq_qual;
2059 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2061 for ( $i = 0; $i < @seqs; $i++ ) {
2062 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2066 REC_TYPE => 'SOLID',
2067 SEQ_NAME => $seq_name,
2069 SEQ_QUAL => join( ";", @scores ),
2070 SEQ_LEN => length $seq_cs,
2071 SEQ => join( "", @seqs ),
2072 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2075 put_record( $record, $out );
2077 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2087 close $data_in if $data_in;
2091 sub script_read_mysql
2093 # Martin A. Hansen, May 2008.
2095 # Read a MySQL query into stream.
2097 my ( $in, # handle to in stream
2098 $out, # handle to out stream
2099 $options, # options hash
2104 my ( $record, $dbh, $results );
2106 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2107 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2109 while ( $record = get_record( $in ) ) {
2110 put_record( $record, $out );
2113 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2115 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2117 Maasha::SQL::disconnect( $dbh );
2119 map { put_record( $_ ) } @{ $results };
2123 sub script_read_ucsc_config
2125 # Martin A. Hansen, November 2008.
2127 # Read track entries from UCSC Genome Browser '.ra' files.
2129 my ( $in, # handle to in stream
2130 $out, # handle to out stream
2131 $options, # options hash
2136 my ( $record, $file, $data_in, $entry, $num );
2138 while ( $record = get_record( $in ) ) {
2139 put_record( $record, $out );
2144 foreach $file ( @{ $options->{ "files" } } )
2146 $data_in = Maasha::Common::read_open( $file );
2148 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
2150 $record->{ 'REC_TYPE' } = "UCSC Config";
2152 put_record( $record, $out );
2154 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2164 close $data_in if $data_in;
2168 sub script_assemble_tag_contigs
2170 # Martin A. Hansen, November 2008.
2172 # Assemble tags from the stream into
2175 # The current implementation is quite
2176 # slow because of heavy use of temporary
2179 my ( $in, # handle to in stream
2180 $out, # handle to out stream
2181 $options, # options hash
2186 my ( $bed_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
2188 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
2189 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2190 $cols = 6; # we only need the first 5 BED columns
2192 while ( $record = get_record( $in ) )
2194 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
2196 $strand = $record->{ 'STRAND' } || '+';
2198 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols );
2204 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
2208 foreach $chr ( sort keys %{ $file_hash } )
2210 $bed_file = Maasha::UCSC::BED::tag_contigs_assemble( $file_hash->{ $chr }, $chr, $strand, $BP_TMP );
2212 $fh_in = Maasha::Filesys::file_read_open( $bed_file );
2214 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in ) )
2216 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
2217 put_record( $record, $out );
2223 unlink $file_hash->{ $chr };
2229 sub script_format_genome
2231 # Martin A. Hansen, Juli 2008.
2233 # Format a genome to speficed formats.
2235 my ( $in, # handle to in stream
2236 $out, # handle to out stream
2237 $options, # options hash
2242 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2244 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2245 $genome = $options->{ 'genome' };
2247 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2248 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2249 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2251 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2253 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2255 $fasta_dir = "$dir/genomes/$genome/fasta";
2259 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2261 $fasta_dir = "$dir/genomes/$genome/fasta";
2263 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2266 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2268 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2270 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2272 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2275 while ( $record = get_record( $in ) )
2277 if ( $fh_out and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2279 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2281 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2283 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2285 $vals = $record->{ 'VALS' };
2289 print $fh_out "$vals\n";
2292 put_record( $record, $out ) if not $options->{ "no_stream" };
2295 foreach $format ( @{ $options->{ 'formats' } } )
2297 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2298 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2299 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2300 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2301 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2304 close $fh_out if $fh_out;
2308 sub script_length_seq
2310 # Martin A. Hansen, August 2007.
2312 # Determine the length of sequences in stream.
2314 my ( $in, # handle to in stream
2315 $out, # handle to out stream
2316 $options, # options hash
2321 my ( $record, $total );
2323 while ( $record = get_record( $in ) )
2325 if ( $record->{ "SEQ" } )
2327 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2328 $total += $record->{ "SEQ_LEN" };
2331 put_record( $record, $out ) if not $options->{ "no_stream" };
2334 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2338 sub script_uppercase_seq
2340 # Martin A. Hansen, August 2007.
2342 # Uppercases sequences in stream.
2344 my ( $in, # handle to in stream
2345 $out, # handle to out stream
2352 while ( $record = get_record( $in ) )
2354 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2356 put_record( $record, $out );
2361 sub script_shuffle_seq
2363 # Martin A. Hansen, December 2007.
2365 # Shuffle sequences in stream.
2367 my ( $in, # handle to in stream
2368 $out, # handle to out stream
2375 while ( $record = get_record( $in ) )
2377 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2379 put_record( $record, $out );
2384 sub script_analyze_seq
2386 # Martin A. Hansen, August 2007.
2388 # Analyze sequence composition of sequences in stream.
2390 my ( $in, # handle to in stream
2391 $out, # handle to out stream
2396 my ( $record, $analysis );
2398 while ( $record = get_record( $in ) )
2400 if ( $record->{ "SEQ" } )
2402 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2404 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2407 put_record( $record, $out );
2412 sub script_analyze_tags
2414 # Martin A. Hansen, August 2008.
2416 # Analyze sequence tags in stream.
2418 my ( $in, # handle to in stream
2419 $out, # handle to out stream
2424 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2426 while ( $record = get_record( $in ) )
2428 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2430 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2434 $len_hash{ length( $record->{ "SEQ" } ) }++;
2435 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2438 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2440 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2444 $len_hash{ $record->{ "BED_LEN" } }++;
2445 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2450 foreach $key ( sort { $a <=> $b } keys %len_hash )
2452 $tag_record->{ "TAG_LEN" } = $key;
2453 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2454 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2456 put_record( $tag_record, $out );
2461 sub script_complexity_seq
2463 # Martin A. Hansen, May 2008.
2465 # Generates an index calculated as the most common di-residue over
2466 # the sequence length for all sequences in stream.
2468 my ( $in, # handle to in stream
2469 $out, # handle to out stream
2474 my ( $record, $index );
2476 while ( $record = get_record( $in ) )
2478 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2480 put_record( $record, $out );
2485 sub script_oligo_freq
2487 # Martin A. Hansen, August 2007.
2489 # Determine the length of sequences in stream.
2491 my ( $in, # handle to in stream
2492 $out, # handle to out stream
2493 $options, # options hash
2498 my ( $record, %oligos, @freq_table );
2500 $options->{ "word_size" } ||= 7;
2502 while ( $record = get_record( $in ) )
2504 if ( $record->{ "SEQ" } )
2506 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2508 if ( not $options->{ "all" } )
2510 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2512 map { put_record( $_, $out ) } @freq_table;
2518 put_record( $record, $out );
2521 if ( $options->{ "all" } )
2523 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2525 map { put_record( $_, $out ) } @freq_table;
2530 sub script_create_weight_matrix
2532 # Martin A. Hansen, August 2007.
2534 # Creates a weight matrix from an alignmnet.
2536 my ( $in, # handle to in stream
2537 $out, # handle to out stream
2538 $options, # options hash
2543 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2547 while ( $record = get_record( $in ) )
2549 if ( $record->{ "SEQ" } )
2551 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2553 $res = substr $record->{ "SEQ" }, $i, 1;
2555 $freq_hash{ $i }{ $res }++;
2556 $res_hash{ $res } = 1;
2563 put_record( $record, $out );
2567 foreach $res ( sort keys %res_hash )
2571 $record->{ "V0" } = $res;
2573 for ( $i = 0; $i < keys %freq_hash; $i++ )
2575 $freq = $freq_hash{ $i }{ $res } || 0;
2577 if ( $options->{ "percent" } ) {
2578 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2581 $record->{ "V" . ( $i + 1 ) } = $freq;
2584 put_record( $record, $out );
2589 sub script_calc_bit_scores
2591 # Martin A. Hansen, March 2007.
2593 # Calculates the bit scores for each position from an alignmnet in the stream.
2595 my ( $in, # handle to in stream
2596 $out, # handle to out stream
2601 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2605 while ( $record = get_record( $in ) )
2607 if ( $record->{ "SEQ" } )
2609 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2611 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2613 $res = substr $record->{ "SEQ" }, $i, 1;
2615 next if $res =~ /-|_|~|\./;
2617 $freq_hash{ $i }{ $res }++;
2624 put_record( $record, $out );
2630 if ( $type eq "protein" ) {
2636 for ( $i = 0; $i < keys %freq_hash; $i++ )
2638 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2640 $bit_diff = $bit_max - $bit_height;
2642 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2645 put_record( $record, $out );
2649 sub script_calc_fixedstep
2651 # Martin A. Hansen, September 2008.
2653 # Calculates fixedstep entries from data in the stream.
2655 my ( $in, # handle to in stream
2656 $out, # handle to out stream
2657 $options, # options hash
2662 my ( $bed_file, $fh_in, $fh_out, $cols, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2664 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2665 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2666 $cols = 5; # we only need the first 5 BED columns
2668 while ( $record = get_record( $in ) )
2670 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
2671 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols );
2677 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file );
2681 foreach $chr ( sort keys %{ $file_hash } )
2683 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $file_hash->{ $chr }, $chr, $options->{ 'score' } );
2685 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2687 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2689 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2690 put_record( $record, $out );
2696 unlink $file_hash->{ $chr };
2702 sub script_reverse_seq
2704 # Martin A. Hansen, August 2007.
2706 # Reverse sequence in record.
2708 my ( $in, # handle to in stream
2709 $out, # handle to out stream
2716 while ( $record = get_record( $in ) )
2718 if ( $record->{ "SEQ" } ) {
2719 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2722 put_record( $record, $out );
2727 sub script_complement_seq
2729 # Martin A. Hansen, August 2007.
2731 # Complement sequence in record.
2733 my ( $in, # handle to in stream
2734 $out, # handle to out stream
2739 my ( $record, $type );
2741 while ( $record = get_record( $in ) )
2743 if ( $record->{ "SEQ" } )
2746 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2749 if ( $type eq "rna" ) {
2750 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2751 } elsif ( $type eq "dna" ) {
2752 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2756 put_record( $record, $out );
2761 sub script_remove_indels
2763 # Martin A. Hansen, August 2007.
2765 # Remove indels from sequences in stream.
2767 my ( $in, # handle to in stream
2768 $out, # handle to out stream
2775 while ( $record = get_record( $in ) )
2777 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2779 put_record( $record, $out );
2784 sub script_transliterate_seq
2786 # Martin A. Hansen, August 2007.
2788 # Transliterate chars from sequence in record.
2790 my ( $in, # handle to in stream
2791 $out, # handle to out stream
2792 $options, # options hash
2797 my ( $record, $search, $replace, $delete );
2799 $search = $options->{ "search" } || "";
2800 $replace = $options->{ "replace" } || "";
2801 $delete = $options->{ "delete" } || "";
2803 while ( $record = get_record( $in ) )
2805 if ( $record->{ "SEQ" } )
2807 if ( $search and $replace ) {
2808 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2809 } elsif ( $delete ) {
2810 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2814 put_record( $record, $out );
2819 sub script_transliterate_vals
2821 # Martin A. Hansen, April 2008.
2823 # Transliterate chars from values in record.
2825 my ( $in, # handle to in stream
2826 $out, # handle to out stream
2827 $options, # options hash
2832 my ( $record, $search, $replace, $delete, $key );
2834 $search = $options->{ "search" } || "";
2835 $replace = $options->{ "replace" } || "";
2836 $delete = $options->{ "delete" } || "";
2838 while ( $record = get_record( $in ) )
2840 foreach $key ( @{ $options->{ "keys" } } )
2842 if ( exists $record->{ $key } )
2844 if ( $search and $replace ) {
2845 eval "\$record->{ $key } =~ tr/$search/$replace/";
2846 } elsif ( $delete ) {
2847 eval "\$record->{ $key } =~ tr/$delete//d";
2852 put_record( $record, $out );
2857 sub script_translate_seq
2859 # Martin A. Hansen, February 2008.
2861 # Translate DNA sequence into protein sequence.
2863 my ( $in, # handle to in stream
2864 $out, # handle to out stream
2865 $options, # options hash
2870 my ( $record, $frame, %new_record );
2872 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2874 while ( $record = get_record( $in ) )
2876 if ( $record->{ "SEQ" } )
2878 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2880 foreach $frame ( @{ $options->{ "frames" } } )
2882 %new_record = %{ $record };
2884 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2885 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2886 $new_record{ "FRAME" } = $frame;
2888 put_record( \%new_record, $out );
2894 put_record( $record, $out );
2900 sub script_extract_seq
2902 # Martin A. Hansen, August 2007.
2904 # Extract subsequences from sequences in record.
2906 my ( $in, # handle to in stream
2907 $out, # handle to out stream
2908 $options, # options hash
2913 my ( $beg, $end, $len, $record );
2915 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2918 $beg = $options->{ "beg" } - 1; # correcting for start offset
2921 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2923 } elsif ( defined $options->{ "end" } ) {
2924 $end = $options->{ "end" } - 1; # correcting for start offset
2927 $len = $options->{ "len" };
2929 # print "beg->$beg, end->$end, len->$len\n";
2931 while ( $record = get_record( $in ) )
2933 if ( $record->{ "SEQ" } )
2935 if ( defined $beg and defined $end )
2937 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2938 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2940 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2943 elsif ( defined $beg and defined $len )
2945 if ( $len > length $record->{ "SEQ" } ) {
2946 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2948 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2951 elsif ( defined $beg )
2953 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2957 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2959 put_record( $record, $out );
2964 sub script_get_genome_seq
2966 # Martin A. Hansen, December 2007.
2968 # Gets a subsequence from a genome.
2970 my ( $in, # handle to in stream
2971 $out, # handle to out stream
2972 $options, # options hash
2977 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2979 $options->{ "flank" } ||= 0;
2981 if ( $options->{ "genome" } )
2983 $genome = $options->{ "genome" };
2985 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2986 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2988 $fh = Maasha::Common::read_open( $genome_file );
2989 $index = Maasha::Fasta::index_retrieve( $index_file );
2991 shift @{ $index }; # Get rid of the file size info
2993 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2995 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2997 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2999 $beg = $index_beg + $options->{ "beg" } - 1;
3001 if ( $options->{ "len" } ) {
3002 $len = $options->{ "len" };
3003 } elsif ( $options->{ "end" } ) {
3004 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
3007 $beg -= $options->{ "flank" };
3008 $len += 2 * $options->{ "flank" };
3010 if ( $beg <= $index_beg )
3012 $len -= $index_beg - $beg;
3016 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3018 next if $beg > $index_beg + $index_len;
3020 $record->{ "CHR" } = $options->{ "chr" };
3021 $record->{ "CHR_BEG" } = $beg - $index_beg;
3022 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3024 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3025 $record->{ "SEQ_LEN" } = $len;
3027 put_record( $record, $out );
3031 while ( $record = get_record( $in ) )
3033 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
3035 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
3037 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
3039 $beg = $record->{ "CHR_BEG" } + $index_beg;
3040 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3042 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
3044 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3046 $beg = $record->{ "S_BEG" } + $index_beg;
3047 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3049 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
3051 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
3053 $beg = $record->{ "S_BEG" } + $index_beg;
3054 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
3057 $beg -= $options->{ "flank" };
3058 $len += 2 * $options->{ "flank" };
3060 if ( $beg <= $index_beg )
3062 $len -= $index_beg - $beg;
3066 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
3068 next if $beg > $index_beg + $index_len;
3070 $record->{ "CHR_BEG" } = $beg - $index_beg;
3071 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
3073 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
3075 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
3077 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
3078 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
3081 if ( $options->{ "mask" } )
3083 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
3085 $record->{ "SEQ" } = lc $record->{ "SEQ" };
3087 @begs = split ",", $record->{ "Q_BEGS" };
3088 @lens = split ",", $record->{ "BLOCKSIZES" };
3090 for ( $i = 0; $i < @begs; $i++ ) {
3091 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3097 put_record( $record, $out );
3104 sub script_get_genome_align
3106 # Martin A. Hansen, April 2008.
3108 # Gets a subalignment from a multiple genome alignment.
3110 my ( $in, # handle to in stream
3111 $out, # handle to out stream
3112 $options, # options hash
3117 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3119 $options->{ "strand" } ||= "+";
3123 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3125 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3127 $beg = $options->{ "beg" } - 1;
3129 if ( $options->{ "end" } ) {
3130 $end = $options->{ "end" };
3131 } elsif ( $options->{ "len" } ) {
3132 $end = $beg + $options->{ "len" };
3135 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3137 foreach $entry ( @{ $align } )
3139 $entry->{ "CHR" } = $record->{ "CHR" };
3140 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3141 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3142 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3143 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3144 $entry->{ "SCORE" } = $record->{ "SCORE" };
3146 put_record( $entry, $out );
3150 while ( $record = get_record( $in ) )
3152 if ( $record->{ "REC_TYPE" } eq "BED" )
3154 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3156 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3158 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3160 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3162 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3165 foreach $entry ( @{ $align } )
3167 $entry->{ "CHR" } = $record->{ "CHR" };
3168 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3169 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3170 $entry->{ "STRAND" } = $record->{ "STRAND" };
3171 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3172 $entry->{ "SCORE" } = $record->{ "SCORE" };
3174 put_record( $entry, $out );
3182 sub script_get_genome_phastcons
3184 # Martin A. Hansen, February 2008.
3186 # Get phastcons scores from genome intervals.
3188 my ( $in, # handle to in stream
3189 $out, # handle to out stream
3190 $options, # options hash
3195 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3197 $options->{ "flank" } ||= 0;
3199 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3200 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3202 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3203 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3205 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3207 $options->{ "beg" } -= 1; # request is 1-based
3208 $options->{ "end" } -= 1; # request is 1-based
3210 if ( $options->{ "len" } ) {
3211 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3214 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3216 $record->{ "CHR" } = $options->{ "chr" };
3217 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3218 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3220 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3221 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3223 put_record( $record, $out );
3226 while ( $record = get_record( $in ) )
3228 if ( $record->{ "REC_TYPE" } eq "BED" )
3230 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3232 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3234 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3236 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3238 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3241 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3242 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3244 put_record( $record, $out );
3247 close $fh_phastcons if $fh_phastcons;
3253 # Martin A. Hansen, December 2007.
3255 # Folds sequences in stream into secondary structures.
3257 my ( $in, # handle to in stream
3258 $out, # handle to out stream
3263 my ( $record, $type, $struct, $index );
3265 while ( $record = get_record( $in ) )
3267 if ( $record->{ "SEQ" } )
3270 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3273 if ( $type ne "protein" )
3275 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3276 $record->{ "SEC_STRUCT" } = $struct;
3277 $record->{ "FREE_ENERGY" } = $index;
3278 $record->{ "SCORE" } = abs int $index;
3279 $record->{ "SIZE" } = length $struct;
3280 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3284 put_record( $record, $out );
3289 sub script_split_seq
3291 # Martin A. Hansen, August 2007.
3293 # Split a sequence in stream into words.
3295 my ( $in, # handle to in stream
3296 $out, # handle to out stream
3297 $options, # options hash
3302 my ( $record, $new_record, $i, $subseq, %lookup );
3304 $options->{ "word_size" } ||= 7;
3306 while ( $record = get_record( $in ) )
3308 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3310 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3312 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3314 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3316 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3317 $new_record->{ "SEQ" } = $subseq;
3319 put_record( $new_record, $out );
3321 $lookup{ $subseq } = 1;
3325 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3326 $new_record->{ "SEQ" } = $subseq;
3328 put_record( $new_record, $out );
3334 put_record( $record, $out );
3340 sub script_split_bed
3342 # Martin A. Hansen, June 2008.
3344 # Split a BED record into overlapping windows.
3346 my ( $in, # handle to in stream
3347 $out, # handle to out stream
3348 $options, # options hash
3353 my ( $record, $new_record, $i );
3355 $options->{ "window_size" } ||= 20;
3356 $options->{ "step_size" } ||= 1;
3358 while ( $record = get_record( $in ) )
3360 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3362 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3364 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3366 $new_record->{ "REC_TYPE" } = "BED";
3367 $new_record->{ "CHR" } = $record->{ "CHR" };
3368 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3369 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3370 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3371 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3372 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3373 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3375 put_record( $new_record, $out );
3380 put_record( $record, $out );
3386 sub script_align_seq
3388 # Martin A. Hansen, August 2007.
3390 # Align sequences in stream.
3392 my ( $in, # handle to in stream
3393 $out, # handle to out stream
3398 my ( $record, @entries, $entry );
3400 while ( $record = get_record( $in ) )
3402 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3403 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3404 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3405 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3407 put_record( $record, $out );
3411 @entries = Maasha::Align::align( \@entries );
3413 foreach $entry ( @entries )
3415 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3418 SEQ_NAME => $entry->[ SEQ_NAME ],
3419 SEQ => $entry->[ SEQ ],
3422 put_record( $record, $out );
3430 # Martin A. Hansen, February 2008.
3432 # Using the first sequence in stream as reference, tile
3433 # all subsequent sequences based on pairwise alignments.
3435 my ( $in, # handle to in stream
3436 $out, # handle to out stream
3437 $options, # options hash
3442 my ( $record, $first, $ref_entry, @entries );
3446 while ( $record = get_record( $in ) )
3448 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3452 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3458 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3463 put_record( $record, $out );
3467 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3469 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3473 sub script_invert_align
3475 # Martin A. Hansen, February 2008.
3477 # Inverts an alignment showing only non-mathing residues
3478 # using the first sequence as reference.
3480 my ( $in, # handle to in stream
3481 $out, # handle to out stream
3482 $options, # options hash
3487 my ( $record, @entries );
3489 while ( $record = get_record( $in ) )
3491 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3493 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3497 put_record( $record, $out );
3501 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3503 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3507 sub script_patscan_seq
3509 # Martin A. Hansen, August 2007.
3511 # Locates patterns in sequences using scan_for_matches.
3513 my ( $in, # handle to in stream
3514 $out, # handle to out stream
3515 $options, # options hash
3520 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3522 if ( $options->{ "patterns" } ) {
3523 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3524 } elsif ( -f $options->{ "patterns_in" } ) {
3525 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3528 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3530 push @args, "-c" if $options->{ "comp" };
3531 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3532 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3534 $seq_file = "$BP_TMP/patscan.seq";
3535 $pat_file = "$BP_TMP/patscan.pat";
3536 $out_file = "$BP_TMP/patscan.out";
3538 $fh_out = Maasha::Common::write_open( $seq_file );
3542 while ( $record = get_record( $in ) )
3544 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3546 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3548 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3550 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3558 $arg = join " ", @args;
3559 $arg .= " -p" if $type eq "protein";
3561 foreach $pattern ( @{ $patterns } )
3563 $fh_out = Maasha::Common::write_open( $pat_file );
3565 print $fh_out "$pattern\n";
3569 if ( $options->{ 'genome' } ) {
3570 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3571 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3573 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3574 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3577 $fh_in = Maasha::Common::read_open( $out_file );
3579 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3581 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3583 if ( $options->{ 'genome' } )
3585 $result->{ "CHR" } = $result->{ "S_ID" };
3586 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3587 $result->{ "CHR_END" } = $result->{ "S_END" };
3589 delete $result->{ "S_ID" };
3590 delete $result->{ "S_BEG" };
3591 delete $result->{ "S_END" };
3595 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3598 put_record( $result, $out );
3610 sub script_create_blast_db
3612 # Martin A. Hansen, September 2007.
3614 # Creates a NCBI BLAST database with formatdb
3616 my ( $in, # handle to in stream
3617 $out, # handle to out stream
3618 $options, # options hash
3623 my ( $fh, $seq_type, $path, $record, $entry );
3625 $path = $options->{ "database" };
3627 $fh = Maasha::Common::write_open( $path );
3629 while ( $record = get_record( $in ) )
3631 put_record( $record, $out ) if not $options->{ "no_stream" };
3633 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3635 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3637 Maasha::Fasta::put_entry( $entry, $fh );
3643 if ( $seq_type eq "protein" ) {
3644 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3646 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3653 sub script_blast_seq
3655 # Martin A. Hansen, September 2007.
3657 # BLASTs sequences in stream against a given database.
3659 my ( $in, # handle to in stream
3660 $out, # handle to out stream
3661 $options, # options hash
3666 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3668 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3669 $options->{ "filter" } = "F";
3670 $options->{ "filter" } = "T" if $options->{ "filter" };
3671 $options->{ "cpus" } ||= 1;
3673 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3675 $tmp_in = "$BP_TMP/blast_query.seq";
3676 $tmp_out = "$BP_TMP/blast.result";
3678 $fh_out = Maasha::Common::write_open( $tmp_in );
3680 while ( $record = get_record( $in ) )
3682 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3684 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3686 Maasha::Fasta::put_entry( $entry, $fh_out );
3689 put_record( $record, $out );
3694 if ( -f $options->{ 'database' } . ".phr" ) {
3695 $s_type = "protein";
3697 $s_type = "nucleotide";
3700 if ( not $options->{ 'program' } )
3702 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3703 $options->{ 'program' } = "blastn";
3704 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3705 $options->{ 'program' } = "blastp";
3706 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3707 $options->{ 'program' } = "blastx";
3708 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3709 $options->{ 'program' } = "tblastn";
3713 if ( $options->{ 'verbose' } )
3715 Maasha::Common::run(
3718 "-p $options->{ 'program' }",
3719 "-e $options->{ 'e_val' }",
3720 "-a $options->{ 'cpus' }",
3723 "-d $options->{ 'database' }",
3724 "-F $options->{ 'filter' }",
3732 Maasha::Common::run(
3735 "-p $options->{ 'program' }",
3736 "-e $options->{ 'e_val' }",
3737 "-a $options->{ 'cpus' }",
3740 "-d $options->{ 'database' }",
3741 "-F $options->{ 'filter' }",
3751 $fh_out = Maasha::Common::read_open( $tmp_out );
3755 while ( $line = <$fh_out> )
3759 next if $line =~ /^#/;
3761 @fields = split /\s+/, $line;
3763 $record->{ "REC_TYPE" } = "BLAST";
3764 $record->{ "Q_ID" } = $fields[ 0 ];
3765 $record->{ "S_ID" } = $fields[ 1 ];
3766 $record->{ "IDENT" } = $fields[ 2 ];
3767 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3768 $record->{ "MISMATCHES" } = $fields[ 4 ];
3769 $record->{ "GAPS" } = $fields[ 5 ];
3770 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3771 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3772 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3773 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3774 $record->{ "E_VAL" } = $fields[ 10 ];
3775 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3777 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3779 $record->{ "STRAND" } = '-';
3781 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3785 $record->{ "STRAND" } = '+';
3788 put_record( $record, $out );
3799 # Martin A. Hansen, August 2007.
3801 # BLATs sequences in stream against a given genome.
3803 my ( $in, # handle to in stream
3804 $out, # handle to out stream
3805 $options, # options hash
3810 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3812 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3814 $options->{ 'tile_size' } ||= 11;
3815 $options->{ 'one_off' } ||= 0;
3816 $options->{ 'min_identity' } ||= 90;
3817 $options->{ 'min_score' } ||= 0;
3818 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3820 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3821 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3822 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3823 $blat_args .= " -minScore=$options->{ 'min_score' }";
3824 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3825 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3827 $query_file = "$BP_TMP/blat.seq";
3829 $fh_out = Maasha::Common::write_open( $query_file );
3831 while ( $record = get_record( $in ) )
3833 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3835 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3836 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3839 put_record( $record, $out );
3844 $blat_args .= " -t=dnax" if $type eq "protein";
3845 $blat_args .= " -q=$type";
3847 $result_file = "$BP_TMP/blat.psl";
3849 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3853 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3855 map { put_record( $_, $out ) } @{ $entries };
3857 unlink $result_file;
3863 # Martin A. Hansen, July 2008.
3865 # soap sequences in stream against a given file or genome.
3867 my ( $in, # handle to in stream
3868 $out, # handle to out stream
3869 $options, # options hash
3874 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3876 $options->{ "seed_size" } ||= 10;
3877 $options->{ "mismatches" } ||= 2;
3878 $options->{ "gap_size" } ||= 0;
3879 $options->{ "cpus" } ||= 1;
3881 if ( $options->{ "genome" } ) {
3882 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3885 $tmp_in = "$BP_TMP/soap_query.seq";
3886 $tmp_out = "$BP_TMP/soap.result";
3888 $fh_out = Maasha::Common::write_open( $tmp_in );
3892 while ( $record = get_record( $in ) )
3894 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3896 Maasha::Fasta::put_entry( $entry, $fh_out );
3901 put_record( $record, $out );
3909 "-s $options->{ 'seed_size' }",
3912 "-v $options->{ 'mismatches' }",
3913 "-g $options->{ 'gap_size' }",
3914 "-p $options->{ 'cpus' }",
3915 "-d $options->{ 'in_file' }",
3919 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3921 Maasha::Common::run( "soap", $args, 1 );
3925 $fh_out = Maasha::Common::read_open( $tmp_out );
3929 while ( $line = <$fh_out> )
3933 @fields = split /\t/, $line;
3935 $record->{ "REC_TYPE" } = "SOAP";
3936 $record->{ "Q_ID" } = $fields[ 0 ];
3937 $record->{ "SCORE" } = $fields[ 3 ];
3938 $record->{ "STRAND" } = $fields[ 6 ];
3939 $record->{ "S_ID" } = $fields[ 7 ];
3940 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3941 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3943 put_record( $record, $out );
3953 sub script_match_seq
3955 # Martin A. Hansen, August 2007.
3957 # BLATs sequences in stream against a given genome.
3959 my ( $in, # handle to in stream
3960 $out, # handle to out stream
3961 $options, # options hash
3966 my ( $record, @entries, $results );
3968 $options->{ "word_size" } ||= 20;
3969 $options->{ "direction" } ||= "both";
3971 while ( $record = get_record( $in ) )
3973 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3974 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3977 put_record( $record, $out );
3980 if ( @entries == 1 )
3982 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3984 map { put_record( $_, $out ) } @{ $results };
3986 elsif ( @entries == 2 )
3988 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3990 map { put_record( $_, $out ) } @{ $results };
3995 sub script_create_vmatch_index
3997 # Martin A. Hansen, January 2008.
3999 # Create a vmatch index from sequences in the stream.
4001 my ( $in, # handle to in stream
4002 $out, # handle to out stream
4003 $options, # options hash
4008 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
4010 if ( $options->{ "index_name" } )
4012 $file_tmp = $options->{ 'index_name' };
4013 $fh_tmp = Maasha::Common::write_open( $file_tmp );
4016 while ( $record = get_record( $in ) )
4018 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4020 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4022 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
4025 put_record( $record, $out ) if not $options->{ "no_stream" };
4028 if ( $options->{ "index_name" } )
4032 if ( $type eq "protein" ) {
4033 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4035 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
4043 sub script_vmatch_seq
4045 # Martin A. Hansen, August 2007.
4047 # Vmatches sequences in stream against a given genome.
4049 my ( $in, # handle to in stream
4050 $out, # handle to out stream
4051 $options, # options hash
4056 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
4058 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
4060 if ( $options->{ "index_name" } )
4062 @index_files = $options->{ "index_name" };
4066 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
4068 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
4070 @index_files = sort keys %hash;
4073 while ( $record = get_record( $in ) )
4075 push @records, $record;
4077 put_record( $record, $out );
4080 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
4084 $fh_in = Maasha::Common::read_open( $result_file );
4086 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
4087 put_record( $record, $out );
4092 unlink $result_file;
4096 sub script_write_fasta
4098 # Martin A. Hansen, August 2007.
4100 # Write FASTA entries from sequences in stream.
4102 my ( $in, # handle to in stream
4103 $out, # handle to out stream
4104 $options, # options hash
4109 my ( $record, $fh, $entry );
4111 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4113 while ( $record = get_record( $in ) )
4115 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4116 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4119 put_record( $record, $out ) if not $options->{ "no_stream" };
4126 sub script_write_align
4128 # Martin A. Hansen, August 2007.
4130 # Write pretty alignments aligned sequences in stream.
4132 my ( $in, # handle to in stream
4133 $out, # handle to out stream
4134 $options, # options hash
4139 my ( $fh, $record, @entries );
4141 $fh = write_stream( $options->{ "data_out" } ) ;
4143 while ( $record = get_record( $in ) )
4145 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4146 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4149 put_record( $record, $out ) if not $options->{ "no_stream" };
4152 if ( scalar( @entries ) == 2 ) {
4153 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4154 } elsif ( scalar ( @entries ) > 2 ) {
4155 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4162 sub script_write_blast
4164 # Martin A. Hansen, November 2007.
4166 # Write data in blast table format (-m8 and 9).
4168 my ( $in, # handle to in stream
4169 $out, # handle to out stream
4170 $options, # options hash
4175 my ( $fh, $record, $first );
4177 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4181 while ( $record = get_record( $in ) )
4183 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4185 if ( $options->{ "comment" } and $first )
4187 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";
4192 if ( $record->{ "STRAND" } eq "-" ) {
4193 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4196 print $fh join( "\t",
4197 $record->{ "Q_ID" },
4198 $record->{ "S_ID" },
4199 $record->{ "IDENT" },
4200 $record->{ "ALIGN_LEN" },
4201 $record->{ "MISMATCHES" },
4202 $record->{ "GAPS" },
4203 $record->{ "Q_BEG" } + 1,
4204 $record->{ "Q_END" } + 1,
4205 $record->{ "S_BEG" } + 1,
4206 $record->{ "S_END" } + 1,
4207 $record->{ "E_VAL" },
4208 $record->{ "BIT_SCORE" }
4212 put_record( $record, $out ) if not $options->{ "no_stream" };
4219 sub script_write_tab
4221 # Martin A. Hansen, August 2007.
4223 # Write data as table.
4225 my ( $in, # handle to in stream
4226 $out, # handle to out stream
4227 $options, # options hash
4232 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4234 $options->{ "delimit" } ||= "\t";
4236 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4238 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4240 while ( $record = get_record( $in ) )
4245 if ( $options->{ "keys" } )
4247 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4251 foreach $key ( @{ $options->{ "keys" } } )
4253 if ( exists $record->{ $key } )
4255 push @keys, $key if $options->{ "comment" };
4256 push @vals, $record->{ $key };
4263 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4265 next if exists $no_keys{ $key };
4267 push @keys, $key if $options->{ "comment" };
4268 push @vals, $record->{ $key };
4272 if ( @keys and $options->{ "comment" } )
4274 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4276 delete $options->{ "comment" };
4279 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4281 put_record( $record, $out ) if not $options->{ "no_stream" };
4288 sub script_write_bed
4290 # Martin A. Hansen, August 2007.
4292 # Write BED format for the UCSC genome browser using records in stream.
4294 my ( $in, # handle to in stream
4295 $out, # handle to out stream
4296 $options, # options hash
4301 my ( $cols, $fh, $record, $bed_entry, $new_record );
4303 $cols = $options->{ 'cols' }->[ 0 ];
4305 $fh = write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
4307 while ( $record = get_record( $in ) )
4309 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
4311 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
4312 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols );
4315 put_record( $record, $out ) if not $options->{ 'no_stream' };
4322 sub script_write_psl
4324 # Martin A. Hansen, August 2007.
4326 # Write PSL output from stream.
4328 my ( $in, # handle to in stream
4329 $out, # handle to out stream
4330 $options, # options hash
4335 my ( $fh, $record, @output, $first );
4339 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4341 while ( $record = get_record( $in ) )
4343 put_record( $record, $out ) if not $options->{ "no_stream" };
4345 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4347 Maasha::UCSC::psl_put_header( $fh ) if $first;
4348 Maasha::UCSC::psl_put_entry( $record, $fh );
4357 sub script_write_fixedstep
4359 # Martin A. Hansen, Juli 2008.
4361 # Write fixedStep entries from recrods in the stream.
4363 my ( $in, # handle to in stream
4364 $out, # handle to out stream
4365 $options, # options hash
4370 my ( $fh, $record, $entry );
4372 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4374 while ( $record = get_record( $in ) )
4376 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4377 map { print $fh "$_\n" } @{ $entry };
4380 put_record( $record, $out ) if not $options->{ "no_stream" };
4387 sub script_write_2bit
4389 # Martin A. Hansen, March 2008.
4391 # Write sequence entries from stream in 2bit format.
4393 my ( $in, # handle to in stream
4394 $out, # handle to out stream
4395 $options, # options hash
4400 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4402 $mask = 1 if not $options->{ "no_mask" };
4404 $tmp_file = "$BP_TMP/write_2bit.fna";
4405 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4407 $fh_out = write_stream( $options->{ "data_out" } );
4409 while ( $record = get_record( $in ) )
4411 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
4412 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4415 put_record( $record, $out ) if not $options->{ "no_stream" };
4420 $fh_in = Maasha::Common::read_open( $tmp_file );
4422 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4431 sub script_write_solid
4433 # Martin A. Hansen, April 2008.
4435 # Write di-base encoded Solid sequence from entries in stream.
4437 my ( $in, # handle to in stream
4438 $out, # handle to out stream
4439 $options, # options hash
4444 my ( $record, $fh, $entry );
4446 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4448 while ( $record = get_record( $in ) )
4450 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
4452 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4454 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4457 put_record( $record, $out ) if not $options->{ "no_stream" };
4464 sub script_write_ucsc_config
4466 # Martin A. Hansen, November 2008.
4468 # Write UCSC Genome Broser configuration (.ra file type) from
4469 # records in the stream.
4471 my ( $in, # handle to in stream
4472 $out, # handle to out stream
4473 $options, # options hash
4478 my ( $record, $fh );
4480 $fh = write_stream( $options->{ "data_out" } );
4482 while ( $record = get_record( $in ) )
4484 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
4486 put_record( $record, $out ) if not $options->{ "no_stream" };
4493 sub script_plot_seqlogo
4495 # Martin A. Hansen, August 2007.
4497 # Calculates and writes a sequence logo for alignments.
4499 my ( $in, # handle to in stream
4500 $out, # handle to out stream
4501 $options, # options hash
4506 my ( $record, @entries, $logo, $fh );
4508 while ( $record = get_record( $in ) )
4510 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4511 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4514 put_record( $record, $out ) if not $options->{ "no_stream" };
4517 $logo = Maasha::Plot::seq_logo( \@entries );
4519 $fh = write_stream( $options->{ "data_out" } );
4527 sub script_plot_phastcons_profiles
4529 # Martin A. Hansen, January 2008.
4531 # Plots PhastCons profiles.
4533 my ( $in, # handle to in stream
4534 $out, # handle to out stream
4535 $options, # options hash
4540 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4542 $options->{ "title" } ||= "PhastCons Profiles";
4544 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4545 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4547 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4548 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4550 while ( $record = get_record( $in ) )
4552 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4554 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4555 $record->{ "CHR_BEG" },
4556 $record->{ "CHR_END" },
4557 $options->{ "flank" } );
4559 push @{ $AoA }, [ @{ $scores } ];
4562 put_record( $record, $out ) if not $options->{ "no_stream" };
4565 Maasha::UCSC::phastcons_normalize( $AoA );
4567 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4568 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4570 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4572 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4574 $fh = write_stream( $options->{ "data_out" } );
4576 print $fh "$_\n" foreach @{ $plot };
4582 sub script_analyze_bed
4584 # Martin A. Hansen, March 2008.
4586 # Analyze BED entries in stream.
4588 my ( $in, # handle to in stream
4589 $out, # handle to out stream
4590 $options, # options hash
4597 while ( $record = get_record( $in ) )
4599 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4601 put_record( $record, $out );
4606 sub script_analyze_vals
4608 # Martin A. Hansen, August 2007.
4610 # Analyze values for given keys in stream.
4612 my ( $in, # handle to in stream
4613 $out, # handle to out stream
4614 $options, # options hash
4619 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4621 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4623 while ( $record = get_record( $in ) )
4625 foreach $key ( keys %{ $record } )
4627 next if $options->{ "keys" } and not exists $key_hash{ $key };
4629 $analysis->{ $key }->{ "COUNT" }++;
4631 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4633 $analysis->{ $key }->{ "TYPE" } = "num";
4634 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4635 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4636 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4640 $len = length $record->{ $key };
4642 $analysis->{ $key }->{ "TYPE" } = "alph";
4643 $analysis->{ $key }->{ "SUM" } += $len;
4644 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4645 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4649 put_record( $record, $out ) if not $options->{ "no_stream" };
4652 foreach $key ( keys %{ $analysis } )
4654 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4655 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4658 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4668 if ( $options->{ "keys" } ) {
4669 @keys = @{ $options->{ "keys" } };
4671 @keys = keys %{ $analysis };
4674 foreach $key ( @keys )
4676 $keys .= sprintf "% 15s", $key;
4677 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4678 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4679 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4680 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4681 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4682 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4685 print $out "$keys\n";
4686 print $out "$types\n";
4687 print $out "$counts\n";
4688 print $out "$mins\n";
4689 print $out "$maxs\n";
4690 print $out "$sums\n";
4691 print $out "$means\n";
4695 sub script_head_records
4697 # Martin A. Hansen, August 2007.
4699 # Display the first sequences in stream.
4701 my ( $in, # handle to in stream
4702 $out, # handle to out stream
4703 $options, # options hash
4708 my ( $record, $count );
4710 $options->{ "num" } ||= 10;
4714 while ( $record = get_record( $in ) )
4718 put_record( $record, $out );
4720 last if $count == $options->{ "num" };
4725 sub script_remove_keys
4727 # Martin A. Hansen, August 2007.
4729 # Remove keys from stream.
4731 my ( $in, # handle to in stream
4732 $out, # handle to out stream
4733 $options, # options hash
4738 my ( $record, $new_record );
4740 while ( $record = get_record( $in ) )
4742 if ( $options->{ "keys" } )
4744 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4746 elsif ( $options->{ "save_keys" } )
4748 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4750 $record = $new_record;
4753 put_record( $record, $out ) if keys %{ $record };
4758 sub script_remove_adaptor
4760 # Martin A. Hansen, August 2008.
4762 # Find and remove adaptor from sequences in the stream.
4764 my ( $in, # handle to in stream
4765 $out, # handle to out stream
4766 $options, # options hash
4771 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4773 $options->{ "remove" } ||= "after";
4775 $max_mismatch = $options->{ "mismatches" } || 0;
4776 $offset = $options->{ "offset" };
4778 if ( not defined $offset ) {
4784 $adaptor = uc $options->{ "adaptor" };
4785 $adaptor_len = length $adaptor;
4787 while ( $record = get_record( $in ) )
4789 if ( $record->{ "SEQ" } )
4791 $seq = uc $record->{ "SEQ" };
4792 $seq_len = length $seq;
4794 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4796 $record->{ "ADAPTOR_POS" } = $pos;
4798 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4800 if ( $options->{ "remove" } eq "after" )
4802 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4803 $record->{ "SEQ_LEN" } = $pos;
4807 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4808 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4812 put_record( $record, $out );
4816 put_record( $record, $out );
4822 sub script_remove_mysql_tables
4824 # Martin A. Hansen, November 2008.
4826 # Remove MySQL tables from values in stream.
4828 my ( $in, # handle to in stream
4829 $out, # handle to out stream
4830 $options, # options hash
4835 my ( $record, %table_hash, $dbh, $table );
4837 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
4838 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
4840 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
4842 while ( $record = get_record( $in ) )
4844 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4846 put_record( $record, $out ) if not $options->{ 'no_stream' };
4849 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4851 foreach $table ( sort keys %table_hash )
4853 if ( Maasha::SQL::table_exists( $dbh, $table ) )
4855 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4856 Maasha::SQL::delete_table( $dbh, $table );
4857 print STDERR "done.\n" if $options->{ 'verbose' };
4861 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
4865 Maasha::SQL::disconnect( $dbh );
4869 sub script_remove_ucsc_tracks
4871 # Martin A. Hansen, November 2008.
4873 # Remove track from MySQL tables and config file.
4875 my ( $in, # handle to in stream
4876 $out, # handle to out stream
4877 $options, # options hash
4882 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
4884 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
4885 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
4886 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
4888 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
4890 while ( $record = get_record( $in ) )
4892 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
4894 put_record( $record, $out ) if not $options->{ 'no_stream' };
4897 # ---- locate track in config file ----
4899 $fh_in = Maasha::Common::read_open( $options->{ 'config_file' } );
4901 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
4902 push @tracks, $track;
4907 map { push @new_tracks, $_ if not exists $track_hash{ $_->{ 'track' } } } @tracks;
4909 print STDERR qq(WARNING: track not found in config file: "$options->{ 'config_file' }"\n) if scalar @tracks == scalar @new_tracks;
4911 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
4913 $fh_out = Maasha::Common::write_open( $options->{ 'config_file' } );
4915 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
4919 # ---- locate track in database ----
4921 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
4923 foreach $track ( sort keys %track_hash )
4925 if ( Maasha::SQL::table_exists( $dbh, $track ) )
4927 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
4928 Maasha::SQL::delete_table( $dbh, $track );
4929 print STDERR "done.\n" if $options->{ 'verbose' };
4933 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
4937 Maasha::SQL::disconnect( $dbh );
4939 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
4943 sub script_rename_keys
4945 # Martin A. Hansen, August 2007.
4947 # Rename keys in stream.
4949 my ( $in, # handle to in stream
4950 $out, # handle to out stream
4951 $options, # options hash
4958 while ( $record = get_record( $in ) )
4960 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4962 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4964 delete $record->{ $options->{ "keys" }->[ 0 ] };
4967 put_record( $record, $out );
4972 sub script_uniq_vals
4974 # Martin A. Hansen, August 2007.
4976 # Find unique values in stream.
4978 my ( $in, # handle to in stream
4979 $out, # handle to out stream
4980 $options, # options hash
4985 my ( %hash, $record );
4987 while ( $record = get_record( $in ) )
4989 if ( $record->{ $options->{ "key" } } )
4991 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4993 put_record( $record, $out );
4995 $hash{ $record->{ $options->{ "key" } } } = 1;
4997 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4999 put_record( $record, $out );
5003 $hash{ $record->{ $options->{ "key" } } } = 1;
5008 put_record( $record, $out );
5014 sub script_merge_vals
5016 # Martin A. Hansen, August 2007.
5018 # Rename keys in stream.
5020 my ( $in, # handle to in stream
5021 $out, # handle to out stream
5022 $options, # options hash
5027 my ( $record, @join, $i );
5029 $options->{ "delimit" } ||= '_';
5031 while ( $record = get_record( $in ) )
5033 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
5035 @join = $record->{ $options->{ "keys" }->[ 0 ] };
5037 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
5038 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
5041 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
5044 put_record( $record, $out );
5049 sub script_merge_records
5051 # Martin A. Hansen, July 2008.
5053 # Merges records in the stream based on identical values of two given keys.
5055 my ( $in, # handle to in stream
5056 $out, # handle to out stream
5057 $options, # options hash
5062 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
5063 $num1, $num2, $num, $cmp, $i );
5065 $merge = $options->{ "merge" } || "AandB";
5067 $file1 = "$BP_TMP/merge_records1.tmp";
5068 $file2 = "$BP_TMP/merge_records2.tmp";
5070 $fh1 = Maasha::Common::write_open( $file1 );
5071 $fh2 = Maasha::Common::write_open( $file2 );
5073 $key1 = $options->{ "keys" }->[ 0 ];
5074 $key2 = $options->{ "keys" }->[ 1 ];
5076 $num = $key2 =~ s/n$//;
5080 while ( $record = get_record( $in ) )
5082 if ( exists $record->{ $key1 } )
5085 @vals1 = $record->{ $key1 };
5087 delete $record->{ $key1 };
5089 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
5091 print $fh1 join( "\t", @vals1 ), "\n";
5095 elsif ( exists $record->{ $key2 } )
5098 @vals2 = $record->{ $key2 };
5100 delete $record->{ $key2 };
5102 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
5104 print $fh2 join( "\t", @vals2 ), "\n";
5115 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5116 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5120 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
5121 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
5124 $fh1 = Maasha::Common::read_open( $file1 );
5125 $fh2 = Maasha::Common::read_open( $file2 );
5127 @vals1 = Maasha::Common::get_fields( $fh1 );
5128 @vals2 = Maasha::Common::get_fields( $fh2 );
5130 while ( $num1 > 0 and $num2 > 0 )
5135 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
5137 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
5142 if ( $merge =~ /^(AorB|AnotB)$/ )
5144 for ( $i = 0; $i < @keys1; $i++ ) {
5145 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5148 put_record( $record, $out );
5151 @vals1 = Maasha::Common::get_fields( $fh1 );
5156 if ( $merge =~ /^(BorA|BnotA)$/ )
5158 for ( $i = 0; $i < @keys2; $i++ ) {
5159 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5162 put_record( $record, $out );
5165 @vals2 = Maasha::Common::get_fields( $fh2 );
5170 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
5172 for ( $i = 0; $i < @keys1; $i++ ) {
5173 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5176 for ( $i = 1; $i < @keys2; $i++ ) {
5177 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5180 put_record( $record, $out );
5183 @vals1 = Maasha::Common::get_fields( $fh1 );
5184 @vals2 = Maasha::Common::get_fields( $fh2 );
5196 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5200 for ( $i = 0; $i < @keys1; $i++ ) {
5201 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5204 put_record( $record, $out );
5207 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5211 for ( $i = 0; $i < @keys2; $i++ ) {
5212 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5215 put_record( $record, $out );
5222 # Martin A. Hansen, August 2007.
5224 # Grab for records in stream.
5226 my ( $in, # handle to in stream
5227 $out, # handle to out stream
5228 $options, # options hash
5233 my ( $keys, $vals_only, $keys_only, $invert, $patterns, $pattern, $regex, $record, $key, $op, $val, %lookup_hash, $found );
5235 $keys = $options->{ 'keys' };
5236 $vals_only = $options->{ 'vals_only' };
5237 $keys_only = $options->{ 'keys_only' };
5238 $invert = $options->{ 'invert' };
5240 if ( $options->{ 'patterns' } )
5242 $patterns = [ split ",", $options->{ 'patterns' } ];
5244 elsif ( -f $options->{ 'patterns_in' } )
5246 $patterns = Maasha::Patscan::read_patterns( $options->{ 'patterns_in' } );
5248 elsif ( $options->{ 'regex' } )
5250 if ( $options->{ 'case_insensitive' } ) {
5251 $regex = qr/$options->{ 'regex' }/i;
5253 $regex = qr/$options->{ 'regex' }/;
5256 elsif ( -f $options->{ 'exact_in' } )
5258 $patterns = Maasha::Patscan::read_patterns( $options->{ 'exact_in' } );
5260 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5264 elsif ( $options->{ 'eval' } )
5266 if ( $options->{ 'eval' } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5274 while ( $record = get_record( $in ) )
5278 if ( %lookup_hash ) {
5279 $found = grab_lookup( \%lookup_hash, $record, $keys, $vals_only, $keys_only );
5280 } elsif ( $patterns ) {
5281 $found = grab_patterns( $patterns, $record, $keys, $vals_only, $keys_only );
5282 } elsif ( $regex ) {
5283 $found = grab_regex( $regex, $record, $keys, $vals_only, $keys_only );
5285 $found = grab_eval( $key, $op, $val, $record );
5288 if ( $found and not $invert ) {
5289 put_record( $record, $out );
5290 } elsif ( not $found and $invert ) {
5291 put_record( $record, $out );
5299 # Martin A. Hansen, August 2007.
5301 # Evaluate extression for records in stream.
5303 my ( $in, # handle to in stream
5304 $out, # handle to out stream
5305 $options, # options hash
5310 my ( $record, $eval_key, @keys, $eval_val );
5312 while ( $record = get_record( $in ) )
5314 if ( $options->{ "eval" } )
5316 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
5323 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
5325 @keys = grep { exists $record->{ $_ } } @keys;
5328 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
5330 $record->{ $eval_key } = eval "$eval_val";
5331 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
5335 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
5339 put_record( $record, $out );
5346 # Martin A. Hansen, June 2008.
5350 my ( $in, # handle to in stream
5351 $out, # handle to out stream
5352 $options, # options hash
5357 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5359 while ( $record = get_record( $in ) )
5363 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5365 push @rows, $record->{ $key };
5369 push @matrix, [ @rows ];
5374 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5376 foreach $row ( @matrix )
5378 for ( $i = 0; $i < @{ $row }; $i++ ) {
5379 $record->{ "V$i" } = $row->[ $i ];
5382 put_record( $record, $out );
5387 sub script_add_ident
5389 # Martin A. Hansen, May 2008.
5391 # Add a unique identifier to each record in stream.
5393 my ( $in, # handle to in stream
5394 $out, # handle to out stream
5395 $options, # options hash
5400 my ( $record, $key, $prefix, $i );
5402 $key = $options->{ "key" } || "ID";
5403 $prefix = $options->{ "prefix" } || "ID";
5407 while ( $record = get_record( $in ) )
5409 $record->{ $key } = sprintf( "$prefix%08d", $i );
5411 put_record( $record, $out );
5418 sub script_count_records
5420 # Martin A. Hansen, August 2007.
5422 # Count records in stream.
5424 my ( $in, # handle to in stream
5425 $out, # handle to out stream
5426 $options, # options hash
5431 my ( $record, $count, $result, $fh, $line );
5435 if ( $options->{ "no_stream" } )
5437 while ( $line = <$in> )
5441 $count++ if $line eq "---";
5446 while ( $record = get_record( $in ) )
5448 put_record( $record, $out );
5454 $result = { "RECORDS_COUNT" => $count };
5456 $fh = write_stream( $options->{ "data_out" } );
5458 put_record( $result, $fh );
5464 sub script_random_records
5466 # Martin A. Hansen, August 2007.
5468 # Pick a number or random records from stream.
5470 my ( $in, # handle to in stream
5471 $out, # handle to out stream
5472 $options, # options hash
5477 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5479 $options->{ "num" } ||= 10;
5481 $tmp_file = "$BP_TMP/random_records.tmp";
5483 $fh_out = Maasha::Common::write_open( $tmp_file );
5487 while ( $record = get_record( $in ) )
5489 put_record( $record, $fh_out );
5499 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5501 while ( $i < $options->{ "num" } )
5503 $rand = int( rand( $count ) );
5505 if ( not exists $rand_hash{ $rand } )
5507 $rand_hash{ $rand } = 1;
5509 $max = $rand if $rand > $max;
5515 $fh_in = Maasha::Common::read_open( $tmp_file );
5519 while ( $record = get_record( $fh_in ) )
5521 put_record( $record, $out ) if exists $rand_hash{ $count };
5523 last if $count == $max;
5534 sub script_sort_records
5536 # Martin A. Hansen, August 2007.
5538 # Sort to sort records according to keys.
5540 my ( $in, # handle to in stream
5541 $out, # handle to out stream
5542 $options, # options hash
5547 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5549 foreach $key ( @{ $options->{ "keys" } } )
5551 if ( $key =~ s/n$// ) {
5552 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5554 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5558 $sort_str = join " or ", @sort_cmd;
5559 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5561 while ( $record = get_record( $in ) ) {
5562 push @records, $record;
5565 @records = sort $sort_sub @records;
5567 if ( $options->{ "reverse" } )
5569 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5570 put_record( $records[ $i ], $out );
5575 for ( $i = 0; $i < scalar @records; $i++ ) {
5576 put_record( $records[ $i ], $out );
5582 sub script_count_vals
5584 # Martin A. Hansen, August 2007.
5586 # Count records in stream.
5588 my ( $in, # handle to in stream
5589 $out, # handle to out stream
5590 $options, # options hash
5595 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5597 $tmp_file = "$BP_TMP/count_cache.tmp";
5599 $fh_out = Maasha::Common::write_open( $tmp_file );
5604 while ( $record = get_record( $in ) )
5606 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5608 push @records, $record;
5610 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5612 map { put_record( $_, $fh_out ) } @records;
5619 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5630 $fh_in = Maasha::Common::read_open( $tmp_file );
5632 while ( $record = get_record( $fh_in ) )
5634 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5636 put_record( $record, $out );
5638 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5646 foreach $record ( @records )
5648 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5650 put_record( $record, $out );
5657 sub script_plot_histogram
5659 # Martin A. Hansen, September 2007.
5661 # Plot a simple histogram for a given key using GNU plot.
5663 my ( $in, # handle to in stream
5664 $out, # handle to out stream
5665 $options, # options hash
5670 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5672 $options->{ "title" } ||= "Histogram";
5673 $options->{ "sort" } ||= "num";
5675 while ( $record = get_record( $in ) )
5677 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5679 put_record( $record, $out ) if not $options->{ "no_stream" };
5682 if ( $options->{ "sort" } eq "num" ) {
5683 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5685 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5688 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5690 $fh = write_stream( $options->{ "data_out" } );
5692 print $fh "$_\n" foreach @{ $result };
5698 sub script_plot_lendist
5700 # Martin A. Hansen, August 2007.
5702 # Plot length distribution using GNU plot.
5704 my ( $in, # handle to in stream
5705 $out, # handle to out stream
5706 $options, # options hash
5711 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5713 $options->{ "title" } ||= "Length Distribution";
5715 while ( $record = get_record( $in ) )
5717 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5719 put_record( $record, $out ) if not $options->{ "no_stream" };
5722 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5724 for ( $i = 0; $i < $max; $i++ ) {
5725 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5728 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5730 $fh = write_stream( $options->{ "data_out" } );
5732 print $fh "$_\n" foreach @{ $result };
5738 sub script_plot_chrdist
5740 # Martin A. Hansen, August 2007.
5742 # Plot chromosome distribution using GNU plot.
5744 my ( $in, # handle to in stream
5745 $out, # handle to out stream
5746 $options, # options hash
5751 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5753 $options->{ "title" } ||= "Chromosome Distribution";
5755 while ( $record = get_record( $in ) )
5757 if ( $record->{ "CHR" } ) { # generic
5758 $data_hash{ $record->{ "CHR" } }++;
5759 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5760 $data_hash{ $record->{ "S_ID" } }++;
5761 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5762 $data_hash{ $record->{ "S_ID" } }++;
5763 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5764 $data_hash{ $record->{ "S_ID" } }++;
5767 put_record( $record, $out ) if not $options->{ "no_stream" };
5770 foreach $elem ( keys %data_hash )
5774 $sort_key =~ s/chr//i;
5776 $sort_key =~ s/^X(.*)/99$1/;
5777 $sort_key =~ s/^Y(.*)/99$1/;
5778 $sort_key =~ s/^Z(.*)/999$1/;
5779 $sort_key =~ s/^M(.*)/9999$1/;
5780 $sort_key =~ s/^U(.*)/99999$1/;
5782 $count = $sort_key =~ tr/_//;
5784 $sort_key =~ s/_.*/"999999" x $count/ex;
5786 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5789 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5791 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5793 $fh = write_stream( $options->{ "data_out" } );
5795 print $fh "$_\n" foreach @{ $result };
5801 sub script_plot_karyogram
5803 # Martin A. Hansen, August 2007.
5805 # Plot hits on karyogram.
5807 my ( $in, # handle to in stream
5808 $out, # handle to out stream
5809 $options, # options hash
5814 my ( %options, $record, @data, $fh, $result, %data_hash );
5816 $options->{ "genome" } ||= "human";
5817 $options->{ "feat_color" } ||= "black";
5819 while ( $record = get_record( $in ) )
5821 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5823 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5826 put_record( $record, $out ) if not $options->{ "no_stream" };
5829 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5831 $fh = write_stream( $options->{ "data_out" } );
5839 sub script_plot_matches
5841 # Martin A. Hansen, August 2007.
5843 # Plot matches in 2D generating a dotplot.
5845 my ( $in, # handle to in stream
5846 $out, # handle to out stream
5847 $options, # options hash
5852 my ( $record, @data, $fh, $result, %data_hash );
5854 $options->{ "direction" } ||= "both";
5856 while ( $record = get_record( $in ) )
5858 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5859 push @data, $record;
5862 put_record( $record, $out ) if not $options->{ "no_stream" };
5865 $options->{ "title" } ||= "plot_matches";
5866 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5867 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5869 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5871 $fh = write_stream( $options->{ "data_out" } );
5873 print $fh "$_\n" foreach @{ $result };
5879 sub script_length_vals
5881 # Martin A. Hansen, August 2007.
5883 # Determine the length of the value for given keys.
5885 my ( $in, # handle to in stream
5886 $out, # handle to out stream
5887 $options, # options hash
5892 my ( $record, $key );
5894 while ( $record = get_record( $in ) )
5896 foreach $key ( @{ $options->{ "keys" } } )
5898 if ( $record->{ $key } ) {
5899 $record->{ $key . "_LEN" } = length $record->{ $key };
5903 put_record( $record, $out );
5910 # Martin A. Hansen, August 2007.
5912 # Calculates the sums for values of given keys.
5914 my ( $in, # handle to in stream
5915 $out, # handle to out stream
5916 $options, # options hash
5921 my ( $record, $key, %sum_hash, $fh );
5923 while ( $record = get_record( $in ) )
5925 foreach $key ( @{ $options->{ "keys" } } )
5927 if ( $record->{ $key } ) {
5928 $sum_hash{ $key } += $record->{ $key };
5932 put_record( $record, $out ) if not $options->{ "no_stream" };
5935 $fh = write_stream( $options->{ "data_out" } );
5937 foreach $key ( @{ $options->{ "keys" } } ) {
5938 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5945 sub script_mean_vals
5947 # Martin A. Hansen, August 2007.
5949 # Calculate the mean of values of given keys.
5951 my ( $in, # handle to in stream
5952 $out, # handle to out stream
5953 $options, # options hash
5958 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5960 while ( $record = get_record( $in ) )
5962 foreach $key ( @{ $options->{ "keys" } } )
5964 if ( $record->{ $key } )
5966 $sum_hash{ $key } += $record->{ $key };
5967 $count_hash{ $key }++;
5971 put_record( $record, $out ) if not $options->{ "no_stream" };
5974 $fh = write_stream( $options->{ "data_out" } );
5976 foreach $key ( @{ $options->{ "keys" } } )
5978 if ( $count_hash{ $key } ) {
5979 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5984 put_record( { $key . "_MEAN" => $mean } , $fh );
5991 sub script_median_vals
5993 # Martin A. Hansen, March 2008.
5995 # Calculate the median values of given keys.
5997 my ( $in, # handle to in stream
5998 $out, # handle to out stream
5999 $options, # options hash
6004 my ( $record, $key, %median_hash, $median, $fh );
6006 while ( $record = get_record( $in ) )
6008 foreach $key ( @{ $options->{ "keys" } } ) {
6009 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
6012 put_record( $record, $out ) if not $options->{ "no_stream" };
6015 $fh = write_stream( $options->{ "data_out" } );
6017 foreach $key ( @{ $options->{ "keys" } } )
6019 if ( $median_hash{ $key } ) {
6020 $median = Maasha::Calc::median( $median_hash{ $key } );
6025 put_record( { $key . "_MEDIAN" => $median } , $fh );
6034 # Martin A. Hansen, February 2008.
6036 # Determine the maximum values of given keys.
6038 my ( $in, # handle to in stream
6039 $out, # handle to out stream
6040 $options, # options hash
6045 my ( $record, $key, $fh, %max_hash, $max_record );
6047 while ( $record = get_record( $in ) )
6049 foreach $key ( @{ $options->{ "keys" } } )
6051 if ( $record->{ $key } )
6053 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
6057 put_record( $record, $out ) if not $options->{ "no_stream" };
6060 $fh = write_stream( $options->{ "data_out" } );
6062 foreach $key ( @{ $options->{ "keys" } } )
6064 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
6067 put_record( $max_record, $fh );
6075 # Martin A. Hansen, February 2008.
6077 # Determine the minimum values of given keys.
6079 my ( $in, # handle to in stream
6080 $out, # handle to out stream
6081 $options, # options hash
6086 my ( $record, $key, $fh, %min_hash, $min_record );
6088 while ( $record = get_record( $in ) )
6090 foreach $key ( @{ $options->{ "keys" } } )
6092 if ( defined $record->{ $key } )
6094 if ( exists $min_hash{ $key } ) {
6095 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6097 $min_hash{ $key } = $record->{ $key };
6102 put_record( $record, $out ) if not $options->{ "no_stream" };
6105 $fh = write_stream( $options->{ "data_out" } );
6107 foreach $key ( @{ $options->{ "keys" } } )
6109 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6112 put_record( $min_record, $fh );
6118 sub script_upload_to_ucsc
6120 # Martin A. Hansen, August 2007.
6122 # Calculate the mean of values of given keys.
6124 # This routine has developed into the most ugly hack. Do something!
6126 my ( $in, # handle to in stream
6127 $out, # handle to out stream
6128 $options, # options hash
6133 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
6135 $options->{ "short_label" } ||= $options->{ 'table' };
6136 $options->{ "long_label" } ||= $options->{ 'table' };
6137 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6138 $options->{ "priority" } ||= 1;
6139 $options->{ "visibility" } ||= "pack";
6140 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6141 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6143 $file = "$BP_TMP/ucsc_upload.tmp";
6148 $fh_out = Maasha::Common::write_open( $file );
6150 while ( $record = get_record( $in ) )
6152 put_record( $record, $out ) if not $options->{ "no_stream" };
6154 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6158 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
6159 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
6162 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6166 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6167 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6171 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6173 # chrom chromStart chromEnd name score strand size secStr conf
6177 print $fh_out join ( "\t",
6179 $record->{ "CHR_BEG" },
6180 $record->{ "CHR_END" } + 1,
6181 $record->{ "Q_ID" },
6182 $record->{ "SCORE" },
6183 $record->{ "STRAND" },
6184 $record->{ "SIZE" },
6185 $record->{ "SEC_STRUCT" },
6186 $record->{ "CONF" },
6189 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6192 $columns = $record->{ "BED_COLS" };
6194 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6195 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns );
6198 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6203 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6204 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns );
6207 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6212 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6214 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6215 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns );
6218 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6223 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
6224 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns );
6228 if ( $i == $options->{ "chunk_size" } )
6232 if ( $format eq "BED" ) {
6233 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6234 } elsif ( $format eq "PSL" ) {
6235 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6244 $fh_out = Maasha::Common::write_open( $file );
6252 if ( exists $options->{ "database" } and $options->{ "table" } )
6254 if ( $format eq "BED" )
6256 $type = "bed $columns";
6258 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6260 elsif ( $format eq "BED_SS" )
6262 $type = "type bed 6 +";
6264 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6266 elsif ( $format eq "PSL" )
6270 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6272 elsif ( $format eq "WIGGLE" )
6274 $options->{ "visibility" } = "full";
6276 $wig_file = "$options->{ 'table' }.wig";
6277 $wib_file = "$options->{ 'table' }.wib";
6279 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6281 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6283 if ( $options->{ 'verbose' } ) {
6284 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6286 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6289 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6297 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6302 Maasha::UCSC::ucsc_update_config( $options, $type );
6307 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6312 # Martin A. Hansen, November 2009.
6314 # Uses keys from a lookup hash to search records. Optionally, a list of
6315 # keys can be given so the lookup is limited to these, also, flags
6316 # can be given to limit lookup to keys or vals only. Returns 1 if lookup
6317 # succeeded, else 0.
6319 my ( $lookup_hash, # hashref with patterns
6321 $keys, # list of keys - OPTIONAL
6322 $vals_only, # only vals flag - OPTIONAL
6323 $keys_only, # only keys flag - OPTIONAL
6330 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } @{ $keys };
6334 if ( not $vals_only ) {
6335 map { return 1 if exists $lookup_hash->{ $_ } } keys %{ $record };
6338 if ( not $keys_only ) {
6339 map { return 1 if exists $lookup_hash->{ $record->{ $_ } } } keys %{ $record };
6349 # Martin A. Hansen, November 2009.
6351 # Uses patterns to match records containing the pattern as a substring.
6352 # Returns 1 if the record is matched, else 0.
6354 my ( $patterns, # list of patterns
6356 $keys, # list of keys - OPTIONAL
6357 $vals_only, # only vals flag - OPTIONAL
6358 $keys_only, # only keys flag - OPTIONAL
6365 foreach $pattern ( @{ $patterns } )
6369 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } @{ $keys };
6373 if ( not $vals_only ) {
6374 map { return 1 if index( $_, $pattern ) >= 0 } keys %{ $record };
6377 if ( not $keys_only ) {
6378 map { return 1 if index( $record->{ $_ }, $pattern ) >= 0 } keys %{ $record };
6389 # Martin A. Hansen, November 2009.
6391 # Uses regex to match records.
6392 # Returns 1 if the record is matched, else 0.
6394 my ( $regex, # regex to match
6396 $keys, # list of keys - OPTIONAL
6397 $vals_only, # only vals flag - OPTIONAL
6398 $keys_only, # only keys flag - OPTIONAL
6405 map { return 1 if $record->{ $_ } =~ /$regex/ } @{ $keys };
6409 if ( not $vals_only ) {
6410 map { return 1 if $_ =~ /$regex/ } keys %{ $record };
6413 if ( not $keys_only ) {
6414 map { return 1 if $record->{ $_ } =~ /$regex/ } keys %{ $record };
6424 # Martin A. Hansen, November 2009.
6426 # Test if the value of a given record key evaluates according
6427 # to a given operator. Returns 1 if eval is OK, else 0.
6429 my ( $key, # record key
6437 if ( defined $record->{ $key } )
6439 return 1 if ( $op eq "<" and $record->{ $key } < $val );
6440 return 1 if ( $op eq ">" and $record->{ $key } > $val );
6441 return 1 if ( $op eq ">=" and $record->{ $key } >= $val );
6442 return 1 if ( $op eq "<=" and $record->{ $key } <= $val );
6443 return 1 if ( $op eq "=" and $record->{ $key } == $val );
6444 return 1 if ( $op eq "!=" and $record->{ $key } != $val );
6445 return 1 if ( $op eq "eq" and $record->{ $key } eq $val );
6446 return 1 if ( $op eq "ne" and $record->{ $key } ne $val );
6455 # Martin A. Hansen, July 2007.
6457 # Opens a stream to STDIN or a file,
6459 my ( $path, # path - OPTIONAL
6462 # Returns filehandle.
6466 if ( not -t STDIN ) {
6467 $fh = Maasha::Common::read_stdin();
6468 } elsif ( not $path ) {
6469 # Maasha::Common::error( qq(no data stream) );
6471 $fh = Maasha::Common::read_open( $path );
6474 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6482 # Martin A. Hansen, August 2007.
6484 # Opens a stream to STDOUT or a file.
6486 my ( $path, # path - OPTIONAL
6487 $gzip, # compress data - OPTIONAL
6490 # Returns filehandle.
6495 $fh = Maasha::Common::write_open( $path, $gzip );
6497 $fh = Maasha::Common::write_stdout();
6506 # Martin A. Hansen, July 2007.
6508 # Reads one record at a time and converts that record
6509 # to a Perl data structure (a hash) which is returned.
6511 my ( $fh, # handle to stream
6516 my ( $block, @lines, $line, $key, $value, %record );
6518 local $/ = "\n---\n";
6524 return if not defined $block;
6526 @lines = split "\n", $block;
6528 foreach $line ( @lines )
6530 ( $key, $value ) = split ": ", $line, 2;
6532 $record{ $key } = $value;
6535 return wantarray ? %record : \%record;
6541 # Martin A. Hansen, July 2007.
6543 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6545 my ( $data, # data structure
6546 $fh, # file handle - OPTIONAL
6551 if ( scalar keys %{ $data } )
6555 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6560 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6571 # Martin A. Hansen, November 2007.
6573 # Extracts files from an explicit GetOpt::Long argument
6574 # allowing for the use of glob. E.g.
6575 # --data_in=test.fna
6576 # --data_in=test.fna,test2.fna
6578 # --data_in=test.fna,/dir/*.fna
6580 my ( $option, # option from GetOpt::Long
6585 my ( $elem, @files );
6587 foreach $elem ( split ",", $option )
6591 } elsif ( $elem =~ /\*/ ) {
6592 push @files, glob( $elem );
6596 return wantarray ? @files : \@files;
6602 # Martin A. Hansen, April 2008.
6604 # Removes temporary directory and exits gracefully.
6605 # This subroutine is meant to be run always as the last
6606 # thing even if a script is dies or is interrupted
6609 my ( $sig, # signal from the %SIG
6612 # print STDERR "signal->$sig<-\n";
6620 if ( $sig =~ /MAASHA_ERROR/ ) {
6621 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6622 } elsif ( $sig eq "INT" ) {
6623 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6624 } elsif ( $sig eq "TERM" ) {
6625 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6627 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6639 # Martin A. Hansen, July 2008.
6641 # Cleans out any unused temporary files and directories in BP_TMP.
6645 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6647 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6649 $curr_pid = Maasha::Common::get_processid();
6651 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6653 foreach $dir ( @dirs )
6655 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6661 if ( $user eq Maasha::Common::get_user() )
6663 if ( not Maasha::Common::process_running( $pid ) )
6665 # print STDERR "Removing stale dir: $dir\n";
6666 Maasha::Common::dir_remove( $dir );
6668 elsif ( $pid == $curr_pid )
6670 # print STDERR "Removing current dir: $dir\n";
6671 # Maasha::Common::dir_remove( $dir );
6685 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<