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;
57 use vars qw( @ISA @EXPORT_OK );
61 @ISA = qw( Exporter );
76 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
79 $SIG{ '__DIE__' } = \&sig_handler;
80 $SIG{ 'INT' } = \&sig_handler;
81 $SIG{ 'TERM' } = \&sig_handler;
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87 my ( $script, $BP_TMP );
89 $script = Maasha::Common::get_scriptname();
90 $BP_TMP = Maasha::Common::get_tmpdir();
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
96 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
97 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
99 $log_global->autoflush( 1 );
100 $log_local->autoflush( 1 );
102 &log( $log_global, $script, \@ARGV );
103 &log( $log_local, $script, \@ARGV );
109 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
112 my $t0 = gettimeofday();
114 run_script( $script );
116 my $t1 = gettimeofday();
118 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
121 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
126 # Martin A. Hansen, January 2008.
128 # Log messages to logfile.
130 my ( $fh, # filehandle to logfile
131 $script, # script name
132 $argv, # reference to @ARGV
137 my ( $time_stamp, $user );
139 $time_stamp = Maasha::Common::time_stamp();
141 $user = $ENV{ 'USER' };
143 $script = "biopieces" if $script eq "-e";
145 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
151 # Martin A. Hansen, August 2007.
153 # Run a specific script.
155 my ( $script, # script name
160 my ( $options, $in, $out );
162 $options = get_options( $script );
164 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
165 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
168 $in = read_stream( $options->{ "stream_in" } );
169 $out = write_stream( $options->{ "stream_out" } );
171 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
172 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
173 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
174 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
175 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
176 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
177 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
178 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
179 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
181 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
182 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
183 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
184 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
185 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
186 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
187 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
188 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
189 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
190 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
191 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
192 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
193 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
194 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
195 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
196 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
197 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
198 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
199 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
200 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
201 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
202 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
203 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
204 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
205 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
206 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
207 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
209 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
210 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
211 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
212 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
213 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
214 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
215 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
216 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
217 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
218 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
219 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
220 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
221 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
222 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
223 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
224 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
225 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
226 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
227 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
228 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
229 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
230 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
231 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
232 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
233 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
234 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
235 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
236 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
237 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
238 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
239 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
240 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
241 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
242 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
243 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
244 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
245 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
246 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
247 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
248 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
249 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
250 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
251 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
252 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
253 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
254 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
255 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
256 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
257 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
258 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
259 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
260 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
262 close $in if defined $in;
265 # unset status - missing
266 # write log file - missing
272 # Martin A. Hansen, February 2008.
274 # Gets options from commandline and checks these vigerously.
276 my ( $script, # name of script
281 my ( %options, @options, $opt, @genomes );
283 if ( $script eq "print_usage" )
289 elsif ( $script eq "read_fasta" )
296 elsif ( $script eq "read_tab" )
307 elsif ( $script eq "read_psl" )
314 elsif ( $script eq "read_bed" )
321 elsif ( $script eq "read_fixedstep" )
328 elsif ( $script eq "read_blast_tab" )
335 elsif ( $script eq "read_embl" )
345 elsif ( $script eq "read_stockholm" )
352 elsif ( $script eq "read_phastcons" )
363 elsif ( $script eq "read_soft" )
370 elsif ( $script eq "read_gff" )
377 elsif ( $script eq "read_2bit" )
385 elsif ( $script eq "read_solexa" )
393 elsif ( $script eq "read_solid" )
401 elsif ( $script eq "read_mysql" )
410 elsif ( $script eq "format_genome" )
419 elsif ( $script eq "length_seq" )
426 elsif ( $script eq "oligo_freq" )
433 elsif ( $script eq "create_weight_matrix" )
439 elsif ( $script eq "transliterate_seq" )
447 elsif ( $script eq "transliterate_vals" )
456 elsif ( $script eq "translate_seq" )
462 elsif ( $script eq "extract_seq" )
470 elsif ( $script eq "get_genome_seq" )
482 elsif ( $script eq "get_genome_align" )
493 elsif ( $script eq "get_genome_phastcons" )
504 elsif ( $script eq "split_seq" )
511 elsif ( $script eq "split_bed" )
518 elsif ( $script eq "tile_seq" )
525 elsif ( $script eq "invert_align" )
531 elsif ( $script eq "patscan_seq" )
542 elsif ( $script eq "create_blast_db" )
549 elsif ( $script eq "blast_seq" )
561 elsif ( $script eq "blat_seq" )
573 elsif ( $script eq "soap_seq" )
583 elsif ( $script eq "match_seq" )
590 elsif ( $script eq "create_vmatch_index" )
598 elsif ( $script eq "vmatch_seq" )
609 elsif ( $script eq "write_fasta" )
618 elsif ( $script eq "write_align" )
628 elsif ( $script eq "write_blast" )
637 elsif ( $script eq "write_tab" )
649 elsif ( $script eq "write_bed" )
657 elsif ( $script eq "write_psl" )
665 elsif ( $script eq "write_fixedstep" )
673 elsif ( $script eq "write_2bit" )
681 elsif ( $script eq "write_solid" )
690 elsif ( $script eq "plot_seqlogo" )
697 elsif ( $script eq "plot_phastcons_profiles" )
712 elsif ( $script eq "analyze_vals" )
719 elsif ( $script eq "head_records" )
725 elsif ( $script eq "remove_keys" )
732 elsif ( $script eq "rename_keys" )
738 elsif ( $script eq "uniq_vals" )
745 elsif ( $script eq "merge_vals" )
752 elsif ( $script eq "grab" )
767 elsif ( $script eq "compute" )
773 elsif ( $script eq "add_ident" )
780 elsif ( $script eq "count_records" )
787 elsif ( $script eq "random_records" )
793 elsif ( $script eq "sort_records" )
800 elsif ( $script eq "count_vals" )
806 elsif ( $script eq "plot_histogram" )
819 elsif ( $script eq "plot_lendist" )
831 elsif ( $script eq "plot_chrdist" )
842 elsif ( $script eq "plot_karyogram" )
851 elsif ( $script eq "plot_matches" )
863 elsif ( $script eq "length_vals" )
869 elsif ( $script eq "sum_vals" )
877 elsif ( $script eq "mean_vals" )
885 elsif ( $script eq "median_vals" )
893 elsif ( $script eq "max_vals" )
901 elsif ( $script eq "min_vals" )
909 elsif ( $script eq "upload_to_ucsc" )
934 # print STDERR Dumper( \@options );
941 $options{ "script" } = $script;
943 # print STDERR Dumper( \%options );
945 if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
946 return wantarray ? %options : \%options;
949 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
950 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
951 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
952 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
953 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
954 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
955 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
956 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
958 # ---- check arguments ----
960 if ( $options{ 'data_in' } )
962 $options{ "files" } = getopt_files( $options{ 'data_in' } );
964 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
967 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
969 # print STDERR Dumper( \%options );
971 foreach $opt ( keys %options )
973 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
975 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
977 elsif ( $opt =~ /beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|num|skip|cpus|window_size|step_size/ and $options{ $opt } !~ /^\d+$/ )
979 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
981 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
983 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
985 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
987 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
989 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
991 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
993 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
995 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
997 elsif ( $opt eq "genome" and $script ne "format_genome" )
999 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1000 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1002 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1003 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1006 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1008 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1010 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1012 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1016 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1017 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1018 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1019 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1020 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1021 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1022 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1023 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1024 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" };
1025 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1026 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" };
1028 if ( $script eq "upload_to_ucsc" )
1030 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1031 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1034 return wantarray ? %options : \%options;
1038 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1041 sub script_print_usage
1043 # Martin A. Hansen, January 2008.
1045 # Retrieves usage information from file and
1046 # prints this nicely formatted.
1048 my ( $in, # handle to in stream
1049 $out, # handle to out stream
1050 $options, # options hash
1055 my ( $file, $wiki, $lines );
1057 if ( $options->{ 'data_in' } ) {
1058 $file = $options->{ 'data_in' };
1060 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1063 $wiki = Maasha::Gwiki::gwiki_read( $file );
1065 if ( not $options->{ "help" } ) {
1066 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1069 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1071 print STDERR "$_\n" foreach @{ $lines };
1077 sub script_list_biopieces
1079 # Martin A. Hansen, January 2008.
1081 # Prints the synopsis from the usage for each of the biopieces.
1083 my ( $in, # handle to in stream
1084 $out, # handle to out stream
1085 $options, # options hash
1090 my ( @files, $file, $wiki, $program, $synopsis );
1092 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1094 foreach $file ( sort @files )
1096 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1100 $wiki = Maasha::Gwiki::gwiki_read( $file );
1102 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1103 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1105 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1107 printf( "%-30s%s\n", $program, $synopsis );
1115 sub script_list_genomes
1117 # Martin A. Hansen, January 2008.
1119 # Prints the synopsis from the usage for each of the biopieces.
1121 my ( $in, # handle to in stream
1122 $out, # handle to out stream
1123 $options, # options hash
1128 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1130 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1132 foreach $genome ( @genomes )
1134 next if $genome =~ /\.$/;
1136 @formats = Maasha::Common::ls_dirs( $genome );
1138 foreach $format ( @formats )
1140 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1142 $hash{ $1 }{ $2 } = 1;
1151 map { push @row, $_ } sort keys %found;
1153 print join( "\t", @row ), "\n";
1155 foreach $genome ( sort keys %hash )
1159 foreach $format ( sort keys %found )
1161 if ( exists $hash{ $genome }{ $format } ) {
1168 print join( "\t", @row ), "\n";
1173 sub script_read_fasta
1175 # Martin A. Hansen, August 2007.
1177 # Read sequences from FASTA file.
1179 my ( $in, # handle to in stream
1180 $out, # handle to out stream
1181 $options, # options hash
1186 my ( $record, $file, $data_in, $entry, $num );
1188 while ( $record = get_record( $in ) ) {
1189 put_record( $record, $out );
1194 foreach $file ( @{ $options->{ "files" } } )
1196 $data_in = Maasha::Common::read_open( $file );
1198 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1200 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1203 SEQ_NAME => $entry->[ SEQ_NAME ],
1204 SEQ => $entry->[ SEQ ],
1205 SEQ_LEN => length $entry->[ SEQ ],
1208 put_record( $record, $out );
1211 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1221 close $data_in if $data_in;
1227 # Martin A. Hansen, August 2007.
1229 # Read table or table columns from stream or file.
1231 my ( $in, # handle to in stream
1232 $out, # handle to out stream
1233 $options, # options hash
1238 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1240 $options->{ 'delimit' } ||= '\s+';
1242 while ( $record = get_record( $in ) ) {
1243 put_record( $record, $out );
1246 $skip = $options->{ 'skip' } ||= 0;
1249 foreach $file ( @{ $options->{ "files" } } )
1251 $data_in = Maasha::Common::read_open( $file );
1253 while ( $line = <$data_in> )
1261 next if $line =~ /^#|^$/;
1268 @fields = split /$options->{'delimit'}/, $line;
1270 if ( $options->{ "cols" } ) {
1271 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1276 for ( $i = 0; $i < @fields2; $i++ )
1278 if ( $options->{ "keys" }->[ $i ] ) {
1279 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1281 $record->{ "V" . $i } = $fields2[ $i ];
1285 put_record( $record, $out );
1287 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1297 close $data_in if $data_in;
1303 # Martin A. Hansen, August 2007.
1305 # Read psl table from stream or file.
1307 my ( $in, # handle to in stream
1308 $out, # handle to out stream
1309 $options, # options hash
1314 my ( $record, @files, $file, $entries, $entry, $num );
1316 while ( $record = get_record( $in ) ) {
1317 put_record( $record, $out );
1322 foreach $file ( @{ $options->{ "files" } } )
1324 $entries = Maasha::UCSC::psl_get_entries( $file );
1326 foreach $entry ( @{ $entries } )
1328 put_record( $entry, $out );
1330 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1342 # Martin A. Hansen, August 2007.
1344 # Read bed table from stream or file.
1346 my ( $in, # handle to in stream
1347 $out, # handle to out stream
1348 $options, # options hash
1353 my ( $file, $record, $entry, $data_in, $num );
1355 while ( $record = get_record( $in ) ) {
1356 put_record( $record, $out );
1361 foreach $file ( @{ $options->{ "files" } } )
1363 $data_in = Maasha::Common::read_open( $file );
1365 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1367 put_record( $entry, $out );
1369 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1379 close $data_in if $data_in;
1383 sub script_read_fixedstep
1385 # Martin A. Hansen, Juli 2008.
1387 # Read fixedStep wiggle format from stream or file.
1389 my ( $in, # handle to in stream
1390 $out, # handle to out stream
1391 $options, # options hash
1396 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1398 while ( $record = get_record( $in ) ) {
1399 put_record( $record, $out );
1404 foreach $file ( @{ $options->{ "files" } } )
1406 $data_in = Maasha::Common::read_open( $file );
1408 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1410 $head = shift @{ $entry };
1412 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1414 $record->{ "CHR" } = $1;
1415 $record->{ "CHR_BEG" } = $2;
1416 $record->{ "STEP" } = $3;
1417 $record->{ "VALS" } = join ",", @{ $entry };
1420 put_record( $record, $out );
1422 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1432 close $data_in if $data_in;
1436 sub script_read_blast_tab
1438 # Martin A. Hansen, September 2007.
1440 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1442 my ( $in, # handle to in stream
1443 $out, # handle to out stream
1444 $options, # options hash
1449 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1451 while ( $record = get_record( $in ) ) {
1452 put_record( $record, $out );
1457 foreach $file ( @{ $options->{ "files" } } )
1459 $data_in = Maasha::Common::read_open( $file );
1461 while ( $line = <$data_in> )
1465 next if $line =~ /^#/;
1467 @fields = split /\t/, $line;
1469 $record->{ "REC_TYPE" } = "BLAST";
1470 $record->{ "Q_ID" } = $fields[ 0 ];
1471 $record->{ "S_ID" } = $fields[ 1 ];
1472 $record->{ "IDENT" } = $fields[ 2 ];
1473 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1474 $record->{ "MISMATCHES" } = $fields[ 4 ];
1475 $record->{ "GAPS" } = $fields[ 5 ];
1476 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1477 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1478 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1479 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1480 $record->{ "E_VAL" } = $fields[ 10 ];
1481 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1483 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1485 $record->{ "STRAND" } = '-';
1487 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1491 $record->{ "STRAND" } = '+';
1494 put_record( $record, $out );
1496 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1506 close $data_in if $data_in;
1510 sub script_read_embl
1512 # Martin A. Hansen, August 2007.
1516 my ( $in, # handle to in stream
1517 $out, # handle to out stream
1518 $options, # options hash
1523 my ( %options2, $file, $data_in, $num, $entry, $record );
1525 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1526 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1527 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1529 while ( $record = get_record( $in ) ) {
1530 put_record( $record, $out );
1535 foreach $file ( @{ $options->{ "files" } } )
1537 $data_in = Maasha::Common::read_open( $file );
1539 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1541 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1543 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1545 $record_copy = dclone $record;
1547 delete $record_copy->{ "FT" };
1549 put_record( $record_copy, $out );
1551 delete $record_copy->{ "SEQ" };
1553 foreach $feat ( keys %{ $record->{ "FT" } } )
1555 $record_copy->{ "FEAT_TYPE" } = $feat;
1557 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1559 foreach $qual ( keys %{ $feat2 } )
1561 $qual_val = join "; ", @{ $feat2->{ $qual } };
1566 $record_copy->{ $qual } = $qual_val;
1569 put_record( $record_copy, $out );
1573 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1583 close $data_in if $data_in;
1587 sub script_read_stockholm
1589 # Martin A. Hansen, August 2007.
1591 # Read Stockholm format.
1593 my ( $in, # handle to in stream
1594 $out, # handle to out stream
1595 $options, # options hash
1600 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1602 while ( $record = get_record( $in ) ) {
1603 put_record( $record, $out );
1608 foreach $file ( @{ $options->{ "files" } } )
1610 $data_in = Maasha::Common::read_open( $file );
1612 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1614 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1618 foreach $key ( keys %{ $record->{ "GF" } } ) {
1619 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1622 $record_anno->{ "ALIGN" } = $num;
1624 put_record( $record_anno, $out );
1626 foreach $seq ( @{ $record->{ "ALIGN" } } )
1628 undef $record_align;
1631 SEQ_NAME => $seq->[ 0 ],
1635 put_record( $record_align, $out );
1638 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1648 close $data_in if $data_in;
1652 sub script_read_phastcons
1654 # Martin A. Hansen, December 2007.
1656 # Read PhastCons format.
1658 my ( $in, # handle to in stream
1659 $out, # handle to out stream
1660 $options, # options hash
1665 my ( $data_in, $file, $num, $entry, @records, $record );
1667 $options->{ "min" } ||= 10;
1668 $options->{ "dist" } ||= 25;
1669 $options->{ "threshold" } ||= 0.8;
1670 $options->{ "gap" } ||= 5;
1672 while ( $record = get_record( $in ) ) {
1673 put_record( $record, $out );
1678 foreach $file ( @{ $options->{ "files" } } )
1680 $data_in = Maasha::Common::read_open( $file );
1682 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1684 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1686 foreach $record ( @records )
1688 $record->{ "REC_TYPE" } = "BED";
1689 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1691 put_record( $record, $out );
1693 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1704 close $data_in if $data_in;
1708 sub script_read_soft
1710 # Martin A. Hansen, December 2007.
1713 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1715 my ( $in, # handle to in stream
1716 $out, # handle to out stream
1717 $options, # options hash
1722 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1724 while ( $record = get_record( $in ) ) {
1725 put_record( $record, $out );
1730 foreach $file ( @{ $options->{ "files" } } )
1732 $soft_index = Maasha::NCBI::soft_index_file( $file );
1734 $fh = Maasha::Common::read_open( $file );
1736 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1738 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1740 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1742 $old_end = $platforms[ -1 ]->[ 2 ];
1744 foreach $sample ( @samples )
1746 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1748 foreach $record ( @{ $records } )
1750 put_record( $record, $out );
1752 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1757 $old_end = $sample->[ 2 ];
1765 close $data_in if $data_in;
1772 # Martin A. Hansen, February 2008.
1775 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1777 my ( $in, # handle to in stream
1778 $out, # handle to out stream
1779 $options, # options hash
1784 my ( $data_in, $file, $fh, $num, $record, $entry );
1786 while ( $record = get_record( $in ) ) {
1787 put_record( $record, $out );
1792 foreach $file ( @{ $options->{ "files" } } )
1794 $fh = Maasha::Common::read_open( $file );
1796 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1798 put_record( $entry, $out );
1800 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1810 close $data_in if $data_in;
1814 sub script_read_2bit
1816 # Martin A. Hansen, March 2008.
1818 # Read sequences from 2bit file.
1820 my ( $in, # handle to in stream
1821 $out, # handle to out stream
1822 $options, # options hash
1827 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1829 $mask = 1 if not $options->{ "no_mask" };
1831 while ( $record = get_record( $in ) ) {
1832 put_record( $record, $out );
1837 foreach $file ( @{ $options->{ "files" } } )
1839 $data_in = Maasha::Common::read_open( $file );
1841 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1843 foreach $line ( @{ $toc } )
1845 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1846 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1847 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1849 put_record( $record, $out );
1851 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1861 close $data_in if $data_in;
1865 sub script_read_solexa
1867 # Martin A. Hansen, March 2008.
1869 # Read Solexa sequence reads from file.
1871 my ( $in, # handle to in stream
1872 $out, # handle to out stream
1873 $options, # options hash
1878 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1880 $options->{ "quality" } ||= 20;
1882 while ( $record = get_record( $in ) ) {
1883 put_record( $record, $out );
1888 foreach $file ( @{ $options->{ "files" } } )
1890 $data_in = Maasha::Common::read_open( $file );
1891 $base_name = Maasha::Common::get_basename( $file );
1892 $base_name =~ s/\..*//;
1896 while ( $line = <$data_in> )
1898 @fields = split /:/, $line;
1899 @seqs = split //, $fields[ 5 ];
1900 @scores = split / /, $fields[ -1 ];
1902 for ( $i = 0; $i < @scores; $i++ ) {
1903 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1906 $seq = join "", @seqs;
1908 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1909 $record->{ "SEQ" } = $seq;
1910 $record->{ "SEQ_LEN" } = length $seq;
1911 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1913 put_record( $record, $out );
1915 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1926 close $data_in if $data_in;
1930 sub script_read_solid
1932 # Martin A. Hansen, April 2008.
1934 # Read Solid sequence from file.
1936 my ( $in, # handle to in stream
1937 $out, # handle to out stream
1938 $options, # options hash
1943 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1945 $options->{ "quality" } ||= 15;
1947 while ( $record = get_record( $in ) ) {
1948 put_record( $record, $out );
1953 foreach $file ( @{ $options->{ "files" } } )
1955 $data_in = Maasha::Common::read_open( $file );
1957 while ( $line = <$data_in> )
1961 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1963 @scores = split /,/, $seq_qual;
1964 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1966 for ( $i = 0; $i < @seqs; $i++ ) {
1967 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1971 SEQ_NAME => $seq_name,
1973 SEQ_QUAL => $seq_qual,
1974 SEQ_LEN => length $seq_cs,
1975 SEQ => join( "", @seqs ),
1976 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1979 put_record( $record, $out );
1981 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1991 close $data_in if $data_in;
1995 sub script_read_mysql
1997 # Martin A. Hansen, May 2008.
1999 # Read a MySQL query into stream.
2001 my ( $in, # handle to in stream
2002 $out, # handle to out stream
2003 $options, # options hash
2008 my ( $record, $dbh, $results );
2010 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2011 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2013 while ( $record = get_record( $in ) ) {
2014 put_record( $record, $out );
2017 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2019 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2021 Maasha::SQL::disconnect( $dbh );
2023 map { put_record( $_ ) } @{ $results };
2027 sub script_format_genome
2029 # Martin A. Hansen, Juli 2008.
2031 # Format a genome to speficed formats.
2033 my ( $in, # handle to in stream
2034 $out, # handle to out stream
2035 $options, # options hash
2040 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index );
2042 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2043 $genome = $options->{ 'genome' };
2045 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2046 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2047 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2049 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2051 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2053 $fasta_dir = "$dir/genomes/$genome/fasta";
2057 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2059 $fasta_dir = "$dir/genomes/$genome/fasta";
2061 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2064 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2066 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2068 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2070 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2073 while ( $record = get_record( $in ) )
2075 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2077 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
2079 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2081 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2083 $vals = $record->{ 'VALS' };
2087 print $fh_out "$vals\n";
2090 put_record( $record, $out ) if not $options->{ "no_stream" };
2093 foreach $format ( @{ $options->{ 'formats' } } )
2095 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2096 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2097 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2098 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2099 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2102 close $fh_out if $fh_out;
2106 sub script_length_seq
2108 # Martin A. Hansen, August 2007.
2110 # Determine the length of sequences in stream.
2112 my ( $in, # handle to in stream
2113 $out, # handle to out stream
2114 $options, # options hash
2119 my ( $record, $total );
2121 while ( $record = get_record( $in ) )
2123 if ( $record->{ "SEQ" } )
2125 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2126 $total += $record->{ "SEQ_LEN" };
2129 put_record( $record, $out ) if not $options->{ "no_stream" };
2132 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2136 sub script_uppercase_seq
2138 # Martin A. Hansen, August 2007.
2140 # Uppercases sequences in stream.
2142 my ( $in, # handle to in stream
2143 $out, # handle to out stream
2150 while ( $record = get_record( $in ) )
2152 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2154 put_record( $record, $out );
2159 sub script_shuffle_seq
2161 # Martin A. Hansen, December 2007.
2163 # Shuffle sequences in stream.
2165 my ( $in, # handle to in stream
2166 $out, # handle to out stream
2173 while ( $record = get_record( $in ) )
2175 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2177 put_record( $record, $out );
2182 sub script_analyze_seq
2184 # Martin A. Hansen, August 2007.
2186 # Analyze sequence composition of sequences in stream.
2188 my ( $in, # handle to in stream
2189 $out, # handle to out stream
2194 my ( $record, $analysis );
2196 while ( $record = get_record( $in ) )
2198 if ( $record->{ "SEQ" } )
2200 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2202 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2205 put_record( $record, $out );
2210 sub script_analyze_tags
2212 # Martin A. Hansen, August 2008.
2214 # Analyze sequence tags in stream.
2216 my ( $in, # handle to in stream
2217 $out, # handle to out stream
2222 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2224 while ( $record = get_record( $in ) )
2226 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2228 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2232 $len_hash{ length( $record->{ "SEQ" } ) }++;
2233 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2236 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2238 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2242 $len_hash{ $record->{ "BED_LEN" } }++;
2243 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2248 foreach $key ( sort { $a <=> $b } keys %len_hash )
2250 $tag_record->{ "TAG_LEN" } = $key;
2251 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2252 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2254 put_record( $tag_record, $out );
2259 sub script_complexity_seq
2261 # Martin A. Hansen, May 2008.
2263 # Generates an index calculated as the most common di-residue over
2264 # the sequence length for all sequences in stream.
2266 my ( $in, # handle to in stream
2267 $out, # handle to out stream
2272 my ( $record, $index );
2274 while ( $record = get_record( $in ) )
2276 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2278 put_record( $record, $out );
2283 sub script_oligo_freq
2285 # Martin A. Hansen, August 2007.
2287 # Determine the length of sequences in stream.
2289 my ( $in, # handle to in stream
2290 $out, # handle to out stream
2291 $options, # options hash
2296 my ( $record, %oligos, @freq_table );
2298 $options->{ "word_size" } ||= 7;
2300 while ( $record = get_record( $in ) )
2302 if ( $record->{ "SEQ" } )
2304 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2306 if ( not $options->{ "all" } )
2308 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2310 map { put_record( $_, $out ) } @freq_table;
2316 put_record( $record, $out );
2319 if ( $options->{ "all" } )
2321 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2323 map { put_record( $_, $out ) } @freq_table;
2328 sub script_create_weight_matrix
2330 # Martin A. Hansen, August 2007.
2332 # Creates a weight matrix from an alignmnet.
2334 my ( $in, # handle to in stream
2335 $out, # handle to out stream
2336 $options, # options hash
2341 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2345 while ( $record = get_record( $in ) )
2347 if ( $record->{ "SEQ" } )
2349 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2351 $res = substr $record->{ "SEQ" }, $i, 1;
2353 $freq_hash{ $i }{ $res }++;
2354 $res_hash{ $res } = 1;
2361 put_record( $record, $out );
2365 foreach $res ( sort keys %res_hash )
2369 $record->{ "V0" } = $res;
2371 for ( $i = 0; $i < keys %freq_hash; $i++ )
2373 $freq = $freq_hash{ $i }{ $res } || 0;
2375 if ( $options->{ "percent" } ) {
2376 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2379 $record->{ "V" . ( $i + 1 ) } = $freq;
2382 put_record( $record, $out );
2387 sub script_calc_bit_scores
2389 # Martin A. Hansen, March 2007.
2391 # Calculates the bit scores for each position from an alignmnet in the stream.
2393 my ( $in, # handle to in stream
2394 $out, # handle to out stream
2399 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2403 while ( $record = get_record( $in ) )
2405 if ( $record->{ "SEQ" } )
2407 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2409 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2411 $res = substr $record->{ "SEQ" }, $i, 1;
2413 next if $res =~ /-|_|~|\./;
2415 $freq_hash{ $i }{ $res }++;
2422 put_record( $record, $out );
2428 if ( $type eq "protein" ) {
2434 for ( $i = 0; $i < keys %freq_hash; $i++ )
2436 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2438 $bit_diff = $bit_max - $bit_height;
2440 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2443 put_record( $record, $out );
2447 sub script_reverse_seq
2449 # Martin A. Hansen, August 2007.
2451 # Reverse sequence in record.
2453 my ( $in, # handle to in stream
2454 $out, # handle to out stream
2461 while ( $record = get_record( $in ) )
2463 if ( $record->{ "SEQ" } ) {
2464 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2467 put_record( $record, $out );
2472 sub script_complement_seq
2474 # Martin A. Hansen, August 2007.
2476 # Complement sequence in record.
2478 my ( $in, # handle to in stream
2479 $out, # handle to out stream
2484 my ( $record, $type );
2486 while ( $record = get_record( $in ) )
2488 if ( $record->{ "SEQ" } )
2491 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2494 if ( $type eq "rna" ) {
2495 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2496 } elsif ( $type eq "dna" ) {
2497 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2501 put_record( $record, $out );
2506 sub script_remove_indels
2508 # Martin A. Hansen, August 2007.
2510 # Remove indels from sequences in stream.
2512 my ( $in, # handle to in stream
2513 $out, # handle to out stream
2520 while ( $record = get_record( $in ) )
2522 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2524 put_record( $record, $out );
2529 sub script_transliterate_seq
2531 # Martin A. Hansen, August 2007.
2533 # Transliterate chars from sequence in record.
2535 my ( $in, # handle to in stream
2536 $out, # handle to out stream
2537 $options, # options hash
2542 my ( $record, $search, $replace, $delete );
2544 $search = $options->{ "search" } || "";
2545 $replace = $options->{ "replace" } || "";
2546 $delete = $options->{ "delete" } || "";
2548 while ( $record = get_record( $in ) )
2550 if ( $record->{ "SEQ" } )
2552 if ( $search and $replace ) {
2553 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2554 } elsif ( $delete ) {
2555 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2559 put_record( $record, $out );
2564 sub script_transliterate_vals
2566 # Martin A. Hansen, April 2008.
2568 # Transliterate chars from values in record.
2570 my ( $in, # handle to in stream
2571 $out, # handle to out stream
2572 $options, # options hash
2577 my ( $record, $search, $replace, $delete, $key );
2579 $search = $options->{ "search" } || "";
2580 $replace = $options->{ "replace" } || "";
2581 $delete = $options->{ "delete" } || "";
2583 while ( $record = get_record( $in ) )
2585 foreach $key ( @{ $options->{ "keys" } } )
2587 if ( exists $record->{ $key } )
2589 if ( $search and $replace ) {
2590 eval "\$record->{ $key } =~ tr/$search/$replace/";
2591 } elsif ( $delete ) {
2592 eval "\$record->{ $key } =~ tr/$delete//d";
2597 put_record( $record, $out );
2602 sub script_translate_seq
2604 # Martin A. Hansen, February 2008.
2606 # Translate DNA sequence into protein sequence.
2608 my ( $in, # handle to in stream
2609 $out, # handle to out stream
2610 $options, # options hash
2615 my ( $record, $frame, %new_record );
2617 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2619 while ( $record = get_record( $in ) )
2621 if ( $record->{ "SEQ" } )
2623 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2625 foreach $frame ( @{ $options->{ "frames" } } )
2627 %new_record = %{ $record };
2629 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2630 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2631 $new_record{ "FRAME" } = $frame;
2633 put_record( \%new_record, $out );
2639 put_record( $record, $out );
2645 sub script_extract_seq
2647 # Martin A. Hansen, August 2007.
2649 # Extract subsequences from sequences in record.
2651 my ( $in, # handle to in stream
2652 $out, # handle to out stream
2653 $options, # options hash
2658 my ( $beg, $end, $len, $record );
2660 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2663 $beg = $options->{ "beg" } - 1; # correcting for start offset
2666 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2668 } elsif ( defined $options->{ "end" } ) {
2669 $end = $options->{ "end" } - 1; # correcting for start offset
2672 $len = $options->{ "len" };
2674 # print "beg->$beg, end->$end, len->$len\n";
2676 while ( $record = get_record( $in ) )
2678 if ( $record->{ "SEQ" } )
2680 if ( defined $beg and defined $end )
2682 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2683 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2685 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2688 elsif ( defined $beg and defined $len )
2690 if ( $len > length $record->{ "SEQ" } ) {
2691 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2693 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2696 elsif ( defined $beg )
2698 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2702 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2704 put_record( $record, $out );
2709 sub script_get_genome_seq
2711 # Martin A. Hansen, December 2007.
2713 # Gets a subsequence from a genome.
2715 my ( $in, # handle to in stream
2716 $out, # handle to out stream
2717 $options, # options hash
2722 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2724 $options->{ "flank" } ||= 0;
2726 if ( $options->{ "genome" } )
2728 $genome = $options->{ "genome" };
2730 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2731 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2733 $fh = Maasha::Common::read_open( $genome_file );
2734 $index = Maasha::Fasta::index_retrieve( $index_file );
2736 shift @{ $index }; # Get rid of the file size info
2738 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2740 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2742 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2744 $beg = $index_beg + $options->{ "beg" } - 1;
2746 if ( $options->{ "len" } ) {
2747 $len = $options->{ "len" };
2748 } elsif ( $options->{ "end" } ) {
2749 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2752 $beg -= $options->{ "flank" };
2753 $len += 2 * $options->{ "flank" };
2755 if ( $beg <= $index_beg )
2757 $len -= $index_beg - $beg;
2761 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2763 next if $beg > $index_beg + $index_len;
2765 $record->{ "CHR" } = $options->{ "chr" };
2766 $record->{ "CHR_BEG" } = $beg - $index_beg;
2767 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2769 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2770 $record->{ "SEQ_LEN" } = $len;
2772 put_record( $record, $out );
2776 while ( $record = get_record( $in ) )
2778 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2780 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2782 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2784 $beg = $record->{ "CHR_BEG" } + $index_beg;
2785 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2787 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2789 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2791 $beg = $record->{ "S_BEG" } + $index_beg;
2792 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2794 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2796 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2798 $beg = $record->{ "S_BEG" } + $index_beg;
2799 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2802 $beg -= $options->{ "flank" };
2803 $len += 2 * $options->{ "flank" };
2805 if ( $beg <= $index_beg )
2807 $len -= $index_beg - $beg;
2811 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2813 next if $beg > $index_beg + $index_len;
2815 $record->{ "CHR_BEG" } = $beg - $index_beg;
2816 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2818 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2820 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2822 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2823 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2826 if ( $options->{ "mask" } )
2828 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2830 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2832 @begs = split ",", $record->{ "Q_BEGS" };
2833 @lens = split ",", $record->{ "BLOCKSIZES" };
2835 for ( $i = 0; $i < @begs; $i++ ) {
2836 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2842 put_record( $record, $out );
2849 sub script_get_genome_align
2851 # Martin A. Hansen, April 2008.
2853 # Gets a subalignment from a multiple genome alignment.
2855 my ( $in, # handle to in stream
2856 $out, # handle to out stream
2857 $options, # options hash
2862 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2864 $options->{ "strand" } ||= "+";
2868 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2870 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2872 $beg = $options->{ "beg" } - 1;
2874 if ( $options->{ "end" } ) {
2875 $end = $options->{ "end" };
2876 } elsif ( $options->{ "len" } ) {
2877 $end = $beg + $options->{ "len" };
2880 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2882 foreach $entry ( @{ $align } )
2884 $entry->{ "CHR" } = $record->{ "CHR" };
2885 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2886 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2887 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2888 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2889 $entry->{ "SCORE" } = $record->{ "SCORE" };
2891 put_record( $entry, $out );
2895 while ( $record = get_record( $in ) )
2897 if ( $record->{ "REC_TYPE" } eq "BED" )
2899 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2901 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2903 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2905 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2907 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2910 foreach $entry ( @{ $align } )
2912 $entry->{ "CHR" } = $record->{ "CHR" };
2913 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2914 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2915 $entry->{ "STRAND" } = $record->{ "STRAND" };
2916 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2917 $entry->{ "SCORE" } = $record->{ "SCORE" };
2919 put_record( $entry, $out );
2927 sub script_get_genome_phastcons
2929 # Martin A. Hansen, February 2008.
2931 # Get phastcons scores from genome intervals.
2933 my ( $in, # handle to in stream
2934 $out, # handle to out stream
2935 $options, # options hash
2940 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2942 $options->{ "flank" } ||= 0;
2944 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2945 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2947 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2948 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2950 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2952 $options->{ "beg" } -= 1; # request is 1-based
2953 $options->{ "end" } -= 1; # request is 1-based
2955 if ( $options->{ "len" } ) {
2956 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2959 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2961 $record->{ "CHR" } = $options->{ "chr" };
2962 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2963 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2965 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2966 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2968 put_record( $record, $out );
2971 while ( $record = get_record( $in ) )
2973 if ( $record->{ "REC_TYPE" } eq "BED" )
2975 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2977 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2979 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2981 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2983 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2986 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2987 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2989 put_record( $record, $out );
2992 close $fh_phastcons if $fh_phastcons;
2998 # Martin A. Hansen, December 2007.
3000 # Folds sequences in stream into secondary structures.
3002 my ( $in, # handle to in stream
3003 $out, # handle to out stream
3008 my ( $record, $type, $struct, $index );
3010 while ( $record = get_record( $in ) )
3012 if ( $record->{ "SEQ" } )
3015 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3018 if ( $type ne "protein" )
3020 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3021 $record->{ "SEC_STRUCT" } = $struct;
3022 $record->{ "FREE_ENERGY" } = $index;
3023 $record->{ "SCORE" } = abs int $index;
3024 $record->{ "SIZE" } = length $struct;
3025 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3029 put_record( $record, $out );
3034 sub script_split_seq
3036 # Martin A. Hansen, August 2007.
3038 # Split a sequence in stream into words.
3040 my ( $in, # handle to in stream
3041 $out, # handle to out stream
3042 $options, # options hash
3047 my ( $record, $new_record, $i, $subseq, %lookup );
3049 $options->{ "word_size" } ||= 7;
3051 while ( $record = get_record( $in ) )
3053 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3055 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3057 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3059 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3061 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3062 $new_record->{ "SEQ" } = $subseq;
3064 put_record( $new_record, $out );
3066 $lookup{ $subseq } = 1;
3070 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3071 $new_record->{ "SEQ" } = $subseq;
3073 put_record( $new_record, $out );
3079 put_record( $record, $out );
3085 sub script_split_bed
3087 # Martin A. Hansen, June 2008.
3089 # Split a BED record into overlapping windows.
3091 my ( $in, # handle to in stream
3092 $out, # handle to out stream
3093 $options, # options hash
3098 my ( $record, $new_record, $i );
3100 $options->{ "window_size" } ||= 20;
3101 $options->{ "step_size" } ||= 1;
3103 while ( $record = get_record( $in ) )
3105 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3107 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3109 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3111 $new_record->{ "REC_TYPE" } = "BED";
3112 $new_record->{ "CHR" } = $record->{ "CHR" };
3113 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3114 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3115 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3116 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3117 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3118 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3120 put_record( $new_record, $out );
3125 put_record( $record, $out );
3131 sub script_align_seq
3133 # Martin A. Hansen, August 2007.
3135 # Align sequences in stream.
3137 my ( $in, # handle to in stream
3138 $out, # handle to out stream
3143 my ( $record, @entries, $entry );
3145 while ( $record = get_record( $in ) )
3147 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3148 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3149 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3150 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3152 put_record( $record, $out );
3156 @entries = Maasha::Align::align( \@entries );
3158 foreach $entry ( @entries )
3160 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3163 SEQ_NAME => $entry->[ SEQ_NAME ],
3164 SEQ => $entry->[ SEQ ],
3167 put_record( $record, $out );
3175 # Martin A. Hansen, February 2008.
3177 # Using the first sequence in stream as reference, tile
3178 # all subsequent sequences based on pairwise alignments.
3180 my ( $in, # handle to in stream
3181 $out, # handle to out stream
3182 $options, # options hash
3187 my ( $record, $first, $ref_entry, @entries );
3191 while ( $record = get_record( $in ) )
3193 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3197 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3203 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3208 put_record( $record, $out );
3212 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3214 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3218 sub script_invert_align
3220 # Martin A. Hansen, February 2008.
3222 # Inverts an alignment showing only non-mathing residues
3223 # using the first sequence as reference.
3225 my ( $in, # handle to in stream
3226 $out, # handle to out stream
3227 $options, # options hash
3232 my ( $record, @entries );
3234 while ( $record = get_record( $in ) )
3236 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3238 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3242 put_record( $record, $out );
3246 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3248 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3252 sub script_patscan_seq
3254 # Martin A. Hansen, August 2007.
3256 # Locates patterns in sequences using scan_for_matches.
3258 my ( $in, # handle to in stream
3259 $out, # handle to out stream
3260 $options, # options hash
3265 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3267 if ( $options->{ "patterns" } ) {
3268 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3269 } elsif ( -f $options->{ "patterns_in" } ) {
3270 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3273 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3275 push @args, "-c" if $options->{ "comp" };
3276 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3277 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3279 $seq_file = "$BP_TMP/patscan.seq";
3280 $pat_file = "$BP_TMP/patscan.pat";
3281 $out_file = "$BP_TMP/patscan.out";
3283 $fh_out = Maasha::Common::write_open( $seq_file );
3287 while ( $record = get_record( $in ) )
3289 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3291 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3293 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3295 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3300 # put_record( $record, $out );
3305 $arg = join " ", @args;
3306 $arg .= " -p" if $type eq "protein";
3308 foreach $pattern ( @{ $patterns } )
3310 $fh_out = Maasha::Common::write_open( $pat_file );
3312 print $fh_out "$pattern\n";
3316 if ( $options->{ 'genome' } ) {
3317 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3318 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3320 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3321 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3324 $fh_in = Maasha::Common::read_open( $out_file );
3326 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3328 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3330 if ( $options->{ 'genome' } )
3332 $result->{ "CHR" } = $result->{ "S_ID" };
3333 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3334 $result->{ "CHR_END" } = $result->{ "S_END" };
3336 delete $result->{ "S_ID" };
3337 delete $result->{ "S_BEG" };
3338 delete $result->{ "S_END" };
3342 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3345 put_record( $result, $out );
3357 sub script_create_blast_db
3359 # Martin A. Hansen, September 2007.
3361 # Creates a NCBI BLAST database with formatdb
3363 my ( $in, # handle to in stream
3364 $out, # handle to out stream
3365 $options, # options hash
3370 my ( $fh, $seq_type, $path, $record );
3372 $path = $options->{ "database" };
3374 $fh = Maasha::Common::write_open( $path );
3376 while ( $record = get_record( $in ) )
3378 put_record( $record, $out ) if not $options->{ "no_stream" };
3380 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3382 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3384 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3390 if ( $seq_type eq "protein" ) {
3391 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3393 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3400 sub script_blast_seq
3402 # Martin A. Hansen, September 2007.
3404 # BLASTs sequences in stream against a given database.
3406 my ( $in, # handle to in stream
3407 $out, # handle to out stream
3408 $options, # options hash
3413 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3415 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3416 $options->{ "filter" } = "F";
3417 $options->{ "filter" } = "T" if $options->{ "filter" };
3418 $options->{ "cpus" } ||= 1;
3420 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3422 $tmp_in = "$BP_TMP/blast_query.seq";
3423 $tmp_out = "$BP_TMP/blast.result";
3425 $fh_out = Maasha::Common::write_open( $tmp_in );
3427 while ( $record = get_record( $in ) )
3429 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3431 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3433 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3436 put_record( $record, $out );
3441 if ( -f $options->{ 'database' } . ".phr" ) {
3442 $s_type = "protein";
3444 $s_type = "nucleotide";
3447 if ( not $options->{ 'program' } )
3449 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3450 $options->{ 'program' } = "blastn";
3451 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3452 $options->{ 'program' } = "blastp";
3453 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3454 $options->{ 'program' } = "blastx";
3455 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3456 $options->{ 'program' } = "tblastn";
3460 Maasha::Common::run( "blastall", "-p $options->{ 'program' } -e $options->{ 'e_val' } -a $options->{ 'cpus' } -m 8 -i $tmp_in -d $options->{ 'database' } -F $options->{ 'filter' } -o $tmp_out > /dev/null 2>&1", 1 );
3464 $fh_out = Maasha::Common::read_open( $tmp_out );
3468 while ( $line = <$fh_out> )
3472 next if $line =~ /^#/;
3474 @fields = split /\s+/, $line;
3476 $record->{ "REC_TYPE" } = "BLAST";
3477 $record->{ "Q_ID" } = $fields[ 0 ];
3478 $record->{ "S_ID" } = $fields[ 1 ];
3479 $record->{ "IDENT" } = $fields[ 2 ];
3480 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3481 $record->{ "MISMATCHES" } = $fields[ 4 ];
3482 $record->{ "GAPS" } = $fields[ 5 ];
3483 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3484 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3485 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3486 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3487 $record->{ "E_VAL" } = $fields[ 10 ];
3488 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3490 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3492 $record->{ "STRAND" } = '-';
3494 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3498 $record->{ "STRAND" } = '+';
3501 put_record( $record, $out );
3512 # Martin A. Hansen, August 2007.
3514 # BLATs sequences in stream against a given genome.
3516 my ( $in, # handle to in stream
3517 $out, # handle to out stream
3518 $options, # options hash
3523 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3525 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3527 $options->{ 'tile_size' } ||= 11;
3528 $options->{ 'one_off' } ||= 0;
3529 $options->{ 'min_identity' } ||= 90;
3530 $options->{ 'min_score' } ||= 0;
3531 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3533 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3534 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3535 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3536 $blat_args .= " -minScore=$options->{ 'min_score' }";
3537 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3538 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3540 $query_file = "$BP_TMP/blat.seq";
3542 $fh_out = Maasha::Common::write_open( $query_file );
3544 while ( $record = get_record( $in ) )
3546 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3548 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3549 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3552 put_record( $record, $out );
3557 $blat_args .= " -t=dnax" if $type eq "protein";
3558 $blat_args .= " -q=$type";
3560 $result_file = "$BP_TMP/blat.psl";
3562 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3566 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3568 map { put_record( $_, $out ) } @{ $entries };
3570 unlink $result_file;
3576 # Martin A. Hansen, July 2008.
3578 # soap sequences in stream against a given file or genome.
3580 my ( $in, # handle to in stream
3581 $out, # handle to out stream
3582 $options, # options hash
3587 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3589 $options->{ "mismatches" } ||= 2;
3590 $options->{ "gap_size" } ||= 0;
3591 $options->{ "cpus" } ||= 1;
3593 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3595 $tmp_in = "$BP_TMP/soap_query.seq";
3596 $tmp_out = "$BP_TMP/soap.result";
3598 $fh_out = Maasha::Common::write_open( $tmp_in );
3600 while ( $record = get_record( $in ) )
3602 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3603 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3606 put_record( $record, $out );
3611 Maasha::Common::run( "soap", "-r 2 -a $tmp_in -v $options->{ 'mismatches' } -g $options->{ 'gap_size'} -p $options->{ 'cpus' } -d $options->{ 'in_file' } -o $tmp_out > /dev/null 2>&1", 1 );
3615 $fh_out = Maasha::Common::read_open( $tmp_out );
3619 while ( $line = <$fh_out> )
3623 @fields = split /\t/, $line;
3625 $record->{ "REC_TYPE" } = "SOAP";
3626 $record->{ "Q_ID" } = $fields[ 0 ];
3627 $record->{ "SCORE" } = $fields[ 3 ];
3628 $record->{ "STRAND" } = $fields[ 6 ];
3629 $record->{ "S_ID" } = $fields[ 7 ];
3630 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is one based
3631 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3633 put_record( $record, $out );
3642 sub script_match_seq
3644 # Martin A. Hansen, August 2007.
3646 # BLATs sequences in stream against a given genome.
3648 my ( $in, # handle to in stream
3649 $out, # handle to out stream
3650 $options, # options hash
3655 my ( $record, @entries, $results );
3657 $options->{ "word_size" } ||= 20;
3658 $options->{ "direction" } ||= "both";
3660 while ( $record = get_record( $in ) )
3662 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3663 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3666 put_record( $record, $out );
3669 if ( @entries == 1 )
3671 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3673 map { put_record( $_, $out ) } @{ $results };
3675 elsif ( @entries == 2 )
3677 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3679 map { put_record( $_, $out ) } @{ $results };
3684 sub script_create_vmatch_index
3686 # Martin A. Hansen, January 2008.
3688 # Create a vmatch index from sequences in the stream.
3690 my ( $in, # handle to in stream
3691 $out, # handle to out stream
3692 $options, # options hash
3697 my ( $record, $file_tmp, $fh_tmp, $type );
3699 if ( $options->{ "index_name" } )
3701 $file_tmp = $options->{ 'index_name' };
3702 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3705 while ( $record = get_record( $in ) )
3707 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3709 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3711 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3714 put_record( $record, $out ) if not $options->{ "no_stream" };
3717 if ( $options->{ "index_name" } )
3721 if ( $type eq "protein" ) {
3722 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3724 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3732 sub script_vmatch_seq
3734 # Martin A. Hansen, August 2007.
3736 # Vmatches sequences in stream against a given genome.
3738 my ( $in, # handle to in stream
3739 $out, # handle to out stream
3740 $options, # options hash
3745 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3747 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3749 if ( $options->{ "index_name" } )
3751 @index_files = $options->{ "index_name" };
3755 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3757 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3759 @index_files = sort keys %hash;
3762 while ( $record = get_record( $in ) )
3764 push @records, $record;
3766 put_record( $record, $out );
3769 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3773 $fh_in = Maasha::Common::read_open( $result_file );
3775 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3776 put_record( $record, $out );
3781 unlink $result_file;
3785 sub script_write_fasta
3787 # Martin A. Hansen, August 2007.
3789 # Write FASTA entries from sequences in stream.
3791 my ( $in, # handle to in stream
3792 $out, # handle to out stream
3793 $options, # options hash
3798 my ( $record, $fh );
3800 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3802 while ( $record = get_record( $in ) )
3804 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3805 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3808 put_record( $record, $out ) if not $options->{ "no_stream" };
3815 sub script_write_align
3817 # Martin A. Hansen, August 2007.
3819 # Write pretty alignments aligned sequences in stream.
3821 my ( $in, # handle to in stream
3822 $out, # handle to out stream
3823 $options, # options hash
3828 my ( $fh, $record, @entries );
3830 $fh = write_stream( $options->{ "data_out" } ) ;
3832 while ( $record = get_record( $in ) )
3834 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3835 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3838 put_record( $record, $out ) if not $options->{ "no_stream" };
3841 if ( scalar( @entries ) == 2 ) {
3842 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3843 } elsif ( scalar ( @entries ) > 2 ) {
3844 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3851 sub script_write_blast
3853 # Martin A. Hansen, November 2007.
3855 # Write data in blast table format (-m8 and 9).
3857 my ( $in, # handle to in stream
3858 $out, # handle to out stream
3859 $options, # options hash
3864 my ( $fh, $record, $first );
3866 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3870 while ( $record = get_record( $in ) )
3872 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3874 if ( $options->{ "comment" } and $first )
3876 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";
3881 if ( $record->{ "STRAND" } eq "-" ) {
3882 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3885 print $fh join( "\t",
3886 $record->{ "Q_ID" },
3887 $record->{ "S_ID" },
3888 $record->{ "IDENT" },
3889 $record->{ "ALIGN_LEN" },
3890 $record->{ "MISMATCHES" },
3891 $record->{ "GAPS" },
3892 $record->{ "Q_BEG" } + 1,
3893 $record->{ "Q_END" } + 1,
3894 $record->{ "S_BEG" } + 1,
3895 $record->{ "S_END" } + 1,
3896 $record->{ "E_VAL" },
3897 $record->{ "BIT_SCORE" }
3901 put_record( $record, $out ) if not $options->{ "no_stream" };
3908 sub script_write_tab
3910 # Martin A. Hansen, August 2007.
3912 # Write data as table.
3914 my ( $in, # handle to in stream
3915 $out, # handle to out stream
3916 $options, # options hash
3921 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3923 $options->{ "delimit" } ||= "\t";
3925 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3927 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3929 while ( $record = get_record( $in ) )
3934 if ( $options->{ "keys" } )
3936 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3940 foreach $key ( @{ $options->{ "keys" } } )
3942 if ( exists $record->{ $key } )
3944 push @keys, $key if $options->{ "comment" };
3945 push @vals, $record->{ $key };
3952 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3954 next if exists $no_keys{ $key };
3956 push @keys, $key if $options->{ "comment" };
3957 push @vals, $record->{ $key };
3961 if ( @keys and $options->{ "comment" } )
3963 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3965 delete $options->{ "comment" };
3968 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3970 put_record( $record, $out ) if not $options->{ "no_stream" };
3977 sub script_write_bed
3979 # Martin A. Hansen, August 2007.
3981 # Write BED format for the UCSC genome browser using records in stream.
3983 my ( $in, # handle to in stream
3984 $out, # handle to out stream
3985 $options, # options hash
3990 my ( $fh, $record, $new_record );
3992 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3994 while ( $record = get_record( $in ) )
3996 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3998 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4000 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4002 $new_record->{ "CHR" } = $record->{ "S_ID" };
4003 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4004 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4005 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4006 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4007 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4009 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4011 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4013 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4015 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4017 $new_record->{ "CHR" } = $record->{ "S_ID" };
4018 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4019 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4020 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4021 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4022 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4024 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4026 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4028 $new_record->{ "CHR" } = $record->{ "S_ID" };
4029 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4030 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4031 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4032 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4033 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4035 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4037 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4039 $new_record->{ "CHR" } = $record->{ "S_ID" };
4040 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4041 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4042 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4043 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4044 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4046 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4048 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4050 Maasha::UCSC::bed_put_entry( $record, $fh );
4053 put_record( $record, $out ) if not $options->{ "no_stream" };
4060 sub script_write_psl
4062 # Martin A. Hansen, August 2007.
4064 # Write PSL output from stream.
4066 my ( $in, # handle to in stream
4067 $out, # handle to out stream
4068 $options, # options hash
4073 my ( $fh, $record, @output, $first );
4077 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4079 while ( $record = get_record( $in ) )
4081 put_record( $record, $out ) if not $options->{ "no_stream" };
4083 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4085 Maasha::UCSC::psl_put_header( $fh ) if $first;
4086 Maasha::UCSC::psl_put_entry( $record, $fh );
4095 sub script_write_fixedstep
4097 # Martin A. Hansen, Juli 2008.
4099 # Write fixedStep entries from recrods in the stream.
4101 my ( $in, # handle to in stream
4102 $out, # handle to out stream
4103 $options, # options hash
4108 my ( $fh, $record, $vals );
4110 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4112 while ( $record = get_record( $in ) )
4114 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4116 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4118 $vals = $record->{ 'VALS' };
4122 print $fh "$vals\n";
4125 put_record( $record, $out ) if not $options->{ "no_stream" };
4132 sub script_write_2bit
4134 # Martin A. Hansen, March 2008.
4136 # Write sequence entries from stream in 2bit format.
4138 my ( $in, # handle to in stream
4139 $out, # handle to out stream
4140 $options, # options hash
4145 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
4147 $mask = 1 if not $options->{ "no_mask" };
4149 $tmp_file = "$BP_TMP/write_2bit.fna";
4150 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4152 $fh_out = write_stream( $options->{ "data_out" } );
4154 while ( $record = get_record( $in ) )
4156 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4157 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
4160 put_record( $record, $out ) if not $options->{ "no_stream" };
4165 $fh_in = Maasha::Common::read_open( $tmp_file );
4167 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4176 sub script_write_solid
4178 # Martin A. Hansen, April 2008.
4180 # Write di-base encoded Solid sequence from entries in stream.
4182 my ( $in, # handle to in stream
4183 $out, # handle to out stream
4184 $options, # options hash
4189 my ( $record, $fh, $seq_cs );
4191 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4193 while ( $record = get_record( $in ) )
4195 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
4197 $seq_cs = Maasha::Solid::seq2color_space( uc $record->{ "SEQ" } );
4199 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
4202 put_record( $record, $out ) if not $options->{ "no_stream" };
4209 sub script_plot_seqlogo
4211 # Martin A. Hansen, August 2007.
4213 # Calculates and writes a sequence logo for alignments.
4215 my ( $in, # handle to in stream
4216 $out, # handle to out stream
4217 $options, # options hash
4222 my ( $record, @entries, $logo, $fh );
4224 while ( $record = get_record( $in ) )
4226 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4227 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4230 put_record( $record, $out ) if not $options->{ "no_stream" };
4233 $logo = Maasha::Plot::seq_logo( \@entries );
4235 $fh = write_stream( $options->{ "data_out" } );
4243 sub script_plot_phastcons_profiles
4245 # Martin A. Hansen, January 2008.
4247 # Plots PhastCons profiles.
4249 my ( $in, # handle to in stream
4250 $out, # handle to out stream
4251 $options, # options hash
4256 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4258 $options->{ "title" } ||= "PhastCons Profiles";
4260 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4261 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4263 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4264 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4266 while ( $record = get_record( $in ) )
4268 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4270 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4272 push @{ $AoA }, [ @{ $scores } ];
4275 put_record( $record, $out ) if not $options->{ "no_stream" };
4278 Maasha::UCSC::phastcons_normalize( $AoA );
4280 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4281 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4283 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4285 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4287 $fh = write_stream( $options->{ "data_out" } );
4289 print $fh "$_\n" foreach @{ $plot };
4295 sub script_analyze_bed
4297 # Martin A. Hansen, March 2008.
4299 # Analyze BED entries in stream.
4301 my ( $in, # handle to in stream
4302 $out, # handle to out stream
4303 $options, # options hash
4310 while ( $record = get_record( $in ) )
4312 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4314 put_record( $record, $out );
4319 sub script_analyze_vals
4321 # Martin A. Hansen, August 2007.
4323 # Analyze values for given keys in stream.
4325 my ( $in, # handle to in stream
4326 $out, # handle to out stream
4327 $options, # options hash
4332 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4334 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4336 while ( $record = get_record( $in ) )
4338 foreach $key ( keys %{ $record } )
4340 next if $options->{ "keys" } and not exists $key_hash{ $key };
4342 $analysis->{ $key }->{ "COUNT" }++;
4344 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4346 $analysis->{ $key }->{ "TYPE" } = "num";
4347 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4348 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4349 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4353 $len = length $record->{ $key };
4355 $analysis->{ $key }->{ "TYPE" } = "alph";
4356 $analysis->{ $key }->{ "SUM" } += $len;
4357 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4358 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4362 put_record( $record, $out ) if not $options->{ "no_stream" };
4365 foreach $key ( keys %{ $analysis } )
4367 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4368 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4371 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4381 if ( $options->{ "keys" } ) {
4382 @keys = @{ $options->{ "keys" } };
4384 @keys = keys %{ $analysis };
4387 foreach $key ( @keys )
4389 $keys .= sprintf "% 15s", $key;
4390 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4391 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4392 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4393 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4394 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4395 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4398 print $out "$keys\n";
4399 print $out "$types\n";
4400 print $out "$counts\n";
4401 print $out "$mins\n";
4402 print $out "$maxs\n";
4403 print $out "$sums\n";
4404 print $out "$means\n";
4408 sub script_head_records
4410 # Martin A. Hansen, August 2007.
4412 # Display the first sequences in stream.
4414 my ( $in, # handle to in stream
4415 $out, # handle to out stream
4416 $options, # options hash
4421 my ( $record, $count );
4423 $options->{ "num" } ||= 10;
4427 while ( $record = get_record( $in ) )
4431 put_record( $record, $out );
4433 last if $count == $options->{ "num" };
4438 sub script_remove_keys
4440 # Martin A. Hansen, August 2007.
4442 # Remove keys from stream.
4444 my ( $in, # handle to in stream
4445 $out, # handle to out stream
4446 $options, # options hash
4451 my ( $record, $new_record );
4453 while ( $record = get_record( $in ) )
4455 if ( $options->{ "keys" } )
4457 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4459 elsif ( $options->{ "save_keys" } )
4461 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4463 $record = $new_record;
4466 put_record( $record, $out ) if keys %{ $record };
4471 sub script_rename_keys
4473 # Martin A. Hansen, August 2007.
4475 # Rename keys in stream.
4477 my ( $in, # handle to in stream
4478 $out, # handle to out stream
4479 $options, # options hash
4486 while ( $record = get_record( $in ) )
4488 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4490 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4492 delete $record->{ $options->{ "keys" }->[ 0 ] };
4495 put_record( $record, $out );
4500 sub script_uniq_vals
4502 # Martin A. Hansen, August 2007.
4504 # Find unique values in stream.
4506 my ( $in, # handle to in stream
4507 $out, # handle to out stream
4508 $options, # options hash
4513 my ( %hash, $record );
4515 while ( $record = get_record( $in ) )
4517 if ( $record->{ $options->{ "key" } } )
4519 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4521 put_record( $record, $out );
4523 $hash{ $record->{ $options->{ "key" } } } = 1;
4525 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4527 put_record( $record, $out );
4531 $hash{ $record->{ $options->{ "key" } } } = 1;
4536 put_record( $record, $out );
4542 sub script_merge_vals
4544 # Martin A. Hansen, August 2007.
4546 # Rename keys in stream.
4548 my ( $in, # handle to in stream
4549 $out, # handle to out stream
4550 $options, # options hash
4555 my ( $record, @join, $i );
4557 $options->{ "delimit" } ||= '_';
4559 while ( $record = get_record( $in ) )
4561 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4563 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4565 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4566 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4569 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4572 put_record( $record, $out );
4579 # Martin A. Hansen, August 2007.
4581 # Grab for records in stream.
4583 my ( $in, # handle to in stream
4584 $out, # handle to out stream
4585 $options, # options hash
4590 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4592 if ( $options->{ "patterns" } )
4594 $patterns = [ split ",", $options->{ "patterns" } ];
4596 elsif ( -f $options->{ "patterns_in" } )
4598 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4600 elsif ( -f $options->{ "exact_in" } )
4602 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4604 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4609 if ( $options->{ "eval" } )
4611 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4619 while ( $record = get_record( $in ) )
4625 if ( $options->{ "keys" } )
4627 foreach $key ( @{ $options->{ "keys" } } )
4629 if ( exists $lookup_hash{ $record->{ $key } } )
4638 foreach $key ( keys %{ $record } )
4640 if ( not $options->{ "vals_only" } )
4642 if ( exists $lookup_hash{ $key } )
4649 if ( not $options->{ "keys_only" } )
4651 if ( exists $lookup_hash{ $record->{ $key } } )
4662 foreach $pattern ( @{ $patterns } )
4664 if ( $options->{ "keys" } )
4666 foreach $key ( @{ $options->{ "keys" } } )
4668 $pos = index $record->{ $key }, $pattern;
4670 goto FOUND if $pos >= 0;
4675 foreach $key ( keys %{ $record } )
4677 if ( not $options->{ "vals_only" } )
4679 $pos = index $key, $pattern;
4681 goto FOUND if $pos >= 0;
4684 if ( not $options->{ "keys_only" } )
4686 $pos = index $record->{ $key }, $pattern;
4688 goto FOUND if $pos >= 0;
4694 elsif ( $options->{ "regex" } )
4696 if ( $options->{ "keys" } )
4698 foreach $key ( @{ $options->{ "keys" } } )
4700 if ( $options->{ "case_insensitive" } ) {
4701 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4703 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4706 goto FOUND if $pos >= 0;
4711 foreach $key ( keys %{ $record } )
4713 if ( not $options->{ "vals_only" } )
4715 if ( $options->{ "case_insensitive" } ) {
4716 $pos = 1 if $key =~ /$options->{'regex'}/i;
4718 $pos = 1 if $key =~ /$options->{'regex'}/;
4721 goto FOUND if $pos >= 0;
4724 if ( not $options->{ "keys_only" } )
4726 if ( $options->{ "case_insensitive" } ) {
4727 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4729 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4732 goto FOUND if $pos >= 0;
4737 elsif ( $options->{ "eval" } )
4739 if ( defined $record->{ $key } )
4741 if ( $op eq "<" and $record->{ $key } < $val ) {
4742 $pos = 1 and goto FOUND;
4743 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4744 $pos = 1 and goto FOUND;
4745 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4746 $pos = 1 and goto FOUND;
4747 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4748 $pos = 1 and goto FOUND;
4749 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4750 $pos = 1 and goto FOUND;
4751 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4752 $pos = 1 and goto FOUND;
4753 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4754 $pos = 1 and goto FOUND;
4755 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4756 $pos = 1 and goto FOUND;
4763 if ( $pos >= 0 and not $options->{ "invert" } ) {
4764 put_record( $record, $out );
4765 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4766 put_record( $record, $out );
4774 # Martin A. Hansen, August 2007.
4776 # Evaluate extression for records in stream.
4778 my ( $in, # handle to in stream
4779 $out, # handle to out stream
4780 $options, # options hash
4785 my ( $record, $eval_key, $eval_val, $check, @keys );
4787 while ( $record = get_record( $in ) )
4789 if ( $options->{ "eval" } )
4791 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4799 @keys = split /\W+/, $eval_val;
4800 @keys = grep { ! /^\d+$/ } @keys;
4805 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4807 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4810 put_record( $record, $out );
4817 # Martin A. Hansen, June 2008.
4821 my ( $in, # handle to in stream
4822 $out, # handle to out stream
4823 $options, # options hash
4828 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4830 while ( $record = get_record( $in ) )
4834 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4836 push @rows, $record->{ $key };
4840 push @matrix, [ @rows ];
4845 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4847 foreach $row ( @matrix )
4849 for ( $i = 0; $i < @{ $row }; $i++ ) {
4850 $record->{ "V$i" } = $row->[ $i ];
4853 put_record( $record, $out );
4858 sub script_add_ident
4860 # Martin A. Hansen, May 2008.
4862 # Add a unique identifier to each record in stream.
4864 my ( $in, # handle to in stream
4865 $out, # handle to out stream
4866 $options, # options hash
4871 my ( $record, $key, $prefix, $i );
4873 $key = $options->{ "key" } || "ID";
4874 $prefix = $options->{ "prefix" } || "ID";
4878 while ( $record = get_record( $in ) )
4880 $record->{ $key } = sprintf( "$prefix%08d", $i );
4882 put_record( $record, $out );
4889 sub script_count_records
4891 # Martin A. Hansen, August 2007.
4893 # Count records in stream.
4895 my ( $in, # handle to in stream
4896 $out, # handle to out stream
4897 $options, # options hash
4902 my ( $record, $count, $result, $fh, $line );
4906 if ( $options->{ "no_stream" } )
4908 while ( $line = <$in> )
4912 $count++ if $line eq "---";
4917 while ( $record = get_record( $in ) )
4919 put_record( $record, $out );
4925 $result = { "count_records" => $count };
4927 $fh = write_stream( $options->{ "data_out" } );
4929 put_record( $result, $fh );
4935 sub script_random_records
4937 # Martin A. Hansen, August 2007.
4939 # Pick a number or random records from stream.
4941 my ( $in, # handle to in stream
4942 $out, # handle to out stream
4943 $options, # options hash
4948 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4950 $options->{ "num" } ||= 10;
4952 $tmp_file = "$BP_TMP/random_records.tmp";
4954 $fh_out = Maasha::Common::write_open( $tmp_file );
4958 while ( $record = get_record( $in ) )
4960 put_record( $record, $fh_out );
4970 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4972 while ( $i < $options->{ "num" } )
4974 $rand = int( rand( $count ) );
4976 if ( not exists $rand_hash{ $rand } )
4978 $rand_hash{ $rand } = 1;
4980 $max = $rand if $rand > $max;
4986 $fh_in = Maasha::Common::read_open( $tmp_file );
4990 while ( $record = get_record( $fh_in ) )
4992 put_record( $record, $out ) if exists $rand_hash{ $count };
4994 last if $count == $max;
5005 sub script_sort_records
5007 # Martin A. Hansen, August 2007.
5009 # Sort to sort records according to keys.
5011 my ( $in, # handle to in stream
5012 $out, # handle to out stream
5013 $options, # options hash
5018 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5020 foreach $key ( @{ $options->{ "keys" } } )
5022 if ( $key =~ s/n$// ) {
5023 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5025 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5029 $sort_str = join " or ", @sort_cmd;
5030 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5032 while ( $record = get_record( $in ) ) {
5033 push @records, $record;
5036 @records = sort $sort_sub @records;
5038 if ( $options->{ "reverse" } )
5040 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5041 put_record( $records[ $i ], $out );
5046 for ( $i = 0; $i < scalar @records; $i++ ) {
5047 put_record( $records[ $i ], $out );
5053 sub script_count_vals
5055 # Martin A. Hansen, August 2007.
5057 # Count records in stream.
5059 my ( $in, # handle to in stream
5060 $out, # handle to out stream
5061 $options, # options hash
5066 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5068 $tmp_file = "$BP_TMP/count_cache.tmp";
5070 $fh_out = Maasha::Common::write_open( $tmp_file );
5074 while ( $record = get_record( $in ) )
5076 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5078 push @records, $record;
5080 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5082 map { put_record( $_, $fh_out ) } @records;
5089 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5100 $fh_in = Maasha::Common::read_open( $tmp_file );
5102 while ( $record = get_record( $fh_in ) )
5104 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5106 put_record( $record, $out );
5108 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5116 foreach $record ( @records )
5118 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5120 put_record( $record, $out );
5127 sub script_plot_histogram
5129 # Martin A. Hansen, September 2007.
5131 # Plot a simple histogram for a given key using GNU plot.
5133 my ( $in, # handle to in stream
5134 $out, # handle to out stream
5135 $options, # options hash
5140 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5142 $options->{ "title" } ||= "Histogram";
5143 $options->{ "sort" } ||= "num";
5145 while ( $record = get_record( $in ) )
5147 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5149 put_record( $record, $out ) if not $options->{ "no_stream" };
5152 if ( $options->{ "sort" } eq "num" ) {
5153 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5155 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5158 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5160 $fh = write_stream( $options->{ "data_out" } );
5162 print $fh "$_\n" foreach @{ $result };
5168 sub script_plot_lendist
5170 # Martin A. Hansen, August 2007.
5172 # Plot length distribution using GNU plot.
5174 my ( $in, # handle to in stream
5175 $out, # handle to out stream
5176 $options, # options hash
5181 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5183 $options->{ "title" } ||= "Length Distribution";
5185 while ( $record = get_record( $in ) )
5187 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5189 put_record( $record, $out ) if not $options->{ "no_stream" };
5192 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5194 for ( $i = 0; $i < $max; $i++ ) {
5195 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5198 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5200 $fh = write_stream( $options->{ "data_out" } );
5202 print $fh "$_\n" foreach @{ $result };
5208 sub script_plot_chrdist
5210 # Martin A. Hansen, August 2007.
5212 # Plot chromosome distribution using GNU plot.
5214 my ( $in, # handle to in stream
5215 $out, # handle to out stream
5216 $options, # options hash
5221 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5223 $options->{ "title" } ||= "Chromosome Distribution";
5225 while ( $record = get_record( $in ) )
5227 if ( $record->{ "CHR" } ) { # generic
5228 $data_hash{ $record->{ "CHR" } }++;
5229 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5230 $data_hash{ $record->{ "S_ID" } }++;
5231 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5232 $data_hash{ $record->{ "S_ID" } }++;
5233 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5234 $data_hash{ $record->{ "S_ID" } }++;
5237 put_record( $record, $out ) if not $options->{ "no_stream" };
5240 foreach $elem ( keys %data_hash )
5244 $sort_key =~ s/chr//i;
5246 $sort_key =~ s/^X(.*)/99$1/;
5247 $sort_key =~ s/^Y(.*)/99$1/;
5248 $sort_key =~ s/^Z(.*)/999$1/;
5249 $sort_key =~ s/^M(.*)/9999$1/;
5250 $sort_key =~ s/^U(.*)/99999$1/;
5252 $count = $sort_key =~ tr/_//;
5254 $sort_key =~ s/_.*/"999999" x $count/ex;
5256 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5259 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5261 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5263 $fh = write_stream( $options->{ "data_out" } );
5265 print $fh "$_\n" foreach @{ $result };
5271 sub script_plot_karyogram
5273 # Martin A. Hansen, August 2007.
5275 # Plot hits on karyogram.
5277 my ( $in, # handle to in stream
5278 $out, # handle to out stream
5279 $options, # options hash
5284 my ( %options, $record, @data, $fh, $result, %data_hash );
5286 $options->{ "genome" } ||= "human";
5287 $options->{ "feat_color" } ||= "black";
5289 while ( $record = get_record( $in ) )
5291 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5293 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5296 put_record( $record, $out ) if not $options->{ "no_stream" };
5299 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5301 $fh = write_stream( $options->{ "data_out" } );
5309 sub script_plot_matches
5311 # Martin A. Hansen, August 2007.
5313 # Plot matches in 2D generating a dotplot.
5315 my ( $in, # handle to in stream
5316 $out, # handle to out stream
5317 $options, # options hash
5322 my ( $record, @data, $fh, $result, %data_hash );
5324 $options->{ "direction" } ||= "both";
5326 while ( $record = get_record( $in ) )
5328 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5329 push @data, $record;
5332 put_record( $record, $out ) if not $options->{ "no_stream" };
5335 $options->{ "title" } ||= "plot_matches";
5336 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5337 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5339 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5341 $fh = write_stream( $options->{ "data_out" } );
5343 print $fh "$_\n" foreach @{ $result };
5349 sub script_length_vals
5351 # Martin A. Hansen, August 2007.
5353 # Determine the length of the value for given keys.
5355 my ( $in, # handle to in stream
5356 $out, # handle to out stream
5357 $options, # options hash
5362 my ( $record, $key );
5364 while ( $record = get_record( $in ) )
5366 foreach $key ( @{ $options->{ "keys" } } )
5368 if ( $record->{ $key } ) {
5369 $record->{ $key . "_LEN" } = length $record->{ $key };
5373 put_record( $record, $out );
5380 # Martin A. Hansen, August 2007.
5382 # Calculates the sums for values of given keys.
5384 my ( $in, # handle to in stream
5385 $out, # handle to out stream
5386 $options, # options hash
5391 my ( $record, $key, %sum_hash, $fh );
5393 while ( $record = get_record( $in ) )
5395 foreach $key ( @{ $options->{ "keys" } } )
5397 if ( $record->{ $key } ) {
5398 $sum_hash{ $key } += $record->{ $key };
5402 put_record( $record, $out ) if not $options->{ "no_stream" };
5405 $fh = write_stream( $options->{ "data_out" } );
5407 foreach $key ( @{ $options->{ "keys" } } ) {
5408 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5415 sub script_mean_vals
5417 # Martin A. Hansen, August 2007.
5419 # Calculate the mean of values of given keys.
5421 my ( $in, # handle to in stream
5422 $out, # handle to out stream
5423 $options, # options hash
5428 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5430 while ( $record = get_record( $in ) )
5432 foreach $key ( @{ $options->{ "keys" } } )
5434 if ( $record->{ $key } )
5436 $sum_hash{ $key } += $record->{ $key };
5437 $count_hash{ $key }++;
5441 put_record( $record, $out ) if not $options->{ "no_stream" };
5444 $fh = write_stream( $options->{ "data_out" } );
5446 foreach $key ( @{ $options->{ "keys" } } )
5448 if ( $count_hash{ $key } ) {
5449 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5454 put_record( { $key . "_MEAN" => $mean } , $fh );
5461 sub script_median_vals
5463 # Martin A. Hansen, March 2008.
5465 # Calculate the median values of given keys.
5467 my ( $in, # handle to in stream
5468 $out, # handle to out stream
5469 $options, # options hash
5474 my ( $record, $key, %median_hash, $median, $fh );
5476 while ( $record = get_record( $in ) )
5478 foreach $key ( @{ $options->{ "keys" } } ) {
5479 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5482 put_record( $record, $out ) if not $options->{ "no_stream" };
5485 $fh = write_stream( $options->{ "data_out" } );
5487 foreach $key ( @{ $options->{ "keys" } } )
5489 if ( $median_hash{ $key } ) {
5490 $median = Maasha::Calc::median( $median_hash{ $key } );
5495 put_record( { $key . "_MEDIAN" => $median } , $fh );
5504 # Martin A. Hansen, February 2008.
5506 # Determine the maximum values of given keys.
5508 my ( $in, # handle to in stream
5509 $out, # handle to out stream
5510 $options, # options hash
5515 my ( $record, $key, $fh, %max_hash, $max_record );
5517 while ( $record = get_record( $in ) )
5519 foreach $key ( @{ $options->{ "keys" } } )
5521 if ( $record->{ $key } )
5523 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5527 put_record( $record, $out ) if not $options->{ "no_stream" };
5530 $fh = write_stream( $options->{ "data_out" } );
5532 foreach $key ( @{ $options->{ "keys" } } )
5534 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5537 put_record( $max_record, $fh );
5545 # Martin A. Hansen, February 2008.
5547 # Determine the minimum values of given keys.
5549 my ( $in, # handle to in stream
5550 $out, # handle to out stream
5551 $options, # options hash
5556 my ( $record, $key, $fh, %min_hash, $min_record );
5558 while ( $record = get_record( $in ) )
5560 foreach $key ( @{ $options->{ "keys" } } )
5562 if ( defined $record->{ $key } )
5564 if ( exists $min_hash{ $key } ) {
5565 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5567 $min_hash{ $key } = $record->{ $key };
5572 put_record( $record, $out ) if not $options->{ "no_stream" };
5575 $fh = write_stream( $options->{ "data_out" } );
5577 foreach $key ( @{ $options->{ "keys" } } )
5579 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5582 put_record( $min_record, $fh );
5588 sub script_upload_to_ucsc
5590 # Martin A. Hansen, August 2007.
5592 # Calculate the mean of values of given keys.
5594 my ( $in, # handle to in stream
5595 $out, # handle to out stream
5596 $options, # options hash
5601 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5602 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5604 $options->{ "short_label" } ||= $options->{ 'table' };
5605 $options->{ "long_label" } ||= $options->{ 'table' };
5606 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5607 $options->{ "priority" } ||= 1;
5608 $options->{ "visibility" } ||= "pack";
5609 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5610 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5612 $file = "$BP_TMP/ucsc_upload.tmp";
5620 if ( $options->{ 'wiggle' } )
5622 $options->{ "visibility" } = "full";
5624 while ( $record = get_record( $in ) )
5626 put_record( $record, $out ) if not $options->{ "no_stream" };
5628 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5629 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5630 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5632 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5634 $fh_out = $fh_hash{ $record->{ "CHR" } };
5636 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5639 map { close $_ } keys %fh_hash;
5641 $fh_out = Maasha::Common::write_open( $file );
5643 foreach $chr ( sort keys %fh_hash )
5645 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5647 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5651 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5653 $chr = $entry->{ 'CHR' };
5654 $beg = $entry->{ 'CHR_BEG' };
5655 $end = $entry->{ 'CHR_END' };
5656 $q_id = $entry->{ 'Q_ID' };
5658 if ( $q_id =~ /_(\d+)$/ ) {
5668 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5673 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5674 $block->[ $i ] += $clones;
5677 $max = Maasha::Calc::max( $max, $end );
5686 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5687 $block->[ $i ] += $clones;
5694 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5696 unlink "$BP_TMP/$chr";
5701 $wig_file = "$options->{ 'table' }.wig";
5702 $wib_file = "$options->{ 'table' }.wib";
5704 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5706 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5708 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5710 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5711 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5721 $fh_out = Maasha::Common::write_open( $file );
5723 while ( $record = get_record( $in ) )
5725 put_record( $record, $out ) if not $options->{ "no_stream" };
5727 if ( $record->{ "REC_TYPE" } eq "PSL" )
5729 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5730 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5734 $format = "PSL" if not $format;
5736 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5738 # chrom chromStart chromEnd name score strand size secStr conf
5740 print $fh_out join ( "\t",
5742 $record->{ "CHR_BEG" },
5743 $record->{ "CHR_END" } + 1,
5744 $record->{ "Q_ID" },
5745 $record->{ "SCORE" },
5746 $record->{ "STRAND" },
5747 $record->{ "SIZE" },
5748 $record->{ "SEC_STRUCT" },
5749 $record->{ "CONF" },
5752 $format = "BED_SS" if not $format;
5754 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5756 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5758 $format = "BED" if not $format;
5759 $columns = $record->{ "BED_COLS" } if not $columns;
5761 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5763 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5765 $format = "BED" if not $format;
5766 $columns = 6 if not $columns;
5768 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5770 $record->{ "CHR" } = $record->{ "S_ID" };
5771 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5772 $record->{ "CHR_END" } = $record->{ "S_END" };
5773 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5775 $format = "BED" if not $format;
5776 $columns = 6 if not $columns;
5778 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5780 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5782 $record->{ "CHR" } = $record->{ "S_ID" };
5783 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5784 $record->{ "CHR_END" } = $record->{ "S_END" };
5785 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5786 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5788 $format = "BED" if not $format;
5789 $columns = 6 if not $columns;
5791 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5794 if ( $i == $options->{ "chunk_size" } )
5798 if ( $format eq "BED" ) {
5799 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5800 } elsif ( $format eq "PSL" ) {
5801 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5810 $fh_out = Maasha::Common::write_open( $file );
5819 if ( exists $options->{ "database" } and $options->{ "table" } )
5821 if ( $format eq "BED" )
5823 $type = "bed $columns";
5825 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5827 elsif ( $format eq "BED_SS" )
5829 $options->{ "sec_struct" } = 1;
5831 $type = "sec_struct";
5833 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5835 elsif ( $format eq "PSL" )
5839 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5841 elsif ( $format eq "WIGGLE" )
5845 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5850 Maasha::UCSC::update_my_tracks( $options, $type );
5855 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5860 # Martin A. Hansen, July 2007.
5862 # Opens a stream to STDIN or a file,
5864 my ( $path, # path - OPTIONAL
5867 # Returns filehandle.
5871 if ( not -t STDIN ) {
5872 $fh = Maasha::Common::read_stdin();
5873 } elsif ( not $path ) {
5874 # Maasha::Common::error( qq(no data stream) );
5876 $fh = Maasha::Common::read_open( $path );
5879 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5887 # Martin A. Hansen, August 2007.
5889 # Opens a stream to STDOUT or a file.
5891 my ( $path, # path - OPTIONAL
5892 $gzip, # compress data - OPTIONAL
5895 # Returns filehandle.
5900 $fh = Maasha::Common::write_open( $path, $gzip );
5902 $fh = Maasha::Common::write_stdout();
5911 # Martin A. Hansen, July 2007.
5913 # Reads one record at a time and converts that record
5914 # to a Perl data structure (a hash) which is returned.
5919 # Returns data structure.
5921 my ( $block, @lines, $line, $key, $value, %record );
5923 local $/ = "\n---\n";
5929 return if not defined $block;
5931 @lines = split "\n", $block;
5933 foreach $line ( @lines )
5935 ( $key, $value ) = split ": ", $line, 2;
5937 $record{ $key } = $value;
5940 return wantarray ? %record : \%record;
5946 # Martin A. Hansen, July 2007.
5948 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5950 my ( $data, # data structure
5951 $fh, # file handle - OPTIONAL
5956 if ( scalar keys %{ $data } )
5960 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5965 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5976 # Martin A. Hansen, November 2007.
5978 # Extracts files from an explicit GetOpt::Long argument
5979 # allowing for the use of glob. E.g.
5980 # --data_in=test.fna
5981 # --data_in=test.fna,test2.fna
5983 # --data_in=test.fna,/dir/*.fna
5985 my ( $option, # option from GetOpt::Long
5990 my ( $elem, @files );
5992 foreach $elem ( split ",", $option )
5996 } elsif ( $elem =~ /\*/ ) {
5997 push @files, glob( $elem );
6001 return wantarray ? @files : \@files;
6007 # Martin A. Hansen, April 2008.
6009 # Removes temporary directory and exits gracefully.
6010 # This subroutine is meant to be run always as the last
6011 # thing even if a script is dies or is interrupted
6014 my ( $sig, # signal from the %SIG
6017 # print STDERR "signal->$sig<-\n";
6025 if ( $sig =~ /MAASHA_ERROR/ ) {
6026 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6027 } elsif ( $sig eq "INT" ) {
6028 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6029 } elsif ( $sig eq "TERM" ) {
6030 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6032 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6035 # This is a really bad solution, potentially, anyone can include this module and set
6036 # the BP_TMP to point at any dir and thus take out the machine !!!
6038 Maasha::Common::dir_remove( $BP_TMP );
6047 # This is a really bad solution, potentially, anyone can include this module and set
6048 # the BP_TMP to point at any dir and thus take out the machine !!!
6050 Maasha::Common::dir_remove( $BP_TMP );
6054 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6061 sub script_read_soft
6063 # Martin A. Hansen, December 2007.
6066 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
6068 my ( $in, # handle to in stream
6069 $out, # handle to out stream
6070 $options, # options hash
6075 my ( $data_in, $file, $num, $records, $record );
6077 while ( $record = get_record( $in ) ) {
6078 put_record( $record, $out );
6083 foreach $file ( @{ $options->{ "files" } } )
6085 $records = Maasha::NCBI::soft_parse( $file );
6087 foreach $record ( @{ $records } )
6089 put_record( $record, $out );
6091 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
6099 close $data_in if $data_in;