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 SEQ_NAME => $seq_name,
2024 SEQ_QUAL => $seq_qual,
2025 SEQ_LEN => length $seq_cs,
2026 SEQ => join( "", @seqs ),
2027 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2030 put_record( $record, $out );
2032 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2042 close $data_in if $data_in;
2046 sub script_read_mysql
2048 # Martin A. Hansen, May 2008.
2050 # Read a MySQL query into stream.
2052 my ( $in, # handle to in stream
2053 $out, # handle to out stream
2054 $options, # options hash
2059 my ( $record, $dbh, $results );
2061 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2062 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2064 while ( $record = get_record( $in ) ) {
2065 put_record( $record, $out );
2068 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2070 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2072 Maasha::SQL::disconnect( $dbh );
2074 map { put_record( $_ ) } @{ $results };
2078 sub script_format_genome
2080 # Martin A. Hansen, Juli 2008.
2082 # Format a genome to speficed formats.
2084 my ( $in, # handle to in stream
2085 $out, # handle to out stream
2086 $options, # options hash
2091 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2093 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2094 $genome = $options->{ 'genome' };
2096 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2097 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2098 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2100 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2102 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2104 $fasta_dir = "$dir/genomes/$genome/fasta";
2108 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2110 $fasta_dir = "$dir/genomes/$genome/fasta";
2112 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2115 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2117 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2119 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2121 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2124 while ( $record = get_record( $in ) )
2126 if ( $fh_out and $entry = record2fasta( $record ) )
2128 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2130 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2132 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2134 $vals = $record->{ 'VALS' };
2138 print $fh_out "$vals\n";
2141 put_record( $record, $out ) if not $options->{ "no_stream" };
2144 foreach $format ( @{ $options->{ 'formats' } } )
2146 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2147 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2148 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2149 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2150 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2153 close $fh_out if $fh_out;
2157 sub script_length_seq
2159 # Martin A. Hansen, August 2007.
2161 # Determine the length of sequences in stream.
2163 my ( $in, # handle to in stream
2164 $out, # handle to out stream
2165 $options, # options hash
2170 my ( $record, $total );
2172 while ( $record = get_record( $in ) )
2174 if ( $record->{ "SEQ" } )
2176 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2177 $total += $record->{ "SEQ_LEN" };
2180 put_record( $record, $out ) if not $options->{ "no_stream" };
2183 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2187 sub script_uppercase_seq
2189 # Martin A. Hansen, August 2007.
2191 # Uppercases sequences in stream.
2193 my ( $in, # handle to in stream
2194 $out, # handle to out stream
2201 while ( $record = get_record( $in ) )
2203 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2205 put_record( $record, $out );
2210 sub script_shuffle_seq
2212 # Martin A. Hansen, December 2007.
2214 # Shuffle sequences in stream.
2216 my ( $in, # handle to in stream
2217 $out, # handle to out stream
2224 while ( $record = get_record( $in ) )
2226 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2228 put_record( $record, $out );
2233 sub script_analyze_seq
2235 # Martin A. Hansen, August 2007.
2237 # Analyze sequence composition of sequences in stream.
2239 my ( $in, # handle to in stream
2240 $out, # handle to out stream
2245 my ( $record, $analysis );
2247 while ( $record = get_record( $in ) )
2249 if ( $record->{ "SEQ" } )
2251 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2253 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2256 put_record( $record, $out );
2261 sub script_analyze_tags
2263 # Martin A. Hansen, August 2008.
2265 # Analyze sequence tags in stream.
2267 my ( $in, # handle to in stream
2268 $out, # handle to out stream
2273 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2275 while ( $record = get_record( $in ) )
2277 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2279 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2283 $len_hash{ length( $record->{ "SEQ" } ) }++;
2284 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2287 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2289 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2293 $len_hash{ $record->{ "BED_LEN" } }++;
2294 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2299 foreach $key ( sort { $a <=> $b } keys %len_hash )
2301 $tag_record->{ "TAG_LEN" } = $key;
2302 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2303 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2305 put_record( $tag_record, $out );
2310 sub script_complexity_seq
2312 # Martin A. Hansen, May 2008.
2314 # Generates an index calculated as the most common di-residue over
2315 # the sequence length for all sequences in stream.
2317 my ( $in, # handle to in stream
2318 $out, # handle to out stream
2323 my ( $record, $index );
2325 while ( $record = get_record( $in ) )
2327 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2329 put_record( $record, $out );
2334 sub script_oligo_freq
2336 # Martin A. Hansen, August 2007.
2338 # Determine the length of sequences in stream.
2340 my ( $in, # handle to in stream
2341 $out, # handle to out stream
2342 $options, # options hash
2347 my ( $record, %oligos, @freq_table );
2349 $options->{ "word_size" } ||= 7;
2351 while ( $record = get_record( $in ) )
2353 if ( $record->{ "SEQ" } )
2355 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2357 if ( not $options->{ "all" } )
2359 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2361 map { put_record( $_, $out ) } @freq_table;
2367 put_record( $record, $out );
2370 if ( $options->{ "all" } )
2372 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2374 map { put_record( $_, $out ) } @freq_table;
2379 sub script_create_weight_matrix
2381 # Martin A. Hansen, August 2007.
2383 # Creates a weight matrix from an alignmnet.
2385 my ( $in, # handle to in stream
2386 $out, # handle to out stream
2387 $options, # options hash
2392 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2396 while ( $record = get_record( $in ) )
2398 if ( $record->{ "SEQ" } )
2400 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2402 $res = substr $record->{ "SEQ" }, $i, 1;
2404 $freq_hash{ $i }{ $res }++;
2405 $res_hash{ $res } = 1;
2412 put_record( $record, $out );
2416 foreach $res ( sort keys %res_hash )
2420 $record->{ "V0" } = $res;
2422 for ( $i = 0; $i < keys %freq_hash; $i++ )
2424 $freq = $freq_hash{ $i }{ $res } || 0;
2426 if ( $options->{ "percent" } ) {
2427 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2430 $record->{ "V" . ( $i + 1 ) } = $freq;
2433 put_record( $record, $out );
2438 sub script_calc_bit_scores
2440 # Martin A. Hansen, March 2007.
2442 # Calculates the bit scores for each position from an alignmnet in the stream.
2444 my ( $in, # handle to in stream
2445 $out, # handle to out stream
2450 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2454 while ( $record = get_record( $in ) )
2456 if ( $record->{ "SEQ" } )
2458 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2460 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2462 $res = substr $record->{ "SEQ" }, $i, 1;
2464 next if $res =~ /-|_|~|\./;
2466 $freq_hash{ $i }{ $res }++;
2473 put_record( $record, $out );
2479 if ( $type eq "protein" ) {
2485 for ( $i = 0; $i < keys %freq_hash; $i++ )
2487 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2489 $bit_diff = $bit_max - $bit_height;
2491 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2494 put_record( $record, $out );
2498 sub script_calc_fixedstep
2500 # Martin A. Hansen, September 2008.
2502 # Calculates fixedstep entries from data in the stream.
2504 my ( $in, # handle to in stream
2505 $out, # handle to out stream
2506 $options, # options hash
2511 my ( $record, %fh_hash, $fh_in, $fh_out, $chr, $chr, $beg, $end, $q_id, $block, $entry, $clones, $beg_block, $max, $i );
2513 while ( $record = get_record( $in ) )
2515 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
2516 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
2517 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
2519 if ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2521 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
2523 $fh_out = $fh_hash{ $record->{ "CHR" } };
2525 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
2529 map { close $_ } keys %fh_hash;
2531 foreach $chr ( sort keys %fh_hash )
2533 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
2535 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
2539 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
2541 $chr = $entry->{ 'CHR' };
2542 $beg = $entry->{ 'CHR_BEG' };
2543 $end = $entry->{ 'CHR_END' };
2544 $q_id = $entry->{ 'Q_ID' };
2546 if ( $options->{ "score" } ) {
2547 $clones = $entry->{ 'SCORE' };
2548 } elsif ( $q_id =~ /_(\d+)$/ ) {
2558 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2560 $record->{ "CHR" } = $chr;
2561 $record->{ "CHR_BEG" } = $beg_block;
2562 $record->{ "STEP" } = 1;
2563 $record->{ "VALS" } = join ";", @{ $block };
2564 $record->{ "REC_TYPE" } = "fixed_step";
2566 put_record( $record, $out );
2572 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
2573 $block->[ $i ] += $clones;
2576 $max = Maasha::Calc::max( $max, $end );
2585 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
2586 $block->[ $i ] += $clones;
2593 map { $_ = sprintf( "%.4f", Maasha::Calc::log10( $_ ) ) } @{ $block } if $options->{ "log10" };
2595 $record->{ "CHR" } = $chr;
2596 $record->{ "CHR_BEG" } = $beg_block;
2597 $record->{ "STEP" } = 1;
2598 $record->{ "VALS" } = join ";", @{ $block };
2599 $record->{ "REC_TYPE" } = "fixed_step";
2601 put_record( $record, $out );
2603 unlink "$BP_TMP/$chr";
2608 sub script_reverse_seq
2610 # Martin A. Hansen, August 2007.
2612 # Reverse sequence in record.
2614 my ( $in, # handle to in stream
2615 $out, # handle to out stream
2622 while ( $record = get_record( $in ) )
2624 if ( $record->{ "SEQ" } ) {
2625 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2628 put_record( $record, $out );
2633 sub script_complement_seq
2635 # Martin A. Hansen, August 2007.
2637 # Complement sequence in record.
2639 my ( $in, # handle to in stream
2640 $out, # handle to out stream
2645 my ( $record, $type );
2647 while ( $record = get_record( $in ) )
2649 if ( $record->{ "SEQ" } )
2652 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2655 if ( $type eq "rna" ) {
2656 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2657 } elsif ( $type eq "dna" ) {
2658 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2662 put_record( $record, $out );
2667 sub script_remove_indels
2669 # Martin A. Hansen, August 2007.
2671 # Remove indels from sequences in stream.
2673 my ( $in, # handle to in stream
2674 $out, # handle to out stream
2681 while ( $record = get_record( $in ) )
2683 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2685 put_record( $record, $out );
2690 sub script_transliterate_seq
2692 # Martin A. Hansen, August 2007.
2694 # Transliterate chars from sequence in record.
2696 my ( $in, # handle to in stream
2697 $out, # handle to out stream
2698 $options, # options hash
2703 my ( $record, $search, $replace, $delete );
2705 $search = $options->{ "search" } || "";
2706 $replace = $options->{ "replace" } || "";
2707 $delete = $options->{ "delete" } || "";
2709 while ( $record = get_record( $in ) )
2711 if ( $record->{ "SEQ" } )
2713 if ( $search and $replace ) {
2714 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2715 } elsif ( $delete ) {
2716 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2720 put_record( $record, $out );
2725 sub script_transliterate_vals
2727 # Martin A. Hansen, April 2008.
2729 # Transliterate chars from values in record.
2731 my ( $in, # handle to in stream
2732 $out, # handle to out stream
2733 $options, # options hash
2738 my ( $record, $search, $replace, $delete, $key );
2740 $search = $options->{ "search" } || "";
2741 $replace = $options->{ "replace" } || "";
2742 $delete = $options->{ "delete" } || "";
2744 while ( $record = get_record( $in ) )
2746 foreach $key ( @{ $options->{ "keys" } } )
2748 if ( exists $record->{ $key } )
2750 if ( $search and $replace ) {
2751 eval "\$record->{ $key } =~ tr/$search/$replace/";
2752 } elsif ( $delete ) {
2753 eval "\$record->{ $key } =~ tr/$delete//d";
2758 put_record( $record, $out );
2763 sub script_translate_seq
2765 # Martin A. Hansen, February 2008.
2767 # Translate DNA sequence into protein sequence.
2769 my ( $in, # handle to in stream
2770 $out, # handle to out stream
2771 $options, # options hash
2776 my ( $record, $frame, %new_record );
2778 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2780 while ( $record = get_record( $in ) )
2782 if ( $record->{ "SEQ" } )
2784 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2786 foreach $frame ( @{ $options->{ "frames" } } )
2788 %new_record = %{ $record };
2790 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2791 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2792 $new_record{ "FRAME" } = $frame;
2794 put_record( \%new_record, $out );
2800 put_record( $record, $out );
2806 sub script_extract_seq
2808 # Martin A. Hansen, August 2007.
2810 # Extract subsequences from sequences in record.
2812 my ( $in, # handle to in stream
2813 $out, # handle to out stream
2814 $options, # options hash
2819 my ( $beg, $end, $len, $record );
2821 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2824 $beg = $options->{ "beg" } - 1; # correcting for start offset
2827 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2829 } elsif ( defined $options->{ "end" } ) {
2830 $end = $options->{ "end" } - 1; # correcting for start offset
2833 $len = $options->{ "len" };
2835 # print "beg->$beg, end->$end, len->$len\n";
2837 while ( $record = get_record( $in ) )
2839 if ( $record->{ "SEQ" } )
2841 if ( defined $beg and defined $end )
2843 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2844 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2846 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2849 elsif ( defined $beg and defined $len )
2851 if ( $len > length $record->{ "SEQ" } ) {
2852 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2854 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2857 elsif ( defined $beg )
2859 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2863 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2865 put_record( $record, $out );
2870 sub script_get_genome_seq
2872 # Martin A. Hansen, December 2007.
2874 # Gets a subsequence from a genome.
2876 my ( $in, # handle to in stream
2877 $out, # handle to out stream
2878 $options, # options hash
2883 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2885 $options->{ "flank" } ||= 0;
2887 if ( $options->{ "genome" } )
2889 $genome = $options->{ "genome" };
2891 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2892 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2894 $fh = Maasha::Common::read_open( $genome_file );
2895 $index = Maasha::Fasta::index_retrieve( $index_file );
2897 shift @{ $index }; # Get rid of the file size info
2899 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2901 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2903 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2905 $beg = $index_beg + $options->{ "beg" } - 1;
2907 if ( $options->{ "len" } ) {
2908 $len = $options->{ "len" };
2909 } elsif ( $options->{ "end" } ) {
2910 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2913 $beg -= $options->{ "flank" };
2914 $len += 2 * $options->{ "flank" };
2916 if ( $beg <= $index_beg )
2918 $len -= $index_beg - $beg;
2922 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2924 next if $beg > $index_beg + $index_len;
2926 $record->{ "CHR" } = $options->{ "chr" };
2927 $record->{ "CHR_BEG" } = $beg - $index_beg;
2928 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2930 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2931 $record->{ "SEQ_LEN" } = $len;
2933 put_record( $record, $out );
2937 while ( $record = get_record( $in ) )
2939 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2941 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2943 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2945 $beg = $record->{ "CHR_BEG" } + $index_beg;
2946 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2948 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2950 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2952 $beg = $record->{ "S_BEG" } + $index_beg;
2953 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2955 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2957 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2959 $beg = $record->{ "S_BEG" } + $index_beg;
2960 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2963 $beg -= $options->{ "flank" };
2964 $len += 2 * $options->{ "flank" };
2966 if ( $beg <= $index_beg )
2968 $len -= $index_beg - $beg;
2972 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2974 next if $beg > $index_beg + $index_len;
2976 $record->{ "CHR_BEG" } = $beg - $index_beg;
2977 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2979 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2981 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2983 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2984 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2987 if ( $options->{ "mask" } )
2989 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2991 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2993 @begs = split ",", $record->{ "Q_BEGS" };
2994 @lens = split ",", $record->{ "BLOCKSIZES" };
2996 for ( $i = 0; $i < @begs; $i++ ) {
2997 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
3003 put_record( $record, $out );
3010 sub script_get_genome_align
3012 # Martin A. Hansen, April 2008.
3014 # Gets a subalignment from a multiple genome alignment.
3016 my ( $in, # handle to in stream
3017 $out, # handle to out stream
3018 $options, # options hash
3023 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
3025 $options->{ "strand" } ||= "+";
3029 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
3031 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
3033 $beg = $options->{ "beg" } - 1;
3035 if ( $options->{ "end" } ) {
3036 $end = $options->{ "end" };
3037 } elsif ( $options->{ "len" } ) {
3038 $end = $beg + $options->{ "len" };
3041 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
3043 foreach $entry ( @{ $align } )
3045 $entry->{ "CHR" } = $record->{ "CHR" };
3046 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3047 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3048 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
3049 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3050 $entry->{ "SCORE" } = $record->{ "SCORE" };
3052 put_record( $entry, $out );
3056 while ( $record = get_record( $in ) )
3058 if ( $record->{ "REC_TYPE" } eq "BED" )
3060 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
3062 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3064 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3066 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3068 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
3071 foreach $entry ( @{ $align } )
3073 $entry->{ "CHR" } = $record->{ "CHR" };
3074 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
3075 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
3076 $entry->{ "STRAND" } = $record->{ "STRAND" };
3077 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
3078 $entry->{ "SCORE" } = $record->{ "SCORE" };
3080 put_record( $entry, $out );
3088 sub script_get_genome_phastcons
3090 # Martin A. Hansen, February 2008.
3092 # Get phastcons scores from genome intervals.
3094 my ( $in, # handle to in stream
3095 $out, # handle to out stream
3096 $options, # options hash
3101 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
3103 $options->{ "flank" } ||= 0;
3105 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3106 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3108 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3109 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3111 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
3113 $options->{ "beg" } -= 1; # request is 1-based
3114 $options->{ "end" } -= 1; # request is 1-based
3116 if ( $options->{ "len" } ) {
3117 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3120 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3122 $record->{ "CHR" } = $options->{ "chr" };
3123 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3124 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3126 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3127 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3129 put_record( $record, $out );
3132 while ( $record = get_record( $in ) )
3134 if ( $record->{ "REC_TYPE" } eq "BED" )
3136 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3138 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3140 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3142 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3144 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3147 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3148 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3150 put_record( $record, $out );
3153 close $fh_phastcons if $fh_phastcons;
3159 # Martin A. Hansen, December 2007.
3161 # Folds sequences in stream into secondary structures.
3163 my ( $in, # handle to in stream
3164 $out, # handle to out stream
3169 my ( $record, $type, $struct, $index );
3171 while ( $record = get_record( $in ) )
3173 if ( $record->{ "SEQ" } )
3176 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3179 if ( $type ne "protein" )
3181 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3182 $record->{ "SEC_STRUCT" } = $struct;
3183 $record->{ "FREE_ENERGY" } = $index;
3184 $record->{ "SCORE" } = abs int $index;
3185 $record->{ "SIZE" } = length $struct;
3186 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3190 put_record( $record, $out );
3195 sub script_split_seq
3197 # Martin A. Hansen, August 2007.
3199 # Split a sequence in stream into words.
3201 my ( $in, # handle to in stream
3202 $out, # handle to out stream
3203 $options, # options hash
3208 my ( $record, $new_record, $i, $subseq, %lookup );
3210 $options->{ "word_size" } ||= 7;
3212 while ( $record = get_record( $in ) )
3214 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3216 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3218 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3220 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3222 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3223 $new_record->{ "SEQ" } = $subseq;
3225 put_record( $new_record, $out );
3227 $lookup{ $subseq } = 1;
3231 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3232 $new_record->{ "SEQ" } = $subseq;
3234 put_record( $new_record, $out );
3240 put_record( $record, $out );
3246 sub script_split_bed
3248 # Martin A. Hansen, June 2008.
3250 # Split a BED record into overlapping windows.
3252 my ( $in, # handle to in stream
3253 $out, # handle to out stream
3254 $options, # options hash
3259 my ( $record, $new_record, $i );
3261 $options->{ "window_size" } ||= 20;
3262 $options->{ "step_size" } ||= 1;
3264 while ( $record = get_record( $in ) )
3266 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3268 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3270 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3272 $new_record->{ "REC_TYPE" } = "BED";
3273 $new_record->{ "CHR" } = $record->{ "CHR" };
3274 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3275 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3276 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3277 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3278 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3279 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3281 put_record( $new_record, $out );
3286 put_record( $record, $out );
3292 sub script_align_seq
3294 # Martin A. Hansen, August 2007.
3296 # Align sequences in stream.
3298 my ( $in, # handle to in stream
3299 $out, # handle to out stream
3304 my ( $record, @entries, $entry );
3306 while ( $record = get_record( $in ) )
3308 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3309 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3310 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3311 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3313 put_record( $record, $out );
3317 @entries = Maasha::Align::align( \@entries );
3319 foreach $entry ( @entries )
3321 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3324 SEQ_NAME => $entry->[ SEQ_NAME ],
3325 SEQ => $entry->[ SEQ ],
3328 put_record( $record, $out );
3336 # Martin A. Hansen, February 2008.
3338 # Using the first sequence in stream as reference, tile
3339 # all subsequent sequences based on pairwise alignments.
3341 my ( $in, # handle to in stream
3342 $out, # handle to out stream
3343 $options, # options hash
3348 my ( $record, $first, $ref_entry, @entries );
3352 while ( $record = get_record( $in ) )
3354 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3358 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3364 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3369 put_record( $record, $out );
3373 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3375 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3379 sub script_invert_align
3381 # Martin A. Hansen, February 2008.
3383 # Inverts an alignment showing only non-mathing residues
3384 # using the first sequence as reference.
3386 my ( $in, # handle to in stream
3387 $out, # handle to out stream
3388 $options, # options hash
3393 my ( $record, @entries );
3395 while ( $record = get_record( $in ) )
3397 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3399 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3403 put_record( $record, $out );
3407 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3409 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3413 sub script_patscan_seq
3415 # Martin A. Hansen, August 2007.
3417 # Locates patterns in sequences using scan_for_matches.
3419 my ( $in, # handle to in stream
3420 $out, # handle to out stream
3421 $options, # options hash
3426 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3428 if ( $options->{ "patterns" } ) {
3429 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3430 } elsif ( -f $options->{ "patterns_in" } ) {
3431 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3434 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3436 push @args, "-c" if $options->{ "comp" };
3437 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3438 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3440 $seq_file = "$BP_TMP/patscan.seq";
3441 $pat_file = "$BP_TMP/patscan.pat";
3442 $out_file = "$BP_TMP/patscan.out";
3444 $fh_out = Maasha::Common::write_open( $seq_file );
3448 while ( $record = get_record( $in ) )
3450 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3452 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3454 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3456 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3464 $arg = join " ", @args;
3465 $arg .= " -p" if $type eq "protein";
3467 foreach $pattern ( @{ $patterns } )
3469 $fh_out = Maasha::Common::write_open( $pat_file );
3471 print $fh_out "$pattern\n";
3475 if ( $options->{ 'genome' } ) {
3476 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3477 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3479 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3480 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3483 $fh_in = Maasha::Common::read_open( $out_file );
3485 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3487 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3489 if ( $options->{ 'genome' } )
3491 $result->{ "CHR" } = $result->{ "S_ID" };
3492 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3493 $result->{ "CHR_END" } = $result->{ "S_END" };
3495 delete $result->{ "S_ID" };
3496 delete $result->{ "S_BEG" };
3497 delete $result->{ "S_END" };
3501 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3504 put_record( $result, $out );
3516 sub script_create_blast_db
3518 # Martin A. Hansen, September 2007.
3520 # Creates a NCBI BLAST database with formatdb
3522 my ( $in, # handle to in stream
3523 $out, # handle to out stream
3524 $options, # options hash
3529 my ( $fh, $seq_type, $path, $record, $entry );
3531 $path = $options->{ "database" };
3533 $fh = Maasha::Common::write_open( $path );
3535 while ( $record = get_record( $in ) )
3537 put_record( $record, $out ) if not $options->{ "no_stream" };
3539 if ( $entry = record2fasta( $record ) )
3541 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3543 Maasha::Fasta::put_entry( $entry, $fh );
3549 if ( $seq_type eq "protein" ) {
3550 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3552 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3559 sub script_blast_seq
3561 # Martin A. Hansen, September 2007.
3563 # BLASTs sequences in stream against a given database.
3565 my ( $in, # handle to in stream
3566 $out, # handle to out stream
3567 $options, # options hash
3572 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3574 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3575 $options->{ "filter" } = "F";
3576 $options->{ "filter" } = "T" if $options->{ "filter" };
3577 $options->{ "cpus" } ||= 1;
3579 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3581 $tmp_in = "$BP_TMP/blast_query.seq";
3582 $tmp_out = "$BP_TMP/blast.result";
3584 $fh_out = Maasha::Common::write_open( $tmp_in );
3586 while ( $record = get_record( $in ) )
3588 if ( $entry = record2fasta( $record ) )
3590 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3592 Maasha::Fasta::put_entry( $entry, $fh_out );
3595 put_record( $record, $out );
3600 if ( -f $options->{ 'database' } . ".phr" ) {
3601 $s_type = "protein";
3603 $s_type = "nucleotide";
3606 if ( not $options->{ 'program' } )
3608 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3609 $options->{ 'program' } = "blastn";
3610 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3611 $options->{ 'program' } = "blastp";
3612 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3613 $options->{ 'program' } = "blastx";
3614 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3615 $options->{ 'program' } = "tblastn";
3619 if ( $options->{ 'verbose' } )
3621 Maasha::Common::run(
3624 "-p $options->{ 'program' }",
3625 "-e $options->{ 'e_val' }",
3626 "-a $options->{ 'cpus' }",
3629 "-d $options->{ 'database' }",
3630 "-F $options->{ 'filter' }",
3638 Maasha::Common::run(
3641 "-p $options->{ 'program' }",
3642 "-e $options->{ 'e_val' }",
3643 "-a $options->{ 'cpus' }",
3646 "-d $options->{ 'database' }",
3647 "-F $options->{ 'filter' }",
3657 $fh_out = Maasha::Common::read_open( $tmp_out );
3661 while ( $line = <$fh_out> )
3665 next if $line =~ /^#/;
3667 @fields = split /\s+/, $line;
3669 $record->{ "REC_TYPE" } = "BLAST";
3670 $record->{ "Q_ID" } = $fields[ 0 ];
3671 $record->{ "S_ID" } = $fields[ 1 ];
3672 $record->{ "IDENT" } = $fields[ 2 ];
3673 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3674 $record->{ "MISMATCHES" } = $fields[ 4 ];
3675 $record->{ "GAPS" } = $fields[ 5 ];
3676 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3677 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3678 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3679 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3680 $record->{ "E_VAL" } = $fields[ 10 ];
3681 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3683 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3685 $record->{ "STRAND" } = '-';
3687 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3691 $record->{ "STRAND" } = '+';
3694 put_record( $record, $out );
3705 # Martin A. Hansen, August 2007.
3707 # BLATs sequences in stream against a given genome.
3709 my ( $in, # handle to in stream
3710 $out, # handle to out stream
3711 $options, # options hash
3716 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3718 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3720 $options->{ 'tile_size' } ||= 11;
3721 $options->{ 'one_off' } ||= 0;
3722 $options->{ 'min_identity' } ||= 90;
3723 $options->{ 'min_score' } ||= 0;
3724 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3726 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3727 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3728 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3729 $blat_args .= " -minScore=$options->{ 'min_score' }";
3730 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3731 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3733 $query_file = "$BP_TMP/blat.seq";
3735 $fh_out = Maasha::Common::write_open( $query_file );
3737 while ( $record = get_record( $in ) )
3739 if ( $entry = record2fasta( $record ) )
3741 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3742 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3745 put_record( $record, $out );
3750 $blat_args .= " -t=dnax" if $type eq "protein";
3751 $blat_args .= " -q=$type";
3753 $result_file = "$BP_TMP/blat.psl";
3755 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3759 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3761 map { put_record( $_, $out ) } @{ $entries };
3763 unlink $result_file;
3769 # Martin A. Hansen, July 2008.
3771 # soap sequences in stream against a given file or genome.
3773 my ( $in, # handle to in stream
3774 $out, # handle to out stream
3775 $options, # options hash
3780 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3782 $options->{ "seed_size" } ||= 10;
3783 $options->{ "mismatches" } ||= 2;
3784 $options->{ "gap_size" } ||= 0;
3785 $options->{ "cpus" } ||= 1;
3787 if ( $options->{ "genome" } ) {
3788 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3791 $tmp_in = "$BP_TMP/soap_query.seq";
3792 $tmp_out = "$BP_TMP/soap.result";
3794 $fh_out = Maasha::Common::write_open( $tmp_in );
3798 while ( $record = get_record( $in ) )
3800 if ( $entry = record2fasta( $record ) )
3802 Maasha::Fasta::put_entry( $entry, $fh_out );
3807 put_record( $record, $out );
3815 "-s $options->{ 'seed_size' }",
3818 "-v $options->{ 'mismatches' }",
3819 "-g $options->{ 'gap_size' }",
3820 "-p $options->{ 'cpus' }",
3821 "-d $options->{ 'in_file' }",
3825 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3827 Maasha::Common::run( "soap", $args, 1 );
3831 $fh_out = Maasha::Common::read_open( $tmp_out );
3835 while ( $line = <$fh_out> )
3839 @fields = split /\t/, $line;
3841 $record->{ "REC_TYPE" } = "SOAP";
3842 $record->{ "Q_ID" } = $fields[ 0 ];
3843 $record->{ "SCORE" } = $fields[ 3 ];
3844 $record->{ "STRAND" } = $fields[ 6 ];
3845 $record->{ "S_ID" } = $fields[ 7 ];
3846 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3847 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3849 put_record( $record, $out );
3859 sub script_match_seq
3861 # Martin A. Hansen, August 2007.
3863 # BLATs sequences in stream against a given genome.
3865 my ( $in, # handle to in stream
3866 $out, # handle to out stream
3867 $options, # options hash
3872 my ( $record, @entries, $results );
3874 $options->{ "word_size" } ||= 20;
3875 $options->{ "direction" } ||= "both";
3877 while ( $record = get_record( $in ) )
3879 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3880 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3883 put_record( $record, $out );
3886 if ( @entries == 1 )
3888 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3890 map { put_record( $_, $out ) } @{ $results };
3892 elsif ( @entries == 2 )
3894 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3896 map { put_record( $_, $out ) } @{ $results };
3901 sub script_create_vmatch_index
3903 # Martin A. Hansen, January 2008.
3905 # Create a vmatch index from sequences in the stream.
3907 my ( $in, # handle to in stream
3908 $out, # handle to out stream
3909 $options, # options hash
3914 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3916 if ( $options->{ "index_name" } )
3918 $file_tmp = $options->{ 'index_name' };
3919 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3922 while ( $record = get_record( $in ) )
3924 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3926 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3928 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3931 put_record( $record, $out ) if not $options->{ "no_stream" };
3934 if ( $options->{ "index_name" } )
3938 if ( $type eq "protein" ) {
3939 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3941 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3949 sub script_vmatch_seq
3951 # Martin A. Hansen, August 2007.
3953 # Vmatches sequences in stream against a given genome.
3955 my ( $in, # handle to in stream
3956 $out, # handle to out stream
3957 $options, # options hash
3962 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3964 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3966 if ( $options->{ "index_name" } )
3968 @index_files = $options->{ "index_name" };
3972 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3974 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3976 @index_files = sort keys %hash;
3979 while ( $record = get_record( $in ) )
3981 push @records, $record;
3983 put_record( $record, $out );
3986 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3990 $fh_in = Maasha::Common::read_open( $result_file );
3992 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3993 put_record( $record, $out );
3998 unlink $result_file;
4002 sub script_write_fasta
4004 # Martin A. Hansen, August 2007.
4006 # Write FASTA entries from sequences in stream.
4008 my ( $in, # handle to in stream
4009 $out, # handle to out stream
4010 $options, # options hash
4015 my ( $record, $fh, $entry );
4017 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4019 while ( $record = get_record( $in ) )
4021 if ( $entry = record2fasta( $record ) ) {
4022 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4025 put_record( $record, $out ) if not $options->{ "no_stream" };
4032 sub script_write_align
4034 # Martin A. Hansen, August 2007.
4036 # Write pretty alignments aligned sequences in stream.
4038 my ( $in, # handle to in stream
4039 $out, # handle to out stream
4040 $options, # options hash
4045 my ( $fh, $record, @entries );
4047 $fh = write_stream( $options->{ "data_out" } ) ;
4049 while ( $record = get_record( $in ) )
4051 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4052 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4055 put_record( $record, $out ) if not $options->{ "no_stream" };
4058 if ( scalar( @entries ) == 2 ) {
4059 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
4060 } elsif ( scalar ( @entries ) > 2 ) {
4061 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
4068 sub script_write_blast
4070 # Martin A. Hansen, November 2007.
4072 # Write data in blast table format (-m8 and 9).
4074 my ( $in, # handle to in stream
4075 $out, # handle to out stream
4076 $options, # options hash
4081 my ( $fh, $record, $first );
4083 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
4087 while ( $record = get_record( $in ) )
4089 if ( $record->{ "REC_TYPE" } eq "BLAST" )
4091 if ( $options->{ "comment" } and $first )
4093 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";
4098 if ( $record->{ "STRAND" } eq "-" ) {
4099 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
4102 print $fh join( "\t",
4103 $record->{ "Q_ID" },
4104 $record->{ "S_ID" },
4105 $record->{ "IDENT" },
4106 $record->{ "ALIGN_LEN" },
4107 $record->{ "MISMATCHES" },
4108 $record->{ "GAPS" },
4109 $record->{ "Q_BEG" } + 1,
4110 $record->{ "Q_END" } + 1,
4111 $record->{ "S_BEG" } + 1,
4112 $record->{ "S_END" } + 1,
4113 $record->{ "E_VAL" },
4114 $record->{ "BIT_SCORE" }
4118 put_record( $record, $out ) if not $options->{ "no_stream" };
4125 sub script_write_tab
4127 # Martin A. Hansen, August 2007.
4129 # Write data as table.
4131 my ( $in, # handle to in stream
4132 $out, # handle to out stream
4133 $options, # options hash
4138 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4140 $options->{ "delimit" } ||= "\t";
4142 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4144 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4146 while ( $record = get_record( $in ) )
4151 if ( $options->{ "keys" } )
4153 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4157 foreach $key ( @{ $options->{ "keys" } } )
4159 if ( exists $record->{ $key } )
4161 push @keys, $key if $options->{ "comment" };
4162 push @vals, $record->{ $key };
4169 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4171 next if exists $no_keys{ $key };
4173 push @keys, $key if $options->{ "comment" };
4174 push @vals, $record->{ $key };
4178 if ( @keys and $options->{ "comment" } )
4180 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4182 delete $options->{ "comment" };
4185 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4187 put_record( $record, $out ) if not $options->{ "no_stream" };
4194 sub script_write_bed
4196 # Martin A. Hansen, August 2007.
4198 # Write BED format for the UCSC genome browser using records in stream.
4200 my ( $in, # handle to in stream
4201 $out, # handle to out stream
4202 $options, # options hash
4207 my ( $fh, $record, $new_record );
4209 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4211 while ( $record = get_record( $in ) )
4213 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4215 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4217 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4219 $new_record->{ "CHR" } = $record->{ "S_ID" };
4220 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4221 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4222 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4223 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4224 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4226 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4228 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4230 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4232 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4234 $new_record->{ "CHR" } = $record->{ "S_ID" };
4235 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4236 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4237 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4238 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4239 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4241 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4243 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4245 $new_record->{ "CHR" } = $record->{ "S_ID" };
4246 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4247 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4248 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4249 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4250 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4252 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4254 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4256 $new_record->{ "CHR" } = $record->{ "S_ID" };
4257 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4258 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4259 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4260 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4261 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4263 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4265 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4267 Maasha::UCSC::bed_put_entry( $record, $fh );
4270 put_record( $record, $out ) if not $options->{ "no_stream" };
4277 sub script_write_psl
4279 # Martin A. Hansen, August 2007.
4281 # Write PSL output from stream.
4283 my ( $in, # handle to in stream
4284 $out, # handle to out stream
4285 $options, # options hash
4290 my ( $fh, $record, @output, $first );
4294 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4296 while ( $record = get_record( $in ) )
4298 put_record( $record, $out ) if not $options->{ "no_stream" };
4300 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4302 Maasha::UCSC::psl_put_header( $fh ) if $first;
4303 Maasha::UCSC::psl_put_entry( $record, $fh );
4312 sub script_write_fixedstep
4314 # Martin A. Hansen, Juli 2008.
4316 # Write fixedStep entries from recrods in the stream.
4318 my ( $in, # handle to in stream
4319 $out, # handle to out stream
4320 $options, # options hash
4325 my ( $fh, $record, $vals );
4327 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4329 while ( $record = get_record( $in ) )
4331 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4333 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4335 $vals = $record->{ 'VALS' };
4339 print $fh "$vals\n";
4342 put_record( $record, $out ) if not $options->{ "no_stream" };
4349 sub script_write_2bit
4351 # Martin A. Hansen, March 2008.
4353 # Write sequence entries from stream in 2bit format.
4355 my ( $in, # handle to in stream
4356 $out, # handle to out stream
4357 $options, # options hash
4362 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4364 $mask = 1 if not $options->{ "no_mask" };
4366 $tmp_file = "$BP_TMP/write_2bit.fna";
4367 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4369 $fh_out = write_stream( $options->{ "data_out" } );
4371 while ( $record = get_record( $in ) )
4373 if ( $entry = record2fasta( $record ) ) {
4374 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4377 put_record( $record, $out ) if not $options->{ "no_stream" };
4382 $fh_in = Maasha::Common::read_open( $tmp_file );
4384 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4393 sub script_write_solid
4395 # Martin A. Hansen, April 2008.
4397 # Write di-base encoded Solid sequence from entries in stream.
4399 my ( $in, # handle to in stream
4400 $out, # handle to out stream
4401 $options, # options hash
4406 my ( $record, $fh, $entry );
4408 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4410 while ( $record = get_record( $in ) )
4412 if ( $entry = record2fasta( $record ) )
4414 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4416 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4419 put_record( $record, $out ) if not $options->{ "no_stream" };
4426 sub script_plot_seqlogo
4428 # Martin A. Hansen, August 2007.
4430 # Calculates and writes a sequence logo for alignments.
4432 my ( $in, # handle to in stream
4433 $out, # handle to out stream
4434 $options, # options hash
4439 my ( $record, @entries, $logo, $fh );
4441 while ( $record = get_record( $in ) )
4443 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4444 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4447 put_record( $record, $out ) if not $options->{ "no_stream" };
4450 $logo = Maasha::Plot::seq_logo( \@entries );
4452 $fh = write_stream( $options->{ "data_out" } );
4460 sub script_plot_phastcons_profiles
4462 # Martin A. Hansen, January 2008.
4464 # Plots PhastCons profiles.
4466 my ( $in, # handle to in stream
4467 $out, # handle to out stream
4468 $options, # options hash
4473 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4475 $options->{ "title" } ||= "PhastCons Profiles";
4477 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4478 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4480 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
4481 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4483 while ( $record = get_record( $in ) )
4485 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4487 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
4488 $record->{ "CHR_BEG" },
4489 $record->{ "CHR_END" },
4490 $options->{ "flank" } );
4492 push @{ $AoA }, [ @{ $scores } ];
4495 put_record( $record, $out ) if not $options->{ "no_stream" };
4498 Maasha::UCSC::phastcons_normalize( $AoA );
4500 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4501 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4503 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4505 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4507 $fh = write_stream( $options->{ "data_out" } );
4509 print $fh "$_\n" foreach @{ $plot };
4515 sub script_analyze_bed
4517 # Martin A. Hansen, March 2008.
4519 # Analyze BED entries in stream.
4521 my ( $in, # handle to in stream
4522 $out, # handle to out stream
4523 $options, # options hash
4530 while ( $record = get_record( $in ) )
4532 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4534 put_record( $record, $out );
4539 sub script_analyze_vals
4541 # Martin A. Hansen, August 2007.
4543 # Analyze values for given keys in stream.
4545 my ( $in, # handle to in stream
4546 $out, # handle to out stream
4547 $options, # options hash
4552 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4554 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4556 while ( $record = get_record( $in ) )
4558 foreach $key ( keys %{ $record } )
4560 next if $options->{ "keys" } and not exists $key_hash{ $key };
4562 $analysis->{ $key }->{ "COUNT" }++;
4564 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4566 $analysis->{ $key }->{ "TYPE" } = "num";
4567 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4568 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4569 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4573 $len = length $record->{ $key };
4575 $analysis->{ $key }->{ "TYPE" } = "alph";
4576 $analysis->{ $key }->{ "SUM" } += $len;
4577 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4578 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4582 put_record( $record, $out ) if not $options->{ "no_stream" };
4585 foreach $key ( keys %{ $analysis } )
4587 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4588 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4591 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4601 if ( $options->{ "keys" } ) {
4602 @keys = @{ $options->{ "keys" } };
4604 @keys = keys %{ $analysis };
4607 foreach $key ( @keys )
4609 $keys .= sprintf "% 15s", $key;
4610 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4611 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4612 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4613 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4614 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4615 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4618 print $out "$keys\n";
4619 print $out "$types\n";
4620 print $out "$counts\n";
4621 print $out "$mins\n";
4622 print $out "$maxs\n";
4623 print $out "$sums\n";
4624 print $out "$means\n";
4628 sub script_head_records
4630 # Martin A. Hansen, August 2007.
4632 # Display the first sequences in stream.
4634 my ( $in, # handle to in stream
4635 $out, # handle to out stream
4636 $options, # options hash
4641 my ( $record, $count );
4643 $options->{ "num" } ||= 10;
4647 while ( $record = get_record( $in ) )
4651 put_record( $record, $out );
4653 last if $count == $options->{ "num" };
4658 sub script_remove_keys
4660 # Martin A. Hansen, August 2007.
4662 # Remove keys from stream.
4664 my ( $in, # handle to in stream
4665 $out, # handle to out stream
4666 $options, # options hash
4671 my ( $record, $new_record );
4673 while ( $record = get_record( $in ) )
4675 if ( $options->{ "keys" } )
4677 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4679 elsif ( $options->{ "save_keys" } )
4681 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4683 $record = $new_record;
4686 put_record( $record, $out ) if keys %{ $record };
4691 sub script_remove_adaptor
4693 # Martin A. Hansen, August 2008.
4695 # Find and remove adaptor from sequences in the stream.
4697 my ( $in, # handle to in stream
4698 $out, # handle to out stream
4699 $options, # options hash
4704 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4706 $options->{ "remove" } ||= "after";
4708 $max_mismatch = $options->{ "mismatches" } || 0;
4709 $offset = $options->{ "offset" };
4711 if ( not defined $offset ) {
4717 $adaptor = uc $options->{ "adaptor" };
4718 $adaptor_len = length $adaptor;
4720 while ( $record = get_record( $in ) )
4722 if ( $record->{ "SEQ" } )
4724 $seq = uc $record->{ "SEQ" };
4725 $seq_len = length $seq;
4727 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
4729 $record->{ "ADAPTOR_POS" } = $pos;
4731 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
4733 if ( $options->{ "remove" } eq "after" )
4735 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4736 $record->{ "SEQ_LEN" } = $pos;
4740 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
4741 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
4745 put_record( $record, $out );
4749 put_record( $record, $out );
4755 sub script_rename_keys
4757 # Martin A. Hansen, August 2007.
4759 # Rename keys in stream.
4761 my ( $in, # handle to in stream
4762 $out, # handle to out stream
4763 $options, # options hash
4770 while ( $record = get_record( $in ) )
4772 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4774 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4776 delete $record->{ $options->{ "keys" }->[ 0 ] };
4779 put_record( $record, $out );
4784 sub script_uniq_vals
4786 # Martin A. Hansen, August 2007.
4788 # Find unique values in stream.
4790 my ( $in, # handle to in stream
4791 $out, # handle to out stream
4792 $options, # options hash
4797 my ( %hash, $record );
4799 while ( $record = get_record( $in ) )
4801 if ( $record->{ $options->{ "key" } } )
4803 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4805 put_record( $record, $out );
4807 $hash{ $record->{ $options->{ "key" } } } = 1;
4809 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4811 put_record( $record, $out );
4815 $hash{ $record->{ $options->{ "key" } } } = 1;
4820 put_record( $record, $out );
4826 sub script_merge_vals
4828 # Martin A. Hansen, August 2007.
4830 # Rename keys in stream.
4832 my ( $in, # handle to in stream
4833 $out, # handle to out stream
4834 $options, # options hash
4839 my ( $record, @join, $i );
4841 $options->{ "delimit" } ||= '_';
4843 while ( $record = get_record( $in ) )
4845 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4847 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4849 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4850 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4853 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4856 put_record( $record, $out );
4861 sub script_merge_records
4863 # Martin A. Hansen, July 2008.
4865 # Merges records in the stream based on identical values of two given keys.
4867 my ( $in, # handle to in stream
4868 $out, # handle to out stream
4869 $options, # options hash
4874 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4875 $num1, $num2, $num, $cmp, $i );
4877 $merge = $options->{ "merge" } || "AandB";
4879 $file1 = "$BP_TMP/merge_records1.tmp";
4880 $file2 = "$BP_TMP/merge_records2.tmp";
4882 $fh1 = Maasha::Common::write_open( $file1 );
4883 $fh2 = Maasha::Common::write_open( $file2 );
4885 $key1 = $options->{ "keys" }->[ 0 ];
4886 $key2 = $options->{ "keys" }->[ 1 ];
4888 $num = $key2 =~ s/n$//;
4892 while ( $record = get_record( $in ) )
4894 if ( exists $record->{ $key1 } )
4897 @vals1 = $record->{ $key1 };
4899 delete $record->{ $key1 };
4901 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4903 print $fh1 join( "\t", @vals1 ), "\n";
4907 elsif ( exists $record->{ $key2 } )
4910 @vals2 = $record->{ $key2 };
4912 delete $record->{ $key2 };
4914 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4916 print $fh2 join( "\t", @vals2 ), "\n";
4927 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4928 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4932 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4933 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4936 $fh1 = Maasha::Common::read_open( $file1 );
4937 $fh2 = Maasha::Common::read_open( $file2 );
4939 @vals1 = Maasha::Common::get_fields( $fh1 );
4940 @vals2 = Maasha::Common::get_fields( $fh2 );
4942 while ( $num1 > 0 and $num2 > 0 )
4947 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4949 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4954 if ( $merge =~ /^(AorB|AnotB)$/ )
4956 for ( $i = 0; $i < @keys1; $i++ ) {
4957 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4960 put_record( $record, $out );
4963 @vals1 = Maasha::Common::get_fields( $fh1 );
4968 if ( $merge =~ /^(BorA|BnotA)$/ )
4970 for ( $i = 0; $i < @keys2; $i++ ) {
4971 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4974 put_record( $record, $out );
4977 @vals2 = Maasha::Common::get_fields( $fh2 );
4982 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4984 for ( $i = 0; $i < @keys1; $i++ ) {
4985 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4988 for ( $i = 1; $i < @keys2; $i++ ) {
4989 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4992 put_record( $record, $out );
4995 @vals1 = Maasha::Common::get_fields( $fh1 );
4996 @vals2 = Maasha::Common::get_fields( $fh2 );
5008 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
5012 for ( $i = 0; $i < @keys1; $i++ ) {
5013 $record->{ $keys1[ $i ] } = $vals1[ $i ];
5016 put_record( $record, $out );
5019 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
5023 for ( $i = 0; $i < @keys2; $i++ ) {
5024 $record->{ $keys2[ $i ] } = $vals2[ $i ];
5027 put_record( $record, $out );
5034 # Martin A. Hansen, August 2007.
5036 # Grab for records in stream.
5038 my ( $in, # handle to in stream
5039 $out, # handle to out stream
5040 $options, # options hash
5045 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
5047 if ( $options->{ "patterns" } )
5049 $patterns = [ split ",", $options->{ "patterns" } ];
5051 elsif ( -f $options->{ "patterns_in" } )
5053 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
5055 elsif ( -f $options->{ "exact_in" } )
5057 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
5059 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
5064 if ( $options->{ "eval" } )
5066 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
5074 while ( $record = get_record( $in ) )
5080 if ( $options->{ "keys" } )
5082 foreach $key ( @{ $options->{ "keys" } } )
5084 if ( exists $lookup_hash{ $record->{ $key } } )
5093 foreach $key ( keys %{ $record } )
5095 if ( not $options->{ "vals_only" } )
5097 if ( exists $lookup_hash{ $key } )
5104 if ( not $options->{ "keys_only" } )
5106 if ( exists $lookup_hash{ $record->{ $key } } )
5117 foreach $pattern ( @{ $patterns } )
5119 if ( $options->{ "keys" } )
5121 foreach $key ( @{ $options->{ "keys" } } )
5123 $pos = index $record->{ $key }, $pattern;
5125 goto FOUND if $pos >= 0;
5130 foreach $key ( keys %{ $record } )
5132 if ( not $options->{ "vals_only" } )
5134 $pos = index $key, $pattern;
5136 goto FOUND if $pos >= 0;
5139 if ( not $options->{ "keys_only" } )
5141 $pos = index $record->{ $key }, $pattern;
5143 goto FOUND if $pos >= 0;
5149 elsif ( $options->{ "regex" } )
5151 if ( $options->{ "keys" } )
5153 foreach $key ( @{ $options->{ "keys" } } )
5155 if ( $options->{ "case_insensitive" } ) {
5156 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5158 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5161 goto FOUND if $pos >= 0;
5166 foreach $key ( keys %{ $record } )
5168 if ( not $options->{ "vals_only" } )
5170 if ( $options->{ "case_insensitive" } ) {
5171 $pos = 1 if $key =~ /$options->{'regex'}/i;
5173 $pos = 1 if $key =~ /$options->{'regex'}/;
5176 goto FOUND if $pos >= 0;
5179 if ( not $options->{ "keys_only" } )
5181 if ( $options->{ "case_insensitive" } ) {
5182 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5184 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5187 goto FOUND if $pos >= 0;
5192 elsif ( $options->{ "eval" } )
5194 if ( defined $record->{ $key } )
5196 if ( $op eq "<" and $record->{ $key } < $val ) {
5197 $pos = 1 and goto FOUND;
5198 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
5199 $pos = 1 and goto FOUND;
5200 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
5201 $pos = 1 and goto FOUND;
5202 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
5203 $pos = 1 and goto FOUND;
5204 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
5205 $pos = 1 and goto FOUND;
5206 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
5207 $pos = 1 and goto FOUND;
5208 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
5209 $pos = 1 and goto FOUND;
5210 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5211 $pos = 1 and goto FOUND;
5218 if ( $pos >= 0 and not $options->{ "invert" } ) {
5219 put_record( $record, $out );
5220 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5221 put_record( $record, $out );
5229 # Martin A. Hansen, August 2007.
5231 # Evaluate extression for records in stream.
5233 my ( $in, # handle to in stream
5234 $out, # handle to out stream
5235 $options, # options hash
5240 my ( $record, $eval_key, $eval_val, $check, @keys );
5242 while ( $record = get_record( $in ) )
5244 if ( $options->{ "eval" } )
5246 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5254 @keys = split /\W+/, $eval_val;
5255 @keys = grep { ! /^\d+$/ } @keys;
5260 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5262 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5265 put_record( $record, $out );
5272 # Martin A. Hansen, June 2008.
5276 my ( $in, # handle to in stream
5277 $out, # handle to out stream
5278 $options, # options hash
5283 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5285 while ( $record = get_record( $in ) )
5289 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5291 push @rows, $record->{ $key };
5295 push @matrix, [ @rows ];
5300 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5302 foreach $row ( @matrix )
5304 for ( $i = 0; $i < @{ $row }; $i++ ) {
5305 $record->{ "V$i" } = $row->[ $i ];
5308 put_record( $record, $out );
5313 sub script_add_ident
5315 # Martin A. Hansen, May 2008.
5317 # Add a unique identifier to each record in stream.
5319 my ( $in, # handle to in stream
5320 $out, # handle to out stream
5321 $options, # options hash
5326 my ( $record, $key, $prefix, $i );
5328 $key = $options->{ "key" } || "ID";
5329 $prefix = $options->{ "prefix" } || "ID";
5333 while ( $record = get_record( $in ) )
5335 $record->{ $key } = sprintf( "$prefix%08d", $i );
5337 put_record( $record, $out );
5344 sub script_count_records
5346 # Martin A. Hansen, August 2007.
5348 # Count records in stream.
5350 my ( $in, # handle to in stream
5351 $out, # handle to out stream
5352 $options, # options hash
5357 my ( $record, $count, $result, $fh, $line );
5361 if ( $options->{ "no_stream" } )
5363 while ( $line = <$in> )
5367 $count++ if $line eq "---";
5372 while ( $record = get_record( $in ) )
5374 put_record( $record, $out );
5380 $result = { "RECORDS_COUNT" => $count };
5382 $fh = write_stream( $options->{ "data_out" } );
5384 put_record( $result, $fh );
5390 sub script_random_records
5392 # Martin A. Hansen, August 2007.
5394 # Pick a number or random records from stream.
5396 my ( $in, # handle to in stream
5397 $out, # handle to out stream
5398 $options, # options hash
5403 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5405 $options->{ "num" } ||= 10;
5407 $tmp_file = "$BP_TMP/random_records.tmp";
5409 $fh_out = Maasha::Common::write_open( $tmp_file );
5413 while ( $record = get_record( $in ) )
5415 put_record( $record, $fh_out );
5425 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5427 while ( $i < $options->{ "num" } )
5429 $rand = int( rand( $count ) );
5431 if ( not exists $rand_hash{ $rand } )
5433 $rand_hash{ $rand } = 1;
5435 $max = $rand if $rand > $max;
5441 $fh_in = Maasha::Common::read_open( $tmp_file );
5445 while ( $record = get_record( $fh_in ) )
5447 put_record( $record, $out ) if exists $rand_hash{ $count };
5449 last if $count == $max;
5460 sub script_sort_records
5462 # Martin A. Hansen, August 2007.
5464 # Sort to sort records according to keys.
5466 my ( $in, # handle to in stream
5467 $out, # handle to out stream
5468 $options, # options hash
5473 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5475 foreach $key ( @{ $options->{ "keys" } } )
5477 if ( $key =~ s/n$// ) {
5478 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5480 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5484 $sort_str = join " or ", @sort_cmd;
5485 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5487 while ( $record = get_record( $in ) ) {
5488 push @records, $record;
5491 @records = sort $sort_sub @records;
5493 if ( $options->{ "reverse" } )
5495 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5496 put_record( $records[ $i ], $out );
5501 for ( $i = 0; $i < scalar @records; $i++ ) {
5502 put_record( $records[ $i ], $out );
5508 sub script_count_vals
5510 # Martin A. Hansen, August 2007.
5512 # Count records in stream.
5514 my ( $in, # handle to in stream
5515 $out, # handle to out stream
5516 $options, # options hash
5521 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5523 $tmp_file = "$BP_TMP/count_cache.tmp";
5525 $fh_out = Maasha::Common::write_open( $tmp_file );
5530 while ( $record = get_record( $in ) )
5532 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5534 push @records, $record;
5536 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5538 map { put_record( $_, $fh_out ) } @records;
5545 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5556 $fh_in = Maasha::Common::read_open( $tmp_file );
5558 while ( $record = get_record( $fh_in ) )
5560 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5562 put_record( $record, $out );
5564 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5572 foreach $record ( @records )
5574 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5576 put_record( $record, $out );
5583 sub script_plot_histogram
5585 # Martin A. Hansen, September 2007.
5587 # Plot a simple histogram for a given key using GNU plot.
5589 my ( $in, # handle to in stream
5590 $out, # handle to out stream
5591 $options, # options hash
5596 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5598 $options->{ "title" } ||= "Histogram";
5599 $options->{ "sort" } ||= "num";
5601 while ( $record = get_record( $in ) )
5603 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5605 put_record( $record, $out ) if not $options->{ "no_stream" };
5608 if ( $options->{ "sort" } eq "num" ) {
5609 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5611 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5614 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5616 $fh = write_stream( $options->{ "data_out" } );
5618 print $fh "$_\n" foreach @{ $result };
5624 sub script_plot_lendist
5626 # Martin A. Hansen, August 2007.
5628 # Plot length distribution using GNU plot.
5630 my ( $in, # handle to in stream
5631 $out, # handle to out stream
5632 $options, # options hash
5637 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5639 $options->{ "title" } ||= "Length Distribution";
5641 while ( $record = get_record( $in ) )
5643 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
5645 put_record( $record, $out ) if not $options->{ "no_stream" };
5648 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5650 for ( $i = 0; $i < $max; $i++ ) {
5651 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5654 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5656 $fh = write_stream( $options->{ "data_out" } );
5658 print $fh "$_\n" foreach @{ $result };
5664 sub script_plot_chrdist
5666 # Martin A. Hansen, August 2007.
5668 # Plot chromosome distribution using GNU plot.
5670 my ( $in, # handle to in stream
5671 $out, # handle to out stream
5672 $options, # options hash
5677 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5679 $options->{ "title" } ||= "Chromosome Distribution";
5681 while ( $record = get_record( $in ) )
5683 if ( $record->{ "CHR" } ) { # generic
5684 $data_hash{ $record->{ "CHR" } }++;
5685 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5686 $data_hash{ $record->{ "S_ID" } }++;
5687 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5688 $data_hash{ $record->{ "S_ID" } }++;
5689 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5690 $data_hash{ $record->{ "S_ID" } }++;
5693 put_record( $record, $out ) if not $options->{ "no_stream" };
5696 foreach $elem ( keys %data_hash )
5700 $sort_key =~ s/chr//i;
5702 $sort_key =~ s/^X(.*)/99$1/;
5703 $sort_key =~ s/^Y(.*)/99$1/;
5704 $sort_key =~ s/^Z(.*)/999$1/;
5705 $sort_key =~ s/^M(.*)/9999$1/;
5706 $sort_key =~ s/^U(.*)/99999$1/;
5708 $count = $sort_key =~ tr/_//;
5710 $sort_key =~ s/_.*/"999999" x $count/ex;
5712 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5715 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5717 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5719 $fh = write_stream( $options->{ "data_out" } );
5721 print $fh "$_\n" foreach @{ $result };
5727 sub script_plot_karyogram
5729 # Martin A. Hansen, August 2007.
5731 # Plot hits on karyogram.
5733 my ( $in, # handle to in stream
5734 $out, # handle to out stream
5735 $options, # options hash
5740 my ( %options, $record, @data, $fh, $result, %data_hash );
5742 $options->{ "genome" } ||= "human";
5743 $options->{ "feat_color" } ||= "black";
5745 while ( $record = get_record( $in ) )
5747 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5749 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5752 put_record( $record, $out ) if not $options->{ "no_stream" };
5755 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5757 $fh = write_stream( $options->{ "data_out" } );
5765 sub script_plot_matches
5767 # Martin A. Hansen, August 2007.
5769 # Plot matches in 2D generating a dotplot.
5771 my ( $in, # handle to in stream
5772 $out, # handle to out stream
5773 $options, # options hash
5778 my ( $record, @data, $fh, $result, %data_hash );
5780 $options->{ "direction" } ||= "both";
5782 while ( $record = get_record( $in ) )
5784 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5785 push @data, $record;
5788 put_record( $record, $out ) if not $options->{ "no_stream" };
5791 $options->{ "title" } ||= "plot_matches";
5792 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5793 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5795 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5797 $fh = write_stream( $options->{ "data_out" } );
5799 print $fh "$_\n" foreach @{ $result };
5805 sub script_length_vals
5807 # Martin A. Hansen, August 2007.
5809 # Determine the length of the value for given keys.
5811 my ( $in, # handle to in stream
5812 $out, # handle to out stream
5813 $options, # options hash
5818 my ( $record, $key );
5820 while ( $record = get_record( $in ) )
5822 foreach $key ( @{ $options->{ "keys" } } )
5824 if ( $record->{ $key } ) {
5825 $record->{ $key . "_LEN" } = length $record->{ $key };
5829 put_record( $record, $out );
5836 # Martin A. Hansen, August 2007.
5838 # Calculates the sums for values of given keys.
5840 my ( $in, # handle to in stream
5841 $out, # handle to out stream
5842 $options, # options hash
5847 my ( $record, $key, %sum_hash, $fh );
5849 while ( $record = get_record( $in ) )
5851 foreach $key ( @{ $options->{ "keys" } } )
5853 if ( $record->{ $key } ) {
5854 $sum_hash{ $key } += $record->{ $key };
5858 put_record( $record, $out ) if not $options->{ "no_stream" };
5861 $fh = write_stream( $options->{ "data_out" } );
5863 foreach $key ( @{ $options->{ "keys" } } ) {
5864 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5871 sub script_mean_vals
5873 # Martin A. Hansen, August 2007.
5875 # Calculate the mean of values of given keys.
5877 my ( $in, # handle to in stream
5878 $out, # handle to out stream
5879 $options, # options hash
5884 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5886 while ( $record = get_record( $in ) )
5888 foreach $key ( @{ $options->{ "keys" } } )
5890 if ( $record->{ $key } )
5892 $sum_hash{ $key } += $record->{ $key };
5893 $count_hash{ $key }++;
5897 put_record( $record, $out ) if not $options->{ "no_stream" };
5900 $fh = write_stream( $options->{ "data_out" } );
5902 foreach $key ( @{ $options->{ "keys" } } )
5904 if ( $count_hash{ $key } ) {
5905 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5910 put_record( { $key . "_MEAN" => $mean } , $fh );
5917 sub script_median_vals
5919 # Martin A. Hansen, March 2008.
5921 # Calculate the median values of given keys.
5923 my ( $in, # handle to in stream
5924 $out, # handle to out stream
5925 $options, # options hash
5930 my ( $record, $key, %median_hash, $median, $fh );
5932 while ( $record = get_record( $in ) )
5934 foreach $key ( @{ $options->{ "keys" } } ) {
5935 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5938 put_record( $record, $out ) if not $options->{ "no_stream" };
5941 $fh = write_stream( $options->{ "data_out" } );
5943 foreach $key ( @{ $options->{ "keys" } } )
5945 if ( $median_hash{ $key } ) {
5946 $median = Maasha::Calc::median( $median_hash{ $key } );
5951 put_record( { $key . "_MEDIAN" => $median } , $fh );
5960 # Martin A. Hansen, February 2008.
5962 # Determine the maximum values of given keys.
5964 my ( $in, # handle to in stream
5965 $out, # handle to out stream
5966 $options, # options hash
5971 my ( $record, $key, $fh, %max_hash, $max_record );
5973 while ( $record = get_record( $in ) )
5975 foreach $key ( @{ $options->{ "keys" } } )
5977 if ( $record->{ $key } )
5979 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5983 put_record( $record, $out ) if not $options->{ "no_stream" };
5986 $fh = write_stream( $options->{ "data_out" } );
5988 foreach $key ( @{ $options->{ "keys" } } )
5990 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5993 put_record( $max_record, $fh );
6001 # Martin A. Hansen, February 2008.
6003 # Determine the minimum values of given keys.
6005 my ( $in, # handle to in stream
6006 $out, # handle to out stream
6007 $options, # options hash
6012 my ( $record, $key, $fh, %min_hash, $min_record );
6014 while ( $record = get_record( $in ) )
6016 foreach $key ( @{ $options->{ "keys" } } )
6018 if ( defined $record->{ $key } )
6020 if ( exists $min_hash{ $key } ) {
6021 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
6023 $min_hash{ $key } = $record->{ $key };
6028 put_record( $record, $out ) if not $options->{ "no_stream" };
6031 $fh = write_stream( $options->{ "data_out" } );
6033 foreach $key ( @{ $options->{ "keys" } } )
6035 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
6038 put_record( $min_record, $fh );
6044 sub script_upload_to_ucsc
6046 # Martin A. Hansen, August 2007.
6048 # Calculate the mean of values of given keys.
6050 # This routine has developed into the most ugly hack. Do something!
6052 my ( $in, # handle to in stream
6053 $out, # handle to out stream
6054 $options, # options hash
6059 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $vals );
6061 $options->{ "short_label" } ||= $options->{ 'table' };
6062 $options->{ "long_label" } ||= $options->{ 'table' };
6063 $options->{ "group" } ||= $ENV{ "LOGNAME" };
6064 $options->{ "priority" } ||= 1;
6065 $options->{ "visibility" } ||= "pack";
6066 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
6067 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
6069 $file = "$BP_TMP/ucsc_upload.tmp";
6077 $fh_out = Maasha::Common::write_open( $file );
6079 while ( $record = get_record( $in ) )
6081 put_record( $record, $out ) if not $options->{ "no_stream" };
6083 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6085 $vals = $record->{ "VALS" };
6088 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6089 print $fh_out "$vals\n";
6091 $format = "WIGGLE" if not $format;
6093 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6095 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6096 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6100 $format = "PSL" if not $format;
6102 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6104 # chrom chromStart chromEnd name score strand size secStr conf
6106 print $fh_out join ( "\t",
6108 $record->{ "CHR_BEG" },
6109 $record->{ "CHR_END" } + 1,
6110 $record->{ "Q_ID" },
6111 $record->{ "SCORE" },
6112 $record->{ "STRAND" },
6113 $record->{ "SIZE" },
6114 $record->{ "SEC_STRUCT" },
6115 $record->{ "CONF" },
6118 $format = "BED_SS" if not $format;
6120 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6122 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6124 $format = "BED" if not $format;
6125 $columns = $record->{ "BED_COLS" } if not $columns;
6127 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6129 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6131 $format = "BED" if not $format;
6132 $columns = 6 if not $columns;
6134 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6136 $record->{ "CHR" } = $record->{ "S_ID" };
6137 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6138 $record->{ "CHR_END" } = $record->{ "S_END" };
6139 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6141 $format = "BED" if not $format;
6142 $columns = 6 if not $columns;
6144 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6146 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6148 $record->{ "CHR" } = $record->{ "S_ID" };
6149 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6150 $record->{ "CHR_END" } = $record->{ "S_END" };
6151 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6152 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6154 $format = "BED" if not $format;
6155 $columns = 6 if not $columns;
6157 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6160 if ( $i == $options->{ "chunk_size" } )
6164 if ( $format eq "BED" ) {
6165 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6166 } elsif ( $format eq "PSL" ) {
6167 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6176 $fh_out = Maasha::Common::write_open( $file );
6184 if ( exists $options->{ "database" } and $options->{ "table" } )
6186 if ( $format eq "BED" )
6188 $type = "bed $columns";
6190 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6192 elsif ( $format eq "BED_SS" )
6194 $options->{ "sec_struct" } = 1;
6196 $type = "sec_struct";
6198 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6200 elsif ( $format eq "PSL" )
6204 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6206 elsif ( $format eq "WIGGLE" )
6208 $options->{ "visibility" } = "full";
6210 $wig_file = "$options->{ 'table' }.wig";
6211 $wib_file = "$options->{ 'table' }.wib";
6213 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6215 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6217 if ( $options->{ 'verbose' } ) {
6218 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6220 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6223 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6231 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6236 Maasha::UCSC::update_my_tracks( $options, $type );
6241 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6246 # Martin A. Hansen, July 2008.
6248 # Given a biopiece record converts it to a FASTA record.
6249 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6250 # tried in that order.
6252 my ( $record, # record
6257 my ( $seq_name, $seq );
6259 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6260 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6262 if ( defined $seq_name and defined $seq ) {
6263 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6272 # Martin A. Hansen, July 2007.
6274 # Opens a stream to STDIN or a file,
6276 my ( $path, # path - OPTIONAL
6279 # Returns filehandle.
6283 if ( not -t STDIN ) {
6284 $fh = Maasha::Common::read_stdin();
6285 } elsif ( not $path ) {
6286 # Maasha::Common::error( qq(no data stream) );
6288 $fh = Maasha::Common::read_open( $path );
6291 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6299 # Martin A. Hansen, August 2007.
6301 # Opens a stream to STDOUT or a file.
6303 my ( $path, # path - OPTIONAL
6304 $gzip, # compress data - OPTIONAL
6307 # Returns filehandle.
6312 $fh = Maasha::Common::write_open( $path, $gzip );
6314 $fh = Maasha::Common::write_stdout();
6323 # Martin A. Hansen, July 2007.
6325 # Reads one record at a time and converts that record
6326 # to a Perl data structure (a hash) which is returned.
6328 my ( $fh, # handle to stream
6333 my ( $block, @lines, $line, $key, $value, %record );
6335 local $/ = "\n---\n";
6341 return if not defined $block;
6343 @lines = split "\n", $block;
6345 foreach $line ( @lines )
6347 ( $key, $value ) = split ": ", $line, 2;
6349 $record{ $key } = $value;
6352 return wantarray ? %record : \%record;
6358 # Martin A. Hansen, July 2007.
6360 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6362 my ( $data, # data structure
6363 $fh, # file handle - OPTIONAL
6368 if ( scalar keys %{ $data } )
6372 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6377 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6388 # Martin A. Hansen, November 2007.
6390 # Extracts files from an explicit GetOpt::Long argument
6391 # allowing for the use of glob. E.g.
6392 # --data_in=test.fna
6393 # --data_in=test.fna,test2.fna
6395 # --data_in=test.fna,/dir/*.fna
6397 my ( $option, # option from GetOpt::Long
6402 my ( $elem, @files );
6404 foreach $elem ( split ",", $option )
6408 } elsif ( $elem =~ /\*/ ) {
6409 push @files, glob( $elem );
6413 return wantarray ? @files : \@files;
6419 # Martin A. Hansen, April 2008.
6421 # Removes temporary directory and exits gracefully.
6422 # This subroutine is meant to be run always as the last
6423 # thing even if a script is dies or is interrupted
6426 my ( $sig, # signal from the %SIG
6429 # print STDERR "signal->$sig<-\n";
6437 if ( $sig =~ /MAASHA_ERROR/ ) {
6438 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6439 } elsif ( $sig eq "INT" ) {
6440 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6441 } elsif ( $sig eq "TERM" ) {
6442 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6444 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6456 # Martin A. Hansen, July 2008.
6458 # Cleans out any unused temporary files and directories in BP_TMP.
6462 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6464 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6466 $curr_pid = Maasha::Common::get_processid();
6468 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6470 foreach $dir ( @dirs )
6472 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6478 if ( $user eq Maasha::Common::get_user() )
6480 if ( not Maasha::Common::process_running( $pid ) )
6482 # print STDERR "Removing stale dir: $dir\n";
6483 Maasha::Common::dir_remove( $dir );
6485 elsif ( $pid == $curr_pid )
6487 # print STDERR "Removing current dir: $dir\n";
6488 # Maasha::Common::dir_remove( $dir );
6502 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<