1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
78 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
81 $SIG{ '__DIE__' } = \&sig_handler;
82 $SIG{ 'INT' } = \&sig_handler;
83 $SIG{ 'TERM' } = \&sig_handler;
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
89 my ( $script, $BP_TMP );
91 $script = Maasha::Common::get_scriptname();
92 $BP_TMP = Maasha::Common::get_tmpdir();
95 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
98 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
99 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
101 $log_global->autoflush( 1 );
102 $log_local->autoflush( 1 );
104 &log( $log_global, $script, \@ARGV );
105 &log( $log_local, $script, \@ARGV );
111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114 my $t0 = gettimeofday();
116 run_script( $script );
118 my $t1 = gettimeofday();
120 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128 # Martin A. Hansen, January 2008.
130 # Log messages to logfile.
132 my ( $fh, # filehandle to logfile
133 $script, # script name
134 $argv, # reference to @ARGV
139 my ( $time_stamp, $user );
141 $time_stamp = Maasha::Common::time_stamp();
143 $user = $ENV{ 'USER' };
145 $script = "biopieces" if $script eq "-e";
147 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
153 # Martin A. Hansen, August 2007.
155 # Run a specific script.
157 my ( $script, # script name
162 my ( $options, $in, $out );
164 $options = get_options( $script );
166 $options->{ "SCRIPT" } = $script;
168 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
169 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
172 $in = read_stream( $options->{ "stream_in" } );
173 $out = write_stream( $options->{ "stream_out" } );
175 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
176 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
177 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
178 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
179 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
181 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
182 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
183 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
184 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
185 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
186 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
187 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
188 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
189 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
190 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
191 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
192 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
193 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
194 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
195 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
196 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
198 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
199 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
200 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
201 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
202 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
203 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
204 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
205 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
206 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
208 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
209 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
213 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
214 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
216 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
217 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
218 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
219 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
220 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
221 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
222 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
223 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
224 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
225 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
226 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
227 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
228 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
229 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
230 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
231 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
232 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
233 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
234 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
235 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
236 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
237 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
238 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
239 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
240 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
241 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
242 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
243 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
244 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
245 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
246 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
247 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
248 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
249 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
250 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
251 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
252 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
253 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
254 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
255 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
256 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
257 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
258 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
259 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
260 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
261 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
262 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
263 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
264 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
265 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
266 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
268 close $in if defined $in;
275 # Martin A. Hansen, February 2008.
277 # Gets options from commandline and checks these vigerously.
279 my ( $script, # name of script
284 my ( %options, @options, $opt, @genomes, $real );
286 if ( $script eq "print_usage" )
292 elsif ( $script eq "read_fasta" )
299 elsif ( $script eq "read_tab" )
310 elsif ( $script eq "read_psl" )
317 elsif ( $script eq "read_bed" )
324 elsif ( $script eq "read_fixedstep" )
331 elsif ( $script eq "read_blast_tab" )
338 elsif ( $script eq "read_embl" )
348 elsif ( $script eq "read_stockholm" )
355 elsif ( $script eq "read_phastcons" )
366 elsif ( $script eq "read_soft" )
374 elsif ( $script eq "read_gff" )
381 elsif ( $script eq "read_2bit" )
389 elsif ( $script eq "read_solexa" )
397 elsif ( $script eq "read_solid" )
405 elsif ( $script eq "read_mysql" )
414 elsif ( $script eq "format_genome" )
423 elsif ( $script eq "length_seq" )
430 elsif ( $script eq "oligo_freq" )
437 elsif ( $script eq "create_weight_matrix" )
443 elsif ( $script eq "transliterate_seq" )
451 elsif ( $script eq "transliterate_vals" )
460 elsif ( $script eq "translate_seq" )
466 elsif ( $script eq "extract_seq" )
474 elsif ( $script eq "get_genome_seq" )
486 elsif ( $script eq "get_genome_align" )
497 elsif ( $script eq "get_genome_phastcons" )
508 elsif ( $script eq "split_seq" )
515 elsif ( $script eq "split_bed" )
522 elsif ( $script eq "tile_seq" )
529 elsif ( $script eq "invert_align" )
535 elsif ( $script eq "patscan_seq" )
546 elsif ( $script eq "create_blast_db" )
553 elsif ( $script eq "blast_seq" )
565 elsif ( $script eq "blat_seq" )
577 elsif ( $script eq "soap_seq" )
588 elsif ( $script eq "match_seq" )
595 elsif ( $script eq "create_vmatch_index" )
603 elsif ( $script eq "vmatch_seq" )
614 elsif ( $script eq "write_fasta" )
623 elsif ( $script eq "write_align" )
633 elsif ( $script eq "write_blast" )
642 elsif ( $script eq "write_tab" )
654 elsif ( $script eq "write_bed" )
662 elsif ( $script eq "write_psl" )
670 elsif ( $script eq "write_fixedstep" )
678 elsif ( $script eq "write_2bit" )
686 elsif ( $script eq "write_solid" )
695 elsif ( $script eq "plot_seqlogo" )
702 elsif ( $script eq "plot_phastcons_profiles" )
717 elsif ( $script eq "analyze_vals" )
724 elsif ( $script eq "head_records" )
730 elsif ( $script eq "remove_keys" )
737 elsif ( $script eq "remove_adaptor" )
746 elsif ( $script eq "rename_keys" )
752 elsif ( $script eq "uniq_vals" )
759 elsif ( $script eq "merge_vals" )
766 elsif ( $script eq "merge_records" )
773 elsif ( $script eq "grab" )
788 elsif ( $script eq "compute" )
794 elsif ( $script eq "add_ident" )
801 elsif ( $script eq "count_records" )
808 elsif ( $script eq "random_records" )
814 elsif ( $script eq "sort_records" )
821 elsif ( $script eq "count_vals" )
827 elsif ( $script eq "plot_histogram" )
840 elsif ( $script eq "plot_lendist" )
852 elsif ( $script eq "plot_chrdist" )
863 elsif ( $script eq "plot_karyogram" )
872 elsif ( $script eq "plot_matches" )
884 elsif ( $script eq "length_vals" )
890 elsif ( $script eq "sum_vals" )
898 elsif ( $script eq "mean_vals" )
906 elsif ( $script eq "median_vals" )
914 elsif ( $script eq "max_vals" )
922 elsif ( $script eq "min_vals" )
930 elsif ( $script eq "upload_to_ucsc" )
957 # print STDERR Dumper( \@options );
964 # print STDERR Dumper( \%options );
966 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
967 return wantarray ? %options : \%options;
970 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
971 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
972 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
973 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
974 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
975 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
976 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
977 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
978 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
980 # ---- check arguments ----
982 if ( $options{ 'data_in' } )
984 $options{ "files" } = getopt_files( $options{ 'data_in' } );
986 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
989 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
991 # print STDERR Dumper( \%options );
993 $real = "beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
995 foreach $opt ( keys %options )
997 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
999 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
1001 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
1003 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
1005 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
1007 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
1009 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
1011 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
1013 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1015 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1017 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1019 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1021 elsif ( $opt eq "genome" and $script ne "format_genome" )
1023 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1024 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1026 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1027 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1030 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
1032 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1034 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
1036 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
1038 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1040 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1044 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1045 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1046 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1047 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1048 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1049 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1050 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1051 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1052 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /format_genome|get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
1053 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1054 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
1056 if ( $script eq "upload_to_ucsc" )
1058 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1059 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1062 return wantarray ? %options : \%options;
1066 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1069 sub script_print_usage
1071 # Martin A. Hansen, January 2008.
1073 # Retrieves usage information from file and
1074 # prints this nicely formatted.
1076 my ( $in, # handle to in stream
1077 $out, # handle to out stream
1078 $options, # options hash
1083 my ( $file, $wiki, $lines );
1085 if ( $options->{ 'data_in' } ) {
1086 $file = $options->{ 'data_in' };
1088 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1091 $wiki = Maasha::Gwiki::gwiki_read( $file );
1093 if ( not $options->{ "help" } ) {
1094 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1097 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1099 print STDERR "$_\n" foreach @{ $lines };
1105 sub script_list_biopieces
1107 # Martin A. Hansen, January 2008.
1109 # Prints the synopsis from the usage for each of the biopieces.
1111 my ( $in, # handle to in stream
1112 $out, # handle to out stream
1113 $options, # options hash
1118 my ( @files, $file, $wiki, $program, $synopsis );
1120 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1122 foreach $file ( sort @files )
1124 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1128 $wiki = Maasha::Gwiki::gwiki_read( $file );
1130 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1131 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1133 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1134 $synopsis =~ s/!(\w)/$1/g;
1136 printf( "%-30s%s\n", $program, $synopsis );
1144 sub script_list_genomes
1146 # Martin A. Hansen, January 2008.
1148 # Prints the synopsis from the usage for each of the biopieces.
1150 my ( $in, # handle to in stream
1151 $out, # handle to out stream
1152 $options, # options hash
1157 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1159 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1161 foreach $genome ( @genomes )
1163 next if $genome =~ /\.$/;
1165 @formats = Maasha::Common::ls_dirs( $genome );
1167 foreach $format ( @formats )
1169 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1171 $hash{ $1 }{ $2 } = 1;
1180 map { push @row, $_ } sort keys %found;
1182 print join( "\t", @row ), "\n";
1184 foreach $genome ( sort keys %hash )
1188 foreach $format ( sort keys %found )
1190 if ( exists $hash{ $genome }{ $format } ) {
1197 print join( "\t", @row ), "\n";
1202 sub script_read_fasta
1204 # Martin A. Hansen, August 2007.
1206 # Read sequences from FASTA file.
1208 my ( $in, # handle to in stream
1209 $out, # handle to out stream
1210 $options, # options hash
1215 my ( $record, $file, $data_in, $entry, $num );
1217 while ( $record = get_record( $in ) ) {
1218 put_record( $record, $out );
1223 foreach $file ( @{ $options->{ "files" } } )
1225 $data_in = Maasha::Common::read_open( $file );
1227 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1229 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1232 SEQ_NAME => $entry->[ SEQ_NAME ],
1233 SEQ => $entry->[ SEQ ],
1234 SEQ_LEN => length $entry->[ SEQ ],
1237 put_record( $record, $out );
1240 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1250 close $data_in if $data_in;
1256 # Martin A. Hansen, August 2007.
1258 # Read table or table columns from stream or file.
1260 my ( $in, # handle to in stream
1261 $out, # handle to out stream
1262 $options, # options hash
1267 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1269 $options->{ 'delimit' } ||= '\s+';
1271 while ( $record = get_record( $in ) ) {
1272 put_record( $record, $out );
1275 $skip = $options->{ 'skip' } ||= 0;
1278 foreach $file ( @{ $options->{ "files" } } )
1280 $data_in = Maasha::Common::read_open( $file );
1282 while ( $line = <$data_in> )
1290 next if $line =~ /^#|^$/;
1297 @fields = split /$options->{'delimit'}/, $line;
1299 if ( $options->{ "cols" } ) {
1300 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1305 for ( $i = 0; $i < @fields2; $i++ )
1307 if ( $options->{ "keys" }->[ $i ] ) {
1308 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1310 $record->{ "V" . $i } = $fields2[ $i ];
1314 put_record( $record, $out );
1316 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1326 close $data_in if $data_in;
1332 # Martin A. Hansen, August 2007.
1334 # Read psl table from stream or file.
1336 my ( $in, # handle to in stream
1337 $out, # handle to out stream
1338 $options, # options hash
1343 my ( $record, $file, $data_in, $num );
1345 while ( $record = get_record( $in ) ) {
1346 put_record( $record, $out );
1351 foreach $file ( @{ $options->{ "files" } } )
1353 $data_in = Maasha::Common::read_open( $file );
1355 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1357 put_record( $record, $out );
1359 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1371 # Martin A. Hansen, August 2007.
1373 # Read bed table from stream or file.
1375 my ( $in, # handle to in stream
1376 $out, # handle to out stream
1377 $options, # options hash
1382 my ( $file, $record, $entry, $data_in, $num );
1384 while ( $record = get_record( $in ) ) {
1385 put_record( $record, $out );
1390 foreach $file ( @{ $options->{ "files" } } )
1392 $data_in = Maasha::Common::read_open( $file );
1394 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1396 put_record( $entry, $out );
1398 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1408 close $data_in if $data_in;
1412 sub script_read_fixedstep
1414 # Martin A. Hansen, Juli 2008.
1416 # Read fixedStep wiggle format from stream or file.
1418 my ( $in, # handle to in stream
1419 $out, # handle to out stream
1420 $options, # options hash
1425 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1427 while ( $record = get_record( $in ) ) {
1428 put_record( $record, $out );
1433 foreach $file ( @{ $options->{ "files" } } )
1435 $data_in = Maasha::Common::read_open( $file );
1437 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1439 $head = shift @{ $entry };
1441 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1443 $record->{ "REC_TYPE" } = "fixed_step";
1444 $record->{ "CHR" } = $1;
1445 $record->{ "CHR_BEG" } = $2;
1446 $record->{ "STEP" } = $3;
1447 $record->{ "VALS" } = join ",", @{ $entry };
1450 put_record( $record, $out );
1452 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1462 close $data_in if $data_in;
1466 sub script_read_blast_tab
1468 # Martin A. Hansen, September 2007.
1470 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1472 my ( $in, # handle to in stream
1473 $out, # handle to out stream
1474 $options, # options hash
1479 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1481 while ( $record = get_record( $in ) ) {
1482 put_record( $record, $out );
1487 foreach $file ( @{ $options->{ "files" } } )
1489 $data_in = Maasha::Common::read_open( $file );
1491 while ( $line = <$data_in> )
1495 next if $line =~ /^#/;
1497 @fields = split /\t/, $line;
1499 $record->{ "REC_TYPE" } = "BLAST";
1500 $record->{ "Q_ID" } = $fields[ 0 ];
1501 $record->{ "S_ID" } = $fields[ 1 ];
1502 $record->{ "IDENT" } = $fields[ 2 ];
1503 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1504 $record->{ "MISMATCHES" } = $fields[ 4 ];
1505 $record->{ "GAPS" } = $fields[ 5 ];
1506 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1507 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1508 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1509 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1510 $record->{ "E_VAL" } = $fields[ 10 ];
1511 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1513 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1515 $record->{ "STRAND" } = '-';
1517 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1521 $record->{ "STRAND" } = '+';
1524 put_record( $record, $out );
1526 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1536 close $data_in if $data_in;
1540 sub script_read_embl
1542 # Martin A. Hansen, August 2007.
1546 my ( $in, # handle to in stream
1547 $out, # handle to out stream
1548 $options, # options hash
1553 my ( %options2, $file, $data_in, $num, $entry, $record );
1555 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1556 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1557 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1559 while ( $record = get_record( $in ) ) {
1560 put_record( $record, $out );
1565 foreach $file ( @{ $options->{ "files" } } )
1567 $data_in = Maasha::Common::read_open( $file );
1569 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1571 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1573 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1575 $record_copy = dclone $record;
1577 delete $record_copy->{ "FT" };
1579 put_record( $record_copy, $out );
1581 delete $record_copy->{ "SEQ" };
1583 foreach $feat ( keys %{ $record->{ "FT" } } )
1585 $record_copy->{ "FEAT_TYPE" } = $feat;
1587 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1589 foreach $qual ( keys %{ $feat2 } )
1591 $qual_val = join "; ", @{ $feat2->{ $qual } };
1596 $record_copy->{ $qual } = $qual_val;
1599 put_record( $record_copy, $out );
1603 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1613 close $data_in if $data_in;
1617 sub script_read_stockholm
1619 # Martin A. Hansen, August 2007.
1621 # Read Stockholm format.
1623 my ( $in, # handle to in stream
1624 $out, # handle to out stream
1625 $options, # options hash
1630 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1632 while ( $record = get_record( $in ) ) {
1633 put_record( $record, $out );
1638 foreach $file ( @{ $options->{ "files" } } )
1640 $data_in = Maasha::Common::read_open( $file );
1642 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1644 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1648 foreach $key ( keys %{ $record->{ "GF" } } ) {
1649 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1652 $record_anno->{ "ALIGN" } = $num;
1654 put_record( $record_anno, $out );
1656 foreach $seq ( @{ $record->{ "ALIGN" } } )
1658 undef $record_align;
1661 SEQ_NAME => $seq->[ 0 ],
1665 put_record( $record_align, $out );
1668 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1678 close $data_in if $data_in;
1682 sub script_read_phastcons
1684 # Martin A. Hansen, December 2007.
1686 # Read PhastCons format.
1688 my ( $in, # handle to in stream
1689 $out, # handle to out stream
1690 $options, # options hash
1695 my ( $data_in, $file, $num, $entry, @records, $record );
1697 $options->{ "min" } ||= 10;
1698 $options->{ "dist" } ||= 25;
1699 $options->{ "threshold" } ||= 0.8;
1700 $options->{ "gap" } ||= 5;
1702 while ( $record = get_record( $in ) ) {
1703 put_record( $record, $out );
1708 foreach $file ( @{ $options->{ "files" } } )
1710 $data_in = Maasha::Common::read_open( $file );
1712 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1714 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1716 foreach $record ( @records )
1718 $record->{ "REC_TYPE" } = "BED";
1719 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1721 put_record( $record, $out );
1723 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1734 close $data_in if $data_in;
1738 sub script_read_soft
1740 # Martin A. Hansen, December 2007.
1743 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1745 my ( $in, # handle to in stream
1746 $out, # handle to out stream
1747 $options, # options hash
1752 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1754 while ( $record = get_record( $in ) ) {
1755 put_record( $record, $out );
1760 foreach $file ( @{ $options->{ "files" } } )
1762 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1764 $soft_index = Maasha::NCBI::soft_index_file( $file );
1766 $fh = Maasha::Common::read_open( $file );
1768 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1770 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1772 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1774 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1776 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1778 foreach $sample ( @samples )
1781 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1783 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1785 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1787 foreach $record ( @{ $records } )
1789 put_record( $record, $out );
1791 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1796 $old_end = $sample->{ "LINE_END" };
1804 close $data_in if $data_in;
1811 # Martin A. Hansen, February 2008.
1814 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1816 my ( $in, # handle to in stream
1817 $out, # handle to out stream
1818 $options, # options hash
1823 my ( $data_in, $file, $fh, $num, $record, $entry );
1825 while ( $record = get_record( $in ) ) {
1826 put_record( $record, $out );
1831 foreach $file ( @{ $options->{ "files" } } )
1833 $fh = Maasha::Common::read_open( $file );
1835 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1837 put_record( $entry, $out );
1839 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1849 close $data_in if $data_in;
1853 sub script_read_2bit
1855 # Martin A. Hansen, March 2008.
1857 # Read sequences from 2bit file.
1859 my ( $in, # handle to in stream
1860 $out, # handle to out stream
1861 $options, # options hash
1866 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1868 $mask = 1 if not $options->{ "no_mask" };
1870 while ( $record = get_record( $in ) ) {
1871 put_record( $record, $out );
1876 foreach $file ( @{ $options->{ "files" } } )
1878 $data_in = Maasha::Common::read_open( $file );
1880 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1882 foreach $line ( @{ $toc } )
1884 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1885 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1886 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1888 put_record( $record, $out );
1890 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1900 close $data_in if $data_in;
1904 sub script_read_solexa
1906 # Martin A. Hansen, March 2008.
1908 # Read Solexa sequence reads from file.
1910 my ( $in, # handle to in stream
1911 $out, # handle to out stream
1912 $options, # options hash
1917 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1919 $options->{ "quality" } ||= 20;
1921 while ( $record = get_record( $in ) ) {
1922 put_record( $record, $out );
1927 foreach $file ( @{ $options->{ "files" } } )
1929 $data_in = Maasha::Common::read_open( $file );
1931 while ( $entry = Maasha::Solexa::solexa_get_entry( $data_in ) )
1933 @seqs = split //, $entry->[ SEQ ];
1934 @scores = split /:/, $entry->[ SCORE ];
1936 for ( $i = 0; $i < @scores; $i++ ) {
1937 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1940 $record->{ "SEQ_NAME" } = $entry->[ SEQ_NAME ];
1941 $record->{ "SEQ" } = join "", @seqs;
1942 $record->{ "SEQ_LEN" } = scalar @seqs;
1943 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1945 put_record( $record, $out );
1947 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1957 close $data_in if $data_in;
1961 sub script_read_solid
1963 # Martin A. Hansen, April 2008.
1965 # Read Solid sequence from file.
1967 my ( $in, # handle to in stream
1968 $out, # handle to out stream
1969 $options, # options hash
1974 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1976 $options->{ "quality" } ||= 15;
1978 while ( $record = get_record( $in ) ) {
1979 put_record( $record, $out );
1984 foreach $file ( @{ $options->{ "files" } } )
1986 $data_in = Maasha::Common::read_open( $file );
1988 while ( $line = <$data_in> )
1992 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1994 @scores = split /,/, $seq_qual;
1995 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1997 for ( $i = 0; $i < @seqs; $i++ ) {
1998 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
2002 SEQ_NAME => $seq_name,
2004 SEQ_QUAL => $seq_qual,
2005 SEQ_LEN => length $seq_cs,
2006 SEQ => join( "", @seqs ),
2007 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
2010 put_record( $record, $out );
2012 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2022 close $data_in if $data_in;
2026 sub script_read_mysql
2028 # Martin A. Hansen, May 2008.
2030 # Read a MySQL query into stream.
2032 my ( $in, # handle to in stream
2033 $out, # handle to out stream
2034 $options, # options hash
2039 my ( $record, $dbh, $results );
2041 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2042 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2044 while ( $record = get_record( $in ) ) {
2045 put_record( $record, $out );
2048 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2050 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2052 Maasha::SQL::disconnect( $dbh );
2054 map { put_record( $_ ) } @{ $results };
2058 sub script_format_genome
2060 # Martin A. Hansen, Juli 2008.
2062 # Format a genome to speficed formats.
2064 my ( $in, # handle to in stream
2065 $out, # handle to out stream
2066 $options, # options hash
2071 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2073 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2074 $genome = $options->{ 'genome' };
2076 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2077 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2078 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2080 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2082 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2084 $fasta_dir = "$dir/genomes/$genome/fasta";
2088 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2090 $fasta_dir = "$dir/genomes/$genome/fasta";
2092 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2095 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2097 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2099 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2101 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2104 while ( $record = get_record( $in ) )
2106 if ( $fh_out and $entry = record2fasta( $record ) )
2108 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2110 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2112 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2114 $vals = $record->{ 'VALS' };
2118 print $fh_out "$vals\n";
2121 put_record( $record, $out ) if not $options->{ "no_stream" };
2124 foreach $format ( @{ $options->{ 'formats' } } )
2126 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2127 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2128 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2129 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2130 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2133 close $fh_out if $fh_out;
2137 sub script_length_seq
2139 # Martin A. Hansen, August 2007.
2141 # Determine the length of sequences in stream.
2143 my ( $in, # handle to in stream
2144 $out, # handle to out stream
2145 $options, # options hash
2150 my ( $record, $total );
2152 while ( $record = get_record( $in ) )
2154 if ( $record->{ "SEQ" } )
2156 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2157 $total += $record->{ "SEQ_LEN" };
2160 put_record( $record, $out ) if not $options->{ "no_stream" };
2163 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2167 sub script_uppercase_seq
2169 # Martin A. Hansen, August 2007.
2171 # Uppercases sequences in stream.
2173 my ( $in, # handle to in stream
2174 $out, # handle to out stream
2181 while ( $record = get_record( $in ) )
2183 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2185 put_record( $record, $out );
2190 sub script_shuffle_seq
2192 # Martin A. Hansen, December 2007.
2194 # Shuffle sequences in stream.
2196 my ( $in, # handle to in stream
2197 $out, # handle to out stream
2204 while ( $record = get_record( $in ) )
2206 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2208 put_record( $record, $out );
2213 sub script_analyze_seq
2215 # Martin A. Hansen, August 2007.
2217 # Analyze sequence composition of sequences in stream.
2219 my ( $in, # handle to in stream
2220 $out, # handle to out stream
2225 my ( $record, $analysis );
2227 while ( $record = get_record( $in ) )
2229 if ( $record->{ "SEQ" } )
2231 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2233 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2236 put_record( $record, $out );
2241 sub script_analyze_tags
2243 # Martin A. Hansen, August 2008.
2245 # Analyze sequence tags in stream.
2247 my ( $in, # handle to in stream
2248 $out, # handle to out stream
2253 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2255 while ( $record = get_record( $in ) )
2257 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2259 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2263 $len_hash{ length( $record->{ "SEQ" } ) }++;
2264 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2267 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2269 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2273 $len_hash{ $record->{ "BED_LEN" } }++;
2274 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2279 foreach $key ( sort { $a <=> $b } keys %len_hash )
2281 $tag_record->{ "TAG_LEN" } = $key;
2282 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2283 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2285 put_record( $tag_record, $out );
2290 sub script_complexity_seq
2292 # Martin A. Hansen, May 2008.
2294 # Generates an index calculated as the most common di-residue over
2295 # the sequence length for all sequences in stream.
2297 my ( $in, # handle to in stream
2298 $out, # handle to out stream
2303 my ( $record, $index );
2305 while ( $record = get_record( $in ) )
2307 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2309 put_record( $record, $out );
2314 sub script_oligo_freq
2316 # Martin A. Hansen, August 2007.
2318 # Determine the length of sequences in stream.
2320 my ( $in, # handle to in stream
2321 $out, # handle to out stream
2322 $options, # options hash
2327 my ( $record, %oligos, @freq_table );
2329 $options->{ "word_size" } ||= 7;
2331 while ( $record = get_record( $in ) )
2333 if ( $record->{ "SEQ" } )
2335 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2337 if ( not $options->{ "all" } )
2339 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2341 map { put_record( $_, $out ) } @freq_table;
2347 put_record( $record, $out );
2350 if ( $options->{ "all" } )
2352 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2354 map { put_record( $_, $out ) } @freq_table;
2359 sub script_create_weight_matrix
2361 # Martin A. Hansen, August 2007.
2363 # Creates a weight matrix from an alignmnet.
2365 my ( $in, # handle to in stream
2366 $out, # handle to out stream
2367 $options, # options hash
2372 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2376 while ( $record = get_record( $in ) )
2378 if ( $record->{ "SEQ" } )
2380 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2382 $res = substr $record->{ "SEQ" }, $i, 1;
2384 $freq_hash{ $i }{ $res }++;
2385 $res_hash{ $res } = 1;
2392 put_record( $record, $out );
2396 foreach $res ( sort keys %res_hash )
2400 $record->{ "V0" } = $res;
2402 for ( $i = 0; $i < keys %freq_hash; $i++ )
2404 $freq = $freq_hash{ $i }{ $res } || 0;
2406 if ( $options->{ "percent" } ) {
2407 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2410 $record->{ "V" . ( $i + 1 ) } = $freq;
2413 put_record( $record, $out );
2418 sub script_calc_bit_scores
2420 # Martin A. Hansen, March 2007.
2422 # Calculates the bit scores for each position from an alignmnet in the stream.
2424 my ( $in, # handle to in stream
2425 $out, # handle to out stream
2430 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2434 while ( $record = get_record( $in ) )
2436 if ( $record->{ "SEQ" } )
2438 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2440 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2442 $res = substr $record->{ "SEQ" }, $i, 1;
2444 next if $res =~ /-|_|~|\./;
2446 $freq_hash{ $i }{ $res }++;
2453 put_record( $record, $out );
2459 if ( $type eq "protein" ) {
2465 for ( $i = 0; $i < keys %freq_hash; $i++ )
2467 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2469 $bit_diff = $bit_max - $bit_height;
2471 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2474 put_record( $record, $out );
2478 sub script_reverse_seq
2480 # Martin A. Hansen, August 2007.
2482 # Reverse sequence in record.
2484 my ( $in, # handle to in stream
2485 $out, # handle to out stream
2492 while ( $record = get_record( $in ) )
2494 if ( $record->{ "SEQ" } ) {
2495 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2498 put_record( $record, $out );
2503 sub script_complement_seq
2505 # Martin A. Hansen, August 2007.
2507 # Complement sequence in record.
2509 my ( $in, # handle to in stream
2510 $out, # handle to out stream
2515 my ( $record, $type );
2517 while ( $record = get_record( $in ) )
2519 if ( $record->{ "SEQ" } )
2522 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2525 if ( $type eq "rna" ) {
2526 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2527 } elsif ( $type eq "dna" ) {
2528 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2532 put_record( $record, $out );
2537 sub script_remove_indels
2539 # Martin A. Hansen, August 2007.
2541 # Remove indels from sequences in stream.
2543 my ( $in, # handle to in stream
2544 $out, # handle to out stream
2551 while ( $record = get_record( $in ) )
2553 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2555 put_record( $record, $out );
2560 sub script_transliterate_seq
2562 # Martin A. Hansen, August 2007.
2564 # Transliterate chars from sequence in record.
2566 my ( $in, # handle to in stream
2567 $out, # handle to out stream
2568 $options, # options hash
2573 my ( $record, $search, $replace, $delete );
2575 $search = $options->{ "search" } || "";
2576 $replace = $options->{ "replace" } || "";
2577 $delete = $options->{ "delete" } || "";
2579 while ( $record = get_record( $in ) )
2581 if ( $record->{ "SEQ" } )
2583 if ( $search and $replace ) {
2584 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2585 } elsif ( $delete ) {
2586 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2590 put_record( $record, $out );
2595 sub script_transliterate_vals
2597 # Martin A. Hansen, April 2008.
2599 # Transliterate chars from values in record.
2601 my ( $in, # handle to in stream
2602 $out, # handle to out stream
2603 $options, # options hash
2608 my ( $record, $search, $replace, $delete, $key );
2610 $search = $options->{ "search" } || "";
2611 $replace = $options->{ "replace" } || "";
2612 $delete = $options->{ "delete" } || "";
2614 while ( $record = get_record( $in ) )
2616 foreach $key ( @{ $options->{ "keys" } } )
2618 if ( exists $record->{ $key } )
2620 if ( $search and $replace ) {
2621 eval "\$record->{ $key } =~ tr/$search/$replace/";
2622 } elsif ( $delete ) {
2623 eval "\$record->{ $key } =~ tr/$delete//d";
2628 put_record( $record, $out );
2633 sub script_translate_seq
2635 # Martin A. Hansen, February 2008.
2637 # Translate DNA sequence into protein sequence.
2639 my ( $in, # handle to in stream
2640 $out, # handle to out stream
2641 $options, # options hash
2646 my ( $record, $frame, %new_record );
2648 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2650 while ( $record = get_record( $in ) )
2652 if ( $record->{ "SEQ" } )
2654 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2656 foreach $frame ( @{ $options->{ "frames" } } )
2658 %new_record = %{ $record };
2660 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2661 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2662 $new_record{ "FRAME" } = $frame;
2664 put_record( \%new_record, $out );
2670 put_record( $record, $out );
2676 sub script_extract_seq
2678 # Martin A. Hansen, August 2007.
2680 # Extract subsequences from sequences in record.
2682 my ( $in, # handle to in stream
2683 $out, # handle to out stream
2684 $options, # options hash
2689 my ( $beg, $end, $len, $record );
2691 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2694 $beg = $options->{ "beg" } - 1; # correcting for start offset
2697 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2699 } elsif ( defined $options->{ "end" } ) {
2700 $end = $options->{ "end" } - 1; # correcting for start offset
2703 $len = $options->{ "len" };
2705 # print "beg->$beg, end->$end, len->$len\n";
2707 while ( $record = get_record( $in ) )
2709 if ( $record->{ "SEQ" } )
2711 if ( defined $beg and defined $end )
2713 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2714 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2716 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2719 elsif ( defined $beg and defined $len )
2721 if ( $len > length $record->{ "SEQ" } ) {
2722 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2724 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2727 elsif ( defined $beg )
2729 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2733 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2735 put_record( $record, $out );
2740 sub script_get_genome_seq
2742 # Martin A. Hansen, December 2007.
2744 # Gets a subsequence from a genome.
2746 my ( $in, # handle to in stream
2747 $out, # handle to out stream
2748 $options, # options hash
2753 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2755 $options->{ "flank" } ||= 0;
2757 if ( $options->{ "genome" } )
2759 $genome = $options->{ "genome" };
2761 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2762 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2764 $fh = Maasha::Common::read_open( $genome_file );
2765 $index = Maasha::Fasta::index_retrieve( $index_file );
2767 shift @{ $index }; # Get rid of the file size info
2769 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2771 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2773 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2775 $beg = $index_beg + $options->{ "beg" } - 1;
2777 if ( $options->{ "len" } ) {
2778 $len = $options->{ "len" };
2779 } elsif ( $options->{ "end" } ) {
2780 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2783 $beg -= $options->{ "flank" };
2784 $len += 2 * $options->{ "flank" };
2786 if ( $beg <= $index_beg )
2788 $len -= $index_beg - $beg;
2792 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2794 next if $beg > $index_beg + $index_len;
2796 $record->{ "CHR" } = $options->{ "chr" };
2797 $record->{ "CHR_BEG" } = $beg - $index_beg;
2798 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2800 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2801 $record->{ "SEQ_LEN" } = $len;
2803 put_record( $record, $out );
2807 while ( $record = get_record( $in ) )
2809 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2811 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2813 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2815 $beg = $record->{ "CHR_BEG" } + $index_beg;
2816 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2818 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2820 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2822 $beg = $record->{ "S_BEG" } + $index_beg;
2823 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2825 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2827 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2829 $beg = $record->{ "S_BEG" } + $index_beg;
2830 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2833 $beg -= $options->{ "flank" };
2834 $len += 2 * $options->{ "flank" };
2836 if ( $beg <= $index_beg )
2838 $len -= $index_beg - $beg;
2842 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2844 next if $beg > $index_beg + $index_len;
2846 $record->{ "CHR_BEG" } = $beg - $index_beg;
2847 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2849 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2851 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2853 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2854 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2857 if ( $options->{ "mask" } )
2859 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2861 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2863 @begs = split ",", $record->{ "Q_BEGS" };
2864 @lens = split ",", $record->{ "BLOCKSIZES" };
2866 for ( $i = 0; $i < @begs; $i++ ) {
2867 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2873 put_record( $record, $out );
2880 sub script_get_genome_align
2882 # Martin A. Hansen, April 2008.
2884 # Gets a subalignment from a multiple genome alignment.
2886 my ( $in, # handle to in stream
2887 $out, # handle to out stream
2888 $options, # options hash
2893 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2895 $options->{ "strand" } ||= "+";
2899 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2901 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2903 $beg = $options->{ "beg" } - 1;
2905 if ( $options->{ "end" } ) {
2906 $end = $options->{ "end" };
2907 } elsif ( $options->{ "len" } ) {
2908 $end = $beg + $options->{ "len" };
2911 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2913 foreach $entry ( @{ $align } )
2915 $entry->{ "CHR" } = $record->{ "CHR" };
2916 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2917 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2918 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2919 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2920 $entry->{ "SCORE" } = $record->{ "SCORE" };
2922 put_record( $entry, $out );
2926 while ( $record = get_record( $in ) )
2928 if ( $record->{ "REC_TYPE" } eq "BED" )
2930 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2932 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2934 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2936 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2938 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2941 foreach $entry ( @{ $align } )
2943 $entry->{ "CHR" } = $record->{ "CHR" };
2944 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2945 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2946 $entry->{ "STRAND" } = $record->{ "STRAND" };
2947 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2948 $entry->{ "SCORE" } = $record->{ "SCORE" };
2950 put_record( $entry, $out );
2958 sub script_get_genome_phastcons
2960 # Martin A. Hansen, February 2008.
2962 # Get phastcons scores from genome intervals.
2964 my ( $in, # handle to in stream
2965 $out, # handle to out stream
2966 $options, # options hash
2971 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2973 $options->{ "flank" } ||= 0;
2975 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2976 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2978 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2979 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2981 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2983 $options->{ "beg" } -= 1; # request is 1-based
2984 $options->{ "end" } -= 1; # request is 1-based
2986 if ( $options->{ "len" } ) {
2987 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2990 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2992 $record->{ "CHR" } = $options->{ "chr" };
2993 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2994 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2996 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2997 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2999 put_record( $record, $out );
3002 while ( $record = get_record( $in ) )
3004 if ( $record->{ "REC_TYPE" } eq "BED" )
3006 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
3008 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
3010 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3012 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
3014 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
3017 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
3018 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3020 put_record( $record, $out );
3023 close $fh_phastcons if $fh_phastcons;
3029 # Martin A. Hansen, December 2007.
3031 # Folds sequences in stream into secondary structures.
3033 my ( $in, # handle to in stream
3034 $out, # handle to out stream
3039 my ( $record, $type, $struct, $index );
3041 while ( $record = get_record( $in ) )
3043 if ( $record->{ "SEQ" } )
3046 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3049 if ( $type ne "protein" )
3051 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3052 $record->{ "SEC_STRUCT" } = $struct;
3053 $record->{ "FREE_ENERGY" } = $index;
3054 $record->{ "SCORE" } = abs int $index;
3055 $record->{ "SIZE" } = length $struct;
3056 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3060 put_record( $record, $out );
3065 sub script_split_seq
3067 # Martin A. Hansen, August 2007.
3069 # Split a sequence in stream into words.
3071 my ( $in, # handle to in stream
3072 $out, # handle to out stream
3073 $options, # options hash
3078 my ( $record, $new_record, $i, $subseq, %lookup );
3080 $options->{ "word_size" } ||= 7;
3082 while ( $record = get_record( $in ) )
3084 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3086 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3088 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3090 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3092 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3093 $new_record->{ "SEQ" } = $subseq;
3095 put_record( $new_record, $out );
3097 $lookup{ $subseq } = 1;
3101 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3102 $new_record->{ "SEQ" } = $subseq;
3104 put_record( $new_record, $out );
3110 put_record( $record, $out );
3116 sub script_split_bed
3118 # Martin A. Hansen, June 2008.
3120 # Split a BED record into overlapping windows.
3122 my ( $in, # handle to in stream
3123 $out, # handle to out stream
3124 $options, # options hash
3129 my ( $record, $new_record, $i );
3131 $options->{ "window_size" } ||= 20;
3132 $options->{ "step_size" } ||= 1;
3134 while ( $record = get_record( $in ) )
3136 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3138 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3140 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3142 $new_record->{ "REC_TYPE" } = "BED";
3143 $new_record->{ "CHR" } = $record->{ "CHR" };
3144 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3145 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3146 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3147 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3148 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3149 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3151 put_record( $new_record, $out );
3156 put_record( $record, $out );
3162 sub script_align_seq
3164 # Martin A. Hansen, August 2007.
3166 # Align sequences in stream.
3168 my ( $in, # handle to in stream
3169 $out, # handle to out stream
3174 my ( $record, @entries, $entry );
3176 while ( $record = get_record( $in ) )
3178 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3179 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3180 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3181 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3183 put_record( $record, $out );
3187 @entries = Maasha::Align::align( \@entries );
3189 foreach $entry ( @entries )
3191 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3194 SEQ_NAME => $entry->[ SEQ_NAME ],
3195 SEQ => $entry->[ SEQ ],
3198 put_record( $record, $out );
3206 # Martin A. Hansen, February 2008.
3208 # Using the first sequence in stream as reference, tile
3209 # all subsequent sequences based on pairwise alignments.
3211 my ( $in, # handle to in stream
3212 $out, # handle to out stream
3213 $options, # options hash
3218 my ( $record, $first, $ref_entry, @entries );
3222 while ( $record = get_record( $in ) )
3224 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3228 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3234 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3239 put_record( $record, $out );
3243 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3245 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3249 sub script_invert_align
3251 # Martin A. Hansen, February 2008.
3253 # Inverts an alignment showing only non-mathing residues
3254 # using the first sequence as reference.
3256 my ( $in, # handle to in stream
3257 $out, # handle to out stream
3258 $options, # options hash
3263 my ( $record, @entries );
3265 while ( $record = get_record( $in ) )
3267 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3269 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3273 put_record( $record, $out );
3277 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3279 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3283 sub script_patscan_seq
3285 # Martin A. Hansen, August 2007.
3287 # Locates patterns in sequences using scan_for_matches.
3289 my ( $in, # handle to in stream
3290 $out, # handle to out stream
3291 $options, # options hash
3296 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3298 if ( $options->{ "patterns" } ) {
3299 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3300 } elsif ( -f $options->{ "patterns_in" } ) {
3301 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3304 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3306 push @args, "-c" if $options->{ "comp" };
3307 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3308 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3310 $seq_file = "$BP_TMP/patscan.seq";
3311 $pat_file = "$BP_TMP/patscan.pat";
3312 $out_file = "$BP_TMP/patscan.out";
3314 $fh_out = Maasha::Common::write_open( $seq_file );
3318 while ( $record = get_record( $in ) )
3320 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3322 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3324 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3326 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3334 $arg = join " ", @args;
3335 $arg .= " -p" if $type eq "protein";
3337 foreach $pattern ( @{ $patterns } )
3339 $fh_out = Maasha::Common::write_open( $pat_file );
3341 print $fh_out "$pattern\n";
3345 if ( $options->{ 'genome' } ) {
3346 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3347 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3349 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3350 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3353 $fh_in = Maasha::Common::read_open( $out_file );
3355 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3357 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3359 if ( $options->{ 'genome' } )
3361 $result->{ "CHR" } = $result->{ "S_ID" };
3362 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3363 $result->{ "CHR_END" } = $result->{ "S_END" };
3365 delete $result->{ "S_ID" };
3366 delete $result->{ "S_BEG" };
3367 delete $result->{ "S_END" };
3371 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3374 put_record( $result, $out );
3386 sub script_create_blast_db
3388 # Martin A. Hansen, September 2007.
3390 # Creates a NCBI BLAST database with formatdb
3392 my ( $in, # handle to in stream
3393 $out, # handle to out stream
3394 $options, # options hash
3399 my ( $fh, $seq_type, $path, $record, $entry );
3401 $path = $options->{ "database" };
3403 $fh = Maasha::Common::write_open( $path );
3405 while ( $record = get_record( $in ) )
3407 put_record( $record, $out ) if not $options->{ "no_stream" };
3409 if ( $entry = record2fasta( $record ) )
3411 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3413 Maasha::Fasta::put_entry( $entry, $fh );
3419 if ( $seq_type eq "protein" ) {
3420 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3422 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3429 sub script_blast_seq
3431 # Martin A. Hansen, September 2007.
3433 # BLASTs sequences in stream against a given database.
3435 my ( $in, # handle to in stream
3436 $out, # handle to out stream
3437 $options, # options hash
3442 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3444 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3445 $options->{ "filter" } = "F";
3446 $options->{ "filter" } = "T" if $options->{ "filter" };
3447 $options->{ "cpus" } ||= 1;
3449 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3451 $tmp_in = "$BP_TMP/blast_query.seq";
3452 $tmp_out = "$BP_TMP/blast.result";
3454 $fh_out = Maasha::Common::write_open( $tmp_in );
3456 while ( $record = get_record( $in ) )
3458 if ( $entry = record2fasta( $record ) )
3460 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3462 Maasha::Fasta::put_entry( $entry, $fh_out );
3465 put_record( $record, $out );
3470 if ( -f $options->{ 'database' } . ".phr" ) {
3471 $s_type = "protein";
3473 $s_type = "nucleotide";
3476 if ( not $options->{ 'program' } )
3478 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3479 $options->{ 'program' } = "blastn";
3480 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3481 $options->{ 'program' } = "blastp";
3482 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3483 $options->{ 'program' } = "blastx";
3484 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3485 $options->{ 'program' } = "tblastn";
3489 if ( $options->{ 'verbose' } )
3491 Maasha::Common::run(
3494 "-p $options->{ 'program' }",
3495 "-e $options->{ 'e_val' }",
3496 "-a $options->{ 'cpus' }",
3499 "-d $options->{ 'database' }",
3500 "-F $options->{ 'filter' }",
3508 Maasha::Common::run(
3511 "-p $options->{ 'program' }",
3512 "-e $options->{ 'e_val' }",
3513 "-a $options->{ 'cpus' }",
3516 "-d $options->{ 'database' }",
3517 "-F $options->{ 'filter' }",
3527 $fh_out = Maasha::Common::read_open( $tmp_out );
3531 while ( $line = <$fh_out> )
3535 next if $line =~ /^#/;
3537 @fields = split /\s+/, $line;
3539 $record->{ "REC_TYPE" } = "BLAST";
3540 $record->{ "Q_ID" } = $fields[ 0 ];
3541 $record->{ "S_ID" } = $fields[ 1 ];
3542 $record->{ "IDENT" } = $fields[ 2 ];
3543 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3544 $record->{ "MISMATCHES" } = $fields[ 4 ];
3545 $record->{ "GAPS" } = $fields[ 5 ];
3546 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3547 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3548 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3549 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3550 $record->{ "E_VAL" } = $fields[ 10 ];
3551 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3553 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3555 $record->{ "STRAND" } = '-';
3557 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3561 $record->{ "STRAND" } = '+';
3564 put_record( $record, $out );
3575 # Martin A. Hansen, August 2007.
3577 # BLATs sequences in stream against a given genome.
3579 my ( $in, # handle to in stream
3580 $out, # handle to out stream
3581 $options, # options hash
3586 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3588 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3590 $options->{ 'tile_size' } ||= 11;
3591 $options->{ 'one_off' } ||= 0;
3592 $options->{ 'min_identity' } ||= 90;
3593 $options->{ 'min_score' } ||= 0;
3594 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3596 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3597 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3598 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3599 $blat_args .= " -minScore=$options->{ 'min_score' }";
3600 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3601 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3603 $query_file = "$BP_TMP/blat.seq";
3605 $fh_out = Maasha::Common::write_open( $query_file );
3607 while ( $record = get_record( $in ) )
3609 if ( $entry = record2fasta( $record ) )
3611 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3612 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3615 put_record( $record, $out );
3620 $blat_args .= " -t=dnax" if $type eq "protein";
3621 $blat_args .= " -q=$type";
3623 $result_file = "$BP_TMP/blat.psl";
3625 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3629 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3631 map { put_record( $_, $out ) } @{ $entries };
3633 unlink $result_file;
3639 # Martin A. Hansen, July 2008.
3641 # soap sequences in stream against a given file or genome.
3643 my ( $in, # handle to in stream
3644 $out, # handle to out stream
3645 $options, # options hash
3650 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3652 $options->{ "seed_size" } ||= 10;
3653 $options->{ "mismatches" } ||= 2;
3654 $options->{ "gap_size" } ||= 0;
3655 $options->{ "cpus" } ||= 1;
3657 if ( $options->{ "genome" } ) {
3658 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3661 $tmp_in = "$BP_TMP/soap_query.seq";
3662 $tmp_out = "$BP_TMP/soap.result";
3664 $fh_out = Maasha::Common::write_open( $tmp_in );
3668 while ( $record = get_record( $in ) )
3670 if ( $entry = record2fasta( $record ) )
3672 Maasha::Fasta::put_entry( $entry, $fh_out );
3677 put_record( $record, $out );
3685 "-s $options->{ 'seed_size' }",
3688 "-v $options->{ 'mismatches' }",
3689 "-g $options->{ 'gap_size' }",
3690 "-p $options->{ 'cpus' }",
3691 "-d $options->{ 'in_file' }",
3695 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3697 Maasha::Common::run( "soap", $args, 1 );
3701 $fh_out = Maasha::Common::read_open( $tmp_out );
3705 while ( $line = <$fh_out> )
3709 @fields = split /\t/, $line;
3711 $record->{ "REC_TYPE" } = "SOAP";
3712 $record->{ "Q_ID" } = $fields[ 0 ];
3713 $record->{ "SCORE" } = $fields[ 3 ];
3714 $record->{ "STRAND" } = $fields[ 6 ];
3715 $record->{ "S_ID" } = $fields[ 7 ];
3716 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3717 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3719 put_record( $record, $out );
3729 sub script_match_seq
3731 # Martin A. Hansen, August 2007.
3733 # BLATs sequences in stream against a given genome.
3735 my ( $in, # handle to in stream
3736 $out, # handle to out stream
3737 $options, # options hash
3742 my ( $record, @entries, $results );
3744 $options->{ "word_size" } ||= 20;
3745 $options->{ "direction" } ||= "both";
3747 while ( $record = get_record( $in ) )
3749 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3750 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3753 put_record( $record, $out );
3756 if ( @entries == 1 )
3758 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3760 map { put_record( $_, $out ) } @{ $results };
3762 elsif ( @entries == 2 )
3764 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3766 map { put_record( $_, $out ) } @{ $results };
3771 sub script_create_vmatch_index
3773 # Martin A. Hansen, January 2008.
3775 # Create a vmatch index from sequences in the stream.
3777 my ( $in, # handle to in stream
3778 $out, # handle to out stream
3779 $options, # options hash
3784 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3786 if ( $options->{ "index_name" } )
3788 $file_tmp = $options->{ 'index_name' };
3789 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3792 while ( $record = get_record( $in ) )
3794 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3796 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3798 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3801 put_record( $record, $out ) if not $options->{ "no_stream" };
3804 if ( $options->{ "index_name" } )
3808 if ( $type eq "protein" ) {
3809 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3811 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3819 sub script_vmatch_seq
3821 # Martin A. Hansen, August 2007.
3823 # Vmatches sequences in stream against a given genome.
3825 my ( $in, # handle to in stream
3826 $out, # handle to out stream
3827 $options, # options hash
3832 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3834 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3836 if ( $options->{ "index_name" } )
3838 @index_files = $options->{ "index_name" };
3842 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3844 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3846 @index_files = sort keys %hash;
3849 while ( $record = get_record( $in ) )
3851 push @records, $record;
3853 put_record( $record, $out );
3856 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3860 $fh_in = Maasha::Common::read_open( $result_file );
3862 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3863 put_record( $record, $out );
3868 unlink $result_file;
3872 sub script_write_fasta
3874 # Martin A. Hansen, August 2007.
3876 # Write FASTA entries from sequences in stream.
3878 my ( $in, # handle to in stream
3879 $out, # handle to out stream
3880 $options, # options hash
3885 my ( $record, $fh, $entry );
3887 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3889 while ( $record = get_record( $in ) )
3891 if ( $entry = record2fasta( $record ) ) {
3892 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3895 put_record( $record, $out ) if not $options->{ "no_stream" };
3902 sub script_write_align
3904 # Martin A. Hansen, August 2007.
3906 # Write pretty alignments aligned sequences in stream.
3908 my ( $in, # handle to in stream
3909 $out, # handle to out stream
3910 $options, # options hash
3915 my ( $fh, $record, @entries );
3917 $fh = write_stream( $options->{ "data_out" } ) ;
3919 while ( $record = get_record( $in ) )
3921 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3922 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3925 put_record( $record, $out ) if not $options->{ "no_stream" };
3928 if ( scalar( @entries ) == 2 ) {
3929 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3930 } elsif ( scalar ( @entries ) > 2 ) {
3931 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3938 sub script_write_blast
3940 # Martin A. Hansen, November 2007.
3942 # Write data in blast table format (-m8 and 9).
3944 my ( $in, # handle to in stream
3945 $out, # handle to out stream
3946 $options, # options hash
3951 my ( $fh, $record, $first );
3953 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3957 while ( $record = get_record( $in ) )
3959 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3961 if ( $options->{ "comment" } and $first )
3963 print "# Fields: Query id, Subject id, % identity, alignment length, mismatches, gap openings, q. start, q. end, s. start, s. end, e-value, bit score\n";
3968 if ( $record->{ "STRAND" } eq "-" ) {
3969 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3972 print $fh join( "\t",
3973 $record->{ "Q_ID" },
3974 $record->{ "S_ID" },
3975 $record->{ "IDENT" },
3976 $record->{ "ALIGN_LEN" },
3977 $record->{ "MISMATCHES" },
3978 $record->{ "GAPS" },
3979 $record->{ "Q_BEG" } + 1,
3980 $record->{ "Q_END" } + 1,
3981 $record->{ "S_BEG" } + 1,
3982 $record->{ "S_END" } + 1,
3983 $record->{ "E_VAL" },
3984 $record->{ "BIT_SCORE" }
3988 put_record( $record, $out ) if not $options->{ "no_stream" };
3995 sub script_write_tab
3997 # Martin A. Hansen, August 2007.
3999 # Write data as table.
4001 my ( $in, # handle to in stream
4002 $out, # handle to out stream
4003 $options, # options hash
4008 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
4010 $options->{ "delimit" } ||= "\t";
4012 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
4014 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4016 while ( $record = get_record( $in ) )
4021 if ( $options->{ "keys" } )
4023 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
4027 foreach $key ( @{ $options->{ "keys" } } )
4029 if ( exists $record->{ $key } )
4031 push @keys, $key if $options->{ "comment" };
4032 push @vals, $record->{ $key };
4039 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4041 next if exists $no_keys{ $key };
4043 push @keys, $key if $options->{ "comment" };
4044 push @vals, $record->{ $key };
4048 if ( @keys and $options->{ "comment" } )
4050 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
4052 delete $options->{ "comment" };
4055 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4057 put_record( $record, $out ) if not $options->{ "no_stream" };
4064 sub script_write_bed
4066 # Martin A. Hansen, August 2007.
4068 # Write BED format for the UCSC genome browser using records in stream.
4070 my ( $in, # handle to in stream
4071 $out, # handle to out stream
4072 $options, # options hash
4077 my ( $fh, $record, $new_record );
4079 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4081 while ( $record = get_record( $in ) )
4083 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4085 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4087 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4089 $new_record->{ "CHR" } = $record->{ "S_ID" };
4090 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4091 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4092 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4093 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4094 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4096 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4098 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4100 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4102 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4104 $new_record->{ "CHR" } = $record->{ "S_ID" };
4105 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4106 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4107 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4108 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4109 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4111 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4113 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4115 $new_record->{ "CHR" } = $record->{ "S_ID" };
4116 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4117 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4118 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4119 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4120 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4122 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4124 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4126 $new_record->{ "CHR" } = $record->{ "S_ID" };
4127 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4128 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4129 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4130 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4131 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4133 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4135 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4137 Maasha::UCSC::bed_put_entry( $record, $fh );
4140 put_record( $record, $out ) if not $options->{ "no_stream" };
4147 sub script_write_psl
4149 # Martin A. Hansen, August 2007.
4151 # Write PSL output from stream.
4153 my ( $in, # handle to in stream
4154 $out, # handle to out stream
4155 $options, # options hash
4160 my ( $fh, $record, @output, $first );
4164 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4166 while ( $record = get_record( $in ) )
4168 put_record( $record, $out ) if not $options->{ "no_stream" };
4170 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4172 Maasha::UCSC::psl_put_header( $fh ) if $first;
4173 Maasha::UCSC::psl_put_entry( $record, $fh );
4182 sub script_write_fixedstep
4184 # Martin A. Hansen, Juli 2008.
4186 # Write fixedStep entries from recrods in the stream.
4188 my ( $in, # handle to in stream
4189 $out, # handle to out stream
4190 $options, # options hash
4195 my ( $fh, $record, $vals );
4197 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4199 while ( $record = get_record( $in ) )
4201 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4203 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4205 $vals = $record->{ 'VALS' };
4209 print $fh "$vals\n";
4212 put_record( $record, $out ) if not $options->{ "no_stream" };
4219 sub script_write_2bit
4221 # Martin A. Hansen, March 2008.
4223 # Write sequence entries from stream in 2bit format.
4225 my ( $in, # handle to in stream
4226 $out, # handle to out stream
4227 $options, # options hash
4232 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4234 $mask = 1 if not $options->{ "no_mask" };
4236 $tmp_file = "$BP_TMP/write_2bit.fna";
4237 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4239 $fh_out = write_stream( $options->{ "data_out" } );
4241 while ( $record = get_record( $in ) )
4243 if ( $entry = record2fasta( $record ) ) {
4244 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4247 put_record( $record, $out ) if not $options->{ "no_stream" };
4252 $fh_in = Maasha::Common::read_open( $tmp_file );
4254 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4263 sub script_write_solid
4265 # Martin A. Hansen, April 2008.
4267 # Write di-base encoded Solid sequence from entries in stream.
4269 my ( $in, # handle to in stream
4270 $out, # handle to out stream
4271 $options, # options hash
4276 my ( $record, $fh, $entry );
4278 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4280 while ( $record = get_record( $in ) )
4282 if ( $entry = record2fasta( $record ) )
4284 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4286 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4289 put_record( $record, $out ) if not $options->{ "no_stream" };
4296 sub script_plot_seqlogo
4298 # Martin A. Hansen, August 2007.
4300 # Calculates and writes a sequence logo for alignments.
4302 my ( $in, # handle to in stream
4303 $out, # handle to out stream
4304 $options, # options hash
4309 my ( $record, @entries, $logo, $fh );
4311 while ( $record = get_record( $in ) )
4313 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4314 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4317 put_record( $record, $out ) if not $options->{ "no_stream" };
4320 $logo = Maasha::Plot::seq_logo( \@entries );
4322 $fh = write_stream( $options->{ "data_out" } );
4330 sub script_plot_phastcons_profiles
4332 # Martin A. Hansen, January 2008.
4334 # Plots PhastCons profiles.
4336 my ( $in, # handle to in stream
4337 $out, # handle to out stream
4338 $options, # options hash
4343 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4345 $options->{ "title" } ||= "PhastCons Profiles";
4347 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4348 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4350 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4351 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4353 while ( $record = get_record( $in ) )
4355 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4357 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4359 push @{ $AoA }, [ @{ $scores } ];
4362 put_record( $record, $out ) if not $options->{ "no_stream" };
4365 Maasha::UCSC::phastcons_normalize( $AoA );
4367 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4368 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4370 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4372 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4374 $fh = write_stream( $options->{ "data_out" } );
4376 print $fh "$_\n" foreach @{ $plot };
4382 sub script_analyze_bed
4384 # Martin A. Hansen, March 2008.
4386 # Analyze BED entries in stream.
4388 my ( $in, # handle to in stream
4389 $out, # handle to out stream
4390 $options, # options hash
4397 while ( $record = get_record( $in ) )
4399 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4401 put_record( $record, $out );
4406 sub script_analyze_vals
4408 # Martin A. Hansen, August 2007.
4410 # Analyze values for given keys in stream.
4412 my ( $in, # handle to in stream
4413 $out, # handle to out stream
4414 $options, # options hash
4419 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4421 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4423 while ( $record = get_record( $in ) )
4425 foreach $key ( keys %{ $record } )
4427 next if $options->{ "keys" } and not exists $key_hash{ $key };
4429 $analysis->{ $key }->{ "COUNT" }++;
4431 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4433 $analysis->{ $key }->{ "TYPE" } = "num";
4434 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4435 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4436 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4440 $len = length $record->{ $key };
4442 $analysis->{ $key }->{ "TYPE" } = "alph";
4443 $analysis->{ $key }->{ "SUM" } += $len;
4444 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4445 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4449 put_record( $record, $out ) if not $options->{ "no_stream" };
4452 foreach $key ( keys %{ $analysis } )
4454 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4455 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4458 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4468 if ( $options->{ "keys" } ) {
4469 @keys = @{ $options->{ "keys" } };
4471 @keys = keys %{ $analysis };
4474 foreach $key ( @keys )
4476 $keys .= sprintf "% 15s", $key;
4477 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4478 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4479 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4480 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4481 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4482 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4485 print $out "$keys\n";
4486 print $out "$types\n";
4487 print $out "$counts\n";
4488 print $out "$mins\n";
4489 print $out "$maxs\n";
4490 print $out "$sums\n";
4491 print $out "$means\n";
4495 sub script_head_records
4497 # Martin A. Hansen, August 2007.
4499 # Display the first sequences in stream.
4501 my ( $in, # handle to in stream
4502 $out, # handle to out stream
4503 $options, # options hash
4508 my ( $record, $count );
4510 $options->{ "num" } ||= 10;
4514 while ( $record = get_record( $in ) )
4518 put_record( $record, $out );
4520 last if $count == $options->{ "num" };
4525 sub script_remove_keys
4527 # Martin A. Hansen, August 2007.
4529 # Remove keys from stream.
4531 my ( $in, # handle to in stream
4532 $out, # handle to out stream
4533 $options, # options hash
4538 my ( $record, $new_record );
4540 while ( $record = get_record( $in ) )
4542 if ( $options->{ "keys" } )
4544 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4546 elsif ( $options->{ "save_keys" } )
4548 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4550 $record = $new_record;
4553 put_record( $record, $out ) if keys %{ $record };
4558 sub script_remove_adaptor
4560 # Martin A. Hansen, August 2008.
4562 # Find and remove adaptor from sequences in the stream.
4564 my ( $in, # handle to in stream
4565 $out, # handle to out stream
4566 $options, # options hash
4571 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
4573 $max_mismatch = $options->{ "mismatches" } || 0;
4574 $offset = $options->{ "offset" } || 15;
4575 $adaptor = $options->{ "adaptor" };
4576 $adaptor_len = length $adaptor;
4577 $adaptor = [ split //, uc $adaptor ];
4579 $max_match = $adaptor_len - $max_mismatch;
4581 while ( $record = get_record( $in ) )
4583 if ( $record->{ "SEQ" } )
4585 $seq = $record->{ "SEQ" };
4586 $seq_len = length $seq;
4587 $seq = [ split //, uc $seq ];
4589 $pos = Maasha::Seq::find_adaptor( $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch );
4591 $record->{ "ADAPTOR_POS" } = $pos;
4593 if ( $pos >= 0 and not $options->{ "no_remove" } )
4595 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
4596 $record->{ "SEQ_LEN" } = $pos;
4599 put_record( $record, $out );
4603 put_record( $record, $out );
4609 sub script_rename_keys
4611 # Martin A. Hansen, August 2007.
4613 # Rename keys in stream.
4615 my ( $in, # handle to in stream
4616 $out, # handle to out stream
4617 $options, # options hash
4624 while ( $record = get_record( $in ) )
4626 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4628 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4630 delete $record->{ $options->{ "keys" }->[ 0 ] };
4633 put_record( $record, $out );
4638 sub script_uniq_vals
4640 # Martin A. Hansen, August 2007.
4642 # Find unique values in stream.
4644 my ( $in, # handle to in stream
4645 $out, # handle to out stream
4646 $options, # options hash
4651 my ( %hash, $record );
4653 while ( $record = get_record( $in ) )
4655 if ( $record->{ $options->{ "key" } } )
4657 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4659 put_record( $record, $out );
4661 $hash{ $record->{ $options->{ "key" } } } = 1;
4663 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4665 put_record( $record, $out );
4669 $hash{ $record->{ $options->{ "key" } } } = 1;
4674 put_record( $record, $out );
4680 sub script_merge_vals
4682 # Martin A. Hansen, August 2007.
4684 # Rename keys in stream.
4686 my ( $in, # handle to in stream
4687 $out, # handle to out stream
4688 $options, # options hash
4693 my ( $record, @join, $i );
4695 $options->{ "delimit" } ||= '_';
4697 while ( $record = get_record( $in ) )
4699 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4701 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4703 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4704 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4707 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4710 put_record( $record, $out );
4715 sub script_merge_records
4717 # Martin A. Hansen, July 2008.
4719 # Merges records in the stream based on identical values of two given keys.
4721 my ( $in, # handle to in stream
4722 $out, # handle to out stream
4723 $options, # options hash
4728 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4729 $num1, $num2, $num, $cmp, $i );
4731 $merge = $options->{ "merge" } || "AandB";
4733 $file1 = "$BP_TMP/merge_records1.tmp";
4734 $file2 = "$BP_TMP/merge_records2.tmp";
4736 $fh1 = Maasha::Common::write_open( $file1 );
4737 $fh2 = Maasha::Common::write_open( $file2 );
4739 $key1 = $options->{ "keys" }->[ 0 ];
4740 $key2 = $options->{ "keys" }->[ 1 ];
4742 $num = $key2 =~ s/n$//;
4746 while ( $record = get_record( $in ) )
4748 if ( exists $record->{ $key1 } )
4751 @vals1 = $record->{ $key1 };
4753 delete $record->{ $key1 };
4755 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4757 print $fh1 join( "\t", @vals1 ), "\n";
4761 elsif ( exists $record->{ $key2 } )
4764 @vals2 = $record->{ $key2 };
4766 delete $record->{ $key2 };
4768 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4770 print $fh2 join( "\t", @vals2 ), "\n";
4781 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4782 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4786 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4787 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4790 $fh1 = Maasha::Common::read_open( $file1 );
4791 $fh2 = Maasha::Common::read_open( $file2 );
4793 @vals1 = Maasha::Common::get_fields( $fh1 );
4794 @vals2 = Maasha::Common::get_fields( $fh2 );
4796 while ( $num1 > 0 and $num2 > 0 )
4801 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4803 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4808 if ( $merge =~ /^(AorB|AnotB)$/ )
4810 for ( $i = 0; $i < @keys1; $i++ ) {
4811 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4814 put_record( $record, $out );
4817 @vals1 = Maasha::Common::get_fields( $fh1 );
4822 if ( $merge =~ /^(BorA|BnotA)$/ )
4824 for ( $i = 0; $i < @keys2; $i++ ) {
4825 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4828 put_record( $record, $out );
4831 @vals2 = Maasha::Common::get_fields( $fh2 );
4836 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4838 for ( $i = 0; $i < @keys1; $i++ ) {
4839 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4842 for ( $i = 1; $i < @keys2; $i++ ) {
4843 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4846 put_record( $record, $out );
4849 @vals1 = Maasha::Common::get_fields( $fh1 );
4850 @vals2 = Maasha::Common::get_fields( $fh2 );
4862 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4866 for ( $i = 0; $i < @keys1; $i++ ) {
4867 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4870 put_record( $record, $out );
4873 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4877 for ( $i = 0; $i < @keys2; $i++ ) {
4878 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4881 put_record( $record, $out );
4888 # Martin A. Hansen, August 2007.
4890 # Grab for records in stream.
4892 my ( $in, # handle to in stream
4893 $out, # handle to out stream
4894 $options, # options hash
4899 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4901 if ( $options->{ "patterns" } )
4903 $patterns = [ split ",", $options->{ "patterns" } ];
4905 elsif ( -f $options->{ "patterns_in" } )
4907 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4909 elsif ( -f $options->{ "exact_in" } )
4911 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4913 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4918 if ( $options->{ "eval" } )
4920 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4928 while ( $record = get_record( $in ) )
4934 if ( $options->{ "keys" } )
4936 foreach $key ( @{ $options->{ "keys" } } )
4938 if ( exists $lookup_hash{ $record->{ $key } } )
4947 foreach $key ( keys %{ $record } )
4949 if ( not $options->{ "vals_only" } )
4951 if ( exists $lookup_hash{ $key } )
4958 if ( not $options->{ "keys_only" } )
4960 if ( exists $lookup_hash{ $record->{ $key } } )
4971 foreach $pattern ( @{ $patterns } )
4973 if ( $options->{ "keys" } )
4975 foreach $key ( @{ $options->{ "keys" } } )
4977 $pos = index $record->{ $key }, $pattern;
4979 goto FOUND if $pos >= 0;
4984 foreach $key ( keys %{ $record } )
4986 if ( not $options->{ "vals_only" } )
4988 $pos = index $key, $pattern;
4990 goto FOUND if $pos >= 0;
4993 if ( not $options->{ "keys_only" } )
4995 $pos = index $record->{ $key }, $pattern;
4997 goto FOUND if $pos >= 0;
5003 elsif ( $options->{ "regex" } )
5005 if ( $options->{ "keys" } )
5007 foreach $key ( @{ $options->{ "keys" } } )
5009 if ( $options->{ "case_insensitive" } ) {
5010 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5012 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5015 goto FOUND if $pos >= 0;
5020 foreach $key ( keys %{ $record } )
5022 if ( not $options->{ "vals_only" } )
5024 if ( $options->{ "case_insensitive" } ) {
5025 $pos = 1 if $key =~ /$options->{'regex'}/i;
5027 $pos = 1 if $key =~ /$options->{'regex'}/;
5030 goto FOUND if $pos >= 0;
5033 if ( not $options->{ "keys_only" } )
5035 if ( $options->{ "case_insensitive" } ) {
5036 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
5038 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
5041 goto FOUND if $pos >= 0;
5046 elsif ( $options->{ "eval" } )
5048 if ( defined $record->{ $key } )
5050 if ( $op eq "<" and $record->{ $key } < $val ) {
5051 $pos = 1 and goto FOUND;
5052 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
5053 $pos = 1 and goto FOUND;
5054 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
5055 $pos = 1 and goto FOUND;
5056 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
5057 $pos = 1 and goto FOUND;
5058 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
5059 $pos = 1 and goto FOUND;
5060 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
5061 $pos = 1 and goto FOUND;
5062 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
5063 $pos = 1 and goto FOUND;
5064 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
5065 $pos = 1 and goto FOUND;
5072 if ( $pos >= 0 and not $options->{ "invert" } ) {
5073 put_record( $record, $out );
5074 } elsif ( $pos < 0 and $options->{ "invert" } ) {
5075 put_record( $record, $out );
5083 # Martin A. Hansen, August 2007.
5085 # Evaluate extression for records in stream.
5087 my ( $in, # handle to in stream
5088 $out, # handle to out stream
5089 $options, # options hash
5094 my ( $record, $eval_key, $eval_val, $check, @keys );
5096 while ( $record = get_record( $in ) )
5098 if ( $options->{ "eval" } )
5100 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5108 @keys = split /\W+/, $eval_val;
5109 @keys = grep { ! /^\d+$/ } @keys;
5114 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5116 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5119 put_record( $record, $out );
5126 # Martin A. Hansen, June 2008.
5130 my ( $in, # handle to in stream
5131 $out, # handle to out stream
5132 $options, # options hash
5137 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5139 while ( $record = get_record( $in ) )
5143 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5145 push @rows, $record->{ $key };
5149 push @matrix, [ @rows ];
5154 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5156 foreach $row ( @matrix )
5158 for ( $i = 0; $i < @{ $row }; $i++ ) {
5159 $record->{ "V$i" } = $row->[ $i ];
5162 put_record( $record, $out );
5167 sub script_add_ident
5169 # Martin A. Hansen, May 2008.
5171 # Add a unique identifier to each record in stream.
5173 my ( $in, # handle to in stream
5174 $out, # handle to out stream
5175 $options, # options hash
5180 my ( $record, $key, $prefix, $i );
5182 $key = $options->{ "key" } || "ID";
5183 $prefix = $options->{ "prefix" } || "ID";
5187 while ( $record = get_record( $in ) )
5189 $record->{ $key } = sprintf( "$prefix%08d", $i );
5191 put_record( $record, $out );
5198 sub script_count_records
5200 # Martin A. Hansen, August 2007.
5202 # Count records in stream.
5204 my ( $in, # handle to in stream
5205 $out, # handle to out stream
5206 $options, # options hash
5211 my ( $record, $count, $result, $fh, $line );
5215 if ( $options->{ "no_stream" } )
5217 while ( $line = <$in> )
5221 $count++ if $line eq "---";
5226 while ( $record = get_record( $in ) )
5228 put_record( $record, $out );
5234 $result = { "RECORDS_COUNT" => $count };
5236 $fh = write_stream( $options->{ "data_out" } );
5238 put_record( $result, $fh );
5244 sub script_random_records
5246 # Martin A. Hansen, August 2007.
5248 # Pick a number or random records from stream.
5250 my ( $in, # handle to in stream
5251 $out, # handle to out stream
5252 $options, # options hash
5257 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5259 $options->{ "num" } ||= 10;
5261 $tmp_file = "$BP_TMP/random_records.tmp";
5263 $fh_out = Maasha::Common::write_open( $tmp_file );
5267 while ( $record = get_record( $in ) )
5269 put_record( $record, $fh_out );
5279 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5281 while ( $i < $options->{ "num" } )
5283 $rand = int( rand( $count ) );
5285 if ( not exists $rand_hash{ $rand } )
5287 $rand_hash{ $rand } = 1;
5289 $max = $rand if $rand > $max;
5295 $fh_in = Maasha::Common::read_open( $tmp_file );
5299 while ( $record = get_record( $fh_in ) )
5301 put_record( $record, $out ) if exists $rand_hash{ $count };
5303 last if $count == $max;
5314 sub script_sort_records
5316 # Martin A. Hansen, August 2007.
5318 # Sort to sort records according to keys.
5320 my ( $in, # handle to in stream
5321 $out, # handle to out stream
5322 $options, # options hash
5327 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5329 foreach $key ( @{ $options->{ "keys" } } )
5331 if ( $key =~ s/n$// ) {
5332 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5334 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5338 $sort_str = join " or ", @sort_cmd;
5339 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5341 while ( $record = get_record( $in ) ) {
5342 push @records, $record;
5345 @records = sort $sort_sub @records;
5347 if ( $options->{ "reverse" } )
5349 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5350 put_record( $records[ $i ], $out );
5355 for ( $i = 0; $i < scalar @records; $i++ ) {
5356 put_record( $records[ $i ], $out );
5362 sub script_count_vals
5364 # Martin A. Hansen, August 2007.
5366 # Count records in stream.
5368 my ( $in, # handle to in stream
5369 $out, # handle to out stream
5370 $options, # options hash
5375 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5377 $tmp_file = "$BP_TMP/count_cache.tmp";
5379 $fh_out = Maasha::Common::write_open( $tmp_file );
5384 while ( $record = get_record( $in ) )
5386 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5388 push @records, $record;
5390 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5392 map { put_record( $_, $fh_out ) } @records;
5399 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5410 $fh_in = Maasha::Common::read_open( $tmp_file );
5412 while ( $record = get_record( $fh_in ) )
5414 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5416 put_record( $record, $out );
5418 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5426 foreach $record ( @records )
5428 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5430 put_record( $record, $out );
5437 sub script_plot_histogram
5439 # Martin A. Hansen, September 2007.
5441 # Plot a simple histogram for a given key using GNU plot.
5443 my ( $in, # handle to in stream
5444 $out, # handle to out stream
5445 $options, # options hash
5450 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5452 $options->{ "title" } ||= "Histogram";
5453 $options->{ "sort" } ||= "num";
5455 while ( $record = get_record( $in ) )
5457 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5459 put_record( $record, $out ) if not $options->{ "no_stream" };
5462 if ( $options->{ "sort" } eq "num" ) {
5463 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5465 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5468 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5470 $fh = write_stream( $options->{ "data_out" } );
5472 print $fh "$_\n" foreach @{ $result };
5478 sub script_plot_lendist
5480 # Martin A. Hansen, August 2007.
5482 # Plot length distribution using GNU plot.
5484 my ( $in, # handle to in stream
5485 $out, # handle to out stream
5486 $options, # options hash
5491 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5493 $options->{ "title" } ||= "Length Distribution";
5495 while ( $record = get_record( $in ) )
5497 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5499 put_record( $record, $out ) if not $options->{ "no_stream" };
5502 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5504 for ( $i = 0; $i < $max; $i++ ) {
5505 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5508 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5510 $fh = write_stream( $options->{ "data_out" } );
5512 print $fh "$_\n" foreach @{ $result };
5518 sub script_plot_chrdist
5520 # Martin A. Hansen, August 2007.
5522 # Plot chromosome distribution using GNU plot.
5524 my ( $in, # handle to in stream
5525 $out, # handle to out stream
5526 $options, # options hash
5531 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5533 $options->{ "title" } ||= "Chromosome Distribution";
5535 while ( $record = get_record( $in ) )
5537 if ( $record->{ "CHR" } ) { # generic
5538 $data_hash{ $record->{ "CHR" } }++;
5539 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5540 $data_hash{ $record->{ "S_ID" } }++;
5541 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5542 $data_hash{ $record->{ "S_ID" } }++;
5543 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5544 $data_hash{ $record->{ "S_ID" } }++;
5547 put_record( $record, $out ) if not $options->{ "no_stream" };
5550 foreach $elem ( keys %data_hash )
5554 $sort_key =~ s/chr//i;
5556 $sort_key =~ s/^X(.*)/99$1/;
5557 $sort_key =~ s/^Y(.*)/99$1/;
5558 $sort_key =~ s/^Z(.*)/999$1/;
5559 $sort_key =~ s/^M(.*)/9999$1/;
5560 $sort_key =~ s/^U(.*)/99999$1/;
5562 $count = $sort_key =~ tr/_//;
5564 $sort_key =~ s/_.*/"999999" x $count/ex;
5566 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5569 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5571 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5573 $fh = write_stream( $options->{ "data_out" } );
5575 print $fh "$_\n" foreach @{ $result };
5581 sub script_plot_karyogram
5583 # Martin A. Hansen, August 2007.
5585 # Plot hits on karyogram.
5587 my ( $in, # handle to in stream
5588 $out, # handle to out stream
5589 $options, # options hash
5594 my ( %options, $record, @data, $fh, $result, %data_hash );
5596 $options->{ "genome" } ||= "human";
5597 $options->{ "feat_color" } ||= "black";
5599 while ( $record = get_record( $in ) )
5601 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5603 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5606 put_record( $record, $out ) if not $options->{ "no_stream" };
5609 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5611 $fh = write_stream( $options->{ "data_out" } );
5619 sub script_plot_matches
5621 # Martin A. Hansen, August 2007.
5623 # Plot matches in 2D generating a dotplot.
5625 my ( $in, # handle to in stream
5626 $out, # handle to out stream
5627 $options, # options hash
5632 my ( $record, @data, $fh, $result, %data_hash );
5634 $options->{ "direction" } ||= "both";
5636 while ( $record = get_record( $in ) )
5638 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5639 push @data, $record;
5642 put_record( $record, $out ) if not $options->{ "no_stream" };
5645 $options->{ "title" } ||= "plot_matches";
5646 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5647 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5649 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5651 $fh = write_stream( $options->{ "data_out" } );
5653 print $fh "$_\n" foreach @{ $result };
5659 sub script_length_vals
5661 # Martin A. Hansen, August 2007.
5663 # Determine the length of the value for given keys.
5665 my ( $in, # handle to in stream
5666 $out, # handle to out stream
5667 $options, # options hash
5672 my ( $record, $key );
5674 while ( $record = get_record( $in ) )
5676 foreach $key ( @{ $options->{ "keys" } } )
5678 if ( $record->{ $key } ) {
5679 $record->{ $key . "_LEN" } = length $record->{ $key };
5683 put_record( $record, $out );
5690 # Martin A. Hansen, August 2007.
5692 # Calculates the sums for values of given keys.
5694 my ( $in, # handle to in stream
5695 $out, # handle to out stream
5696 $options, # options hash
5701 my ( $record, $key, %sum_hash, $fh );
5703 while ( $record = get_record( $in ) )
5705 foreach $key ( @{ $options->{ "keys" } } )
5707 if ( $record->{ $key } ) {
5708 $sum_hash{ $key } += $record->{ $key };
5712 put_record( $record, $out ) if not $options->{ "no_stream" };
5715 $fh = write_stream( $options->{ "data_out" } );
5717 foreach $key ( @{ $options->{ "keys" } } ) {
5718 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5725 sub script_mean_vals
5727 # Martin A. Hansen, August 2007.
5729 # Calculate the mean of values of given keys.
5731 my ( $in, # handle to in stream
5732 $out, # handle to out stream
5733 $options, # options hash
5738 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5740 while ( $record = get_record( $in ) )
5742 foreach $key ( @{ $options->{ "keys" } } )
5744 if ( $record->{ $key } )
5746 $sum_hash{ $key } += $record->{ $key };
5747 $count_hash{ $key }++;
5751 put_record( $record, $out ) if not $options->{ "no_stream" };
5754 $fh = write_stream( $options->{ "data_out" } );
5756 foreach $key ( @{ $options->{ "keys" } } )
5758 if ( $count_hash{ $key } ) {
5759 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5764 put_record( { $key . "_MEAN" => $mean } , $fh );
5771 sub script_median_vals
5773 # Martin A. Hansen, March 2008.
5775 # Calculate the median values of given keys.
5777 my ( $in, # handle to in stream
5778 $out, # handle to out stream
5779 $options, # options hash
5784 my ( $record, $key, %median_hash, $median, $fh );
5786 while ( $record = get_record( $in ) )
5788 foreach $key ( @{ $options->{ "keys" } } ) {
5789 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5792 put_record( $record, $out ) if not $options->{ "no_stream" };
5795 $fh = write_stream( $options->{ "data_out" } );
5797 foreach $key ( @{ $options->{ "keys" } } )
5799 if ( $median_hash{ $key } ) {
5800 $median = Maasha::Calc::median( $median_hash{ $key } );
5805 put_record( { $key . "_MEDIAN" => $median } , $fh );
5814 # Martin A. Hansen, February 2008.
5816 # Determine the maximum values of given keys.
5818 my ( $in, # handle to in stream
5819 $out, # handle to out stream
5820 $options, # options hash
5825 my ( $record, $key, $fh, %max_hash, $max_record );
5827 while ( $record = get_record( $in ) )
5829 foreach $key ( @{ $options->{ "keys" } } )
5831 if ( $record->{ $key } )
5833 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5837 put_record( $record, $out ) if not $options->{ "no_stream" };
5840 $fh = write_stream( $options->{ "data_out" } );
5842 foreach $key ( @{ $options->{ "keys" } } )
5844 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5847 put_record( $max_record, $fh );
5855 # Martin A. Hansen, February 2008.
5857 # Determine the minimum values of given keys.
5859 my ( $in, # handle to in stream
5860 $out, # handle to out stream
5861 $options, # options hash
5866 my ( $record, $key, $fh, %min_hash, $min_record );
5868 while ( $record = get_record( $in ) )
5870 foreach $key ( @{ $options->{ "keys" } } )
5872 if ( defined $record->{ $key } )
5874 if ( exists $min_hash{ $key } ) {
5875 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5877 $min_hash{ $key } = $record->{ $key };
5882 put_record( $record, $out ) if not $options->{ "no_stream" };
5885 $fh = write_stream( $options->{ "data_out" } );
5887 foreach $key ( @{ $options->{ "keys" } } )
5889 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5892 put_record( $min_record, $fh );
5898 sub script_upload_to_ucsc
5900 # Martin A. Hansen, August 2007.
5902 # Calculate the mean of values of given keys.
5904 # This routine has developed into the most ugly hack. Do something!
5906 my ( $in, # handle to in stream
5907 $out, # handle to out stream
5908 $options, # options hash
5913 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5914 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones, $vals );
5916 $options->{ "short_label" } ||= $options->{ 'table' };
5917 $options->{ "long_label" } ||= $options->{ 'table' };
5918 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5919 $options->{ "priority" } ||= 1;
5920 $options->{ "visibility" } ||= "pack";
5921 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5922 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5924 $file = "$BP_TMP/ucsc_upload.tmp";
5932 if ( $options->{ 'wiggle' } )
5934 while ( $record = get_record( $in ) )
5936 put_record( $record, $out ) if not $options->{ "no_stream" };
5938 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5939 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5940 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5942 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5944 $fh_out = $fh_hash{ $record->{ "CHR" } };
5946 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5949 map { close $_ } keys %fh_hash;
5951 $fh_out = Maasha::Common::write_open( $file );
5953 foreach $chr ( sort keys %fh_hash )
5955 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5957 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5961 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5963 $chr = $entry->{ 'CHR' };
5964 $beg = $entry->{ 'CHR_BEG' };
5965 $end = $entry->{ 'CHR_END' };
5966 $q_id = $entry->{ 'Q_ID' };
5968 if ( $options->{ "score" } ) {
5969 $clones = $entry->{ 'SCORE' };
5970 } if ( $q_id =~ /_(\d+)$/ ) {
5980 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5985 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5986 $block->[ $i ] += $clones;
5989 $max = Maasha::Calc::max( $max, $end );
5998 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5999 $block->[ $i ] += $clones;
6006 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
6008 unlink "$BP_TMP/$chr";
6017 $fh_out = Maasha::Common::write_open( $file );
6019 while ( $record = get_record( $in ) )
6021 put_record( $record, $out ) if not $options->{ "no_stream" };
6023 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
6025 $vals = $record->{ "VALS" };
6028 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
6029 print $fh_out "$vals\n";
6031 $format = "WIGGLE" if not $format;
6033 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
6035 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
6036 Maasha::UCSC::psl_put_entry( $record, $fh_out );
6040 $format = "PSL" if not $format;
6042 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
6044 # chrom chromStart chromEnd name score strand size secStr conf
6046 print $fh_out join ( "\t",
6048 $record->{ "CHR_BEG" },
6049 $record->{ "CHR_END" } + 1,
6050 $record->{ "Q_ID" },
6051 $record->{ "SCORE" },
6052 $record->{ "STRAND" },
6053 $record->{ "SIZE" },
6054 $record->{ "SEC_STRUCT" },
6055 $record->{ "CONF" },
6058 $format = "BED_SS" if not $format;
6060 elsif ( $record->{ "REC_TYPE" } eq "BED" )
6062 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
6064 $format = "BED" if not $format;
6065 $columns = $record->{ "BED_COLS" } if not $columns;
6067 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
6069 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6071 $format = "BED" if not $format;
6072 $columns = 6 if not $columns;
6074 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
6076 $record->{ "CHR" } = $record->{ "S_ID" };
6077 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6078 $record->{ "CHR_END" } = $record->{ "S_END" };
6079 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
6081 $format = "BED" if not $format;
6082 $columns = 6 if not $columns;
6084 Maasha::UCSC::bed_put_entry( $record, $fh_out );
6086 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
6088 $record->{ "CHR" } = $record->{ "S_ID" };
6089 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
6090 $record->{ "CHR_END" } = $record->{ "S_END" };
6091 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
6092 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
6094 $format = "BED" if not $format;
6095 $columns = 6 if not $columns;
6097 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
6100 if ( $i == $options->{ "chunk_size" } )
6104 if ( $format eq "BED" ) {
6105 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6106 } elsif ( $format eq "PSL" ) {
6107 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6116 $fh_out = Maasha::Common::write_open( $file );
6125 if ( exists $options->{ "database" } and $options->{ "table" } )
6127 if ( $format eq "BED" )
6129 $type = "bed $columns";
6131 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6133 elsif ( $format eq "BED_SS" )
6135 $options->{ "sec_struct" } = 1;
6137 $type = "sec_struct";
6139 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6141 elsif ( $format eq "PSL" )
6145 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6147 elsif ( $format eq "WIGGLE" )
6149 $options->{ "visibility" } = "full";
6151 $wig_file = "$options->{ 'table' }.wig";
6152 $wib_file = "$options->{ 'table' }.wib";
6154 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
6156 Maasha::Common::dir_create_if_not_exists( $wib_dir );
6158 if ( $options->{ 'verbose' } ) {
6159 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
6161 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
6164 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
6172 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6177 Maasha::UCSC::update_my_tracks( $options, $type );
6182 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6187 # Martin A. Hansen, July 2008.
6189 # Given a biopiece record converts it to a FASTA record.
6190 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6191 # tried in that order.
6193 my ( $record, # record
6198 my ( $seq_name, $seq );
6200 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6201 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6203 if ( defined $seq_name and defined $seq ) {
6204 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6213 # Martin A. Hansen, July 2007.
6215 # Opens a stream to STDIN or a file,
6217 my ( $path, # path - OPTIONAL
6220 # Returns filehandle.
6224 if ( not -t STDIN ) {
6225 $fh = Maasha::Common::read_stdin();
6226 } elsif ( not $path ) {
6227 # Maasha::Common::error( qq(no data stream) );
6229 $fh = Maasha::Common::read_open( $path );
6232 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6240 # Martin A. Hansen, August 2007.
6242 # Opens a stream to STDOUT or a file.
6244 my ( $path, # path - OPTIONAL
6245 $gzip, # compress data - OPTIONAL
6248 # Returns filehandle.
6253 $fh = Maasha::Common::write_open( $path, $gzip );
6255 $fh = Maasha::Common::write_stdout();
6264 # Martin A. Hansen, July 2007.
6266 # Reads one record at a time and converts that record
6267 # to a Perl data structure (a hash) which is returned.
6269 my ( $fh, # handle to stream
6274 my ( $block, @lines, $line, $key, $value, %record );
6276 local $/ = "\n---\n";
6282 return if not defined $block;
6284 @lines = split "\n", $block;
6286 foreach $line ( @lines )
6288 ( $key, $value ) = split ": ", $line, 2;
6290 $record{ $key } = $value;
6293 return wantarray ? %record : \%record;
6299 # Martin A. Hansen, July 2007.
6301 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6303 my ( $data, # data structure
6304 $fh, # file handle - OPTIONAL
6309 if ( scalar keys %{ $data } )
6313 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6318 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6329 # Martin A. Hansen, November 2007.
6331 # Extracts files from an explicit GetOpt::Long argument
6332 # allowing for the use of glob. E.g.
6333 # --data_in=test.fna
6334 # --data_in=test.fna,test2.fna
6336 # --data_in=test.fna,/dir/*.fna
6338 my ( $option, # option from GetOpt::Long
6343 my ( $elem, @files );
6345 foreach $elem ( split ",", $option )
6349 } elsif ( $elem =~ /\*/ ) {
6350 push @files, glob( $elem );
6354 return wantarray ? @files : \@files;
6360 # Martin A. Hansen, April 2008.
6362 # Removes temporary directory and exits gracefully.
6363 # This subroutine is meant to be run always as the last
6364 # thing even if a script is dies or is interrupted
6367 my ( $sig, # signal from the %SIG
6370 # print STDERR "signal->$sig<-\n";
6378 if ( $sig =~ /MAASHA_ERROR/ ) {
6379 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6380 } elsif ( $sig eq "INT" ) {
6381 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6382 } elsif ( $sig eq "TERM" ) {
6383 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6385 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6397 # Martin A. Hansen, July 2008.
6399 # Cleans out any unused temporary files and direcotries in BP_TMP.
6403 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6405 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6407 $curr_pid = Maasha::Common::get_processid();
6409 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6411 foreach $dir ( @dirs )
6413 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6419 if ( $user eq Maasha::Common::get_user() )
6421 if ( not Maasha::Common::process_running( $pid ) )
6423 # print STDERR "Removing stale dir: $dir\n";
6424 Maasha::Common::dir_remove( $dir );
6426 elsif ( $pid == $curr_pid )
6428 # print STDERR "Removing current dir: $dir\n";
6429 Maasha::Common::dir_remove( $dir );
6443 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<