1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
77 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
80 $SIG{ '__DIE__' } = \&sig_handler;
81 $SIG{ 'INT' } = \&sig_handler;
82 $SIG{ 'TERM' } = \&sig_handler;
85 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
88 my ( $script, $BP_TMP );
90 $script = Maasha::Common::get_scriptname();
91 $BP_TMP = Maasha::Common::get_tmpdir();
94 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
97 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
98 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
100 $log_global->autoflush( 1 );
101 $log_local->autoflush( 1 );
103 &log( $log_global, $script, \@ARGV );
104 &log( $log_local, $script, \@ARGV );
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
113 run_script( $script );
116 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121 # Martin A. Hansen, January 2008.
123 # Log messages to logfile.
125 my ( $fh, # filehandle to logfile
126 $script, # script name
127 $argv, # reference to @ARGV
132 my ( $time_stamp, $user );
134 $time_stamp = Maasha::Common::time_stamp();
136 $user = $ENV{ 'USER' };
138 $script = "biopieces" if $script eq "-e";
140 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
146 # Martin A. Hansen, August 2007.
148 # Run a specific script.
150 my ( $script, # script name
155 my ( $t0, $t1, $options, $in, $out );
157 $t0 = gettimeofday();
159 $options = get_options( $script );
161 $options->{ "SCRIPT" } = $script;
163 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
164 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
167 $in = read_stream( $options->{ "stream_in" } );
168 $out = write_stream( $options->{ "stream_out" } );
170 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
171 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
172 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
173 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
174 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
175 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
176 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
177 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
178 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
179 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
180 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
181 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
182 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
183 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
184 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
185 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
186 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
187 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
188 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
189 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
190 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
191 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
192 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
193 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
194 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
195 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
196 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
197 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
198 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
199 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
200 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
201 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
202 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
203 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
204 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
205 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
206 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
207 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
209 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
210 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
211 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
212 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
213 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
214 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
215 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
216 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
217 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
218 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
219 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
220 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
221 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
222 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
223 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
224 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
225 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
226 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
227 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
228 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
229 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
230 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
231 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
232 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
233 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
234 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
235 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
236 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
237 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
238 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
239 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
240 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
241 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
242 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
243 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
244 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
245 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
246 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
247 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
248 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
249 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
250 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
251 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
252 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
253 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
254 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
255 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
256 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
257 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
258 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
259 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
260 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
261 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
262 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
264 close $in if defined $in;
267 $t1 = gettimeofday();
269 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
275 # Martin A. Hansen, February 2008.
277 # Gets options from commandline and checks these vigerously.
279 my ( $script, # name of script
284 my ( %options, @options, $opt, @genomes, $real );
286 if ( $script eq "print_usage" )
292 elsif ( $script eq "read_fasta" )
299 elsif ( $script eq "read_tab" )
310 elsif ( $script eq "read_psl" )
317 elsif ( $script eq "read_bed" )
324 elsif ( $script eq "read_fixedstep" )
331 elsif ( $script eq "read_blast_tab" )
338 elsif ( $script eq "read_embl" )
348 elsif ( $script eq "read_stockholm" )
355 elsif ( $script eq "read_phastcons" )
366 elsif ( $script eq "read_soft" )
374 elsif ( $script eq "read_gff" )
381 elsif ( $script eq "read_2bit" )
389 elsif ( $script eq "read_solexa" )
398 elsif ( $script eq "read_solid" )
406 elsif ( $script eq "read_mysql" )
415 elsif ( $script eq "format_genome" )
424 elsif ( $script eq "length_seq" )
431 elsif ( $script eq "oligo_freq" )
438 elsif ( $script eq "create_weight_matrix" )
444 elsif ( $script eq "calc_fixedstep" )
451 elsif ( $script eq "transliterate_seq" )
459 elsif ( $script eq "transliterate_vals" )
468 elsif ( $script eq "translate_seq" )
474 elsif ( $script eq "extract_seq" )
482 elsif ( $script eq "get_genome_seq" )
494 elsif ( $script eq "get_genome_align" )
505 elsif ( $script eq "get_genome_phastcons" )
516 elsif ( $script eq "split_seq" )
523 elsif ( $script eq "split_bed" )
530 elsif ( $script eq "tile_seq" )
537 elsif ( $script eq "invert_align" )
543 elsif ( $script eq "patscan_seq" )
554 elsif ( $script eq "create_blast_db" )
561 elsif ( $script eq "blast_seq" )
573 elsif ( $script eq "blat_seq" )
585 elsif ( $script eq "soap_seq" )
596 elsif ( $script eq "match_seq" )
603 elsif ( $script eq "create_vmatch_index" )
611 elsif ( $script eq "vmatch_seq" )
622 elsif ( $script eq "write_fasta" )
631 elsif ( $script eq "write_align" )
641 elsif ( $script eq "write_blast" )
650 elsif ( $script eq "write_tab" )
662 elsif ( $script eq "write_bed" )
670 elsif ( $script eq "write_psl" )
678 elsif ( $script eq "write_fixedstep" )
686 elsif ( $script eq "write_2bit" )
694 elsif ( $script eq "write_solid" )
703 elsif ( $script eq "plot_seqlogo" )
710 elsif ( $script eq "plot_phastcons_profiles" )
725 elsif ( $script eq "analyze_vals" )
732 elsif ( $script eq "head_records" )
738 elsif ( $script eq "remove_keys" )
745 elsif ( $script eq "remove_adaptor" )
754 elsif ( $script eq "rename_keys" )
760 elsif ( $script eq "uniq_vals" )
767 elsif ( $script eq "merge_vals" )
774 elsif ( $script eq "merge_records" )
781 elsif ( $script eq "grab" )
796 elsif ( $script eq "compute" )
802 elsif ( $script eq "add_ident" )
809 elsif ( $script eq "count_records" )
816 elsif ( $script eq "random_records" )
822 elsif ( $script eq "sort_records" )
829 elsif ( $script eq "count_vals" )
835 elsif ( $script eq "plot_histogram" )
848 elsif ( $script eq "plot_lendist" )
860 elsif ( $script eq "plot_chrdist" )
871 elsif ( $script eq "plot_karyogram" )
880 elsif ( $script eq "plot_matches" )
892 elsif ( $script eq "length_vals" )
898 elsif ( $script eq "sum_vals" )
906 elsif ( $script eq "mean_vals" )
914 elsif ( $script eq "median_vals" )
922 elsif ( $script eq "max_vals" )
930 elsif ( $script eq "min_vals" )
938 elsif ( $script eq "upload_to_ucsc" )
962 # print STDERR Dumper( \@options );
969 # print STDERR Dumper( \%options );
971 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
972 return wantarray ? %options : \%options;
975 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
976 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
977 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
978 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
979 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
980 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
981 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
982 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
983 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
985 # ---- check arguments ----
987 if ( $options{ 'data_in' } )
989 $options{ "files" } = getopt_files( $options{ 'data_in' } );
991 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
994 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
996 # print STDERR Dumper( \%options );
998 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
1000 foreach $opt ( keys %options )
1002 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
1004 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1006 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1008 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1010 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1012 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1014 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1016 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1018 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1020 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1022 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1024 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1026 elsif ( $opt eq "genome" and $script ne "format_genome" )
1028 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1029 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1031 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1032 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1035 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1037 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1039 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1041 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1043 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1045 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1047 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1049 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1051 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
1053 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
1057 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1058 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1059 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1060 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1061 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1062 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1063 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1064 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1065 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" };
1066 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1067 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" };
1069 if ( $script eq "upload_to_ucsc" )
1071 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1072 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1075 return wantarray ? %options : \%options;
1079 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1082 sub script_print_usage
1084 # Martin A. Hansen, January 2008.
1086 # Retrieves usage information from file and
1087 # prints this nicely formatted.
1089 my ( $in, # handle to in stream
1090 $out, # handle to out stream
1091 $options, # options hash
1096 my ( $file, $wiki, $lines );
1098 if ( $options->{ 'data_in' } ) {
1099 $file = $options->{ 'data_in' };
1101 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1104 $wiki = Maasha::Gwiki::gwiki_read( $file );
1106 if ( not $options->{ "help" } ) {
1107 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1110 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1112 print STDERR "$_\n" foreach @{ $lines };
1118 sub script_list_biopieces
1120 # Martin A. Hansen, January 2008.
1122 # Prints the synopsis from the usage for each of the biopieces.
1124 my ( $in, # handle to in stream
1125 $out, # handle to out stream
1126 $options, # options hash
1131 my ( @files, $file, $wiki, $program, $synopsis );
1133 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1135 foreach $file ( sort @files )
1137 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1141 $wiki = Maasha::Gwiki::gwiki_read( $file );
1143 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1144 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1146 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1147 $synopsis =~ s/!(\w)/$1/g;
1149 printf( "%-30s%s\n", $program, $synopsis );
1157 sub script_list_genomes
1159 # Martin A. Hansen, January 2008.
1161 # Prints the synopsis from the usage for each of the biopieces.
1163 my ( $in, # handle to in stream
1164 $out, # handle to out stream
1165 $options, # options hash
1170 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1172 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1174 foreach $genome ( @genomes )
1176 next if $genome =~ /\.$/;
1178 @formats = Maasha::Common::ls_dirs( $genome );
1180 foreach $format ( @formats )
1182 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1184 $hash{ $1 }{ $2 } = 1;
1193 map { push @row, $_ } sort keys %found;
1195 print join( "\t", @row ), "\n";
1197 foreach $genome ( sort keys %hash )
1201 foreach $format ( sort keys %found )
1203 if ( exists $hash{ $genome }{ $format } ) {
1210 print join( "\t", @row ), "\n";
1215 sub script_read_fasta
1217 # Martin A. Hansen, August 2007.
1219 # Read sequences from FASTA file.
1221 my ( $in, # handle to in stream
1222 $out, # handle to out stream
1223 $options, # options hash
1228 my ( $record, $file, $data_in, $entry, $num );
1230 while ( $record = get_record( $in ) ) {
1231 put_record( $record, $out );
1236 foreach $file ( @{ $options->{ "files" } } )
1238 $data_in = Maasha::Common::read_open( $file );
1240 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1242 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1245 SEQ_NAME => $entry->[ SEQ_NAME ],
1246 SEQ => $entry->[ SEQ ],
1247 SEQ_LEN => length $entry->[ SEQ ],
1250 put_record( $record, $out );
1253 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1263 close $data_in if $data_in;
1269 # Martin A. Hansen, August 2007.
1271 # Read table or table columns from stream or file.
1273 my ( $in, # handle to in stream
1274 $out, # handle to out stream
1275 $options, # options hash
1280 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1282 $options->{ 'delimit' } ||= '\s+';
1284 while ( $record = get_record( $in ) ) {
1285 put_record( $record, $out );
1288 $skip = $options->{ 'skip' } ||= 0;
1291 foreach $file ( @{ $options->{ "files" } } )
1293 $data_in = Maasha::Common::read_open( $file );
1295 while ( $line = <$data_in> )
1303 next if $line =~ /^#|^$/;
1310 @fields = split /$options->{'delimit'}/, $line;
1312 if ( $options->{ "cols" } ) {
1313 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1318 for ( $i = 0; $i < @fields2; $i++ )
1320 if ( $options->{ "keys" }->[ $i ] ) {
1321 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1323 $record->{ "V" . $i } = $fields2[ $i ];
1327 put_record( $record, $out );
1329 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1339 close $data_in if $data_in;
1345 # Martin A. Hansen, August 2007.
1347 # Read psl table from stream or file.
1349 my ( $in, # handle to in stream
1350 $out, # handle to out stream
1351 $options, # options hash
1356 my ( $record, $file, $data_in, $num );
1358 while ( $record = get_record( $in ) ) {
1359 put_record( $record, $out );
1364 foreach $file ( @{ $options->{ "files" } } )
1366 $data_in = Maasha::Common::read_open( $file );
1368 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1370 put_record( $record, $out );
1372 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1384 # Martin A. Hansen, August 2007.
1386 # Read bed table from stream or file.
1388 my ( $in, # handle to in stream
1389 $out, # handle to out stream
1390 $options, # options hash
1395 my ( $file, $record, $entry, $data_in, $num );
1397 while ( $record = get_record( $in ) ) {
1398 put_record( $record, $out );
1403 foreach $file ( @{ $options->{ "files" } } )
1405 $data_in = Maasha::Common::read_open( $file );
1407 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1409 put_record( $entry, $out );
1411 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1421 close $data_in if $data_in;
1425 sub script_read_fixedstep
1427 # Martin A. Hansen, Juli 2008.
1429 # Read fixedstep wiggle format from stream or file.
1431 my ( $in, # handle to in stream
1432 $out, # handle to out stream
1433 $options, # options hash
1438 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1440 while ( $record = get_record( $in ) ) {
1441 put_record( $record, $out );
1446 foreach $file ( @{ $options->{ "files" } } )
1448 $data_in = Maasha::Common::read_open( $file );
1450 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1452 $head = shift @{ $entry };
1454 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1456 $record->{ "REC_TYPE" } = "fixed_step";
1457 $record->{ "CHR" } = $1;
1458 $record->{ "CHR_BEG" } = $2;
1459 $record->{ "STEP" } = $3;
1460 $record->{ "VALS" } = join ";", @{ $entry };
1463 put_record( $record, $out );
1465 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1475 close $data_in if $data_in;
1479 sub script_read_blast_tab
1481 # Martin A. Hansen, September 2007.
1483 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1485 my ( $in, # handle to in stream
1486 $out, # handle to out stream
1487 $options, # options hash
1492 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1494 while ( $record = get_record( $in ) ) {
1495 put_record( $record, $out );
1500 foreach $file ( @{ $options->{ "files" } } )
1502 $data_in = Maasha::Common::read_open( $file );
1504 while ( $line = <$data_in> )
1508 next if $line =~ /^#/;
1510 @fields = split /\t/, $line;
1512 $record->{ "REC_TYPE" } = "BLAST";
1513 $record->{ "Q_ID" } = $fields[ 0 ];
1514 $record->{ "S_ID" } = $fields[ 1 ];
1515 $record->{ "IDENT" } = $fields[ 2 ];
1516 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1517 $record->{ "MISMATCHES" } = $fields[ 4 ];
1518 $record->{ "GAPS" } = $fields[ 5 ];
1519 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1520 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1521 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1522 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1523 $record->{ "E_VAL" } = $fields[ 10 ];
1524 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1526 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1528 $record->{ "STRAND" } = '-';
1530 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1534 $record->{ "STRAND" } = '+';
1537 put_record( $record, $out );
1539 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1549 close $data_in if $data_in;
1553 sub script_read_embl
1555 # Martin A. Hansen, August 2007.
1559 my ( $in, # handle to in stream
1560 $out, # handle to out stream
1561 $options, # options hash
1566 my ( %options2, $file, $data_in, $num, $entry, $record );
1568 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1569 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1570 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1572 while ( $record = get_record( $in ) ) {
1573 put_record( $record, $out );
1578 foreach $file ( @{ $options->{ "files" } } )
1580 $data_in = Maasha::Common::read_open( $file );
1582 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1584 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1586 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1588 $record_copy = dclone $record;
1590 delete $record_copy->{ "FT" };
1592 put_record( $record_copy, $out );
1594 delete $record_copy->{ "SEQ" };
1596 foreach $feat ( keys %{ $record->{ "FT" } } )
1598 $record_copy->{ "FEAT_TYPE" } = $feat;
1600 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1602 foreach $qual ( keys %{ $feat2 } )
1604 $qual_val = join "; ", @{ $feat2->{ $qual } };
1609 $record_copy->{ $qual } = $qual_val;
1612 put_record( $record_copy, $out );
1616 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1626 close $data_in if $data_in;
1630 sub script_read_stockholm
1632 # Martin A. Hansen, August 2007.
1634 # Read Stockholm format.
1636 my ( $in, # handle to in stream
1637 $out, # handle to out stream
1638 $options, # options hash
1643 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1645 while ( $record = get_record( $in ) ) {
1646 put_record( $record, $out );
1651 foreach $file ( @{ $options->{ "files" } } )
1653 $data_in = Maasha::Common::read_open( $file );
1655 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1657 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1661 foreach $key ( keys %{ $record->{ "GF" } } ) {
1662 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1665 $record_anno->{ "ALIGN" } = $num;
1667 put_record( $record_anno, $out );
1669 foreach $seq ( @{ $record->{ "ALIGN" } } )
1671 undef $record_align;
1674 SEQ_NAME => $seq->[ 0 ],
1678 put_record( $record_align, $out );
1681 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1691 close $data_in if $data_in;
1695 sub script_read_phastcons
1697 # Martin A. Hansen, December 2007.
1699 # Read PhastCons format.
1701 my ( $in, # handle to in stream
1702 $out, # handle to out stream
1703 $options, # options hash
1708 my ( $data_in, $file, $num, $entry, @records, $record );
1710 $options->{ "min" } ||= 10;
1711 $options->{ "dist" } ||= 25;
1712 $options->{ "threshold" } ||= 0.8;
1713 $options->{ "gap" } ||= 5;
1715 while ( $record = get_record( $in ) ) {
1716 put_record( $record, $out );
1721 foreach $file ( @{ $options->{ "files" } } )
1723 $data_in = Maasha::Common::read_open( $file );
1725 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1727 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1729 foreach $record ( @records )
1731 $record->{ "REC_TYPE" } = "BED";
1732 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1734 put_record( $record, $out );
1736 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1747 close $data_in if $data_in;
1751 sub script_read_soft
1753 # Martin A. Hansen, December 2007.
1756 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1758 my ( $in, # handle to in stream
1759 $out, # handle to out stream
1760 $options, # options hash
1765 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1767 while ( $record = get_record( $in ) ) {
1768 put_record( $record, $out );
1773 foreach $file ( @{ $options->{ "files" } } )
1775 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1777 $soft_index = Maasha::NCBI::soft_index_file( $file );
1779 $fh = Maasha::Common::read_open( $file );
1781 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1783 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1785 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1787 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1789 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1791 foreach $sample ( @samples )
1794 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1796 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1798 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1800 foreach $record ( @{ $records } )
1802 put_record( $record, $out );
1804 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1809 $old_end = $sample->{ "LINE_END" };
1817 close $data_in if $data_in;
1824 # Martin A. Hansen, February 2008.
1827 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1829 my ( $in, # handle to in stream
1830 $out, # handle to out stream
1831 $options, # options hash
1836 my ( $data_in, $file, $fh, $num, $record, $entry );
1838 while ( $record = get_record( $in ) ) {
1839 put_record( $record, $out );
1844 foreach $file ( @{ $options->{ "files" } } )
1846 $fh = Maasha::Common::read_open( $file );
1848 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1850 put_record( $entry, $out );
1852 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1862 close $data_in if $data_in;
1866 sub script_read_2bit
1868 # Martin A. Hansen, March 2008.
1870 # Read sequences from 2bit file.
1872 my ( $in, # handle to in stream
1873 $out, # handle to out stream
1874 $options, # options hash
1879 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1881 $mask = 1 if not $options->{ "no_mask" };
1883 while ( $record = get_record( $in ) ) {
1884 put_record( $record, $out );
1889 foreach $file ( @{ $options->{ "files" } } )
1891 $data_in = Maasha::Common::read_open( $file );
1893 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1895 foreach $line ( @{ $toc } )
1897 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1898 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1899 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1901 put_record( $record, $out );
1903 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1913 close $data_in if $data_in;
1917 sub script_read_solexa
1919 # Martin A. Hansen, March 2008.
1921 # Read Solexa sequence reads from file.
1923 my ( $in, # handle to in stream
1924 $out, # handle to out stream
1925 $options, # options hash
1930 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1932 $options->{ "format" } ||= "octal";
1933 $options->{ "quality" } ||= 20;
1935 while ( $record = get_record( $in ) ) {
1936 put_record( $record, $out );
1941 foreach $file ( @{ $options->{ "files" } } )
1943 $data_in = Maasha::Common::read_open( $file );
1945 if ( $options->{ "format" } eq "octal" )
1947 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1949 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1951 put_record( $record, $out );
1953 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1960 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1962 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1964 put_record( $record, $out );
1966 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1977 close $data_in if $data_in;
1981 sub script_read_solid
1983 # Martin A. Hansen, April 2008.
1985 # Read Solid sequence from file.
1987 my ( $in, # handle to in stream
1988 $out, # handle to out stream
1989 $options, # options hash
1994 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1996 $options->{ "quality" } ||= 15;
1998 while ( $record = get_record( $in ) ) {
1999 put_record( $record, $out );
2004 foreach $file ( @{ $options->{ "files" } } )
2006 $data_in = Maasha::Common::read_open( $file );
2008 while ( $line = <$data_in> )
2012 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2014 @scores = split /,/, $seq_qual;
2015 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2017 for ( $i = 0; $i < @seqs; $i++ ) {
2018 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2022 REC_TYPE => 'SOLID',
2023 SEQ_NAME => $seq_name,
2025 SEQ_QUAL => join( ";", @scores ),
2026 SEQ_LEN => length $seq_cs,
2027 SEQ => join( "", @seqs ),
2028 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2031 put_record( $record, $out );
2033 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2043 close $data_in if $data_in;
2047 sub script_read_mysql
2049 # Martin A. Hansen, May 2008.
2051 # Read a MySQL query into stream.
2053 my ( $in, # handle to in stream
2054 $out, # handle to out stream
2055 $options, # options hash
2060 my ( $record, $dbh, $results );
2062 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2063 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2065 while ( $record = get_record( $in ) ) {
2066 put_record( $record, $out );
2069 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2071 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2073 Maasha::SQL::disconnect( $dbh );
2075 map { put_record( $_ ) } @{ $results };
2079 sub script_format_genome
2081 # Martin A. Hansen, Juli 2008.
2083 # Format a genome to speficed formats.
2085 my ( $in, # handle to in stream
2086 $out, # handle to out stream
2087 $options, # options hash
2092 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2094 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2095 $genome = $options->{ 'genome' };
2097 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2098 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2099 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2101 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2103 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2105 $fasta_dir = "$dir/genomes/$genome/fasta";
2109 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2111 $fasta_dir = "$dir/genomes/$genome/fasta";
2113 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2116 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2118 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2120 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2122 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2125 while ( $record = get_record( $in ) )
2127 if ( $fh_out and $entry = record2fasta( $record ) )
2129 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2131 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2133 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2135 $vals = $record->{ 'VALS' };
2139 print $fh_out "$vals\n";
2142 put_record( $record, $out ) if not $options->{ "no_stream" };
2145 foreach $format ( @{ $options->{ 'formats' } } )
2147 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2148 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2149 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2150 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2151 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2154 close $fh_out if $fh_out;
2158 sub script_length_seq
2160 # Martin A. Hansen, August 2007.
2162 # Determine the length of sequences in stream.
2164 my ( $in, # handle to in stream
2165 $out, # handle to out stream
2166 $options, # options hash
2171 my ( $record, $total );
2173 while ( $record = get_record( $in ) )
2175 if ( $record->{ "SEQ" } )
2177 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2178 $total += $record->{ "SEQ_LEN" };
2181 put_record( $record, $out ) if not $options->{ "no_stream" };
2184 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2188 sub script_uppercase_seq
2190 # Martin A. Hansen, August 2007.
2192 # Uppercases sequences in stream.
2194 my ( $in, # handle to in stream
2195 $out, # handle to out stream
2202 while ( $record = get_record( $in ) )
2204 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2206 put_record( $record, $out );
2211 sub script_shuffle_seq
2213 # Martin A. Hansen, December 2007.
2215 # Shuffle sequences in stream.
2217 my ( $in, # handle to in stream
2218 $out, # handle to out stream
2225 while ( $record = get_record( $in ) )
2227 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2229 put_record( $record, $out );
2234 sub script_analyze_seq
2236 # Martin A. Hansen, August 2007.
2238 # Analyze sequence composition of sequences in stream.
2240 my ( $in, # handle to in stream
2241 $out, # handle to out stream
2246 my ( $record, $analysis );
2248 while ( $record = get_record( $in ) )
2250 if ( $record->{ "SEQ" } )
2252 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2254 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2257 put_record( $record, $out );
2262 sub script_analyze_tags
2264 # Martin A. Hansen, August 2008.
2266 # Analyze sequence tags in stream.
2268 my ( $in, # handle to in stream
2269 $out, # handle to out stream
2274 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2276 while ( $record = get_record( $in ) )
2278 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2280 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2284 $len_hash{ length( $record->{ "SEQ" } ) }++;
2285 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2288 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2290 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2294 $len_hash{ $record->{ "BED_LEN" } }++;
2295 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2300 foreach $key ( sort { $a <=> $b } keys %len_hash )
2302 $tag_record->{ "TAG_LEN" } = $key;
2303 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2304 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2306 put_record( $tag_record, $out );
2311 sub script_complexity_seq
2313 # Martin A. Hansen, May 2008.
2315 # Generates an index calculated as the most common di-residue over
2316 # the sequence length for all sequences in stream.
2318 my ( $in, # handle to in stream
2319 $out, # handle to out stream
2324 my ( $record, $index );
2326 while ( $record = get_record( $in ) )
2328 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2330 put_record( $record, $out );
2335 sub script_oligo_freq
2337 # Martin A. Hansen, August 2007.
2339 # Determine the length of sequences in stream.
2341 my ( $in, # handle to in stream
2342 $out, # handle to out stream
2343 $options, # options hash
2348 my ( $record, %oligos, @freq_table );
2350 $options->{ "word_size" } ||= 7;
2352 while ( $record = get_record( $in ) )
2354 if ( $record->{ "SEQ" } )
2356 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2358 if ( not $options->{ "all" } )
2360 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2362 map { put_record( $_, $out ) } @freq_table;
2368 put_record( $record, $out );
2371 if ( $options->{ "all" } )
2373 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2375 map { put_record( $_, $out ) } @freq_table;
2380 sub script_create_weight_matrix
2382 # Martin A. Hansen, August 2007.
2384 # Creates a weight matrix from an alignmnet.
2386 my ( $in, # handle to in stream
2387 $out, # handle to out stream
2388 $options, # options hash
2393 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2397 while ( $record = get_record( $in ) )
2399 if ( $record->{ "SEQ" } )
2401 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2403 $res = substr $record->{ "SEQ" }, $i, 1;
2405 $freq_hash{ $i }{ $res }++;
2406 $res_hash{ $res } = 1;
2413 put_record( $record, $out );
2417 foreach $res ( sort keys %res_hash )
2421 $record->{ "V0" } = $res;
2423 for ( $i = 0; $i < keys %freq_hash; $i++ )
2425 $freq = $freq_hash{ $i }{ $res } || 0;
2427 if ( $options->{ "percent" } ) {
2428 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2431 $record->{ "V" . ( $i + 1 ) } = $freq;
2434 put_record( $record, $out );
2439 sub script_calc_bit_scores
2441 # Martin A. Hansen, March 2007.
2443 # Calculates the bit scores for each position from an alignmnet in the stream.
2445 my ( $in, # handle to in stream
2446 $out, # handle to out stream
2451 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2455 while ( $record = get_record( $in ) )
2457 if ( $record->{ "SEQ" } )
2459 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2461 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2463 $res = substr $record->{ "SEQ" }, $i, 1;
2465 next if $res =~ /-|_|~|\./;
2467 $freq_hash{ $i }{ $res }++;
2474 put_record( $record, $out );
2480 if ( $type eq "protein" ) {
2486 for ( $i = 0; $i < keys %freq_hash; $i++ )
2488 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2490 $bit_diff = $bit_max - $bit_height;
2492 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2495 put_record( $record, $out );
2499 sub script_calc_fixedstep
2501 # Martin A. Hansen, September 2008.
2503 # Calculates fixedstep entries from data in the stream.
2505 my ( $in, # handle to in stream
2506 $out, # handle to out stream
2507 $options, # options hash
2512 my ( $record, %fh_hash, $fh_in, $fh_out, $chr, $chr, $beg, $end, $q_id, $block, $entry, $clones, $beg_block, $max, $i );
2514 while ( $record = get_record( $in ) )
2516 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2517 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2518 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2520 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2522 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2524 $fh_out = $fh_hash{ $record->{ "CHR" } };
2526 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2530 map { close $_ } keys %fh_hash;
2532 foreach $chr ( sort keys %fh_hash )
2534 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
2536 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
2540 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
2542 $chr = $entry->{ 'CHR' };
2543 $beg = $entry->{ 'CHR_BEG' };
2544 $end = $entry->{ 'CHR_END' };
2545 $q_id = $entry->{ 'Q_ID' };
2547 if ( $options->{ "score" } ) {
2548 $clones = $entry->{ 'SCORE' };
2549 } elsif ( $q_id =~ /_(\d+)$/ ) {
2559 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2561 $record->{ "CHR" } = $chr;
2562 $record->{ "CHR_BEG" } = $beg_block;
2563 $record->{ "STEP" } = 1;
2564 $record->{ "VALS" } = join ";", @{ $block };
2565 $record->{ "REC_TYPE" } = "fixed_step";
2567 put_record( $record, $out );
2573 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
2574 $block->[ $i ] += $clones;
2577 $max = Maasha::Calc::max( $max, $end );
2586 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
2587 $block->[ $i ] += $clones;
2594 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2596 $record->{ "CHR" } = $chr;
2597 $record->{ "CHR_BEG" } = $beg_block;
2598 $record->{ "STEP" } = 1;
2599 $record->{ "VALS" } = join ";", @{ $block };
2600 $record->{ "REC_TYPE" } = "fixed_step";
2602 put_record( $record, $out );
2604 unlink "$BP_TMP/$chr";
2609 sub script_reverse_seq
2611 # Martin A. Hansen, August 2007.
2613 # Reverse sequence in record.
2615 my ( $in, # handle to in stream
2616 $out, # handle to out stream
2623 while ( $record = get_record( $in ) )
2625 if ( $record->{ "SEQ" } ) {
2626 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2629 put_record( $record, $out );
2634 sub script_complement_seq
2636 # Martin A. Hansen, August 2007.
2638 # Complement sequence in record.
2640 my ( $in, # handle to in stream
2641 $out, # handle to out stream
2646 my ( $record, $type );
2648 while ( $record = get_record( $in ) )
2650 if ( $record->{ "SEQ" } )
2653 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2656 if ( $type eq "rna" ) {
2657 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2658 } elsif ( $type eq "dna" ) {
2659 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2663 put_record( $record, $out );
2668 sub script_remove_indels
2670 # Martin A. Hansen, August 2007.
2672 # Remove indels from sequences in stream.
2674 my ( $in, # handle to in stream
2675 $out, # handle to out stream
2682 while ( $record = get_record( $in ) )
2684 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2686 put_record( $record, $out );
2691 sub script_transliterate_seq
2693 # Martin A. Hansen, August 2007.
2695 # Transliterate chars from sequence in record.
2697 my ( $in, # handle to in stream
2698 $out, # handle to out stream
2699 $options, # options hash
2704 my ( $record, $search, $replace, $delete );
2706 $search = $options->{ "search" } || "";
2707 $replace = $options->{ "replace" } || "";
2708 $delete = $options->{ "delete" } || "";
2710 while ( $record = get_record( $in ) )
2712 if ( $record->{ "SEQ" } )
2714 if ( $search and $replace ) {
2715 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2716 } elsif ( $delete ) {
2717 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2721 put_record( $record, $out );
2726 sub script_transliterate_vals
2728 # Martin A. Hansen, April 2008.
2730 # Transliterate chars from values in record.
2732 my ( $in, # handle to in stream
2733 $out, # handle to out stream
2734 $options, # options hash
2739 my ( $record, $search, $replace, $delete, $key );
2741 $search = $options->{ "search" } || "";
2742 $replace = $options->{ "replace" } || "";
2743 $delete = $options->{ "delete" } || "";
2745 while ( $record = get_record( $in ) )
2747 foreach $key ( @{ $options->{ "keys" } } )
2749 if ( exists $record->{ $key } )
2751 if ( $search and $replace ) {
2752 eval "\$record->{ $key } =~ tr/$search/$replace/";
2753 } elsif ( $delete ) {
2754 eval "\$record->{ $key } =~ tr/$delete//d";
2759 put_record( $record, $out );
2764 sub script_translate_seq
2766 # Martin A. Hansen, February 2008.
2768 # Translate DNA sequence into protein sequence.
2770 my ( $in, # handle to in stream
2771 $out, # handle to out stream
2772 $options, # options hash
2777 my ( $record, $frame, %new_record );
2779 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2781 while ( $record = get_record( $in ) )
2783 if ( $record->{ "SEQ" } )
2785 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2787 foreach $frame ( @{ $options->{ "frames" } } )
2789 %new_record = %{ $record };
2791 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2792 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2793 $new_record{ "FRAME" } = $frame;
2795 put_record( \%new_record, $out );
2801 put_record( $record, $out );
2807 sub script_extract_seq
2809 # Martin A. Hansen, August 2007.
2811 # Extract subsequences from sequences in record.
2813 my ( $in, # handle to in stream
2814 $out, # handle to out stream
2815 $options, # options hash
2820 my ( $beg, $end, $len, $record );
2822 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2825 $beg = $options->{ "beg" } - 1; # correcting for start offset
2828 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2830 } elsif ( defined $options->{ "end" } ) {
2831 $end = $options->{ "end" } - 1; # correcting for start offset
2834 $len = $options->{ "len" };
2836 # print "beg->$beg, end->$end, len->$len\n";
2838 while ( $record = get_record( $in ) )
2840 if ( $record->{ "SEQ" } )
2842 if ( defined $beg and defined $end )
2844 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2845 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2847 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2850 elsif ( defined $beg and defined $len )
2852 if ( $len > length $record->{ "SEQ" } ) {
2853 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2855 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2858 elsif ( defined $beg )
2860 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2864 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2866 put_record( $record, $out );
2871 sub script_get_genome_seq
2873 # Martin A. Hansen, December 2007.
2875 # Gets a subsequence from a genome.
2877 my ( $in, # handle to in stream
2878 $out, # handle to out stream
2879 $options, # options hash
2884 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2886 $options->{ "flank" } ||= 0;
2888 if ( $options->{ "genome" } )
2890 $genome = $options->{ "genome" };
2892 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2893 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2895 $fh = Maasha::Common::read_open( $genome_file );
2896 $index = Maasha::Fasta::index_retrieve( $index_file );
2898 shift @{ $index }; # Get rid of the file size info
2900 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2902 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2904 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2906 $beg = $index_beg + $options->{ "beg" } - 1;
2908 if ( $options->{ "len" } ) {
2909 $len = $options->{ "len" };
2910 } elsif ( $options->{ "end" } ) {
2911 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2914 $beg -= $options->{ "flank" };
2915 $len += 2 * $options->{ "flank" };
2917 if ( $beg <= $index_beg )
2919 $len -= $index_beg - $beg;
2923 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2925 next if $beg > $index_beg + $index_len;
2927 $record->{ "CHR" } = $options->{ "chr" };
2928 $record->{ "CHR_BEG" } = $beg - $index_beg;
2929 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2931 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2932 $record->{ "SEQ_LEN" } = $len;
2934 put_record( $record, $out );
2938 while ( $record = get_record( $in ) )
2940 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2942 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2944 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2946 $beg = $record->{ "CHR_BEG" } + $index_beg;
2947 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2949 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2951 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2953 $beg = $record->{ "S_BEG" } + $index_beg;
2954 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2956 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2958 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2960 $beg = $record->{ "S_BEG" } + $index_beg;
2961 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2964 $beg -= $options->{ "flank" };
2965 $len += 2 * $options->{ "flank" };
2967 if ( $beg <= $index_beg )
2969 $len -= $index_beg - $beg;
2973 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2975 next if $beg > $index_beg + $index_len;
2977 $record->{ "CHR_BEG" } = $beg - $index_beg;
2978 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2980 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2982 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2984 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2985 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2988 if ( $options->{ "mask" } )
2990 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2992 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2994 @begs = split ",", $record->{ "Q_BEGS" };
2995 @lens = split ",", $record->{ "BLOCKSIZES" };
2997 for ( $i = 0; $i < @begs; $i++ ) {
2998 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3004 put_record( $record, $out );
3011 sub script_get_genome_align
3013 # Martin A. Hansen, April 2008.
3015 # Gets a subalignment from a multiple genome alignment.
3017 my ( $in, # handle to in stream
3018 $out, # handle to out stream
3019 $options, # options hash
3024 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3026 $options->{ "strand" } ||= "+";
3030 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3032 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3034 $beg = $options->{ "beg" } - 1;
3036 if ( $options->{ "end" } ) {
3037 $end = $options->{ "end" };
3038 } elsif ( $options->{ "len" } ) {
3039 $end = $beg + $options->{ "len" };
3042 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3044 foreach $entry ( @{ $align } )
3046 $entry->{ "CHR" } = $record->{ "CHR" };
3047 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3048 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3049 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3050 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3051 $entry->{ "SCORE" } = $record->{ "SCORE" };
3053 put_record( $entry, $out );
3057 while ( $record = get_record( $in ) )
3059 if ( $record->{ "REC_TYPE" } eq "BED" )
3061 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3063 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3065 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3067 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3069 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3072 foreach $entry ( @{ $align } )
3074 $entry->{ "CHR" } = $record->{ "CHR" };
3075 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3076 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3077 $entry->{ "STRAND" } = $record->{ "STRAND" };
3078 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3079 $entry->{ "SCORE" } = $record->{ "SCORE" };
3081 put_record( $entry, $out );
3089 sub script_get_genome_phastcons
3091 # Martin A. Hansen, February 2008.
3093 # Get phastcons scores from genome intervals.
3095 my ( $in, # handle to in stream
3096 $out, # handle to out stream
3097 $options, # options hash
3102 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3104 $options->{ "flank" } ||= 0;
3106 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3107 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3109 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3110 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3112 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3114 $options->{ "beg" } -= 1; # request is 1-based
3115 $options->{ "end" } -= 1; # request is 1-based
3117 if ( $options->{ "len" } ) {
3118 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3121 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3123 $record->{ "CHR" } = $options->{ "chr" };
3124 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3125 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3127 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3128 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3130 put_record( $record, $out );
3133 while ( $record = get_record( $in ) )
3135 if ( $record->{ "REC_TYPE" } eq "BED" )
3137 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3139 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3141 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3143 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3145 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3148 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3149 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3151 put_record( $record, $out );
3154 close $fh_phastcons if $fh_phastcons;
3160 # Martin A. Hansen, December 2007.
3162 # Folds sequences in stream into secondary structures.
3164 my ( $in, # handle to in stream
3165 $out, # handle to out stream
3170 my ( $record, $type, $struct, $index );
3172 while ( $record = get_record( $in ) )
3174 if ( $record->{ "SEQ" } )
3177 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3180 if ( $type ne "protein" )
3182 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3183 $record->{ "SEC_STRUCT" } = $struct;
3184 $record->{ "FREE_ENERGY" } = $index;
3185 $record->{ "SCORE" } = abs int $index;
3186 $record->{ "SIZE" } = length $struct;
3187 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3191 put_record( $record, $out );
3196 sub script_split_seq
3198 # Martin A. Hansen, August 2007.
3200 # Split a sequence in stream into words.
3202 my ( $in, # handle to in stream
3203 $out, # handle to out stream
3204 $options, # options hash
3209 my ( $record, $new_record, $i, $subseq, %lookup );
3211 $options->{ "word_size" } ||= 7;
3213 while ( $record = get_record( $in ) )
3215 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3217 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3219 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3221 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3223 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3224 $new_record->{ "SEQ" } = $subseq;
3226 put_record( $new_record, $out );
3228 $lookup{ $subseq } = 1;
3232 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3233 $new_record->{ "SEQ" } = $subseq;
3235 put_record( $new_record, $out );
3241 put_record( $record, $out );
3247 sub script_split_bed
3249 # Martin A. Hansen, June 2008.
3251 # Split a BED record into overlapping windows.
3253 my ( $in, # handle to in stream
3254 $out, # handle to out stream
3255 $options, # options hash
3260 my ( $record, $new_record, $i );
3262 $options->{ "window_size" } ||= 20;
3263 $options->{ "step_size" } ||= 1;
3265 while ( $record = get_record( $in ) )
3267 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3269 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3271 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3273 $new_record->{ "REC_TYPE" } = "BED";
3274 $new_record->{ "CHR" } = $record->{ "CHR" };
3275 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3276 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3277 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3278 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3279 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3280 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3282 put_record( $new_record, $out );
3287 put_record( $record, $out );
3293 sub script_align_seq
3295 # Martin A. Hansen, August 2007.
3297 # Align sequences in stream.
3299 my ( $in, # handle to in stream
3300 $out, # handle to out stream
3305 my ( $record, @entries, $entry );
3307 while ( $record = get_record( $in ) )
3309 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3310 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3311 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3312 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3314 put_record( $record, $out );
3318 @entries = Maasha::Align::align( \@entries );
3320 foreach $entry ( @entries )
3322 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3325 SEQ_NAME => $entry->[ SEQ_NAME ],
3326 SEQ => $entry->[ SEQ ],
3329 put_record( $record, $out );
3337 # Martin A. Hansen, February 2008.
3339 # Using the first sequence in stream as reference, tile
3340 # all subsequent sequences based on pairwise alignments.
3342 my ( $in, # handle to in stream
3343 $out, # handle to out stream
3344 $options, # options hash
3349 my ( $record, $first, $ref_entry, @entries );
3353 while ( $record = get_record( $in ) )
3355 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3359 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3365 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3370 put_record( $record, $out );
3374 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3376 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3380 sub script_invert_align
3382 # Martin A. Hansen, February 2008.
3384 # Inverts an alignment showing only non-mathing residues
3385 # using the first sequence as reference.
3387 my ( $in, # handle to in stream
3388 $out, # handle to out stream
3389 $options, # options hash
3394 my ( $record, @entries );
3396 while ( $record = get_record( $in ) )
3398 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3400 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3404 put_record( $record, $out );
3408 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3410 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3414 sub script_patscan_seq
3416 # Martin A. Hansen, August 2007.
3418 # Locates patterns in sequences using scan_for_matches.
3420 my ( $in, # handle to in stream
3421 $out, # handle to out stream
3422 $options, # options hash
3427 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3429 if ( $options->{ "patterns" } ) {
3430 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3431 } elsif ( -f $options->{ "patterns_in" } ) {
3432 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3435 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3437 push @args, "-c" if $options->{ "comp" };
3438 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3439 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3441 $seq_file = "$BP_TMP/patscan.seq";
3442 $pat_file = "$BP_TMP/patscan.pat";
3443 $out_file = "$BP_TMP/patscan.out";
3445 $fh_out = Maasha::Common::write_open( $seq_file );
3449 while ( $record = get_record( $in ) )
3451 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3453 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3455 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3457 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3465 $arg = join " ", @args;
3466 $arg .= " -p" if $type eq "protein";
3468 foreach $pattern ( @{ $patterns } )
3470 $fh_out = Maasha::Common::write_open( $pat_file );
3472 print $fh_out "$pattern\n";
3476 if ( $options->{ 'genome' } ) {
3477 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3478 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3480 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3481 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3484 $fh_in = Maasha::Common::read_open( $out_file );
3486 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3488 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3490 if ( $options->{ 'genome' } )
3492 $result->{ "CHR" } = $result->{ "S_ID" };
3493 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3494 $result->{ "CHR_END" } = $result->{ "S_END" };
3496 delete $result->{ "S_ID" };
3497 delete $result->{ "S_BEG" };
3498 delete $result->{ "S_END" };
3502 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3505 put_record( $result, $out );
3517 sub script_create_blast_db
3519 # Martin A. Hansen, September 2007.
3521 # Creates a NCBI BLAST database with formatdb
3523 my ( $in, # handle to in stream
3524 $out, # handle to out stream
3525 $options, # options hash
3530 my ( $fh, $seq_type, $path, $record, $entry );
3532 $path = $options->{ "database" };
3534 $fh = Maasha::Common::write_open( $path );
3536 while ( $record = get_record( $in ) )
3538 put_record( $record, $out ) if not $options->{ "no_stream" };
3540 if ( $entry = record2fasta( $record ) )
3542 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3544 Maasha::Fasta::put_entry( $entry, $fh );
3550 if ( $seq_type eq "protein" ) {
3551 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3553 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3560 sub script_blast_seq
3562 # Martin A. Hansen, September 2007.
3564 # BLASTs sequences in stream against a given database.
3566 my ( $in, # handle to in stream
3567 $out, # handle to out stream
3568 $options, # options hash
3573 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3575 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3576 $options->{ "filter" } = "F";
3577 $options->{ "filter" } = "T" if $options->{ "filter" };
3578 $options->{ "cpus" } ||= 1;
3580 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3582 $tmp_in = "$BP_TMP/blast_query.seq";
3583 $tmp_out = "$BP_TMP/blast.result";
3585 $fh_out = Maasha::Common::write_open( $tmp_in );
3587 while ( $record = get_record( $in ) )
3589 if ( $entry = record2fasta( $record ) )
3591 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3593 Maasha::Fasta::put_entry( $entry, $fh_out );
3596 put_record( $record, $out );
3601 if ( -f $options->{ 'database' } . ".phr" ) {
3602 $s_type = "protein";
3604 $s_type = "nucleotide";
3607 if ( not $options->{ 'program' } )
3609 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3610 $options->{ 'program' } = "blastn";
3611 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3612 $options->{ 'program' } = "blastp";
3613 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3614 $options->{ 'program' } = "blastx";
3615 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3616 $options->{ 'program' } = "tblastn";
3620 if ( $options->{ 'verbose' } )
3622 Maasha::Common::run(
3625 "-p $options->{ 'program' }",
3626 "-e $options->{ 'e_val' }",
3627 "-a $options->{ 'cpus' }",
3630 "-d $options->{ 'database' }",
3631 "-F $options->{ 'filter' }",
3639 Maasha::Common::run(
3642 "-p $options->{ 'program' }",
3643 "-e $options->{ 'e_val' }",
3644 "-a $options->{ 'cpus' }",
3647 "-d $options->{ 'database' }",
3648 "-F $options->{ 'filter' }",
3658 $fh_out = Maasha::Common::read_open( $tmp_out );
3662 while ( $line = <$fh_out> )
3666 next if $line =~ /^#/;
3668 @fields = split /\s+/, $line;
3670 $record->{ "REC_TYPE" } = "BLAST";
3671 $record->{ "Q_ID" } = $fields[ 0 ];
3672 $record->{ "S_ID" } = $fields[ 1 ];
3673 $record->{ "IDENT" } = $fields[ 2 ];
3674 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3675 $record->{ "MISMATCHES" } = $fields[ 4 ];
3676 $record->{ "GAPS" } = $fields[ 5 ];
3677 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3678 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3679 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3680 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3681 $record->{ "E_VAL" } = $fields[ 10 ];
3682 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3684 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3686 $record->{ "STRAND" } = '-';
3688 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3692 $record->{ "STRAND" } = '+';
3695 put_record( $record, $out );
3706 # Martin A. Hansen, August 2007.
3708 # BLATs sequences in stream against a given genome.
3710 my ( $in, # handle to in stream
3711 $out, # handle to out stream
3712 $options, # options hash
3717 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3719 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3721 $options->{ 'tile_size' } ||= 11;
3722 $options->{ 'one_off' } ||= 0;
3723 $options->{ 'min_identity' } ||= 90;
3724 $options->{ 'min_score' } ||= 0;
3725 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3727 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3728 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3729 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3730 $blat_args .= " -minScore=$options->{ 'min_score' }";
3731 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3732 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3734 $query_file = "$BP_TMP/blat.seq";
3736 $fh_out = Maasha::Common::write_open( $query_file );
3738 while ( $record = get_record( $in ) )
3740 if ( $entry = record2fasta( $record ) )
3742 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3743 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3746 put_record( $record, $out );
3751 $blat_args .= " -t=dnax" if $type eq "protein";
3752 $blat_args .= " -q=$type";
3754 $result_file = "$BP_TMP/blat.psl";
3756 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3760 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3762 map { put_record( $_, $out ) } @{ $entries };
3764 unlink $result_file;
3770 # Martin A. Hansen, July 2008.
3772 # soap sequences in stream against a given file or genome.
3774 my ( $in, # handle to in stream
3775 $out, # handle to out stream
3776 $options, # options hash
3781 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3783 $options->{ "seed_size" } ||= 10;
3784 $options->{ "mismatches" } ||= 2;
3785 $options->{ "gap_size" } ||= 0;
3786 $options->{ "cpus" } ||= 1;
3788 if ( $options->{ "genome" } ) {
3789 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3792 $tmp_in = "$BP_TMP/soap_query.seq";
3793 $tmp_out = "$BP_TMP/soap.result";
3795 $fh_out = Maasha::Common::write_open( $tmp_in );
3799 while ( $record = get_record( $in ) )
3801 if ( $entry = record2fasta( $record ) )
3803 Maasha::Fasta::put_entry( $entry, $fh_out );
3808 put_record( $record, $out );
3816 "-s $options->{ 'seed_size' }",
3819 "-v $options->{ 'mismatches' }",
3820 "-g $options->{ 'gap_size' }",
3821 "-p $options->{ 'cpus' }",
3822 "-d $options->{ 'in_file' }",
3826 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3828 Maasha::Common::run( "soap", $args, 1 );
3832 $fh_out = Maasha::Common::read_open( $tmp_out );
3836 while ( $line = <$fh_out> )
3840 @fields = split /\t/, $line;
3842 $record->{ "REC_TYPE" } = "SOAP";
3843 $record->{ "Q_ID" } = $fields[ 0 ];
3844 $record->{ "SCORE" } = $fields[ 3 ];
3845 $record->{ "STRAND" } = $fields[ 6 ];
3846 $record->{ "S_ID" } = $fields[ 7 ];
3847 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3848 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3850 put_record( $record, $out );
3860 sub script_match_seq
3862 # Martin A. Hansen, August 2007.
3864 # BLATs sequences in stream against a given genome.
3866 my ( $in, # handle to in stream
3867 $out, # handle to out stream
3868 $options, # options hash
3873 my ( $record, @entries, $results );
3875 $options->{ "word_size" } ||= 20;
3876 $options->{ "direction" } ||= "both";
3878 while ( $record = get_record( $in ) )
3880 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3881 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3884 put_record( $record, $out );
3887 if ( @entries == 1 )
3889 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3891 map { put_record( $_, $out ) } @{ $results };
3893 elsif ( @entries == 2 )
3895 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3897 map { put_record( $_, $out ) } @{ $results };
3902 sub script_create_vmatch_index
3904 # Martin A. Hansen, January 2008.
3906 # Create a vmatch index from sequences in the stream.
3908 my ( $in, # handle to in stream
3909 $out, # handle to out stream
3910 $options, # options hash
3915 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3917 if ( $options->{ "index_name" } )
3919 $file_tmp = $options->{ 'index_name' };
3920 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3923 while ( $record = get_record( $in ) )
3925 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3927 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3929 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3932 put_record( $record, $out ) if not $options->{ "no_stream" };
3935 if ( $options->{ "index_name" } )
3939 if ( $type eq "protein" ) {
3940 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3942 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3950 sub script_vmatch_seq
3952 # Martin A. Hansen, August 2007.
3954 # Vmatches sequences in stream against a given genome.
3956 my ( $in, # handle to in stream
3957 $out, # handle to out stream
3958 $options, # options hash
3963 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3965 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3967 if ( $options->{ "index_name" } )
3969 @index_files = $options->{ "index_name" };
3973 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3975 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3977 @index_files = sort keys %hash;
3980 while ( $record = get_record( $in ) )
3982 push @records, $record;
3984 put_record( $record, $out );
3987 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3991 $fh_in = Maasha::Common::read_open( $result_file );
3993 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3994 put_record( $record, $out );
3999 unlink $result_file;
4003 sub script_write_fasta
4005 # Martin A. Hansen, August 2007.
4007 # Write FASTA entries from sequences in stream.
4009 my ( $in, # handle to in stream
4010 $out, # handle to out stream
4011 $options, # options hash
4016 my ( $record, $fh, $entry );
4018 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4020 while ( $record = get_record( $in ) )
4022 if ( $entry = record2fasta( $record ) ) {
4023 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4026 put_record( $record, $out ) if not $options->{ "no_stream" };
4033 sub script_write_align
4035 # Martin A. Hansen, August 2007.
4037 # Write pretty alignments aligned sequences in stream.
4039 my ( $in, # handle to in stream
4040 $out, # handle to out stream
4041 $options, # options hash
4046 my ( $fh, $record, @entries );
4048 $fh = write_stream( $options->{ "data_out" } ) ;
4050 while ( $record = get_record( $in ) )
4052 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4053 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4056 put_record( $record, $out ) if not $options->{ "no_stream" };
4059 if ( scalar( @entries ) == 2 ) {
4060 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4061 } elsif ( scalar ( @entries ) > 2 ) {
4062 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4069 sub script_write_blast
4071 # Martin A. Hansen, November 2007.
4073 # Write data in blast table format (-m8 and 9).
4075 my ( $in, # handle to in stream
4076 $out, # handle to out stream
4077 $options, # options hash
4082 my ( $fh, $record, $first );
4084 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4088 while ( $record = get_record( $in ) )
4090 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4092 if ( $options->{ "comment" } and $first )
4094 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";
4099 if ( $record->{ "STRAND" } eq "-" ) {
4100 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4103 print $fh join( "\t",
4104 $record->{ "Q_ID" },
4105 $record->{ "S_ID" },
4106 $record->{ "IDENT" },
4107 $record->{ "ALIGN_LEN" },
4108 $record->{ "MISMATCHES" },
4109 $record->{ "GAPS" },
4110 $record->{ "Q_BEG" } + 1,
4111 $record->{ "Q_END" } + 1,
4112 $record->{ "S_BEG" } + 1,
4113 $record->{ "S_END" } + 1,
4114 $record->{ "E_VAL" },
4115 $record->{ "BIT_SCORE" }
4119 put_record( $record, $out ) if not $options->{ "no_stream" };
4126 sub script_write_tab
4128 # Martin A. Hansen, August 2007.
4130 # Write data as table.
4132 my ( $in, # handle to in stream
4133 $out, # handle to out stream
4134 $options, # options hash
4139 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4141 $options->{ "delimit" } ||= "\t";
4143 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4145 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4147 while ( $record = get_record( $in ) )
4152 if ( $options->{ "keys" } )
4154 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4158 foreach $key ( @{ $options->{ "keys" } } )
4160 if ( exists $record->{ $key } )
4162 push @keys, $key if $options->{ "comment" };
4163 push @vals, $record->{ $key };
4170 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4172 next if exists $no_keys{ $key };
4174 push @keys, $key if $options->{ "comment" };
4175 push @vals, $record->{ $key };
4179 if ( @keys and $options->{ "comment" } )
4181 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4183 delete $options->{ "comment" };
4186 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4188 put_record( $record, $out ) if not $options->{ "no_stream" };
4195 sub script_write_bed
4197 # Martin A. Hansen, August 2007.
4199 # Write BED format for the UCSC genome browser using records in stream.
4201 my ( $in, # handle to in stream
4202 $out, # handle to out stream
4203 $options, # options hash
4208 my ( $fh, $record, $new_record );
4210 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4212 while ( $record = get_record( $in ) )
4214 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4216 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4218 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4220 $new_record->{ "CHR" } = $record->{ "S_ID" };
4221 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4222 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4223 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4224 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4225 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4227 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4229 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4231 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4233 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4235 $new_record->{ "CHR" } = $record->{ "S_ID" };
4236 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4237 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4238 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4239 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4240 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4242 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4244 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4246 $new_record->{ "CHR" } = $record->{ "S_ID" };
4247 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4248 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4249 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4250 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4251 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4253 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4255 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4257 $new_record->{ "CHR" } = $record->{ "S_ID" };
4258 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4259 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4260 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4261 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4262 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4264 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4266 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4268 Maasha::UCSC::bed_put_entry( $record, $fh );
4271 put_record( $record, $out ) if not $options->{ "no_stream" };
4278 sub script_write_psl
4280 # Martin A. Hansen, August 2007.
4282 # Write PSL output from stream.
4284 my ( $in, # handle to in stream
4285 $out, # handle to out stream
4286 $options, # options hash
4291 my ( $fh, $record, @output, $first );
4295 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4297 while ( $record = get_record( $in ) )
4299 put_record( $record, $out ) if not $options->{ "no_stream" };
4301 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4303 Maasha::UCSC::psl_put_header( $fh ) if $first;
4304 Maasha::UCSC::psl_put_entry( $record, $fh );
4313 sub script_write_fixedstep
4315 # Martin A. Hansen, Juli 2008.
4317 # Write fixedStep entries from recrods in the stream.
4319 my ( $in, # handle to in stream
4320 $out, # handle to out stream
4321 $options, # options hash
4326 my ( $fh, $record, $vals );
4328 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4330 while ( $record = get_record( $in ) )
4332 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4334 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4336 $vals = $record->{ 'VALS' };
4340 print $fh "$vals\n";
4343 put_record( $record, $out ) if not $options->{ "no_stream" };
4350 sub script_write_2bit
4352 # Martin A. Hansen, March 2008.
4354 # Write sequence entries from stream in 2bit format.
4356 my ( $in, # handle to in stream
4357 $out, # handle to out stream
4358 $options, # options hash
4363 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4365 $mask = 1 if not $options->{ "no_mask" };
4367 $tmp_file = "$BP_TMP/write_2bit.fna";
4368 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4370 $fh_out = write_stream( $options->{ "data_out" } );
4372 while ( $record = get_record( $in ) )
4374 if ( $entry = record2fasta( $record ) ) {
4375 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4378 put_record( $record, $out ) if not $options->{ "no_stream" };
4383 $fh_in = Maasha::Common::read_open( $tmp_file );
4385 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4394 sub script_write_solid
4396 # Martin A. Hansen, April 2008.
4398 # Write di-base encoded Solid sequence from entries in stream.
4400 my ( $in, # handle to in stream
4401 $out, # handle to out stream
4402 $options, # options hash
4407 my ( $record, $fh, $entry );
4409 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4411 while ( $record = get_record( $in ) )
4413 if ( $entry = record2fasta( $record ) )
4415 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4417 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4420 put_record( $record, $out ) if not $options->{ "no_stream" };
4427 sub script_plot_seqlogo
4429 # Martin A. Hansen, August 2007.
4431 # Calculates and writes a sequence logo for alignments.
4433 my ( $in, # handle to in stream
4434 $out, # handle to out stream
4435 $options, # options hash
4440 my ( $record, @entries, $logo, $fh );
4442 while ( $record = get_record( $in ) )
4444 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4445 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4448 put_record( $record, $out ) if not $options->{ "no_stream" };
4451 $logo = Maasha::Plot::seq_logo( \@entries );
4453 $fh = write_stream( $options->{ "data_out" } );
4461 sub script_plot_phastcons_profiles
4463 # Martin A. Hansen, January 2008.
4465 # Plots PhastCons profiles.
4467 my ( $in, # handle to in stream
4468 $out, # handle to out stream
4469 $options, # options hash
4474 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4476 $options->{ "title" } ||= "PhastCons Profiles";
4478 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4479 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4481 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4482 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4484 while ( $record = get_record( $in ) )
4486 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4488 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4489 $record->{ "CHR_BEG" },
4490 $record->{ "CHR_END" },
4491 $options->{ "flank" } );
4493 push @{ $AoA }, [ @{ $scores } ];
4496 put_record( $record, $out ) if not $options->{ "no_stream" };
4499 Maasha::UCSC::phastcons_normalize( $AoA );
4501 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4502 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4504 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4506 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4508 $fh = write_stream( $options->{ "data_out" } );
4510 print $fh "$_\n" foreach @{ $plot };
4516 sub script_analyze_bed
4518 # Martin A. Hansen, March 2008.
4520 # Analyze BED entries in stream.
4522 my ( $in, # handle to in stream
4523 $out, # handle to out stream
4524 $options, # options hash
4531 while ( $record = get_record( $in ) )
4533 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4535 put_record( $record, $out );
4540 sub script_analyze_vals
4542 # Martin A. Hansen, August 2007.
4544 # Analyze values for given keys in stream.
4546 my ( $in, # handle to in stream
4547 $out, # handle to out stream
4548 $options, # options hash
4553 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4555 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4557 while ( $record = get_record( $in ) )
4559 foreach $key ( keys %{ $record } )
4561 next if $options->{ "keys" } and not exists $key_hash{ $key };
4563 $analysis->{ $key }->{ "COUNT" }++;
4565 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4567 $analysis->{ $key }->{ "TYPE" } = "num";
4568 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4569 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4570 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4574 $len = length $record->{ $key };
4576 $analysis->{ $key }->{ "TYPE" } = "alph";
4577 $analysis->{ $key }->{ "SUM" } += $len;
4578 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4579 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4583 put_record( $record, $out ) if not $options->{ "no_stream" };
4586 foreach $key ( keys %{ $analysis } )
4588 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4589 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4592 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4602 if ( $options->{ "keys" } ) {
4603 @keys = @{ $options->{ "keys" } };
4605 @keys = keys %{ $analysis };
4608 foreach $key ( @keys )
4610 $keys .= sprintf "% 15s", $key;
4611 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4612 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4613 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4614 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4615 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4616 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4619 print $out "$keys\n";
4620 print $out "$types\n";
4621 print $out "$counts\n";
4622 print $out "$mins\n";
4623 print $out "$maxs\n";
4624 print $out "$sums\n";
4625 print $out "$means\n";
4629 sub script_head_records
4631 # Martin A. Hansen, August 2007.
4633 # Display the first sequences in stream.
4635 my ( $in, # handle to in stream
4636 $out, # handle to out stream
4637 $options, # options hash
4642 my ( $record, $count );
4644 $options->{ "num" } ||= 10;
4648 while ( $record = get_record( $in ) )
4652 put_record( $record, $out );
4654 last if $count == $options->{ "num" };
4659 sub script_remove_keys
4661 # Martin A. Hansen, August 2007.
4663 # Remove keys from stream.
4665 my ( $in, # handle to in stream
4666 $out, # handle to out stream
4667 $options, # options hash
4672 my ( $record, $new_record );
4674 while ( $record = get_record( $in ) )
4676 if ( $options->{ "keys" } )
4678 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4680 elsif ( $options->{ "save_keys" } )
4682 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4684 $record = $new_record;
4687 put_record( $record, $out ) if keys %{ $record };
4692 sub script_remove_adaptor
4694 # Martin A. Hansen, August 2008.
4696 # Find and remove adaptor from sequences in the stream.
4698 my ( $in, # handle to in stream
4699 $out, # handle to out stream
4700 $options, # options hash
4705 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4707 $options->{ "remove" } ||= "after";
4709 $max_mismatch = $options->{ "mismatches" } || 0;
4710 $offset = $options->{ "offset" };
4712 if ( not defined $offset ) {
4718 $adaptor = uc $options->{ "adaptor" };
4719 $adaptor_len = length $adaptor;
4721 while ( $record = get_record( $in ) )
4723 if ( $record->{ "SEQ" } )
4725 $seq = uc $record->{ "SEQ" };
4726 $seq_len = length $seq;
4728 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4730 $record->{ "ADAPTOR_POS" } = $pos;
4732 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4734 if ( $options->{ "remove" } eq "after" )
4736 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4737 $record->{ "SEQ_LEN" } = $pos;
4741 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4742 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4746 put_record( $record, $out );
4750 put_record( $record, $out );
4756 sub script_rename_keys
4758 # Martin A. Hansen, August 2007.
4760 # Rename keys in stream.
4762 my ( $in, # handle to in stream
4763 $out, # handle to out stream
4764 $options, # options hash
4771 while ( $record = get_record( $in ) )
4773 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4775 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4777 delete $record->{ $options->{ "keys" }->[ 0 ] };
4780 put_record( $record, $out );
4785 sub script_uniq_vals
4787 # Martin A. Hansen, August 2007.
4789 # Find unique values in stream.
4791 my ( $in, # handle to in stream
4792 $out, # handle to out stream
4793 $options, # options hash
4798 my ( %hash, $record );
4800 while ( $record = get_record( $in ) )
4802 if ( $record->{ $options->{ "key" } } )
4804 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4806 put_record( $record, $out );
4808 $hash{ $record->{ $options->{ "key" } } } = 1;
4810 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4812 put_record( $record, $out );
4816 $hash{ $record->{ $options->{ "key" } } } = 1;
4821 put_record( $record, $out );
4827 sub script_merge_vals
4829 # Martin A. Hansen, August 2007.
4831 # Rename keys in stream.
4833 my ( $in, # handle to in stream
4834 $out, # handle to out stream
4835 $options, # options hash
4840 my ( $record, @join, $i );
4842 $options->{ "delimit" } ||= '_';
4844 while ( $record = get_record( $in ) )
4846 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4848 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4850 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4851 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4854 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4857 put_record( $record, $out );
4862 sub script_merge_records
4864 # Martin A. Hansen, July 2008.
4866 # Merges records in the stream based on identical values of two given keys.
4868 my ( $in, # handle to in stream
4869 $out, # handle to out stream
4870 $options, # options hash
4875 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4876 $num1, $num2, $num, $cmp, $i );
4878 $merge = $options->{ "merge" } || "AandB";
4880 $file1 = "$BP_TMP/merge_records1.tmp";
4881 $file2 = "$BP_TMP/merge_records2.tmp";
4883 $fh1 = Maasha::Common::write_open( $file1 );
4884 $fh2 = Maasha::Common::write_open( $file2 );
4886 $key1 = $options->{ "keys" }->[ 0 ];
4887 $key2 = $options->{ "keys" }->[ 1 ];
4889 $num = $key2 =~ s/n$//;
4893 while ( $record = get_record( $in ) )
4895 if ( exists $record->{ $key1 } )
4898 @vals1 = $record->{ $key1 };
4900 delete $record->{ $key1 };
4902 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4904 print $fh1 join( "\t", @vals1 ), "\n";
4908 elsif ( exists $record->{ $key2 } )
4911 @vals2 = $record->{ $key2 };
4913 delete $record->{ $key2 };
4915 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4917 print $fh2 join( "\t", @vals2 ), "\n";
4928 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4929 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4933 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4934 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4937 $fh1 = Maasha::Common::read_open( $file1 );
4938 $fh2 = Maasha::Common::read_open( $file2 );
4940 @vals1 = Maasha::Common::get_fields( $fh1 );
4941 @vals2 = Maasha::Common::get_fields( $fh2 );
4943 while ( $num1 > 0 and $num2 > 0 )
4948 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4950 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4955 if ( $merge =~ /^(AorB|AnotB)$/ )
4957 for ( $i = 0; $i < @keys1; $i++ ) {
4958 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4961 put_record( $record, $out );
4964 @vals1 = Maasha::Common::get_fields( $fh1 );
4969 if ( $merge =~ /^(BorA|BnotA)$/ )
4971 for ( $i = 0; $i < @keys2; $i++ ) {
4972 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4975 put_record( $record, $out );
4978 @vals2 = Maasha::Common::get_fields( $fh2 );
4983 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4985 for ( $i = 0; $i < @keys1; $i++ ) {
4986 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4989 for ( $i = 1; $i < @keys2; $i++ ) {
4990 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4993 put_record( $record, $out );
4996 @vals1 = Maasha::Common::get_fields( $fh1 );
4997 @vals2 = Maasha::Common::get_fields( $fh2 );
5009 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5013 for ( $i = 0; $i < @keys1; $i++ ) {
5014 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5017 put_record( $record, $out );
5020 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5024 for ( $i = 0; $i < @keys2; $i++ ) {
5025 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5028 put_record( $record, $out );
5035 # Martin A. Hansen, August 2007.
5037 # Grab for records in stream.
5039 my ( $in, # handle to in stream
5040 $out, # handle to out stream
5041 $options, # options hash
5046 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
5048 if ( $options->{ "patterns" } )
5050 $patterns = [ split ",", $options->{ "patterns" } ];
5052 elsif ( -f $options->{ "patterns_in" } )
5054 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
5056 elsif ( -f $options->{ "exact_in" } )
5058 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
5060 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5065 if ( $options->{ "eval" } )
5067 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5075 while ( $record = get_record( $in ) )
5081 if ( $options->{ "keys" } )
5083 foreach $key ( @{ $options->{ "keys" } } )
5085 if ( exists $lookup_hash{ $record->{ $key } } )
5094 foreach $key ( keys %{ $record } )
5096 if ( not $options->{ "vals_only" } )
5098 if ( exists $lookup_hash{ $key } )
5105 if ( not $options->{ "keys_only" } )
5107 if ( exists $lookup_hash{ $record->{ $key } } )
5118 foreach $pattern ( @{ $patterns } )
5120 if ( $options->{ "keys" } )
5122 foreach $key ( @{ $options->{ "keys" } } )
5124 $pos = index $record->{ $key }, $pattern;
5126 goto FOUND if $pos >= 0;
5131 foreach $key ( keys %{ $record } )
5133 if ( not $options->{ "vals_only" } )
5135 $pos = index $key, $pattern;
5137 goto FOUND if $pos >= 0;
5140 if ( not $options->{ "keys_only" } )
5142 $pos = index $record->{ $key }, $pattern;
5144 goto FOUND if $pos >= 0;
5150 elsif ( $options->{ "regex" } )
5152 if ( $options->{ "keys" } )
5154 foreach $key ( @{ $options->{ "keys" } } )
5156 if ( $options->{ "case_insensitive" } ) {
5157 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5159 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5162 goto FOUND if $pos >= 0;
5167 foreach $key ( keys %{ $record } )
5169 if ( not $options->{ "vals_only" } )
5171 if ( $options->{ "case_insensitive" } ) {
5172 $pos = 1 if $key =~ /$options->{'regex'}/i;
5174 $pos = 1 if $key =~ /$options->{'regex'}/;
5177 goto FOUND if $pos >= 0;
5180 if ( not $options->{ "keys_only" } )
5182 if ( $options->{ "case_insensitive" } ) {
5183 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5185 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5188 goto FOUND if $pos >= 0;
5193 elsif ( $options->{ "eval" } )
5195 if ( defined $record->{ $key } )
5197 if ( $op eq "<" and $record->{ $key } < $val ) {
5198 $pos = 1 and goto FOUND;
5199 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
5200 $pos = 1 and goto FOUND;
5201 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
5202 $pos = 1 and goto FOUND;
5203 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
5204 $pos = 1 and goto FOUND;
5205 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
5206 $pos = 1 and goto FOUND;
5207 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
5208 $pos = 1 and goto FOUND;
5209 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
5210 $pos = 1 and goto FOUND;
5211 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5212 $pos = 1 and goto FOUND;
5219 if ( $pos >= 0 and not $options->{ "invert" } ) {
5220 put_record( $record, $out );
5221 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5222 put_record( $record, $out );
5230 # Martin A. Hansen, August 2007.
5232 # Evaluate extression for records in stream.
5234 my ( $in, # handle to in stream
5235 $out, # handle to out stream
5236 $options, # options hash
5241 my ( $record, $eval_key, $eval_val, $check, @keys );
5243 while ( $record = get_record( $in ) )
5245 if ( $options->{ "eval" } )
5247 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5255 @keys = split /\W+/, $eval_val;
5256 @keys = grep { ! /^\d+$/ } @keys;
5261 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5263 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5266 put_record( $record, $out );
5273 # Martin A. Hansen, June 2008.
5277 my ( $in, # handle to in stream
5278 $out, # handle to out stream
5279 $options, # options hash
5284 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5286 while ( $record = get_record( $in ) )
5290 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5292 push @rows, $record->{ $key };
5296 push @matrix, [ @rows ];
5301 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5303 foreach $row ( @matrix )
5305 for ( $i = 0; $i < @{ $row }; $i++ ) {
5306 $record->{ "V$i" } = $row->[ $i ];
5309 put_record( $record, $out );
5314 sub script_add_ident
5316 # Martin A. Hansen, May 2008.
5318 # Add a unique identifier to each record in stream.
5320 my ( $in, # handle to in stream
5321 $out, # handle to out stream
5322 $options, # options hash
5327 my ( $record, $key, $prefix, $i );
5329 $key = $options->{ "key" } || "ID";
5330 $prefix = $options->{ "prefix" } || "ID";
5334 while ( $record = get_record( $in ) )
5336 $record->{ $key } = sprintf( "$prefix%08d", $i );
5338 put_record( $record, $out );
5345 sub script_count_records
5347 # Martin A. Hansen, August 2007.
5349 # Count records in stream.
5351 my ( $in, # handle to in stream
5352 $out, # handle to out stream
5353 $options, # options hash
5358 my ( $record, $count, $result, $fh, $line );
5362 if ( $options->{ "no_stream" } )
5364 while ( $line = <$in> )
5368 $count++ if $line eq "---";
5373 while ( $record = get_record( $in ) )
5375 put_record( $record, $out );
5381 $result = { "RECORDS_COUNT" => $count };
5383 $fh = write_stream( $options->{ "data_out" } );
5385 put_record( $result, $fh );
5391 sub script_random_records
5393 # Martin A. Hansen, August 2007.
5395 # Pick a number or random records from stream.
5397 my ( $in, # handle to in stream
5398 $out, # handle to out stream
5399 $options, # options hash
5404 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5406 $options->{ "num" } ||= 10;
5408 $tmp_file = "$BP_TMP/random_records.tmp";
5410 $fh_out = Maasha::Common::write_open( $tmp_file );
5414 while ( $record = get_record( $in ) )
5416 put_record( $record, $fh_out );
5426 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5428 while ( $i < $options->{ "num" } )
5430 $rand = int( rand( $count ) );
5432 if ( not exists $rand_hash{ $rand } )
5434 $rand_hash{ $rand } = 1;
5436 $max = $rand if $rand > $max;
5442 $fh_in = Maasha::Common::read_open( $tmp_file );
5446 while ( $record = get_record( $fh_in ) )
5448 put_record( $record, $out ) if exists $rand_hash{ $count };
5450 last if $count == $max;
5461 sub script_sort_records
5463 # Martin A. Hansen, August 2007.
5465 # Sort to sort records according to keys.
5467 my ( $in, # handle to in stream
5468 $out, # handle to out stream
5469 $options, # options hash
5474 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5476 foreach $key ( @{ $options->{ "keys" } } )
5478 if ( $key =~ s/n$// ) {
5479 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5481 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5485 $sort_str = join " or ", @sort_cmd;
5486 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5488 while ( $record = get_record( $in ) ) {
5489 push @records, $record;
5492 @records = sort $sort_sub @records;
5494 if ( $options->{ "reverse" } )
5496 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5497 put_record( $records[ $i ], $out );
5502 for ( $i = 0; $i < scalar @records; $i++ ) {
5503 put_record( $records[ $i ], $out );
5509 sub script_count_vals
5511 # Martin A. Hansen, August 2007.
5513 # Count records in stream.
5515 my ( $in, # handle to in stream
5516 $out, # handle to out stream
5517 $options, # options hash
5522 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5524 $tmp_file = "$BP_TMP/count_cache.tmp";
5526 $fh_out = Maasha::Common::write_open( $tmp_file );
5531 while ( $record = get_record( $in ) )
5533 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5535 push @records, $record;
5537 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5539 map { put_record( $_, $fh_out ) } @records;
5546 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5557 $fh_in = Maasha::Common::read_open( $tmp_file );
5559 while ( $record = get_record( $fh_in ) )
5561 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5563 put_record( $record, $out );
5565 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5573 foreach $record ( @records )
5575 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5577 put_record( $record, $out );
5584 sub script_plot_histogram
5586 # Martin A. Hansen, September 2007.
5588 # Plot a simple histogram for a given key using GNU plot.
5590 my ( $in, # handle to in stream
5591 $out, # handle to out stream
5592 $options, # options hash
5597 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5599 $options->{ "title" } ||= "Histogram";
5600 $options->{ "sort" } ||= "num";
5602 while ( $record = get_record( $in ) )
5604 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5606 put_record( $record, $out ) if not $options->{ "no_stream" };
5609 if ( $options->{ "sort" } eq "num" ) {
5610 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5612 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5615 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5617 $fh = write_stream( $options->{ "data_out" } );
5619 print $fh "$_\n" foreach @{ $result };
5625 sub script_plot_lendist
5627 # Martin A. Hansen, August 2007.
5629 # Plot length distribution using GNU plot.
5631 my ( $in, # handle to in stream
5632 $out, # handle to out stream
5633 $options, # options hash
5638 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5640 $options->{ "title" } ||= "Length Distribution";
5642 while ( $record = get_record( $in ) )
5644 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5646 put_record( $record, $out ) if not $options->{ "no_stream" };
5649 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5651 for ( $i = 0; $i < $max; $i++ ) {
5652 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5655 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5657 $fh = write_stream( $options->{ "data_out" } );
5659 print $fh "$_\n" foreach @{ $result };
5665 sub script_plot_chrdist
5667 # Martin A. Hansen, August 2007.
5669 # Plot chromosome distribution using GNU plot.
5671 my ( $in, # handle to in stream
5672 $out, # handle to out stream
5673 $options, # options hash
5678 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5680 $options->{ "title" } ||= "Chromosome Distribution";
5682 while ( $record = get_record( $in ) )
5684 if ( $record->{ "CHR" } ) { # generic
5685 $data_hash{ $record->{ "CHR" } }++;
5686 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5687 $data_hash{ $record->{ "S_ID" } }++;
5688 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5689 $data_hash{ $record->{ "S_ID" } }++;
5690 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5691 $data_hash{ $record->{ "S_ID" } }++;
5694 put_record( $record, $out ) if not $options->{ "no_stream" };
5697 foreach $elem ( keys %data_hash )
5701 $sort_key =~ s/chr//i;
5703 $sort_key =~ s/^X(.*)/99$1/;
5704 $sort_key =~ s/^Y(.*)/99$1/;
5705 $sort_key =~ s/^Z(.*)/999$1/;
5706 $sort_key =~ s/^M(.*)/9999$1/;
5707 $sort_key =~ s/^U(.*)/99999$1/;
5709 $count = $sort_key =~ tr/_//;
5711 $sort_key =~ s/_.*/"999999" x $count/ex;
5713 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5716 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5718 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5720 $fh = write_stream( $options->{ "data_out" } );
5722 print $fh "$_\n" foreach @{ $result };
5728 sub script_plot_karyogram
5730 # Martin A. Hansen, August 2007.
5732 # Plot hits on karyogram.
5734 my ( $in, # handle to in stream
5735 $out, # handle to out stream
5736 $options, # options hash
5741 my ( %options, $record, @data, $fh, $result, %data_hash );
5743 $options->{ "genome" } ||= "human";
5744 $options->{ "feat_color" } ||= "black";
5746 while ( $record = get_record( $in ) )
5748 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5750 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5753 put_record( $record, $out ) if not $options->{ "no_stream" };
5756 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5758 $fh = write_stream( $options->{ "data_out" } );
5766 sub script_plot_matches
5768 # Martin A. Hansen, August 2007.
5770 # Plot matches in 2D generating a dotplot.
5772 my ( $in, # handle to in stream
5773 $out, # handle to out stream
5774 $options, # options hash
5779 my ( $record, @data, $fh, $result, %data_hash );
5781 $options->{ "direction" } ||= "both";
5783 while ( $record = get_record( $in ) )
5785 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5786 push @data, $record;
5789 put_record( $record, $out ) if not $options->{ "no_stream" };
5792 $options->{ "title" } ||= "plot_matches";
5793 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5794 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5796 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5798 $fh = write_stream( $options->{ "data_out" } );
5800 print $fh "$_\n" foreach @{ $result };
5806 sub script_length_vals
5808 # Martin A. Hansen, August 2007.
5810 # Determine the length of the value for given keys.
5812 my ( $in, # handle to in stream
5813 $out, # handle to out stream
5814 $options, # options hash
5819 my ( $record, $key );
5821 while ( $record = get_record( $in ) )
5823 foreach $key ( @{ $options->{ "keys" } } )
5825 if ( $record->{ $key } ) {
5826 $record->{ $key . "_LEN" } = length $record->{ $key };
5830 put_record( $record, $out );
5837 # Martin A. Hansen, August 2007.
5839 # Calculates the sums for values of given keys.
5841 my ( $in, # handle to in stream
5842 $out, # handle to out stream
5843 $options, # options hash
5848 my ( $record, $key, %sum_hash, $fh );
5850 while ( $record = get_record( $in ) )
5852 foreach $key ( @{ $options->{ "keys" } } )
5854 if ( $record->{ $key } ) {
5855 $sum_hash{ $key } += $record->{ $key };
5859 put_record( $record, $out ) if not $options->{ "no_stream" };
5862 $fh = write_stream( $options->{ "data_out" } );
5864 foreach $key ( @{ $options->{ "keys" } } ) {
5865 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5872 sub script_mean_vals
5874 # Martin A. Hansen, August 2007.
5876 # Calculate the mean of values of given keys.
5878 my ( $in, # handle to in stream
5879 $out, # handle to out stream
5880 $options, # options hash
5885 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5887 while ( $record = get_record( $in ) )
5889 foreach $key ( @{ $options->{ "keys" } } )
5891 if ( $record->{ $key } )
5893 $sum_hash{ $key } += $record->{ $key };
5894 $count_hash{ $key }++;
5898 put_record( $record, $out ) if not $options->{ "no_stream" };
5901 $fh = write_stream( $options->{ "data_out" } );
5903 foreach $key ( @{ $options->{ "keys" } } )
5905 if ( $count_hash{ $key } ) {
5906 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5911 put_record( { $key . "_MEAN" => $mean } , $fh );
5918 sub script_median_vals
5920 # Martin A. Hansen, March 2008.
5922 # Calculate the median values of given keys.
5924 my ( $in, # handle to in stream
5925 $out, # handle to out stream
5926 $options, # options hash
5931 my ( $record, $key, %median_hash, $median, $fh );
5933 while ( $record = get_record( $in ) )
5935 foreach $key ( @{ $options->{ "keys" } } ) {
5936 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5939 put_record( $record, $out ) if not $options->{ "no_stream" };
5942 $fh = write_stream( $options->{ "data_out" } );
5944 foreach $key ( @{ $options->{ "keys" } } )
5946 if ( $median_hash{ $key } ) {
5947 $median = Maasha::Calc::median( $median_hash{ $key } );
5952 put_record( { $key . "_MEDIAN" => $median } , $fh );
5961 # Martin A. Hansen, February 2008.
5963 # Determine the maximum values of given keys.
5965 my ( $in, # handle to in stream
5966 $out, # handle to out stream
5967 $options, # options hash
5972 my ( $record, $key, $fh, %max_hash, $max_record );
5974 while ( $record = get_record( $in ) )
5976 foreach $key ( @{ $options->{ "keys" } } )
5978 if ( $record->{ $key } )
5980 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5984 put_record( $record, $out ) if not $options->{ "no_stream" };
5987 $fh = write_stream( $options->{ "data_out" } );
5989 foreach $key ( @{ $options->{ "keys" } } )
5991 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5994 put_record( $max_record, $fh );
6002 # Martin A. Hansen, February 2008.
6004 # Determine the minimum values of given keys.
6006 my ( $in, # handle to in stream
6007 $out, # handle to out stream
6008 $options, # options hash
6013 my ( $record, $key, $fh, %min_hash, $min_record );
6015 while ( $record = get_record( $in ) )
6017 foreach $key ( @{ $options->{ "keys" } } )
6019 if ( defined $record->{ $key } )
6021 if ( exists $min_hash{ $key } ) {
6022 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6024 $min_hash{ $key } = $record->{ $key };
6029 put_record( $record, $out ) if not $options->{ "no_stream" };
6032 $fh = write_stream( $options->{ "data_out" } );
6034 foreach $key ( @{ $options->{ "keys" } } )
6036 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6039 put_record( $min_record, $fh );
6045 sub script_upload_to_ucsc
6047 # Martin A. Hansen, August 2007.
6049 # Calculate the mean of values of given keys.
6051 # This routine has developed into the most ugly hack. Do something!
6053 my ( $in, # handle to in stream
6054 $out, # handle to out stream
6055 $options, # options hash
6060 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
6062 $options->{ "short_label" } ||= $options->{ 'table' };
6063 $options->{ "long_label" } ||= $options->{ 'table' };
6064 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6065 $options->{ "priority" } ||= 1;
6066 $options->{ "visibility" } ||= "pack";
6067 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6068 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6070 $file = "$BP_TMP/ucsc_upload.tmp";
6078 $fh_out = Maasha::Common::write_open( $file );
6080 while ( $record = get_record( $in ) )
6082 put_record( $record, $out ) if not $options->{ "no_stream" };
6084 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6086 $vals = $record->{ "VALS" };
6089 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6090 print $fh_out "$vals\n";
6092 $format = "WIGGLE" if not $format;
6094 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6096 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6097 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6101 $format = "PSL" if not $format;
6103 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6105 # chrom chromStart chromEnd name score strand size secStr conf
6107 print $fh_out join ( "\t",
6109 $record->{ "CHR_BEG" },
6110 $record->{ "CHR_END" } + 1,
6111 $record->{ "Q_ID" },
6112 $record->{ "SCORE" },
6113 $record->{ "STRAND" },
6114 $record->{ "SIZE" },
6115 $record->{ "SEC_STRUCT" },
6116 $record->{ "CONF" },
6119 $format = "BED_SS" if not $format;
6121 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6123 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6125 $format = "BED" if not $format;
6126 $columns = $record->{ "BED_COLS" } if not $columns;
6128 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6130 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6132 $format = "BED" if not $format;
6133 $columns = 6 if not $columns;
6135 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6137 $record->{ "CHR" } = $record->{ "S_ID" };
6138 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6139 $record->{ "CHR_END" } = $record->{ "S_END" };
6140 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6142 $format = "BED" if not $format;
6143 $columns = 6 if not $columns;
6145 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6147 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6149 $record->{ "CHR" } = $record->{ "S_ID" };
6150 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6151 $record->{ "CHR_END" } = $record->{ "S_END" };
6152 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6153 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6155 $format = "BED" if not $format;
6156 $columns = 6 if not $columns;
6158 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6161 if ( $i == $options->{ "chunk_size" } )
6165 if ( $format eq "BED" ) {
6166 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6167 } elsif ( $format eq "PSL" ) {
6168 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6177 $fh_out = Maasha::Common::write_open( $file );
6185 if ( exists $options->{ "database" } and $options->{ "table" } )
6187 if ( $format eq "BED" )
6189 $type = "bed $columns";
6191 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6193 elsif ( $format eq "BED_SS" )
6195 $options->{ "sec_struct" } = 1;
6197 $type = "sec_struct";
6199 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6201 elsif ( $format eq "PSL" )
6205 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6207 elsif ( $format eq "WIGGLE" )
6209 $options->{ "visibility" } = "full";
6211 $wig_file = "$options->{ 'table' }.wig";
6212 $wib_file = "$options->{ 'table' }.wib";
6214 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6216 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6218 if ( $options->{ 'verbose' } ) {
6219 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6221 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6224 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6232 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6237 Maasha::UCSC::update_my_tracks( $options, $type );
6242 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6247 # Martin A. Hansen, July 2008.
6249 # Given a biopiece record converts it to a FASTA record.
6250 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6251 # tried in that order.
6253 my ( $record, # record
6258 my ( $seq_name, $seq );
6260 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6261 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6263 if ( defined $seq_name and defined $seq ) {
6264 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6273 # Martin A. Hansen, July 2007.
6275 # Opens a stream to STDIN or a file,
6277 my ( $path, # path - OPTIONAL
6280 # Returns filehandle.
6284 if ( not -t STDIN ) {
6285 $fh = Maasha::Common::read_stdin();
6286 } elsif ( not $path ) {
6287 # Maasha::Common::error( qq(no data stream) );
6289 $fh = Maasha::Common::read_open( $path );
6292 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6300 # Martin A. Hansen, August 2007.
6302 # Opens a stream to STDOUT or a file.
6304 my ( $path, # path - OPTIONAL
6305 $gzip, # compress data - OPTIONAL
6308 # Returns filehandle.
6313 $fh = Maasha::Common::write_open( $path, $gzip );
6315 $fh = Maasha::Common::write_stdout();
6324 # Martin A. Hansen, July 2007.
6326 # Reads one record at a time and converts that record
6327 # to a Perl data structure (a hash) which is returned.
6329 my ( $fh, # handle to stream
6334 my ( $block, @lines, $line, $key, $value, %record );
6336 local $/ = "\n---\n";
6342 return if not defined $block;
6344 @lines = split "\n", $block;
6346 foreach $line ( @lines )
6348 ( $key, $value ) = split ": ", $line, 2;
6350 $record{ $key } = $value;
6353 return wantarray ? %record : \%record;
6359 # Martin A. Hansen, July 2007.
6361 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6363 my ( $data, # data structure
6364 $fh, # file handle - OPTIONAL
6369 if ( scalar keys %{ $data } )
6373 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6378 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6389 # Martin A. Hansen, November 2007.
6391 # Extracts files from an explicit GetOpt::Long argument
6392 # allowing for the use of glob. E.g.
6393 # --data_in=test.fna
6394 # --data_in=test.fna,test2.fna
6396 # --data_in=test.fna,/dir/*.fna
6398 my ( $option, # option from GetOpt::Long
6403 my ( $elem, @files );
6405 foreach $elem ( split ",", $option )
6409 } elsif ( $elem =~ /\*/ ) {
6410 push @files, glob( $elem );
6414 return wantarray ? @files : \@files;
6420 # Martin A. Hansen, April 2008.
6422 # Removes temporary directory and exits gracefully.
6423 # This subroutine is meant to be run always as the last
6424 # thing even if a script is dies or is interrupted
6427 my ( $sig, # signal from the %SIG
6430 # print STDERR "signal->$sig<-\n";
6438 if ( $sig =~ /MAASHA_ERROR/ ) {
6439 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6440 } elsif ( $sig eq "INT" ) {
6441 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6442 } elsif ( $sig eq "TERM" ) {
6443 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6445 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6457 # Martin A. Hansen, July 2008.
6459 # Cleans out any unused temporary files and directories in BP_TMP.
6463 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6465 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6467 $curr_pid = Maasha::Common::get_processid();
6469 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6471 foreach $dir ( @dirs )
6473 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6479 if ( $user eq Maasha::Common::get_user() )
6481 if ( not Maasha::Common::process_running( $pid ) )
6483 # print STDERR "Removing stale dir: $dir\n";
6484 Maasha::Common::dir_remove( $dir );
6486 elsif ( $pid == $curr_pid )
6488 # print STDERR "Removing current dir: $dir\n";
6489 # Maasha::Common::dir_remove( $dir );
6503 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<