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 );
78 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
81 $SIG{ '__DIE__' } = \&sig_handler;
82 $SIG{ 'INT' } = \&sig_handler;
83 $SIG{ 'TERM' } = \&sig_handler;
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
89 my ( $script, $BP_TMP );
91 $script = Maasha::Common::get_scriptname();
92 $BP_TMP = Maasha::Common::get_tmpdir();
95 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
98 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
99 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
101 $log_global->autoflush( 1 );
102 $log_local->autoflush( 1 );
104 &log( $log_global, $script, \@ARGV );
105 &log( $log_local, $script, \@ARGV );
111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114 my $t0 = gettimeofday();
116 run_script( $script );
118 my $t1 = gettimeofday();
120 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128 # Martin A. Hansen, January 2008.
130 # Log messages to logfile.
132 my ( $fh, # filehandle to logfile
133 $script, # script name
134 $argv, # reference to @ARGV
139 my ( $time_stamp, $user );
141 $time_stamp = Maasha::Common::time_stamp();
143 $user = $ENV{ 'USER' };
145 $script = "biopieces" if $script eq "-e";
147 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
153 # Martin A. Hansen, August 2007.
155 # Run a specific script.
157 my ( $script, # script name
162 my ( $options, $in, $out );
164 $options = get_options( $script );
166 $options->{ "SCRIPT" } = $script;
168 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
169 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
172 $in = read_stream( $options->{ "stream_in" } );
173 $out = write_stream( $options->{ "stream_out" } );
175 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
176 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
177 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
178 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
179 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
181 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
182 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
183 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
184 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
185 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
186 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
187 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
188 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
189 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
190 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
191 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
192 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
193 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
194 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
195 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
196 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
198 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
199 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
200 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
201 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
202 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
203 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
204 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
205 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
206 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
208 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
209 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
213 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
214 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
216 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
217 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
218 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
219 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
220 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
221 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
222 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
223 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
224 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
225 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
226 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
227 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
228 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
229 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
230 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
231 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
232 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
233 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
234 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
235 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
236 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
237 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
238 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
239 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
240 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
241 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
242 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
243 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
244 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
245 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
246 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
247 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
248 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
249 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
250 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
251 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
252 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
253 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
254 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
255 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
256 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
257 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
258 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
259 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
260 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
261 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
262 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
263 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
264 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
265 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
266 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
268 close $in if defined $in;
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" )
397 elsif ( $script eq "read_solid" )
405 elsif ( $script eq "read_mysql" )
414 elsif ( $script eq "format_genome" )
423 elsif ( $script eq "length_seq" )
430 elsif ( $script eq "oligo_freq" )
437 elsif ( $script eq "create_weight_matrix" )
443 elsif ( $script eq "transliterate_seq" )
451 elsif ( $script eq "transliterate_vals" )
460 elsif ( $script eq "translate_seq" )
466 elsif ( $script eq "extract_seq" )
474 elsif ( $script eq "get_genome_seq" )
486 elsif ( $script eq "get_genome_align" )
497 elsif ( $script eq "get_genome_phastcons" )
508 elsif ( $script eq "split_seq" )
515 elsif ( $script eq "split_bed" )
522 elsif ( $script eq "tile_seq" )
529 elsif ( $script eq "invert_align" )
535 elsif ( $script eq "patscan_seq" )
546 elsif ( $script eq "create_blast_db" )
553 elsif ( $script eq "blast_seq" )
565 elsif ( $script eq "blat_seq" )
577 elsif ( $script eq "soap_seq" )
588 elsif ( $script eq "match_seq" )
595 elsif ( $script eq "create_vmatch_index" )
603 elsif ( $script eq "vmatch_seq" )
614 elsif ( $script eq "write_fasta" )
623 elsif ( $script eq "write_align" )
633 elsif ( $script eq "write_blast" )
642 elsif ( $script eq "write_tab" )
654 elsif ( $script eq "write_bed" )
662 elsif ( $script eq "write_psl" )
670 elsif ( $script eq "write_fixedstep" )
678 elsif ( $script eq "write_2bit" )
686 elsif ( $script eq "write_solid" )
695 elsif ( $script eq "plot_seqlogo" )
702 elsif ( $script eq "plot_phastcons_profiles" )
717 elsif ( $script eq "analyze_vals" )
724 elsif ( $script eq "head_records" )
730 elsif ( $script eq "remove_keys" )
737 elsif ( $script eq "remove_adaptor" )
746 elsif ( $script eq "rename_keys" )
752 elsif ( $script eq "uniq_vals" )
759 elsif ( $script eq "merge_vals" )
766 elsif ( $script eq "merge_records" )
773 elsif ( $script eq "grab" )
788 elsif ( $script eq "compute" )
794 elsif ( $script eq "add_ident" )
801 elsif ( $script eq "count_records" )
808 elsif ( $script eq "random_records" )
814 elsif ( $script eq "sort_records" )
821 elsif ( $script eq "count_vals" )
827 elsif ( $script eq "plot_histogram" )
840 elsif ( $script eq "plot_lendist" )
852 elsif ( $script eq "plot_chrdist" )
863 elsif ( $script eq "plot_karyogram" )
872 elsif ( $script eq "plot_matches" )
884 elsif ( $script eq "length_vals" )
890 elsif ( $script eq "sum_vals" )
898 elsif ( $script eq "mean_vals" )
906 elsif ( $script eq "median_vals" )
914 elsif ( $script eq "max_vals" )
922 elsif ( $script eq "min_vals" )
930 elsif ( $script eq "upload_to_ucsc" )
957 # print STDERR Dumper( \@options );
964 # print STDERR Dumper( \%options );
966 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
967 return wantarray ? %options : \%options;
970 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
971 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
972 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
973 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
974 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
975 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
976 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
977 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
978 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
980 # ---- check arguments ----
982 if ( $options{ 'data_in' } )
984 $options{ "files" } = getopt_files( $options{ 'data_in' } );
986 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
989 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
991 # print STDERR Dumper( \%options );
993 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
995 foreach $opt ( keys %options )
997 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
999 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1001 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1003 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1005 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1007 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1009 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1011 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1013 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1015 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1017 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1019 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1021 elsif ( $opt eq "genome" and $script ne "format_genome" )
1023 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1024 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1026 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1027 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1030 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1032 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1034 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1036 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1038 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1040 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1044 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1045 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1046 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1047 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1048 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1049 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1050 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1051 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1052 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" };
1053 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1054 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" };
1056 if ( $script eq "upload_to_ucsc" )
1058 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1059 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1062 return wantarray ? %options : \%options;
1066 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1069 sub script_print_usage
1071 # Martin A. Hansen, January 2008.
1073 # Retrieves usage information from file and
1074 # prints this nicely formatted.
1076 my ( $in, # handle to in stream
1077 $out, # handle to out stream
1078 $options, # options hash
1083 my ( $file, $wiki, $lines );
1085 if ( $options->{ 'data_in' } ) {
1086 $file = $options->{ 'data_in' };
1088 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1091 $wiki = Maasha::Gwiki::gwiki_read( $file );
1093 if ( not $options->{ "help" } ) {
1094 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1097 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1099 print STDERR "$_\n" foreach @{ $lines };
1105 sub script_list_biopieces
1107 # Martin A. Hansen, January 2008.
1109 # Prints the synopsis from the usage for each of the biopieces.
1111 my ( $in, # handle to in stream
1112 $out, # handle to out stream
1113 $options, # options hash
1118 my ( @files, $file, $wiki, $program, $synopsis );
1120 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1122 foreach $file ( sort @files )
1124 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1128 $wiki = Maasha::Gwiki::gwiki_read( $file );
1130 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1131 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1133 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1134 $synopsis =~ s/!(\w)/$1/g;
1136 printf( "%-30s%s\n", $program, $synopsis );
1144 sub script_list_genomes
1146 # Martin A. Hansen, January 2008.
1148 # Prints the synopsis from the usage for each of the biopieces.
1150 my ( $in, # handle to in stream
1151 $out, # handle to out stream
1152 $options, # options hash
1157 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1159 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1161 foreach $genome ( @genomes )
1163 next if $genome =~ /\.$/;
1165 @formats = Maasha::Common::ls_dirs( $genome );
1167 foreach $format ( @formats )
1169 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1171 $hash{ $1 }{ $2 } = 1;
1180 map { push @row, $_ } sort keys %found;
1182 print join( "\t", @row ), "\n";
1184 foreach $genome ( sort keys %hash )
1188 foreach $format ( sort keys %found )
1190 if ( exists $hash{ $genome }{ $format } ) {
1197 print join( "\t", @row ), "\n";
1202 sub script_read_fasta
1204 # Martin A. Hansen, August 2007.
1206 # Read sequences from FASTA file.
1208 my ( $in, # handle to in stream
1209 $out, # handle to out stream
1210 $options, # options hash
1215 my ( $record, $file, $data_in, $entry, $num );
1217 while ( $record = get_record( $in ) ) {
1218 put_record( $record, $out );
1223 foreach $file ( @{ $options->{ "files" } } )
1225 $data_in = Maasha::Common::read_open( $file );
1227 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1229 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1232 SEQ_NAME => $entry->[ SEQ_NAME ],
1233 SEQ => $entry->[ SEQ ],
1234 SEQ_LEN => length $entry->[ SEQ ],
1237 put_record( $record, $out );
1240 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1250 close $data_in if $data_in;
1256 # Martin A. Hansen, August 2007.
1258 # Read table or table columns from stream or file.
1260 my ( $in, # handle to in stream
1261 $out, # handle to out stream
1262 $options, # options hash
1267 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1269 $options->{ 'delimit' } ||= '\s+';
1271 while ( $record = get_record( $in ) ) {
1272 put_record( $record, $out );
1275 $skip = $options->{ 'skip' } ||= 0;
1278 foreach $file ( @{ $options->{ "files" } } )
1280 $data_in = Maasha::Common::read_open( $file );
1282 while ( $line = <$data_in> )
1290 next if $line =~ /^#|^$/;
1297 @fields = split /$options->{'delimit'}/, $line;
1299 if ( $options->{ "cols" } ) {
1300 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1305 for ( $i = 0; $i < @fields2; $i++ )
1307 if ( $options->{ "keys" }->[ $i ] ) {
1308 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1310 $record->{ "V" . $i } = $fields2[ $i ];
1314 put_record( $record, $out );
1316 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1326 close $data_in if $data_in;
1332 # Martin A. Hansen, August 2007.
1334 # Read psl table from stream or file.
1336 my ( $in, # handle to in stream
1337 $out, # handle to out stream
1338 $options, # options hash
1343 my ( $record, $file, $data_in, $num );
1345 while ( $record = get_record( $in ) ) {
1346 put_record( $record, $out );
1351 foreach $file ( @{ $options->{ "files" } } )
1353 $data_in = Maasha::Common::read_open( $file );
1355 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1357 put_record( $record, $out );
1359 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1371 # Martin A. Hansen, August 2007.
1373 # Read bed table from stream or file.
1375 my ( $in, # handle to in stream
1376 $out, # handle to out stream
1377 $options, # options hash
1382 my ( $file, $record, $entry, $data_in, $num );
1384 while ( $record = get_record( $in ) ) {
1385 put_record( $record, $out );
1390 foreach $file ( @{ $options->{ "files" } } )
1392 $data_in = Maasha::Common::read_open( $file );
1394 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1396 put_record( $entry, $out );
1398 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1408 close $data_in if $data_in;
1412 sub script_read_fixedstep
1414 # Martin A. Hansen, Juli 2008.
1416 # Read fixedStep wiggle format from stream or file.
1418 my ( $in, # handle to in stream
1419 $out, # handle to out stream
1420 $options, # options hash
1425 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1427 while ( $record = get_record( $in ) ) {
1428 put_record( $record, $out );
1433 foreach $file ( @{ $options->{ "files" } } )
1435 $data_in = Maasha::Common::read_open( $file );
1437 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1439 $head = shift @{ $entry };
1441 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1443 $record->{ "REC_TYPE" } = "fixed_step";
1444 $record->{ "CHR" } = $1;
1445 $record->{ "CHR_BEG" } = $2;
1446 $record->{ "STEP" } = $3;
1447 $record->{ "VALS" } = join ",", @{ $entry };
1450 put_record( $record, $out );
1452 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1462 close $data_in if $data_in;
1466 sub script_read_blast_tab
1468 # Martin A. Hansen, September 2007.
1470 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1472 my ( $in, # handle to in stream
1473 $out, # handle to out stream
1474 $options, # options hash
1479 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1481 while ( $record = get_record( $in ) ) {
1482 put_record( $record, $out );
1487 foreach $file ( @{ $options->{ "files" } } )
1489 $data_in = Maasha::Common::read_open( $file );
1491 while ( $line = <$data_in> )
1495 next if $line =~ /^#/;
1497 @fields = split /\t/, $line;
1499 $record->{ "REC_TYPE" } = "BLAST";
1500 $record->{ "Q_ID" } = $fields[ 0 ];
1501 $record->{ "S_ID" } = $fields[ 1 ];
1502 $record->{ "IDENT" } = $fields[ 2 ];
1503 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1504 $record->{ "MISMATCHES" } = $fields[ 4 ];
1505 $record->{ "GAPS" } = $fields[ 5 ];
1506 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1507 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1508 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1509 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1510 $record->{ "E_VAL" } = $fields[ 10 ];
1511 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1513 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1515 $record->{ "STRAND" } = '-';
1517 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1521 $record->{ "STRAND" } = '+';
1524 put_record( $record, $out );
1526 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1536 close $data_in if $data_in;
1540 sub script_read_embl
1542 # Martin A. Hansen, August 2007.
1546 my ( $in, # handle to in stream
1547 $out, # handle to out stream
1548 $options, # options hash
1553 my ( %options2, $file, $data_in, $num, $entry, $record );
1555 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1556 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1557 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1559 while ( $record = get_record( $in ) ) {
1560 put_record( $record, $out );
1565 foreach $file ( @{ $options->{ "files" } } )
1567 $data_in = Maasha::Common::read_open( $file );
1569 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1571 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1573 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1575 $record_copy = dclone $record;
1577 delete $record_copy->{ "FT" };
1579 put_record( $record_copy, $out );
1581 delete $record_copy->{ "SEQ" };
1583 foreach $feat ( keys %{ $record->{ "FT" } } )
1585 $record_copy->{ "FEAT_TYPE" } = $feat;
1587 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1589 foreach $qual ( keys %{ $feat2 } )
1591 $qual_val = join "; ", @{ $feat2->{ $qual } };
1596 $record_copy->{ $qual } = $qual_val;
1599 put_record( $record_copy, $out );
1603 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1613 close $data_in if $data_in;
1617 sub script_read_stockholm
1619 # Martin A. Hansen, August 2007.
1621 # Read Stockholm format.
1623 my ( $in, # handle to in stream
1624 $out, # handle to out stream
1625 $options, # options hash
1630 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1632 while ( $record = get_record( $in ) ) {
1633 put_record( $record, $out );
1638 foreach $file ( @{ $options->{ "files" } } )
1640 $data_in = Maasha::Common::read_open( $file );
1642 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1644 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1648 foreach $key ( keys %{ $record->{ "GF" } } ) {
1649 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1652 $record_anno->{ "ALIGN" } = $num;
1654 put_record( $record_anno, $out );
1656 foreach $seq ( @{ $record->{ "ALIGN" } } )
1658 undef $record_align;
1661 SEQ_NAME => $seq->[ 0 ],
1665 put_record( $record_align, $out );
1668 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1678 close $data_in if $data_in;
1682 sub script_read_phastcons
1684 # Martin A. Hansen, December 2007.
1686 # Read PhastCons format.
1688 my ( $in, # handle to in stream
1689 $out, # handle to out stream
1690 $options, # options hash
1695 my ( $data_in, $file, $num, $entry, @records, $record );
1697 $options->{ "min" } ||= 10;
1698 $options->{ "dist" } ||= 25;
1699 $options->{ "threshold" } ||= 0.8;
1700 $options->{ "gap" } ||= 5;
1702 while ( $record = get_record( $in ) ) {
1703 put_record( $record, $out );
1708 foreach $file ( @{ $options->{ "files" } } )
1710 $data_in = Maasha::Common::read_open( $file );
1712 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1714 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1716 foreach $record ( @records )
1718 $record->{ "REC_TYPE" } = "BED";
1719 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1721 put_record( $record, $out );
1723 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1734 close $data_in if $data_in;
1738 sub script_read_soft
1740 # Martin A. Hansen, December 2007.
1743 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1745 my ( $in, # handle to in stream
1746 $out, # handle to out stream
1747 $options, # options hash
1752 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1754 while ( $record = get_record( $in ) ) {
1755 put_record( $record, $out );
1760 foreach $file ( @{ $options->{ "files" } } )
1762 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1764 $soft_index = Maasha::NCBI::soft_index_file( $file );
1766 $fh = Maasha::Common::read_open( $file );
1768 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1770 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1772 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1774 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1776 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1778 foreach $sample ( @samples )
1781 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1783 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1785 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1787 foreach $record ( @{ $records } )
1789 put_record( $record, $out );
1791 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1796 $old_end = $sample->{ "LINE_END" };
1804 close $data_in if $data_in;
1811 # Martin A. Hansen, February 2008.
1814 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1816 my ( $in, # handle to in stream
1817 $out, # handle to out stream
1818 $options, # options hash
1823 my ( $data_in, $file, $fh, $num, $record, $entry );
1825 while ( $record = get_record( $in ) ) {
1826 put_record( $record, $out );
1831 foreach $file ( @{ $options->{ "files" } } )
1833 $fh = Maasha::Common::read_open( $file );
1835 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1837 put_record( $entry, $out );
1839 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1849 close $data_in if $data_in;
1853 sub script_read_2bit
1855 # Martin A. Hansen, March 2008.
1857 # Read sequences from 2bit file.
1859 my ( $in, # handle to in stream
1860 $out, # handle to out stream
1861 $options, # options hash
1866 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1868 $mask = 1 if not $options->{ "no_mask" };
1870 while ( $record = get_record( $in ) ) {
1871 put_record( $record, $out );
1876 foreach $file ( @{ $options->{ "files" } } )
1878 $data_in = Maasha::Common::read_open( $file );
1880 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1882 foreach $line ( @{ $toc } )
1884 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1885 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1886 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1888 put_record( $record, $out );
1890 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1900 close $data_in if $data_in;
1904 sub script_read_solexa
1906 # Martin A. Hansen, March 2008.
1908 # Read Solexa sequence reads from file.
1910 my ( $in, # handle to in stream
1911 $out, # handle to out stream
1912 $options, # options hash
1917 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1919 $options->{ "quality" } ||= 20;
1921 while ( $record = get_record( $in ) ) {
1922 put_record( $record, $out );
1927 foreach $file ( @{ $options->{ "files" } } )
1929 $data_in = Maasha::Common::read_open( $file );
1931 while ( $entry = Maasha::Solexa::solexa_get_entry( $data_in ) )
1933 @seqs = split //, $entry->[ SEQ ];
1934 @scores = split /:/, $entry->[ SCORE ];
1936 for ( $i = 0; $i < @scores; $i++ ) {
1937 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1940 $record->{ "SEQ_NAME" } = $entry->[ SEQ_NAME ];
1941 $record->{ "SEQ" } = join "", @seqs;
1942 $record->{ "SEQ_LEN" } = scalar @seqs;
1943 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1945 put_record( $record, $out );
1947 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1957 close $data_in if $data_in;
1961 sub script_read_solid
1963 # Martin A. Hansen, April 2008.
1965 # Read Solid sequence from file.
1967 my ( $in, # handle to in stream
1968 $out, # handle to out stream
1969 $options, # options hash
1974 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1976 $options->{ "quality" } ||= 15;
1978 while ( $record = get_record( $in ) ) {
1979 put_record( $record, $out );
1984 foreach $file ( @{ $options->{ "files" } } )
1986 $data_in = Maasha::Common::read_open( $file );
1988 while ( $line = <$data_in> )
1992 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1994 @scores = split /,/, $seq_qual;
1995 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1997 for ( $i = 0; $i < @seqs; $i++ ) {
1998 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2002 SEQ_NAME => $seq_name,
2004 SEQ_QUAL => $seq_qual,
2005 SEQ_LEN => length $seq_cs,
2006 SEQ => join( "", @seqs ),
2007 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2010 put_record( $record, $out );
2012 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2022 close $data_in if $data_in;
2026 sub script_read_mysql
2028 # Martin A. Hansen, May 2008.
2030 # Read a MySQL query into stream.
2032 my ( $in, # handle to in stream
2033 $out, # handle to out stream
2034 $options, # options hash
2039 my ( $record, $dbh, $results );
2041 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2042 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2044 while ( $record = get_record( $in ) ) {
2045 put_record( $record, $out );
2048 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2050 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2052 Maasha::SQL::disconnect( $dbh );
2054 map { put_record( $_ ) } @{ $results };
2058 sub script_format_genome
2060 # Martin A. Hansen, Juli 2008.
2062 # Format a genome to speficed formats.
2064 my ( $in, # handle to in stream
2065 $out, # handle to out stream
2066 $options, # options hash
2071 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2073 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2074 $genome = $options->{ 'genome' };
2076 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2077 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2078 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2080 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2082 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2084 $fasta_dir = "$dir/genomes/$genome/fasta";
2088 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2090 $fasta_dir = "$dir/genomes/$genome/fasta";
2092 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2095 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2097 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2099 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2101 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2104 while ( $record = get_record( $in ) )
2106 if ( $fh_out and $entry = record2fasta( $record ) )
2108 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2110 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2112 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2114 $vals = $record->{ 'VALS' };
2118 print $fh_out "$vals\n";
2121 put_record( $record, $out ) if not $options->{ "no_stream" };
2124 foreach $format ( @{ $options->{ 'formats' } } )
2126 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2127 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2128 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2129 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2130 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2133 close $fh_out if $fh_out;
2137 sub script_length_seq
2139 # Martin A. Hansen, August 2007.
2141 # Determine the length of sequences in stream.
2143 my ( $in, # handle to in stream
2144 $out, # handle to out stream
2145 $options, # options hash
2150 my ( $record, $total );
2152 while ( $record = get_record( $in ) )
2154 if ( $record->{ "SEQ" } )
2156 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2157 $total += $record->{ "SEQ_LEN" };
2160 put_record( $record, $out ) if not $options->{ "no_stream" };
2163 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2167 sub script_uppercase_seq
2169 # Martin A. Hansen, August 2007.
2171 # Uppercases sequences in stream.
2173 my ( $in, # handle to in stream
2174 $out, # handle to out stream
2181 while ( $record = get_record( $in ) )
2183 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2185 put_record( $record, $out );
2190 sub script_shuffle_seq
2192 # Martin A. Hansen, December 2007.
2194 # Shuffle sequences in stream.
2196 my ( $in, # handle to in stream
2197 $out, # handle to out stream
2204 while ( $record = get_record( $in ) )
2206 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2208 put_record( $record, $out );
2213 sub script_analyze_seq
2215 # Martin A. Hansen, August 2007.
2217 # Analyze sequence composition of sequences in stream.
2219 my ( $in, # handle to in stream
2220 $out, # handle to out stream
2225 my ( $record, $analysis );
2227 while ( $record = get_record( $in ) )
2229 if ( $record->{ "SEQ" } )
2231 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2233 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2236 put_record( $record, $out );
2241 sub script_analyze_tags
2243 # Martin A. Hansen, August 2008.
2245 # Analyze sequence tags in stream.
2247 my ( $in, # handle to in stream
2248 $out, # handle to out stream
2253 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2255 while ( $record = get_record( $in ) )
2257 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2259 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2263 $len_hash{ length( $record->{ "SEQ" } ) }++;
2264 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2267 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2269 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2273 $len_hash{ $record->{ "BED_LEN" } }++;
2274 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2279 foreach $key ( sort { $a <=> $b } keys %len_hash )
2281 $tag_record->{ "TAG_LEN" } = $key;
2282 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2283 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2285 put_record( $tag_record, $out );
2290 sub script_complexity_seq
2292 # Martin A. Hansen, May 2008.
2294 # Generates an index calculated as the most common di-residue over
2295 # the sequence length for all sequences in stream.
2297 my ( $in, # handle to in stream
2298 $out, # handle to out stream
2303 my ( $record, $index );
2305 while ( $record = get_record( $in ) )
2307 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2309 put_record( $record, $out );
2314 sub script_oligo_freq
2316 # Martin A. Hansen, August 2007.
2318 # Determine the length of sequences in stream.
2320 my ( $in, # handle to in stream
2321 $out, # handle to out stream
2322 $options, # options hash
2327 my ( $record, %oligos, @freq_table );
2329 $options->{ "word_size" } ||= 7;
2331 while ( $record = get_record( $in ) )
2333 if ( $record->{ "SEQ" } )
2335 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2337 if ( not $options->{ "all" } )
2339 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2341 map { put_record( $_, $out ) } @freq_table;
2347 put_record( $record, $out );
2350 if ( $options->{ "all" } )
2352 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2354 map { put_record( $_, $out ) } @freq_table;
2359 sub script_create_weight_matrix
2361 # Martin A. Hansen, August 2007.
2363 # Creates a weight matrix from an alignmnet.
2365 my ( $in, # handle to in stream
2366 $out, # handle to out stream
2367 $options, # options hash
2372 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2376 while ( $record = get_record( $in ) )
2378 if ( $record->{ "SEQ" } )
2380 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2382 $res = substr $record->{ "SEQ" }, $i, 1;
2384 $freq_hash{ $i }{ $res }++;
2385 $res_hash{ $res } = 1;
2392 put_record( $record, $out );
2396 foreach $res ( sort keys %res_hash )
2400 $record->{ "V0" } = $res;
2402 for ( $i = 0; $i < keys %freq_hash; $i++ )
2404 $freq = $freq_hash{ $i }{ $res } || 0;
2406 if ( $options->{ "percent" } ) {
2407 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2410 $record->{ "V" . ( $i + 1 ) } = $freq;
2413 put_record( $record, $out );
2418 sub script_calc_bit_scores
2420 # Martin A. Hansen, March 2007.
2422 # Calculates the bit scores for each position from an alignmnet in the stream.
2424 my ( $in, # handle to in stream
2425 $out, # handle to out stream
2430 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2434 while ( $record = get_record( $in ) )
2436 if ( $record->{ "SEQ" } )
2438 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2440 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2442 $res = substr $record->{ "SEQ" }, $i, 1;
2444 next if $res =~ /-|_|~|\./;
2446 $freq_hash{ $i }{ $res }++;
2453 put_record( $record, $out );
2459 if ( $type eq "protein" ) {
2465 for ( $i = 0; $i < keys %freq_hash; $i++ )
2467 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2469 $bit_diff = $bit_max - $bit_height;
2471 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2474 put_record( $record, $out );
2478 sub script_reverse_seq
2480 # Martin A. Hansen, August 2007.
2482 # Reverse sequence in record.
2484 my ( $in, # handle to in stream
2485 $out, # handle to out stream
2492 while ( $record = get_record( $in ) )
2494 if ( $record->{ "SEQ" } ) {
2495 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2498 put_record( $record, $out );
2503 sub script_complement_seq
2505 # Martin A. Hansen, August 2007.
2507 # Complement sequence in record.
2509 my ( $in, # handle to in stream
2510 $out, # handle to out stream
2515 my ( $record, $type );
2517 while ( $record = get_record( $in ) )
2519 if ( $record->{ "SEQ" } )
2522 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2525 if ( $type eq "rna" ) {
2526 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2527 } elsif ( $type eq "dna" ) {
2528 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2532 put_record( $record, $out );
2537 sub script_remove_indels
2539 # Martin A. Hansen, August 2007.
2541 # Remove indels from sequences in stream.
2543 my ( $in, # handle to in stream
2544 $out, # handle to out stream
2551 while ( $record = get_record( $in ) )
2553 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2555 put_record( $record, $out );
2560 sub script_transliterate_seq
2562 # Martin A. Hansen, August 2007.
2564 # Transliterate chars from sequence in record.
2566 my ( $in, # handle to in stream
2567 $out, # handle to out stream
2568 $options, # options hash
2573 my ( $record, $search, $replace, $delete );
2575 $search = $options->{ "search" } || "";
2576 $replace = $options->{ "replace" } || "";
2577 $delete = $options->{ "delete" } || "";
2579 while ( $record = get_record( $in ) )
2581 if ( $record->{ "SEQ" } )
2583 if ( $search and $replace ) {
2584 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2585 } elsif ( $delete ) {
2586 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2590 put_record( $record, $out );
2595 sub script_transliterate_vals
2597 # Martin A. Hansen, April 2008.
2599 # Transliterate chars from values in record.
2601 my ( $in, # handle to in stream
2602 $out, # handle to out stream
2603 $options, # options hash
2608 my ( $record, $search, $replace, $delete, $key );
2610 $search = $options->{ "search" } || "";
2611 $replace = $options->{ "replace" } || "";
2612 $delete = $options->{ "delete" } || "";
2614 while ( $record = get_record( $in ) )
2616 foreach $key ( @{ $options->{ "keys" } } )
2618 if ( exists $record->{ $key } )
2620 if ( $search and $replace ) {
2621 eval "\$record->{ $key } =~ tr/$search/$replace/";
2622 } elsif ( $delete ) {
2623 eval "\$record->{ $key } =~ tr/$delete//d";
2628 put_record( $record, $out );
2633 sub script_translate_seq
2635 # Martin A. Hansen, February 2008.
2637 # Translate DNA sequence into protein sequence.
2639 my ( $in, # handle to in stream
2640 $out, # handle to out stream
2641 $options, # options hash
2646 my ( $record, $frame, %new_record );
2648 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2650 while ( $record = get_record( $in ) )
2652 if ( $record->{ "SEQ" } )
2654 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2656 foreach $frame ( @{ $options->{ "frames" } } )
2658 %new_record = %{ $record };
2660 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2661 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2662 $new_record{ "FRAME" } = $frame;
2664 put_record( \%new_record, $out );
2670 put_record( $record, $out );
2676 sub script_extract_seq
2678 # Martin A. Hansen, August 2007.
2680 # Extract subsequences from sequences in record.
2682 my ( $in, # handle to in stream
2683 $out, # handle to out stream
2684 $options, # options hash
2689 my ( $beg, $end, $len, $record );
2691 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2694 $beg = $options->{ "beg" } - 1; # correcting for start offset
2697 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2699 } elsif ( defined $options->{ "end" } ) {
2700 $end = $options->{ "end" } - 1; # correcting for start offset
2703 $len = $options->{ "len" };
2705 # print "beg->$beg, end->$end, len->$len\n";
2707 while ( $record = get_record( $in ) )
2709 if ( $record->{ "SEQ" } )
2711 if ( defined $beg and defined $end )
2713 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2714 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2716 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2719 elsif ( defined $beg and defined $len )
2721 if ( $len > length $record->{ "SEQ" } ) {
2722 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2724 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2727 elsif ( defined $beg )
2729 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2733 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2735 put_record( $record, $out );
2740 sub script_get_genome_seq
2742 # Martin A. Hansen, December 2007.
2744 # Gets a subsequence from a genome.
2746 my ( $in, # handle to in stream
2747 $out, # handle to out stream
2748 $options, # options hash
2753 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2755 $options->{ "flank" } ||= 0;
2757 if ( $options->{ "genome" } )
2759 $genome = $options->{ "genome" };
2761 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2762 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2764 $fh = Maasha::Common::read_open( $genome_file );
2765 $index = Maasha::Fasta::index_retrieve( $index_file );
2767 shift @{ $index }; # Get rid of the file size info
2769 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2771 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2773 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2775 $beg = $index_beg + $options->{ "beg" } - 1;
2777 if ( $options->{ "len" } ) {
2778 $len = $options->{ "len" };
2779 } elsif ( $options->{ "end" } ) {
2780 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2783 $beg -= $options->{ "flank" };
2784 $len += 2 * $options->{ "flank" };
2786 if ( $beg <= $index_beg )
2788 $len -= $index_beg - $beg;
2792 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2794 next if $beg > $index_beg + $index_len;
2796 $record->{ "CHR" } = $options->{ "chr" };
2797 $record->{ "CHR_BEG" } = $beg - $index_beg;
2798 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2800 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2801 $record->{ "SEQ_LEN" } = $len;
2803 put_record( $record, $out );
2807 while ( $record = get_record( $in ) )
2809 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2811 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2813 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2815 $beg = $record->{ "CHR_BEG" } + $index_beg;
2816 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2818 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2820 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2822 $beg = $record->{ "S_BEG" } + $index_beg;
2823 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2825 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2827 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2829 $beg = $record->{ "S_BEG" } + $index_beg;
2830 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2833 $beg -= $options->{ "flank" };
2834 $len += 2 * $options->{ "flank" };
2836 if ( $beg <= $index_beg )
2838 $len -= $index_beg - $beg;
2842 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2844 next if $beg > $index_beg + $index_len;
2846 $record->{ "CHR_BEG" } = $beg - $index_beg;
2847 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2849 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2851 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2853 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2854 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2857 if ( $options->{ "mask" } )
2859 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2861 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2863 @begs = split ",", $record->{ "Q_BEGS" };
2864 @lens = split ",", $record->{ "BLOCKSIZES" };
2866 for ( $i = 0; $i < @begs; $i++ ) {
2867 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2873 put_record( $record, $out );
2880 sub script_get_genome_align
2882 # Martin A. Hansen, April 2008.
2884 # Gets a subalignment from a multiple genome alignment.
2886 my ( $in, # handle to in stream
2887 $out, # handle to out stream
2888 $options, # options hash
2893 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2895 $options->{ "strand" } ||= "+";
2899 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2901 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2903 $beg = $options->{ "beg" } - 1;
2905 if ( $options->{ "end" } ) {
2906 $end = $options->{ "end" };
2907 } elsif ( $options->{ "len" } ) {
2908 $end = $beg + $options->{ "len" };
2911 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2913 foreach $entry ( @{ $align } )
2915 $entry->{ "CHR" } = $record->{ "CHR" };
2916 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2917 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2918 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2919 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2920 $entry->{ "SCORE" } = $record->{ "SCORE" };
2922 put_record( $entry, $out );
2926 while ( $record = get_record( $in ) )
2928 if ( $record->{ "REC_TYPE" } eq "BED" )
2930 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2932 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2934 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2936 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2938 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2941 foreach $entry ( @{ $align } )
2943 $entry->{ "CHR" } = $record->{ "CHR" };
2944 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2945 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2946 $entry->{ "STRAND" } = $record->{ "STRAND" };
2947 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2948 $entry->{ "SCORE" } = $record->{ "SCORE" };
2950 put_record( $entry, $out );
2958 sub script_get_genome_phastcons
2960 # Martin A. Hansen, February 2008.
2962 # Get phastcons scores from genome intervals.
2964 my ( $in, # handle to in stream
2965 $out, # handle to out stream
2966 $options, # options hash
2971 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2973 $options->{ "flank" } ||= 0;
2975 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2976 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2978 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2979 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2981 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2983 $options->{ "beg" } -= 1; # request is 1-based
2984 $options->{ "end" } -= 1; # request is 1-based
2986 if ( $options->{ "len" } ) {
2987 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2990 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2992 $record->{ "CHR" } = $options->{ "chr" };
2993 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2994 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2996 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2997 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2999 put_record( $record, $out );
3002 while ( $record = get_record( $in ) )
3004 if ( $record->{ "REC_TYPE" } eq "BED" )
3006 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3008 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3010 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3012 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3014 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3017 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3018 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3020 put_record( $record, $out );
3023 close $fh_phastcons if $fh_phastcons;
3029 # Martin A. Hansen, December 2007.
3031 # Folds sequences in stream into secondary structures.
3033 my ( $in, # handle to in stream
3034 $out, # handle to out stream
3039 my ( $record, $type, $struct, $index );
3041 while ( $record = get_record( $in ) )
3043 if ( $record->{ "SEQ" } )
3046 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3049 if ( $type ne "protein" )
3051 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3052 $record->{ "SEC_STRUCT" } = $struct;
3053 $record->{ "FREE_ENERGY" } = $index;
3054 $record->{ "SCORE" } = abs int $index;
3055 $record->{ "SIZE" } = length $struct;
3056 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3060 put_record( $record, $out );
3065 sub script_split_seq
3067 # Martin A. Hansen, August 2007.
3069 # Split a sequence in stream into words.
3071 my ( $in, # handle to in stream
3072 $out, # handle to out stream
3073 $options, # options hash
3078 my ( $record, $new_record, $i, $subseq, %lookup );
3080 $options->{ "word_size" } ||= 7;
3082 while ( $record = get_record( $in ) )
3084 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3086 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3088 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3090 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3092 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3093 $new_record->{ "SEQ" } = $subseq;
3095 put_record( $new_record, $out );
3097 $lookup{ $subseq } = 1;
3101 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3102 $new_record->{ "SEQ" } = $subseq;
3104 put_record( $new_record, $out );
3110 put_record( $record, $out );
3116 sub script_split_bed
3118 # Martin A. Hansen, June 2008.
3120 # Split a BED record into overlapping windows.
3122 my ( $in, # handle to in stream
3123 $out, # handle to out stream
3124 $options, # options hash
3129 my ( $record, $new_record, $i );
3131 $options->{ "window_size" } ||= 20;
3132 $options->{ "step_size" } ||= 1;
3134 while ( $record = get_record( $in ) )
3136 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3138 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3140 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3142 $new_record->{ "REC_TYPE" } = "BED";
3143 $new_record->{ "CHR" } = $record->{ "CHR" };
3144 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3145 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3146 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3147 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3148 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3149 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3151 put_record( $new_record, $out );
3156 put_record( $record, $out );
3162 sub script_align_seq
3164 # Martin A. Hansen, August 2007.
3166 # Align sequences in stream.
3168 my ( $in, # handle to in stream
3169 $out, # handle to out stream
3174 my ( $record, @entries, $entry );
3176 while ( $record = get_record( $in ) )
3178 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3179 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3180 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3181 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3183 put_record( $record, $out );
3187 @entries = Maasha::Align::align( \@entries );
3189 foreach $entry ( @entries )
3191 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3194 SEQ_NAME => $entry->[ SEQ_NAME ],
3195 SEQ => $entry->[ SEQ ],
3198 put_record( $record, $out );
3206 # Martin A. Hansen, February 2008.
3208 # Using the first sequence in stream as reference, tile
3209 # all subsequent sequences based on pairwise alignments.
3211 my ( $in, # handle to in stream
3212 $out, # handle to out stream
3213 $options, # options hash
3218 my ( $record, $first, $ref_entry, @entries );
3222 while ( $record = get_record( $in ) )
3224 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3228 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3234 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3239 put_record( $record, $out );
3243 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3245 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3249 sub script_invert_align
3251 # Martin A. Hansen, February 2008.
3253 # Inverts an alignment showing only non-mathing residues
3254 # using the first sequence as reference.
3256 my ( $in, # handle to in stream
3257 $out, # handle to out stream
3258 $options, # options hash
3263 my ( $record, @entries );
3265 while ( $record = get_record( $in ) )
3267 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3269 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3273 put_record( $record, $out );
3277 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3279 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3283 sub script_patscan_seq
3285 # Martin A. Hansen, August 2007.
3287 # Locates patterns in sequences using scan_for_matches.
3289 my ( $in, # handle to in stream
3290 $out, # handle to out stream
3291 $options, # options hash
3296 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3298 if ( $options->{ "patterns" } ) {
3299 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3300 } elsif ( -f $options->{ "patterns_in" } ) {
3301 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3304 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3306 push @args, "-c" if $options->{ "comp" };
3307 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3308 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3310 $seq_file = "$BP_TMP/patscan.seq";
3311 $pat_file = "$BP_TMP/patscan.pat";
3312 $out_file = "$BP_TMP/patscan.out";
3314 $fh_out = Maasha::Common::write_open( $seq_file );
3318 while ( $record = get_record( $in ) )
3320 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3322 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3324 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3326 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3334 $arg = join " ", @args;
3335 $arg .= " -p" if $type eq "protein";
3337 foreach $pattern ( @{ $patterns } )
3339 $fh_out = Maasha::Common::write_open( $pat_file );
3341 print $fh_out "$pattern\n";
3345 if ( $options->{ 'genome' } ) {
3346 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3347 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3349 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3350 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3353 $fh_in = Maasha::Common::read_open( $out_file );
3355 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3357 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3359 if ( $options->{ 'genome' } )
3361 $result->{ "CHR" } = $result->{ "S_ID" };
3362 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3363 $result->{ "CHR_END" } = $result->{ "S_END" };
3365 delete $result->{ "S_ID" };
3366 delete $result->{ "S_BEG" };
3367 delete $result->{ "S_END" };
3371 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3374 put_record( $result, $out );
3386 sub script_create_blast_db
3388 # Martin A. Hansen, September 2007.
3390 # Creates a NCBI BLAST database with formatdb
3392 my ( $in, # handle to in stream
3393 $out, # handle to out stream
3394 $options, # options hash
3399 my ( $fh, $seq_type, $path, $record, $entry );
3401 $path = $options->{ "database" };
3403 $fh = Maasha::Common::write_open( $path );
3405 while ( $record = get_record( $in ) )
3407 put_record( $record, $out ) if not $options->{ "no_stream" };
3409 if ( $entry = record2fasta( $record ) )
3411 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3413 Maasha::Fasta::put_entry( $entry, $fh );
3419 if ( $seq_type eq "protein" ) {
3420 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3422 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3429 sub script_blast_seq
3431 # Martin A. Hansen, September 2007.
3433 # BLASTs sequences in stream against a given database.
3435 my ( $in, # handle to in stream
3436 $out, # handle to out stream
3437 $options, # options hash
3442 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3444 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3445 $options->{ "filter" } = "F";
3446 $options->{ "filter" } = "T" if $options->{ "filter" };
3447 $options->{ "cpus" } ||= 1;
3449 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3451 $tmp_in = "$BP_TMP/blast_query.seq";
3452 $tmp_out = "$BP_TMP/blast.result";
3454 $fh_out = Maasha::Common::write_open( $tmp_in );
3456 while ( $record = get_record( $in ) )
3458 if ( $entry = record2fasta( $record ) )
3460 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3462 Maasha::Fasta::put_entry( $entry, $fh_out );
3465 put_record( $record, $out );
3470 if ( -f $options->{ 'database' } . ".phr" ) {
3471 $s_type = "protein";
3473 $s_type = "nucleotide";
3476 if ( not $options->{ 'program' } )
3478 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3479 $options->{ 'program' } = "blastn";
3480 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3481 $options->{ 'program' } = "blastp";
3482 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3483 $options->{ 'program' } = "blastx";
3484 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3485 $options->{ 'program' } = "tblastn";
3489 if ( $options->{ 'verbose' } )
3491 Maasha::Common::run(
3494 "-p $options->{ 'program' }",
3495 "-e $options->{ 'e_val' }",
3496 "-a $options->{ 'cpus' }",
3499 "-d $options->{ 'database' }",
3500 "-F $options->{ 'filter' }",
3508 Maasha::Common::run(
3511 "-p $options->{ 'program' }",
3512 "-e $options->{ 'e_val' }",
3513 "-a $options->{ 'cpus' }",
3516 "-d $options->{ 'database' }",
3517 "-F $options->{ 'filter' }",
3527 $fh_out = Maasha::Common::read_open( $tmp_out );
3531 while ( $line = <$fh_out> )
3535 next if $line =~ /^#/;
3537 @fields = split /\s+/, $line;
3539 $record->{ "REC_TYPE" } = "BLAST";
3540 $record->{ "Q_ID" } = $fields[ 0 ];
3541 $record->{ "S_ID" } = $fields[ 1 ];
3542 $record->{ "IDENT" } = $fields[ 2 ];
3543 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3544 $record->{ "MISMATCHES" } = $fields[ 4 ];
3545 $record->{ "GAPS" } = $fields[ 5 ];
3546 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3547 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3548 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3549 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3550 $record->{ "E_VAL" } = $fields[ 10 ];
3551 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3553 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3555 $record->{ "STRAND" } = '-';
3557 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3561 $record->{ "STRAND" } = '+';
3564 put_record( $record, $out );
3575 # Martin A. Hansen, August 2007.
3577 # BLATs sequences in stream against a given genome.
3579 my ( $in, # handle to in stream
3580 $out, # handle to out stream
3581 $options, # options hash
3586 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3588 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3590 $options->{ 'tile_size' } ||= 11;
3591 $options->{ 'one_off' } ||= 0;
3592 $options->{ 'min_identity' } ||= 90;
3593 $options->{ 'min_score' } ||= 0;
3594 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3596 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3597 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3598 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3599 $blat_args .= " -minScore=$options->{ 'min_score' }";
3600 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3601 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3603 $query_file = "$BP_TMP/blat.seq";
3605 $fh_out = Maasha::Common::write_open( $query_file );
3607 while ( $record = get_record( $in ) )
3609 if ( $entry = record2fasta( $record ) )
3611 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3612 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3615 put_record( $record, $out );
3620 $blat_args .= " -t=dnax" if $type eq "protein";
3621 $blat_args .= " -q=$type";
3623 $result_file = "$BP_TMP/blat.psl";
3625 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3629 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3631 map { put_record( $_, $out ) } @{ $entries };
3633 unlink $result_file;
3639 # Martin A. Hansen, July 2008.
3641 # soap sequences in stream against a given file or genome.
3643 my ( $in, # handle to in stream
3644 $out, # handle to out stream
3645 $options, # options hash
3650 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3652 $options->{ "seed_size" } ||= 10;
3653 $options->{ "mismatches" } ||= 2;
3654 $options->{ "gap_size" } ||= 0;
3655 $options->{ "cpus" } ||= 1;
3657 if ( $options->{ "genome" } ) {
3658 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3661 $tmp_in = "$BP_TMP/soap_query.seq";
3662 $tmp_out = "$BP_TMP/soap.result";
3664 $fh_out = Maasha::Common::write_open( $tmp_in );
3668 while ( $record = get_record( $in ) )
3670 if ( $entry = record2fasta( $record ) )
3672 Maasha::Fasta::put_entry( $entry, $fh_out );
3677 put_record( $record, $out );
3685 "-s $options->{ 'seed_size' }",
3688 "-v $options->{ 'mismatches' }",
3689 "-g $options->{ 'gap_size' }",
3690 "-p $options->{ 'cpus' }",
3691 "-d $options->{ 'in_file' }",
3695 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3697 Maasha::Common::run( "soap", $args, 1 );
3701 $fh_out = Maasha::Common::read_open( $tmp_out );
3705 while ( $line = <$fh_out> )
3709 @fields = split /\t/, $line;
3711 $record->{ "REC_TYPE" } = "SOAP";
3712 $record->{ "Q_ID" } = $fields[ 0 ];
3713 $record->{ "SCORE" } = $fields[ 3 ];
3714 $record->{ "STRAND" } = $fields[ 6 ];
3715 $record->{ "S_ID" } = $fields[ 7 ];
3716 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3717 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3719 put_record( $record, $out );
3729 sub script_match_seq
3731 # Martin A. Hansen, August 2007.
3733 # BLATs sequences in stream against a given genome.
3735 my ( $in, # handle to in stream
3736 $out, # handle to out stream
3737 $options, # options hash
3742 my ( $record, @entries, $results );
3744 $options->{ "word_size" } ||= 20;
3745 $options->{ "direction" } ||= "both";
3747 while ( $record = get_record( $in ) )
3749 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3750 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3753 put_record( $record, $out );
3756 if ( @entries == 1 )
3758 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3760 map { put_record( $_, $out ) } @{ $results };
3762 elsif ( @entries == 2 )
3764 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3766 map { put_record( $_, $out ) } @{ $results };
3771 sub script_create_vmatch_index
3773 # Martin A. Hansen, January 2008.
3775 # Create a vmatch index from sequences in the stream.
3777 my ( $in, # handle to in stream
3778 $out, # handle to out stream
3779 $options, # options hash
3784 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3786 if ( $options->{ "index_name" } )
3788 $file_tmp = $options->{ 'index_name' };
3789 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3792 while ( $record = get_record( $in ) )
3794 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3796 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3798 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3801 put_record( $record, $out ) if not $options->{ "no_stream" };
3804 if ( $options->{ "index_name" } )
3808 if ( $type eq "protein" ) {
3809 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3811 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3819 sub script_vmatch_seq
3821 # Martin A. Hansen, August 2007.
3823 # Vmatches sequences in stream against a given genome.
3825 my ( $in, # handle to in stream
3826 $out, # handle to out stream
3827 $options, # options hash
3832 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3834 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3836 if ( $options->{ "index_name" } )
3838 @index_files = $options->{ "index_name" };
3842 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3844 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3846 @index_files = sort keys %hash;
3849 while ( $record = get_record( $in ) )
3851 push @records, $record;
3853 put_record( $record, $out );
3856 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3860 $fh_in = Maasha::Common::read_open( $result_file );
3862 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3863 put_record( $record, $out );
3868 unlink $result_file;
3872 sub script_write_fasta
3874 # Martin A. Hansen, August 2007.
3876 # Write FASTA entries from sequences in stream.
3878 my ( $in, # handle to in stream
3879 $out, # handle to out stream
3880 $options, # options hash
3885 my ( $record, $fh, $entry );
3887 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3889 while ( $record = get_record( $in ) )
3891 if ( $entry = record2fasta( $record ) ) {
3892 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3895 put_record( $record, $out ) if not $options->{ "no_stream" };
3902 sub script_write_align
3904 # Martin A. Hansen, August 2007.
3906 # Write pretty alignments aligned sequences in stream.
3908 my ( $in, # handle to in stream
3909 $out, # handle to out stream
3910 $options, # options hash
3915 my ( $fh, $record, @entries );
3917 $fh = write_stream( $options->{ "data_out" } ) ;
3919 while ( $record = get_record( $in ) )
3921 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3922 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3925 put_record( $record, $out ) if not $options->{ "no_stream" };
3928 if ( scalar( @entries ) == 2 ) {
3929 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3930 } elsif ( scalar ( @entries ) > 2 ) {
3931 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3938 sub script_write_blast
3940 # Martin A. Hansen, November 2007.
3942 # Write data in blast table format (-m8 and 9).
3944 my ( $in, # handle to in stream
3945 $out, # handle to out stream
3946 $options, # options hash
3951 my ( $fh, $record, $first );
3953 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3957 while ( $record = get_record( $in ) )
3959 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3961 if ( $options->{ "comment" } and $first )
3963 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";
3968 if ( $record->{ "STRAND" } eq "-" ) {
3969 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3972 print $fh join( "\t",
3973 $record->{ "Q_ID" },
3974 $record->{ "S_ID" },
3975 $record->{ "IDENT" },
3976 $record->{ "ALIGN_LEN" },
3977 $record->{ "MISMATCHES" },
3978 $record->{ "GAPS" },
3979 $record->{ "Q_BEG" } + 1,
3980 $record->{ "Q_END" } + 1,
3981 $record->{ "S_BEG" } + 1,
3982 $record->{ "S_END" } + 1,
3983 $record->{ "E_VAL" },
3984 $record->{ "BIT_SCORE" }
3988 put_record( $record, $out ) if not $options->{ "no_stream" };
3995 sub script_write_tab
3997 # Martin A. Hansen, August 2007.
3999 # Write data as table.
4001 my ( $in, # handle to in stream
4002 $out, # handle to out stream
4003 $options, # options hash
4008 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4010 $options->{ "delimit" } ||= "\t";
4012 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4014 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4016 while ( $record = get_record( $in ) )
4021 if ( $options->{ "keys" } )
4023 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4027 foreach $key ( @{ $options->{ "keys" } } )
4029 if ( exists $record->{ $key } )
4031 push @keys, $key if $options->{ "comment" };
4032 push @vals, $record->{ $key };
4039 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4041 next if exists $no_keys{ $key };
4043 push @keys, $key if $options->{ "comment" };
4044 push @vals, $record->{ $key };
4048 if ( @keys and $options->{ "comment" } )
4050 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4052 delete $options->{ "comment" };
4055 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4057 put_record( $record, $out ) if not $options->{ "no_stream" };
4064 sub script_write_bed
4066 # Martin A. Hansen, August 2007.
4068 # Write BED format for the UCSC genome browser using records in stream.
4070 my ( $in, # handle to in stream
4071 $out, # handle to out stream
4072 $options, # options hash
4077 my ( $fh, $record, $new_record );
4079 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4081 while ( $record = get_record( $in ) )
4083 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4085 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4087 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4089 $new_record->{ "CHR" } = $record->{ "S_ID" };
4090 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4091 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4092 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4093 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4094 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4096 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4098 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4100 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4102 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4104 $new_record->{ "CHR" } = $record->{ "S_ID" };
4105 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4106 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4107 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4108 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4109 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4111 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4113 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4115 $new_record->{ "CHR" } = $record->{ "S_ID" };
4116 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4117 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4118 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4119 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4120 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4122 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4124 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4126 $new_record->{ "CHR" } = $record->{ "S_ID" };
4127 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4128 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4129 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4130 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4131 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4133 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4135 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4137 Maasha::UCSC::bed_put_entry( $record, $fh );
4140 put_record( $record, $out ) if not $options->{ "no_stream" };
4147 sub script_write_psl
4149 # Martin A. Hansen, August 2007.
4151 # Write PSL output from stream.
4153 my ( $in, # handle to in stream
4154 $out, # handle to out stream
4155 $options, # options hash
4160 my ( $fh, $record, @output, $first );
4164 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4166 while ( $record = get_record( $in ) )
4168 put_record( $record, $out ) if not $options->{ "no_stream" };
4170 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4172 Maasha::UCSC::psl_put_header( $fh ) if $first;
4173 Maasha::UCSC::psl_put_entry( $record, $fh );
4182 sub script_write_fixedstep
4184 # Martin A. Hansen, Juli 2008.
4186 # Write fixedStep entries from recrods in the stream.
4188 my ( $in, # handle to in stream
4189 $out, # handle to out stream
4190 $options, # options hash
4195 my ( $fh, $record, $vals );
4197 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4199 while ( $record = get_record( $in ) )
4201 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4203 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4205 $vals = $record->{ 'VALS' };
4209 print $fh "$vals\n";
4212 put_record( $record, $out ) if not $options->{ "no_stream" };
4219 sub script_write_2bit
4221 # Martin A. Hansen, March 2008.
4223 # Write sequence entries from stream in 2bit format.
4225 my ( $in, # handle to in stream
4226 $out, # handle to out stream
4227 $options, # options hash
4232 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4234 $mask = 1 if not $options->{ "no_mask" };
4236 $tmp_file = "$BP_TMP/write_2bit.fna";
4237 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4239 $fh_out = write_stream( $options->{ "data_out" } );
4241 while ( $record = get_record( $in ) )
4243 if ( $entry = record2fasta( $record ) ) {
4244 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4247 put_record( $record, $out ) if not $options->{ "no_stream" };
4252 $fh_in = Maasha::Common::read_open( $tmp_file );
4254 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4263 sub script_write_solid
4265 # Martin A. Hansen, April 2008.
4267 # Write di-base encoded Solid sequence from entries in stream.
4269 my ( $in, # handle to in stream
4270 $out, # handle to out stream
4271 $options, # options hash
4276 my ( $record, $fh, $entry );
4278 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4280 while ( $record = get_record( $in ) )
4282 if ( $entry = record2fasta( $record ) )
4284 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4286 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4289 put_record( $record, $out ) if not $options->{ "no_stream" };
4296 sub script_plot_seqlogo
4298 # Martin A. Hansen, August 2007.
4300 # Calculates and writes a sequence logo for alignments.
4302 my ( $in, # handle to in stream
4303 $out, # handle to out stream
4304 $options, # options hash
4309 my ( $record, @entries, $logo, $fh );
4311 while ( $record = get_record( $in ) )
4313 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4314 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4317 put_record( $record, $out ) if not $options->{ "no_stream" };
4320 $logo = Maasha::Plot::seq_logo( \@entries );
4322 $fh = write_stream( $options->{ "data_out" } );
4330 sub script_plot_phastcons_profiles
4332 # Martin A. Hansen, January 2008.
4334 # Plots PhastCons profiles.
4336 my ( $in, # handle to in stream
4337 $out, # handle to out stream
4338 $options, # options hash
4343 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4345 $options->{ "title" } ||= "PhastCons Profiles";
4347 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4348 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4350 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4351 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4353 while ( $record = get_record( $in ) )
4355 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4357 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4359 push @{ $AoA }, [ @{ $scores } ];
4362 put_record( $record, $out ) if not $options->{ "no_stream" };
4365 Maasha::UCSC::phastcons_normalize( $AoA );
4367 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4368 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4370 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4372 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4374 $fh = write_stream( $options->{ "data_out" } );
4376 print $fh "$_\n" foreach @{ $plot };
4382 sub script_analyze_bed
4384 # Martin A. Hansen, March 2008.
4386 # Analyze BED entries in stream.
4388 my ( $in, # handle to in stream
4389 $out, # handle to out stream
4390 $options, # options hash
4397 while ( $record = get_record( $in ) )
4399 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4401 put_record( $record, $out );
4406 sub script_analyze_vals
4408 # Martin A. Hansen, August 2007.
4410 # Analyze values for given keys in stream.
4412 my ( $in, # handle to in stream
4413 $out, # handle to out stream
4414 $options, # options hash
4419 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4421 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4423 while ( $record = get_record( $in ) )
4425 foreach $key ( keys %{ $record } )
4427 next if $options->{ "keys" } and not exists $key_hash{ $key };
4429 $analysis->{ $key }->{ "COUNT" }++;
4431 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4433 $analysis->{ $key }->{ "TYPE" } = "num";
4434 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4435 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4436 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4440 $len = length $record->{ $key };
4442 $analysis->{ $key }->{ "TYPE" } = "alph";
4443 $analysis->{ $key }->{ "SUM" } += $len;
4444 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4445 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4449 put_record( $record, $out ) if not $options->{ "no_stream" };
4452 foreach $key ( keys %{ $analysis } )
4454 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4455 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4458 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4468 if ( $options->{ "keys" } ) {
4469 @keys = @{ $options->{ "keys" } };
4471 @keys = keys %{ $analysis };
4474 foreach $key ( @keys )
4476 $keys .= sprintf "% 15s", $key;
4477 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4478 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4479 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4480 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4481 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4482 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4485 print $out "$keys\n";
4486 print $out "$types\n";
4487 print $out "$counts\n";
4488 print $out "$mins\n";
4489 print $out "$maxs\n";
4490 print $out "$sums\n";
4491 print $out "$means\n";
4495 sub script_head_records
4497 # Martin A. Hansen, August 2007.
4499 # Display the first sequences in stream.
4501 my ( $in, # handle to in stream
4502 $out, # handle to out stream
4503 $options, # options hash
4508 my ( $record, $count );
4510 $options->{ "num" } ||= 10;
4514 while ( $record = get_record( $in ) )
4518 put_record( $record, $out );
4520 last if $count == $options->{ "num" };
4525 sub script_remove_keys
4527 # Martin A. Hansen, August 2007.
4529 # Remove keys from stream.
4531 my ( $in, # handle to in stream
4532 $out, # handle to out stream
4533 $options, # options hash
4538 my ( $record, $new_record );
4540 while ( $record = get_record( $in ) )
4542 if ( $options->{ "keys" } )
4544 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4546 elsif ( $options->{ "save_keys" } )
4548 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4550 $record = $new_record;
4553 put_record( $record, $out ) if keys %{ $record };
4558 sub script_remove_adaptor
4560 # Martin A. Hansen, August 2008.
4562 # Find and remove adaptor from sequences in the stream.
4564 my ( $in, # handle to in stream
4565 $out, # handle to out stream
4566 $options, # options hash
4571 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4573 $max_mismatch = $options->{ "mismatches" } || 0;
4574 $offset = $options->{ "offset" } || 15;
4575 $adaptor = $options->{ "adaptor" };
4576 $adaptor_len = length $adaptor;
4577 $adaptor = [ split //, uc $adaptor ];
4579 $max_match = $adaptor_len - $max_mismatch;
4581 while ( $record = get_record( $in ) )
4583 if ( $record->{ "SEQ" } )
4585 $seq = $record->{ "SEQ" };
4586 $seq_len = length $seq;
4587 $seq = [ split //, uc $seq ];
4589 $pos = Maasha::Seq::find_adaptor( $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch );
4591 $record->{ "ADAPTOR_POS" } = $pos;
4595 if ( not $options->{ "no_remove" } ) {
4596 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4599 put_record( $record, $out );
4604 put_record( $record, $out );
4610 sub script_rename_keys
4612 # Martin A. Hansen, August 2007.
4614 # Rename keys in stream.
4616 my ( $in, # handle to in stream
4617 $out, # handle to out stream
4618 $options, # options hash
4625 while ( $record = get_record( $in ) )
4627 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4629 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4631 delete $record->{ $options->{ "keys" }->[ 0 ] };
4634 put_record( $record, $out );
4639 sub script_uniq_vals
4641 # Martin A. Hansen, August 2007.
4643 # Find unique values in stream.
4645 my ( $in, # handle to in stream
4646 $out, # handle to out stream
4647 $options, # options hash
4652 my ( %hash, $record );
4654 while ( $record = get_record( $in ) )
4656 if ( $record->{ $options->{ "key" } } )
4658 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4660 put_record( $record, $out );
4662 $hash{ $record->{ $options->{ "key" } } } = 1;
4664 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4666 put_record( $record, $out );
4670 $hash{ $record->{ $options->{ "key" } } } = 1;
4675 put_record( $record, $out );
4681 sub script_merge_vals
4683 # Martin A. Hansen, August 2007.
4685 # Rename keys in stream.
4687 my ( $in, # handle to in stream
4688 $out, # handle to out stream
4689 $options, # options hash
4694 my ( $record, @join, $i );
4696 $options->{ "delimit" } ||= '_';
4698 while ( $record = get_record( $in ) )
4700 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4702 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4704 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4705 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4708 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4711 put_record( $record, $out );
4716 sub script_merge_records
4718 # Martin A. Hansen, July 2008.
4720 # Merges records in the stream based on identical values of two given keys.
4722 my ( $in, # handle to in stream
4723 $out, # handle to out stream
4724 $options, # options hash
4729 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4730 $num1, $num2, $num, $cmp, $i );
4732 $merge = $options->{ "merge" } || "AandB";
4734 $file1 = "$BP_TMP/merge_records1.tmp";
4735 $file2 = "$BP_TMP/merge_records2.tmp";
4737 $fh1 = Maasha::Common::write_open( $file1 );
4738 $fh2 = Maasha::Common::write_open( $file2 );
4740 $key1 = $options->{ "keys" }->[ 0 ];
4741 $key2 = $options->{ "keys" }->[ 1 ];
4743 $num = $key2 =~ s/n$//;
4747 while ( $record = get_record( $in ) )
4749 if ( exists $record->{ $key1 } )
4752 @vals1 = $record->{ $key1 };
4754 delete $record->{ $key1 };
4756 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4758 print $fh1 join( "\t", @vals1 ), "\n";
4762 elsif ( exists $record->{ $key2 } )
4765 @vals2 = $record->{ $key2 };
4767 delete $record->{ $key2 };
4769 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4771 print $fh2 join( "\t", @vals2 ), "\n";
4782 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4783 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4787 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4788 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4791 $fh1 = Maasha::Common::read_open( $file1 );
4792 $fh2 = Maasha::Common::read_open( $file2 );
4794 @vals1 = Maasha::Common::get_fields( $fh1 );
4795 @vals2 = Maasha::Common::get_fields( $fh2 );
4797 while ( $num1 > 0 and $num2 > 0 )
4802 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4804 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4809 if ( $merge =~ /^(AorB|AnotB)$/ )
4811 for ( $i = 0; $i < @keys1; $i++ ) {
4812 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4815 put_record( $record, $out );
4818 @vals1 = Maasha::Common::get_fields( $fh1 );
4823 if ( $merge =~ /^(BorA|BnotA)$/ )
4825 for ( $i = 0; $i < @keys2; $i++ ) {
4826 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4829 put_record( $record, $out );
4832 @vals2 = Maasha::Common::get_fields( $fh2 );
4837 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4839 for ( $i = 0; $i < @keys1; $i++ ) {
4840 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4843 for ( $i = 1; $i < @keys2; $i++ ) {
4844 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4847 put_record( $record, $out );
4850 @vals1 = Maasha::Common::get_fields( $fh1 );
4851 @vals2 = Maasha::Common::get_fields( $fh2 );
4863 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4867 for ( $i = 0; $i < @keys1; $i++ ) {
4868 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4871 put_record( $record, $out );
4874 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4878 for ( $i = 0; $i < @keys2; $i++ ) {
4879 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4882 put_record( $record, $out );
4889 # Martin A. Hansen, August 2007.
4891 # Grab for records in stream.
4893 my ( $in, # handle to in stream
4894 $out, # handle to out stream
4895 $options, # options hash
4900 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4902 if ( $options->{ "patterns" } )
4904 $patterns = [ split ",", $options->{ "patterns" } ];
4906 elsif ( -f $options->{ "patterns_in" } )
4908 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4910 elsif ( -f $options->{ "exact_in" } )
4912 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4914 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4919 if ( $options->{ "eval" } )
4921 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4929 while ( $record = get_record( $in ) )
4935 if ( $options->{ "keys" } )
4937 foreach $key ( @{ $options->{ "keys" } } )
4939 if ( exists $lookup_hash{ $record->{ $key } } )
4948 foreach $key ( keys %{ $record } )
4950 if ( not $options->{ "vals_only" } )
4952 if ( exists $lookup_hash{ $key } )
4959 if ( not $options->{ "keys_only" } )
4961 if ( exists $lookup_hash{ $record->{ $key } } )
4972 foreach $pattern ( @{ $patterns } )
4974 if ( $options->{ "keys" } )
4976 foreach $key ( @{ $options->{ "keys" } } )
4978 $pos = index $record->{ $key }, $pattern;
4980 goto FOUND if $pos >= 0;
4985 foreach $key ( keys %{ $record } )
4987 if ( not $options->{ "vals_only" } )
4989 $pos = index $key, $pattern;
4991 goto FOUND if $pos >= 0;
4994 if ( not $options->{ "keys_only" } )
4996 $pos = index $record->{ $key }, $pattern;
4998 goto FOUND if $pos >= 0;
5004 elsif ( $options->{ "regex" } )
5006 if ( $options->{ "keys" } )
5008 foreach $key ( @{ $options->{ "keys" } } )
5010 if ( $options->{ "case_insensitive" } ) {
5011 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5013 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5016 goto FOUND if $pos >= 0;
5021 foreach $key ( keys %{ $record } )
5023 if ( not $options->{ "vals_only" } )
5025 if ( $options->{ "case_insensitive" } ) {
5026 $pos = 1 if $key =~ /$options->{'regex'}/i;
5028 $pos = 1 if $key =~ /$options->{'regex'}/;
5031 goto FOUND if $pos >= 0;
5034 if ( not $options->{ "keys_only" } )
5036 if ( $options->{ "case_insensitive" } ) {
5037 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5039 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5042 goto FOUND if $pos >= 0;
5047 elsif ( $options->{ "eval" } )
5049 if ( defined $record->{ $key } )
5051 if ( $op eq "<" and $record->{ $key } < $val ) {
5052 $pos = 1 and goto FOUND;
5053 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
5054 $pos = 1 and goto FOUND;
5055 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
5056 $pos = 1 and goto FOUND;
5057 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
5058 $pos = 1 and goto FOUND;
5059 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
5060 $pos = 1 and goto FOUND;
5061 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
5062 $pos = 1 and goto FOUND;
5063 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
5064 $pos = 1 and goto FOUND;
5065 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5066 $pos = 1 and goto FOUND;
5073 if ( $pos >= 0 and not $options->{ "invert" } ) {
5074 put_record( $record, $out );
5075 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5076 put_record( $record, $out );
5084 # Martin A. Hansen, August 2007.
5086 # Evaluate extression for records in stream.
5088 my ( $in, # handle to in stream
5089 $out, # handle to out stream
5090 $options, # options hash
5095 my ( $record, $eval_key, $eval_val, $check, @keys );
5097 while ( $record = get_record( $in ) )
5099 if ( $options->{ "eval" } )
5101 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5109 @keys = split /\W+/, $eval_val;
5110 @keys = grep { ! /^\d+$/ } @keys;
5115 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5117 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5120 put_record( $record, $out );
5127 # Martin A. Hansen, June 2008.
5131 my ( $in, # handle to in stream
5132 $out, # handle to out stream
5133 $options, # options hash
5138 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5140 while ( $record = get_record( $in ) )
5144 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5146 push @rows, $record->{ $key };
5150 push @matrix, [ @rows ];
5155 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5157 foreach $row ( @matrix )
5159 for ( $i = 0; $i < @{ $row }; $i++ ) {
5160 $record->{ "V$i" } = $row->[ $i ];
5163 put_record( $record, $out );
5168 sub script_add_ident
5170 # Martin A. Hansen, May 2008.
5172 # Add a unique identifier to each record in stream.
5174 my ( $in, # handle to in stream
5175 $out, # handle to out stream
5176 $options, # options hash
5181 my ( $record, $key, $prefix, $i );
5183 $key = $options->{ "key" } || "ID";
5184 $prefix = $options->{ "prefix" } || "ID";
5188 while ( $record = get_record( $in ) )
5190 $record->{ $key } = sprintf( "$prefix%08d", $i );
5192 put_record( $record, $out );
5199 sub script_count_records
5201 # Martin A. Hansen, August 2007.
5203 # Count records in stream.
5205 my ( $in, # handle to in stream
5206 $out, # handle to out stream
5207 $options, # options hash
5212 my ( $record, $count, $result, $fh, $line );
5216 if ( $options->{ "no_stream" } )
5218 while ( $line = <$in> )
5222 $count++ if $line eq "---";
5227 while ( $record = get_record( $in ) )
5229 put_record( $record, $out );
5235 $result = { "RECORDS_COUNT" => $count };
5237 $fh = write_stream( $options->{ "data_out" } );
5239 put_record( $result, $fh );
5245 sub script_random_records
5247 # Martin A. Hansen, August 2007.
5249 # Pick a number or random records from stream.
5251 my ( $in, # handle to in stream
5252 $out, # handle to out stream
5253 $options, # options hash
5258 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5260 $options->{ "num" } ||= 10;
5262 $tmp_file = "$BP_TMP/random_records.tmp";
5264 $fh_out = Maasha::Common::write_open( $tmp_file );
5268 while ( $record = get_record( $in ) )
5270 put_record( $record, $fh_out );
5280 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5282 while ( $i < $options->{ "num" } )
5284 $rand = int( rand( $count ) );
5286 if ( not exists $rand_hash{ $rand } )
5288 $rand_hash{ $rand } = 1;
5290 $max = $rand if $rand > $max;
5296 $fh_in = Maasha::Common::read_open( $tmp_file );
5300 while ( $record = get_record( $fh_in ) )
5302 put_record( $record, $out ) if exists $rand_hash{ $count };
5304 last if $count == $max;
5315 sub script_sort_records
5317 # Martin A. Hansen, August 2007.
5319 # Sort to sort records according to keys.
5321 my ( $in, # handle to in stream
5322 $out, # handle to out stream
5323 $options, # options hash
5328 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5330 foreach $key ( @{ $options->{ "keys" } } )
5332 if ( $key =~ s/n$// ) {
5333 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5335 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5339 $sort_str = join " or ", @sort_cmd;
5340 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5342 while ( $record = get_record( $in ) ) {
5343 push @records, $record;
5346 @records = sort $sort_sub @records;
5348 if ( $options->{ "reverse" } )
5350 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5351 put_record( $records[ $i ], $out );
5356 for ( $i = 0; $i < scalar @records; $i++ ) {
5357 put_record( $records[ $i ], $out );
5363 sub script_count_vals
5365 # Martin A. Hansen, August 2007.
5367 # Count records in stream.
5369 my ( $in, # handle to in stream
5370 $out, # handle to out stream
5371 $options, # options hash
5376 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5378 $tmp_file = "$BP_TMP/count_cache.tmp";
5380 $fh_out = Maasha::Common::write_open( $tmp_file );
5385 while ( $record = get_record( $in ) )
5387 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5389 push @records, $record;
5391 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5393 map { put_record( $_, $fh_out ) } @records;
5400 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5411 $fh_in = Maasha::Common::read_open( $tmp_file );
5413 while ( $record = get_record( $fh_in ) )
5415 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5417 put_record( $record, $out );
5419 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5427 foreach $record ( @records )
5429 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5431 put_record( $record, $out );
5438 sub script_plot_histogram
5440 # Martin A. Hansen, September 2007.
5442 # Plot a simple histogram for a given key using GNU plot.
5444 my ( $in, # handle to in stream
5445 $out, # handle to out stream
5446 $options, # options hash
5451 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5453 $options->{ "title" } ||= "Histogram";
5454 $options->{ "sort" } ||= "num";
5456 while ( $record = get_record( $in ) )
5458 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5460 put_record( $record, $out ) if not $options->{ "no_stream" };
5463 if ( $options->{ "sort" } eq "num" ) {
5464 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5466 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5469 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5471 $fh = write_stream( $options->{ "data_out" } );
5473 print $fh "$_\n" foreach @{ $result };
5479 sub script_plot_lendist
5481 # Martin A. Hansen, August 2007.
5483 # Plot length distribution using GNU plot.
5485 my ( $in, # handle to in stream
5486 $out, # handle to out stream
5487 $options, # options hash
5492 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5494 $options->{ "title" } ||= "Length Distribution";
5496 while ( $record = get_record( $in ) )
5498 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5500 put_record( $record, $out ) if not $options->{ "no_stream" };
5503 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5505 for ( $i = 0; $i < $max; $i++ ) {
5506 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5509 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5511 $fh = write_stream( $options->{ "data_out" } );
5513 print $fh "$_\n" foreach @{ $result };
5519 sub script_plot_chrdist
5521 # Martin A. Hansen, August 2007.
5523 # Plot chromosome distribution using GNU plot.
5525 my ( $in, # handle to in stream
5526 $out, # handle to out stream
5527 $options, # options hash
5532 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5534 $options->{ "title" } ||= "Chromosome Distribution";
5536 while ( $record = get_record( $in ) )
5538 if ( $record->{ "CHR" } ) { # generic
5539 $data_hash{ $record->{ "CHR" } }++;
5540 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5541 $data_hash{ $record->{ "S_ID" } }++;
5542 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5543 $data_hash{ $record->{ "S_ID" } }++;
5544 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5545 $data_hash{ $record->{ "S_ID" } }++;
5548 put_record( $record, $out ) if not $options->{ "no_stream" };
5551 foreach $elem ( keys %data_hash )
5555 $sort_key =~ s/chr//i;
5557 $sort_key =~ s/^X(.*)/99$1/;
5558 $sort_key =~ s/^Y(.*)/99$1/;
5559 $sort_key =~ s/^Z(.*)/999$1/;
5560 $sort_key =~ s/^M(.*)/9999$1/;
5561 $sort_key =~ s/^U(.*)/99999$1/;
5563 $count = $sort_key =~ tr/_//;
5565 $sort_key =~ s/_.*/"999999" x $count/ex;
5567 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5570 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5572 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5574 $fh = write_stream( $options->{ "data_out" } );
5576 print $fh "$_\n" foreach @{ $result };
5582 sub script_plot_karyogram
5584 # Martin A. Hansen, August 2007.
5586 # Plot hits on karyogram.
5588 my ( $in, # handle to in stream
5589 $out, # handle to out stream
5590 $options, # options hash
5595 my ( %options, $record, @data, $fh, $result, %data_hash );
5597 $options->{ "genome" } ||= "human";
5598 $options->{ "feat_color" } ||= "black";
5600 while ( $record = get_record( $in ) )
5602 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5604 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5607 put_record( $record, $out ) if not $options->{ "no_stream" };
5610 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5612 $fh = write_stream( $options->{ "data_out" } );
5620 sub script_plot_matches
5622 # Martin A. Hansen, August 2007.
5624 # Plot matches in 2D generating a dotplot.
5626 my ( $in, # handle to in stream
5627 $out, # handle to out stream
5628 $options, # options hash
5633 my ( $record, @data, $fh, $result, %data_hash );
5635 $options->{ "direction" } ||= "both";
5637 while ( $record = get_record( $in ) )
5639 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5640 push @data, $record;
5643 put_record( $record, $out ) if not $options->{ "no_stream" };
5646 $options->{ "title" } ||= "plot_matches";
5647 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5648 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5650 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5652 $fh = write_stream( $options->{ "data_out" } );
5654 print $fh "$_\n" foreach @{ $result };
5660 sub script_length_vals
5662 # Martin A. Hansen, August 2007.
5664 # Determine the length of the value for given keys.
5666 my ( $in, # handle to in stream
5667 $out, # handle to out stream
5668 $options, # options hash
5673 my ( $record, $key );
5675 while ( $record = get_record( $in ) )
5677 foreach $key ( @{ $options->{ "keys" } } )
5679 if ( $record->{ $key } ) {
5680 $record->{ $key . "_LEN" } = length $record->{ $key };
5684 put_record( $record, $out );
5691 # Martin A. Hansen, August 2007.
5693 # Calculates the sums for values of given keys.
5695 my ( $in, # handle to in stream
5696 $out, # handle to out stream
5697 $options, # options hash
5702 my ( $record, $key, %sum_hash, $fh );
5704 while ( $record = get_record( $in ) )
5706 foreach $key ( @{ $options->{ "keys" } } )
5708 if ( $record->{ $key } ) {
5709 $sum_hash{ $key } += $record->{ $key };
5713 put_record( $record, $out ) if not $options->{ "no_stream" };
5716 $fh = write_stream( $options->{ "data_out" } );
5718 foreach $key ( @{ $options->{ "keys" } } ) {
5719 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5726 sub script_mean_vals
5728 # Martin A. Hansen, August 2007.
5730 # Calculate the mean of values of given keys.
5732 my ( $in, # handle to in stream
5733 $out, # handle to out stream
5734 $options, # options hash
5739 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5741 while ( $record = get_record( $in ) )
5743 foreach $key ( @{ $options->{ "keys" } } )
5745 if ( $record->{ $key } )
5747 $sum_hash{ $key } += $record->{ $key };
5748 $count_hash{ $key }++;
5752 put_record( $record, $out ) if not $options->{ "no_stream" };
5755 $fh = write_stream( $options->{ "data_out" } );
5757 foreach $key ( @{ $options->{ "keys" } } )
5759 if ( $count_hash{ $key } ) {
5760 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5765 put_record( { $key . "_MEAN" => $mean } , $fh );
5772 sub script_median_vals
5774 # Martin A. Hansen, March 2008.
5776 # Calculate the median values of given keys.
5778 my ( $in, # handle to in stream
5779 $out, # handle to out stream
5780 $options, # options hash
5785 my ( $record, $key, %median_hash, $median, $fh );
5787 while ( $record = get_record( $in ) )
5789 foreach $key ( @{ $options->{ "keys" } } ) {
5790 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5793 put_record( $record, $out ) if not $options->{ "no_stream" };
5796 $fh = write_stream( $options->{ "data_out" } );
5798 foreach $key ( @{ $options->{ "keys" } } )
5800 if ( $median_hash{ $key } ) {
5801 $median = Maasha::Calc::median( $median_hash{ $key } );
5806 put_record( { $key . "_MEDIAN" => $median } , $fh );
5815 # Martin A. Hansen, February 2008.
5817 # Determine the maximum values of given keys.
5819 my ( $in, # handle to in stream
5820 $out, # handle to out stream
5821 $options, # options hash
5826 my ( $record, $key, $fh, %max_hash, $max_record );
5828 while ( $record = get_record( $in ) )
5830 foreach $key ( @{ $options->{ "keys" } } )
5832 if ( $record->{ $key } )
5834 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5838 put_record( $record, $out ) if not $options->{ "no_stream" };
5841 $fh = write_stream( $options->{ "data_out" } );
5843 foreach $key ( @{ $options->{ "keys" } } )
5845 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5848 put_record( $max_record, $fh );
5856 # Martin A. Hansen, February 2008.
5858 # Determine the minimum values of given keys.
5860 my ( $in, # handle to in stream
5861 $out, # handle to out stream
5862 $options, # options hash
5867 my ( $record, $key, $fh, %min_hash, $min_record );
5869 while ( $record = get_record( $in ) )
5871 foreach $key ( @{ $options->{ "keys" } } )
5873 if ( defined $record->{ $key } )
5875 if ( exists $min_hash{ $key } ) {
5876 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5878 $min_hash{ $key } = $record->{ $key };
5883 put_record( $record, $out ) if not $options->{ "no_stream" };
5886 $fh = write_stream( $options->{ "data_out" } );
5888 foreach $key ( @{ $options->{ "keys" } } )
5890 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5893 put_record( $min_record, $fh );
5899 sub script_upload_to_ucsc
5901 # Martin A. Hansen, August 2007.
5903 # Calculate the mean of values of given keys.
5905 # This routine has developed into the most ugly hack. Do something!
5907 my ( $in, # handle to in stream
5908 $out, # handle to out stream
5909 $options, # options hash
5914 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5915 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones, $vals );
5917 $options->{ "short_label" } ||= $options->{ 'table' };
5918 $options->{ "long_label" } ||= $options->{ 'table' };
5919 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5920 $options->{ "priority" } ||= 1;
5921 $options->{ "visibility" } ||= "pack";
5922 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5923 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5925 $file = "$BP_TMP/ucsc_upload.tmp";
5933 if ( $options->{ 'wiggle' } )
5935 while ( $record = get_record( $in ) )
5937 put_record( $record, $out ) if not $options->{ "no_stream" };
5939 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5940 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5941 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5943 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5945 $fh_out = $fh_hash{ $record->{ "CHR" } };
5947 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5950 map { close $_ } keys %fh_hash;
5952 $fh_out = Maasha::Common::write_open( $file );
5954 foreach $chr ( sort keys %fh_hash )
5956 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5958 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5962 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5964 $chr = $entry->{ 'CHR' };
5965 $beg = $entry->{ 'CHR_BEG' };
5966 $end = $entry->{ 'CHR_END' };
5967 $q_id = $entry->{ 'Q_ID' };
5969 if ( $options->{ "score" } ) {
5970 $clones = $entry->{ 'SCORE' };
5971 } if ( $q_id =~ /_(\d+)$/ ) {
5981 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5986 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5987 $block->[ $i ] += $clones;
5990 $max = Maasha::Calc::max( $max, $end );
5999 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
6000 $block->[ $i ] += $clones;
6007 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
6009 unlink "$BP_TMP/$chr";
6018 $fh_out = Maasha::Common::write_open( $file );
6020 while ( $record = get_record( $in ) )
6022 put_record( $record, $out ) if not $options->{ "no_stream" };
6024 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6026 $vals = $record->{ "VALS" };
6029 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6030 print $fh_out "$vals\n";
6032 $format = "WIGGLE" if not $format;
6034 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6036 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6037 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6041 $format = "PSL" if not $format;
6043 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6045 # chrom chromStart chromEnd name score strand size secStr conf
6047 print $fh_out join ( "\t",
6049 $record->{ "CHR_BEG" },
6050 $record->{ "CHR_END" } + 1,
6051 $record->{ "Q_ID" },
6052 $record->{ "SCORE" },
6053 $record->{ "STRAND" },
6054 $record->{ "SIZE" },
6055 $record->{ "SEC_STRUCT" },
6056 $record->{ "CONF" },
6059 $format = "BED_SS" if not $format;
6061 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6063 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6065 $format = "BED" if not $format;
6066 $columns = $record->{ "BED_COLS" } if not $columns;
6068 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6070 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6072 $format = "BED" if not $format;
6073 $columns = 6 if not $columns;
6075 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6077 $record->{ "CHR" } = $record->{ "S_ID" };
6078 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6079 $record->{ "CHR_END" } = $record->{ "S_END" };
6080 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6082 $format = "BED" if not $format;
6083 $columns = 6 if not $columns;
6085 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6087 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6089 $record->{ "CHR" } = $record->{ "S_ID" };
6090 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6091 $record->{ "CHR_END" } = $record->{ "S_END" };
6092 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6093 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6095 $format = "BED" if not $format;
6096 $columns = 6 if not $columns;
6098 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6101 if ( $i == $options->{ "chunk_size" } )
6105 if ( $format eq "BED" ) {
6106 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6107 } elsif ( $format eq "PSL" ) {
6108 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6117 $fh_out = Maasha::Common::write_open( $file );
6126 if ( exists $options->{ "database" } and $options->{ "table" } )
6128 if ( $format eq "BED" )
6130 $type = "bed $columns";
6132 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6134 elsif ( $format eq "BED_SS" )
6136 $options->{ "sec_struct" } = 1;
6138 $type = "sec_struct";
6140 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6142 elsif ( $format eq "PSL" )
6146 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6148 elsif ( $format eq "WIGGLE" )
6150 $options->{ "visibility" } = "full";
6152 $wig_file = "$options->{ 'table' }.wig";
6153 $wib_file = "$options->{ 'table' }.wib";
6155 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6157 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6159 if ( $options->{ 'verbose' } ) {
6160 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6162 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6165 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6173 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6178 Maasha::UCSC::update_my_tracks( $options, $type );
6183 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6188 # Martin A. Hansen, July 2008.
6190 # Given a biopiece record converts it to a FASTA record.
6191 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6192 # tried in that order.
6194 my ( $record, # record
6199 my ( $seq_name, $seq );
6201 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6202 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6204 if ( defined $seq_name and defined $seq ) {
6205 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6214 # Martin A. Hansen, July 2007.
6216 # Opens a stream to STDIN or a file,
6218 my ( $path, # path - OPTIONAL
6221 # Returns filehandle.
6225 if ( not -t STDIN ) {
6226 $fh = Maasha::Common::read_stdin();
6227 } elsif ( not $path ) {
6228 # Maasha::Common::error( qq(no data stream) );
6230 $fh = Maasha::Common::read_open( $path );
6233 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6241 # Martin A. Hansen, August 2007.
6243 # Opens a stream to STDOUT or a file.
6245 my ( $path, # path - OPTIONAL
6246 $gzip, # compress data - OPTIONAL
6249 # Returns filehandle.
6254 $fh = Maasha::Common::write_open( $path, $gzip );
6256 $fh = Maasha::Common::write_stdout();
6265 # Martin A. Hansen, July 2007.
6267 # Reads one record at a time and converts that record
6268 # to a Perl data structure (a hash) which is returned.
6270 my ( $fh, # handle to stream
6275 my ( $block, @lines, $line, $key, $value, %record );
6277 local $/ = "\n---\n";
6283 return if not defined $block;
6285 @lines = split "\n", $block;
6287 foreach $line ( @lines )
6289 ( $key, $value ) = split ": ", $line, 2;
6291 $record{ $key } = $value;
6294 return wantarray ? %record : \%record;
6300 # Martin A. Hansen, July 2007.
6302 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6304 my ( $data, # data structure
6305 $fh, # file handle - OPTIONAL
6310 if ( scalar keys %{ $data } )
6314 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6319 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6330 # Martin A. Hansen, November 2007.
6332 # Extracts files from an explicit GetOpt::Long argument
6333 # allowing for the use of glob. E.g.
6334 # --data_in=test.fna
6335 # --data_in=test.fna,test2.fna
6337 # --data_in=test.fna,/dir/*.fna
6339 my ( $option, # option from GetOpt::Long
6344 my ( $elem, @files );
6346 foreach $elem ( split ",", $option )
6350 } elsif ( $elem =~ /\*/ ) {
6351 push @files, glob( $elem );
6355 return wantarray ? @files : \@files;
6361 # Martin A. Hansen, April 2008.
6363 # Removes temporary directory and exits gracefully.
6364 # This subroutine is meant to be run always as the last
6365 # thing even if a script is dies or is interrupted
6368 my ( $sig, # signal from the %SIG
6371 # print STDERR "signal->$sig<-\n";
6379 if ( $sig =~ /MAASHA_ERROR/ ) {
6380 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6381 } elsif ( $sig eq "INT" ) {
6382 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6383 } elsif ( $sig eq "TERM" ) {
6384 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6386 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6398 # Martin A. Hansen, July 2008.
6400 # Cleans out any unused temporary files and direcotries in BP_TMP.
6404 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6406 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6408 $curr_pid = Maasha::Common::get_processid();
6410 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6412 foreach $dir ( @dirs )
6414 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6420 if ( $user eq Maasha::Common::get_user() )
6422 if ( not Maasha::Common::process_running( $pid ) )
6424 # print STDERR "Removing stale dir: $dir\n";
6425 Maasha::Common::dir_remove( $dir );
6427 elsif ( $pid == $curr_pid )
6429 # print STDERR "Removing current dir: $dir\n";
6430 Maasha::Common::dir_remove( $dir );
6444 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<