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_fh = Maasha::Common::append_open( "$ENV{ 'BP_TMP' }/biopieces.log" );
98 $log_fh->autoflush( 1 );
100 &log( $log_fh, $script, \@ARGV );
105 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
108 my $t0 = gettimeofday();
110 run_script( $script );
112 my $t1 = gettimeofday();
114 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
117 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
122 # Martin A. Hansen, January 2008.
124 # Log messages to logfile.
126 my ( $fh, # filehandle to logfile
127 $script, # script name
128 $argv, # reference to @ARGV
133 my ( $time_stamp, $user );
135 $time_stamp = Maasha::Common::time_stamp();
137 $user = $ENV{ 'USER' };
139 $script = "biopieces" if $script eq "-e";
141 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
147 # Martin A. Hansen, August 2007.
149 # Run a specific script.
151 my ( $script, # script name
156 my ( $options, $in, $out );
158 $options = get_options( $script );
160 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
161 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
164 $in = read_stream( $options->{ "stream_in" } );
165 $out = write_stream( $options->{ "stream_out" } );
167 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
168 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
169 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
170 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
171 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
172 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
173 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
174 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
175 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
176 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
177 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
178 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
179 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
180 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
181 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
182 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
183 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
184 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
185 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
186 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
187 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
188 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
189 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
190 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
191 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
192 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
193 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
194 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
195 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
196 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
197 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
198 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
199 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
200 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
201 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
202 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
203 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
204 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
205 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
206 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
207 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
208 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
209 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
210 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
211 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
212 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
213 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
214 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
215 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
216 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
217 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
218 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
219 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
220 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
221 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
222 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
223 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
224 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
225 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
226 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
227 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
228 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
229 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
230 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
231 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
232 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
233 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
234 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
235 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
236 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
237 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
238 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
239 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
240 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
241 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
242 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
243 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
244 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
245 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
246 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
247 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
248 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
249 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
250 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
251 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
252 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
253 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
254 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
255 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
257 close $in if defined $in;
260 # unset status - missing
261 # write log file - missing
267 # Martin A. Hansen, February 2008.
269 # Gets options from commandline and checks these vigerously.
271 my ( $script, # name of script
276 my ( %options, @options, $opt, @genomes );
278 if ( $script eq "print_usage" )
284 elsif ( $script eq "read_fasta" )
291 elsif ( $script eq "read_tab" )
302 elsif ( $script eq "read_psl" )
309 elsif ( $script eq "read_bed" )
316 elsif ( $script eq "read_fixedstep" )
323 elsif ( $script eq "read_blast_tab" )
330 elsif ( $script eq "read_embl" )
340 elsif ( $script eq "read_stockholm" )
347 elsif ( $script eq "read_phastcons" )
358 elsif ( $script eq "read_soft" )
365 elsif ( $script eq "read_gff" )
372 elsif ( $script eq "read_2bit" )
380 elsif ( $script eq "read_solexa" )
388 elsif ( $script eq "read_solid" )
396 elsif ( $script eq "read_mysql" )
405 elsif ( $script eq "format_genome" )
414 elsif ( $script eq "length_seq" )
421 elsif ( $script eq "oligo_freq" )
428 elsif ( $script eq "create_weight_matrix" )
434 elsif ( $script eq "transliterate_seq" )
442 elsif ( $script eq "transliterate_vals" )
451 elsif ( $script eq "translate_seq" )
457 elsif ( $script eq "extract_seq" )
465 elsif ( $script eq "get_genome_seq" )
477 elsif ( $script eq "get_genome_align" )
488 elsif ( $script eq "get_genome_phastcons" )
499 elsif ( $script eq "split_seq" )
506 elsif ( $script eq "split_bed" )
513 elsif ( $script eq "tile_seq" )
520 elsif ( $script eq "invert_align" )
526 elsif ( $script eq "patscan_seq" )
537 elsif ( $script eq "create_blast_db" )
544 elsif ( $script eq "blast_seq" )
556 elsif ( $script eq "blat_seq" )
568 elsif ( $script eq "match_seq" )
575 elsif ( $script eq "create_vmatch_index" )
583 elsif ( $script eq "vmatch_seq" )
594 elsif ( $script eq "write_fasta" )
603 elsif ( $script eq "write_align" )
613 elsif ( $script eq "write_blast" )
622 elsif ( $script eq "write_tab" )
634 elsif ( $script eq "write_bed" )
642 elsif ( $script eq "write_psl" )
650 elsif ( $script eq "write_fixedstep" )
658 elsif ( $script eq "write_2bit" )
666 elsif ( $script eq "write_solid" )
675 elsif ( $script eq "plot_seqlogo" )
682 elsif ( $script eq "plot_phastcons_profiles" )
697 elsif ( $script eq "analyze_vals" )
704 elsif ( $script eq "head_records" )
710 elsif ( $script eq "remove_keys" )
717 elsif ( $script eq "rename_keys" )
723 elsif ( $script eq "uniq_vals" )
730 elsif ( $script eq "merge_vals" )
737 elsif ( $script eq "grab" )
752 elsif ( $script eq "compute" )
758 elsif ( $script eq "add_ident" )
765 elsif ( $script eq "count_records" )
772 elsif ( $script eq "random_records" )
778 elsif ( $script eq "sort_records" )
785 elsif ( $script eq "count_vals" )
791 elsif ( $script eq "plot_histogram" )
804 elsif ( $script eq "plot_lendist" )
816 elsif ( $script eq "plot_chrdist" )
827 elsif ( $script eq "plot_karyogram" )
836 elsif ( $script eq "plot_matches" )
848 elsif ( $script eq "length_vals" )
854 elsif ( $script eq "sum_vals" )
862 elsif ( $script eq "mean_vals" )
870 elsif ( $script eq "median_vals" )
878 elsif ( $script eq "max_vals" )
886 elsif ( $script eq "min_vals" )
894 elsif ( $script eq "upload_to_ucsc" )
919 # print STDERR Dumper( \@options );
926 $options{ "script" } = $script;
928 # print STDERR Dumper( \%options );
930 if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
931 return wantarray ? %options : \%options;
934 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
935 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
936 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
937 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
938 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
939 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
940 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
941 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
943 # ---- check arguments ----
945 if ( $options{ 'data_in' } )
947 $options{ "files" } = getopt_files( $options{ 'data_in' } );
949 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
952 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
954 # print STDERR Dumper( \%options );
956 foreach $opt ( keys %options )
958 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
960 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
962 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+$/ )
964 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
966 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
968 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
970 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
972 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
974 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
976 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
978 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
980 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
982 elsif ( $opt eq "genome" and $script ne "format_genome" )
984 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
985 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
987 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
988 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
991 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
993 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
995 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
997 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1001 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1002 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1003 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1004 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1005 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1006 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
1007 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" };
1008 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1009 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" };
1011 if ( $script eq "upload_to_ucsc" )
1013 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1014 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1017 return wantarray ? %options : \%options;
1021 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1024 sub script_print_usage
1026 # Martin A. Hansen, January 2008.
1028 # Retrieves usage information from file and
1029 # prints this nicely formatted.
1031 my ( $in, # handle to in stream
1032 $out, # handle to out stream
1033 $options, # options hash
1038 my ( $file, $wiki, $lines );
1040 if ( $options->{ 'data_in' } ) {
1041 $file = $options->{ 'data_in' };
1043 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1046 $wiki = Maasha::Gwiki::gwiki_read( $file );
1048 if ( not $options->{ "help" } ) {
1049 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1052 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1054 print STDERR "$_\n" foreach @{ $lines };
1060 sub script_list_biopieces
1062 # Martin A. Hansen, January 2008.
1064 # Prints the synopsis from the usage for each of the biopieces.
1066 my ( $in, # handle to in stream
1067 $out, # handle to out stream
1068 $options, # options hash
1073 my ( @files, $file, $wiki, $program, $synopsis );
1075 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1077 foreach $file ( sort @files )
1079 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1083 $wiki = Maasha::Gwiki::gwiki_read( $file );
1085 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1086 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1088 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1090 printf( "%-30s%s\n", $program, $synopsis );
1098 sub script_list_genomes
1100 # Martin A. Hansen, January 2008.
1102 # Prints the synopsis from the usage for each of the biopieces.
1104 my ( $in, # handle to in stream
1105 $out, # handle to out stream
1106 $options, # options hash
1111 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1113 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1115 foreach $genome ( @genomes )
1117 next if $genome =~ /\.$/;
1119 @formats = Maasha::Common::ls_dirs( $genome );
1121 foreach $format ( @formats )
1123 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1125 $hash{ $1 }{ $2 } = 1;
1134 map { push @row, $_ } sort keys %found;
1136 print join( "\t", @row ), "\n";
1138 foreach $genome ( sort keys %hash )
1142 foreach $format ( sort keys %found )
1144 if ( exists $hash{ $genome }{ $format } ) {
1151 print join( "\t", @row ), "\n";
1156 sub script_read_fasta
1158 # Martin A. Hansen, August 2007.
1160 # Read sequences from FASTA file.
1162 my ( $in, # handle to in stream
1163 $out, # handle to out stream
1164 $options, # options hash
1169 my ( $record, $file, $data_in, $entry, $num );
1171 while ( $record = get_record( $in ) ) {
1172 put_record( $record, $out );
1177 foreach $file ( @{ $options->{ "files" } } )
1179 $data_in = Maasha::Common::read_open( $file );
1181 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1183 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1186 SEQ_NAME => $entry->[ SEQ_NAME ],
1187 SEQ => $entry->[ SEQ ],
1188 SEQ_LEN => length $entry->[ SEQ ],
1191 put_record( $record, $out );
1194 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1204 close $data_in if $data_in;
1210 # Martin A. Hansen, August 2007.
1212 # Read table or table columns from stream or file.
1214 my ( $in, # handle to in stream
1215 $out, # handle to out stream
1216 $options, # options hash
1221 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1223 $options->{ 'delimit' } ||= '\s+';
1225 while ( $record = get_record( $in ) ) {
1226 put_record( $record, $out );
1229 $skip = $options->{ 'skip' } ||= 0;
1232 foreach $file ( @{ $options->{ "files" } } )
1234 $data_in = Maasha::Common::read_open( $file );
1236 while ( $line = <$data_in> )
1244 next if $line =~ /^#|^$/;
1251 @fields = split /$options->{'delimit'}/, $line;
1253 if ( $options->{ "cols" } ) {
1254 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1259 for ( $i = 0; $i < @fields2; $i++ )
1261 if ( $options->{ "keys" }->[ $i ] ) {
1262 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1264 $record->{ "V" . $i } = $fields2[ $i ];
1268 put_record( $record, $out );
1270 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1280 close $data_in if $data_in;
1286 # Martin A. Hansen, August 2007.
1288 # Read psl table from stream or file.
1290 my ( $in, # handle to in stream
1291 $out, # handle to out stream
1292 $options, # options hash
1297 my ( $record, @files, $file, $entries, $entry, $num );
1299 while ( $record = get_record( $in ) ) {
1300 put_record( $record, $out );
1305 foreach $file ( @{ $options->{ "files" } } )
1307 $entries = Maasha::UCSC::psl_get_entries( $file );
1309 foreach $entry ( @{ $entries } )
1311 put_record( $entry, $out );
1313 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1325 # Martin A. Hansen, August 2007.
1327 # Read bed table from stream or file.
1329 my ( $in, # handle to in stream
1330 $out, # handle to out stream
1331 $options, # options hash
1336 my ( $file, $record, $entry, $data_in, $num );
1338 while ( $record = get_record( $in ) ) {
1339 put_record( $record, $out );
1344 foreach $file ( @{ $options->{ "files" } } )
1346 $data_in = Maasha::Common::read_open( $file );
1348 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1350 put_record( $entry, $out );
1352 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1362 close $data_in if $data_in;
1366 sub script_read_fixedstep
1368 # Martin A. Hansen, Juli 2008.
1370 # Read fixedStep wiggle format from stream or file.
1372 my ( $in, # handle to in stream
1373 $out, # handle to out stream
1374 $options, # options hash
1379 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1381 while ( $record = get_record( $in ) ) {
1382 put_record( $record, $out );
1387 foreach $file ( @{ $options->{ "files" } } )
1389 $data_in = Maasha::Common::read_open( $file );
1391 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1393 $head = shift @{ $entry };
1395 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1397 $record->{ "CHR" } = $1;
1398 $record->{ "CHR_BEG" } = $2;
1399 $record->{ "STEP" } = $3;
1400 $record->{ "VALS" } = join ",", @{ $entry };
1403 put_record( $record, $out );
1405 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1415 close $data_in if $data_in;
1419 sub script_read_blast_tab
1421 # Martin A. Hansen, September 2007.
1423 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1425 my ( $in, # handle to in stream
1426 $out, # handle to out stream
1427 $options, # options hash
1432 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1434 while ( $record = get_record( $in ) ) {
1435 put_record( $record, $out );
1440 foreach $file ( @{ $options->{ "files" } } )
1442 $data_in = Maasha::Common::read_open( $file );
1444 while ( $line = <$data_in> )
1448 next if $line =~ /^#/;
1450 @fields = split /\t/, $line;
1452 $record->{ "REC_TYPE" } = "BLAST";
1453 $record->{ "Q_ID" } = $fields[ 0 ];
1454 $record->{ "S_ID" } = $fields[ 1 ];
1455 $record->{ "IDENT" } = $fields[ 2 ];
1456 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1457 $record->{ "MISMATCHES" } = $fields[ 4 ];
1458 $record->{ "GAPS" } = $fields[ 5 ];
1459 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1460 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1461 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1462 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1463 $record->{ "E_VAL" } = $fields[ 10 ];
1464 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1466 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1468 $record->{ "STRAND" } = '-';
1470 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1474 $record->{ "STRAND" } = '+';
1477 put_record( $record, $out );
1479 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1489 close $data_in if $data_in;
1493 sub script_read_embl
1495 # Martin A. Hansen, August 2007.
1499 my ( $in, # handle to in stream
1500 $out, # handle to out stream
1501 $options, # options hash
1506 my ( %options2, $file, $data_in, $num, $entry, $record );
1508 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1509 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1510 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1512 while ( $record = get_record( $in ) ) {
1513 put_record( $record, $out );
1518 foreach $file ( @{ $options->{ "files" } } )
1520 $data_in = Maasha::Common::read_open( $file );
1522 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1524 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1526 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1528 $record_copy = dclone $record;
1530 delete $record_copy->{ "FT" };
1532 put_record( $record_copy, $out );
1534 delete $record_copy->{ "SEQ" };
1536 foreach $feat ( keys %{ $record->{ "FT" } } )
1538 $record_copy->{ "FEAT_TYPE" } = $feat;
1540 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1542 foreach $qual ( keys %{ $feat2 } )
1544 $qual_val = join "; ", @{ $feat2->{ $qual } };
1549 $record_copy->{ $qual } = $qual_val;
1552 put_record( $record_copy, $out );
1556 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1566 close $data_in if $data_in;
1570 sub script_read_stockholm
1572 # Martin A. Hansen, August 2007.
1574 # Read Stockholm format.
1576 my ( $in, # handle to in stream
1577 $out, # handle to out stream
1578 $options, # options hash
1583 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1585 while ( $record = get_record( $in ) ) {
1586 put_record( $record, $out );
1591 foreach $file ( @{ $options->{ "files" } } )
1593 $data_in = Maasha::Common::read_open( $file );
1595 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1597 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1601 foreach $key ( keys %{ $record->{ "GF" } } ) {
1602 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1605 $record_anno->{ "ALIGN" } = $num;
1607 put_record( $record_anno, $out );
1609 foreach $seq ( @{ $record->{ "ALIGN" } } )
1611 undef $record_align;
1614 SEQ_NAME => $seq->[ 0 ],
1618 put_record( $record_align, $out );
1621 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1631 close $data_in if $data_in;
1635 sub script_read_phastcons
1637 # Martin A. Hansen, December 2007.
1639 # Read PhastCons format.
1641 my ( $in, # handle to in stream
1642 $out, # handle to out stream
1643 $options, # options hash
1648 my ( $data_in, $file, $num, $entry, @records, $record );
1650 $options->{ "min" } ||= 10;
1651 $options->{ "dist" } ||= 25;
1652 $options->{ "threshold" } ||= 0.8;
1653 $options->{ "gap" } ||= 5;
1655 while ( $record = get_record( $in ) ) {
1656 put_record( $record, $out );
1661 foreach $file ( @{ $options->{ "files" } } )
1663 $data_in = Maasha::Common::read_open( $file );
1665 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1667 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1669 foreach $record ( @records )
1671 $record->{ "REC_TYPE" } = "BED";
1672 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1674 put_record( $record, $out );
1676 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1687 close $data_in if $data_in;
1691 sub script_read_soft
1693 # Martin A. Hansen, December 2007.
1696 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1698 my ( $in, # handle to in stream
1699 $out, # handle to out stream
1700 $options, # options hash
1705 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1707 while ( $record = get_record( $in ) ) {
1708 put_record( $record, $out );
1713 foreach $file ( @{ $options->{ "files" } } )
1715 $soft_index = Maasha::NCBI::soft_index_file( $file );
1717 $fh = Maasha::Common::read_open( $file );
1719 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1721 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1723 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1725 $old_end = $platforms[ -1 ]->[ 2 ];
1727 foreach $sample ( @samples )
1729 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1731 foreach $record ( @{ $records } )
1733 put_record( $record, $out );
1735 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1740 $old_end = $sample->[ 2 ];
1748 close $data_in if $data_in;
1755 # Martin A. Hansen, February 2008.
1758 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1760 my ( $in, # handle to in stream
1761 $out, # handle to out stream
1762 $options, # options hash
1767 my ( $data_in, $file, $fh, $num, $record, $entry );
1769 while ( $record = get_record( $in ) ) {
1770 put_record( $record, $out );
1775 foreach $file ( @{ $options->{ "files" } } )
1777 $fh = Maasha::Common::read_open( $file );
1779 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1781 put_record( $entry, $out );
1783 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1793 close $data_in if $data_in;
1797 sub script_read_2bit
1799 # Martin A. Hansen, March 2008.
1801 # Read sequences from 2bit file.
1803 my ( $in, # handle to in stream
1804 $out, # handle to out stream
1805 $options, # options hash
1810 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1812 $mask = 1 if not $options->{ "no_mask" };
1814 while ( $record = get_record( $in ) ) {
1815 put_record( $record, $out );
1820 foreach $file ( @{ $options->{ "files" } } )
1822 $data_in = Maasha::Common::read_open( $file );
1824 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1826 foreach $line ( @{ $toc } )
1828 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1829 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1830 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1832 put_record( $record, $out );
1834 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1844 close $data_in if $data_in;
1848 sub script_read_solexa
1850 # Martin A. Hansen, March 2008.
1852 # Read Solexa sequence reads from file.
1854 my ( $in, # handle to in stream
1855 $out, # handle to out stream
1856 $options, # options hash
1861 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1863 $options->{ "quality" } ||= 20;
1865 while ( $record = get_record( $in ) ) {
1866 put_record( $record, $out );
1871 foreach $file ( @{ $options->{ "files" } } )
1873 $data_in = Maasha::Common::read_open( $file );
1874 $base_name = Maasha::Common::get_basename( $file );
1875 $base_name =~ s/\..*//;
1879 while ( $line = <$data_in> )
1881 @fields = split /:/, $line;
1882 @seqs = split //, $fields[ 5 ];
1883 @scores = split / /, $fields[ -1 ];
1885 for ( $i = 0; $i < @scores; $i++ ) {
1886 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1889 $seq = join "", @seqs;
1891 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1892 $record->{ "SEQ" } = $seq;
1893 $record->{ "SEQ_LEN" } = length $seq;
1894 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1896 put_record( $record, $out );
1898 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1909 close $data_in if $data_in;
1913 sub script_read_solid
1915 # Martin A. Hansen, April 2008.
1917 # Read Solid sequence from file.
1919 my ( $in, # handle to in stream
1920 $out, # handle to out stream
1921 $options, # options hash
1926 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1928 $options->{ "quality" } ||= 15;
1930 while ( $record = get_record( $in ) ) {
1931 put_record( $record, $out );
1936 foreach $file ( @{ $options->{ "files" } } )
1938 $data_in = Maasha::Common::read_open( $file );
1940 while ( $line = <$data_in> )
1944 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1946 @scores = split /,/, $seq_qual;
1947 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1949 for ( $i = 0; $i < @seqs; $i++ ) {
1950 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1954 SEQ_NAME => $seq_name,
1956 SEQ_QUAL => $seq_qual,
1957 SEQ_LEN => length $seq_cs,
1958 SEQ => join( "", @seqs ),
1959 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1962 put_record( $record, $out );
1964 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1974 close $data_in if $data_in;
1978 sub script_read_mysql
1980 # Martin A. Hansen, May 2008.
1982 # Read a MySQL query into stream.
1984 my ( $in, # handle to in stream
1985 $out, # handle to out stream
1986 $options, # options hash
1991 my ( $record, $dbh, $results );
1993 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1994 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1996 while ( $record = get_record( $in ) ) {
1997 put_record( $record, $out );
2000 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2002 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2004 Maasha::SQL::disconnect( $dbh );
2006 map { put_record( $_ ) } @{ $results };
2010 sub script_format_genome
2012 # Martin A. Hansen, Juli 2008.
2014 # Format a genome to speficed formats.
2016 my ( $in, # handle to in stream
2017 $out, # handle to out stream
2018 $options, # options hash
2023 my ( $dir, $genome, $fasta_dir, $fh_out, $record, $format, $index );
2025 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2026 $genome = $options->{ 'genome' };
2028 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2029 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2030 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2032 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2034 $fasta_dir = "$dir/genomes/$genome/fasta";
2036 elsif ( grep { $_ =~ /fasta/i } @{ $options->{ "formats" } } )
2038 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2040 $fasta_dir = "$dir/genomes/$genome/fasta";
2042 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2046 $fasta_dir = $BP_TMP;
2048 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2051 while ( $record = get_record( $in ) )
2053 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2054 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
2057 put_record( $record, $out ) if not $options->{ "no_stream" };
2060 foreach $format ( @{ $options->{ 'formats' } } )
2062 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2063 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2064 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2065 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2068 close $fh_out if $fh_out;
2072 sub script_length_seq
2074 # Martin A. Hansen, August 2007.
2076 # Determine the length of sequences in stream.
2078 my ( $in, # handle to in stream
2079 $out, # handle to out stream
2080 $options, # options hash
2085 my ( $record, $total );
2087 while ( $record = get_record( $in ) )
2089 if ( $record->{ "SEQ" } )
2091 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2092 $total += $record->{ "SEQ_LEN" };
2095 put_record( $record, $out ) if not $options->{ "no_stream" };
2098 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2102 sub script_uppercase_seq
2104 # Martin A. Hansen, August 2007.
2106 # Uppercases sequences in stream.
2108 my ( $in, # handle to in stream
2109 $out, # handle to out stream
2116 while ( $record = get_record( $in ) )
2118 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2120 put_record( $record, $out );
2125 sub script_shuffle_seq
2127 # Martin A. Hansen, December 2007.
2129 # Shuffle sequences in stream.
2131 my ( $in, # handle to in stream
2132 $out, # handle to out stream
2139 while ( $record = get_record( $in ) )
2141 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2143 put_record( $record, $out );
2148 sub script_analyze_seq
2150 # Martin A. Hansen, August 2007.
2152 # Analyze sequence composition of sequences in stream.
2154 my ( $in, # handle to in stream
2155 $out, # handle to out stream
2160 my ( $record, $analysis );
2162 while ( $record = get_record( $in ) )
2164 if ( $record->{ "SEQ" } )
2166 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2168 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2171 put_record( $record, $out );
2176 sub script_analyze_tags
2178 # Martin A. Hansen, August 2008.
2180 # Analyze sequence tags in stream.
2182 my ( $in, # handle to in stream
2183 $out, # handle to out stream
2188 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2190 while ( $record = get_record( $in ) )
2192 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2194 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2198 $len_hash{ length( $record->{ "SEQ" } ) }++;
2199 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2202 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2204 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2208 $len_hash{ $record->{ "BED_LEN" } }++;
2209 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2214 foreach $key ( sort { $a <=> $b } keys %len_hash )
2216 $tag_record->{ "TAG_LEN" } = $key;
2217 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2218 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2220 put_record( $tag_record, $out );
2225 sub script_complexity_seq
2227 # Martin A. Hansen, May 2008.
2229 # Generates an index calculated as the most common di-residue over
2230 # the sequence length for all sequences in stream.
2232 my ( $in, # handle to in stream
2233 $out, # handle to out stream
2238 my ( $record, $index );
2240 while ( $record = get_record( $in ) )
2242 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2244 put_record( $record, $out );
2249 sub script_oligo_freq
2251 # Martin A. Hansen, August 2007.
2253 # Determine the length of sequences in stream.
2255 my ( $in, # handle to in stream
2256 $out, # handle to out stream
2257 $options, # options hash
2262 my ( $record, %oligos, @freq_table );
2264 $options->{ "word_size" } ||= 7;
2266 while ( $record = get_record( $in ) )
2268 if ( $record->{ "SEQ" } )
2270 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2272 if ( not $options->{ "all" } )
2274 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2276 map { put_record( $_, $out ) } @freq_table;
2282 put_record( $record, $out );
2285 if ( $options->{ "all" } )
2287 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2289 map { put_record( $_, $out ) } @freq_table;
2294 sub script_create_weight_matrix
2296 # Martin A. Hansen, August 2007.
2298 # Creates a weight matrix from an alignmnet.
2300 my ( $in, # handle to in stream
2301 $out, # handle to out stream
2302 $options, # options hash
2307 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2311 while ( $record = get_record( $in ) )
2313 if ( $record->{ "SEQ" } )
2315 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2317 $res = substr $record->{ "SEQ" }, $i, 1;
2319 $freq_hash{ $i }{ $res }++;
2320 $res_hash{ $res } = 1;
2327 put_record( $record, $out );
2331 foreach $res ( sort keys %res_hash )
2335 $record->{ "V0" } = $res;
2337 for ( $i = 0; $i < keys %freq_hash; $i++ )
2339 $freq = $freq_hash{ $i }{ $res } || 0;
2341 if ( $options->{ "percent" } ) {
2342 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2345 $record->{ "V" . ( $i + 1 ) } = $freq;
2348 put_record( $record, $out );
2353 sub script_calc_bit_scores
2355 # Martin A. Hansen, March 2007.
2357 # Calculates the bit scores for each position from an alignmnet in the stream.
2359 my ( $in, # handle to in stream
2360 $out, # handle to out stream
2365 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2369 while ( $record = get_record( $in ) )
2371 if ( $record->{ "SEQ" } )
2373 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2375 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2377 $res = substr $record->{ "SEQ" }, $i, 1;
2379 next if $res =~ /-|_|~|\./;
2381 $freq_hash{ $i }{ $res }++;
2388 put_record( $record, $out );
2394 if ( $type eq "protein" ) {
2400 for ( $i = 0; $i < keys %freq_hash; $i++ )
2402 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2404 $bit_diff = $bit_max - $bit_height;
2406 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2409 put_record( $record, $out );
2413 sub script_reverse_seq
2415 # Martin A. Hansen, August 2007.
2417 # Reverse sequence in record.
2419 my ( $in, # handle to in stream
2420 $out, # handle to out stream
2427 while ( $record = get_record( $in ) )
2429 if ( $record->{ "SEQ" } ) {
2430 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2433 put_record( $record, $out );
2438 sub script_complement_seq
2440 # Martin A. Hansen, August 2007.
2442 # Complement sequence in record.
2444 my ( $in, # handle to in stream
2445 $out, # handle to out stream
2450 my ( $record, $type );
2452 while ( $record = get_record( $in ) )
2454 if ( $record->{ "SEQ" } )
2457 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2460 if ( $type eq "rna" ) {
2461 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2462 } elsif ( $type eq "dna" ) {
2463 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2467 put_record( $record, $out );
2472 sub script_remove_indels
2474 # Martin A. Hansen, August 2007.
2476 # Remove indels from sequences in stream.
2478 my ( $in, # handle to in stream
2479 $out, # handle to out stream
2486 while ( $record = get_record( $in ) )
2488 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2490 put_record( $record, $out );
2495 sub script_transliterate_seq
2497 # Martin A. Hansen, August 2007.
2499 # Transliterate chars from sequence in record.
2501 my ( $in, # handle to in stream
2502 $out, # handle to out stream
2503 $options, # options hash
2508 my ( $record, $search, $replace, $delete );
2510 $search = $options->{ "search" } || "";
2511 $replace = $options->{ "replace" } || "";
2512 $delete = $options->{ "delete" } || "";
2514 while ( $record = get_record( $in ) )
2516 if ( $record->{ "SEQ" } )
2518 if ( $search and $replace ) {
2519 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2520 } elsif ( $delete ) {
2521 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2525 put_record( $record, $out );
2530 sub script_transliterate_vals
2532 # Martin A. Hansen, April 2008.
2534 # Transliterate chars from values in record.
2536 my ( $in, # handle to in stream
2537 $out, # handle to out stream
2538 $options, # options hash
2543 my ( $record, $search, $replace, $delete, $key );
2545 $search = $options->{ "search" } || "";
2546 $replace = $options->{ "replace" } || "";
2547 $delete = $options->{ "delete" } || "";
2549 while ( $record = get_record( $in ) )
2551 foreach $key ( @{ $options->{ "keys" } } )
2553 if ( exists $record->{ $key } )
2555 if ( $search and $replace ) {
2556 eval "\$record->{ $key } =~ tr/$search/$replace/";
2557 } elsif ( $delete ) {
2558 eval "\$record->{ $key } =~ tr/$delete//d";
2563 put_record( $record, $out );
2568 sub script_translate_seq
2570 # Martin A. Hansen, February 2008.
2572 # Translate DNA sequence into protein sequence.
2574 my ( $in, # handle to in stream
2575 $out, # handle to out stream
2576 $options, # options hash
2581 my ( $record, $frame, %new_record );
2583 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2585 while ( $record = get_record( $in ) )
2587 if ( $record->{ "SEQ" } )
2589 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2591 foreach $frame ( @{ $options->{ "frames" } } )
2593 %new_record = %{ $record };
2595 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2596 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2597 $new_record{ "FRAME" } = $frame;
2599 put_record( \%new_record, $out );
2605 put_record( $record, $out );
2611 sub script_extract_seq
2613 # Martin A. Hansen, August 2007.
2615 # Extract subsequences from sequences in record.
2617 my ( $in, # handle to in stream
2618 $out, # handle to out stream
2619 $options, # options hash
2624 my ( $beg, $end, $len, $record );
2626 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2629 $beg = $options->{ "beg" } - 1; # correcting for start offset
2632 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2634 } elsif ( defined $options->{ "end" } ) {
2635 $end = $options->{ "end" } - 1; # correcting for start offset
2638 $len = $options->{ "len" };
2640 # print "beg->$beg, end->$end, len->$len\n";
2642 while ( $record = get_record( $in ) )
2644 if ( $record->{ "SEQ" } )
2646 if ( defined $beg and defined $end )
2648 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2649 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2651 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2654 elsif ( defined $beg and defined $len )
2656 if ( $len > length $record->{ "SEQ" } ) {
2657 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2659 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2662 elsif ( defined $beg )
2664 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2668 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2670 put_record( $record, $out );
2675 sub script_get_genome_seq
2677 # Martin A. Hansen, December 2007.
2679 # Gets a subsequence from a genome.
2681 my ( $in, # handle to in stream
2682 $out, # handle to out stream
2683 $options, # options hash
2688 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2690 $options->{ "flank" } ||= 0;
2692 if ( $options->{ "genome" } )
2694 $genome = $options->{ "genome" };
2696 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2697 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2699 $fh = Maasha::Common::read_open( $genome_file );
2700 $index = Maasha::Fasta::index_retrieve( $index_file );
2702 shift @{ $index }; # Get rid of the file size info
2704 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2706 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2708 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2710 $beg = $index_beg + $options->{ "beg" } - 1;
2712 if ( $options->{ "len" } ) {
2713 $len = $options->{ "len" };
2714 } elsif ( $options->{ "end" } ) {
2715 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2718 $beg -= $options->{ "flank" };
2719 $len += 2 * $options->{ "flank" };
2721 if ( $beg <= $index_beg )
2723 $len -= $index_beg - $beg;
2727 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2729 next if $beg > $index_beg + $index_len;
2731 $record->{ "CHR" } = $options->{ "chr" };
2732 $record->{ "CHR_BEG" } = $beg - $index_beg;
2733 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2735 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2736 $record->{ "SEQ_LEN" } = $len;
2738 put_record( $record, $out );
2742 while ( $record = get_record( $in ) )
2744 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2746 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2748 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2750 $beg = $record->{ "CHR_BEG" } + $index_beg;
2751 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2753 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2755 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2757 $beg = $record->{ "S_BEG" } + $index_beg;
2758 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2760 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2762 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2764 $beg = $record->{ "S_BEG" } + $index_beg;
2765 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2768 $beg -= $options->{ "flank" };
2769 $len += 2 * $options->{ "flank" };
2771 if ( $beg <= $index_beg )
2773 $len -= $index_beg - $beg;
2777 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2779 next if $beg > $index_beg + $index_len;
2781 $record->{ "CHR_BEG" } = $beg - $index_beg;
2782 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2784 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2786 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2788 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2789 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2792 if ( $options->{ "mask" } )
2794 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2796 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2798 @begs = split ",", $record->{ "Q_BEGS" };
2799 @lens = split ",", $record->{ "BLOCKSIZES" };
2801 for ( $i = 0; $i < @begs; $i++ ) {
2802 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2808 put_record( $record, $out );
2815 sub script_get_genome_align
2817 # Martin A. Hansen, April 2008.
2819 # Gets a subalignment from a multiple genome alignment.
2821 my ( $in, # handle to in stream
2822 $out, # handle to out stream
2823 $options, # options hash
2828 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2830 $options->{ "strand" } ||= "+";
2834 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2836 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2838 $beg = $options->{ "beg" } - 1;
2840 if ( $options->{ "end" } ) {
2841 $end = $options->{ "end" };
2842 } elsif ( $options->{ "len" } ) {
2843 $end = $beg + $options->{ "len" };
2846 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2848 foreach $entry ( @{ $align } )
2850 $entry->{ "CHR" } = $record->{ "CHR" };
2851 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2852 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2853 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2854 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2855 $entry->{ "SCORE" } = $record->{ "SCORE" };
2857 put_record( $entry, $out );
2861 while ( $record = get_record( $in ) )
2863 if ( $record->{ "REC_TYPE" } eq "BED" )
2865 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2867 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2869 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2871 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2873 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2876 foreach $entry ( @{ $align } )
2878 $entry->{ "CHR" } = $record->{ "CHR" };
2879 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2880 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2881 $entry->{ "STRAND" } = $record->{ "STRAND" };
2882 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2883 $entry->{ "SCORE" } = $record->{ "SCORE" };
2885 put_record( $entry, $out );
2893 sub script_get_genome_phastcons
2895 # Martin A. Hansen, February 2008.
2897 # Get phastcons scores from genome intervals.
2899 my ( $in, # handle to in stream
2900 $out, # handle to out stream
2901 $options, # options hash
2906 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2908 $options->{ "flank" } ||= 0;
2910 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2911 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2913 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2914 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2916 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2918 $options->{ "beg" } -= 1; # request is 1-based
2919 $options->{ "end" } -= 1; # request is 1-based
2921 if ( $options->{ "len" } ) {
2922 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2925 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2927 $record->{ "CHR" } = $options->{ "chr" };
2928 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2929 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2931 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2932 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2934 put_record( $record, $out );
2937 while ( $record = get_record( $in ) )
2939 if ( $record->{ "REC_TYPE" } eq "BED" )
2941 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2943 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2945 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2947 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2949 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2952 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2953 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2955 put_record( $record, $out );
2958 close $fh_phastcons if $fh_phastcons;
2964 # Martin A. Hansen, December 2007.
2966 # Folds sequences in stream into secondary structures.
2968 my ( $in, # handle to in stream
2969 $out, # handle to out stream
2974 my ( $record, $type, $struct, $index );
2976 while ( $record = get_record( $in ) )
2978 if ( $record->{ "SEQ" } )
2981 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2984 if ( $type ne "protein" )
2986 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2987 $record->{ "SEC_STRUCT" } = $struct;
2988 $record->{ "FREE_ENERGY" } = $index;
2989 $record->{ "SCORE" } = abs int $index;
2990 $record->{ "SIZE" } = length $struct;
2991 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2995 put_record( $record, $out );
3000 sub script_split_seq
3002 # Martin A. Hansen, August 2007.
3004 # Split a sequence in stream into words.
3006 my ( $in, # handle to in stream
3007 $out, # handle to out stream
3008 $options, # options hash
3013 my ( $record, $new_record, $i, $subseq, %lookup );
3015 $options->{ "word_size" } ||= 7;
3017 while ( $record = get_record( $in ) )
3019 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3021 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3023 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3025 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3027 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3028 $new_record->{ "SEQ" } = $subseq;
3030 put_record( $new_record, $out );
3032 $lookup{ $subseq } = 1;
3036 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3037 $new_record->{ "SEQ" } = $subseq;
3039 put_record( $new_record, $out );
3045 put_record( $record, $out );
3051 sub script_split_bed
3053 # Martin A. Hansen, June 2008.
3055 # Split a BED record into overlapping windows.
3057 my ( $in, # handle to in stream
3058 $out, # handle to out stream
3059 $options, # options hash
3064 my ( $record, $new_record, $i );
3066 $options->{ "window_size" } ||= 20;
3067 $options->{ "step_size" } ||= 1;
3069 while ( $record = get_record( $in ) )
3071 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3073 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3075 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3077 $new_record->{ "REC_TYPE" } = "BED";
3078 $new_record->{ "CHR" } = $record->{ "CHR" };
3079 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3080 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3081 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3082 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3083 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3084 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3086 put_record( $new_record, $out );
3091 put_record( $record, $out );
3097 sub script_align_seq
3099 # Martin A. Hansen, August 2007.
3101 # Align sequences in stream.
3103 my ( $in, # handle to in stream
3104 $out, # handle to out stream
3109 my ( $record, @entries, $entry );
3111 while ( $record = get_record( $in ) )
3113 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3114 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3115 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3116 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3118 put_record( $record, $out );
3122 @entries = Maasha::Align::align( \@entries );
3124 foreach $entry ( @entries )
3126 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3129 SEQ_NAME => $entry->[ SEQ_NAME ],
3130 SEQ => $entry->[ SEQ ],
3133 put_record( $record, $out );
3141 # Martin A. Hansen, February 2008.
3143 # Using the first sequence in stream as reference, tile
3144 # all subsequent sequences based on pairwise alignments.
3146 my ( $in, # handle to in stream
3147 $out, # handle to out stream
3148 $options, # options hash
3153 my ( $record, $first, $ref_entry, @entries );
3157 while ( $record = get_record( $in ) )
3159 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3163 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3169 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3174 put_record( $record, $out );
3178 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3180 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3184 sub script_invert_align
3186 # Martin A. Hansen, February 2008.
3188 # Inverts an alignment showing only non-mathing residues
3189 # using the first sequence as reference.
3191 my ( $in, # handle to in stream
3192 $out, # handle to out stream
3193 $options, # options hash
3198 my ( $record, @entries );
3200 while ( $record = get_record( $in ) )
3202 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3204 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3208 put_record( $record, $out );
3212 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3214 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3218 sub script_patscan_seq
3220 # Martin A. Hansen, August 2007.
3222 # Locates patterns in sequences using scan_for_matches.
3224 my ( $in, # handle to in stream
3225 $out, # handle to out stream
3226 $options, # options hash
3231 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3233 if ( $options->{ "patterns" } ) {
3234 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3235 } elsif ( -f $options->{ "patterns_in" } ) {
3236 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3239 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3241 push @args, "-c" if $options->{ "comp" };
3242 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3243 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3245 $seq_file = "$BP_TMP/patscan.seq";
3246 $pat_file = "$BP_TMP/patscan.pat";
3247 $out_file = "$BP_TMP/patscan.out";
3249 $fh_out = Maasha::Common::write_open( $seq_file );
3253 while ( $record = get_record( $in ) )
3255 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3257 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3259 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3261 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3266 # put_record( $record, $out );
3271 $arg = join " ", @args;
3272 $arg .= " -p" if $type eq "protein";
3274 foreach $pattern ( @{ $patterns } )
3276 $fh_out = Maasha::Common::write_open( $pat_file );
3278 print $fh_out "$pattern\n";
3282 if ( $options->{ 'genome' } ) {
3283 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3284 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3286 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3287 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3290 $fh_in = Maasha::Common::read_open( $out_file );
3292 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3294 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3296 if ( $options->{ 'genome' } )
3298 $result->{ "CHR" } = $result->{ "S_ID" };
3299 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3300 $result->{ "CHR_END" } = $result->{ "S_END" };
3302 delete $result->{ "S_ID" };
3303 delete $result->{ "S_BEG" };
3304 delete $result->{ "S_END" };
3308 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3311 put_record( $result, $out );
3323 sub script_create_blast_db
3325 # Martin A. Hansen, September 2007.
3327 # Creates a NCBI BLAST database with formatdb
3329 my ( $in, # handle to in stream
3330 $out, # handle to out stream
3331 $options, # options hash
3336 my ( $fh, $seq_type, $path, $record );
3338 $path = $options->{ "database" };
3340 $fh = Maasha::Common::write_open( $path );
3342 while ( $record = get_record( $in ) )
3344 put_record( $record, $out ) if not $options->{ "no_stream" };
3346 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3348 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3350 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3356 if ( $seq_type eq "protein" ) {
3357 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3359 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3366 sub script_blast_seq
3368 # Martin A. Hansen, September 2007.
3370 # BLASTs sequences in stream against a given database.
3372 my ( $in, # handle to in stream
3373 $out, # handle to out stream
3374 $options, # options hash
3379 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3381 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3382 $options->{ "filter" } = "F";
3383 $options->{ "filter" } = "T" if $options->{ "filter" };
3384 $options->{ "cpus" } ||= 1;
3386 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna"if $options->{ 'genome' };
3388 $tmp_in = "$BP_TMP/blast_query.seq";
3389 $tmp_out = "$BP_TMP/blast.result";
3391 $fh_out = Maasha::Common::write_open( $tmp_in );
3393 while ( $record = get_record( $in ) )
3395 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3397 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3399 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3402 put_record( $record, $out );
3407 if ( -f $options->{ 'database' } . ".phr" ) {
3408 $s_type = "protein";
3410 $s_type = "nucleotide";
3413 if ( not $options->{ 'program' } )
3415 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3416 $options->{ 'program' } = "blastn";
3417 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3418 $options->{ 'program' } = "blastp";
3419 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3420 $options->{ 'program' } = "blastx";
3421 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3422 $options->{ 'program' } = "tblastn";
3426 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 );
3430 $fh_out = Maasha::Common::read_open( $tmp_out );
3434 while ( $line = <$fh_out> )
3438 next if $line =~ /^#/;
3440 @fields = split /\s+/, $line;
3442 $record->{ "REC_TYPE" } = "BLAST";
3443 $record->{ "Q_ID" } = $fields[ 0 ];
3444 $record->{ "S_ID" } = $fields[ 1 ];
3445 $record->{ "IDENT" } = $fields[ 2 ];
3446 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3447 $record->{ "MISMATCHES" } = $fields[ 4 ];
3448 $record->{ "GAPS" } = $fields[ 5 ];
3449 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3450 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3451 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3452 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3453 $record->{ "E_VAL" } = $fields[ 10 ];
3454 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3456 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3458 $record->{ "STRAND" } = '-';
3460 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3464 $record->{ "STRAND" } = '+';
3467 put_record( $record, $out );
3478 # Martin A. Hansen, August 2007.
3480 # BLATs sequences in stream against a given genome.
3482 my ( $in, # handle to in stream
3483 $out, # handle to out stream
3484 $options, # options hash
3489 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3491 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3493 $options->{ 'tile_size' } ||= 11;
3494 $options->{ 'one_off' } ||= 0;
3495 $options->{ 'min_identity' } ||= 90;
3496 $options->{ 'min_score' } ||= 0;
3497 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3499 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3500 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3501 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3502 $blat_args .= " -minScore=$options->{ 'min_score' }";
3503 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3504 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3506 $query_file = "$BP_TMP/blat.seq";
3508 $fh_out = Maasha::Common::write_open( $query_file );
3510 while ( $record = get_record( $in ) )
3512 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3514 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3515 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3518 put_record( $record, $out );
3523 $blat_args .= " -t=dnax" if $type eq "protein";
3524 $blat_args .= " -q=$type";
3526 $result_file = "$BP_TMP/blat.psl";
3528 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3532 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3534 map { put_record( $_, $out ) } @{ $entries };
3536 unlink $result_file;
3540 sub script_match_seq
3542 # Martin A. Hansen, August 2007.
3544 # BLATs sequences in stream against a given genome.
3546 my ( $in, # handle to in stream
3547 $out, # handle to out stream
3548 $options, # options hash
3553 my ( $record, @entries, $results );
3555 $options->{ "word_size" } ||= 20;
3556 $options->{ "direction" } ||= "both";
3558 while ( $record = get_record( $in ) )
3560 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3561 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3564 put_record( $record, $out );
3567 if ( @entries == 1 )
3569 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3571 map { put_record( $_, $out ) } @{ $results };
3573 elsif ( @entries == 2 )
3575 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3577 map { put_record( $_, $out ) } @{ $results };
3582 sub script_create_vmatch_index
3584 # Martin A. Hansen, January 2008.
3586 # Create a vmatch index from sequences in the stream.
3588 my ( $in, # handle to in stream
3589 $out, # handle to out stream
3590 $options, # options hash
3595 my ( $record, $file_tmp, $fh_tmp, $type );
3597 if ( $options->{ "index_name" } )
3599 $file_tmp = $options->{ 'index_name' };
3600 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3603 while ( $record = get_record( $in ) )
3605 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3607 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3609 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3612 put_record( $record, $out ) if not $options->{ "no_stream" };
3615 if ( $options->{ "index_name" } )
3619 if ( $type eq "protein" ) {
3620 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3622 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3630 sub script_vmatch_seq
3632 # Martin A. Hansen, August 2007.
3634 # Vmatches sequences in stream against a given genome.
3636 my ( $in, # handle to in stream
3637 $out, # handle to out stream
3638 $options, # options hash
3643 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3645 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3647 if ( $options->{ "index_name" } )
3649 @index_files = $options->{ "index_name" };
3653 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3655 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3657 @index_files = sort keys %hash;
3660 while ( $record = get_record( $in ) )
3662 push @records, $record;
3664 put_record( $record, $out );
3667 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3671 $fh_in = Maasha::Common::read_open( $result_file );
3673 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3674 put_record( $record, $out );
3679 unlink $result_file;
3683 sub script_write_fasta
3685 # Martin A. Hansen, August 2007.
3687 # Write FASTA entries from sequences in stream.
3689 my ( $in, # handle to in stream
3690 $out, # handle to out stream
3691 $options, # options hash
3696 my ( $record, $fh );
3698 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3700 while ( $record = get_record( $in ) )
3702 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3703 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3706 put_record( $record, $out ) if not $options->{ "no_stream" };
3713 sub script_write_align
3715 # Martin A. Hansen, August 2007.
3717 # Write pretty alignments aligned sequences in stream.
3719 my ( $in, # handle to in stream
3720 $out, # handle to out stream
3721 $options, # options hash
3726 my ( $fh, $record, @entries );
3728 $fh = write_stream( $options->{ "data_out" } ) ;
3730 while ( $record = get_record( $in ) )
3732 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3733 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3736 put_record( $record, $out ) if not $options->{ "no_stream" };
3739 if ( scalar( @entries ) == 2 ) {
3740 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3741 } elsif ( scalar ( @entries ) > 2 ) {
3742 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3749 sub script_write_blast
3751 # Martin A. Hansen, November 2007.
3753 # Write data in blast table format (-m8 and 9).
3755 my ( $in, # handle to in stream
3756 $out, # handle to out stream
3757 $options, # options hash
3762 my ( $fh, $record, $first );
3764 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3768 while ( $record = get_record( $in ) )
3770 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3772 if ( $options->{ "comment" } and $first )
3774 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";
3779 if ( $record->{ "STRAND" } eq "-" ) {
3780 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3783 print $fh join( "\t",
3784 $record->{ "Q_ID" },
3785 $record->{ "S_ID" },
3786 $record->{ "IDENT" },
3787 $record->{ "ALIGN_LEN" },
3788 $record->{ "MISMATCHES" },
3789 $record->{ "GAPS" },
3790 $record->{ "Q_BEG" } + 1,
3791 $record->{ "Q_END" } + 1,
3792 $record->{ "S_BEG" } + 1,
3793 $record->{ "S_END" } + 1,
3794 $record->{ "E_VAL" },
3795 $record->{ "BIT_SCORE" }
3799 put_record( $record, $out ) if not $options->{ "no_stream" };
3806 sub script_write_tab
3808 # Martin A. Hansen, August 2007.
3810 # Write data as table.
3812 my ( $in, # handle to in stream
3813 $out, # handle to out stream
3814 $options, # options hash
3819 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3821 $options->{ "delimit" } ||= "\t";
3823 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3825 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3827 while ( $record = get_record( $in ) )
3832 if ( $options->{ "keys" } )
3834 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3838 foreach $key ( @{ $options->{ "keys" } } )
3840 if ( exists $record->{ $key } )
3842 push @keys, $key if $options->{ "comment" };
3843 push @vals, $record->{ $key };
3850 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3852 next if exists $no_keys{ $key };
3854 push @keys, $key if $options->{ "comment" };
3855 push @vals, $record->{ $key };
3859 if ( @keys and $options->{ "comment" } )
3861 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3863 delete $options->{ "comment" };
3866 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3868 put_record( $record, $out ) if not $options->{ "no_stream" };
3875 sub script_write_bed
3877 # Martin A. Hansen, August 2007.
3879 # Write BED format for the UCSC genome browser using records in stream.
3881 my ( $in, # handle to in stream
3882 $out, # handle to out stream
3883 $options, # options hash
3888 my ( $fh, $record, $new_record );
3890 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3892 while ( $record = get_record( $in ) )
3894 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3896 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3898 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3900 $new_record->{ "CHR" } = $record->{ "S_ID" };
3901 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3902 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3903 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3904 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3905 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3907 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3909 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3911 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3913 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3915 $new_record->{ "CHR" } = $record->{ "S_ID" };
3916 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3917 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3918 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3919 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3920 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3922 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3924 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3926 $new_record->{ "CHR" } = $record->{ "S_ID" };
3927 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3928 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3929 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3930 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3931 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3933 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3935 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3937 Maasha::UCSC::bed_put_entry( $record, $fh );
3940 put_record( $record, $out ) if not $options->{ "no_stream" };
3947 sub script_write_psl
3949 # Martin A. Hansen, August 2007.
3951 # Write PSL output from stream.
3953 my ( $in, # handle to in stream
3954 $out, # handle to out stream
3955 $options, # options hash
3960 my ( $fh, $record, @output, $first );
3964 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3966 while ( $record = get_record( $in ) )
3968 put_record( $record, $out ) if not $options->{ "no_stream" };
3970 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3972 Maasha::UCSC::psl_put_header( $fh ) if $first;
3973 Maasha::UCSC::psl_put_entry( $record, $fh );
3982 sub script_write_fixedstep
3984 # Martin A. Hansen, Juli 2008.
3986 # Write fixedStep entries from recrods in the stream.
3988 my ( $in, # handle to in stream
3989 $out, # handle to out stream
3990 $options, # options hash
3995 my ( $fh, $record, $vals );
3997 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3999 while ( $record = get_record( $in ) )
4001 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4003 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4005 $vals = $record->{ 'VALS' };
4009 print $fh "$vals\n";
4012 put_record( $record, $out ) if not $options->{ "no_stream" };
4019 sub script_write_2bit
4021 # Martin A. Hansen, March 2008.
4023 # Write sequence entries from stream in 2bit format.
4025 my ( $in, # handle to in stream
4026 $out, # handle to out stream
4027 $options, # options hash
4032 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
4034 $mask = 1 if not $options->{ "no_mask" };
4036 $tmp_file = "$BP_TMP/write_2bit.fna";
4037 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4039 $fh_out = write_stream( $options->{ "data_out" } );
4041 while ( $record = get_record( $in ) )
4043 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4044 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
4047 put_record( $record, $out ) if not $options->{ "no_stream" };
4052 $fh_in = Maasha::Common::read_open( $tmp_file );
4054 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4063 sub script_write_solid
4065 # Martin A. Hansen, April 2008.
4067 # Write di-base encoded Solid sequence from entries in stream.
4069 my ( $in, # handle to in stream
4070 $out, # handle to out stream
4071 $options, # options hash
4076 my ( $record, $fh, $seq_cs );
4078 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4080 while ( $record = get_record( $in ) )
4082 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
4084 $seq_cs = Maasha::Solid::seq2color_space( uc $record->{ "SEQ" } );
4086 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
4089 put_record( $record, $out ) if not $options->{ "no_stream" };
4096 sub script_plot_seqlogo
4098 # Martin A. Hansen, August 2007.
4100 # Calculates and writes a sequence logo for alignments.
4102 my ( $in, # handle to in stream
4103 $out, # handle to out stream
4104 $options, # options hash
4109 my ( $record, @entries, $logo, $fh );
4111 while ( $record = get_record( $in ) )
4113 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4114 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4117 put_record( $record, $out ) if not $options->{ "no_stream" };
4120 $logo = Maasha::Plot::seq_logo( \@entries );
4122 $fh = write_stream( $options->{ "data_out" } );
4130 sub script_plot_phastcons_profiles
4132 # Martin A. Hansen, January 2008.
4134 # Plots PhastCons profiles.
4136 my ( $in, # handle to in stream
4137 $out, # handle to out stream
4138 $options, # options hash
4143 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4145 $options->{ "title" } ||= "PhastCons Profiles";
4147 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4148 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4150 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4151 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4153 while ( $record = get_record( $in ) )
4155 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4157 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4159 push @{ $AoA }, [ @{ $scores } ];
4162 put_record( $record, $out ) if not $options->{ "no_stream" };
4165 Maasha::UCSC::phastcons_normalize( $AoA );
4167 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4168 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4170 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4172 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4174 $fh = write_stream( $options->{ "data_out" } );
4176 print $fh "$_\n" foreach @{ $plot };
4182 sub script_analyze_bed
4184 # Martin A. Hansen, March 2008.
4186 # Analyze BED entries in stream.
4188 my ( $in, # handle to in stream
4189 $out, # handle to out stream
4190 $options, # options hash
4197 while ( $record = get_record( $in ) )
4199 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4201 put_record( $record, $out );
4206 sub script_analyze_vals
4208 # Martin A. Hansen, August 2007.
4210 # Analyze values for given keys in stream.
4212 my ( $in, # handle to in stream
4213 $out, # handle to out stream
4214 $options, # options hash
4219 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4221 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4223 while ( $record = get_record( $in ) )
4225 foreach $key ( keys %{ $record } )
4227 next if $options->{ "keys" } and not exists $key_hash{ $key };
4229 $analysis->{ $key }->{ "COUNT" }++;
4231 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4233 $analysis->{ $key }->{ "TYPE" } = "num";
4234 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4235 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4236 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4240 $len = length $record->{ $key };
4242 $analysis->{ $key }->{ "TYPE" } = "alph";
4243 $analysis->{ $key }->{ "SUM" } += $len;
4244 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4245 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4249 put_record( $record, $out ) if not $options->{ "no_stream" };
4252 foreach $key ( keys %{ $analysis } )
4254 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4255 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4258 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4268 if ( $options->{ "keys" } ) {
4269 @keys = @{ $options->{ "keys" } };
4271 @keys = keys %{ $analysis };
4274 foreach $key ( @keys )
4276 $keys .= sprintf "% 15s", $key;
4277 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4278 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4279 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4280 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4281 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4282 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4285 print $out "$keys\n";
4286 print $out "$types\n";
4287 print $out "$counts\n";
4288 print $out "$mins\n";
4289 print $out "$maxs\n";
4290 print $out "$sums\n";
4291 print $out "$means\n";
4295 sub script_head_records
4297 # Martin A. Hansen, August 2007.
4299 # Display the first sequences in stream.
4301 my ( $in, # handle to in stream
4302 $out, # handle to out stream
4303 $options, # options hash
4308 my ( $record, $count );
4310 $options->{ "num" } ||= 10;
4314 while ( $record = get_record( $in ) )
4318 put_record( $record, $out );
4320 last if $count == $options->{ "num" };
4325 sub script_remove_keys
4327 # Martin A. Hansen, August 2007.
4329 # Remove keys from stream.
4331 my ( $in, # handle to in stream
4332 $out, # handle to out stream
4333 $options, # options hash
4338 my ( $record, $new_record );
4340 while ( $record = get_record( $in ) )
4342 if ( $options->{ "keys" } )
4344 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4346 elsif ( $options->{ "save_keys" } )
4348 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4350 $record = $new_record;
4353 put_record( $record, $out ) if keys %{ $record };
4358 sub script_rename_keys
4360 # Martin A. Hansen, August 2007.
4362 # Rename keys in stream.
4364 my ( $in, # handle to in stream
4365 $out, # handle to out stream
4366 $options, # options hash
4373 while ( $record = get_record( $in ) )
4375 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4377 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4379 delete $record->{ $options->{ "keys" }->[ 0 ] };
4382 put_record( $record, $out );
4387 sub script_uniq_vals
4389 # Martin A. Hansen, August 2007.
4391 # Find unique values in stream.
4393 my ( $in, # handle to in stream
4394 $out, # handle to out stream
4395 $options, # options hash
4400 my ( %hash, $record );
4402 while ( $record = get_record( $in ) )
4404 if ( $record->{ $options->{ "key" } } )
4406 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4408 put_record( $record, $out );
4410 $hash{ $record->{ $options->{ "key" } } } = 1;
4412 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4414 put_record( $record, $out );
4418 $hash{ $record->{ $options->{ "key" } } } = 1;
4423 put_record( $record, $out );
4429 sub script_merge_vals
4431 # Martin A. Hansen, August 2007.
4433 # Rename keys in stream.
4435 my ( $in, # handle to in stream
4436 $out, # handle to out stream
4437 $options, # options hash
4442 my ( $record, @join, $i );
4444 $options->{ "delimit" } ||= '_';
4446 while ( $record = get_record( $in ) )
4448 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4450 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4452 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4453 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4456 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4459 put_record( $record, $out );
4466 # Martin A. Hansen, August 2007.
4468 # Grab for records in stream.
4470 my ( $in, # handle to in stream
4471 $out, # handle to out stream
4472 $options, # options hash
4477 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4479 if ( $options->{ "patterns" } )
4481 $patterns = [ split ",", $options->{ "patterns" } ];
4483 elsif ( -f $options->{ "patterns_in" } )
4485 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4487 elsif ( -f $options->{ "exact_in" } )
4489 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4491 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4496 if ( $options->{ "eval" } )
4498 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4506 while ( $record = get_record( $in ) )
4512 if ( $options->{ "keys" } )
4514 foreach $key ( @{ $options->{ "keys" } } )
4516 if ( exists $lookup_hash{ $record->{ $key } } )
4525 foreach $key ( keys %{ $record } )
4527 if ( not $options->{ "vals_only" } )
4529 if ( exists $lookup_hash{ $key } )
4536 if ( not $options->{ "keys_only" } )
4538 if ( exists $lookup_hash{ $record->{ $key } } )
4549 foreach $pattern ( @{ $patterns } )
4551 if ( $options->{ "keys" } )
4553 foreach $key ( @{ $options->{ "keys" } } )
4555 $pos = index $record->{ $key }, $pattern;
4557 goto FOUND if $pos >= 0;
4562 foreach $key ( keys %{ $record } )
4564 if ( not $options->{ "vals_only" } )
4566 $pos = index $key, $pattern;
4568 goto FOUND if $pos >= 0;
4571 if ( not $options->{ "keys_only" } )
4573 $pos = index $record->{ $key }, $pattern;
4575 goto FOUND if $pos >= 0;
4581 elsif ( $options->{ "regex" } )
4583 if ( $options->{ "keys" } )
4585 foreach $key ( @{ $options->{ "keys" } } )
4587 if ( $options->{ "case_insensitive" } ) {
4588 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4590 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4593 goto FOUND if $pos >= 0;
4598 foreach $key ( keys %{ $record } )
4600 if ( not $options->{ "vals_only" } )
4602 if ( $options->{ "case_insensitive" } ) {
4603 $pos = 1 if $key =~ /$options->{'regex'}/i;
4605 $pos = 1 if $key =~ /$options->{'regex'}/;
4608 goto FOUND if $pos >= 0;
4611 if ( not $options->{ "keys_only" } )
4613 if ( $options->{ "case_insensitive" } ) {
4614 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4616 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4619 goto FOUND if $pos >= 0;
4624 elsif ( $options->{ "eval" } )
4626 if ( defined $record->{ $key } )
4628 if ( $op eq "<" and $record->{ $key } < $val ) {
4629 $pos = 1 and goto FOUND;
4630 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4631 $pos = 1 and goto FOUND;
4632 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4633 $pos = 1 and goto FOUND;
4634 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4635 $pos = 1 and goto FOUND;
4636 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4637 $pos = 1 and goto FOUND;
4638 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4639 $pos = 1 and goto FOUND;
4640 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4641 $pos = 1 and goto FOUND;
4642 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4643 $pos = 1 and goto FOUND;
4650 if ( $pos >= 0 and not $options->{ "invert" } ) {
4651 put_record( $record, $out );
4652 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4653 put_record( $record, $out );
4661 # Martin A. Hansen, August 2007.
4663 # Evaluate extression for records in stream.
4665 my ( $in, # handle to in stream
4666 $out, # handle to out stream
4667 $options, # options hash
4672 my ( $record, $eval_key, $eval_val, $check, @keys );
4674 while ( $record = get_record( $in ) )
4676 if ( $options->{ "eval" } )
4678 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4686 @keys = split /\W+/, $eval_val;
4687 @keys = grep { ! /^\d+$/ } @keys;
4692 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4694 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4697 put_record( $record, $out );
4704 # Martin A. Hansen, June 2008.
4708 my ( $in, # handle to in stream
4709 $out, # handle to out stream
4710 $options, # options hash
4715 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4717 while ( $record = get_record( $in ) )
4721 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4723 push @rows, $record->{ $key };
4727 push @matrix, [ @rows ];
4732 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4734 foreach $row ( @matrix )
4736 for ( $i = 0; $i < @{ $row }; $i++ ) {
4737 $record->{ "V$i" } = $row->[ $i ];
4740 put_record( $record, $out );
4745 sub script_add_ident
4747 # Martin A. Hansen, May 2008.
4749 # Add a unique identifier to each record in stream.
4751 my ( $in, # handle to in stream
4752 $out, # handle to out stream
4753 $options, # options hash
4758 my ( $record, $key, $prefix, $i );
4760 $key = $options->{ "key" } || "ID";
4761 $prefix = $options->{ "prefix" } || "ID";
4765 while ( $record = get_record( $in ) )
4767 $record->{ $key } = sprintf( "$prefix%08d", $i );
4769 put_record( $record, $out );
4776 sub script_count_records
4778 # Martin A. Hansen, August 2007.
4780 # Count records in stream.
4782 my ( $in, # handle to in stream
4783 $out, # handle to out stream
4784 $options, # options hash
4789 my ( $record, $count, $result, $fh, $line );
4793 if ( $options->{ "no_stream" } )
4795 while ( $line = <$in> )
4799 $count++ if $line eq "---";
4804 while ( $record = get_record( $in ) )
4806 put_record( $record, $out );
4812 $result = { "count_records" => $count };
4814 $fh = write_stream( $options->{ "data_out" } );
4816 put_record( $result, $fh );
4822 sub script_random_records
4824 # Martin A. Hansen, August 2007.
4826 # Pick a number or random records from stream.
4828 my ( $in, # handle to in stream
4829 $out, # handle to out stream
4830 $options, # options hash
4835 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4837 $options->{ "num" } ||= 10;
4839 $tmp_file = "$BP_TMP/random_records.tmp";
4841 $fh_out = Maasha::Common::write_open( $tmp_file );
4845 while ( $record = get_record( $in ) )
4847 put_record( $record, $fh_out );
4857 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4859 while ( $i < $options->{ "num" } )
4861 $rand = int( rand( $count ) );
4863 if ( not exists $rand_hash{ $rand } )
4865 $rand_hash{ $rand } = 1;
4867 $max = $rand if $rand > $max;
4873 $fh_in = Maasha::Common::read_open( $tmp_file );
4877 while ( $record = get_record( $fh_in ) )
4879 put_record( $record, $out ) if exists $rand_hash{ $count };
4881 last if $count == $max;
4892 sub script_sort_records
4894 # Martin A. Hansen, August 2007.
4896 # Sort to sort records according to keys.
4898 my ( $in, # handle to in stream
4899 $out, # handle to out stream
4900 $options, # options hash
4905 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4907 foreach $key ( @{ $options->{ "keys" } } )
4909 if ( $key =~ s/n$// ) {
4910 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4912 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4916 $sort_str = join " or ", @sort_cmd;
4917 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4919 while ( $record = get_record( $in ) ) {
4920 push @records, $record;
4923 @records = sort $sort_sub @records;
4925 if ( $options->{ "reverse" } )
4927 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4928 put_record( $records[ $i ], $out );
4933 for ( $i = 0; $i < scalar @records; $i++ ) {
4934 put_record( $records[ $i ], $out );
4940 sub script_count_vals
4942 # Martin A. Hansen, August 2007.
4944 # Count records in stream.
4946 my ( $in, # handle to in stream
4947 $out, # handle to out stream
4948 $options, # options hash
4953 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4955 $tmp_file = "$BP_TMP/count_cache.tmp";
4957 $fh_out = Maasha::Common::write_open( $tmp_file );
4961 while ( $record = get_record( $in ) )
4963 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4965 push @records, $record;
4967 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4969 map { put_record( $_, $fh_out ) } @records;
4976 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4987 $fh_in = Maasha::Common::read_open( $tmp_file );
4989 while ( $record = get_record( $fh_in ) )
4991 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4993 put_record( $record, $out );
4995 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5003 foreach $record ( @records )
5005 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5007 put_record( $record, $out );
5014 sub script_plot_histogram
5016 # Martin A. Hansen, September 2007.
5018 # Plot a simple histogram for a given key using GNU plot.
5020 my ( $in, # handle to in stream
5021 $out, # handle to out stream
5022 $options, # options hash
5027 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5029 $options->{ "title" } ||= "Histogram";
5030 $options->{ "sort" } ||= "num";
5032 while ( $record = get_record( $in ) )
5034 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5036 put_record( $record, $out ) if not $options->{ "no_stream" };
5039 if ( $options->{ "sort" } eq "num" ) {
5040 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5042 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5045 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5047 $fh = write_stream( $options->{ "data_out" } );
5049 print $fh "$_\n" foreach @{ $result };
5055 sub script_plot_lendist
5057 # Martin A. Hansen, August 2007.
5059 # Plot length distribution using GNU plot.
5061 my ( $in, # handle to in stream
5062 $out, # handle to out stream
5063 $options, # options hash
5068 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5070 $options->{ "title" } ||= "Length Distribution";
5072 while ( $record = get_record( $in ) )
5074 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5076 put_record( $record, $out ) if not $options->{ "no_stream" };
5079 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5081 for ( $i = 0; $i < $max; $i++ ) {
5082 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5085 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5087 $fh = write_stream( $options->{ "data_out" } );
5089 print $fh "$_\n" foreach @{ $result };
5095 sub script_plot_chrdist
5097 # Martin A. Hansen, August 2007.
5099 # Plot chromosome distribution using GNU plot.
5101 my ( $in, # handle to in stream
5102 $out, # handle to out stream
5103 $options, # options hash
5108 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5110 $options->{ "title" } ||= "Chromosome Distribution";
5112 while ( $record = get_record( $in ) )
5114 if ( $record->{ "CHR" } ) { # generic
5115 $data_hash{ $record->{ "CHR" } }++;
5116 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5117 $data_hash{ $record->{ "S_ID" } }++;
5118 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5119 $data_hash{ $record->{ "S_ID" } }++;
5120 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5121 $data_hash{ $record->{ "S_ID" } }++;
5124 put_record( $record, $out ) if not $options->{ "no_stream" };
5127 foreach $elem ( keys %data_hash )
5131 $sort_key =~ s/chr//i;
5133 $sort_key =~ s/^X(.*)/99$1/;
5134 $sort_key =~ s/^Y(.*)/99$1/;
5135 $sort_key =~ s/^Z(.*)/999$1/;
5136 $sort_key =~ s/^M(.*)/9999$1/;
5137 $sort_key =~ s/^U(.*)/99999$1/;
5139 $count = $sort_key =~ tr/_//;
5141 $sort_key =~ s/_.*/"999999" x $count/ex;
5143 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5146 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5148 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5150 $fh = write_stream( $options->{ "data_out" } );
5152 print $fh "$_\n" foreach @{ $result };
5158 sub script_plot_karyogram
5160 # Martin A. Hansen, August 2007.
5162 # Plot hits on karyogram.
5164 my ( $in, # handle to in stream
5165 $out, # handle to out stream
5166 $options, # options hash
5171 my ( %options, $record, @data, $fh, $result, %data_hash );
5173 $options->{ "genome" } ||= "human";
5174 $options->{ "feat_color" } ||= "black";
5176 while ( $record = get_record( $in ) )
5178 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5180 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5183 put_record( $record, $out ) if not $options->{ "no_stream" };
5186 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5188 $fh = write_stream( $options->{ "data_out" } );
5196 sub script_plot_matches
5198 # Martin A. Hansen, August 2007.
5200 # Plot matches in 2D generating a dotplot.
5202 my ( $in, # handle to in stream
5203 $out, # handle to out stream
5204 $options, # options hash
5209 my ( $record, @data, $fh, $result, %data_hash );
5211 $options->{ "direction" } ||= "both";
5213 while ( $record = get_record( $in ) )
5215 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5216 push @data, $record;
5219 put_record( $record, $out ) if not $options->{ "no_stream" };
5222 $options->{ "title" } ||= "plot_matches";
5223 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5224 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5226 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5228 $fh = write_stream( $options->{ "data_out" } );
5230 print $fh "$_\n" foreach @{ $result };
5236 sub script_length_vals
5238 # Martin A. Hansen, August 2007.
5240 # Determine the length of the value for given keys.
5242 my ( $in, # handle to in stream
5243 $out, # handle to out stream
5244 $options, # options hash
5249 my ( $record, $key );
5251 while ( $record = get_record( $in ) )
5253 foreach $key ( @{ $options->{ "keys" } } )
5255 if ( $record->{ $key } ) {
5256 $record->{ $key . "_LEN" } = length $record->{ $key };
5260 put_record( $record, $out );
5267 # Martin A. Hansen, August 2007.
5269 # Calculates the sums for values of given keys.
5271 my ( $in, # handle to in stream
5272 $out, # handle to out stream
5273 $options, # options hash
5278 my ( $record, $key, %sum_hash, $fh );
5280 while ( $record = get_record( $in ) )
5282 foreach $key ( @{ $options->{ "keys" } } )
5284 if ( $record->{ $key } ) {
5285 $sum_hash{ $key } += $record->{ $key };
5289 put_record( $record, $out ) if not $options->{ "no_stream" };
5292 $fh = write_stream( $options->{ "data_out" } );
5294 foreach $key ( @{ $options->{ "keys" } } ) {
5295 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5302 sub script_mean_vals
5304 # Martin A. Hansen, August 2007.
5306 # Calculate the mean of values of given keys.
5308 my ( $in, # handle to in stream
5309 $out, # handle to out stream
5310 $options, # options hash
5315 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5317 while ( $record = get_record( $in ) )
5319 foreach $key ( @{ $options->{ "keys" } } )
5321 if ( $record->{ $key } )
5323 $sum_hash{ $key } += $record->{ $key };
5324 $count_hash{ $key }++;
5328 put_record( $record, $out ) if not $options->{ "no_stream" };
5331 $fh = write_stream( $options->{ "data_out" } );
5333 foreach $key ( @{ $options->{ "keys" } } )
5335 if ( $count_hash{ $key } ) {
5336 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5341 put_record( { $key . "_MEAN" => $mean } , $fh );
5348 sub script_median_vals
5350 # Martin A. Hansen, March 2008.
5352 # Calculate the median values of given keys.
5354 my ( $in, # handle to in stream
5355 $out, # handle to out stream
5356 $options, # options hash
5361 my ( $record, $key, %median_hash, $median, $fh );
5363 while ( $record = get_record( $in ) )
5365 foreach $key ( @{ $options->{ "keys" } } ) {
5366 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5369 put_record( $record, $out ) if not $options->{ "no_stream" };
5372 $fh = write_stream( $options->{ "data_out" } );
5374 foreach $key ( @{ $options->{ "keys" } } )
5376 if ( $median_hash{ $key } ) {
5377 $median = Maasha::Calc::median( $median_hash{ $key } );
5382 put_record( { $key . "_MEDIAN" => $median } , $fh );
5391 # Martin A. Hansen, February 2008.
5393 # Determine the maximum values of given keys.
5395 my ( $in, # handle to in stream
5396 $out, # handle to out stream
5397 $options, # options hash
5402 my ( $record, $key, $fh, %max_hash, $max_record );
5404 while ( $record = get_record( $in ) )
5406 foreach $key ( @{ $options->{ "keys" } } )
5408 if ( $record->{ $key } )
5410 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5414 put_record( $record, $out ) if not $options->{ "no_stream" };
5417 $fh = write_stream( $options->{ "data_out" } );
5419 foreach $key ( @{ $options->{ "keys" } } )
5421 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5424 put_record( $max_record, $fh );
5432 # Martin A. Hansen, February 2008.
5434 # Determine the minimum values of given keys.
5436 my ( $in, # handle to in stream
5437 $out, # handle to out stream
5438 $options, # options hash
5443 my ( $record, $key, $fh, %min_hash, $min_record );
5445 while ( $record = get_record( $in ) )
5447 foreach $key ( @{ $options->{ "keys" } } )
5449 if ( defined $record->{ $key } )
5451 if ( exists $min_hash{ $key } ) {
5452 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5454 $min_hash{ $key } = $record->{ $key };
5459 put_record( $record, $out ) if not $options->{ "no_stream" };
5462 $fh = write_stream( $options->{ "data_out" } );
5464 foreach $key ( @{ $options->{ "keys" } } )
5466 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5469 put_record( $min_record, $fh );
5475 sub script_upload_to_ucsc
5477 # Martin A. Hansen, August 2007.
5479 # Calculate the mean of values of given keys.
5481 my ( $in, # handle to in stream
5482 $out, # handle to out stream
5483 $options, # options hash
5488 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5489 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5491 $options->{ "short_label" } ||= $options->{ 'table' };
5492 $options->{ "long_label" } ||= $options->{ 'table' };
5493 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5494 $options->{ "priority" } ||= 1;
5495 $options->{ "visibility" } ||= "pack";
5496 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5497 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5499 $file = "$BP_TMP/ucsc_upload.tmp";
5507 if ( $options->{ 'wiggle' } )
5509 $options->{ "visibility" } = "full";
5511 while ( $record = get_record( $in ) )
5513 put_record( $record, $out ) if not $options->{ "no_stream" };
5515 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5516 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5517 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5519 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5521 $fh_out = $fh_hash{ $record->{ "CHR" } };
5523 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5526 map { close $_ } keys %fh_hash;
5528 $fh_out = Maasha::Common::write_open( $file );
5530 foreach $chr ( sort keys %fh_hash )
5532 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5534 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5538 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5540 $chr = $entry->{ 'CHR' };
5541 $beg = $entry->{ 'CHR_BEG' };
5542 $end = $entry->{ 'CHR_END' };
5543 $q_id = $entry->{ 'Q_ID' };
5545 if ( $q_id =~ /_(\d+)$/ ) {
5555 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5560 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5561 $block->[ $i ] += $clones;
5564 $max = Maasha::Calc::max( $max, $end );
5573 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5574 $block->[ $i ] += $clones;
5581 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5583 unlink "$BP_TMP/$chr";
5588 $wig_file = "$options->{ 'table' }.wig";
5589 $wib_file = "$options->{ 'table' }.wib";
5591 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5593 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5595 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5597 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5598 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5608 $fh_out = Maasha::Common::write_open( $file );
5610 while ( $record = get_record( $in ) )
5612 put_record( $record, $out ) if not $options->{ "no_stream" };
5614 if ( $record->{ "REC_TYPE" } eq "PSL" )
5616 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5617 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5621 $format = "PSL" if not $format;
5623 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5625 # chrom chromStart chromEnd name score strand size secStr conf
5627 print $fh_out join ( "\t",
5629 $record->{ "CHR_BEG" },
5630 $record->{ "CHR_END" } + 1,
5631 $record->{ "Q_ID" },
5632 $record->{ "SCORE" },
5633 $record->{ "STRAND" },
5634 $record->{ "SIZE" },
5635 $record->{ "SEC_STRUCT" },
5636 $record->{ "CONF" },
5639 $format = "BED_SS" if not $format;
5641 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5643 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5645 $format = "BED" if not $format;
5646 $columns = $record->{ "BED_COLS" } if not $columns;
5648 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5650 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5652 $format = "BED" if not $format;
5653 $columns = 6 if not $columns;
5655 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5657 $record->{ "CHR" } = $record->{ "S_ID" };
5658 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5659 $record->{ "CHR_END" } = $record->{ "S_END" };
5660 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5662 $format = "BED" if not $format;
5663 $columns = 6 if not $columns;
5665 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5667 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5669 $record->{ "CHR" } = $record->{ "S_ID" };
5670 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5671 $record->{ "CHR_END" } = $record->{ "S_END" };
5672 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5673 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5675 $format = "BED" if not $format;
5676 $columns = 6 if not $columns;
5678 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5681 if ( $i == $options->{ "chunk_size" } )
5685 if ( $format eq "BED" ) {
5686 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5687 } elsif ( $format eq "PSL" ) {
5688 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5697 $fh_out = Maasha::Common::write_open( $file );
5706 if ( exists $options->{ "database" } and $options->{ "table" } )
5708 if ( $format eq "BED" )
5710 $type = "bed $columns";
5712 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5714 elsif ( $format eq "BED_SS" )
5716 $options->{ "sec_struct" } = 1;
5718 $type = "sec_struct";
5720 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5722 elsif ( $format eq "PSL" )
5726 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5728 elsif ( $format eq "WIGGLE" )
5732 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5737 Maasha::UCSC::update_my_tracks( $options, $type );
5742 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5747 # Martin A. Hansen, July 2007.
5749 # Opens a stream to STDIN or a file,
5751 my ( $path, # path - OPTIONAL
5754 # Returns filehandle.
5758 if ( not -t STDIN ) {
5759 $fh = Maasha::Common::read_stdin();
5760 } elsif ( not $path ) {
5761 # Maasha::Common::error( qq(no data stream) );
5763 $fh = Maasha::Common::read_open( $path );
5766 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5774 # Martin A. Hansen, August 2007.
5776 # Opens a stream to STDOUT or a file.
5778 my ( $path, # path - OPTIONAL
5779 $gzip, # compress data - OPTIONAL
5782 # Returns filehandle.
5787 $fh = Maasha::Common::write_open( $path, $gzip );
5789 $fh = Maasha::Common::write_stdout();
5798 # Martin A. Hansen, July 2007.
5800 # Reads one record at a time and converts that record
5801 # to a Perl data structure (a hash) which is returned.
5806 # Returns data structure.
5808 my ( $block, @lines, $line, $key, $value, %record );
5810 local $/ = "\n---\n";
5816 return if not defined $block;
5818 @lines = split "\n", $block;
5820 foreach $line ( @lines )
5822 ( $key, $value ) = split ": ", $line;
5824 $record{ $key } = $value;
5827 return wantarray ? %record : \%record;
5833 # Martin A. Hansen, July 2007.
5835 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5837 my ( $data, # data structure
5838 $fh, # file handle - OPTIONAL
5843 if ( scalar keys %{ $data } )
5847 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5852 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5863 # Martin A. Hansen, November 2007.
5865 # Extracts files from an explicit GetOpt::Long argument
5866 # allowing for the use of glob. E.g.
5867 # --data_in=test.fna
5868 # --data_in=test.fna,test2.fna
5870 # --data_in=test.fna,/dir/*.fna
5872 my ( $option, # option from GetOpt::Long
5877 my ( $elem, @files );
5879 foreach $elem ( split ",", $option )
5883 } elsif ( $elem =~ /\*/ ) {
5884 push @files, glob( $elem );
5888 return wantarray ? @files : \@files;
5894 # Martin A. Hansen, April 2008.
5896 # Removes temporary directory and exits gracefully.
5897 # This subroutine is meant to be run always as the last
5898 # thing even if a script is dies or is interrupted
5901 my ( $sig, # signal from the %SIG
5904 # print STDERR "signal->$sig<-\n";
5912 if ( $sig =~ /MAASHA_ERROR/ ) {
5913 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5914 } elsif ( $sig eq "INT" ) {
5915 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5916 } elsif ( $sig eq "TERM" ) {
5917 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5919 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5922 # This is a really bad solution, potentially, anyone can include this module and set
5923 # the BP_TMP to point at any dir and thus take out the machine !!!
5925 Maasha::Common::dir_remove( $BP_TMP );
5934 # This is a really bad solution, potentially, anyone can include this module and set
5935 # the BP_TMP to point at any dir and thus take out the machine !!!
5937 Maasha::Common::dir_remove( $BP_TMP );
5941 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5948 sub script_read_soft
5950 # Martin A. Hansen, December 2007.
5953 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
5955 my ( $in, # handle to in stream
5956 $out, # handle to out stream
5957 $options, # options hash
5962 my ( $data_in, $file, $num, $records, $record );
5964 while ( $record = get_record( $in ) ) {
5965 put_record( $record, $out );
5970 foreach $file ( @{ $options->{ "files" } } )
5972 $records = Maasha::NCBI::soft_parse( $file );
5974 foreach $record ( @{ $records } )
5976 put_record( $record, $out );
5978 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
5986 close $data_in if $data_in;