1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
57 use vars qw( @ISA @EXPORT_OK );
61 @ISA = qw( Exporter );
76 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
79 $SIG{ '__DIE__' } = \&sig_handler;
80 $SIG{ 'INT' } = \&sig_handler;
81 $SIG{ 'TERM' } = \&sig_handler;
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87 my ( $script, $BP_TMP );
89 $script = Maasha::Common::get_scriptname();
90 $BP_TMP = Maasha::Common::get_tmpdir();
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
96 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
97 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
99 $log_global->autoflush( 1 );
100 $log_local->autoflush( 1 );
102 &log( $log_global, $script, \@ARGV );
103 &log( $log_local, $script, \@ARGV );
109 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
112 my $t0 = gettimeofday();
114 run_script( $script );
116 my $t1 = gettimeofday();
118 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
121 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
126 # Martin A. Hansen, January 2008.
128 # Log messages to logfile.
130 my ( $fh, # filehandle to logfile
131 $script, # script name
132 $argv, # reference to @ARGV
137 my ( $time_stamp, $user );
139 $time_stamp = Maasha::Common::time_stamp();
141 $user = $ENV{ 'USER' };
143 $script = "biopieces" if $script eq "-e";
145 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
151 # Martin A. Hansen, August 2007.
153 # Run a specific script.
155 my ( $script, # script name
160 my ( $options, $in, $out );
162 $options = get_options( $script );
164 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
165 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
168 $in = read_stream( $options->{ "stream_in" } );
169 $out = write_stream( $options->{ "stream_out" } );
171 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
172 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
173 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
174 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
175 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
176 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
177 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
178 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
179 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
181 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
182 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
183 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
184 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
185 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
186 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
187 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
188 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
189 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
190 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
191 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
192 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
193 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
194 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
195 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
196 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
197 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
198 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
199 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
200 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
201 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
202 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
203 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
204 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
205 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
206 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
207 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
209 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
210 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
211 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
212 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
213 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
214 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
215 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
216 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
217 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
218 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
219 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
220 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
221 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
222 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
223 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
224 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
225 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
226 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
227 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
228 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
229 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
230 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
231 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
232 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
233 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
234 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
235 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
236 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
237 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
238 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
239 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
240 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
241 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
242 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
243 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
244 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
245 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
246 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
247 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
248 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
249 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
250 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
251 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
252 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
253 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
254 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
255 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
256 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
257 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
258 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
259 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
261 close $in if defined $in;
264 # unset status - missing
265 # write log file - missing
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 "match_seq" )
579 elsif ( $script eq "create_vmatch_index" )
587 elsif ( $script eq "vmatch_seq" )
598 elsif ( $script eq "write_fasta" )
607 elsif ( $script eq "write_align" )
617 elsif ( $script eq "write_blast" )
626 elsif ( $script eq "write_tab" )
638 elsif ( $script eq "write_bed" )
646 elsif ( $script eq "write_psl" )
654 elsif ( $script eq "write_fixedstep" )
662 elsif ( $script eq "write_2bit" )
670 elsif ( $script eq "write_solid" )
679 elsif ( $script eq "plot_seqlogo" )
686 elsif ( $script eq "plot_phastcons_profiles" )
701 elsif ( $script eq "analyze_vals" )
708 elsif ( $script eq "head_records" )
714 elsif ( $script eq "remove_keys" )
721 elsif ( $script eq "rename_keys" )
727 elsif ( $script eq "uniq_vals" )
734 elsif ( $script eq "merge_vals" )
741 elsif ( $script eq "grab" )
756 elsif ( $script eq "compute" )
762 elsif ( $script eq "add_ident" )
769 elsif ( $script eq "count_records" )
776 elsif ( $script eq "random_records" )
782 elsif ( $script eq "sort_records" )
789 elsif ( $script eq "count_vals" )
795 elsif ( $script eq "plot_histogram" )
808 elsif ( $script eq "plot_lendist" )
820 elsif ( $script eq "plot_chrdist" )
831 elsif ( $script eq "plot_karyogram" )
840 elsif ( $script eq "plot_matches" )
852 elsif ( $script eq "length_vals" )
858 elsif ( $script eq "sum_vals" )
866 elsif ( $script eq "mean_vals" )
874 elsif ( $script eq "median_vals" )
882 elsif ( $script eq "max_vals" )
890 elsif ( $script eq "min_vals" )
898 elsif ( $script eq "upload_to_ucsc" )
923 # print STDERR Dumper( \@options );
930 $options{ "script" } = $script;
932 # print STDERR Dumper( \%options );
934 if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
935 return wantarray ? %options : \%options;
938 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
939 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
940 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
941 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
942 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
943 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
944 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
945 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
947 # ---- check arguments ----
949 if ( $options{ 'data_in' } )
951 $options{ "files" } = getopt_files( $options{ 'data_in' } );
953 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
956 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
958 # print STDERR Dumper( \%options );
960 foreach $opt ( keys %options )
962 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
964 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
966 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+$/ )
968 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
970 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
972 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
974 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
976 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
978 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
980 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
982 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
984 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
986 elsif ( $opt eq "genome" and $script ne "format_genome" )
988 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
989 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
991 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
992 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
995 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
997 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
999 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1001 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1005 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1006 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1007 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1008 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1009 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1010 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
1011 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" };
1012 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1013 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" };
1015 if ( $script eq "upload_to_ucsc" )
1017 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1018 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1021 return wantarray ? %options : \%options;
1025 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1028 sub script_print_usage
1030 # Martin A. Hansen, January 2008.
1032 # Retrieves usage information from file and
1033 # prints this nicely formatted.
1035 my ( $in, # handle to in stream
1036 $out, # handle to out stream
1037 $options, # options hash
1042 my ( $file, $wiki, $lines );
1044 if ( $options->{ 'data_in' } ) {
1045 $file = $options->{ 'data_in' };
1047 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1050 $wiki = Maasha::Gwiki::gwiki_read( $file );
1052 if ( not $options->{ "help" } ) {
1053 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1056 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1058 print STDERR "$_\n" foreach @{ $lines };
1064 sub script_list_biopieces
1066 # Martin A. Hansen, January 2008.
1068 # Prints the synopsis from the usage for each of the biopieces.
1070 my ( $in, # handle to in stream
1071 $out, # handle to out stream
1072 $options, # options hash
1077 my ( @files, $file, $wiki, $program, $synopsis );
1079 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1081 foreach $file ( sort @files )
1083 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1087 $wiki = Maasha::Gwiki::gwiki_read( $file );
1089 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1090 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1092 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1094 printf( "%-30s%s\n", $program, $synopsis );
1102 sub script_list_genomes
1104 # Martin A. Hansen, January 2008.
1106 # Prints the synopsis from the usage for each of the biopieces.
1108 my ( $in, # handle to in stream
1109 $out, # handle to out stream
1110 $options, # options hash
1115 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1117 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1119 foreach $genome ( @genomes )
1121 next if $genome =~ /\.$/;
1123 @formats = Maasha::Common::ls_dirs( $genome );
1125 foreach $format ( @formats )
1127 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1129 $hash{ $1 }{ $2 } = 1;
1138 map { push @row, $_ } sort keys %found;
1140 print join( "\t", @row ), "\n";
1142 foreach $genome ( sort keys %hash )
1146 foreach $format ( sort keys %found )
1148 if ( exists $hash{ $genome }{ $format } ) {
1155 print join( "\t", @row ), "\n";
1160 sub script_read_fasta
1162 # Martin A. Hansen, August 2007.
1164 # Read sequences from FASTA file.
1166 my ( $in, # handle to in stream
1167 $out, # handle to out stream
1168 $options, # options hash
1173 my ( $record, $file, $data_in, $entry, $num );
1175 while ( $record = get_record( $in ) ) {
1176 put_record( $record, $out );
1181 foreach $file ( @{ $options->{ "files" } } )
1183 $data_in = Maasha::Common::read_open( $file );
1185 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1187 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1190 SEQ_NAME => $entry->[ SEQ_NAME ],
1191 SEQ => $entry->[ SEQ ],
1192 SEQ_LEN => length $entry->[ SEQ ],
1195 put_record( $record, $out );
1198 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1208 close $data_in if $data_in;
1214 # Martin A. Hansen, August 2007.
1216 # Read table or table columns from stream or file.
1218 my ( $in, # handle to in stream
1219 $out, # handle to out stream
1220 $options, # options hash
1225 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1227 $options->{ 'delimit' } ||= '\s+';
1229 while ( $record = get_record( $in ) ) {
1230 put_record( $record, $out );
1233 $skip = $options->{ 'skip' } ||= 0;
1236 foreach $file ( @{ $options->{ "files" } } )
1238 $data_in = Maasha::Common::read_open( $file );
1240 while ( $line = <$data_in> )
1248 next if $line =~ /^#|^$/;
1255 @fields = split /$options->{'delimit'}/, $line;
1257 if ( $options->{ "cols" } ) {
1258 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1263 for ( $i = 0; $i < @fields2; $i++ )
1265 if ( $options->{ "keys" }->[ $i ] ) {
1266 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1268 $record->{ "V" . $i } = $fields2[ $i ];
1272 put_record( $record, $out );
1274 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1284 close $data_in if $data_in;
1290 # Martin A. Hansen, August 2007.
1292 # Read psl table from stream or file.
1294 my ( $in, # handle to in stream
1295 $out, # handle to out stream
1296 $options, # options hash
1301 my ( $record, @files, $file, $entries, $entry, $num );
1303 while ( $record = get_record( $in ) ) {
1304 put_record( $record, $out );
1309 foreach $file ( @{ $options->{ "files" } } )
1311 $entries = Maasha::UCSC::psl_get_entries( $file );
1313 foreach $entry ( @{ $entries } )
1315 put_record( $entry, $out );
1317 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1329 # Martin A. Hansen, August 2007.
1331 # Read bed table from stream or file.
1333 my ( $in, # handle to in stream
1334 $out, # handle to out stream
1335 $options, # options hash
1340 my ( $file, $record, $entry, $data_in, $num );
1342 while ( $record = get_record( $in ) ) {
1343 put_record( $record, $out );
1348 foreach $file ( @{ $options->{ "files" } } )
1350 $data_in = Maasha::Common::read_open( $file );
1352 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1354 put_record( $entry, $out );
1356 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1366 close $data_in if $data_in;
1370 sub script_read_fixedstep
1372 # Martin A. Hansen, Juli 2008.
1374 # Read fixedStep wiggle format from stream or file.
1376 my ( $in, # handle to in stream
1377 $out, # handle to out stream
1378 $options, # options hash
1383 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1385 while ( $record = get_record( $in ) ) {
1386 put_record( $record, $out );
1391 foreach $file ( @{ $options->{ "files" } } )
1393 $data_in = Maasha::Common::read_open( $file );
1395 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1397 $head = shift @{ $entry };
1399 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1401 $record->{ "CHR" } = $1;
1402 $record->{ "CHR_BEG" } = $2;
1403 $record->{ "STEP" } = $3;
1404 $record->{ "VALS" } = join ",", @{ $entry };
1407 put_record( $record, $out );
1409 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1419 close $data_in if $data_in;
1423 sub script_read_blast_tab
1425 # Martin A. Hansen, September 2007.
1427 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1429 my ( $in, # handle to in stream
1430 $out, # handle to out stream
1431 $options, # options hash
1436 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1438 while ( $record = get_record( $in ) ) {
1439 put_record( $record, $out );
1444 foreach $file ( @{ $options->{ "files" } } )
1446 $data_in = Maasha::Common::read_open( $file );
1448 while ( $line = <$data_in> )
1452 next if $line =~ /^#/;
1454 @fields = split /\t/, $line;
1456 $record->{ "REC_TYPE" } = "BLAST";
1457 $record->{ "Q_ID" } = $fields[ 0 ];
1458 $record->{ "S_ID" } = $fields[ 1 ];
1459 $record->{ "IDENT" } = $fields[ 2 ];
1460 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1461 $record->{ "MISMATCHES" } = $fields[ 4 ];
1462 $record->{ "GAPS" } = $fields[ 5 ];
1463 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1464 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1465 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1466 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1467 $record->{ "E_VAL" } = $fields[ 10 ];
1468 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1470 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1472 $record->{ "STRAND" } = '-';
1474 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1478 $record->{ "STRAND" } = '+';
1481 put_record( $record, $out );
1483 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1493 close $data_in if $data_in;
1497 sub script_read_embl
1499 # Martin A. Hansen, August 2007.
1503 my ( $in, # handle to in stream
1504 $out, # handle to out stream
1505 $options, # options hash
1510 my ( %options2, $file, $data_in, $num, $entry, $record );
1512 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1513 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1514 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1516 while ( $record = get_record( $in ) ) {
1517 put_record( $record, $out );
1522 foreach $file ( @{ $options->{ "files" } } )
1524 $data_in = Maasha::Common::read_open( $file );
1526 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1528 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1530 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1532 $record_copy = dclone $record;
1534 delete $record_copy->{ "FT" };
1536 put_record( $record_copy, $out );
1538 delete $record_copy->{ "SEQ" };
1540 foreach $feat ( keys %{ $record->{ "FT" } } )
1542 $record_copy->{ "FEAT_TYPE" } = $feat;
1544 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1546 foreach $qual ( keys %{ $feat2 } )
1548 $qual_val = join "; ", @{ $feat2->{ $qual } };
1553 $record_copy->{ $qual } = $qual_val;
1556 put_record( $record_copy, $out );
1560 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1570 close $data_in if $data_in;
1574 sub script_read_stockholm
1576 # Martin A. Hansen, August 2007.
1578 # Read Stockholm format.
1580 my ( $in, # handle to in stream
1581 $out, # handle to out stream
1582 $options, # options hash
1587 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1589 while ( $record = get_record( $in ) ) {
1590 put_record( $record, $out );
1595 foreach $file ( @{ $options->{ "files" } } )
1597 $data_in = Maasha::Common::read_open( $file );
1599 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1601 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1605 foreach $key ( keys %{ $record->{ "GF" } } ) {
1606 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1609 $record_anno->{ "ALIGN" } = $num;
1611 put_record( $record_anno, $out );
1613 foreach $seq ( @{ $record->{ "ALIGN" } } )
1615 undef $record_align;
1618 SEQ_NAME => $seq->[ 0 ],
1622 put_record( $record_align, $out );
1625 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1635 close $data_in if $data_in;
1639 sub script_read_phastcons
1641 # Martin A. Hansen, December 2007.
1643 # Read PhastCons format.
1645 my ( $in, # handle to in stream
1646 $out, # handle to out stream
1647 $options, # options hash
1652 my ( $data_in, $file, $num, $entry, @records, $record );
1654 $options->{ "min" } ||= 10;
1655 $options->{ "dist" } ||= 25;
1656 $options->{ "threshold" } ||= 0.8;
1657 $options->{ "gap" } ||= 5;
1659 while ( $record = get_record( $in ) ) {
1660 put_record( $record, $out );
1665 foreach $file ( @{ $options->{ "files" } } )
1667 $data_in = Maasha::Common::read_open( $file );
1669 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1671 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1673 foreach $record ( @records )
1675 $record->{ "REC_TYPE" } = "BED";
1676 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1678 put_record( $record, $out );
1680 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1691 close $data_in if $data_in;
1695 sub script_read_soft
1697 # Martin A. Hansen, December 2007.
1700 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1702 my ( $in, # handle to in stream
1703 $out, # handle to out stream
1704 $options, # options hash
1709 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1711 while ( $record = get_record( $in ) ) {
1712 put_record( $record, $out );
1717 foreach $file ( @{ $options->{ "files" } } )
1719 $soft_index = Maasha::NCBI::soft_index_file( $file );
1721 $fh = Maasha::Common::read_open( $file );
1723 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1725 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1727 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1729 $old_end = $platforms[ -1 ]->[ 2 ];
1731 foreach $sample ( @samples )
1733 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1735 foreach $record ( @{ $records } )
1737 put_record( $record, $out );
1739 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1744 $old_end = $sample->[ 2 ];
1752 close $data_in if $data_in;
1759 # Martin A. Hansen, February 2008.
1762 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1764 my ( $in, # handle to in stream
1765 $out, # handle to out stream
1766 $options, # options hash
1771 my ( $data_in, $file, $fh, $num, $record, $entry );
1773 while ( $record = get_record( $in ) ) {
1774 put_record( $record, $out );
1779 foreach $file ( @{ $options->{ "files" } } )
1781 $fh = Maasha::Common::read_open( $file );
1783 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1785 put_record( $entry, $out );
1787 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1797 close $data_in if $data_in;
1801 sub script_read_2bit
1803 # Martin A. Hansen, March 2008.
1805 # Read sequences from 2bit file.
1807 my ( $in, # handle to in stream
1808 $out, # handle to out stream
1809 $options, # options hash
1814 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1816 $mask = 1 if not $options->{ "no_mask" };
1818 while ( $record = get_record( $in ) ) {
1819 put_record( $record, $out );
1824 foreach $file ( @{ $options->{ "files" } } )
1826 $data_in = Maasha::Common::read_open( $file );
1828 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1830 foreach $line ( @{ $toc } )
1832 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1833 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1834 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1836 put_record( $record, $out );
1838 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1848 close $data_in if $data_in;
1852 sub script_read_solexa
1854 # Martin A. Hansen, March 2008.
1856 # Read Solexa sequence reads from file.
1858 my ( $in, # handle to in stream
1859 $out, # handle to out stream
1860 $options, # options hash
1865 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1867 $options->{ "quality" } ||= 20;
1869 while ( $record = get_record( $in ) ) {
1870 put_record( $record, $out );
1875 foreach $file ( @{ $options->{ "files" } } )
1877 $data_in = Maasha::Common::read_open( $file );
1878 $base_name = Maasha::Common::get_basename( $file );
1879 $base_name =~ s/\..*//;
1883 while ( $line = <$data_in> )
1885 @fields = split /:/, $line;
1886 @seqs = split //, $fields[ 5 ];
1887 @scores = split / /, $fields[ -1 ];
1889 for ( $i = 0; $i < @scores; $i++ ) {
1890 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1893 $seq = join "", @seqs;
1895 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1896 $record->{ "SEQ" } = $seq;
1897 $record->{ "SEQ_LEN" } = length $seq;
1898 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1900 put_record( $record, $out );
1902 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1913 close $data_in if $data_in;
1917 sub script_read_solid
1919 # Martin A. Hansen, April 2008.
1921 # Read Solid sequence from file.
1923 my ( $in, # handle to in stream
1924 $out, # handle to out stream
1925 $options, # options hash
1930 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1932 $options->{ "quality" } ||= 15;
1934 while ( $record = get_record( $in ) ) {
1935 put_record( $record, $out );
1940 foreach $file ( @{ $options->{ "files" } } )
1942 $data_in = Maasha::Common::read_open( $file );
1944 while ( $line = <$data_in> )
1948 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1950 @scores = split /,/, $seq_qual;
1951 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1953 for ( $i = 0; $i < @seqs; $i++ ) {
1954 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1958 SEQ_NAME => $seq_name,
1960 SEQ_QUAL => $seq_qual,
1961 SEQ_LEN => length $seq_cs,
1962 SEQ => join( "", @seqs ),
1963 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1966 put_record( $record, $out );
1968 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1978 close $data_in if $data_in;
1982 sub script_read_mysql
1984 # Martin A. Hansen, May 2008.
1986 # Read a MySQL query into stream.
1988 my ( $in, # handle to in stream
1989 $out, # handle to out stream
1990 $options, # options hash
1995 my ( $record, $dbh, $results );
1997 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1998 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2000 while ( $record = get_record( $in ) ) {
2001 put_record( $record, $out );
2004 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2006 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2008 Maasha::SQL::disconnect( $dbh );
2010 map { put_record( $_ ) } @{ $results };
2014 sub script_format_genome
2016 # Martin A. Hansen, Juli 2008.
2018 # Format a genome to speficed formats.
2020 my ( $in, # handle to in stream
2021 $out, # handle to out stream
2022 $options, # options hash
2027 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index );
2029 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2030 $genome = $options->{ 'genome' };
2032 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2033 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2034 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2036 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2038 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2040 $fasta_dir = "$dir/genomes/$genome/fasta";
2044 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2046 $fasta_dir = "$dir/genomes/$genome/fasta";
2048 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2051 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2053 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2055 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2057 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2060 while ( $record = get_record( $in ) )
2062 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2064 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
2066 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2068 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2070 $vals = $record->{ 'VALS' };
2074 print $fh_out "$vals\n";
2077 put_record( $record, $out ) if not $options->{ "no_stream" };
2080 foreach $format ( @{ $options->{ 'formats' } } )
2082 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2083 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2084 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2085 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2086 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2089 close $fh_out if $fh_out;
2093 sub script_length_seq
2095 # Martin A. Hansen, August 2007.
2097 # Determine the length of sequences in stream.
2099 my ( $in, # handle to in stream
2100 $out, # handle to out stream
2101 $options, # options hash
2106 my ( $record, $total );
2108 while ( $record = get_record( $in ) )
2110 if ( $record->{ "SEQ" } )
2112 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2113 $total += $record->{ "SEQ_LEN" };
2116 put_record( $record, $out ) if not $options->{ "no_stream" };
2119 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2123 sub script_uppercase_seq
2125 # Martin A. Hansen, August 2007.
2127 # Uppercases sequences in stream.
2129 my ( $in, # handle to in stream
2130 $out, # handle to out stream
2137 while ( $record = get_record( $in ) )
2139 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2141 put_record( $record, $out );
2146 sub script_shuffle_seq
2148 # Martin A. Hansen, December 2007.
2150 # Shuffle sequences in stream.
2152 my ( $in, # handle to in stream
2153 $out, # handle to out stream
2160 while ( $record = get_record( $in ) )
2162 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2164 put_record( $record, $out );
2169 sub script_analyze_seq
2171 # Martin A. Hansen, August 2007.
2173 # Analyze sequence composition of sequences in stream.
2175 my ( $in, # handle to in stream
2176 $out, # handle to out stream
2181 my ( $record, $analysis );
2183 while ( $record = get_record( $in ) )
2185 if ( $record->{ "SEQ" } )
2187 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2189 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2192 put_record( $record, $out );
2197 sub script_analyze_tags
2199 # Martin A. Hansen, August 2008.
2201 # Analyze sequence tags in stream.
2203 my ( $in, # handle to in stream
2204 $out, # handle to out stream
2209 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2211 while ( $record = get_record( $in ) )
2213 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2215 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2219 $len_hash{ length( $record->{ "SEQ" } ) }++;
2220 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2223 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2225 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2229 $len_hash{ $record->{ "BED_LEN" } }++;
2230 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2235 foreach $key ( sort { $a <=> $b } keys %len_hash )
2237 $tag_record->{ "TAG_LEN" } = $key;
2238 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2239 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2241 put_record( $tag_record, $out );
2246 sub script_complexity_seq
2248 # Martin A. Hansen, May 2008.
2250 # Generates an index calculated as the most common di-residue over
2251 # the sequence length for all sequences in stream.
2253 my ( $in, # handle to in stream
2254 $out, # handle to out stream
2259 my ( $record, $index );
2261 while ( $record = get_record( $in ) )
2263 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2265 put_record( $record, $out );
2270 sub script_oligo_freq
2272 # Martin A. Hansen, August 2007.
2274 # Determine the length of sequences in stream.
2276 my ( $in, # handle to in stream
2277 $out, # handle to out stream
2278 $options, # options hash
2283 my ( $record, %oligos, @freq_table );
2285 $options->{ "word_size" } ||= 7;
2287 while ( $record = get_record( $in ) )
2289 if ( $record->{ "SEQ" } )
2291 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2293 if ( not $options->{ "all" } )
2295 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2297 map { put_record( $_, $out ) } @freq_table;
2303 put_record( $record, $out );
2306 if ( $options->{ "all" } )
2308 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2310 map { put_record( $_, $out ) } @freq_table;
2315 sub script_create_weight_matrix
2317 # Martin A. Hansen, August 2007.
2319 # Creates a weight matrix from an alignmnet.
2321 my ( $in, # handle to in stream
2322 $out, # handle to out stream
2323 $options, # options hash
2328 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2332 while ( $record = get_record( $in ) )
2334 if ( $record->{ "SEQ" } )
2336 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2338 $res = substr $record->{ "SEQ" }, $i, 1;
2340 $freq_hash{ $i }{ $res }++;
2341 $res_hash{ $res } = 1;
2348 put_record( $record, $out );
2352 foreach $res ( sort keys %res_hash )
2356 $record->{ "V0" } = $res;
2358 for ( $i = 0; $i < keys %freq_hash; $i++ )
2360 $freq = $freq_hash{ $i }{ $res } || 0;
2362 if ( $options->{ "percent" } ) {
2363 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2366 $record->{ "V" . ( $i + 1 ) } = $freq;
2369 put_record( $record, $out );
2374 sub script_calc_bit_scores
2376 # Martin A. Hansen, March 2007.
2378 # Calculates the bit scores for each position from an alignmnet in the stream.
2380 my ( $in, # handle to in stream
2381 $out, # handle to out stream
2386 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2390 while ( $record = get_record( $in ) )
2392 if ( $record->{ "SEQ" } )
2394 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2396 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2398 $res = substr $record->{ "SEQ" }, $i, 1;
2400 next if $res =~ /-|_|~|\./;
2402 $freq_hash{ $i }{ $res }++;
2409 put_record( $record, $out );
2415 if ( $type eq "protein" ) {
2421 for ( $i = 0; $i < keys %freq_hash; $i++ )
2423 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2425 $bit_diff = $bit_max - $bit_height;
2427 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2430 put_record( $record, $out );
2434 sub script_reverse_seq
2436 # Martin A. Hansen, August 2007.
2438 # Reverse sequence in record.
2440 my ( $in, # handle to in stream
2441 $out, # handle to out stream
2448 while ( $record = get_record( $in ) )
2450 if ( $record->{ "SEQ" } ) {
2451 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2454 put_record( $record, $out );
2459 sub script_complement_seq
2461 # Martin A. Hansen, August 2007.
2463 # Complement sequence in record.
2465 my ( $in, # handle to in stream
2466 $out, # handle to out stream
2471 my ( $record, $type );
2473 while ( $record = get_record( $in ) )
2475 if ( $record->{ "SEQ" } )
2478 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2481 if ( $type eq "rna" ) {
2482 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2483 } elsif ( $type eq "dna" ) {
2484 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2488 put_record( $record, $out );
2493 sub script_remove_indels
2495 # Martin A. Hansen, August 2007.
2497 # Remove indels from sequences in stream.
2499 my ( $in, # handle to in stream
2500 $out, # handle to out stream
2507 while ( $record = get_record( $in ) )
2509 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2511 put_record( $record, $out );
2516 sub script_transliterate_seq
2518 # Martin A. Hansen, August 2007.
2520 # Transliterate chars from sequence in record.
2522 my ( $in, # handle to in stream
2523 $out, # handle to out stream
2524 $options, # options hash
2529 my ( $record, $search, $replace, $delete );
2531 $search = $options->{ "search" } || "";
2532 $replace = $options->{ "replace" } || "";
2533 $delete = $options->{ "delete" } || "";
2535 while ( $record = get_record( $in ) )
2537 if ( $record->{ "SEQ" } )
2539 if ( $search and $replace ) {
2540 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2541 } elsif ( $delete ) {
2542 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2546 put_record( $record, $out );
2551 sub script_transliterate_vals
2553 # Martin A. Hansen, April 2008.
2555 # Transliterate chars from values in record.
2557 my ( $in, # handle to in stream
2558 $out, # handle to out stream
2559 $options, # options hash
2564 my ( $record, $search, $replace, $delete, $key );
2566 $search = $options->{ "search" } || "";
2567 $replace = $options->{ "replace" } || "";
2568 $delete = $options->{ "delete" } || "";
2570 while ( $record = get_record( $in ) )
2572 foreach $key ( @{ $options->{ "keys" } } )
2574 if ( exists $record->{ $key } )
2576 if ( $search and $replace ) {
2577 eval "\$record->{ $key } =~ tr/$search/$replace/";
2578 } elsif ( $delete ) {
2579 eval "\$record->{ $key } =~ tr/$delete//d";
2584 put_record( $record, $out );
2589 sub script_translate_seq
2591 # Martin A. Hansen, February 2008.
2593 # Translate DNA sequence into protein sequence.
2595 my ( $in, # handle to in stream
2596 $out, # handle to out stream
2597 $options, # options hash
2602 my ( $record, $frame, %new_record );
2604 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2606 while ( $record = get_record( $in ) )
2608 if ( $record->{ "SEQ" } )
2610 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2612 foreach $frame ( @{ $options->{ "frames" } } )
2614 %new_record = %{ $record };
2616 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2617 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2618 $new_record{ "FRAME" } = $frame;
2620 put_record( \%new_record, $out );
2626 put_record( $record, $out );
2632 sub script_extract_seq
2634 # Martin A. Hansen, August 2007.
2636 # Extract subsequences from sequences in record.
2638 my ( $in, # handle to in stream
2639 $out, # handle to out stream
2640 $options, # options hash
2645 my ( $beg, $end, $len, $record );
2647 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2650 $beg = $options->{ "beg" } - 1; # correcting for start offset
2653 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2655 } elsif ( defined $options->{ "end" } ) {
2656 $end = $options->{ "end" } - 1; # correcting for start offset
2659 $len = $options->{ "len" };
2661 # print "beg->$beg, end->$end, len->$len\n";
2663 while ( $record = get_record( $in ) )
2665 if ( $record->{ "SEQ" } )
2667 if ( defined $beg and defined $end )
2669 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2670 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2672 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2675 elsif ( defined $beg and defined $len )
2677 if ( $len > length $record->{ "SEQ" } ) {
2678 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2680 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2683 elsif ( defined $beg )
2685 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2689 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2691 put_record( $record, $out );
2696 sub script_get_genome_seq
2698 # Martin A. Hansen, December 2007.
2700 # Gets a subsequence from a genome.
2702 my ( $in, # handle to in stream
2703 $out, # handle to out stream
2704 $options, # options hash
2709 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2711 $options->{ "flank" } ||= 0;
2713 if ( $options->{ "genome" } )
2715 $genome = $options->{ "genome" };
2717 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2718 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2720 $fh = Maasha::Common::read_open( $genome_file );
2721 $index = Maasha::Fasta::index_retrieve( $index_file );
2723 shift @{ $index }; # Get rid of the file size info
2725 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2727 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2729 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2731 $beg = $index_beg + $options->{ "beg" } - 1;
2733 if ( $options->{ "len" } ) {
2734 $len = $options->{ "len" };
2735 } elsif ( $options->{ "end" } ) {
2736 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2739 $beg -= $options->{ "flank" };
2740 $len += 2 * $options->{ "flank" };
2742 if ( $beg <= $index_beg )
2744 $len -= $index_beg - $beg;
2748 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2750 next if $beg > $index_beg + $index_len;
2752 $record->{ "CHR" } = $options->{ "chr" };
2753 $record->{ "CHR_BEG" } = $beg - $index_beg;
2754 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2756 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2757 $record->{ "SEQ_LEN" } = $len;
2759 put_record( $record, $out );
2763 while ( $record = get_record( $in ) )
2765 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2767 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2769 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2771 $beg = $record->{ "CHR_BEG" } + $index_beg;
2772 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2774 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2776 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2778 $beg = $record->{ "S_BEG" } + $index_beg;
2779 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2781 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2783 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2785 $beg = $record->{ "S_BEG" } + $index_beg;
2786 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2789 $beg -= $options->{ "flank" };
2790 $len += 2 * $options->{ "flank" };
2792 if ( $beg <= $index_beg )
2794 $len -= $index_beg - $beg;
2798 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2800 next if $beg > $index_beg + $index_len;
2802 $record->{ "CHR_BEG" } = $beg - $index_beg;
2803 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2805 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2807 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2809 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2810 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2813 if ( $options->{ "mask" } )
2815 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2817 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2819 @begs = split ",", $record->{ "Q_BEGS" };
2820 @lens = split ",", $record->{ "BLOCKSIZES" };
2822 for ( $i = 0; $i < @begs; $i++ ) {
2823 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2829 put_record( $record, $out );
2836 sub script_get_genome_align
2838 # Martin A. Hansen, April 2008.
2840 # Gets a subalignment from a multiple genome alignment.
2842 my ( $in, # handle to in stream
2843 $out, # handle to out stream
2844 $options, # options hash
2849 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2851 $options->{ "strand" } ||= "+";
2855 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2857 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2859 $beg = $options->{ "beg" } - 1;
2861 if ( $options->{ "end" } ) {
2862 $end = $options->{ "end" };
2863 } elsif ( $options->{ "len" } ) {
2864 $end = $beg + $options->{ "len" };
2867 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2869 foreach $entry ( @{ $align } )
2871 $entry->{ "CHR" } = $record->{ "CHR" };
2872 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2873 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2874 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2875 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2876 $entry->{ "SCORE" } = $record->{ "SCORE" };
2878 put_record( $entry, $out );
2882 while ( $record = get_record( $in ) )
2884 if ( $record->{ "REC_TYPE" } eq "BED" )
2886 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2888 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2890 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2892 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2894 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2897 foreach $entry ( @{ $align } )
2899 $entry->{ "CHR" } = $record->{ "CHR" };
2900 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2901 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2902 $entry->{ "STRAND" } = $record->{ "STRAND" };
2903 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2904 $entry->{ "SCORE" } = $record->{ "SCORE" };
2906 put_record( $entry, $out );
2914 sub script_get_genome_phastcons
2916 # Martin A. Hansen, February 2008.
2918 # Get phastcons scores from genome intervals.
2920 my ( $in, # handle to in stream
2921 $out, # handle to out stream
2922 $options, # options hash
2927 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2929 $options->{ "flank" } ||= 0;
2931 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2932 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2934 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2935 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2937 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2939 $options->{ "beg" } -= 1; # request is 1-based
2940 $options->{ "end" } -= 1; # request is 1-based
2942 if ( $options->{ "len" } ) {
2943 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2946 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2948 $record->{ "CHR" } = $options->{ "chr" };
2949 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2950 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2952 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2953 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2955 put_record( $record, $out );
2958 while ( $record = get_record( $in ) )
2960 if ( $record->{ "REC_TYPE" } eq "BED" )
2962 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2964 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2966 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2968 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2970 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2973 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2974 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2976 put_record( $record, $out );
2979 close $fh_phastcons if $fh_phastcons;
2985 # Martin A. Hansen, December 2007.
2987 # Folds sequences in stream into secondary structures.
2989 my ( $in, # handle to in stream
2990 $out, # handle to out stream
2995 my ( $record, $type, $struct, $index );
2997 while ( $record = get_record( $in ) )
2999 if ( $record->{ "SEQ" } )
3002 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3005 if ( $type ne "protein" )
3007 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3008 $record->{ "SEC_STRUCT" } = $struct;
3009 $record->{ "FREE_ENERGY" } = $index;
3010 $record->{ "SCORE" } = abs int $index;
3011 $record->{ "SIZE" } = length $struct;
3012 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3016 put_record( $record, $out );
3021 sub script_split_seq
3023 # Martin A. Hansen, August 2007.
3025 # Split a sequence in stream into words.
3027 my ( $in, # handle to in stream
3028 $out, # handle to out stream
3029 $options, # options hash
3034 my ( $record, $new_record, $i, $subseq, %lookup );
3036 $options->{ "word_size" } ||= 7;
3038 while ( $record = get_record( $in ) )
3040 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3042 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3044 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3046 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3048 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3049 $new_record->{ "SEQ" } = $subseq;
3051 put_record( $new_record, $out );
3053 $lookup{ $subseq } = 1;
3057 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3058 $new_record->{ "SEQ" } = $subseq;
3060 put_record( $new_record, $out );
3066 put_record( $record, $out );
3072 sub script_split_bed
3074 # Martin A. Hansen, June 2008.
3076 # Split a BED record into overlapping windows.
3078 my ( $in, # handle to in stream
3079 $out, # handle to out stream
3080 $options, # options hash
3085 my ( $record, $new_record, $i );
3087 $options->{ "window_size" } ||= 20;
3088 $options->{ "step_size" } ||= 1;
3090 while ( $record = get_record( $in ) )
3092 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3094 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3096 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3098 $new_record->{ "REC_TYPE" } = "BED";
3099 $new_record->{ "CHR" } = $record->{ "CHR" };
3100 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3101 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3102 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3103 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3104 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3105 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3107 put_record( $new_record, $out );
3112 put_record( $record, $out );
3118 sub script_align_seq
3120 # Martin A. Hansen, August 2007.
3122 # Align sequences in stream.
3124 my ( $in, # handle to in stream
3125 $out, # handle to out stream
3130 my ( $record, @entries, $entry );
3132 while ( $record = get_record( $in ) )
3134 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3135 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3136 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3137 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3139 put_record( $record, $out );
3143 @entries = Maasha::Align::align( \@entries );
3145 foreach $entry ( @entries )
3147 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3150 SEQ_NAME => $entry->[ SEQ_NAME ],
3151 SEQ => $entry->[ SEQ ],
3154 put_record( $record, $out );
3162 # Martin A. Hansen, February 2008.
3164 # Using the first sequence in stream as reference, tile
3165 # all subsequent sequences based on pairwise alignments.
3167 my ( $in, # handle to in stream
3168 $out, # handle to out stream
3169 $options, # options hash
3174 my ( $record, $first, $ref_entry, @entries );
3178 while ( $record = get_record( $in ) )
3180 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3184 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3190 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3195 put_record( $record, $out );
3199 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3201 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3205 sub script_invert_align
3207 # Martin A. Hansen, February 2008.
3209 # Inverts an alignment showing only non-mathing residues
3210 # using the first sequence as reference.
3212 my ( $in, # handle to in stream
3213 $out, # handle to out stream
3214 $options, # options hash
3219 my ( $record, @entries );
3221 while ( $record = get_record( $in ) )
3223 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3225 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3229 put_record( $record, $out );
3233 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3235 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3239 sub script_patscan_seq
3241 # Martin A. Hansen, August 2007.
3243 # Locates patterns in sequences using scan_for_matches.
3245 my ( $in, # handle to in stream
3246 $out, # handle to out stream
3247 $options, # options hash
3252 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3254 if ( $options->{ "patterns" } ) {
3255 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3256 } elsif ( -f $options->{ "patterns_in" } ) {
3257 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3260 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3262 push @args, "-c" if $options->{ "comp" };
3263 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3264 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3266 $seq_file = "$BP_TMP/patscan.seq";
3267 $pat_file = "$BP_TMP/patscan.pat";
3268 $out_file = "$BP_TMP/patscan.out";
3270 $fh_out = Maasha::Common::write_open( $seq_file );
3274 while ( $record = get_record( $in ) )
3276 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3278 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3280 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3282 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3287 # put_record( $record, $out );
3292 $arg = join " ", @args;
3293 $arg .= " -p" if $type eq "protein";
3295 foreach $pattern ( @{ $patterns } )
3297 $fh_out = Maasha::Common::write_open( $pat_file );
3299 print $fh_out "$pattern\n";
3303 if ( $options->{ 'genome' } ) {
3304 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3305 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3307 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3308 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3311 $fh_in = Maasha::Common::read_open( $out_file );
3313 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3315 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3317 if ( $options->{ 'genome' } )
3319 $result->{ "CHR" } = $result->{ "S_ID" };
3320 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3321 $result->{ "CHR_END" } = $result->{ "S_END" };
3323 delete $result->{ "S_ID" };
3324 delete $result->{ "S_BEG" };
3325 delete $result->{ "S_END" };
3329 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3332 put_record( $result, $out );
3344 sub script_create_blast_db
3346 # Martin A. Hansen, September 2007.
3348 # Creates a NCBI BLAST database with formatdb
3350 my ( $in, # handle to in stream
3351 $out, # handle to out stream
3352 $options, # options hash
3357 my ( $fh, $seq_type, $path, $record );
3359 $path = $options->{ "database" };
3361 $fh = Maasha::Common::write_open( $path );
3363 while ( $record = get_record( $in ) )
3365 put_record( $record, $out ) if not $options->{ "no_stream" };
3367 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3369 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3371 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3377 if ( $seq_type eq "protein" ) {
3378 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3380 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3387 sub script_blast_seq
3389 # Martin A. Hansen, September 2007.
3391 # BLASTs sequences in stream against a given database.
3393 my ( $in, # handle to in stream
3394 $out, # handle to out stream
3395 $options, # options hash
3400 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3402 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3403 $options->{ "filter" } = "F";
3404 $options->{ "filter" } = "T" if $options->{ "filter" };
3405 $options->{ "cpus" } ||= 1;
3407 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna"if $options->{ 'genome' };
3409 $tmp_in = "$BP_TMP/blast_query.seq";
3410 $tmp_out = "$BP_TMP/blast.result";
3412 $fh_out = Maasha::Common::write_open( $tmp_in );
3414 while ( $record = get_record( $in ) )
3416 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3418 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3420 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3423 put_record( $record, $out );
3428 if ( -f $options->{ 'database' } . ".phr" ) {
3429 $s_type = "protein";
3431 $s_type = "nucleotide";
3434 if ( not $options->{ 'program' } )
3436 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3437 $options->{ 'program' } = "blastn";
3438 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3439 $options->{ 'program' } = "blastp";
3440 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3441 $options->{ 'program' } = "blastx";
3442 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3443 $options->{ 'program' } = "tblastn";
3447 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 );
3451 $fh_out = Maasha::Common::read_open( $tmp_out );
3455 while ( $line = <$fh_out> )
3459 next if $line =~ /^#/;
3461 @fields = split /\s+/, $line;
3463 $record->{ "REC_TYPE" } = "BLAST";
3464 $record->{ "Q_ID" } = $fields[ 0 ];
3465 $record->{ "S_ID" } = $fields[ 1 ];
3466 $record->{ "IDENT" } = $fields[ 2 ];
3467 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3468 $record->{ "MISMATCHES" } = $fields[ 4 ];
3469 $record->{ "GAPS" } = $fields[ 5 ];
3470 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3471 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3472 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3473 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3474 $record->{ "E_VAL" } = $fields[ 10 ];
3475 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3477 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3479 $record->{ "STRAND" } = '-';
3481 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3485 $record->{ "STRAND" } = '+';
3488 put_record( $record, $out );
3499 # Martin A. Hansen, August 2007.
3501 # BLATs sequences in stream against a given genome.
3503 my ( $in, # handle to in stream
3504 $out, # handle to out stream
3505 $options, # options hash
3510 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3512 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3514 $options->{ 'tile_size' } ||= 11;
3515 $options->{ 'one_off' } ||= 0;
3516 $options->{ 'min_identity' } ||= 90;
3517 $options->{ 'min_score' } ||= 0;
3518 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3520 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3521 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3522 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3523 $blat_args .= " -minScore=$options->{ 'min_score' }";
3524 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3525 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3527 $query_file = "$BP_TMP/blat.seq";
3529 $fh_out = Maasha::Common::write_open( $query_file );
3531 while ( $record = get_record( $in ) )
3533 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3535 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3536 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3539 put_record( $record, $out );
3544 $blat_args .= " -t=dnax" if $type eq "protein";
3545 $blat_args .= " -q=$type";
3547 $result_file = "$BP_TMP/blat.psl";
3549 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3553 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3555 map { put_record( $_, $out ) } @{ $entries };
3557 unlink $result_file;
3561 sub script_match_seq
3563 # Martin A. Hansen, August 2007.
3565 # BLATs sequences in stream against a given genome.
3567 my ( $in, # handle to in stream
3568 $out, # handle to out stream
3569 $options, # options hash
3574 my ( $record, @entries, $results );
3576 $options->{ "word_size" } ||= 20;
3577 $options->{ "direction" } ||= "both";
3579 while ( $record = get_record( $in ) )
3581 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3582 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3585 put_record( $record, $out );
3588 if ( @entries == 1 )
3590 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3592 map { put_record( $_, $out ) } @{ $results };
3594 elsif ( @entries == 2 )
3596 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3598 map { put_record( $_, $out ) } @{ $results };
3603 sub script_create_vmatch_index
3605 # Martin A. Hansen, January 2008.
3607 # Create a vmatch index from sequences in the stream.
3609 my ( $in, # handle to in stream
3610 $out, # handle to out stream
3611 $options, # options hash
3616 my ( $record, $file_tmp, $fh_tmp, $type );
3618 if ( $options->{ "index_name" } )
3620 $file_tmp = $options->{ 'index_name' };
3621 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3624 while ( $record = get_record( $in ) )
3626 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3628 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3630 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3633 put_record( $record, $out ) if not $options->{ "no_stream" };
3636 if ( $options->{ "index_name" } )
3640 if ( $type eq "protein" ) {
3641 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3643 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3651 sub script_vmatch_seq
3653 # Martin A. Hansen, August 2007.
3655 # Vmatches sequences in stream against a given genome.
3657 my ( $in, # handle to in stream
3658 $out, # handle to out stream
3659 $options, # options hash
3664 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3666 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3668 if ( $options->{ "index_name" } )
3670 @index_files = $options->{ "index_name" };
3674 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3676 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3678 @index_files = sort keys %hash;
3681 while ( $record = get_record( $in ) )
3683 push @records, $record;
3685 put_record( $record, $out );
3688 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3692 $fh_in = Maasha::Common::read_open( $result_file );
3694 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3695 put_record( $record, $out );
3700 unlink $result_file;
3704 sub script_write_fasta
3706 # Martin A. Hansen, August 2007.
3708 # Write FASTA entries from sequences in stream.
3710 my ( $in, # handle to in stream
3711 $out, # handle to out stream
3712 $options, # options hash
3717 my ( $record, $fh );
3719 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3721 while ( $record = get_record( $in ) )
3723 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3724 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3727 put_record( $record, $out ) if not $options->{ "no_stream" };
3734 sub script_write_align
3736 # Martin A. Hansen, August 2007.
3738 # Write pretty alignments aligned sequences in stream.
3740 my ( $in, # handle to in stream
3741 $out, # handle to out stream
3742 $options, # options hash
3747 my ( $fh, $record, @entries );
3749 $fh = write_stream( $options->{ "data_out" } ) ;
3751 while ( $record = get_record( $in ) )
3753 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3754 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3757 put_record( $record, $out ) if not $options->{ "no_stream" };
3760 if ( scalar( @entries ) == 2 ) {
3761 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3762 } elsif ( scalar ( @entries ) > 2 ) {
3763 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3770 sub script_write_blast
3772 # Martin A. Hansen, November 2007.
3774 # Write data in blast table format (-m8 and 9).
3776 my ( $in, # handle to in stream
3777 $out, # handle to out stream
3778 $options, # options hash
3783 my ( $fh, $record, $first );
3785 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3789 while ( $record = get_record( $in ) )
3791 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3793 if ( $options->{ "comment" } and $first )
3795 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";
3800 if ( $record->{ "STRAND" } eq "-" ) {
3801 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3804 print $fh join( "\t",
3805 $record->{ "Q_ID" },
3806 $record->{ "S_ID" },
3807 $record->{ "IDENT" },
3808 $record->{ "ALIGN_LEN" },
3809 $record->{ "MISMATCHES" },
3810 $record->{ "GAPS" },
3811 $record->{ "Q_BEG" } + 1,
3812 $record->{ "Q_END" } + 1,
3813 $record->{ "S_BEG" } + 1,
3814 $record->{ "S_END" } + 1,
3815 $record->{ "E_VAL" },
3816 $record->{ "BIT_SCORE" }
3820 put_record( $record, $out ) if not $options->{ "no_stream" };
3827 sub script_write_tab
3829 # Martin A. Hansen, August 2007.
3831 # Write data as table.
3833 my ( $in, # handle to in stream
3834 $out, # handle to out stream
3835 $options, # options hash
3840 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3842 $options->{ "delimit" } ||= "\t";
3844 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3846 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3848 while ( $record = get_record( $in ) )
3853 if ( $options->{ "keys" } )
3855 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3859 foreach $key ( @{ $options->{ "keys" } } )
3861 if ( exists $record->{ $key } )
3863 push @keys, $key if $options->{ "comment" };
3864 push @vals, $record->{ $key };
3871 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3873 next if exists $no_keys{ $key };
3875 push @keys, $key if $options->{ "comment" };
3876 push @vals, $record->{ $key };
3880 if ( @keys and $options->{ "comment" } )
3882 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3884 delete $options->{ "comment" };
3887 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3889 put_record( $record, $out ) if not $options->{ "no_stream" };
3896 sub script_write_bed
3898 # Martin A. Hansen, August 2007.
3900 # Write BED format for the UCSC genome browser using records in stream.
3902 my ( $in, # handle to in stream
3903 $out, # handle to out stream
3904 $options, # options hash
3909 my ( $fh, $record, $new_record );
3911 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3913 while ( $record = get_record( $in ) )
3915 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3917 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3919 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3921 $new_record->{ "CHR" } = $record->{ "S_ID" };
3922 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3923 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3924 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3925 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3926 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3928 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3930 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3932 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3934 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3936 $new_record->{ "CHR" } = $record->{ "S_ID" };
3937 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3938 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3939 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3940 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3941 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3943 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3945 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3947 $new_record->{ "CHR" } = $record->{ "S_ID" };
3948 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3949 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3950 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3951 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3952 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3954 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3956 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3958 Maasha::UCSC::bed_put_entry( $record, $fh );
3961 put_record( $record, $out ) if not $options->{ "no_stream" };
3968 sub script_write_psl
3970 # Martin A. Hansen, August 2007.
3972 # Write PSL output from stream.
3974 my ( $in, # handle to in stream
3975 $out, # handle to out stream
3976 $options, # options hash
3981 my ( $fh, $record, @output, $first );
3985 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3987 while ( $record = get_record( $in ) )
3989 put_record( $record, $out ) if not $options->{ "no_stream" };
3991 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3993 Maasha::UCSC::psl_put_header( $fh ) if $first;
3994 Maasha::UCSC::psl_put_entry( $record, $fh );
4003 sub script_write_fixedstep
4005 # Martin A. Hansen, Juli 2008.
4007 # Write fixedStep entries from recrods in the stream.
4009 my ( $in, # handle to in stream
4010 $out, # handle to out stream
4011 $options, # options hash
4016 my ( $fh, $record, $vals );
4018 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4020 while ( $record = get_record( $in ) )
4022 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4024 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4026 $vals = $record->{ 'VALS' };
4030 print $fh "$vals\n";
4033 put_record( $record, $out ) if not $options->{ "no_stream" };
4040 sub script_write_2bit
4042 # Martin A. Hansen, March 2008.
4044 # Write sequence entries from stream in 2bit format.
4046 my ( $in, # handle to in stream
4047 $out, # handle to out stream
4048 $options, # options hash
4053 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
4055 $mask = 1 if not $options->{ "no_mask" };
4057 $tmp_file = "$BP_TMP/write_2bit.fna";
4058 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4060 $fh_out = write_stream( $options->{ "data_out" } );
4062 while ( $record = get_record( $in ) )
4064 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4065 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
4068 put_record( $record, $out ) if not $options->{ "no_stream" };
4073 $fh_in = Maasha::Common::read_open( $tmp_file );
4075 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4084 sub script_write_solid
4086 # Martin A. Hansen, April 2008.
4088 # Write di-base encoded Solid sequence from entries in stream.
4090 my ( $in, # handle to in stream
4091 $out, # handle to out stream
4092 $options, # options hash
4097 my ( $record, $fh, $seq_cs );
4099 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4101 while ( $record = get_record( $in ) )
4103 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
4105 $seq_cs = Maasha::Solid::seq2color_space( uc $record->{ "SEQ" } );
4107 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
4110 put_record( $record, $out ) if not $options->{ "no_stream" };
4117 sub script_plot_seqlogo
4119 # Martin A. Hansen, August 2007.
4121 # Calculates and writes a sequence logo for alignments.
4123 my ( $in, # handle to in stream
4124 $out, # handle to out stream
4125 $options, # options hash
4130 my ( $record, @entries, $logo, $fh );
4132 while ( $record = get_record( $in ) )
4134 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4135 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4138 put_record( $record, $out ) if not $options->{ "no_stream" };
4141 $logo = Maasha::Plot::seq_logo( \@entries );
4143 $fh = write_stream( $options->{ "data_out" } );
4151 sub script_plot_phastcons_profiles
4153 # Martin A. Hansen, January 2008.
4155 # Plots PhastCons profiles.
4157 my ( $in, # handle to in stream
4158 $out, # handle to out stream
4159 $options, # options hash
4164 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4166 $options->{ "title" } ||= "PhastCons Profiles";
4168 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4169 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4171 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4172 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4174 while ( $record = get_record( $in ) )
4176 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4178 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4180 push @{ $AoA }, [ @{ $scores } ];
4183 put_record( $record, $out ) if not $options->{ "no_stream" };
4186 Maasha::UCSC::phastcons_normalize( $AoA );
4188 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4189 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4191 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4193 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4195 $fh = write_stream( $options->{ "data_out" } );
4197 print $fh "$_\n" foreach @{ $plot };
4203 sub script_analyze_bed
4205 # Martin A. Hansen, March 2008.
4207 # Analyze BED entries in stream.
4209 my ( $in, # handle to in stream
4210 $out, # handle to out stream
4211 $options, # options hash
4218 while ( $record = get_record( $in ) )
4220 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4222 put_record( $record, $out );
4227 sub script_analyze_vals
4229 # Martin A. Hansen, August 2007.
4231 # Analyze values for given keys in stream.
4233 my ( $in, # handle to in stream
4234 $out, # handle to out stream
4235 $options, # options hash
4240 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4242 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4244 while ( $record = get_record( $in ) )
4246 foreach $key ( keys %{ $record } )
4248 next if $options->{ "keys" } and not exists $key_hash{ $key };
4250 $analysis->{ $key }->{ "COUNT" }++;
4252 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4254 $analysis->{ $key }->{ "TYPE" } = "num";
4255 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4256 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4257 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4261 $len = length $record->{ $key };
4263 $analysis->{ $key }->{ "TYPE" } = "alph";
4264 $analysis->{ $key }->{ "SUM" } += $len;
4265 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4266 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4270 put_record( $record, $out ) if not $options->{ "no_stream" };
4273 foreach $key ( keys %{ $analysis } )
4275 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4276 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4279 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4289 if ( $options->{ "keys" } ) {
4290 @keys = @{ $options->{ "keys" } };
4292 @keys = keys %{ $analysis };
4295 foreach $key ( @keys )
4297 $keys .= sprintf "% 15s", $key;
4298 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4299 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4300 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4301 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4302 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4303 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4306 print $out "$keys\n";
4307 print $out "$types\n";
4308 print $out "$counts\n";
4309 print $out "$mins\n";
4310 print $out "$maxs\n";
4311 print $out "$sums\n";
4312 print $out "$means\n";
4316 sub script_head_records
4318 # Martin A. Hansen, August 2007.
4320 # Display the first sequences in stream.
4322 my ( $in, # handle to in stream
4323 $out, # handle to out stream
4324 $options, # options hash
4329 my ( $record, $count );
4331 $options->{ "num" } ||= 10;
4335 while ( $record = get_record( $in ) )
4339 put_record( $record, $out );
4341 last if $count == $options->{ "num" };
4346 sub script_remove_keys
4348 # Martin A. Hansen, August 2007.
4350 # Remove keys from stream.
4352 my ( $in, # handle to in stream
4353 $out, # handle to out stream
4354 $options, # options hash
4359 my ( $record, $new_record );
4361 while ( $record = get_record( $in ) )
4363 if ( $options->{ "keys" } )
4365 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4367 elsif ( $options->{ "save_keys" } )
4369 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4371 $record = $new_record;
4374 put_record( $record, $out ) if keys %{ $record };
4379 sub script_rename_keys
4381 # Martin A. Hansen, August 2007.
4383 # Rename keys in stream.
4385 my ( $in, # handle to in stream
4386 $out, # handle to out stream
4387 $options, # options hash
4394 while ( $record = get_record( $in ) )
4396 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4398 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4400 delete $record->{ $options->{ "keys" }->[ 0 ] };
4403 put_record( $record, $out );
4408 sub script_uniq_vals
4410 # Martin A. Hansen, August 2007.
4412 # Find unique values in stream.
4414 my ( $in, # handle to in stream
4415 $out, # handle to out stream
4416 $options, # options hash
4421 my ( %hash, $record );
4423 while ( $record = get_record( $in ) )
4425 if ( $record->{ $options->{ "key" } } )
4427 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4429 put_record( $record, $out );
4431 $hash{ $record->{ $options->{ "key" } } } = 1;
4433 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4435 put_record( $record, $out );
4439 $hash{ $record->{ $options->{ "key" } } } = 1;
4444 put_record( $record, $out );
4450 sub script_merge_vals
4452 # Martin A. Hansen, August 2007.
4454 # Rename keys in stream.
4456 my ( $in, # handle to in stream
4457 $out, # handle to out stream
4458 $options, # options hash
4463 my ( $record, @join, $i );
4465 $options->{ "delimit" } ||= '_';
4467 while ( $record = get_record( $in ) )
4469 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4471 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4473 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4474 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4477 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4480 put_record( $record, $out );
4487 # Martin A. Hansen, August 2007.
4489 # Grab for records in stream.
4491 my ( $in, # handle to in stream
4492 $out, # handle to out stream
4493 $options, # options hash
4498 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4500 if ( $options->{ "patterns" } )
4502 $patterns = [ split ",", $options->{ "patterns" } ];
4504 elsif ( -f $options->{ "patterns_in" } )
4506 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4508 elsif ( -f $options->{ "exact_in" } )
4510 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4512 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4517 if ( $options->{ "eval" } )
4519 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4527 while ( $record = get_record( $in ) )
4533 if ( $options->{ "keys" } )
4535 foreach $key ( @{ $options->{ "keys" } } )
4537 if ( exists $lookup_hash{ $record->{ $key } } )
4546 foreach $key ( keys %{ $record } )
4548 if ( not $options->{ "vals_only" } )
4550 if ( exists $lookup_hash{ $key } )
4557 if ( not $options->{ "keys_only" } )
4559 if ( exists $lookup_hash{ $record->{ $key } } )
4570 foreach $pattern ( @{ $patterns } )
4572 if ( $options->{ "keys" } )
4574 foreach $key ( @{ $options->{ "keys" } } )
4576 $pos = index $record->{ $key }, $pattern;
4578 goto FOUND if $pos >= 0;
4583 foreach $key ( keys %{ $record } )
4585 if ( not $options->{ "vals_only" } )
4587 $pos = index $key, $pattern;
4589 goto FOUND if $pos >= 0;
4592 if ( not $options->{ "keys_only" } )
4594 $pos = index $record->{ $key }, $pattern;
4596 goto FOUND if $pos >= 0;
4602 elsif ( $options->{ "regex" } )
4604 if ( $options->{ "keys" } )
4606 foreach $key ( @{ $options->{ "keys" } } )
4608 if ( $options->{ "case_insensitive" } ) {
4609 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4611 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4614 goto FOUND if $pos >= 0;
4619 foreach $key ( keys %{ $record } )
4621 if ( not $options->{ "vals_only" } )
4623 if ( $options->{ "case_insensitive" } ) {
4624 $pos = 1 if $key =~ /$options->{'regex'}/i;
4626 $pos = 1 if $key =~ /$options->{'regex'}/;
4629 goto FOUND if $pos >= 0;
4632 if ( not $options->{ "keys_only" } )
4634 if ( $options->{ "case_insensitive" } ) {
4635 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4637 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4640 goto FOUND if $pos >= 0;
4645 elsif ( $options->{ "eval" } )
4647 if ( defined $record->{ $key } )
4649 if ( $op eq "<" and $record->{ $key } < $val ) {
4650 $pos = 1 and goto FOUND;
4651 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4652 $pos = 1 and goto FOUND;
4653 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4654 $pos = 1 and goto FOUND;
4655 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4656 $pos = 1 and goto FOUND;
4657 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4658 $pos = 1 and goto FOUND;
4659 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4660 $pos = 1 and goto FOUND;
4661 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4662 $pos = 1 and goto FOUND;
4663 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4664 $pos = 1 and goto FOUND;
4671 if ( $pos >= 0 and not $options->{ "invert" } ) {
4672 put_record( $record, $out );
4673 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4674 put_record( $record, $out );
4682 # Martin A. Hansen, August 2007.
4684 # Evaluate extression for records in stream.
4686 my ( $in, # handle to in stream
4687 $out, # handle to out stream
4688 $options, # options hash
4693 my ( $record, $eval_key, $eval_val, $check, @keys );
4695 while ( $record = get_record( $in ) )
4697 if ( $options->{ "eval" } )
4699 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4707 @keys = split /\W+/, $eval_val;
4708 @keys = grep { ! /^\d+$/ } @keys;
4713 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4715 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4718 put_record( $record, $out );
4725 # Martin A. Hansen, June 2008.
4729 my ( $in, # handle to in stream
4730 $out, # handle to out stream
4731 $options, # options hash
4736 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4738 while ( $record = get_record( $in ) )
4742 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4744 push @rows, $record->{ $key };
4748 push @matrix, [ @rows ];
4753 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4755 foreach $row ( @matrix )
4757 for ( $i = 0; $i < @{ $row }; $i++ ) {
4758 $record->{ "V$i" } = $row->[ $i ];
4761 put_record( $record, $out );
4766 sub script_add_ident
4768 # Martin A. Hansen, May 2008.
4770 # Add a unique identifier to each record in stream.
4772 my ( $in, # handle to in stream
4773 $out, # handle to out stream
4774 $options, # options hash
4779 my ( $record, $key, $prefix, $i );
4781 $key = $options->{ "key" } || "ID";
4782 $prefix = $options->{ "prefix" } || "ID";
4786 while ( $record = get_record( $in ) )
4788 $record->{ $key } = sprintf( "$prefix%08d", $i );
4790 put_record( $record, $out );
4797 sub script_count_records
4799 # Martin A. Hansen, August 2007.
4801 # Count records in stream.
4803 my ( $in, # handle to in stream
4804 $out, # handle to out stream
4805 $options, # options hash
4810 my ( $record, $count, $result, $fh, $line );
4814 if ( $options->{ "no_stream" } )
4816 while ( $line = <$in> )
4820 $count++ if $line eq "---";
4825 while ( $record = get_record( $in ) )
4827 put_record( $record, $out );
4833 $result = { "count_records" => $count };
4835 $fh = write_stream( $options->{ "data_out" } );
4837 put_record( $result, $fh );
4843 sub script_random_records
4845 # Martin A. Hansen, August 2007.
4847 # Pick a number or random records from stream.
4849 my ( $in, # handle to in stream
4850 $out, # handle to out stream
4851 $options, # options hash
4856 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4858 $options->{ "num" } ||= 10;
4860 $tmp_file = "$BP_TMP/random_records.tmp";
4862 $fh_out = Maasha::Common::write_open( $tmp_file );
4866 while ( $record = get_record( $in ) )
4868 put_record( $record, $fh_out );
4878 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4880 while ( $i < $options->{ "num" } )
4882 $rand = int( rand( $count ) );
4884 if ( not exists $rand_hash{ $rand } )
4886 $rand_hash{ $rand } = 1;
4888 $max = $rand if $rand > $max;
4894 $fh_in = Maasha::Common::read_open( $tmp_file );
4898 while ( $record = get_record( $fh_in ) )
4900 put_record( $record, $out ) if exists $rand_hash{ $count };
4902 last if $count == $max;
4913 sub script_sort_records
4915 # Martin A. Hansen, August 2007.
4917 # Sort to sort records according to keys.
4919 my ( $in, # handle to in stream
4920 $out, # handle to out stream
4921 $options, # options hash
4926 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4928 foreach $key ( @{ $options->{ "keys" } } )
4930 if ( $key =~ s/n$// ) {
4931 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4933 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4937 $sort_str = join " or ", @sort_cmd;
4938 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4940 while ( $record = get_record( $in ) ) {
4941 push @records, $record;
4944 @records = sort $sort_sub @records;
4946 if ( $options->{ "reverse" } )
4948 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4949 put_record( $records[ $i ], $out );
4954 for ( $i = 0; $i < scalar @records; $i++ ) {
4955 put_record( $records[ $i ], $out );
4961 sub script_count_vals
4963 # Martin A. Hansen, August 2007.
4965 # Count records in stream.
4967 my ( $in, # handle to in stream
4968 $out, # handle to out stream
4969 $options, # options hash
4974 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4976 $tmp_file = "$BP_TMP/count_cache.tmp";
4978 $fh_out = Maasha::Common::write_open( $tmp_file );
4982 while ( $record = get_record( $in ) )
4984 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4986 push @records, $record;
4988 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4990 map { put_record( $_, $fh_out ) } @records;
4997 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5008 $fh_in = Maasha::Common::read_open( $tmp_file );
5010 while ( $record = get_record( $fh_in ) )
5012 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5014 put_record( $record, $out );
5016 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5024 foreach $record ( @records )
5026 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5028 put_record( $record, $out );
5035 sub script_plot_histogram
5037 # Martin A. Hansen, September 2007.
5039 # Plot a simple histogram for a given key using GNU plot.
5041 my ( $in, # handle to in stream
5042 $out, # handle to out stream
5043 $options, # options hash
5048 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5050 $options->{ "title" } ||= "Histogram";
5051 $options->{ "sort" } ||= "num";
5053 while ( $record = get_record( $in ) )
5055 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5057 put_record( $record, $out ) if not $options->{ "no_stream" };
5060 if ( $options->{ "sort" } eq "num" ) {
5061 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5063 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5066 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5068 $fh = write_stream( $options->{ "data_out" } );
5070 print $fh "$_\n" foreach @{ $result };
5076 sub script_plot_lendist
5078 # Martin A. Hansen, August 2007.
5080 # Plot length distribution using GNU plot.
5082 my ( $in, # handle to in stream
5083 $out, # handle to out stream
5084 $options, # options hash
5089 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5091 $options->{ "title" } ||= "Length Distribution";
5093 while ( $record = get_record( $in ) )
5095 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5097 put_record( $record, $out ) if not $options->{ "no_stream" };
5100 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5102 for ( $i = 0; $i < $max; $i++ ) {
5103 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5106 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5108 $fh = write_stream( $options->{ "data_out" } );
5110 print $fh "$_\n" foreach @{ $result };
5116 sub script_plot_chrdist
5118 # Martin A. Hansen, August 2007.
5120 # Plot chromosome distribution using GNU plot.
5122 my ( $in, # handle to in stream
5123 $out, # handle to out stream
5124 $options, # options hash
5129 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5131 $options->{ "title" } ||= "Chromosome Distribution";
5133 while ( $record = get_record( $in ) )
5135 if ( $record->{ "CHR" } ) { # generic
5136 $data_hash{ $record->{ "CHR" } }++;
5137 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5138 $data_hash{ $record->{ "S_ID" } }++;
5139 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5140 $data_hash{ $record->{ "S_ID" } }++;
5141 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5142 $data_hash{ $record->{ "S_ID" } }++;
5145 put_record( $record, $out ) if not $options->{ "no_stream" };
5148 foreach $elem ( keys %data_hash )
5152 $sort_key =~ s/chr//i;
5154 $sort_key =~ s/^X(.*)/99$1/;
5155 $sort_key =~ s/^Y(.*)/99$1/;
5156 $sort_key =~ s/^Z(.*)/999$1/;
5157 $sort_key =~ s/^M(.*)/9999$1/;
5158 $sort_key =~ s/^U(.*)/99999$1/;
5160 $count = $sort_key =~ tr/_//;
5162 $sort_key =~ s/_.*/"999999" x $count/ex;
5164 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5167 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5169 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5171 $fh = write_stream( $options->{ "data_out" } );
5173 print $fh "$_\n" foreach @{ $result };
5179 sub script_plot_karyogram
5181 # Martin A. Hansen, August 2007.
5183 # Plot hits on karyogram.
5185 my ( $in, # handle to in stream
5186 $out, # handle to out stream
5187 $options, # options hash
5192 my ( %options, $record, @data, $fh, $result, %data_hash );
5194 $options->{ "genome" } ||= "human";
5195 $options->{ "feat_color" } ||= "black";
5197 while ( $record = get_record( $in ) )
5199 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5201 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5204 put_record( $record, $out ) if not $options->{ "no_stream" };
5207 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5209 $fh = write_stream( $options->{ "data_out" } );
5217 sub script_plot_matches
5219 # Martin A. Hansen, August 2007.
5221 # Plot matches in 2D generating a dotplot.
5223 my ( $in, # handle to in stream
5224 $out, # handle to out stream
5225 $options, # options hash
5230 my ( $record, @data, $fh, $result, %data_hash );
5232 $options->{ "direction" } ||= "both";
5234 while ( $record = get_record( $in ) )
5236 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5237 push @data, $record;
5240 put_record( $record, $out ) if not $options->{ "no_stream" };
5243 $options->{ "title" } ||= "plot_matches";
5244 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5245 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5247 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5249 $fh = write_stream( $options->{ "data_out" } );
5251 print $fh "$_\n" foreach @{ $result };
5257 sub script_length_vals
5259 # Martin A. Hansen, August 2007.
5261 # Determine the length of the value for given keys.
5263 my ( $in, # handle to in stream
5264 $out, # handle to out stream
5265 $options, # options hash
5270 my ( $record, $key );
5272 while ( $record = get_record( $in ) )
5274 foreach $key ( @{ $options->{ "keys" } } )
5276 if ( $record->{ $key } ) {
5277 $record->{ $key . "_LEN" } = length $record->{ $key };
5281 put_record( $record, $out );
5288 # Martin A. Hansen, August 2007.
5290 # Calculates the sums for values of given keys.
5292 my ( $in, # handle to in stream
5293 $out, # handle to out stream
5294 $options, # options hash
5299 my ( $record, $key, %sum_hash, $fh );
5301 while ( $record = get_record( $in ) )
5303 foreach $key ( @{ $options->{ "keys" } } )
5305 if ( $record->{ $key } ) {
5306 $sum_hash{ $key } += $record->{ $key };
5310 put_record( $record, $out ) if not $options->{ "no_stream" };
5313 $fh = write_stream( $options->{ "data_out" } );
5315 foreach $key ( @{ $options->{ "keys" } } ) {
5316 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5323 sub script_mean_vals
5325 # Martin A. Hansen, August 2007.
5327 # Calculate the mean of values of given keys.
5329 my ( $in, # handle to in stream
5330 $out, # handle to out stream
5331 $options, # options hash
5336 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5338 while ( $record = get_record( $in ) )
5340 foreach $key ( @{ $options->{ "keys" } } )
5342 if ( $record->{ $key } )
5344 $sum_hash{ $key } += $record->{ $key };
5345 $count_hash{ $key }++;
5349 put_record( $record, $out ) if not $options->{ "no_stream" };
5352 $fh = write_stream( $options->{ "data_out" } );
5354 foreach $key ( @{ $options->{ "keys" } } )
5356 if ( $count_hash{ $key } ) {
5357 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5362 put_record( { $key . "_MEAN" => $mean } , $fh );
5369 sub script_median_vals
5371 # Martin A. Hansen, March 2008.
5373 # Calculate the median values of given keys.
5375 my ( $in, # handle to in stream
5376 $out, # handle to out stream
5377 $options, # options hash
5382 my ( $record, $key, %median_hash, $median, $fh );
5384 while ( $record = get_record( $in ) )
5386 foreach $key ( @{ $options->{ "keys" } } ) {
5387 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5390 put_record( $record, $out ) if not $options->{ "no_stream" };
5393 $fh = write_stream( $options->{ "data_out" } );
5395 foreach $key ( @{ $options->{ "keys" } } )
5397 if ( $median_hash{ $key } ) {
5398 $median = Maasha::Calc::median( $median_hash{ $key } );
5403 put_record( { $key . "_MEDIAN" => $median } , $fh );
5412 # Martin A. Hansen, February 2008.
5414 # Determine the maximum values of given keys.
5416 my ( $in, # handle to in stream
5417 $out, # handle to out stream
5418 $options, # options hash
5423 my ( $record, $key, $fh, %max_hash, $max_record );
5425 while ( $record = get_record( $in ) )
5427 foreach $key ( @{ $options->{ "keys" } } )
5429 if ( $record->{ $key } )
5431 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5435 put_record( $record, $out ) if not $options->{ "no_stream" };
5438 $fh = write_stream( $options->{ "data_out" } );
5440 foreach $key ( @{ $options->{ "keys" } } )
5442 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5445 put_record( $max_record, $fh );
5453 # Martin A. Hansen, February 2008.
5455 # Determine the minimum values of given keys.
5457 my ( $in, # handle to in stream
5458 $out, # handle to out stream
5459 $options, # options hash
5464 my ( $record, $key, $fh, %min_hash, $min_record );
5466 while ( $record = get_record( $in ) )
5468 foreach $key ( @{ $options->{ "keys" } } )
5470 if ( defined $record->{ $key } )
5472 if ( exists $min_hash{ $key } ) {
5473 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5475 $min_hash{ $key } = $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 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5490 put_record( $min_record, $fh );
5496 sub script_upload_to_ucsc
5498 # Martin A. Hansen, August 2007.
5500 # Calculate the mean of values of given keys.
5502 my ( $in, # handle to in stream
5503 $out, # handle to out stream
5504 $options, # options hash
5509 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5510 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5512 $options->{ "short_label" } ||= $options->{ 'table' };
5513 $options->{ "long_label" } ||= $options->{ 'table' };
5514 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5515 $options->{ "priority" } ||= 1;
5516 $options->{ "visibility" } ||= "pack";
5517 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5518 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5520 $file = "$BP_TMP/ucsc_upload.tmp";
5528 if ( $options->{ 'wiggle' } )
5530 $options->{ "visibility" } = "full";
5532 while ( $record = get_record( $in ) )
5534 put_record( $record, $out ) if not $options->{ "no_stream" };
5536 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5537 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5538 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5540 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5542 $fh_out = $fh_hash{ $record->{ "CHR" } };
5544 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5547 map { close $_ } keys %fh_hash;
5549 $fh_out = Maasha::Common::write_open( $file );
5551 foreach $chr ( sort keys %fh_hash )
5553 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5555 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5559 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5561 $chr = $entry->{ 'CHR' };
5562 $beg = $entry->{ 'CHR_BEG' };
5563 $end = $entry->{ 'CHR_END' };
5564 $q_id = $entry->{ 'Q_ID' };
5566 if ( $q_id =~ /_(\d+)$/ ) {
5576 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5581 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5582 $block->[ $i ] += $clones;
5585 $max = Maasha::Calc::max( $max, $end );
5594 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5595 $block->[ $i ] += $clones;
5602 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5604 unlink "$BP_TMP/$chr";
5609 $wig_file = "$options->{ 'table' }.wig";
5610 $wib_file = "$options->{ 'table' }.wib";
5612 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5614 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5616 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5618 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5619 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5629 $fh_out = Maasha::Common::write_open( $file );
5631 while ( $record = get_record( $in ) )
5633 put_record( $record, $out ) if not $options->{ "no_stream" };
5635 if ( $record->{ "REC_TYPE" } eq "PSL" )
5637 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5638 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5642 $format = "PSL" if not $format;
5644 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5646 # chrom chromStart chromEnd name score strand size secStr conf
5648 print $fh_out join ( "\t",
5650 $record->{ "CHR_BEG" },
5651 $record->{ "CHR_END" } + 1,
5652 $record->{ "Q_ID" },
5653 $record->{ "SCORE" },
5654 $record->{ "STRAND" },
5655 $record->{ "SIZE" },
5656 $record->{ "SEC_STRUCT" },
5657 $record->{ "CONF" },
5660 $format = "BED_SS" if not $format;
5662 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5664 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5666 $format = "BED" if not $format;
5667 $columns = $record->{ "BED_COLS" } if not $columns;
5669 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5671 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5673 $format = "BED" if not $format;
5674 $columns = 6 if not $columns;
5676 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5678 $record->{ "CHR" } = $record->{ "S_ID" };
5679 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5680 $record->{ "CHR_END" } = $record->{ "S_END" };
5681 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5683 $format = "BED" if not $format;
5684 $columns = 6 if not $columns;
5686 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5688 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5690 $record->{ "CHR" } = $record->{ "S_ID" };
5691 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5692 $record->{ "CHR_END" } = $record->{ "S_END" };
5693 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5694 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5696 $format = "BED" if not $format;
5697 $columns = 6 if not $columns;
5699 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5702 if ( $i == $options->{ "chunk_size" } )
5706 if ( $format eq "BED" ) {
5707 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5708 } elsif ( $format eq "PSL" ) {
5709 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5718 $fh_out = Maasha::Common::write_open( $file );
5727 if ( exists $options->{ "database" } and $options->{ "table" } )
5729 if ( $format eq "BED" )
5731 $type = "bed $columns";
5733 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5735 elsif ( $format eq "BED_SS" )
5737 $options->{ "sec_struct" } = 1;
5739 $type = "sec_struct";
5741 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5743 elsif ( $format eq "PSL" )
5747 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5749 elsif ( $format eq "WIGGLE" )
5753 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5758 Maasha::UCSC::update_my_tracks( $options, $type );
5763 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5768 # Martin A. Hansen, July 2007.
5770 # Opens a stream to STDIN or a file,
5772 my ( $path, # path - OPTIONAL
5775 # Returns filehandle.
5779 if ( not -t STDIN ) {
5780 $fh = Maasha::Common::read_stdin();
5781 } elsif ( not $path ) {
5782 # Maasha::Common::error( qq(no data stream) );
5784 $fh = Maasha::Common::read_open( $path );
5787 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5795 # Martin A. Hansen, August 2007.
5797 # Opens a stream to STDOUT or a file.
5799 my ( $path, # path - OPTIONAL
5800 $gzip, # compress data - OPTIONAL
5803 # Returns filehandle.
5808 $fh = Maasha::Common::write_open( $path, $gzip );
5810 $fh = Maasha::Common::write_stdout();
5819 # Martin A. Hansen, July 2007.
5821 # Reads one record at a time and converts that record
5822 # to a Perl data structure (a hash) which is returned.
5827 # Returns data structure.
5829 my ( $block, @lines, $line, $key, $value, %record );
5831 local $/ = "\n---\n";
5837 return if not defined $block;
5839 @lines = split "\n", $block;
5841 foreach $line ( @lines )
5843 ( $key, $value ) = split ": ", $line;
5845 $record{ $key } = $value;
5848 return wantarray ? %record : \%record;
5854 # Martin A. Hansen, July 2007.
5856 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5858 my ( $data, # data structure
5859 $fh, # file handle - OPTIONAL
5864 if ( scalar keys %{ $data } )
5868 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5873 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5884 # Martin A. Hansen, November 2007.
5886 # Extracts files from an explicit GetOpt::Long argument
5887 # allowing for the use of glob. E.g.
5888 # --data_in=test.fna
5889 # --data_in=test.fna,test2.fna
5891 # --data_in=test.fna,/dir/*.fna
5893 my ( $option, # option from GetOpt::Long
5898 my ( $elem, @files );
5900 foreach $elem ( split ",", $option )
5904 } elsif ( $elem =~ /\*/ ) {
5905 push @files, glob( $elem );
5909 return wantarray ? @files : \@files;
5915 # Martin A. Hansen, April 2008.
5917 # Removes temporary directory and exits gracefully.
5918 # This subroutine is meant to be run always as the last
5919 # thing even if a script is dies or is interrupted
5922 my ( $sig, # signal from the %SIG
5925 # print STDERR "signal->$sig<-\n";
5933 if ( $sig =~ /MAASHA_ERROR/ ) {
5934 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5935 } elsif ( $sig eq "INT" ) {
5936 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5937 } elsif ( $sig eq "TERM" ) {
5938 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5940 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5943 # This is a really bad solution, potentially, anyone can include this module and set
5944 # the BP_TMP to point at any dir and thus take out the machine !!!
5946 Maasha::Common::dir_remove( $BP_TMP );
5955 # This is a really bad solution, potentially, anyone can include this module and set
5956 # the BP_TMP to point at any dir and thus take out the machine !!!
5958 Maasha::Common::dir_remove( $BP_TMP );
5962 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5969 sub script_read_soft
5971 # Martin A. Hansen, December 2007.
5974 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
5976 my ( $in, # handle to in stream
5977 $out, # handle to out stream
5978 $options, # options hash
5983 my ( $data_in, $file, $num, $records, $record );
5985 while ( $record = get_record( $in ) ) {
5986 put_record( $record, $out );
5991 foreach $file ( @{ $options->{ "files" } } )
5993 $records = Maasha::NCBI::soft_parse( $file );
5995 foreach $record ( @{ $records } )
5997 put_record( $record, $out );
5999 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
6007 close $data_in if $data_in;