1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
77 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
80 $SIG{ '__DIE__' } = \&sig_handler;
81 $SIG{ 'INT' } = \&sig_handler;
82 $SIG{ 'TERM' } = \&sig_handler;
85 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
88 my ( $script, $BP_TMP );
90 $script = Maasha::Common::get_scriptname();
91 $BP_TMP = Maasha::Common::get_tmpdir();
94 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
97 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
98 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
100 $log_global->autoflush( 1 );
101 $log_local->autoflush( 1 );
103 &log( $log_global, $script, \@ARGV );
104 &log( $log_local, $script, \@ARGV );
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
113 my $t0 = gettimeofday();
115 run_script( $script );
117 my $t1 = gettimeofday();
119 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
122 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
127 # Martin A. Hansen, January 2008.
129 # Log messages to logfile.
131 my ( $fh, # filehandle to logfile
132 $script, # script name
133 $argv, # reference to @ARGV
138 my ( $time_stamp, $user );
140 $time_stamp = Maasha::Common::time_stamp();
142 $user = $ENV{ 'USER' };
144 $script = "biopieces" if $script eq "-e";
146 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
152 # Martin A. Hansen, August 2007.
154 # Run a specific script.
156 my ( $script, # script name
161 my ( $options, $in, $out );
163 $options = get_options( $script );
165 $options->{ "SCRIPT" } = $script;
167 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
168 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
171 $in = read_stream( $options->{ "stream_in" } );
172 $out = write_stream( $options->{ "stream_out" } );
174 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
175 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
176 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
177 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
178 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
179 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
180 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
181 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
182 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
183 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
184 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
185 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
186 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
187 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
188 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
189 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
190 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
191 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
192 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
193 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
194 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
195 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
196 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
198 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
199 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
200 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
201 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
202 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
203 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
204 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
205 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
206 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
207 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
208 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
209 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
212 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
213 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
214 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
215 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
216 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
217 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
218 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
219 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
220 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
221 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
222 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
223 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
224 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
225 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
226 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
227 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
228 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
229 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
230 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
231 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
232 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
233 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
234 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
235 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
236 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
237 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
238 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
239 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
240 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
241 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
242 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
243 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
244 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
245 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
246 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
247 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
248 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
249 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
250 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
251 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
252 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
253 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
254 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
255 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
256 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
257 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
258 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
259 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
260 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
261 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
262 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
263 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
264 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
265 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
267 close $in if defined $in;
274 # Martin A. Hansen, February 2008.
276 # Gets options from commandline and checks these vigerously.
278 my ( $script, # name of script
283 my ( %options, @options, $opt, @genomes, $real );
285 if ( $script eq "print_usage" )
291 elsif ( $script eq "read_fasta" )
298 elsif ( $script eq "read_tab" )
309 elsif ( $script eq "read_psl" )
316 elsif ( $script eq "read_bed" )
323 elsif ( $script eq "read_fixedstep" )
330 elsif ( $script eq "read_blast_tab" )
337 elsif ( $script eq "read_embl" )
347 elsif ( $script eq "read_stockholm" )
354 elsif ( $script eq "read_phastcons" )
365 elsif ( $script eq "read_soft" )
373 elsif ( $script eq "read_gff" )
380 elsif ( $script eq "read_2bit" )
388 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 }") );
1042 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
1044 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
1048 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1049 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1050 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1051 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1052 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1053 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1054 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1055 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1056 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" };
1057 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1058 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" };
1060 if ( $script eq "upload_to_ucsc" )
1062 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1063 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1066 return wantarray ? %options : \%options;
1070 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1073 sub script_print_usage
1075 # Martin A. Hansen, January 2008.
1077 # Retrieves usage information from file and
1078 # prints this nicely formatted.
1080 my ( $in, # handle to in stream
1081 $out, # handle to out stream
1082 $options, # options hash
1087 my ( $file, $wiki, $lines );
1089 if ( $options->{ 'data_in' } ) {
1090 $file = $options->{ 'data_in' };
1092 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1095 $wiki = Maasha::Gwiki::gwiki_read( $file );
1097 if ( not $options->{ "help" } ) {
1098 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1101 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1103 print STDERR "$_\n" foreach @{ $lines };
1109 sub script_list_biopieces
1111 # Martin A. Hansen, January 2008.
1113 # Prints the synopsis from the usage for each of the biopieces.
1115 my ( $in, # handle to in stream
1116 $out, # handle to out stream
1117 $options, # options hash
1122 my ( @files, $file, $wiki, $program, $synopsis );
1124 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1126 foreach $file ( sort @files )
1128 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1132 $wiki = Maasha::Gwiki::gwiki_read( $file );
1134 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1135 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1137 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1138 $synopsis =~ s/!(\w)/$1/g;
1140 printf( "%-30s%s\n", $program, $synopsis );
1148 sub script_list_genomes
1150 # Martin A. Hansen, January 2008.
1152 # Prints the synopsis from the usage for each of the biopieces.
1154 my ( $in, # handle to in stream
1155 $out, # handle to out stream
1156 $options, # options hash
1161 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1163 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1165 foreach $genome ( @genomes )
1167 next if $genome =~ /\.$/;
1169 @formats = Maasha::Common::ls_dirs( $genome );
1171 foreach $format ( @formats )
1173 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1175 $hash{ $1 }{ $2 } = 1;
1184 map { push @row, $_ } sort keys %found;
1186 print join( "\t", @row ), "\n";
1188 foreach $genome ( sort keys %hash )
1192 foreach $format ( sort keys %found )
1194 if ( exists $hash{ $genome }{ $format } ) {
1201 print join( "\t", @row ), "\n";
1206 sub script_read_fasta
1208 # Martin A. Hansen, August 2007.
1210 # Read sequences from FASTA file.
1212 my ( $in, # handle to in stream
1213 $out, # handle to out stream
1214 $options, # options hash
1219 my ( $record, $file, $data_in, $entry, $num );
1221 while ( $record = get_record( $in ) ) {
1222 put_record( $record, $out );
1227 foreach $file ( @{ $options->{ "files" } } )
1229 $data_in = Maasha::Common::read_open( $file );
1231 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1233 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1236 SEQ_NAME => $entry->[ SEQ_NAME ],
1237 SEQ => $entry->[ SEQ ],
1238 SEQ_LEN => length $entry->[ SEQ ],
1241 put_record( $record, $out );
1244 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1254 close $data_in if $data_in;
1260 # Martin A. Hansen, August 2007.
1262 # Read table or table columns from stream or file.
1264 my ( $in, # handle to in stream
1265 $out, # handle to out stream
1266 $options, # options hash
1271 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1273 $options->{ 'delimit' } ||= '\s+';
1275 while ( $record = get_record( $in ) ) {
1276 put_record( $record, $out );
1279 $skip = $options->{ 'skip' } ||= 0;
1282 foreach $file ( @{ $options->{ "files" } } )
1284 $data_in = Maasha::Common::read_open( $file );
1286 while ( $line = <$data_in> )
1294 next if $line =~ /^#|^$/;
1301 @fields = split /$options->{'delimit'}/, $line;
1303 if ( $options->{ "cols" } ) {
1304 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1309 for ( $i = 0; $i < @fields2; $i++ )
1311 if ( $options->{ "keys" }->[ $i ] ) {
1312 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1314 $record->{ "V" . $i } = $fields2[ $i ];
1318 put_record( $record, $out );
1320 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1330 close $data_in if $data_in;
1336 # Martin A. Hansen, August 2007.
1338 # Read psl table from stream or file.
1340 my ( $in, # handle to in stream
1341 $out, # handle to out stream
1342 $options, # options hash
1347 my ( $record, $file, $data_in, $num );
1349 while ( $record = get_record( $in ) ) {
1350 put_record( $record, $out );
1355 foreach $file ( @{ $options->{ "files" } } )
1357 $data_in = Maasha::Common::read_open( $file );
1359 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1361 put_record( $record, $out );
1363 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1375 # Martin A. Hansen, August 2007.
1377 # Read bed table from stream or file.
1379 my ( $in, # handle to in stream
1380 $out, # handle to out stream
1381 $options, # options hash
1386 my ( $file, $record, $entry, $data_in, $num );
1388 while ( $record = get_record( $in ) ) {
1389 put_record( $record, $out );
1394 foreach $file ( @{ $options->{ "files" } } )
1396 $data_in = Maasha::Common::read_open( $file );
1398 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1400 put_record( $entry, $out );
1402 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1412 close $data_in if $data_in;
1416 sub script_read_fixedstep
1418 # Martin A. Hansen, Juli 2008.
1420 # Read fixedStep wiggle format from stream or file.
1422 my ( $in, # handle to in stream
1423 $out, # handle to out stream
1424 $options, # options hash
1429 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1431 while ( $record = get_record( $in ) ) {
1432 put_record( $record, $out );
1437 foreach $file ( @{ $options->{ "files" } } )
1439 $data_in = Maasha::Common::read_open( $file );
1441 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1443 $head = shift @{ $entry };
1445 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1447 $record->{ "REC_TYPE" } = "fixed_step";
1448 $record->{ "CHR" } = $1;
1449 $record->{ "CHR_BEG" } = $2;
1450 $record->{ "STEP" } = $3;
1451 $record->{ "VALS" } = join ",", @{ $entry };
1454 put_record( $record, $out );
1456 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1466 close $data_in if $data_in;
1470 sub script_read_blast_tab
1472 # Martin A. Hansen, September 2007.
1474 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1476 my ( $in, # handle to in stream
1477 $out, # handle to out stream
1478 $options, # options hash
1483 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1485 while ( $record = get_record( $in ) ) {
1486 put_record( $record, $out );
1491 foreach $file ( @{ $options->{ "files" } } )
1493 $data_in = Maasha::Common::read_open( $file );
1495 while ( $line = <$data_in> )
1499 next if $line =~ /^#/;
1501 @fields = split /\t/, $line;
1503 $record->{ "REC_TYPE" } = "BLAST";
1504 $record->{ "Q_ID" } = $fields[ 0 ];
1505 $record->{ "S_ID" } = $fields[ 1 ];
1506 $record->{ "IDENT" } = $fields[ 2 ];
1507 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1508 $record->{ "MISMATCHES" } = $fields[ 4 ];
1509 $record->{ "GAPS" } = $fields[ 5 ];
1510 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1511 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1512 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1513 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1514 $record->{ "E_VAL" } = $fields[ 10 ];
1515 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1517 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1519 $record->{ "STRAND" } = '-';
1521 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1525 $record->{ "STRAND" } = '+';
1528 put_record( $record, $out );
1530 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1540 close $data_in if $data_in;
1544 sub script_read_embl
1546 # Martin A. Hansen, August 2007.
1550 my ( $in, # handle to in stream
1551 $out, # handle to out stream
1552 $options, # options hash
1557 my ( %options2, $file, $data_in, $num, $entry, $record );
1559 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1560 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1561 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1563 while ( $record = get_record( $in ) ) {
1564 put_record( $record, $out );
1569 foreach $file ( @{ $options->{ "files" } } )
1571 $data_in = Maasha::Common::read_open( $file );
1573 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1575 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1577 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1579 $record_copy = dclone $record;
1581 delete $record_copy->{ "FT" };
1583 put_record( $record_copy, $out );
1585 delete $record_copy->{ "SEQ" };
1587 foreach $feat ( keys %{ $record->{ "FT" } } )
1589 $record_copy->{ "FEAT_TYPE" } = $feat;
1591 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1593 foreach $qual ( keys %{ $feat2 } )
1595 $qual_val = join "; ", @{ $feat2->{ $qual } };
1600 $record_copy->{ $qual } = $qual_val;
1603 put_record( $record_copy, $out );
1607 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1617 close $data_in if $data_in;
1621 sub script_read_stockholm
1623 # Martin A. Hansen, August 2007.
1625 # Read Stockholm format.
1627 my ( $in, # handle to in stream
1628 $out, # handle to out stream
1629 $options, # options hash
1634 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1636 while ( $record = get_record( $in ) ) {
1637 put_record( $record, $out );
1642 foreach $file ( @{ $options->{ "files" } } )
1644 $data_in = Maasha::Common::read_open( $file );
1646 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1648 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1652 foreach $key ( keys %{ $record->{ "GF" } } ) {
1653 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1656 $record_anno->{ "ALIGN" } = $num;
1658 put_record( $record_anno, $out );
1660 foreach $seq ( @{ $record->{ "ALIGN" } } )
1662 undef $record_align;
1665 SEQ_NAME => $seq->[ 0 ],
1669 put_record( $record_align, $out );
1672 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1682 close $data_in if $data_in;
1686 sub script_read_phastcons
1688 # Martin A. Hansen, December 2007.
1690 # Read PhastCons format.
1692 my ( $in, # handle to in stream
1693 $out, # handle to out stream
1694 $options, # options hash
1699 my ( $data_in, $file, $num, $entry, @records, $record );
1701 $options->{ "min" } ||= 10;
1702 $options->{ "dist" } ||= 25;
1703 $options->{ "threshold" } ||= 0.8;
1704 $options->{ "gap" } ||= 5;
1706 while ( $record = get_record( $in ) ) {
1707 put_record( $record, $out );
1712 foreach $file ( @{ $options->{ "files" } } )
1714 $data_in = Maasha::Common::read_open( $file );
1716 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1718 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1720 foreach $record ( @records )
1722 $record->{ "REC_TYPE" } = "BED";
1723 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1725 put_record( $record, $out );
1727 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1738 close $data_in if $data_in;
1742 sub script_read_soft
1744 # Martin A. Hansen, December 2007.
1747 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1749 my ( $in, # handle to in stream
1750 $out, # handle to out stream
1751 $options, # options hash
1756 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1758 while ( $record = get_record( $in ) ) {
1759 put_record( $record, $out );
1764 foreach $file ( @{ $options->{ "files" } } )
1766 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1768 $soft_index = Maasha::NCBI::soft_index_file( $file );
1770 $fh = Maasha::Common::read_open( $file );
1772 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1774 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1776 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1778 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1780 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1782 foreach $sample ( @samples )
1785 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1787 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1789 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1791 foreach $record ( @{ $records } )
1793 put_record( $record, $out );
1795 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1800 $old_end = $sample->{ "LINE_END" };
1808 close $data_in if $data_in;
1815 # Martin A. Hansen, February 2008.
1818 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1820 my ( $in, # handle to in stream
1821 $out, # handle to out stream
1822 $options, # options hash
1827 my ( $data_in, $file, $fh, $num, $record, $entry );
1829 while ( $record = get_record( $in ) ) {
1830 put_record( $record, $out );
1835 foreach $file ( @{ $options->{ "files" } } )
1837 $fh = Maasha::Common::read_open( $file );
1839 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1841 put_record( $entry, $out );
1843 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1853 close $data_in if $data_in;
1857 sub script_read_2bit
1859 # Martin A. Hansen, March 2008.
1861 # Read sequences from 2bit file.
1863 my ( $in, # handle to in stream
1864 $out, # handle to out stream
1865 $options, # options hash
1870 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1872 $mask = 1 if not $options->{ "no_mask" };
1874 while ( $record = get_record( $in ) ) {
1875 put_record( $record, $out );
1880 foreach $file ( @{ $options->{ "files" } } )
1882 $data_in = Maasha::Common::read_open( $file );
1884 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1886 foreach $line ( @{ $toc } )
1888 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1889 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1890 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1892 put_record( $record, $out );
1894 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1904 close $data_in if $data_in;
1908 sub script_read_solexa
1910 # Martin A. Hansen, March 2008.
1912 # Read Solexa sequence reads from file.
1914 my ( $in, # handle to in stream
1915 $out, # handle to out stream
1916 $options, # options hash
1921 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1923 $options->{ "format" } ||= "octal";
1924 $options->{ "quality" } ||= 20;
1926 while ( $record = get_record( $in ) ) {
1927 put_record( $record, $out );
1932 foreach $file ( @{ $options->{ "files" } } )
1934 $data_in = Maasha::Common::read_open( $file );
1936 if ( $options->{ "format" } eq "octal" )
1938 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1940 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1942 put_record( $record, $out );
1944 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1951 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1953 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1955 put_record( $record, $out );
1957 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1968 close $data_in if $data_in;
1972 sub script_read_solid
1974 # Martin A. Hansen, April 2008.
1976 # Read Solid sequence from file.
1978 my ( $in, # handle to in stream
1979 $out, # handle to out stream
1980 $options, # options hash
1985 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1987 $options->{ "quality" } ||= 15;
1989 while ( $record = get_record( $in ) ) {
1990 put_record( $record, $out );
1995 foreach $file ( @{ $options->{ "files" } } )
1997 $data_in = Maasha::Common::read_open( $file );
1999 while ( $line = <$data_in> )
2003 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
2005 @scores = split /,/, $seq_qual;
2006 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
2008 for ( $i = 0; $i < @seqs; $i++ ) {
2009 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2013 SEQ_NAME => $seq_name,
2015 SEQ_QUAL => $seq_qual,
2016 SEQ_LEN => length $seq_cs,
2017 SEQ => join( "", @seqs ),
2018 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2021 put_record( $record, $out );
2023 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2033 close $data_in if $data_in;
2037 sub script_read_mysql
2039 # Martin A. Hansen, May 2008.
2041 # Read a MySQL query into stream.
2043 my ( $in, # handle to in stream
2044 $out, # handle to out stream
2045 $options, # options hash
2050 my ( $record, $dbh, $results );
2052 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2053 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2055 while ( $record = get_record( $in ) ) {
2056 put_record( $record, $out );
2059 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2061 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2063 Maasha::SQL::disconnect( $dbh );
2065 map { put_record( $_ ) } @{ $results };
2069 sub script_format_genome
2071 # Martin A. Hansen, Juli 2008.
2073 # Format a genome to speficed formats.
2075 my ( $in, # handle to in stream
2076 $out, # handle to out stream
2077 $options, # options hash
2082 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2084 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2085 $genome = $options->{ 'genome' };
2087 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2088 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2089 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2091 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2093 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2095 $fasta_dir = "$dir/genomes/$genome/fasta";
2099 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2101 $fasta_dir = "$dir/genomes/$genome/fasta";
2103 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2106 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2108 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2110 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2112 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2115 while ( $record = get_record( $in ) )
2117 if ( $fh_out and $entry = record2fasta( $record ) )
2119 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2121 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2123 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2125 $vals = $record->{ 'VALS' };
2129 print $fh_out "$vals\n";
2132 put_record( $record, $out ) if not $options->{ "no_stream" };
2135 foreach $format ( @{ $options->{ 'formats' } } )
2137 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2138 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2139 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2140 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2141 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2144 close $fh_out if $fh_out;
2148 sub script_length_seq
2150 # Martin A. Hansen, August 2007.
2152 # Determine the length of sequences in stream.
2154 my ( $in, # handle to in stream
2155 $out, # handle to out stream
2156 $options, # options hash
2161 my ( $record, $total );
2163 while ( $record = get_record( $in ) )
2165 if ( $record->{ "SEQ" } )
2167 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2168 $total += $record->{ "SEQ_LEN" };
2171 put_record( $record, $out ) if not $options->{ "no_stream" };
2174 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2178 sub script_uppercase_seq
2180 # Martin A. Hansen, August 2007.
2182 # Uppercases sequences in stream.
2184 my ( $in, # handle to in stream
2185 $out, # handle to out stream
2192 while ( $record = get_record( $in ) )
2194 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2196 put_record( $record, $out );
2201 sub script_shuffle_seq
2203 # Martin A. Hansen, December 2007.
2205 # Shuffle sequences in stream.
2207 my ( $in, # handle to in stream
2208 $out, # handle to out stream
2215 while ( $record = get_record( $in ) )
2217 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2219 put_record( $record, $out );
2224 sub script_analyze_seq
2226 # Martin A. Hansen, August 2007.
2228 # Analyze sequence composition of sequences in stream.
2230 my ( $in, # handle to in stream
2231 $out, # handle to out stream
2236 my ( $record, $analysis );
2238 while ( $record = get_record( $in ) )
2240 if ( $record->{ "SEQ" } )
2242 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2244 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2247 put_record( $record, $out );
2252 sub script_analyze_tags
2254 # Martin A. Hansen, August 2008.
2256 # Analyze sequence tags in stream.
2258 my ( $in, # handle to in stream
2259 $out, # handle to out stream
2264 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2266 while ( $record = get_record( $in ) )
2268 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2270 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2274 $len_hash{ length( $record->{ "SEQ" } ) }++;
2275 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2278 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2280 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2284 $len_hash{ $record->{ "BED_LEN" } }++;
2285 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2290 foreach $key ( sort { $a <=> $b } keys %len_hash )
2292 $tag_record->{ "TAG_LEN" } = $key;
2293 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2294 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2296 put_record( $tag_record, $out );
2301 sub script_complexity_seq
2303 # Martin A. Hansen, May 2008.
2305 # Generates an index calculated as the most common di-residue over
2306 # the sequence length for all sequences in stream.
2308 my ( $in, # handle to in stream
2309 $out, # handle to out stream
2314 my ( $record, $index );
2316 while ( $record = get_record( $in ) )
2318 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2320 put_record( $record, $out );
2325 sub script_oligo_freq
2327 # Martin A. Hansen, August 2007.
2329 # Determine the length of sequences in stream.
2331 my ( $in, # handle to in stream
2332 $out, # handle to out stream
2333 $options, # options hash
2338 my ( $record, %oligos, @freq_table );
2340 $options->{ "word_size" } ||= 7;
2342 while ( $record = get_record( $in ) )
2344 if ( $record->{ "SEQ" } )
2346 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2348 if ( not $options->{ "all" } )
2350 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2352 map { put_record( $_, $out ) } @freq_table;
2358 put_record( $record, $out );
2361 if ( $options->{ "all" } )
2363 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2365 map { put_record( $_, $out ) } @freq_table;
2370 sub script_create_weight_matrix
2372 # Martin A. Hansen, August 2007.
2374 # Creates a weight matrix from an alignmnet.
2376 my ( $in, # handle to in stream
2377 $out, # handle to out stream
2378 $options, # options hash
2383 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2387 while ( $record = get_record( $in ) )
2389 if ( $record->{ "SEQ" } )
2391 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2393 $res = substr $record->{ "SEQ" }, $i, 1;
2395 $freq_hash{ $i }{ $res }++;
2396 $res_hash{ $res } = 1;
2403 put_record( $record, $out );
2407 foreach $res ( sort keys %res_hash )
2411 $record->{ "V0" } = $res;
2413 for ( $i = 0; $i < keys %freq_hash; $i++ )
2415 $freq = $freq_hash{ $i }{ $res } || 0;
2417 if ( $options->{ "percent" } ) {
2418 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2421 $record->{ "V" . ( $i + 1 ) } = $freq;
2424 put_record( $record, $out );
2429 sub script_calc_bit_scores
2431 # Martin A. Hansen, March 2007.
2433 # Calculates the bit scores for each position from an alignmnet in the stream.
2435 my ( $in, # handle to in stream
2436 $out, # handle to out stream
2441 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2445 while ( $record = get_record( $in ) )
2447 if ( $record->{ "SEQ" } )
2449 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2451 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2453 $res = substr $record->{ "SEQ" }, $i, 1;
2455 next if $res =~ /-|_|~|\./;
2457 $freq_hash{ $i }{ $res }++;
2464 put_record( $record, $out );
2470 if ( $type eq "protein" ) {
2476 for ( $i = 0; $i < keys %freq_hash; $i++ )
2478 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2480 $bit_diff = $bit_max - $bit_height;
2482 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2485 put_record( $record, $out );
2489 sub script_reverse_seq
2491 # Martin A. Hansen, August 2007.
2493 # Reverse sequence in record.
2495 my ( $in, # handle to in stream
2496 $out, # handle to out stream
2503 while ( $record = get_record( $in ) )
2505 if ( $record->{ "SEQ" } ) {
2506 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2509 put_record( $record, $out );
2514 sub script_complement_seq
2516 # Martin A. Hansen, August 2007.
2518 # Complement sequence in record.
2520 my ( $in, # handle to in stream
2521 $out, # handle to out stream
2526 my ( $record, $type );
2528 while ( $record = get_record( $in ) )
2530 if ( $record->{ "SEQ" } )
2533 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2536 if ( $type eq "rna" ) {
2537 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2538 } elsif ( $type eq "dna" ) {
2539 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2543 put_record( $record, $out );
2548 sub script_remove_indels
2550 # Martin A. Hansen, August 2007.
2552 # Remove indels from sequences in stream.
2554 my ( $in, # handle to in stream
2555 $out, # handle to out stream
2562 while ( $record = get_record( $in ) )
2564 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2566 put_record( $record, $out );
2571 sub script_transliterate_seq
2573 # Martin A. Hansen, August 2007.
2575 # Transliterate chars from sequence in record.
2577 my ( $in, # handle to in stream
2578 $out, # handle to out stream
2579 $options, # options hash
2584 my ( $record, $search, $replace, $delete );
2586 $search = $options->{ "search" } || "";
2587 $replace = $options->{ "replace" } || "";
2588 $delete = $options->{ "delete" } || "";
2590 while ( $record = get_record( $in ) )
2592 if ( $record->{ "SEQ" } )
2594 if ( $search and $replace ) {
2595 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2596 } elsif ( $delete ) {
2597 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2601 put_record( $record, $out );
2606 sub script_transliterate_vals
2608 # Martin A. Hansen, April 2008.
2610 # Transliterate chars from values in record.
2612 my ( $in, # handle to in stream
2613 $out, # handle to out stream
2614 $options, # options hash
2619 my ( $record, $search, $replace, $delete, $key );
2621 $search = $options->{ "search" } || "";
2622 $replace = $options->{ "replace" } || "";
2623 $delete = $options->{ "delete" } || "";
2625 while ( $record = get_record( $in ) )
2627 foreach $key ( @{ $options->{ "keys" } } )
2629 if ( exists $record->{ $key } )
2631 if ( $search and $replace ) {
2632 eval "\$record->{ $key } =~ tr/$search/$replace/";
2633 } elsif ( $delete ) {
2634 eval "\$record->{ $key } =~ tr/$delete//d";
2639 put_record( $record, $out );
2644 sub script_translate_seq
2646 # Martin A. Hansen, February 2008.
2648 # Translate DNA sequence into protein sequence.
2650 my ( $in, # handle to in stream
2651 $out, # handle to out stream
2652 $options, # options hash
2657 my ( $record, $frame, %new_record );
2659 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2661 while ( $record = get_record( $in ) )
2663 if ( $record->{ "SEQ" } )
2665 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2667 foreach $frame ( @{ $options->{ "frames" } } )
2669 %new_record = %{ $record };
2671 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2672 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2673 $new_record{ "FRAME" } = $frame;
2675 put_record( \%new_record, $out );
2681 put_record( $record, $out );
2687 sub script_extract_seq
2689 # Martin A. Hansen, August 2007.
2691 # Extract subsequences from sequences in record.
2693 my ( $in, # handle to in stream
2694 $out, # handle to out stream
2695 $options, # options hash
2700 my ( $beg, $end, $len, $record );
2702 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2705 $beg = $options->{ "beg" } - 1; # correcting for start offset
2708 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2710 } elsif ( defined $options->{ "end" } ) {
2711 $end = $options->{ "end" } - 1; # correcting for start offset
2714 $len = $options->{ "len" };
2716 # print "beg->$beg, end->$end, len->$len\n";
2718 while ( $record = get_record( $in ) )
2720 if ( $record->{ "SEQ" } )
2722 if ( defined $beg and defined $end )
2724 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2725 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2727 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2730 elsif ( defined $beg and defined $len )
2732 if ( $len > length $record->{ "SEQ" } ) {
2733 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2735 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2738 elsif ( defined $beg )
2740 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2744 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2746 put_record( $record, $out );
2751 sub script_get_genome_seq
2753 # Martin A. Hansen, December 2007.
2755 # Gets a subsequence from a genome.
2757 my ( $in, # handle to in stream
2758 $out, # handle to out stream
2759 $options, # options hash
2764 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2766 $options->{ "flank" } ||= 0;
2768 if ( $options->{ "genome" } )
2770 $genome = $options->{ "genome" };
2772 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2773 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2775 $fh = Maasha::Common::read_open( $genome_file );
2776 $index = Maasha::Fasta::index_retrieve( $index_file );
2778 shift @{ $index }; # Get rid of the file size info
2780 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2782 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2784 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2786 $beg = $index_beg + $options->{ "beg" } - 1;
2788 if ( $options->{ "len" } ) {
2789 $len = $options->{ "len" };
2790 } elsif ( $options->{ "end" } ) {
2791 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2794 $beg -= $options->{ "flank" };
2795 $len += 2 * $options->{ "flank" };
2797 if ( $beg <= $index_beg )
2799 $len -= $index_beg - $beg;
2803 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2805 next if $beg > $index_beg + $index_len;
2807 $record->{ "CHR" } = $options->{ "chr" };
2808 $record->{ "CHR_BEG" } = $beg - $index_beg;
2809 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2811 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2812 $record->{ "SEQ_LEN" } = $len;
2814 put_record( $record, $out );
2818 while ( $record = get_record( $in ) )
2820 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2822 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2824 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2826 $beg = $record->{ "CHR_BEG" } + $index_beg;
2827 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2829 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2831 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2833 $beg = $record->{ "S_BEG" } + $index_beg;
2834 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2836 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2838 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2840 $beg = $record->{ "S_BEG" } + $index_beg;
2841 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2844 $beg -= $options->{ "flank" };
2845 $len += 2 * $options->{ "flank" };
2847 if ( $beg <= $index_beg )
2849 $len -= $index_beg - $beg;
2853 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2855 next if $beg > $index_beg + $index_len;
2857 $record->{ "CHR_BEG" } = $beg - $index_beg;
2858 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2860 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2862 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2864 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2865 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2868 if ( $options->{ "mask" } )
2870 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2872 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2874 @begs = split ",", $record->{ "Q_BEGS" };
2875 @lens = split ",", $record->{ "BLOCKSIZES" };
2877 for ( $i = 0; $i < @begs; $i++ ) {
2878 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2884 put_record( $record, $out );
2891 sub script_get_genome_align
2893 # Martin A. Hansen, April 2008.
2895 # Gets a subalignment from a multiple genome alignment.
2897 my ( $in, # handle to in stream
2898 $out, # handle to out stream
2899 $options, # options hash
2904 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2906 $options->{ "strand" } ||= "+";
2910 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2912 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2914 $beg = $options->{ "beg" } - 1;
2916 if ( $options->{ "end" } ) {
2917 $end = $options->{ "end" };
2918 } elsif ( $options->{ "len" } ) {
2919 $end = $beg + $options->{ "len" };
2922 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2924 foreach $entry ( @{ $align } )
2926 $entry->{ "CHR" } = $record->{ "CHR" };
2927 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2928 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2929 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2930 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2931 $entry->{ "SCORE" } = $record->{ "SCORE" };
2933 put_record( $entry, $out );
2937 while ( $record = get_record( $in ) )
2939 if ( $record->{ "REC_TYPE" } eq "BED" )
2941 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2943 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2945 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2947 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2949 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2952 foreach $entry ( @{ $align } )
2954 $entry->{ "CHR" } = $record->{ "CHR" };
2955 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2956 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2957 $entry->{ "STRAND" } = $record->{ "STRAND" };
2958 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2959 $entry->{ "SCORE" } = $record->{ "SCORE" };
2961 put_record( $entry, $out );
2969 sub script_get_genome_phastcons
2971 # Martin A. Hansen, February 2008.
2973 # Get phastcons scores from genome intervals.
2975 my ( $in, # handle to in stream
2976 $out, # handle to out stream
2977 $options, # options hash
2982 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2984 $options->{ "flank" } ||= 0;
2986 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2987 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2989 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2990 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2992 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2994 $options->{ "beg" } -= 1; # request is 1-based
2995 $options->{ "end" } -= 1; # request is 1-based
2997 if ( $options->{ "len" } ) {
2998 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
3001 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
3003 $record->{ "CHR" } = $options->{ "chr" };
3004 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
3005 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
3007 $record->{ "PHASTCONS" } = join ",", @{ $scores };
3008 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
3010 put_record( $record, $out );
3013 while ( $record = get_record( $in ) )
3015 if ( $record->{ "REC_TYPE" } eq "BED" )
3017 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3019 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3021 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3023 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3025 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3028 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3029 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3031 put_record( $record, $out );
3034 close $fh_phastcons if $fh_phastcons;
3040 # Martin A. Hansen, December 2007.
3042 # Folds sequences in stream into secondary structures.
3044 my ( $in, # handle to in stream
3045 $out, # handle to out stream
3050 my ( $record, $type, $struct, $index );
3052 while ( $record = get_record( $in ) )
3054 if ( $record->{ "SEQ" } )
3057 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3060 if ( $type ne "protein" )
3062 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3063 $record->{ "SEC_STRUCT" } = $struct;
3064 $record->{ "FREE_ENERGY" } = $index;
3065 $record->{ "SCORE" } = abs int $index;
3066 $record->{ "SIZE" } = length $struct;
3067 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3071 put_record( $record, $out );
3076 sub script_split_seq
3078 # Martin A. Hansen, August 2007.
3080 # Split a sequence in stream into words.
3082 my ( $in, # handle to in stream
3083 $out, # handle to out stream
3084 $options, # options hash
3089 my ( $record, $new_record, $i, $subseq, %lookup );
3091 $options->{ "word_size" } ||= 7;
3093 while ( $record = get_record( $in ) )
3095 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3097 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3099 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3101 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3103 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3104 $new_record->{ "SEQ" } = $subseq;
3106 put_record( $new_record, $out );
3108 $lookup{ $subseq } = 1;
3112 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3113 $new_record->{ "SEQ" } = $subseq;
3115 put_record( $new_record, $out );
3121 put_record( $record, $out );
3127 sub script_split_bed
3129 # Martin A. Hansen, June 2008.
3131 # Split a BED record into overlapping windows.
3133 my ( $in, # handle to in stream
3134 $out, # handle to out stream
3135 $options, # options hash
3140 my ( $record, $new_record, $i );
3142 $options->{ "window_size" } ||= 20;
3143 $options->{ "step_size" } ||= 1;
3145 while ( $record = get_record( $in ) )
3147 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3149 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3151 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3153 $new_record->{ "REC_TYPE" } = "BED";
3154 $new_record->{ "CHR" } = $record->{ "CHR" };
3155 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3156 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3157 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3158 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3159 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3160 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3162 put_record( $new_record, $out );
3167 put_record( $record, $out );
3173 sub script_align_seq
3175 # Martin A. Hansen, August 2007.
3177 # Align sequences in stream.
3179 my ( $in, # handle to in stream
3180 $out, # handle to out stream
3185 my ( $record, @entries, $entry );
3187 while ( $record = get_record( $in ) )
3189 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3190 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3191 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3192 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3194 put_record( $record, $out );
3198 @entries = Maasha::Align::align( \@entries );
3200 foreach $entry ( @entries )
3202 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3205 SEQ_NAME => $entry->[ SEQ_NAME ],
3206 SEQ => $entry->[ SEQ ],
3209 put_record( $record, $out );
3217 # Martin A. Hansen, February 2008.
3219 # Using the first sequence in stream as reference, tile
3220 # all subsequent sequences based on pairwise alignments.
3222 my ( $in, # handle to in stream
3223 $out, # handle to out stream
3224 $options, # options hash
3229 my ( $record, $first, $ref_entry, @entries );
3233 while ( $record = get_record( $in ) )
3235 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3239 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3245 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3250 put_record( $record, $out );
3254 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3256 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3260 sub script_invert_align
3262 # Martin A. Hansen, February 2008.
3264 # Inverts an alignment showing only non-mathing residues
3265 # using the first sequence as reference.
3267 my ( $in, # handle to in stream
3268 $out, # handle to out stream
3269 $options, # options hash
3274 my ( $record, @entries );
3276 while ( $record = get_record( $in ) )
3278 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3280 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3284 put_record( $record, $out );
3288 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3290 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3294 sub script_patscan_seq
3296 # Martin A. Hansen, August 2007.
3298 # Locates patterns in sequences using scan_for_matches.
3300 my ( $in, # handle to in stream
3301 $out, # handle to out stream
3302 $options, # options hash
3307 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3309 if ( $options->{ "patterns" } ) {
3310 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3311 } elsif ( -f $options->{ "patterns_in" } ) {
3312 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3315 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3317 push @args, "-c" if $options->{ "comp" };
3318 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3319 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3321 $seq_file = "$BP_TMP/patscan.seq";
3322 $pat_file = "$BP_TMP/patscan.pat";
3323 $out_file = "$BP_TMP/patscan.out";
3325 $fh_out = Maasha::Common::write_open( $seq_file );
3329 while ( $record = get_record( $in ) )
3331 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3333 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3335 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3337 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3345 $arg = join " ", @args;
3346 $arg .= " -p" if $type eq "protein";
3348 foreach $pattern ( @{ $patterns } )
3350 $fh_out = Maasha::Common::write_open( $pat_file );
3352 print $fh_out "$pattern\n";
3356 if ( $options->{ 'genome' } ) {
3357 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3358 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3360 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3361 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3364 $fh_in = Maasha::Common::read_open( $out_file );
3366 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3368 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3370 if ( $options->{ 'genome' } )
3372 $result->{ "CHR" } = $result->{ "S_ID" };
3373 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3374 $result->{ "CHR_END" } = $result->{ "S_END" };
3376 delete $result->{ "S_ID" };
3377 delete $result->{ "S_BEG" };
3378 delete $result->{ "S_END" };
3382 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3385 put_record( $result, $out );
3397 sub script_create_blast_db
3399 # Martin A. Hansen, September 2007.
3401 # Creates a NCBI BLAST database with formatdb
3403 my ( $in, # handle to in stream
3404 $out, # handle to out stream
3405 $options, # options hash
3410 my ( $fh, $seq_type, $path, $record, $entry );
3412 $path = $options->{ "database" };
3414 $fh = Maasha::Common::write_open( $path );
3416 while ( $record = get_record( $in ) )
3418 put_record( $record, $out ) if not $options->{ "no_stream" };
3420 if ( $entry = record2fasta( $record ) )
3422 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3424 Maasha::Fasta::put_entry( $entry, $fh );
3430 if ( $seq_type eq "protein" ) {
3431 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3433 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3440 sub script_blast_seq
3442 # Martin A. Hansen, September 2007.
3444 # BLASTs sequences in stream against a given database.
3446 my ( $in, # handle to in stream
3447 $out, # handle to out stream
3448 $options, # options hash
3453 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3455 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3456 $options->{ "filter" } = "F";
3457 $options->{ "filter" } = "T" if $options->{ "filter" };
3458 $options->{ "cpus" } ||= 1;
3460 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3462 $tmp_in = "$BP_TMP/blast_query.seq";
3463 $tmp_out = "$BP_TMP/blast.result";
3465 $fh_out = Maasha::Common::write_open( $tmp_in );
3467 while ( $record = get_record( $in ) )
3469 if ( $entry = record2fasta( $record ) )
3471 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3473 Maasha::Fasta::put_entry( $entry, $fh_out );
3476 put_record( $record, $out );
3481 if ( -f $options->{ 'database' } . ".phr" ) {
3482 $s_type = "protein";
3484 $s_type = "nucleotide";
3487 if ( not $options->{ 'program' } )
3489 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3490 $options->{ 'program' } = "blastn";
3491 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3492 $options->{ 'program' } = "blastp";
3493 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3494 $options->{ 'program' } = "blastx";
3495 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3496 $options->{ 'program' } = "tblastn";
3500 if ( $options->{ 'verbose' } )
3502 Maasha::Common::run(
3505 "-p $options->{ 'program' }",
3506 "-e $options->{ 'e_val' }",
3507 "-a $options->{ 'cpus' }",
3510 "-d $options->{ 'database' }",
3511 "-F $options->{ 'filter' }",
3519 Maasha::Common::run(
3522 "-p $options->{ 'program' }",
3523 "-e $options->{ 'e_val' }",
3524 "-a $options->{ 'cpus' }",
3527 "-d $options->{ 'database' }",
3528 "-F $options->{ 'filter' }",
3538 $fh_out = Maasha::Common::read_open( $tmp_out );
3542 while ( $line = <$fh_out> )
3546 next if $line =~ /^#/;
3548 @fields = split /\s+/, $line;
3550 $record->{ "REC_TYPE" } = "BLAST";
3551 $record->{ "Q_ID" } = $fields[ 0 ];
3552 $record->{ "S_ID" } = $fields[ 1 ];
3553 $record->{ "IDENT" } = $fields[ 2 ];
3554 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3555 $record->{ "MISMATCHES" } = $fields[ 4 ];
3556 $record->{ "GAPS" } = $fields[ 5 ];
3557 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3558 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3559 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3560 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3561 $record->{ "E_VAL" } = $fields[ 10 ];
3562 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3564 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3566 $record->{ "STRAND" } = '-';
3568 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3572 $record->{ "STRAND" } = '+';
3575 put_record( $record, $out );
3586 # Martin A. Hansen, August 2007.
3588 # BLATs sequences in stream against a given genome.
3590 my ( $in, # handle to in stream
3591 $out, # handle to out stream
3592 $options, # options hash
3597 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3599 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3601 $options->{ 'tile_size' } ||= 11;
3602 $options->{ 'one_off' } ||= 0;
3603 $options->{ 'min_identity' } ||= 90;
3604 $options->{ 'min_score' } ||= 0;
3605 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3607 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3608 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3609 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3610 $blat_args .= " -minScore=$options->{ 'min_score' }";
3611 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3612 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3614 $query_file = "$BP_TMP/blat.seq";
3616 $fh_out = Maasha::Common::write_open( $query_file );
3618 while ( $record = get_record( $in ) )
3620 if ( $entry = record2fasta( $record ) )
3622 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3623 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3626 put_record( $record, $out );
3631 $blat_args .= " -t=dnax" if $type eq "protein";
3632 $blat_args .= " -q=$type";
3634 $result_file = "$BP_TMP/blat.psl";
3636 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3640 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3642 map { put_record( $_, $out ) } @{ $entries };
3644 unlink $result_file;
3650 # Martin A. Hansen, July 2008.
3652 # soap sequences in stream against a given file or genome.
3654 my ( $in, # handle to in stream
3655 $out, # handle to out stream
3656 $options, # options hash
3661 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3663 $options->{ "seed_size" } ||= 10;
3664 $options->{ "mismatches" } ||= 2;
3665 $options->{ "gap_size" } ||= 0;
3666 $options->{ "cpus" } ||= 1;
3668 if ( $options->{ "genome" } ) {
3669 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3672 $tmp_in = "$BP_TMP/soap_query.seq";
3673 $tmp_out = "$BP_TMP/soap.result";
3675 $fh_out = Maasha::Common::write_open( $tmp_in );
3679 while ( $record = get_record( $in ) )
3681 if ( $entry = record2fasta( $record ) )
3683 Maasha::Fasta::put_entry( $entry, $fh_out );
3688 put_record( $record, $out );
3696 "-s $options->{ 'seed_size' }",
3699 "-v $options->{ 'mismatches' }",
3700 "-g $options->{ 'gap_size' }",
3701 "-p $options->{ 'cpus' }",
3702 "-d $options->{ 'in_file' }",
3706 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3708 Maasha::Common::run( "soap", $args, 1 );
3712 $fh_out = Maasha::Common::read_open( $tmp_out );
3716 while ( $line = <$fh_out> )
3720 @fields = split /\t/, $line;
3722 $record->{ "REC_TYPE" } = "SOAP";
3723 $record->{ "Q_ID" } = $fields[ 0 ];
3724 $record->{ "SCORE" } = $fields[ 3 ];
3725 $record->{ "STRAND" } = $fields[ 6 ];
3726 $record->{ "S_ID" } = $fields[ 7 ];
3727 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3728 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3730 put_record( $record, $out );
3740 sub script_match_seq
3742 # Martin A. Hansen, August 2007.
3744 # BLATs sequences in stream against a given genome.
3746 my ( $in, # handle to in stream
3747 $out, # handle to out stream
3748 $options, # options hash
3753 my ( $record, @entries, $results );
3755 $options->{ "word_size" } ||= 20;
3756 $options->{ "direction" } ||= "both";
3758 while ( $record = get_record( $in ) )
3760 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3761 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3764 put_record( $record, $out );
3767 if ( @entries == 1 )
3769 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3771 map { put_record( $_, $out ) } @{ $results };
3773 elsif ( @entries == 2 )
3775 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3777 map { put_record( $_, $out ) } @{ $results };
3782 sub script_create_vmatch_index
3784 # Martin A. Hansen, January 2008.
3786 # Create a vmatch index from sequences in the stream.
3788 my ( $in, # handle to in stream
3789 $out, # handle to out stream
3790 $options, # options hash
3795 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3797 if ( $options->{ "index_name" } )
3799 $file_tmp = $options->{ 'index_name' };
3800 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3803 while ( $record = get_record( $in ) )
3805 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3807 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3809 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3812 put_record( $record, $out ) if not $options->{ "no_stream" };
3815 if ( $options->{ "index_name" } )
3819 if ( $type eq "protein" ) {
3820 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3822 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3830 sub script_vmatch_seq
3832 # Martin A. Hansen, August 2007.
3834 # Vmatches sequences in stream against a given genome.
3836 my ( $in, # handle to in stream
3837 $out, # handle to out stream
3838 $options, # options hash
3843 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3845 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3847 if ( $options->{ "index_name" } )
3849 @index_files = $options->{ "index_name" };
3853 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3855 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3857 @index_files = sort keys %hash;
3860 while ( $record = get_record( $in ) )
3862 push @records, $record;
3864 put_record( $record, $out );
3867 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3871 $fh_in = Maasha::Common::read_open( $result_file );
3873 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3874 put_record( $record, $out );
3879 unlink $result_file;
3883 sub script_write_fasta
3885 # Martin A. Hansen, August 2007.
3887 # Write FASTA entries from sequences in stream.
3889 my ( $in, # handle to in stream
3890 $out, # handle to out stream
3891 $options, # options hash
3896 my ( $record, $fh, $entry );
3898 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3900 while ( $record = get_record( $in ) )
3902 if ( $entry = record2fasta( $record ) ) {
3903 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3906 put_record( $record, $out ) if not $options->{ "no_stream" };
3913 sub script_write_align
3915 # Martin A. Hansen, August 2007.
3917 # Write pretty alignments aligned sequences in stream.
3919 my ( $in, # handle to in stream
3920 $out, # handle to out stream
3921 $options, # options hash
3926 my ( $fh, $record, @entries );
3928 $fh = write_stream( $options->{ "data_out" } ) ;
3930 while ( $record = get_record( $in ) )
3932 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3933 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3936 put_record( $record, $out ) if not $options->{ "no_stream" };
3939 if ( scalar( @entries ) == 2 ) {
3940 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3941 } elsif ( scalar ( @entries ) > 2 ) {
3942 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3949 sub script_write_blast
3951 # Martin A. Hansen, November 2007.
3953 # Write data in blast table format (-m8 and 9).
3955 my ( $in, # handle to in stream
3956 $out, # handle to out stream
3957 $options, # options hash
3962 my ( $fh, $record, $first );
3964 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3968 while ( $record = get_record( $in ) )
3970 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3972 if ( $options->{ "comment" } and $first )
3974 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";
3979 if ( $record->{ "STRAND" } eq "-" ) {
3980 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3983 print $fh join( "\t",
3984 $record->{ "Q_ID" },
3985 $record->{ "S_ID" },
3986 $record->{ "IDENT" },
3987 $record->{ "ALIGN_LEN" },
3988 $record->{ "MISMATCHES" },
3989 $record->{ "GAPS" },
3990 $record->{ "Q_BEG" } + 1,
3991 $record->{ "Q_END" } + 1,
3992 $record->{ "S_BEG" } + 1,
3993 $record->{ "S_END" } + 1,
3994 $record->{ "E_VAL" },
3995 $record->{ "BIT_SCORE" }
3999 put_record( $record, $out ) if not $options->{ "no_stream" };
4006 sub script_write_tab
4008 # Martin A. Hansen, August 2007.
4010 # Write data as table.
4012 my ( $in, # handle to in stream
4013 $out, # handle to out stream
4014 $options, # options hash
4019 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4021 $options->{ "delimit" } ||= "\t";
4023 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4025 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4027 while ( $record = get_record( $in ) )
4032 if ( $options->{ "keys" } )
4034 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4038 foreach $key ( @{ $options->{ "keys" } } )
4040 if ( exists $record->{ $key } )
4042 push @keys, $key if $options->{ "comment" };
4043 push @vals, $record->{ $key };
4050 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4052 next if exists $no_keys{ $key };
4054 push @keys, $key if $options->{ "comment" };
4055 push @vals, $record->{ $key };
4059 if ( @keys and $options->{ "comment" } )
4061 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4063 delete $options->{ "comment" };
4066 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4068 put_record( $record, $out ) if not $options->{ "no_stream" };
4075 sub script_write_bed
4077 # Martin A. Hansen, August 2007.
4079 # Write BED format for the UCSC genome browser using records in stream.
4081 my ( $in, # handle to in stream
4082 $out, # handle to out stream
4083 $options, # options hash
4088 my ( $fh, $record, $new_record );
4090 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4092 while ( $record = get_record( $in ) )
4094 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4096 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4098 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4100 $new_record->{ "CHR" } = $record->{ "S_ID" };
4101 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4102 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4103 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4104 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4105 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4107 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4109 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4111 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4113 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
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 "VMATCH" 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; # or use E_VAL somehow
4131 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4133 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4135 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4137 $new_record->{ "CHR" } = $record->{ "S_ID" };
4138 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4139 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4140 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4141 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4142 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4144 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4146 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4148 Maasha::UCSC::bed_put_entry( $record, $fh );
4151 put_record( $record, $out ) if not $options->{ "no_stream" };
4158 sub script_write_psl
4160 # Martin A. Hansen, August 2007.
4162 # Write PSL output from stream.
4164 my ( $in, # handle to in stream
4165 $out, # handle to out stream
4166 $options, # options hash
4171 my ( $fh, $record, @output, $first );
4175 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4177 while ( $record = get_record( $in ) )
4179 put_record( $record, $out ) if not $options->{ "no_stream" };
4181 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4183 Maasha::UCSC::psl_put_header( $fh ) if $first;
4184 Maasha::UCSC::psl_put_entry( $record, $fh );
4193 sub script_write_fixedstep
4195 # Martin A. Hansen, Juli 2008.
4197 # Write fixedStep entries from recrods in the stream.
4199 my ( $in, # handle to in stream
4200 $out, # handle to out stream
4201 $options, # options hash
4206 my ( $fh, $record, $vals );
4208 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4210 while ( $record = get_record( $in ) )
4212 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4214 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4216 $vals = $record->{ 'VALS' };
4220 print $fh "$vals\n";
4223 put_record( $record, $out ) if not $options->{ "no_stream" };
4230 sub script_write_2bit
4232 # Martin A. Hansen, March 2008.
4234 # Write sequence entries from stream in 2bit format.
4236 my ( $in, # handle to in stream
4237 $out, # handle to out stream
4238 $options, # options hash
4243 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4245 $mask = 1 if not $options->{ "no_mask" };
4247 $tmp_file = "$BP_TMP/write_2bit.fna";
4248 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4250 $fh_out = write_stream( $options->{ "data_out" } );
4252 while ( $record = get_record( $in ) )
4254 if ( $entry = record2fasta( $record ) ) {
4255 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4258 put_record( $record, $out ) if not $options->{ "no_stream" };
4263 $fh_in = Maasha::Common::read_open( $tmp_file );
4265 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4274 sub script_write_solid
4276 # Martin A. Hansen, April 2008.
4278 # Write di-base encoded Solid sequence from entries in stream.
4280 my ( $in, # handle to in stream
4281 $out, # handle to out stream
4282 $options, # options hash
4287 my ( $record, $fh, $entry );
4289 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4291 while ( $record = get_record( $in ) )
4293 if ( $entry = record2fasta( $record ) )
4295 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4297 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4300 put_record( $record, $out ) if not $options->{ "no_stream" };
4307 sub script_plot_seqlogo
4309 # Martin A. Hansen, August 2007.
4311 # Calculates and writes a sequence logo for alignments.
4313 my ( $in, # handle to in stream
4314 $out, # handle to out stream
4315 $options, # options hash
4320 my ( $record, @entries, $logo, $fh );
4322 while ( $record = get_record( $in ) )
4324 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4325 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4328 put_record( $record, $out ) if not $options->{ "no_stream" };
4331 $logo = Maasha::Plot::seq_logo( \@entries );
4333 $fh = write_stream( $options->{ "data_out" } );
4341 sub script_plot_phastcons_profiles
4343 # Martin A. Hansen, January 2008.
4345 # Plots PhastCons profiles.
4347 my ( $in, # handle to in stream
4348 $out, # handle to out stream
4349 $options, # options hash
4354 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4356 $options->{ "title" } ||= "PhastCons Profiles";
4358 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4359 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4361 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4362 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4364 while ( $record = get_record( $in ) )
4366 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4368 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4370 push @{ $AoA }, [ @{ $scores } ];
4373 put_record( $record, $out ) if not $options->{ "no_stream" };
4376 Maasha::UCSC::phastcons_normalize( $AoA );
4378 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4379 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4381 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4383 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4385 $fh = write_stream( $options->{ "data_out" } );
4387 print $fh "$_\n" foreach @{ $plot };
4393 sub script_analyze_bed
4395 # Martin A. Hansen, March 2008.
4397 # Analyze BED entries in stream.
4399 my ( $in, # handle to in stream
4400 $out, # handle to out stream
4401 $options, # options hash
4408 while ( $record = get_record( $in ) )
4410 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4412 put_record( $record, $out );
4417 sub script_analyze_vals
4419 # Martin A. Hansen, August 2007.
4421 # Analyze values for given keys in stream.
4423 my ( $in, # handle to in stream
4424 $out, # handle to out stream
4425 $options, # options hash
4430 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4432 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4434 while ( $record = get_record( $in ) )
4436 foreach $key ( keys %{ $record } )
4438 next if $options->{ "keys" } and not exists $key_hash{ $key };
4440 $analysis->{ $key }->{ "COUNT" }++;
4442 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4444 $analysis->{ $key }->{ "TYPE" } = "num";
4445 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4446 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4447 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4451 $len = length $record->{ $key };
4453 $analysis->{ $key }->{ "TYPE" } = "alph";
4454 $analysis->{ $key }->{ "SUM" } += $len;
4455 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4456 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4460 put_record( $record, $out ) if not $options->{ "no_stream" };
4463 foreach $key ( keys %{ $analysis } )
4465 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4466 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4469 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4479 if ( $options->{ "keys" } ) {
4480 @keys = @{ $options->{ "keys" } };
4482 @keys = keys %{ $analysis };
4485 foreach $key ( @keys )
4487 $keys .= sprintf "% 15s", $key;
4488 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4489 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4490 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4491 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4492 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4493 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4496 print $out "$keys\n";
4497 print $out "$types\n";
4498 print $out "$counts\n";
4499 print $out "$mins\n";
4500 print $out "$maxs\n";
4501 print $out "$sums\n";
4502 print $out "$means\n";
4506 sub script_head_records
4508 # Martin A. Hansen, August 2007.
4510 # Display the first sequences in stream.
4512 my ( $in, # handle to in stream
4513 $out, # handle to out stream
4514 $options, # options hash
4519 my ( $record, $count );
4521 $options->{ "num" } ||= 10;
4525 while ( $record = get_record( $in ) )
4529 put_record( $record, $out );
4531 last if $count == $options->{ "num" };
4536 sub script_remove_keys
4538 # Martin A. Hansen, August 2007.
4540 # Remove keys from stream.
4542 my ( $in, # handle to in stream
4543 $out, # handle to out stream
4544 $options, # options hash
4549 my ( $record, $new_record );
4551 while ( $record = get_record( $in ) )
4553 if ( $options->{ "keys" } )
4555 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4557 elsif ( $options->{ "save_keys" } )
4559 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4561 $record = $new_record;
4564 put_record( $record, $out ) if keys %{ $record };
4569 sub script_remove_adaptor
4571 # Martin A. Hansen, August 2008.
4573 # Find and remove adaptor from sequences in the stream.
4575 my ( $in, # handle to in stream
4576 $out, # handle to out stream
4577 $options, # options hash
4582 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4584 $max_mismatch = $options->{ "mismatches" } || 0;
4585 $offset = $options->{ "offset" } || 15;
4586 $adaptor = $options->{ "adaptor" };
4587 $adaptor_len = length $adaptor;
4588 $adaptor = [ split //, uc $adaptor ];
4590 $max_match = $adaptor_len - $max_mismatch;
4592 while ( $record = get_record( $in ) )
4594 if ( $record->{ "SEQ" } )
4596 $seq = $record->{ "SEQ" };
4597 $seq_len = length $seq;
4598 $seq = [ split //, uc $seq ];
4600 $pos = Maasha::Seq::find_adaptor( $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch );
4602 $record->{ "ADAPTOR_POS" } = $pos;
4604 if ( $pos >= 0 and not $options->{ "no_remove" } )
4606 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4607 $record->{ "SEQ_LEN" } = $pos;
4610 put_record( $record, $out );
4614 put_record( $record, $out );
4620 sub script_rename_keys
4622 # Martin A. Hansen, August 2007.
4624 # Rename keys in stream.
4626 my ( $in, # handle to in stream
4627 $out, # handle to out stream
4628 $options, # options hash
4635 while ( $record = get_record( $in ) )
4637 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4639 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4641 delete $record->{ $options->{ "keys" }->[ 0 ] };
4644 put_record( $record, $out );
4649 sub script_uniq_vals
4651 # Martin A. Hansen, August 2007.
4653 # Find unique values in stream.
4655 my ( $in, # handle to in stream
4656 $out, # handle to out stream
4657 $options, # options hash
4662 my ( %hash, $record );
4664 while ( $record = get_record( $in ) )
4666 if ( $record->{ $options->{ "key" } } )
4668 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4670 put_record( $record, $out );
4672 $hash{ $record->{ $options->{ "key" } } } = 1;
4674 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4676 put_record( $record, $out );
4680 $hash{ $record->{ $options->{ "key" } } } = 1;
4685 put_record( $record, $out );
4691 sub script_merge_vals
4693 # Martin A. Hansen, August 2007.
4695 # Rename keys in stream.
4697 my ( $in, # handle to in stream
4698 $out, # handle to out stream
4699 $options, # options hash
4704 my ( $record, @join, $i );
4706 $options->{ "delimit" } ||= '_';
4708 while ( $record = get_record( $in ) )
4710 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4712 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4714 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4715 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4718 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4721 put_record( $record, $out );
4726 sub script_merge_records
4728 # Martin A. Hansen, July 2008.
4730 # Merges records in the stream based on identical values of two given keys.
4732 my ( $in, # handle to in stream
4733 $out, # handle to out stream
4734 $options, # options hash
4739 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4740 $num1, $num2, $num, $cmp, $i );
4742 $merge = $options->{ "merge" } || "AandB";
4744 $file1 = "$BP_TMP/merge_records1.tmp";
4745 $file2 = "$BP_TMP/merge_records2.tmp";
4747 $fh1 = Maasha::Common::write_open( $file1 );
4748 $fh2 = Maasha::Common::write_open( $file2 );
4750 $key1 = $options->{ "keys" }->[ 0 ];
4751 $key2 = $options->{ "keys" }->[ 1 ];
4753 $num = $key2 =~ s/n$//;
4757 while ( $record = get_record( $in ) )
4759 if ( exists $record->{ $key1 } )
4762 @vals1 = $record->{ $key1 };
4764 delete $record->{ $key1 };
4766 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4768 print $fh1 join( "\t", @vals1 ), "\n";
4772 elsif ( exists $record->{ $key2 } )
4775 @vals2 = $record->{ $key2 };
4777 delete $record->{ $key2 };
4779 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4781 print $fh2 join( "\t", @vals2 ), "\n";
4792 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4793 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4797 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4798 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4801 $fh1 = Maasha::Common::read_open( $file1 );
4802 $fh2 = Maasha::Common::read_open( $file2 );
4804 @vals1 = Maasha::Common::get_fields( $fh1 );
4805 @vals2 = Maasha::Common::get_fields( $fh2 );
4807 while ( $num1 > 0 and $num2 > 0 )
4812 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4814 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4819 if ( $merge =~ /^(AorB|AnotB)$/ )
4821 for ( $i = 0; $i < @keys1; $i++ ) {
4822 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4825 put_record( $record, $out );
4828 @vals1 = Maasha::Common::get_fields( $fh1 );
4833 if ( $merge =~ /^(BorA|BnotA)$/ )
4835 for ( $i = 0; $i < @keys2; $i++ ) {
4836 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4839 put_record( $record, $out );
4842 @vals2 = Maasha::Common::get_fields( $fh2 );
4847 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4849 for ( $i = 0; $i < @keys1; $i++ ) {
4850 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4853 for ( $i = 1; $i < @keys2; $i++ ) {
4854 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4857 put_record( $record, $out );
4860 @vals1 = Maasha::Common::get_fields( $fh1 );
4861 @vals2 = Maasha::Common::get_fields( $fh2 );
4873 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4877 for ( $i = 0; $i < @keys1; $i++ ) {
4878 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4881 put_record( $record, $out );
4884 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4888 for ( $i = 0; $i < @keys2; $i++ ) {
4889 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4892 put_record( $record, $out );
4899 # Martin A. Hansen, August 2007.
4901 # Grab for records in stream.
4903 my ( $in, # handle to in stream
4904 $out, # handle to out stream
4905 $options, # options hash
4910 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4912 if ( $options->{ "patterns" } )
4914 $patterns = [ split ",", $options->{ "patterns" } ];
4916 elsif ( -f $options->{ "patterns_in" } )
4918 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4920 elsif ( -f $options->{ "exact_in" } )
4922 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4924 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4929 if ( $options->{ "eval" } )
4931 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4939 while ( $record = get_record( $in ) )
4945 if ( $options->{ "keys" } )
4947 foreach $key ( @{ $options->{ "keys" } } )
4949 if ( exists $lookup_hash{ $record->{ $key } } )
4958 foreach $key ( keys %{ $record } )
4960 if ( not $options->{ "vals_only" } )
4962 if ( exists $lookup_hash{ $key } )
4969 if ( not $options->{ "keys_only" } )
4971 if ( exists $lookup_hash{ $record->{ $key } } )
4982 foreach $pattern ( @{ $patterns } )
4984 if ( $options->{ "keys" } )
4986 foreach $key ( @{ $options->{ "keys" } } )
4988 $pos = index $record->{ $key }, $pattern;
4990 goto FOUND if $pos >= 0;
4995 foreach $key ( keys %{ $record } )
4997 if ( not $options->{ "vals_only" } )
4999 $pos = index $key, $pattern;
5001 goto FOUND if $pos >= 0;
5004 if ( not $options->{ "keys_only" } )
5006 $pos = index $record->{ $key }, $pattern;
5008 goto FOUND if $pos >= 0;
5014 elsif ( $options->{ "regex" } )
5016 if ( $options->{ "keys" } )
5018 foreach $key ( @{ $options->{ "keys" } } )
5020 if ( $options->{ "case_insensitive" } ) {
5021 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5023 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5026 goto FOUND if $pos >= 0;
5031 foreach $key ( keys %{ $record } )
5033 if ( not $options->{ "vals_only" } )
5035 if ( $options->{ "case_insensitive" } ) {
5036 $pos = 1 if $key =~ /$options->{'regex'}/i;
5038 $pos = 1 if $key =~ /$options->{'regex'}/;
5041 goto FOUND if $pos >= 0;
5044 if ( not $options->{ "keys_only" } )
5046 if ( $options->{ "case_insensitive" } ) {
5047 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5049 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5052 goto FOUND if $pos >= 0;
5057 elsif ( $options->{ "eval" } )
5059 if ( defined $record->{ $key } )
5061 if ( $op eq "<" and $record->{ $key } < $val ) {
5062 $pos = 1 and goto FOUND;
5063 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
5064 $pos = 1 and goto FOUND;
5065 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
5066 $pos = 1 and goto FOUND;
5067 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
5068 $pos = 1 and goto FOUND;
5069 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
5070 $pos = 1 and goto FOUND;
5071 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
5072 $pos = 1 and goto FOUND;
5073 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
5074 $pos = 1 and goto FOUND;
5075 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5076 $pos = 1 and goto FOUND;
5083 if ( $pos >= 0 and not $options->{ "invert" } ) {
5084 put_record( $record, $out );
5085 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5086 put_record( $record, $out );
5094 # Martin A. Hansen, August 2007.
5096 # Evaluate extression for records in stream.
5098 my ( $in, # handle to in stream
5099 $out, # handle to out stream
5100 $options, # options hash
5105 my ( $record, $eval_key, $eval_val, $check, @keys );
5107 while ( $record = get_record( $in ) )
5109 if ( $options->{ "eval" } )
5111 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5119 @keys = split /\W+/, $eval_val;
5120 @keys = grep { ! /^\d+$/ } @keys;
5125 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5127 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5130 put_record( $record, $out );
5137 # Martin A. Hansen, June 2008.
5141 my ( $in, # handle to in stream
5142 $out, # handle to out stream
5143 $options, # options hash
5148 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5150 while ( $record = get_record( $in ) )
5154 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5156 push @rows, $record->{ $key };
5160 push @matrix, [ @rows ];
5165 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5167 foreach $row ( @matrix )
5169 for ( $i = 0; $i < @{ $row }; $i++ ) {
5170 $record->{ "V$i" } = $row->[ $i ];
5173 put_record( $record, $out );
5178 sub script_add_ident
5180 # Martin A. Hansen, May 2008.
5182 # Add a unique identifier to each record in stream.
5184 my ( $in, # handle to in stream
5185 $out, # handle to out stream
5186 $options, # options hash
5191 my ( $record, $key, $prefix, $i );
5193 $key = $options->{ "key" } || "ID";
5194 $prefix = $options->{ "prefix" } || "ID";
5198 while ( $record = get_record( $in ) )
5200 $record->{ $key } = sprintf( "$prefix%08d", $i );
5202 put_record( $record, $out );
5209 sub script_count_records
5211 # Martin A. Hansen, August 2007.
5213 # Count records in stream.
5215 my ( $in, # handle to in stream
5216 $out, # handle to out stream
5217 $options, # options hash
5222 my ( $record, $count, $result, $fh, $line );
5226 if ( $options->{ "no_stream" } )
5228 while ( $line = <$in> )
5232 $count++ if $line eq "---";
5237 while ( $record = get_record( $in ) )
5239 put_record( $record, $out );
5245 $result = { "RECORDS_COUNT" => $count };
5247 $fh = write_stream( $options->{ "data_out" } );
5249 put_record( $result, $fh );
5255 sub script_random_records
5257 # Martin A. Hansen, August 2007.
5259 # Pick a number or random records from stream.
5261 my ( $in, # handle to in stream
5262 $out, # handle to out stream
5263 $options, # options hash
5268 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5270 $options->{ "num" } ||= 10;
5272 $tmp_file = "$BP_TMP/random_records.tmp";
5274 $fh_out = Maasha::Common::write_open( $tmp_file );
5278 while ( $record = get_record( $in ) )
5280 put_record( $record, $fh_out );
5290 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5292 while ( $i < $options->{ "num" } )
5294 $rand = int( rand( $count ) );
5296 if ( not exists $rand_hash{ $rand } )
5298 $rand_hash{ $rand } = 1;
5300 $max = $rand if $rand > $max;
5306 $fh_in = Maasha::Common::read_open( $tmp_file );
5310 while ( $record = get_record( $fh_in ) )
5312 put_record( $record, $out ) if exists $rand_hash{ $count };
5314 last if $count == $max;
5325 sub script_sort_records
5327 # Martin A. Hansen, August 2007.
5329 # Sort to sort records according to keys.
5331 my ( $in, # handle to in stream
5332 $out, # handle to out stream
5333 $options, # options hash
5338 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5340 foreach $key ( @{ $options->{ "keys" } } )
5342 if ( $key =~ s/n$// ) {
5343 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5345 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5349 $sort_str = join " or ", @sort_cmd;
5350 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5352 while ( $record = get_record( $in ) ) {
5353 push @records, $record;
5356 @records = sort $sort_sub @records;
5358 if ( $options->{ "reverse" } )
5360 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5361 put_record( $records[ $i ], $out );
5366 for ( $i = 0; $i < scalar @records; $i++ ) {
5367 put_record( $records[ $i ], $out );
5373 sub script_count_vals
5375 # Martin A. Hansen, August 2007.
5377 # Count records in stream.
5379 my ( $in, # handle to in stream
5380 $out, # handle to out stream
5381 $options, # options hash
5386 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5388 $tmp_file = "$BP_TMP/count_cache.tmp";
5390 $fh_out = Maasha::Common::write_open( $tmp_file );
5395 while ( $record = get_record( $in ) )
5397 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5399 push @records, $record;
5401 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5403 map { put_record( $_, $fh_out ) } @records;
5410 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5421 $fh_in = Maasha::Common::read_open( $tmp_file );
5423 while ( $record = get_record( $fh_in ) )
5425 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5427 put_record( $record, $out );
5429 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5437 foreach $record ( @records )
5439 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5441 put_record( $record, $out );
5448 sub script_plot_histogram
5450 # Martin A. Hansen, September 2007.
5452 # Plot a simple histogram for a given key using GNU plot.
5454 my ( $in, # handle to in stream
5455 $out, # handle to out stream
5456 $options, # options hash
5461 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5463 $options->{ "title" } ||= "Histogram";
5464 $options->{ "sort" } ||= "num";
5466 while ( $record = get_record( $in ) )
5468 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5470 put_record( $record, $out ) if not $options->{ "no_stream" };
5473 if ( $options->{ "sort" } eq "num" ) {
5474 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5476 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5479 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5481 $fh = write_stream( $options->{ "data_out" } );
5483 print $fh "$_\n" foreach @{ $result };
5489 sub script_plot_lendist
5491 # Martin A. Hansen, August 2007.
5493 # Plot length distribution using GNU plot.
5495 my ( $in, # handle to in stream
5496 $out, # handle to out stream
5497 $options, # options hash
5502 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5504 $options->{ "title" } ||= "Length Distribution";
5506 while ( $record = get_record( $in ) )
5508 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5510 put_record( $record, $out ) if not $options->{ "no_stream" };
5513 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5515 for ( $i = 0; $i < $max; $i++ ) {
5516 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5519 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5521 $fh = write_stream( $options->{ "data_out" } );
5523 print $fh "$_\n" foreach @{ $result };
5529 sub script_plot_chrdist
5531 # Martin A. Hansen, August 2007.
5533 # Plot chromosome distribution using GNU plot.
5535 my ( $in, # handle to in stream
5536 $out, # handle to out stream
5537 $options, # options hash
5542 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5544 $options->{ "title" } ||= "Chromosome Distribution";
5546 while ( $record = get_record( $in ) )
5548 if ( $record->{ "CHR" } ) { # generic
5549 $data_hash{ $record->{ "CHR" } }++;
5550 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5551 $data_hash{ $record->{ "S_ID" } }++;
5552 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5553 $data_hash{ $record->{ "S_ID" } }++;
5554 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5555 $data_hash{ $record->{ "S_ID" } }++;
5558 put_record( $record, $out ) if not $options->{ "no_stream" };
5561 foreach $elem ( keys %data_hash )
5565 $sort_key =~ s/chr//i;
5567 $sort_key =~ s/^X(.*)/99$1/;
5568 $sort_key =~ s/^Y(.*)/99$1/;
5569 $sort_key =~ s/^Z(.*)/999$1/;
5570 $sort_key =~ s/^M(.*)/9999$1/;
5571 $sort_key =~ s/^U(.*)/99999$1/;
5573 $count = $sort_key =~ tr/_//;
5575 $sort_key =~ s/_.*/"999999" x $count/ex;
5577 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5580 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5582 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5584 $fh = write_stream( $options->{ "data_out" } );
5586 print $fh "$_\n" foreach @{ $result };
5592 sub script_plot_karyogram
5594 # Martin A. Hansen, August 2007.
5596 # Plot hits on karyogram.
5598 my ( $in, # handle to in stream
5599 $out, # handle to out stream
5600 $options, # options hash
5605 my ( %options, $record, @data, $fh, $result, %data_hash );
5607 $options->{ "genome" } ||= "human";
5608 $options->{ "feat_color" } ||= "black";
5610 while ( $record = get_record( $in ) )
5612 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5614 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5617 put_record( $record, $out ) if not $options->{ "no_stream" };
5620 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5622 $fh = write_stream( $options->{ "data_out" } );
5630 sub script_plot_matches
5632 # Martin A. Hansen, August 2007.
5634 # Plot matches in 2D generating a dotplot.
5636 my ( $in, # handle to in stream
5637 $out, # handle to out stream
5638 $options, # options hash
5643 my ( $record, @data, $fh, $result, %data_hash );
5645 $options->{ "direction" } ||= "both";
5647 while ( $record = get_record( $in ) )
5649 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5650 push @data, $record;
5653 put_record( $record, $out ) if not $options->{ "no_stream" };
5656 $options->{ "title" } ||= "plot_matches";
5657 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5658 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5660 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5662 $fh = write_stream( $options->{ "data_out" } );
5664 print $fh "$_\n" foreach @{ $result };
5670 sub script_length_vals
5672 # Martin A. Hansen, August 2007.
5674 # Determine the length of the value for given keys.
5676 my ( $in, # handle to in stream
5677 $out, # handle to out stream
5678 $options, # options hash
5683 my ( $record, $key );
5685 while ( $record = get_record( $in ) )
5687 foreach $key ( @{ $options->{ "keys" } } )
5689 if ( $record->{ $key } ) {
5690 $record->{ $key . "_LEN" } = length $record->{ $key };
5694 put_record( $record, $out );
5701 # Martin A. Hansen, August 2007.
5703 # Calculates the sums for values of given keys.
5705 my ( $in, # handle to in stream
5706 $out, # handle to out stream
5707 $options, # options hash
5712 my ( $record, $key, %sum_hash, $fh );
5714 while ( $record = get_record( $in ) )
5716 foreach $key ( @{ $options->{ "keys" } } )
5718 if ( $record->{ $key } ) {
5719 $sum_hash{ $key } += $record->{ $key };
5723 put_record( $record, $out ) if not $options->{ "no_stream" };
5726 $fh = write_stream( $options->{ "data_out" } );
5728 foreach $key ( @{ $options->{ "keys" } } ) {
5729 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5736 sub script_mean_vals
5738 # Martin A. Hansen, August 2007.
5740 # Calculate the mean of values of given keys.
5742 my ( $in, # handle to in stream
5743 $out, # handle to out stream
5744 $options, # options hash
5749 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5751 while ( $record = get_record( $in ) )
5753 foreach $key ( @{ $options->{ "keys" } } )
5755 if ( $record->{ $key } )
5757 $sum_hash{ $key } += $record->{ $key };
5758 $count_hash{ $key }++;
5762 put_record( $record, $out ) if not $options->{ "no_stream" };
5765 $fh = write_stream( $options->{ "data_out" } );
5767 foreach $key ( @{ $options->{ "keys" } } )
5769 if ( $count_hash{ $key } ) {
5770 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5775 put_record( { $key . "_MEAN" => $mean } , $fh );
5782 sub script_median_vals
5784 # Martin A. Hansen, March 2008.
5786 # Calculate the median values of given keys.
5788 my ( $in, # handle to in stream
5789 $out, # handle to out stream
5790 $options, # options hash
5795 my ( $record, $key, %median_hash, $median, $fh );
5797 while ( $record = get_record( $in ) )
5799 foreach $key ( @{ $options->{ "keys" } } ) {
5800 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5803 put_record( $record, $out ) if not $options->{ "no_stream" };
5806 $fh = write_stream( $options->{ "data_out" } );
5808 foreach $key ( @{ $options->{ "keys" } } )
5810 if ( $median_hash{ $key } ) {
5811 $median = Maasha::Calc::median( $median_hash{ $key } );
5816 put_record( { $key . "_MEDIAN" => $median } , $fh );
5825 # Martin A. Hansen, February 2008.
5827 # Determine the maximum values of given keys.
5829 my ( $in, # handle to in stream
5830 $out, # handle to out stream
5831 $options, # options hash
5836 my ( $record, $key, $fh, %max_hash, $max_record );
5838 while ( $record = get_record( $in ) )
5840 foreach $key ( @{ $options->{ "keys" } } )
5842 if ( $record->{ $key } )
5844 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5848 put_record( $record, $out ) if not $options->{ "no_stream" };
5851 $fh = write_stream( $options->{ "data_out" } );
5853 foreach $key ( @{ $options->{ "keys" } } )
5855 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5858 put_record( $max_record, $fh );
5866 # Martin A. Hansen, February 2008.
5868 # Determine the minimum values of given keys.
5870 my ( $in, # handle to in stream
5871 $out, # handle to out stream
5872 $options, # options hash
5877 my ( $record, $key, $fh, %min_hash, $min_record );
5879 while ( $record = get_record( $in ) )
5881 foreach $key ( @{ $options->{ "keys" } } )
5883 if ( defined $record->{ $key } )
5885 if ( exists $min_hash{ $key } ) {
5886 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5888 $min_hash{ $key } = $record->{ $key };
5893 put_record( $record, $out ) if not $options->{ "no_stream" };
5896 $fh = write_stream( $options->{ "data_out" } );
5898 foreach $key ( @{ $options->{ "keys" } } )
5900 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5903 put_record( $min_record, $fh );
5909 sub script_upload_to_ucsc
5911 # Martin A. Hansen, August 2007.
5913 # Calculate the mean of values of given keys.
5915 # This routine has developed into the most ugly hack. Do something!
5917 my ( $in, # handle to in stream
5918 $out, # handle to out stream
5919 $options, # options hash
5924 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5925 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones, $vals );
5927 $options->{ "short_label" } ||= $options->{ 'table' };
5928 $options->{ "long_label" } ||= $options->{ 'table' };
5929 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5930 $options->{ "priority" } ||= 1;
5931 $options->{ "visibility" } ||= "pack";
5932 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5933 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5935 $file = "$BP_TMP/ucsc_upload.tmp";
5943 if ( $options->{ 'wiggle' } )
5945 while ( $record = get_record( $in ) )
5947 put_record( $record, $out ) if not $options->{ "no_stream" };
5949 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5950 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5951 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5953 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5955 $fh_out = $fh_hash{ $record->{ "CHR" } };
5957 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5960 map { close $_ } keys %fh_hash;
5962 $fh_out = Maasha::Common::write_open( $file );
5964 foreach $chr ( sort keys %fh_hash )
5966 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5968 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5972 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5974 $chr = $entry->{ 'CHR' };
5975 $beg = $entry->{ 'CHR_BEG' };
5976 $end = $entry->{ 'CHR_END' };
5977 $q_id = $entry->{ 'Q_ID' };
5979 if ( $options->{ "score" } ) {
5980 $clones = $entry->{ 'SCORE' };
5981 } elsif ( $q_id =~ /_(\d+)$/ ) {
5991 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5996 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5997 $block->[ $i ] += $clones;
6000 $max = Maasha::Calc::max( $max, $end );
6009 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
6010 $block->[ $i ] += $clones;
6017 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
6019 unlink "$BP_TMP/$chr";
6028 $fh_out = Maasha::Common::write_open( $file );
6030 while ( $record = get_record( $in ) )
6032 put_record( $record, $out ) if not $options->{ "no_stream" };
6034 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6036 $vals = $record->{ "VALS" };
6039 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6040 print $fh_out "$vals\n";
6042 $format = "WIGGLE" if not $format;
6044 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6046 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6047 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6051 $format = "PSL" if not $format;
6053 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6055 # chrom chromStart chromEnd name score strand size secStr conf
6057 print $fh_out join ( "\t",
6059 $record->{ "CHR_BEG" },
6060 $record->{ "CHR_END" } + 1,
6061 $record->{ "Q_ID" },
6062 $record->{ "SCORE" },
6063 $record->{ "STRAND" },
6064 $record->{ "SIZE" },
6065 $record->{ "SEC_STRUCT" },
6066 $record->{ "CONF" },
6069 $format = "BED_SS" if not $format;
6071 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6073 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6075 $format = "BED" if not $format;
6076 $columns = $record->{ "BED_COLS" } if not $columns;
6078 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6080 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6082 $format = "BED" if not $format;
6083 $columns = 6 if not $columns;
6085 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6087 $record->{ "CHR" } = $record->{ "S_ID" };
6088 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6089 $record->{ "CHR_END" } = $record->{ "S_END" };
6090 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6092 $format = "BED" if not $format;
6093 $columns = 6 if not $columns;
6095 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6097 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6099 $record->{ "CHR" } = $record->{ "S_ID" };
6100 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6101 $record->{ "CHR_END" } = $record->{ "S_END" };
6102 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6103 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6105 $format = "BED" if not $format;
6106 $columns = 6 if not $columns;
6108 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6111 if ( $i == $options->{ "chunk_size" } )
6115 if ( $format eq "BED" ) {
6116 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6117 } elsif ( $format eq "PSL" ) {
6118 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6127 $fh_out = Maasha::Common::write_open( $file );
6136 if ( exists $options->{ "database" } and $options->{ "table" } )
6138 if ( $format eq "BED" )
6140 $type = "bed $columns";
6142 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6144 elsif ( $format eq "BED_SS" )
6146 $options->{ "sec_struct" } = 1;
6148 $type = "sec_struct";
6150 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6152 elsif ( $format eq "PSL" )
6156 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6158 elsif ( $format eq "WIGGLE" )
6160 $options->{ "visibility" } = "full";
6162 $wig_file = "$options->{ 'table' }.wig";
6163 $wib_file = "$options->{ 'table' }.wib";
6165 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6167 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6169 if ( $options->{ 'verbose' } ) {
6170 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6172 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6175 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6183 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6188 Maasha::UCSC::update_my_tracks( $options, $type );
6193 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6198 # Martin A. Hansen, July 2008.
6200 # Given a biopiece record converts it to a FASTA record.
6201 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6202 # tried in that order.
6204 my ( $record, # record
6209 my ( $seq_name, $seq );
6211 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6212 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6214 if ( defined $seq_name and defined $seq ) {
6215 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6224 # Martin A. Hansen, July 2007.
6226 # Opens a stream to STDIN or a file,
6228 my ( $path, # path - OPTIONAL
6231 # Returns filehandle.
6235 if ( not -t STDIN ) {
6236 $fh = Maasha::Common::read_stdin();
6237 } elsif ( not $path ) {
6238 # Maasha::Common::error( qq(no data stream) );
6240 $fh = Maasha::Common::read_open( $path );
6243 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6251 # Martin A. Hansen, August 2007.
6253 # Opens a stream to STDOUT or a file.
6255 my ( $path, # path - OPTIONAL
6256 $gzip, # compress data - OPTIONAL
6259 # Returns filehandle.
6264 $fh = Maasha::Common::write_open( $path, $gzip );
6266 $fh = Maasha::Common::write_stdout();
6275 # Martin A. Hansen, July 2007.
6277 # Reads one record at a time and converts that record
6278 # to a Perl data structure (a hash) which is returned.
6280 my ( $fh, # handle to stream
6285 my ( $block, @lines, $line, $key, $value, %record );
6287 local $/ = "\n---\n";
6293 return if not defined $block;
6295 @lines = split "\n", $block;
6297 foreach $line ( @lines )
6299 ( $key, $value ) = split ": ", $line, 2;
6301 $record{ $key } = $value;
6304 return wantarray ? %record : \%record;
6310 # Martin A. Hansen, July 2007.
6312 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6314 my ( $data, # data structure
6315 $fh, # file handle - OPTIONAL
6320 if ( scalar keys %{ $data } )
6324 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6329 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6340 # Martin A. Hansen, November 2007.
6342 # Extracts files from an explicit GetOpt::Long argument
6343 # allowing for the use of glob. E.g.
6344 # --data_in=test.fna
6345 # --data_in=test.fna,test2.fna
6347 # --data_in=test.fna,/dir/*.fna
6349 my ( $option, # option from GetOpt::Long
6354 my ( $elem, @files );
6356 foreach $elem ( split ",", $option )
6360 } elsif ( $elem =~ /\*/ ) {
6361 push @files, glob( $elem );
6365 return wantarray ? @files : \@files;
6371 # Martin A. Hansen, April 2008.
6373 # Removes temporary directory and exits gracefully.
6374 # This subroutine is meant to be run always as the last
6375 # thing even if a script is dies or is interrupted
6378 my ( $sig, # signal from the %SIG
6381 # print STDERR "signal->$sig<-\n";
6389 if ( $sig =~ /MAASHA_ERROR/ ) {
6390 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6391 } elsif ( $sig eq "INT" ) {
6392 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6393 } elsif ( $sig eq "TERM" ) {
6394 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6396 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6408 # Martin A. Hansen, July 2008.
6410 # Cleans out any unused temporary files and direcotries in BP_TMP.
6414 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6416 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6418 $curr_pid = Maasha::Common::get_processid();
6420 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6422 foreach $dir ( @dirs )
6424 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6430 if ( $user eq Maasha::Common::get_user() )
6432 if ( not Maasha::Common::process_running( $pid ) )
6434 # print STDERR "Removing stale dir: $dir\n";
6435 Maasha::Common::dir_remove( $dir );
6437 elsif ( $pid == $curr_pid )
6439 # print STDERR "Removing current dir: $dir\n";
6440 Maasha::Common::dir_remove( $dir );
6454 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<