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 $options->{ "SCRIPT" } = $script;
166 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
167 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
170 $in = read_stream( $options->{ "stream_in" } );
171 $out = write_stream( $options->{ "stream_out" } );
173 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
174 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
175 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
176 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
177 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
178 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
179 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
180 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
181 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
182 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
183 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
184 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
185 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
186 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
187 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
188 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
189 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
190 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
191 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
192 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
193 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
194 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
195 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
196 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
197 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
198 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
199 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
200 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
201 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
202 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
203 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
204 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
205 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
206 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
207 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
209 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
211 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
212 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
213 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
214 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
215 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
216 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
217 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
218 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
219 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
220 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
221 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
222 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
223 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
224 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
225 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
226 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
227 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
228 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
229 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
230 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
231 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
232 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
233 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
234 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
235 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
236 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
237 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
238 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
239 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
240 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
241 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
242 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
243 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
244 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
245 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
246 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
247 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
248 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
249 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
250 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
251 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
252 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
253 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
254 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
255 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
256 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
257 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
258 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
259 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
260 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
261 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
262 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
264 close $in if defined $in;
271 # Martin A. Hansen, February 2008.
273 # Gets options from commandline and checks these vigerously.
275 my ( $script, # name of script
280 my ( %options, @options, $opt, @genomes );
282 if ( $script eq "print_usage" )
288 elsif ( $script eq "read_fasta" )
295 elsif ( $script eq "read_tab" )
306 elsif ( $script eq "read_psl" )
313 elsif ( $script eq "read_bed" )
320 elsif ( $script eq "read_fixedstep" )
327 elsif ( $script eq "read_blast_tab" )
334 elsif ( $script eq "read_embl" )
344 elsif ( $script eq "read_stockholm" )
351 elsif ( $script eq "read_phastcons" )
362 elsif ( $script eq "read_soft" )
369 elsif ( $script eq "read_gff" )
376 elsif ( $script eq "read_2bit" )
384 elsif ( $script eq "read_solexa" )
392 elsif ( $script eq "read_solid" )
400 elsif ( $script eq "read_mysql" )
409 elsif ( $script eq "format_genome" )
418 elsif ( $script eq "length_seq" )
425 elsif ( $script eq "oligo_freq" )
432 elsif ( $script eq "create_weight_matrix" )
438 elsif ( $script eq "transliterate_seq" )
446 elsif ( $script eq "transliterate_vals" )
455 elsif ( $script eq "translate_seq" )
461 elsif ( $script eq "extract_seq" )
469 elsif ( $script eq "get_genome_seq" )
481 elsif ( $script eq "get_genome_align" )
492 elsif ( $script eq "get_genome_phastcons" )
503 elsif ( $script eq "split_seq" )
510 elsif ( $script eq "split_bed" )
517 elsif ( $script eq "tile_seq" )
524 elsif ( $script eq "invert_align" )
530 elsif ( $script eq "patscan_seq" )
541 elsif ( $script eq "create_blast_db" )
548 elsif ( $script eq "blast_seq" )
560 elsif ( $script eq "blat_seq" )
572 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 # print STDERR Dumper( \%options );
943 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
944 return wantarray ? %options : \%options;
947 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
948 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
949 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
950 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
951 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
952 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
953 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
954 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
956 # ---- check arguments ----
958 if ( $options{ 'data_in' } )
960 $options{ "files" } = getopt_files( $options{ 'data_in' } );
962 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
965 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
967 # print STDERR Dumper( \%options );
969 foreach $opt ( keys %options )
971 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
973 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
975 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+$/ )
977 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
979 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
981 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
983 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
985 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
987 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
989 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
991 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
993 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
995 elsif ( $opt eq "genome" and $script ne "format_genome" )
997 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
998 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1000 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1001 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1004 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1006 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1008 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1010 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1014 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1015 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1016 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1017 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1018 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1019 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1020 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1021 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1022 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" };
1023 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1024 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" };
1026 if ( $script eq "upload_to_ucsc" )
1028 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1029 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1032 return wantarray ? %options : \%options;
1036 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1039 sub script_print_usage
1041 # Martin A. Hansen, January 2008.
1043 # Retrieves usage information from file and
1044 # prints this nicely formatted.
1046 my ( $in, # handle to in stream
1047 $out, # handle to out stream
1048 $options, # options hash
1053 my ( $file, $wiki, $lines );
1055 if ( $options->{ 'data_in' } ) {
1056 $file = $options->{ 'data_in' };
1058 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1061 $wiki = Maasha::Gwiki::gwiki_read( $file );
1063 if ( not $options->{ "help" } ) {
1064 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1067 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1069 print STDERR "$_\n" foreach @{ $lines };
1075 sub script_list_biopieces
1077 # Martin A. Hansen, January 2008.
1079 # Prints the synopsis from the usage for each of the biopieces.
1081 my ( $in, # handle to in stream
1082 $out, # handle to out stream
1083 $options, # options hash
1088 my ( @files, $file, $wiki, $program, $synopsis );
1090 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1092 foreach $file ( sort @files )
1094 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1098 $wiki = Maasha::Gwiki::gwiki_read( $file );
1100 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1101 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1103 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1104 $synopsis =~ s/!(\w)/$1/g;
1106 printf( "%-30s%s\n", $program, $synopsis );
1114 sub script_list_genomes
1116 # Martin A. Hansen, January 2008.
1118 # Prints the synopsis from the usage for each of the biopieces.
1120 my ( $in, # handle to in stream
1121 $out, # handle to out stream
1122 $options, # options hash
1127 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1129 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1131 foreach $genome ( @genomes )
1133 next if $genome =~ /\.$/;
1135 @formats = Maasha::Common::ls_dirs( $genome );
1137 foreach $format ( @formats )
1139 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1141 $hash{ $1 }{ $2 } = 1;
1150 map { push @row, $_ } sort keys %found;
1152 print join( "\t", @row ), "\n";
1154 foreach $genome ( sort keys %hash )
1158 foreach $format ( sort keys %found )
1160 if ( exists $hash{ $genome }{ $format } ) {
1167 print join( "\t", @row ), "\n";
1172 sub script_read_fasta
1174 # Martin A. Hansen, August 2007.
1176 # Read sequences from FASTA file.
1178 my ( $in, # handle to in stream
1179 $out, # handle to out stream
1180 $options, # options hash
1185 my ( $record, $file, $data_in, $entry, $num );
1187 while ( $record = get_record( $in ) ) {
1188 put_record( $record, $out );
1193 foreach $file ( @{ $options->{ "files" } } )
1195 $data_in = Maasha::Common::read_open( $file );
1197 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1199 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1202 SEQ_NAME => $entry->[ SEQ_NAME ],
1203 SEQ => $entry->[ SEQ ],
1204 SEQ_LEN => length $entry->[ SEQ ],
1207 put_record( $record, $out );
1210 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1220 close $data_in if $data_in;
1226 # Martin A. Hansen, August 2007.
1228 # Read table or table columns from stream or file.
1230 my ( $in, # handle to in stream
1231 $out, # handle to out stream
1232 $options, # options hash
1237 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1239 $options->{ 'delimit' } ||= '\s+';
1241 while ( $record = get_record( $in ) ) {
1242 put_record( $record, $out );
1245 $skip = $options->{ 'skip' } ||= 0;
1248 foreach $file ( @{ $options->{ "files" } } )
1250 $data_in = Maasha::Common::read_open( $file );
1252 while ( $line = <$data_in> )
1260 next if $line =~ /^#|^$/;
1267 @fields = split /$options->{'delimit'}/, $line;
1269 if ( $options->{ "cols" } ) {
1270 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1275 for ( $i = 0; $i < @fields2; $i++ )
1277 if ( $options->{ "keys" }->[ $i ] ) {
1278 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1280 $record->{ "V" . $i } = $fields2[ $i ];
1284 put_record( $record, $out );
1286 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1296 close $data_in if $data_in;
1302 # Martin A. Hansen, August 2007.
1304 # Read psl table from stream or file.
1306 my ( $in, # handle to in stream
1307 $out, # handle to out stream
1308 $options, # options hash
1313 my ( $record, @files, $file, $entries, $entry, $num );
1315 while ( $record = get_record( $in ) ) {
1316 put_record( $record, $out );
1321 foreach $file ( @{ $options->{ "files" } } )
1323 $entries = Maasha::UCSC::psl_get_entries( $file );
1325 foreach $entry ( @{ $entries } )
1327 put_record( $entry, $out );
1329 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1341 # Martin A. Hansen, August 2007.
1343 # Read bed table from stream or file.
1345 my ( $in, # handle to in stream
1346 $out, # handle to out stream
1347 $options, # options hash
1352 my ( $file, $record, $entry, $data_in, $num );
1354 while ( $record = get_record( $in ) ) {
1355 put_record( $record, $out );
1360 foreach $file ( @{ $options->{ "files" } } )
1362 $data_in = Maasha::Common::read_open( $file );
1364 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1366 put_record( $entry, $out );
1368 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1378 close $data_in if $data_in;
1382 sub script_read_fixedstep
1384 # Martin A. Hansen, Juli 2008.
1386 # Read fixedStep wiggle format from stream or file.
1388 my ( $in, # handle to in stream
1389 $out, # handle to out stream
1390 $options, # options hash
1395 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1397 while ( $record = get_record( $in ) ) {
1398 put_record( $record, $out );
1403 foreach $file ( @{ $options->{ "files" } } )
1405 $data_in = Maasha::Common::read_open( $file );
1407 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1409 $head = shift @{ $entry };
1411 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1413 $record->{ "CHR" } = $1;
1414 $record->{ "CHR_BEG" } = $2;
1415 $record->{ "STEP" } = $3;
1416 $record->{ "VALS" } = join ",", @{ $entry };
1419 put_record( $record, $out );
1421 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1431 close $data_in if $data_in;
1435 sub script_read_blast_tab
1437 # Martin A. Hansen, September 2007.
1439 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1441 my ( $in, # handle to in stream
1442 $out, # handle to out stream
1443 $options, # options hash
1448 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1450 while ( $record = get_record( $in ) ) {
1451 put_record( $record, $out );
1456 foreach $file ( @{ $options->{ "files" } } )
1458 $data_in = Maasha::Common::read_open( $file );
1460 while ( $line = <$data_in> )
1464 next if $line =~ /^#/;
1466 @fields = split /\t/, $line;
1468 $record->{ "REC_TYPE" } = "BLAST";
1469 $record->{ "Q_ID" } = $fields[ 0 ];
1470 $record->{ "S_ID" } = $fields[ 1 ];
1471 $record->{ "IDENT" } = $fields[ 2 ];
1472 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1473 $record->{ "MISMATCHES" } = $fields[ 4 ];
1474 $record->{ "GAPS" } = $fields[ 5 ];
1475 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1476 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1477 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1478 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1479 $record->{ "E_VAL" } = $fields[ 10 ];
1480 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1482 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1484 $record->{ "STRAND" } = '-';
1486 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1490 $record->{ "STRAND" } = '+';
1493 put_record( $record, $out );
1495 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1505 close $data_in if $data_in;
1509 sub script_read_embl
1511 # Martin A. Hansen, August 2007.
1515 my ( $in, # handle to in stream
1516 $out, # handle to out stream
1517 $options, # options hash
1522 my ( %options2, $file, $data_in, $num, $entry, $record );
1524 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1525 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1526 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1528 while ( $record = get_record( $in ) ) {
1529 put_record( $record, $out );
1534 foreach $file ( @{ $options->{ "files" } } )
1536 $data_in = Maasha::Common::read_open( $file );
1538 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1540 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1542 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1544 $record_copy = dclone $record;
1546 delete $record_copy->{ "FT" };
1548 put_record( $record_copy, $out );
1550 delete $record_copy->{ "SEQ" };
1552 foreach $feat ( keys %{ $record->{ "FT" } } )
1554 $record_copy->{ "FEAT_TYPE" } = $feat;
1556 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1558 foreach $qual ( keys %{ $feat2 } )
1560 $qual_val = join "; ", @{ $feat2->{ $qual } };
1565 $record_copy->{ $qual } = $qual_val;
1568 put_record( $record_copy, $out );
1572 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1582 close $data_in if $data_in;
1586 sub script_read_stockholm
1588 # Martin A. Hansen, August 2007.
1590 # Read Stockholm format.
1592 my ( $in, # handle to in stream
1593 $out, # handle to out stream
1594 $options, # options hash
1599 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1601 while ( $record = get_record( $in ) ) {
1602 put_record( $record, $out );
1607 foreach $file ( @{ $options->{ "files" } } )
1609 $data_in = Maasha::Common::read_open( $file );
1611 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1613 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1617 foreach $key ( keys %{ $record->{ "GF" } } ) {
1618 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1621 $record_anno->{ "ALIGN" } = $num;
1623 put_record( $record_anno, $out );
1625 foreach $seq ( @{ $record->{ "ALIGN" } } )
1627 undef $record_align;
1630 SEQ_NAME => $seq->[ 0 ],
1634 put_record( $record_align, $out );
1637 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1647 close $data_in if $data_in;
1651 sub script_read_phastcons
1653 # Martin A. Hansen, December 2007.
1655 # Read PhastCons format.
1657 my ( $in, # handle to in stream
1658 $out, # handle to out stream
1659 $options, # options hash
1664 my ( $data_in, $file, $num, $entry, @records, $record );
1666 $options->{ "min" } ||= 10;
1667 $options->{ "dist" } ||= 25;
1668 $options->{ "threshold" } ||= 0.8;
1669 $options->{ "gap" } ||= 5;
1671 while ( $record = get_record( $in ) ) {
1672 put_record( $record, $out );
1677 foreach $file ( @{ $options->{ "files" } } )
1679 $data_in = Maasha::Common::read_open( $file );
1681 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1683 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1685 foreach $record ( @records )
1687 $record->{ "REC_TYPE" } = "BED";
1688 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1690 put_record( $record, $out );
1692 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1703 close $data_in if $data_in;
1707 sub script_read_soft
1709 # Martin A. Hansen, December 2007.
1712 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1714 my ( $in, # handle to in stream
1715 $out, # handle to out stream
1716 $options, # options hash
1721 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1723 while ( $record = get_record( $in ) ) {
1724 put_record( $record, $out );
1729 foreach $file ( @{ $options->{ "files" } } )
1731 $soft_index = Maasha::NCBI::soft_index_file( $file );
1733 $fh = Maasha::Common::read_open( $file );
1735 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1737 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1739 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1741 $old_end = $platforms[ -1 ]->[ 2 ];
1743 foreach $sample ( @samples )
1745 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1747 foreach $record ( @{ $records } )
1749 put_record( $record, $out );
1751 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1756 $old_end = $sample->[ 2 ];
1764 close $data_in if $data_in;
1771 # Martin A. Hansen, February 2008.
1774 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1776 my ( $in, # handle to in stream
1777 $out, # handle to out stream
1778 $options, # options hash
1783 my ( $data_in, $file, $fh, $num, $record, $entry );
1785 while ( $record = get_record( $in ) ) {
1786 put_record( $record, $out );
1791 foreach $file ( @{ $options->{ "files" } } )
1793 $fh = Maasha::Common::read_open( $file );
1795 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1797 put_record( $entry, $out );
1799 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1809 close $data_in if $data_in;
1813 sub script_read_2bit
1815 # Martin A. Hansen, March 2008.
1817 # Read sequences from 2bit file.
1819 my ( $in, # handle to in stream
1820 $out, # handle to out stream
1821 $options, # options hash
1826 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1828 $mask = 1 if not $options->{ "no_mask" };
1830 while ( $record = get_record( $in ) ) {
1831 put_record( $record, $out );
1836 foreach $file ( @{ $options->{ "files" } } )
1838 $data_in = Maasha::Common::read_open( $file );
1840 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1842 foreach $line ( @{ $toc } )
1844 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1845 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1846 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1848 put_record( $record, $out );
1850 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1860 close $data_in if $data_in;
1864 sub script_read_solexa
1866 # Martin A. Hansen, March 2008.
1868 # Read Solexa sequence reads from file.
1870 my ( $in, # handle to in stream
1871 $out, # handle to out stream
1872 $options, # options hash
1877 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1879 $options->{ "quality" } ||= 20;
1881 while ( $record = get_record( $in ) ) {
1882 put_record( $record, $out );
1887 foreach $file ( @{ $options->{ "files" } } )
1889 $data_in = Maasha::Common::read_open( $file );
1890 $base_name = Maasha::Common::get_basename( $file );
1891 $base_name =~ s/\..*//;
1895 while ( $line = <$data_in> )
1897 @fields = split /:/, $line;
1898 @seqs = split //, $fields[ 5 ];
1899 @scores = split / /, $fields[ -1 ];
1901 for ( $i = 0; $i < @scores; $i++ ) {
1902 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1905 $seq = join "", @seqs;
1907 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1908 $record->{ "SEQ" } = $seq;
1909 $record->{ "SEQ_LEN" } = length $seq;
1910 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1912 put_record( $record, $out );
1914 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1925 close $data_in if $data_in;
1929 sub script_read_solid
1931 # Martin A. Hansen, April 2008.
1933 # Read Solid sequence from file.
1935 my ( $in, # handle to in stream
1936 $out, # handle to out stream
1937 $options, # options hash
1942 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1944 $options->{ "quality" } ||= 15;
1946 while ( $record = get_record( $in ) ) {
1947 put_record( $record, $out );
1952 foreach $file ( @{ $options->{ "files" } } )
1954 $data_in = Maasha::Common::read_open( $file );
1956 while ( $line = <$data_in> )
1960 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1962 @scores = split /,/, $seq_qual;
1963 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1965 for ( $i = 0; $i < @seqs; $i++ ) {
1966 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1970 SEQ_NAME => $seq_name,
1972 SEQ_QUAL => $seq_qual,
1973 SEQ_LEN => length $seq_cs,
1974 SEQ => join( "", @seqs ),
1975 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1978 put_record( $record, $out );
1980 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1990 close $data_in if $data_in;
1994 sub script_read_mysql
1996 # Martin A. Hansen, May 2008.
1998 # Read a MySQL query into stream.
2000 my ( $in, # handle to in stream
2001 $out, # handle to out stream
2002 $options, # options hash
2007 my ( $record, $dbh, $results );
2009 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2010 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2012 while ( $record = get_record( $in ) ) {
2013 put_record( $record, $out );
2016 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2018 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2020 Maasha::SQL::disconnect( $dbh );
2022 map { put_record( $_ ) } @{ $results };
2026 sub script_format_genome
2028 # Martin A. Hansen, Juli 2008.
2030 # Format a genome to speficed formats.
2032 my ( $in, # handle to in stream
2033 $out, # handle to out stream
2034 $options, # options hash
2039 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2041 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2042 $genome = $options->{ 'genome' };
2044 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2045 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2046 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2048 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2050 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2052 $fasta_dir = "$dir/genomes/$genome/fasta";
2056 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2058 $fasta_dir = "$dir/genomes/$genome/fasta";
2060 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2063 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2065 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2067 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2069 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2072 while ( $record = get_record( $in ) )
2074 if ( $fh_out and $entry = record2fasta( $record ) )
2076 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2078 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2080 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2082 $vals = $record->{ 'VALS' };
2086 print $fh_out "$vals\n";
2089 put_record( $record, $out ) if not $options->{ "no_stream" };
2092 foreach $format ( @{ $options->{ 'formats' } } )
2094 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2095 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2096 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2097 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2098 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2101 close $fh_out if $fh_out;
2105 sub script_length_seq
2107 # Martin A. Hansen, August 2007.
2109 # Determine the length of sequences in stream.
2111 my ( $in, # handle to in stream
2112 $out, # handle to out stream
2113 $options, # options hash
2118 my ( $record, $total );
2120 while ( $record = get_record( $in ) )
2122 if ( $record->{ "SEQ" } )
2124 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2125 $total += $record->{ "SEQ_LEN" };
2128 put_record( $record, $out ) if not $options->{ "no_stream" };
2131 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2135 sub script_uppercase_seq
2137 # Martin A. Hansen, August 2007.
2139 # Uppercases sequences in stream.
2141 my ( $in, # handle to in stream
2142 $out, # handle to out stream
2149 while ( $record = get_record( $in ) )
2151 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2153 put_record( $record, $out );
2158 sub script_shuffle_seq
2160 # Martin A. Hansen, December 2007.
2162 # Shuffle sequences in stream.
2164 my ( $in, # handle to in stream
2165 $out, # handle to out stream
2172 while ( $record = get_record( $in ) )
2174 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2176 put_record( $record, $out );
2181 sub script_analyze_seq
2183 # Martin A. Hansen, August 2007.
2185 # Analyze sequence composition of sequences in stream.
2187 my ( $in, # handle to in stream
2188 $out, # handle to out stream
2193 my ( $record, $analysis );
2195 while ( $record = get_record( $in ) )
2197 if ( $record->{ "SEQ" } )
2199 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2201 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2204 put_record( $record, $out );
2209 sub script_analyze_tags
2211 # Martin A. Hansen, August 2008.
2213 # Analyze sequence tags in stream.
2215 my ( $in, # handle to in stream
2216 $out, # handle to out stream
2221 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2223 while ( $record = get_record( $in ) )
2225 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2227 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2231 $len_hash{ length( $record->{ "SEQ" } ) }++;
2232 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2235 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2237 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2241 $len_hash{ $record->{ "BED_LEN" } }++;
2242 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2247 foreach $key ( sort { $a <=> $b } keys %len_hash )
2249 $tag_record->{ "TAG_LEN" } = $key;
2250 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2251 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2253 put_record( $tag_record, $out );
2258 sub script_complexity_seq
2260 # Martin A. Hansen, May 2008.
2262 # Generates an index calculated as the most common di-residue over
2263 # the sequence length for all sequences in stream.
2265 my ( $in, # handle to in stream
2266 $out, # handle to out stream
2271 my ( $record, $index );
2273 while ( $record = get_record( $in ) )
2275 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2277 put_record( $record, $out );
2282 sub script_oligo_freq
2284 # Martin A. Hansen, August 2007.
2286 # Determine the length of sequences in stream.
2288 my ( $in, # handle to in stream
2289 $out, # handle to out stream
2290 $options, # options hash
2295 my ( $record, %oligos, @freq_table );
2297 $options->{ "word_size" } ||= 7;
2299 while ( $record = get_record( $in ) )
2301 if ( $record->{ "SEQ" } )
2303 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2305 if ( not $options->{ "all" } )
2307 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2309 map { put_record( $_, $out ) } @freq_table;
2315 put_record( $record, $out );
2318 if ( $options->{ "all" } )
2320 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2322 map { put_record( $_, $out ) } @freq_table;
2327 sub script_create_weight_matrix
2329 # Martin A. Hansen, August 2007.
2331 # Creates a weight matrix from an alignmnet.
2333 my ( $in, # handle to in stream
2334 $out, # handle to out stream
2335 $options, # options hash
2340 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2344 while ( $record = get_record( $in ) )
2346 if ( $record->{ "SEQ" } )
2348 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2350 $res = substr $record->{ "SEQ" }, $i, 1;
2352 $freq_hash{ $i }{ $res }++;
2353 $res_hash{ $res } = 1;
2360 put_record( $record, $out );
2364 foreach $res ( sort keys %res_hash )
2368 $record->{ "V0" } = $res;
2370 for ( $i = 0; $i < keys %freq_hash; $i++ )
2372 $freq = $freq_hash{ $i }{ $res } || 0;
2374 if ( $options->{ "percent" } ) {
2375 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2378 $record->{ "V" . ( $i + 1 ) } = $freq;
2381 put_record( $record, $out );
2386 sub script_calc_bit_scores
2388 # Martin A. Hansen, March 2007.
2390 # Calculates the bit scores for each position from an alignmnet in the stream.
2392 my ( $in, # handle to in stream
2393 $out, # handle to out stream
2398 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2402 while ( $record = get_record( $in ) )
2404 if ( $record->{ "SEQ" } )
2406 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2408 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2410 $res = substr $record->{ "SEQ" }, $i, 1;
2412 next if $res =~ /-|_|~|\./;
2414 $freq_hash{ $i }{ $res }++;
2421 put_record( $record, $out );
2427 if ( $type eq "protein" ) {
2433 for ( $i = 0; $i < keys %freq_hash; $i++ )
2435 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2437 $bit_diff = $bit_max - $bit_height;
2439 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2442 put_record( $record, $out );
2446 sub script_reverse_seq
2448 # Martin A. Hansen, August 2007.
2450 # Reverse sequence in record.
2452 my ( $in, # handle to in stream
2453 $out, # handle to out stream
2460 while ( $record = get_record( $in ) )
2462 if ( $record->{ "SEQ" } ) {
2463 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2466 put_record( $record, $out );
2471 sub script_complement_seq
2473 # Martin A. Hansen, August 2007.
2475 # Complement sequence in record.
2477 my ( $in, # handle to in stream
2478 $out, # handle to out stream
2483 my ( $record, $type );
2485 while ( $record = get_record( $in ) )
2487 if ( $record->{ "SEQ" } )
2490 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2493 if ( $type eq "rna" ) {
2494 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2495 } elsif ( $type eq "dna" ) {
2496 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2500 put_record( $record, $out );
2505 sub script_remove_indels
2507 # Martin A. Hansen, August 2007.
2509 # Remove indels from sequences in stream.
2511 my ( $in, # handle to in stream
2512 $out, # handle to out stream
2519 while ( $record = get_record( $in ) )
2521 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2523 put_record( $record, $out );
2528 sub script_transliterate_seq
2530 # Martin A. Hansen, August 2007.
2532 # Transliterate chars from sequence in record.
2534 my ( $in, # handle to in stream
2535 $out, # handle to out stream
2536 $options, # options hash
2541 my ( $record, $search, $replace, $delete );
2543 $search = $options->{ "search" } || "";
2544 $replace = $options->{ "replace" } || "";
2545 $delete = $options->{ "delete" } || "";
2547 while ( $record = get_record( $in ) )
2549 if ( $record->{ "SEQ" } )
2551 if ( $search and $replace ) {
2552 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2553 } elsif ( $delete ) {
2554 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2558 put_record( $record, $out );
2563 sub script_transliterate_vals
2565 # Martin A. Hansen, April 2008.
2567 # Transliterate chars from values in record.
2569 my ( $in, # handle to in stream
2570 $out, # handle to out stream
2571 $options, # options hash
2576 my ( $record, $search, $replace, $delete, $key );
2578 $search = $options->{ "search" } || "";
2579 $replace = $options->{ "replace" } || "";
2580 $delete = $options->{ "delete" } || "";
2582 while ( $record = get_record( $in ) )
2584 foreach $key ( @{ $options->{ "keys" } } )
2586 if ( exists $record->{ $key } )
2588 if ( $search and $replace ) {
2589 eval "\$record->{ $key } =~ tr/$search/$replace/";
2590 } elsif ( $delete ) {
2591 eval "\$record->{ $key } =~ tr/$delete//d";
2596 put_record( $record, $out );
2601 sub script_translate_seq
2603 # Martin A. Hansen, February 2008.
2605 # Translate DNA sequence into protein sequence.
2607 my ( $in, # handle to in stream
2608 $out, # handle to out stream
2609 $options, # options hash
2614 my ( $record, $frame, %new_record );
2616 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2618 while ( $record = get_record( $in ) )
2620 if ( $record->{ "SEQ" } )
2622 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2624 foreach $frame ( @{ $options->{ "frames" } } )
2626 %new_record = %{ $record };
2628 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2629 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2630 $new_record{ "FRAME" } = $frame;
2632 put_record( \%new_record, $out );
2638 put_record( $record, $out );
2644 sub script_extract_seq
2646 # Martin A. Hansen, August 2007.
2648 # Extract subsequences from sequences in record.
2650 my ( $in, # handle to in stream
2651 $out, # handle to out stream
2652 $options, # options hash
2657 my ( $beg, $end, $len, $record );
2659 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2662 $beg = $options->{ "beg" } - 1; # correcting for start offset
2665 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2667 } elsif ( defined $options->{ "end" } ) {
2668 $end = $options->{ "end" } - 1; # correcting for start offset
2671 $len = $options->{ "len" };
2673 # print "beg->$beg, end->$end, len->$len\n";
2675 while ( $record = get_record( $in ) )
2677 if ( $record->{ "SEQ" } )
2679 if ( defined $beg and defined $end )
2681 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2682 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2684 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2687 elsif ( defined $beg and defined $len )
2689 if ( $len > length $record->{ "SEQ" } ) {
2690 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2692 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2695 elsif ( defined $beg )
2697 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2701 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2703 put_record( $record, $out );
2708 sub script_get_genome_seq
2710 # Martin A. Hansen, December 2007.
2712 # Gets a subsequence from a genome.
2714 my ( $in, # handle to in stream
2715 $out, # handle to out stream
2716 $options, # options hash
2721 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2723 $options->{ "flank" } ||= 0;
2725 if ( $options->{ "genome" } )
2727 $genome = $options->{ "genome" };
2729 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2730 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2732 $fh = Maasha::Common::read_open( $genome_file );
2733 $index = Maasha::Fasta::index_retrieve( $index_file );
2735 shift @{ $index }; # Get rid of the file size info
2737 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2739 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2741 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2743 $beg = $index_beg + $options->{ "beg" } - 1;
2745 if ( $options->{ "len" } ) {
2746 $len = $options->{ "len" };
2747 } elsif ( $options->{ "end" } ) {
2748 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2751 $beg -= $options->{ "flank" };
2752 $len += 2 * $options->{ "flank" };
2754 if ( $beg <= $index_beg )
2756 $len -= $index_beg - $beg;
2760 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2762 next if $beg > $index_beg + $index_len;
2764 $record->{ "CHR" } = $options->{ "chr" };
2765 $record->{ "CHR_BEG" } = $beg - $index_beg;
2766 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2768 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2769 $record->{ "SEQ_LEN" } = $len;
2771 put_record( $record, $out );
2775 while ( $record = get_record( $in ) )
2777 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2779 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2781 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2783 $beg = $record->{ "CHR_BEG" } + $index_beg;
2784 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2786 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2788 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2790 $beg = $record->{ "S_BEG" } + $index_beg;
2791 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2793 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2795 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2797 $beg = $record->{ "S_BEG" } + $index_beg;
2798 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2801 $beg -= $options->{ "flank" };
2802 $len += 2 * $options->{ "flank" };
2804 if ( $beg <= $index_beg )
2806 $len -= $index_beg - $beg;
2810 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2812 next if $beg > $index_beg + $index_len;
2814 $record->{ "CHR_BEG" } = $beg - $index_beg;
2815 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2817 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2819 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2821 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2822 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2825 if ( $options->{ "mask" } )
2827 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2829 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2831 @begs = split ",", $record->{ "Q_BEGS" };
2832 @lens = split ",", $record->{ "BLOCKSIZES" };
2834 for ( $i = 0; $i < @begs; $i++ ) {
2835 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2841 put_record( $record, $out );
2848 sub script_get_genome_align
2850 # Martin A. Hansen, April 2008.
2852 # Gets a subalignment from a multiple genome alignment.
2854 my ( $in, # handle to in stream
2855 $out, # handle to out stream
2856 $options, # options hash
2861 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2863 $options->{ "strand" } ||= "+";
2867 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2869 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2871 $beg = $options->{ "beg" } - 1;
2873 if ( $options->{ "end" } ) {
2874 $end = $options->{ "end" };
2875 } elsif ( $options->{ "len" } ) {
2876 $end = $beg + $options->{ "len" };
2879 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2881 foreach $entry ( @{ $align } )
2883 $entry->{ "CHR" } = $record->{ "CHR" };
2884 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2885 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2886 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2887 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2888 $entry->{ "SCORE" } = $record->{ "SCORE" };
2890 put_record( $entry, $out );
2894 while ( $record = get_record( $in ) )
2896 if ( $record->{ "REC_TYPE" } eq "BED" )
2898 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2900 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2902 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2904 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2906 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2909 foreach $entry ( @{ $align } )
2911 $entry->{ "CHR" } = $record->{ "CHR" };
2912 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2913 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2914 $entry->{ "STRAND" } = $record->{ "STRAND" };
2915 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2916 $entry->{ "SCORE" } = $record->{ "SCORE" };
2918 put_record( $entry, $out );
2926 sub script_get_genome_phastcons
2928 # Martin A. Hansen, February 2008.
2930 # Get phastcons scores from genome intervals.
2932 my ( $in, # handle to in stream
2933 $out, # handle to out stream
2934 $options, # options hash
2939 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2941 $options->{ "flank" } ||= 0;
2943 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2944 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2946 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2947 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2949 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2951 $options->{ "beg" } -= 1; # request is 1-based
2952 $options->{ "end" } -= 1; # request is 1-based
2954 if ( $options->{ "len" } ) {
2955 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2958 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2960 $record->{ "CHR" } = $options->{ "chr" };
2961 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2962 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2964 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2965 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2967 put_record( $record, $out );
2970 while ( $record = get_record( $in ) )
2972 if ( $record->{ "REC_TYPE" } eq "BED" )
2974 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2976 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2978 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2980 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2982 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2985 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2986 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2988 put_record( $record, $out );
2991 close $fh_phastcons if $fh_phastcons;
2997 # Martin A. Hansen, December 2007.
2999 # Folds sequences in stream into secondary structures.
3001 my ( $in, # handle to in stream
3002 $out, # handle to out stream
3007 my ( $record, $type, $struct, $index );
3009 while ( $record = get_record( $in ) )
3011 if ( $record->{ "SEQ" } )
3014 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3017 if ( $type ne "protein" )
3019 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3020 $record->{ "SEC_STRUCT" } = $struct;
3021 $record->{ "FREE_ENERGY" } = $index;
3022 $record->{ "SCORE" } = abs int $index;
3023 $record->{ "SIZE" } = length $struct;
3024 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3028 put_record( $record, $out );
3033 sub script_split_seq
3035 # Martin A. Hansen, August 2007.
3037 # Split a sequence in stream into words.
3039 my ( $in, # handle to in stream
3040 $out, # handle to out stream
3041 $options, # options hash
3046 my ( $record, $new_record, $i, $subseq, %lookup );
3048 $options->{ "word_size" } ||= 7;
3050 while ( $record = get_record( $in ) )
3052 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3054 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3056 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3058 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3060 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3061 $new_record->{ "SEQ" } = $subseq;
3063 put_record( $new_record, $out );
3065 $lookup{ $subseq } = 1;
3069 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3070 $new_record->{ "SEQ" } = $subseq;
3072 put_record( $new_record, $out );
3078 put_record( $record, $out );
3084 sub script_split_bed
3086 # Martin A. Hansen, June 2008.
3088 # Split a BED record into overlapping windows.
3090 my ( $in, # handle to in stream
3091 $out, # handle to out stream
3092 $options, # options hash
3097 my ( $record, $new_record, $i );
3099 $options->{ "window_size" } ||= 20;
3100 $options->{ "step_size" } ||= 1;
3102 while ( $record = get_record( $in ) )
3104 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3106 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3108 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3110 $new_record->{ "REC_TYPE" } = "BED";
3111 $new_record->{ "CHR" } = $record->{ "CHR" };
3112 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3113 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3114 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3115 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3116 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3117 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3119 put_record( $new_record, $out );
3124 put_record( $record, $out );
3130 sub script_align_seq
3132 # Martin A. Hansen, August 2007.
3134 # Align sequences in stream.
3136 my ( $in, # handle to in stream
3137 $out, # handle to out stream
3142 my ( $record, @entries, $entry );
3144 while ( $record = get_record( $in ) )
3146 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3147 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3148 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3149 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3151 put_record( $record, $out );
3155 @entries = Maasha::Align::align( \@entries );
3157 foreach $entry ( @entries )
3159 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3162 SEQ_NAME => $entry->[ SEQ_NAME ],
3163 SEQ => $entry->[ SEQ ],
3166 put_record( $record, $out );
3174 # Martin A. Hansen, February 2008.
3176 # Using the first sequence in stream as reference, tile
3177 # all subsequent sequences based on pairwise alignments.
3179 my ( $in, # handle to in stream
3180 $out, # handle to out stream
3181 $options, # options hash
3186 my ( $record, $first, $ref_entry, @entries );
3190 while ( $record = get_record( $in ) )
3192 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3196 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3202 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3207 put_record( $record, $out );
3211 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3213 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3217 sub script_invert_align
3219 # Martin A. Hansen, February 2008.
3221 # Inverts an alignment showing only non-mathing residues
3222 # using the first sequence as reference.
3224 my ( $in, # handle to in stream
3225 $out, # handle to out stream
3226 $options, # options hash
3231 my ( $record, @entries );
3233 while ( $record = get_record( $in ) )
3235 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3237 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3241 put_record( $record, $out );
3245 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3247 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3251 sub script_patscan_seq
3253 # Martin A. Hansen, August 2007.
3255 # Locates patterns in sequences using scan_for_matches.
3257 my ( $in, # handle to in stream
3258 $out, # handle to out stream
3259 $options, # options hash
3264 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3266 if ( $options->{ "patterns" } ) {
3267 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3268 } elsif ( -f $options->{ "patterns_in" } ) {
3269 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3272 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3274 push @args, "-c" if $options->{ "comp" };
3275 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3276 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3278 $seq_file = "$BP_TMP/patscan.seq";
3279 $pat_file = "$BP_TMP/patscan.pat";
3280 $out_file = "$BP_TMP/patscan.out";
3282 $fh_out = Maasha::Common::write_open( $seq_file );
3286 while ( $record = get_record( $in ) )
3288 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3290 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3292 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3294 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3302 $arg = join " ", @args;
3303 $arg .= " -p" if $type eq "protein";
3305 foreach $pattern ( @{ $patterns } )
3307 $fh_out = Maasha::Common::write_open( $pat_file );
3309 print $fh_out "$pattern\n";
3313 if ( $options->{ 'genome' } ) {
3314 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3315 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3317 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3318 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3321 $fh_in = Maasha::Common::read_open( $out_file );
3323 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3325 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3327 if ( $options->{ 'genome' } )
3329 $result->{ "CHR" } = $result->{ "S_ID" };
3330 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3331 $result->{ "CHR_END" } = $result->{ "S_END" };
3333 delete $result->{ "S_ID" };
3334 delete $result->{ "S_BEG" };
3335 delete $result->{ "S_END" };
3339 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3342 put_record( $result, $out );
3354 sub script_create_blast_db
3356 # Martin A. Hansen, September 2007.
3358 # Creates a NCBI BLAST database with formatdb
3360 my ( $in, # handle to in stream
3361 $out, # handle to out stream
3362 $options, # options hash
3367 my ( $fh, $seq_type, $path, $record, $entry );
3369 $path = $options->{ "database" };
3371 $fh = Maasha::Common::write_open( $path );
3373 while ( $record = get_record( $in ) )
3375 put_record( $record, $out ) if not $options->{ "no_stream" };
3377 if ( $entry = record2fasta( $record ) )
3379 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3381 Maasha::Fasta::put_entry( $entry, $fh );
3387 if ( $seq_type eq "protein" ) {
3388 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3390 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3397 sub script_blast_seq
3399 # Martin A. Hansen, September 2007.
3401 # BLASTs sequences in stream against a given database.
3403 my ( $in, # handle to in stream
3404 $out, # handle to out stream
3405 $options, # options hash
3410 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3412 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3413 $options->{ "filter" } = "F";
3414 $options->{ "filter" } = "T" if $options->{ "filter" };
3415 $options->{ "cpus" } ||= 1;
3417 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3419 $tmp_in = "$BP_TMP/blast_query.seq";
3420 $tmp_out = "$BP_TMP/blast.result";
3422 $fh_out = Maasha::Common::write_open( $tmp_in );
3424 while ( $record = get_record( $in ) )
3426 if ( $entry = record2fasta( $record ) )
3428 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3430 Maasha::Fasta::put_entry( $entry, $fh_out );
3433 put_record( $record, $out );
3438 if ( -f $options->{ 'database' } . ".phr" ) {
3439 $s_type = "protein";
3441 $s_type = "nucleotide";
3444 if ( not $options->{ 'program' } )
3446 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3447 $options->{ 'program' } = "blastn";
3448 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3449 $options->{ 'program' } = "blastp";
3450 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3451 $options->{ 'program' } = "blastx";
3452 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3453 $options->{ 'program' } = "tblastn";
3457 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 );
3461 $fh_out = Maasha::Common::read_open( $tmp_out );
3465 while ( $line = <$fh_out> )
3469 next if $line =~ /^#/;
3471 @fields = split /\s+/, $line;
3473 $record->{ "REC_TYPE" } = "BLAST";
3474 $record->{ "Q_ID" } = $fields[ 0 ];
3475 $record->{ "S_ID" } = $fields[ 1 ];
3476 $record->{ "IDENT" } = $fields[ 2 ];
3477 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3478 $record->{ "MISMATCHES" } = $fields[ 4 ];
3479 $record->{ "GAPS" } = $fields[ 5 ];
3480 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3481 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3482 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3483 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3484 $record->{ "E_VAL" } = $fields[ 10 ];
3485 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3487 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3489 $record->{ "STRAND" } = '-';
3491 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3495 $record->{ "STRAND" } = '+';
3498 put_record( $record, $out );
3509 # Martin A. Hansen, August 2007.
3511 # BLATs sequences in stream against a given genome.
3513 my ( $in, # handle to in stream
3514 $out, # handle to out stream
3515 $options, # options hash
3520 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3522 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3524 $options->{ 'tile_size' } ||= 11;
3525 $options->{ 'one_off' } ||= 0;
3526 $options->{ 'min_identity' } ||= 90;
3527 $options->{ 'min_score' } ||= 0;
3528 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3530 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3531 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3532 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3533 $blat_args .= " -minScore=$options->{ 'min_score' }";
3534 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3535 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3537 $query_file = "$BP_TMP/blat.seq";
3539 $fh_out = Maasha::Common::write_open( $query_file );
3541 while ( $record = get_record( $in ) )
3543 if ( $entry = record2fasta( $record ) )
3545 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3546 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3549 put_record( $record, $out );
3554 $blat_args .= " -t=dnax" if $type eq "protein";
3555 $blat_args .= " -q=$type";
3557 $result_file = "$BP_TMP/blat.psl";
3559 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3563 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3565 map { put_record( $_, $out ) } @{ $entries };
3567 unlink $result_file;
3573 # Martin A. Hansen, July 2008.
3575 # soap sequences in stream against a given file or genome.
3577 my ( $in, # handle to in stream
3578 $out, # handle to out stream
3579 $options, # options hash
3584 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3586 $options->{ "seed_size" } ||= 10;
3587 $options->{ "mismatches" } ||= 2;
3588 $options->{ "gap_size" } ||= 0;
3589 $options->{ "cpus" } ||= 1;
3591 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3593 $tmp_in = "$BP_TMP/soap_query.seq";
3594 $tmp_out = "$BP_TMP/soap.result";
3596 $fh_out = Maasha::Common::write_open( $tmp_in );
3598 while ( $record = get_record( $in ) )
3600 if ( $entry = record2fasta( $record ) ) {
3601 Maasha::Fasta::put_entry( $entry, $fh_out );
3604 put_record( $record, $out );
3609 Maasha::Common::run( "soap", "-s $options->{ 'seed_size' } -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 );
3613 $fh_out = Maasha::Common::read_open( $tmp_out );
3617 while ( $line = <$fh_out> )
3621 @fields = split /\t/, $line;
3623 $record->{ "REC_TYPE" } = "SOAP";
3624 $record->{ "Q_ID" } = $fields[ 0 ];
3625 $record->{ "SCORE" } = $fields[ 3 ];
3626 $record->{ "STRAND" } = $fields[ 6 ];
3627 $record->{ "S_ID" } = $fields[ 7 ];
3628 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3629 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3631 put_record( $record, $out );
3640 sub script_match_seq
3642 # Martin A. Hansen, August 2007.
3644 # BLATs sequences in stream against a given genome.
3646 my ( $in, # handle to in stream
3647 $out, # handle to out stream
3648 $options, # options hash
3653 my ( $record, @entries, $results );
3655 $options->{ "word_size" } ||= 20;
3656 $options->{ "direction" } ||= "both";
3658 while ( $record = get_record( $in ) )
3660 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3661 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3664 put_record( $record, $out );
3667 if ( @entries == 1 )
3669 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3671 map { put_record( $_, $out ) } @{ $results };
3673 elsif ( @entries == 2 )
3675 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3677 map { put_record( $_, $out ) } @{ $results };
3682 sub script_create_vmatch_index
3684 # Martin A. Hansen, January 2008.
3686 # Create a vmatch index from sequences in the stream.
3688 my ( $in, # handle to in stream
3689 $out, # handle to out stream
3690 $options, # options hash
3695 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3697 if ( $options->{ "index_name" } )
3699 $file_tmp = $options->{ 'index_name' };
3700 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3703 while ( $record = get_record( $in ) )
3705 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3707 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3709 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3712 put_record( $record, $out ) if not $options->{ "no_stream" };
3715 if ( $options->{ "index_name" } )
3719 if ( $type eq "protein" ) {
3720 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3722 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3730 sub script_vmatch_seq
3732 # Martin A. Hansen, August 2007.
3734 # Vmatches sequences in stream against a given genome.
3736 my ( $in, # handle to in stream
3737 $out, # handle to out stream
3738 $options, # options hash
3743 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3745 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3747 if ( $options->{ "index_name" } )
3749 @index_files = $options->{ "index_name" };
3753 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3755 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3757 @index_files = sort keys %hash;
3760 while ( $record = get_record( $in ) )
3762 push @records, $record;
3764 put_record( $record, $out );
3767 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3771 $fh_in = Maasha::Common::read_open( $result_file );
3773 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3774 put_record( $record, $out );
3779 unlink $result_file;
3783 sub script_write_fasta
3785 # Martin A. Hansen, August 2007.
3787 # Write FASTA entries from sequences in stream.
3789 my ( $in, # handle to in stream
3790 $out, # handle to out stream
3791 $options, # options hash
3796 my ( $record, $fh, $entry );
3798 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3800 while ( $record = get_record( $in ) )
3802 if ( $entry = record2fasta( $record ) ) {
3803 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3806 put_record( $record, $out ) if not $options->{ "no_stream" };
3813 sub script_write_align
3815 # Martin A. Hansen, August 2007.
3817 # Write pretty alignments aligned sequences in stream.
3819 my ( $in, # handle to in stream
3820 $out, # handle to out stream
3821 $options, # options hash
3826 my ( $fh, $record, @entries );
3828 $fh = write_stream( $options->{ "data_out" } ) ;
3830 while ( $record = get_record( $in ) )
3832 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3833 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3836 put_record( $record, $out ) if not $options->{ "no_stream" };
3839 if ( scalar( @entries ) == 2 ) {
3840 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3841 } elsif ( scalar ( @entries ) > 2 ) {
3842 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3849 sub script_write_blast
3851 # Martin A. Hansen, November 2007.
3853 # Write data in blast table format (-m8 and 9).
3855 my ( $in, # handle to in stream
3856 $out, # handle to out stream
3857 $options, # options hash
3862 my ( $fh, $record, $first );
3864 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3868 while ( $record = get_record( $in ) )
3870 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3872 if ( $options->{ "comment" } and $first )
3874 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";
3879 if ( $record->{ "STRAND" } eq "-" ) {
3880 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3883 print $fh join( "\t",
3884 $record->{ "Q_ID" },
3885 $record->{ "S_ID" },
3886 $record->{ "IDENT" },
3887 $record->{ "ALIGN_LEN" },
3888 $record->{ "MISMATCHES" },
3889 $record->{ "GAPS" },
3890 $record->{ "Q_BEG" } + 1,
3891 $record->{ "Q_END" } + 1,
3892 $record->{ "S_BEG" } + 1,
3893 $record->{ "S_END" } + 1,
3894 $record->{ "E_VAL" },
3895 $record->{ "BIT_SCORE" }
3899 put_record( $record, $out ) if not $options->{ "no_stream" };
3906 sub script_write_tab
3908 # Martin A. Hansen, August 2007.
3910 # Write data as table.
3912 my ( $in, # handle to in stream
3913 $out, # handle to out stream
3914 $options, # options hash
3919 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3921 $options->{ "delimit" } ||= "\t";
3923 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3925 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3927 while ( $record = get_record( $in ) )
3932 if ( $options->{ "keys" } )
3934 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3938 foreach $key ( @{ $options->{ "keys" } } )
3940 if ( exists $record->{ $key } )
3942 push @keys, $key if $options->{ "comment" };
3943 push @vals, $record->{ $key };
3950 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3952 next if exists $no_keys{ $key };
3954 push @keys, $key if $options->{ "comment" };
3955 push @vals, $record->{ $key };
3959 if ( @keys and $options->{ "comment" } )
3961 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3963 delete $options->{ "comment" };
3966 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3968 put_record( $record, $out ) if not $options->{ "no_stream" };
3975 sub script_write_bed
3977 # Martin A. Hansen, August 2007.
3979 # Write BED format for the UCSC genome browser using records in stream.
3981 my ( $in, # handle to in stream
3982 $out, # handle to out stream
3983 $options, # options hash
3988 my ( $fh, $record, $new_record );
3990 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3992 while ( $record = get_record( $in ) )
3994 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3996 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3998 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4000 $new_record->{ "CHR" } = $record->{ "S_ID" };
4001 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4002 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4003 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4004 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4005 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4007 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4009 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4011 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4013 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4015 $new_record->{ "CHR" } = $record->{ "S_ID" };
4016 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4017 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4018 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4019 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4020 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4022 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4024 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4026 $new_record->{ "CHR" } = $record->{ "S_ID" };
4027 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4028 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4029 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4030 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4031 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4033 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4035 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4037 $new_record->{ "CHR" } = $record->{ "S_ID" };
4038 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4039 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4040 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4041 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4042 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4044 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4046 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4048 Maasha::UCSC::bed_put_entry( $record, $fh );
4051 put_record( $record, $out ) if not $options->{ "no_stream" };
4058 sub script_write_psl
4060 # Martin A. Hansen, August 2007.
4062 # Write PSL output from stream.
4064 my ( $in, # handle to in stream
4065 $out, # handle to out stream
4066 $options, # options hash
4071 my ( $fh, $record, @output, $first );
4075 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4077 while ( $record = get_record( $in ) )
4079 put_record( $record, $out ) if not $options->{ "no_stream" };
4081 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4083 Maasha::UCSC::psl_put_header( $fh ) if $first;
4084 Maasha::UCSC::psl_put_entry( $record, $fh );
4093 sub script_write_fixedstep
4095 # Martin A. Hansen, Juli 2008.
4097 # Write fixedStep entries from recrods in the stream.
4099 my ( $in, # handle to in stream
4100 $out, # handle to out stream
4101 $options, # options hash
4106 my ( $fh, $record, $vals );
4108 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4110 while ( $record = get_record( $in ) )
4112 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4114 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4116 $vals = $record->{ 'VALS' };
4120 print $fh "$vals\n";
4123 put_record( $record, $out ) if not $options->{ "no_stream" };
4130 sub script_write_2bit
4132 # Martin A. Hansen, March 2008.
4134 # Write sequence entries from stream in 2bit format.
4136 my ( $in, # handle to in stream
4137 $out, # handle to out stream
4138 $options, # options hash
4143 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4145 $mask = 1 if not $options->{ "no_mask" };
4147 $tmp_file = "$BP_TMP/write_2bit.fna";
4148 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4150 $fh_out = write_stream( $options->{ "data_out" } );
4152 while ( $record = get_record( $in ) )
4154 if ( $entry = record2fasta( $record ) ) {
4155 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4158 put_record( $record, $out ) if not $options->{ "no_stream" };
4163 $fh_in = Maasha::Common::read_open( $tmp_file );
4165 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4174 sub script_write_solid
4176 # Martin A. Hansen, April 2008.
4178 # Write di-base encoded Solid sequence from entries in stream.
4180 my ( $in, # handle to in stream
4181 $out, # handle to out stream
4182 $options, # options hash
4187 my ( $record, $fh, $entry );
4189 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4191 while ( $record = get_record( $in ) )
4193 if ( $entry = record2fasta( $record ) )
4195 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4197 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4200 put_record( $record, $out ) if not $options->{ "no_stream" };
4207 sub script_plot_seqlogo
4209 # Martin A. Hansen, August 2007.
4211 # Calculates and writes a sequence logo for alignments.
4213 my ( $in, # handle to in stream
4214 $out, # handle to out stream
4215 $options, # options hash
4220 my ( $record, @entries, $logo, $fh );
4222 while ( $record = get_record( $in ) )
4224 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4225 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4228 put_record( $record, $out ) if not $options->{ "no_stream" };
4231 $logo = Maasha::Plot::seq_logo( \@entries );
4233 $fh = write_stream( $options->{ "data_out" } );
4241 sub script_plot_phastcons_profiles
4243 # Martin A. Hansen, January 2008.
4245 # Plots PhastCons profiles.
4247 my ( $in, # handle to in stream
4248 $out, # handle to out stream
4249 $options, # options hash
4254 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4256 $options->{ "title" } ||= "PhastCons Profiles";
4258 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4259 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4261 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4262 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4264 while ( $record = get_record( $in ) )
4266 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4268 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4270 push @{ $AoA }, [ @{ $scores } ];
4273 put_record( $record, $out ) if not $options->{ "no_stream" };
4276 Maasha::UCSC::phastcons_normalize( $AoA );
4278 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4279 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4281 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4283 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4285 $fh = write_stream( $options->{ "data_out" } );
4287 print $fh "$_\n" foreach @{ $plot };
4293 sub script_analyze_bed
4295 # Martin A. Hansen, March 2008.
4297 # Analyze BED entries in stream.
4299 my ( $in, # handle to in stream
4300 $out, # handle to out stream
4301 $options, # options hash
4308 while ( $record = get_record( $in ) )
4310 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4312 put_record( $record, $out );
4317 sub script_analyze_vals
4319 # Martin A. Hansen, August 2007.
4321 # Analyze values for given keys in stream.
4323 my ( $in, # handle to in stream
4324 $out, # handle to out stream
4325 $options, # options hash
4330 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4332 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4334 while ( $record = get_record( $in ) )
4336 foreach $key ( keys %{ $record } )
4338 next if $options->{ "keys" } and not exists $key_hash{ $key };
4340 $analysis->{ $key }->{ "COUNT" }++;
4342 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4344 $analysis->{ $key }->{ "TYPE" } = "num";
4345 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4346 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4347 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4351 $len = length $record->{ $key };
4353 $analysis->{ $key }->{ "TYPE" } = "alph";
4354 $analysis->{ $key }->{ "SUM" } += $len;
4355 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4356 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4360 put_record( $record, $out ) if not $options->{ "no_stream" };
4363 foreach $key ( keys %{ $analysis } )
4365 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4366 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4369 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4379 if ( $options->{ "keys" } ) {
4380 @keys = @{ $options->{ "keys" } };
4382 @keys = keys %{ $analysis };
4385 foreach $key ( @keys )
4387 $keys .= sprintf "% 15s", $key;
4388 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4389 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4390 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4391 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4392 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4393 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4396 print $out "$keys\n";
4397 print $out "$types\n";
4398 print $out "$counts\n";
4399 print $out "$mins\n";
4400 print $out "$maxs\n";
4401 print $out "$sums\n";
4402 print $out "$means\n";
4406 sub script_head_records
4408 # Martin A. Hansen, August 2007.
4410 # Display the first sequences in stream.
4412 my ( $in, # handle to in stream
4413 $out, # handle to out stream
4414 $options, # options hash
4419 my ( $record, $count );
4421 $options->{ "num" } ||= 10;
4425 while ( $record = get_record( $in ) )
4429 put_record( $record, $out );
4431 last if $count == $options->{ "num" };
4436 sub script_remove_keys
4438 # Martin A. Hansen, August 2007.
4440 # Remove keys from stream.
4442 my ( $in, # handle to in stream
4443 $out, # handle to out stream
4444 $options, # options hash
4449 my ( $record, $new_record );
4451 while ( $record = get_record( $in ) )
4453 if ( $options->{ "keys" } )
4455 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4457 elsif ( $options->{ "save_keys" } )
4459 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4461 $record = $new_record;
4464 put_record( $record, $out ) if keys %{ $record };
4469 sub script_rename_keys
4471 # Martin A. Hansen, August 2007.
4473 # Rename keys in stream.
4475 my ( $in, # handle to in stream
4476 $out, # handle to out stream
4477 $options, # options hash
4484 while ( $record = get_record( $in ) )
4486 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4488 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4490 delete $record->{ $options->{ "keys" }->[ 0 ] };
4493 put_record( $record, $out );
4498 sub script_uniq_vals
4500 # Martin A. Hansen, August 2007.
4502 # Find unique values in stream.
4504 my ( $in, # handle to in stream
4505 $out, # handle to out stream
4506 $options, # options hash
4511 my ( %hash, $record );
4513 while ( $record = get_record( $in ) )
4515 if ( $record->{ $options->{ "key" } } )
4517 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4519 put_record( $record, $out );
4521 $hash{ $record->{ $options->{ "key" } } } = 1;
4523 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4525 put_record( $record, $out );
4529 $hash{ $record->{ $options->{ "key" } } } = 1;
4534 put_record( $record, $out );
4540 sub script_merge_vals
4542 # Martin A. Hansen, August 2007.
4544 # Rename keys in stream.
4546 my ( $in, # handle to in stream
4547 $out, # handle to out stream
4548 $options, # options hash
4553 my ( $record, @join, $i );
4555 $options->{ "delimit" } ||= '_';
4557 while ( $record = get_record( $in ) )
4559 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4561 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4563 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4564 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4567 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4570 put_record( $record, $out );
4577 # Martin A. Hansen, August 2007.
4579 # Grab for records in stream.
4581 my ( $in, # handle to in stream
4582 $out, # handle to out stream
4583 $options, # options hash
4588 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4590 if ( $options->{ "patterns" } )
4592 $patterns = [ split ",", $options->{ "patterns" } ];
4594 elsif ( -f $options->{ "patterns_in" } )
4596 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4598 elsif ( -f $options->{ "exact_in" } )
4600 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4602 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4607 if ( $options->{ "eval" } )
4609 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4617 while ( $record = get_record( $in ) )
4623 if ( $options->{ "keys" } )
4625 foreach $key ( @{ $options->{ "keys" } } )
4627 if ( exists $lookup_hash{ $record->{ $key } } )
4636 foreach $key ( keys %{ $record } )
4638 if ( not $options->{ "vals_only" } )
4640 if ( exists $lookup_hash{ $key } )
4647 if ( not $options->{ "keys_only" } )
4649 if ( exists $lookup_hash{ $record->{ $key } } )
4660 foreach $pattern ( @{ $patterns } )
4662 if ( $options->{ "keys" } )
4664 foreach $key ( @{ $options->{ "keys" } } )
4666 $pos = index $record->{ $key }, $pattern;
4668 goto FOUND if $pos >= 0;
4673 foreach $key ( keys %{ $record } )
4675 if ( not $options->{ "vals_only" } )
4677 $pos = index $key, $pattern;
4679 goto FOUND if $pos >= 0;
4682 if ( not $options->{ "keys_only" } )
4684 $pos = index $record->{ $key }, $pattern;
4686 goto FOUND if $pos >= 0;
4692 elsif ( $options->{ "regex" } )
4694 if ( $options->{ "keys" } )
4696 foreach $key ( @{ $options->{ "keys" } } )
4698 if ( $options->{ "case_insensitive" } ) {
4699 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4701 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4704 goto FOUND if $pos >= 0;
4709 foreach $key ( keys %{ $record } )
4711 if ( not $options->{ "vals_only" } )
4713 if ( $options->{ "case_insensitive" } ) {
4714 $pos = 1 if $key =~ /$options->{'regex'}/i;
4716 $pos = 1 if $key =~ /$options->{'regex'}/;
4719 goto FOUND if $pos >= 0;
4722 if ( not $options->{ "keys_only" } )
4724 if ( $options->{ "case_insensitive" } ) {
4725 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4727 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4730 goto FOUND if $pos >= 0;
4735 elsif ( $options->{ "eval" } )
4737 if ( defined $record->{ $key } )
4739 if ( $op eq "<" and $record->{ $key } < $val ) {
4740 $pos = 1 and goto FOUND;
4741 } elsif ( $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 "eq" and $record->{ $key } eq $val ) {
4752 $pos = 1 and goto FOUND;
4753 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4754 $pos = 1 and goto FOUND;
4761 if ( $pos >= 0 and not $options->{ "invert" } ) {
4762 put_record( $record, $out );
4763 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4764 put_record( $record, $out );
4772 # Martin A. Hansen, August 2007.
4774 # Evaluate extression for records in stream.
4776 my ( $in, # handle to in stream
4777 $out, # handle to out stream
4778 $options, # options hash
4783 my ( $record, $eval_key, $eval_val, $check, @keys );
4785 while ( $record = get_record( $in ) )
4787 if ( $options->{ "eval" } )
4789 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4797 @keys = split /\W+/, $eval_val;
4798 @keys = grep { ! /^\d+$/ } @keys;
4803 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4805 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4808 put_record( $record, $out );
4815 # Martin A. Hansen, June 2008.
4819 my ( $in, # handle to in stream
4820 $out, # handle to out stream
4821 $options, # options hash
4826 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4828 while ( $record = get_record( $in ) )
4832 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4834 push @rows, $record->{ $key };
4838 push @matrix, [ @rows ];
4843 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4845 foreach $row ( @matrix )
4847 for ( $i = 0; $i < @{ $row }; $i++ ) {
4848 $record->{ "V$i" } = $row->[ $i ];
4851 put_record( $record, $out );
4856 sub script_add_ident
4858 # Martin A. Hansen, May 2008.
4860 # Add a unique identifier to each record in stream.
4862 my ( $in, # handle to in stream
4863 $out, # handle to out stream
4864 $options, # options hash
4869 my ( $record, $key, $prefix, $i );
4871 $key = $options->{ "key" } || "ID";
4872 $prefix = $options->{ "prefix" } || "ID";
4876 while ( $record = get_record( $in ) )
4878 $record->{ $key } = sprintf( "$prefix%08d", $i );
4880 put_record( $record, $out );
4887 sub script_count_records
4889 # Martin A. Hansen, August 2007.
4891 # Count records in stream.
4893 my ( $in, # handle to in stream
4894 $out, # handle to out stream
4895 $options, # options hash
4900 my ( $record, $count, $result, $fh, $line );
4904 if ( $options->{ "no_stream" } )
4906 while ( $line = <$in> )
4910 $count++ if $line eq "---";
4915 while ( $record = get_record( $in ) )
4917 put_record( $record, $out );
4923 $result = { "count_records" => $count };
4925 $fh = write_stream( $options->{ "data_out" } );
4927 put_record( $result, $fh );
4933 sub script_random_records
4935 # Martin A. Hansen, August 2007.
4937 # Pick a number or random records from stream.
4939 my ( $in, # handle to in stream
4940 $out, # handle to out stream
4941 $options, # options hash
4946 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4948 $options->{ "num" } ||= 10;
4950 $tmp_file = "$BP_TMP/random_records.tmp";
4952 $fh_out = Maasha::Common::write_open( $tmp_file );
4956 while ( $record = get_record( $in ) )
4958 put_record( $record, $fh_out );
4968 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4970 while ( $i < $options->{ "num" } )
4972 $rand = int( rand( $count ) );
4974 if ( not exists $rand_hash{ $rand } )
4976 $rand_hash{ $rand } = 1;
4978 $max = $rand if $rand > $max;
4984 $fh_in = Maasha::Common::read_open( $tmp_file );
4988 while ( $record = get_record( $fh_in ) )
4990 put_record( $record, $out ) if exists $rand_hash{ $count };
4992 last if $count == $max;
5003 sub script_sort_records
5005 # Martin A. Hansen, August 2007.
5007 # Sort to sort records according to keys.
5009 my ( $in, # handle to in stream
5010 $out, # handle to out stream
5011 $options, # options hash
5016 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5018 foreach $key ( @{ $options->{ "keys" } } )
5020 if ( $key =~ s/n$// ) {
5021 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5023 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5027 $sort_str = join " or ", @sort_cmd;
5028 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5030 while ( $record = get_record( $in ) ) {
5031 push @records, $record;
5034 @records = sort $sort_sub @records;
5036 if ( $options->{ "reverse" } )
5038 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5039 put_record( $records[ $i ], $out );
5044 for ( $i = 0; $i < scalar @records; $i++ ) {
5045 put_record( $records[ $i ], $out );
5051 sub script_count_vals
5053 # Martin A. Hansen, August 2007.
5055 # Count records in stream.
5057 my ( $in, # handle to in stream
5058 $out, # handle to out stream
5059 $options, # options hash
5064 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5066 $tmp_file = "$BP_TMP/count_cache.tmp";
5068 $fh_out = Maasha::Common::write_open( $tmp_file );
5072 while ( $record = get_record( $in ) )
5074 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5076 push @records, $record;
5078 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5080 map { put_record( $_, $fh_out ) } @records;
5087 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5098 $fh_in = Maasha::Common::read_open( $tmp_file );
5100 while ( $record = get_record( $fh_in ) )
5102 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5104 put_record( $record, $out );
5106 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5114 foreach $record ( @records )
5116 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5118 put_record( $record, $out );
5125 sub script_plot_histogram
5127 # Martin A. Hansen, September 2007.
5129 # Plot a simple histogram for a given key using GNU plot.
5131 my ( $in, # handle to in stream
5132 $out, # handle to out stream
5133 $options, # options hash
5138 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5140 $options->{ "title" } ||= "Histogram";
5141 $options->{ "sort" } ||= "num";
5143 while ( $record = get_record( $in ) )
5145 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5147 put_record( $record, $out ) if not $options->{ "no_stream" };
5150 if ( $options->{ "sort" } eq "num" ) {
5151 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5153 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5156 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5158 $fh = write_stream( $options->{ "data_out" } );
5160 print $fh "$_\n" foreach @{ $result };
5166 sub script_plot_lendist
5168 # Martin A. Hansen, August 2007.
5170 # Plot length distribution using GNU plot.
5172 my ( $in, # handle to in stream
5173 $out, # handle to out stream
5174 $options, # options hash
5179 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5181 $options->{ "title" } ||= "Length Distribution";
5183 while ( $record = get_record( $in ) )
5185 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5187 put_record( $record, $out ) if not $options->{ "no_stream" };
5190 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5192 for ( $i = 0; $i < $max; $i++ ) {
5193 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5196 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5198 $fh = write_stream( $options->{ "data_out" } );
5200 print $fh "$_\n" foreach @{ $result };
5206 sub script_plot_chrdist
5208 # Martin A. Hansen, August 2007.
5210 # Plot chromosome distribution using GNU plot.
5212 my ( $in, # handle to in stream
5213 $out, # handle to out stream
5214 $options, # options hash
5219 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5221 $options->{ "title" } ||= "Chromosome Distribution";
5223 while ( $record = get_record( $in ) )
5225 if ( $record->{ "CHR" } ) { # generic
5226 $data_hash{ $record->{ "CHR" } }++;
5227 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5228 $data_hash{ $record->{ "S_ID" } }++;
5229 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5230 $data_hash{ $record->{ "S_ID" } }++;
5231 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5232 $data_hash{ $record->{ "S_ID" } }++;
5235 put_record( $record, $out ) if not $options->{ "no_stream" };
5238 foreach $elem ( keys %data_hash )
5242 $sort_key =~ s/chr//i;
5244 $sort_key =~ s/^X(.*)/99$1/;
5245 $sort_key =~ s/^Y(.*)/99$1/;
5246 $sort_key =~ s/^Z(.*)/999$1/;
5247 $sort_key =~ s/^M(.*)/9999$1/;
5248 $sort_key =~ s/^U(.*)/99999$1/;
5250 $count = $sort_key =~ tr/_//;
5252 $sort_key =~ s/_.*/"999999" x $count/ex;
5254 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5257 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5259 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5261 $fh = write_stream( $options->{ "data_out" } );
5263 print $fh "$_\n" foreach @{ $result };
5269 sub script_plot_karyogram
5271 # Martin A. Hansen, August 2007.
5273 # Plot hits on karyogram.
5275 my ( $in, # handle to in stream
5276 $out, # handle to out stream
5277 $options, # options hash
5282 my ( %options, $record, @data, $fh, $result, %data_hash );
5284 $options->{ "genome" } ||= "human";
5285 $options->{ "feat_color" } ||= "black";
5287 while ( $record = get_record( $in ) )
5289 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5291 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5294 put_record( $record, $out ) if not $options->{ "no_stream" };
5297 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5299 $fh = write_stream( $options->{ "data_out" } );
5307 sub script_plot_matches
5309 # Martin A. Hansen, August 2007.
5311 # Plot matches in 2D generating a dotplot.
5313 my ( $in, # handle to in stream
5314 $out, # handle to out stream
5315 $options, # options hash
5320 my ( $record, @data, $fh, $result, %data_hash );
5322 $options->{ "direction" } ||= "both";
5324 while ( $record = get_record( $in ) )
5326 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5327 push @data, $record;
5330 put_record( $record, $out ) if not $options->{ "no_stream" };
5333 $options->{ "title" } ||= "plot_matches";
5334 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5335 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5337 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5339 $fh = write_stream( $options->{ "data_out" } );
5341 print $fh "$_\n" foreach @{ $result };
5347 sub script_length_vals
5349 # Martin A. Hansen, August 2007.
5351 # Determine the length of the value for given keys.
5353 my ( $in, # handle to in stream
5354 $out, # handle to out stream
5355 $options, # options hash
5360 my ( $record, $key );
5362 while ( $record = get_record( $in ) )
5364 foreach $key ( @{ $options->{ "keys" } } )
5366 if ( $record->{ $key } ) {
5367 $record->{ $key . "_LEN" } = length $record->{ $key };
5371 put_record( $record, $out );
5378 # Martin A. Hansen, August 2007.
5380 # Calculates the sums for values of given keys.
5382 my ( $in, # handle to in stream
5383 $out, # handle to out stream
5384 $options, # options hash
5389 my ( $record, $key, %sum_hash, $fh );
5391 while ( $record = get_record( $in ) )
5393 foreach $key ( @{ $options->{ "keys" } } )
5395 if ( $record->{ $key } ) {
5396 $sum_hash{ $key } += $record->{ $key };
5400 put_record( $record, $out ) if not $options->{ "no_stream" };
5403 $fh = write_stream( $options->{ "data_out" } );
5405 foreach $key ( @{ $options->{ "keys" } } ) {
5406 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5413 sub script_mean_vals
5415 # Martin A. Hansen, August 2007.
5417 # Calculate the mean of values of given keys.
5419 my ( $in, # handle to in stream
5420 $out, # handle to out stream
5421 $options, # options hash
5426 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5428 while ( $record = get_record( $in ) )
5430 foreach $key ( @{ $options->{ "keys" } } )
5432 if ( $record->{ $key } )
5434 $sum_hash{ $key } += $record->{ $key };
5435 $count_hash{ $key }++;
5439 put_record( $record, $out ) if not $options->{ "no_stream" };
5442 $fh = write_stream( $options->{ "data_out" } );
5444 foreach $key ( @{ $options->{ "keys" } } )
5446 if ( $count_hash{ $key } ) {
5447 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5452 put_record( { $key . "_MEAN" => $mean } , $fh );
5459 sub script_median_vals
5461 # Martin A. Hansen, March 2008.
5463 # Calculate the median values of given keys.
5465 my ( $in, # handle to in stream
5466 $out, # handle to out stream
5467 $options, # options hash
5472 my ( $record, $key, %median_hash, $median, $fh );
5474 while ( $record = get_record( $in ) )
5476 foreach $key ( @{ $options->{ "keys" } } ) {
5477 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5480 put_record( $record, $out ) if not $options->{ "no_stream" };
5483 $fh = write_stream( $options->{ "data_out" } );
5485 foreach $key ( @{ $options->{ "keys" } } )
5487 if ( $median_hash{ $key } ) {
5488 $median = Maasha::Calc::median( $median_hash{ $key } );
5493 put_record( { $key . "_MEDIAN" => $median } , $fh );
5502 # Martin A. Hansen, February 2008.
5504 # Determine the maximum values of given keys.
5506 my ( $in, # handle to in stream
5507 $out, # handle to out stream
5508 $options, # options hash
5513 my ( $record, $key, $fh, %max_hash, $max_record );
5515 while ( $record = get_record( $in ) )
5517 foreach $key ( @{ $options->{ "keys" } } )
5519 if ( $record->{ $key } )
5521 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5525 put_record( $record, $out ) if not $options->{ "no_stream" };
5528 $fh = write_stream( $options->{ "data_out" } );
5530 foreach $key ( @{ $options->{ "keys" } } )
5532 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5535 put_record( $max_record, $fh );
5543 # Martin A. Hansen, February 2008.
5545 # Determine the minimum values of given keys.
5547 my ( $in, # handle to in stream
5548 $out, # handle to out stream
5549 $options, # options hash
5554 my ( $record, $key, $fh, %min_hash, $min_record );
5556 while ( $record = get_record( $in ) )
5558 foreach $key ( @{ $options->{ "keys" } } )
5560 if ( defined $record->{ $key } )
5562 if ( exists $min_hash{ $key } ) {
5563 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5565 $min_hash{ $key } = $record->{ $key };
5570 put_record( $record, $out ) if not $options->{ "no_stream" };
5573 $fh = write_stream( $options->{ "data_out" } );
5575 foreach $key ( @{ $options->{ "keys" } } )
5577 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5580 put_record( $min_record, $fh );
5586 sub script_upload_to_ucsc
5588 # Martin A. Hansen, August 2007.
5590 # Calculate the mean of values of given keys.
5592 my ( $in, # handle to in stream
5593 $out, # handle to out stream
5594 $options, # options hash
5599 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5600 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5602 $options->{ "short_label" } ||= $options->{ 'table' };
5603 $options->{ "long_label" } ||= $options->{ 'table' };
5604 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5605 $options->{ "priority" } ||= 1;
5606 $options->{ "visibility" } ||= "pack";
5607 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5608 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5610 $file = "$BP_TMP/ucsc_upload.tmp";
5618 if ( $options->{ 'wiggle' } )
5620 $options->{ "visibility" } = "full";
5622 while ( $record = get_record( $in ) )
5624 put_record( $record, $out ) if not $options->{ "no_stream" };
5626 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5627 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5628 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5630 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5632 $fh_out = $fh_hash{ $record->{ "CHR" } };
5634 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5637 map { close $_ } keys %fh_hash;
5639 $fh_out = Maasha::Common::write_open( $file );
5641 foreach $chr ( sort keys %fh_hash )
5643 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5645 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5649 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5651 $chr = $entry->{ 'CHR' };
5652 $beg = $entry->{ 'CHR_BEG' };
5653 $end = $entry->{ 'CHR_END' };
5654 $q_id = $entry->{ 'Q_ID' };
5656 if ( $q_id =~ /_(\d+)$/ ) {
5666 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5671 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5672 $block->[ $i ] += $clones;
5675 $max = Maasha::Calc::max( $max, $end );
5684 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5685 $block->[ $i ] += $clones;
5692 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5694 unlink "$BP_TMP/$chr";
5699 $wig_file = "$options->{ 'table' }.wig";
5700 $wib_file = "$options->{ 'table' }.wib";
5702 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5704 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5706 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5708 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5709 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5719 $fh_out = Maasha::Common::write_open( $file );
5721 while ( $record = get_record( $in ) )
5723 put_record( $record, $out ) if not $options->{ "no_stream" };
5725 if ( $record->{ "REC_TYPE" } eq "PSL" )
5727 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5728 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5732 $format = "PSL" if not $format;
5734 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5736 # chrom chromStart chromEnd name score strand size secStr conf
5738 print $fh_out join ( "\t",
5740 $record->{ "CHR_BEG" },
5741 $record->{ "CHR_END" } + 1,
5742 $record->{ "Q_ID" },
5743 $record->{ "SCORE" },
5744 $record->{ "STRAND" },
5745 $record->{ "SIZE" },
5746 $record->{ "SEC_STRUCT" },
5747 $record->{ "CONF" },
5750 $format = "BED_SS" if not $format;
5752 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5754 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5756 $format = "BED" if not $format;
5757 $columns = $record->{ "BED_COLS" } if not $columns;
5759 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5761 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5763 $format = "BED" if not $format;
5764 $columns = 6 if not $columns;
5766 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5768 $record->{ "CHR" } = $record->{ "S_ID" };
5769 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5770 $record->{ "CHR_END" } = $record->{ "S_END" };
5771 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5773 $format = "BED" if not $format;
5774 $columns = 6 if not $columns;
5776 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5778 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5780 $record->{ "CHR" } = $record->{ "S_ID" };
5781 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5782 $record->{ "CHR_END" } = $record->{ "S_END" };
5783 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5784 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5786 $format = "BED" if not $format;
5787 $columns = 6 if not $columns;
5789 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5792 if ( $i == $options->{ "chunk_size" } )
5796 if ( $format eq "BED" ) {
5797 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5798 } elsif ( $format eq "PSL" ) {
5799 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5808 $fh_out = Maasha::Common::write_open( $file );
5817 if ( exists $options->{ "database" } and $options->{ "table" } )
5819 if ( $format eq "BED" )
5821 $type = "bed $columns";
5823 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5825 elsif ( $format eq "BED_SS" )
5827 $options->{ "sec_struct" } = 1;
5829 $type = "sec_struct";
5831 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5833 elsif ( $format eq "PSL" )
5837 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5839 elsif ( $format eq "WIGGLE" )
5843 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5848 Maasha::UCSC::update_my_tracks( $options, $type );
5853 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5858 # Martin A. Hansen, July 2008.
5860 # Given a biopiece record converts it to a FASTA record.
5861 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
5862 # tried in that order.
5864 my ( $record, # record
5869 my ( $seq_name, $seq );
5871 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
5872 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
5874 if ( defined $seq_name and defined $seq ) {
5875 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
5884 # Martin A. Hansen, July 2007.
5886 # Opens a stream to STDIN or a file,
5888 my ( $path, # path - OPTIONAL
5891 # Returns filehandle.
5895 if ( not -t STDIN ) {
5896 $fh = Maasha::Common::read_stdin();
5897 } elsif ( not $path ) {
5898 # Maasha::Common::error( qq(no data stream) );
5900 $fh = Maasha::Common::read_open( $path );
5903 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5911 # Martin A. Hansen, August 2007.
5913 # Opens a stream to STDOUT or a file.
5915 my ( $path, # path - OPTIONAL
5916 $gzip, # compress data - OPTIONAL
5919 # Returns filehandle.
5924 $fh = Maasha::Common::write_open( $path, $gzip );
5926 $fh = Maasha::Common::write_stdout();
5935 # Martin A. Hansen, July 2007.
5937 # Reads one record at a time and converts that record
5938 # to a Perl data structure (a hash) which is returned.
5940 my ( $fh, # handle to stream
5945 my ( $block, @lines, $line, $key, $value, %record );
5947 local $/ = "\n---\n";
5953 return if not defined $block;
5955 @lines = split "\n", $block;
5957 foreach $line ( @lines )
5959 ( $key, $value ) = split ": ", $line, 2;
5961 $record{ $key } = $value;
5964 return wantarray ? %record : \%record;
5970 # Martin A. Hansen, July 2007.
5972 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5974 my ( $data, # data structure
5975 $fh, # file handle - OPTIONAL
5980 if ( scalar keys %{ $data } )
5984 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5989 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6000 # Martin A. Hansen, November 2007.
6002 # Extracts files from an explicit GetOpt::Long argument
6003 # allowing for the use of glob. E.g.
6004 # --data_in=test.fna
6005 # --data_in=test.fna,test2.fna
6007 # --data_in=test.fna,/dir/*.fna
6009 my ( $option, # option from GetOpt::Long
6014 my ( $elem, @files );
6016 foreach $elem ( split ",", $option )
6020 } elsif ( $elem =~ /\*/ ) {
6021 push @files, glob( $elem );
6025 return wantarray ? @files : \@files;
6031 # Martin A. Hansen, April 2008.
6033 # Removes temporary directory and exits gracefully.
6034 # This subroutine is meant to be run always as the last
6035 # thing even if a script is dies or is interrupted
6038 my ( $sig, # signal from the %SIG
6041 # print STDERR "signal->$sig<-\n";
6049 if ( $sig =~ /MAASHA_ERROR/ ) {
6050 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6051 } elsif ( $sig eq "INT" ) {
6052 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6053 } elsif ( $sig eq "TERM" ) {
6054 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6056 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6068 # Martin A. Hansen, July 2008.
6070 # Cleans out any unused temporary files and direcotries in BP_TMP.
6074 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6076 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6078 $curr_pid = Maasha::Common::get_processid();
6080 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6082 foreach $dir ( @dirs )
6084 if ( $dir =~ /(.+)_(\d+)_(\d+)_bp_tmp/ )
6090 if ( not Maasha::Common::process_running( $pid ) )
6092 # print STDERR "Removing stale dir: $dir\n";
6093 Maasha::Common::dir_remove( $dir );
6095 elsif ( $pid == $curr_pid )
6097 # print STDERR "Removing current dir: $dir\n";
6098 Maasha::Common::dir_remove( $dir );
6111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<