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 "create_fixedstep_index" ) { script_create_fixedstep_index( $in, $out, $options ) }
218 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
219 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
220 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
221 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
222 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
223 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
224 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
225 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
226 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
227 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
228 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
229 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
230 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
231 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
232 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
233 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
234 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
235 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
236 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
237 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
238 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
239 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
240 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
241 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
242 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
243 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
244 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
245 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
246 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
247 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
248 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
249 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
250 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
251 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
252 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
253 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
254 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
255 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
256 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
258 close $in if defined $in;
261 # unset status - missing
262 # write log file - missing
268 # Martin A. Hansen, February 2008.
270 # Gets options from commandline and checks these vigerously.
272 my ( $script, # name of script
277 my ( %options, @options, $opt, @genomes );
279 if ( $script eq "print_usage" )
285 elsif ( $script eq "read_fasta" )
292 elsif ( $script eq "read_tab" )
303 elsif ( $script eq "read_psl" )
310 elsif ( $script eq "read_bed" )
317 elsif ( $script eq "read_fixedstep" )
324 elsif ( $script eq "read_blast_tab" )
331 elsif ( $script eq "read_embl" )
341 elsif ( $script eq "read_stockholm" )
348 elsif ( $script eq "read_phastcons" )
359 elsif ( $script eq "read_soft" )
366 elsif ( $script eq "read_gff" )
373 elsif ( $script eq "read_2bit" )
381 elsif ( $script eq "read_solexa" )
389 elsif ( $script eq "read_solid" )
397 elsif ( $script eq "read_mysql" )
406 elsif ( $script eq "format_genome" )
415 elsif ( $script eq "length_seq" )
422 elsif ( $script eq "oligo_freq" )
429 elsif ( $script eq "create_weight_matrix" )
435 elsif ( $script eq "transliterate_seq" )
443 elsif ( $script eq "transliterate_vals" )
452 elsif ( $script eq "translate_seq" )
458 elsif ( $script eq "extract_seq" )
466 elsif ( $script eq "get_genome_seq" )
478 elsif ( $script eq "get_genome_align" )
489 elsif ( $script eq "get_genome_phastcons" )
500 elsif ( $script eq "split_seq" )
507 elsif ( $script eq "split_bed" )
514 elsif ( $script eq "tile_seq" )
521 elsif ( $script eq "invert_align" )
527 elsif ( $script eq "patscan_seq" )
538 elsif ( $script eq "create_blast_db" )
545 elsif ( $script eq "blast_seq" )
557 elsif ( $script eq "blat_seq" )
569 elsif ( $script eq "match_seq" )
576 elsif ( $script eq "create_vmatch_index" )
584 elsif ( $script eq "create_fixedstep_index" )
591 elsif ( $script eq "vmatch_seq" )
602 elsif ( $script eq "write_fasta" )
611 elsif ( $script eq "write_align" )
621 elsif ( $script eq "write_blast" )
630 elsif ( $script eq "write_tab" )
642 elsif ( $script eq "write_bed" )
650 elsif ( $script eq "write_psl" )
658 elsif ( $script eq "write_fixedstep" )
666 elsif ( $script eq "write_2bit" )
674 elsif ( $script eq "write_solid" )
683 elsif ( $script eq "plot_seqlogo" )
690 elsif ( $script eq "plot_phastcons_profiles" )
705 elsif ( $script eq "analyze_vals" )
712 elsif ( $script eq "head_records" )
718 elsif ( $script eq "remove_keys" )
725 elsif ( $script eq "rename_keys" )
731 elsif ( $script eq "uniq_vals" )
738 elsif ( $script eq "merge_vals" )
745 elsif ( $script eq "grab" )
760 elsif ( $script eq "compute" )
766 elsif ( $script eq "add_ident" )
773 elsif ( $script eq "count_records" )
780 elsif ( $script eq "random_records" )
786 elsif ( $script eq "sort_records" )
793 elsif ( $script eq "count_vals" )
799 elsif ( $script eq "plot_histogram" )
812 elsif ( $script eq "plot_lendist" )
824 elsif ( $script eq "plot_chrdist" )
835 elsif ( $script eq "plot_karyogram" )
844 elsif ( $script eq "plot_matches" )
856 elsif ( $script eq "length_vals" )
862 elsif ( $script eq "sum_vals" )
870 elsif ( $script eq "mean_vals" )
878 elsif ( $script eq "median_vals" )
886 elsif ( $script eq "max_vals" )
894 elsif ( $script eq "min_vals" )
902 elsif ( $script eq "upload_to_ucsc" )
927 # print STDERR Dumper( \@options );
934 $options{ "script" } = $script;
936 # print STDERR Dumper( \%options );
938 if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
939 return wantarray ? %options : \%options;
942 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
943 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
944 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
945 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
946 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
947 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
948 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
949 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
951 # ---- check arguments ----
953 if ( $options{ 'data_in' } )
955 $options{ "files" } = getopt_files( $options{ 'data_in' } );
957 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
960 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
962 # print STDERR Dumper( \%options );
964 foreach $opt ( keys %options )
966 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
968 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
970 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+$/ )
972 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
974 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
976 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
978 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
980 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
982 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
984 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
986 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
988 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
990 elsif ( $opt eq "genome" and $script ne "format_genome" )
992 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
993 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
995 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
996 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
999 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1001 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1003 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1005 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1009 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1010 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index|create_fixedstep_index/ and not $options{ "index_name" };
1011 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1012 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1013 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1014 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
1015 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" };
1016 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1017 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" };
1019 if ( $script eq "upload_to_ucsc" )
1021 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1022 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1025 return wantarray ? %options : \%options;
1029 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1032 sub script_print_usage
1034 # Martin A. Hansen, January 2008.
1036 # Retrieves usage information from file and
1037 # prints this nicely formatted.
1039 my ( $in, # handle to in stream
1040 $out, # handle to out stream
1041 $options, # options hash
1046 my ( $file, $wiki, $lines );
1048 if ( $options->{ 'data_in' } ) {
1049 $file = $options->{ 'data_in' };
1051 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1054 $wiki = Maasha::Gwiki::gwiki_read( $file );
1056 if ( not $options->{ "help" } ) {
1057 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1060 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1062 print STDERR "$_\n" foreach @{ $lines };
1068 sub script_list_biopieces
1070 # Martin A. Hansen, January 2008.
1072 # Prints the synopsis from the usage for each of the biopieces.
1074 my ( $in, # handle to in stream
1075 $out, # handle to out stream
1076 $options, # options hash
1081 my ( @files, $file, $wiki, $program, $synopsis );
1083 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1085 foreach $file ( sort @files )
1087 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1091 $wiki = Maasha::Gwiki::gwiki_read( $file );
1093 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1094 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1096 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1098 printf( "%-30s%s\n", $program, $synopsis );
1106 sub script_list_genomes
1108 # Martin A. Hansen, January 2008.
1110 # Prints the synopsis from the usage for each of the biopieces.
1112 my ( $in, # handle to in stream
1113 $out, # handle to out stream
1114 $options, # options hash
1119 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1121 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1123 foreach $genome ( @genomes )
1125 next if $genome =~ /\.$/;
1127 @formats = Maasha::Common::ls_dirs( $genome );
1129 foreach $format ( @formats )
1131 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1133 $hash{ $1 }{ $2 } = 1;
1142 map { push @row, $_ } sort keys %found;
1144 print join( "\t", @row ), "\n";
1146 foreach $genome ( sort keys %hash )
1150 foreach $format ( sort keys %found )
1152 if ( exists $hash{ $genome }{ $format } ) {
1159 print join( "\t", @row ), "\n";
1164 sub script_read_fasta
1166 # Martin A. Hansen, August 2007.
1168 # Read sequences from FASTA file.
1170 my ( $in, # handle to in stream
1171 $out, # handle to out stream
1172 $options, # options hash
1177 my ( $record, $file, $data_in, $entry, $num );
1179 while ( $record = get_record( $in ) ) {
1180 put_record( $record, $out );
1185 foreach $file ( @{ $options->{ "files" } } )
1187 $data_in = Maasha::Common::read_open( $file );
1189 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1191 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1194 SEQ_NAME => $entry->[ SEQ_NAME ],
1195 SEQ => $entry->[ SEQ ],
1196 SEQ_LEN => length $entry->[ SEQ ],
1199 put_record( $record, $out );
1202 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1212 close $data_in if $data_in;
1218 # Martin A. Hansen, August 2007.
1220 # Read table or table columns from stream or file.
1222 my ( $in, # handle to in stream
1223 $out, # handle to out stream
1224 $options, # options hash
1229 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1231 $options->{ 'delimit' } ||= '\s+';
1233 while ( $record = get_record( $in ) ) {
1234 put_record( $record, $out );
1237 $skip = $options->{ 'skip' } ||= 0;
1240 foreach $file ( @{ $options->{ "files" } } )
1242 $data_in = Maasha::Common::read_open( $file );
1244 while ( $line = <$data_in> )
1252 next if $line =~ /^#|^$/;
1259 @fields = split /$options->{'delimit'}/, $line;
1261 if ( $options->{ "cols" } ) {
1262 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1267 for ( $i = 0; $i < @fields2; $i++ )
1269 if ( $options->{ "keys" }->[ $i ] ) {
1270 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1272 $record->{ "V" . $i } = $fields2[ $i ];
1276 put_record( $record, $out );
1278 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1288 close $data_in if $data_in;
1294 # Martin A. Hansen, August 2007.
1296 # Read psl table from stream or file.
1298 my ( $in, # handle to in stream
1299 $out, # handle to out stream
1300 $options, # options hash
1305 my ( $record, @files, $file, $entries, $entry, $num );
1307 while ( $record = get_record( $in ) ) {
1308 put_record( $record, $out );
1313 foreach $file ( @{ $options->{ "files" } } )
1315 $entries = Maasha::UCSC::psl_get_entries( $file );
1317 foreach $entry ( @{ $entries } )
1319 put_record( $entry, $out );
1321 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1333 # Martin A. Hansen, August 2007.
1335 # Read bed table from stream or file.
1337 my ( $in, # handle to in stream
1338 $out, # handle to out stream
1339 $options, # options hash
1344 my ( $file, $record, $entry, $data_in, $num );
1346 while ( $record = get_record( $in ) ) {
1347 put_record( $record, $out );
1352 foreach $file ( @{ $options->{ "files" } } )
1354 $data_in = Maasha::Common::read_open( $file );
1356 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1358 put_record( $entry, $out );
1360 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1370 close $data_in if $data_in;
1374 sub script_read_fixedstep
1376 # Martin A. Hansen, Juli 2008.
1378 # Read fixedStep wiggle format from stream or file.
1380 my ( $in, # handle to in stream
1381 $out, # handle to out stream
1382 $options, # options hash
1387 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1389 while ( $record = get_record( $in ) ) {
1390 put_record( $record, $out );
1395 foreach $file ( @{ $options->{ "files" } } )
1397 $data_in = Maasha::Common::read_open( $file );
1399 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1401 $head = shift @{ $entry };
1403 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1405 $record->{ "CHR" } = $1;
1406 $record->{ "CHR_BEG" } = $2;
1407 $record->{ "STEP" } = $3;
1408 $record->{ "VALS" } = join ",", @{ $entry };
1411 put_record( $record, $out );
1413 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1423 close $data_in if $data_in;
1427 sub script_read_blast_tab
1429 # Martin A. Hansen, September 2007.
1431 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1433 my ( $in, # handle to in stream
1434 $out, # handle to out stream
1435 $options, # options hash
1440 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1442 while ( $record = get_record( $in ) ) {
1443 put_record( $record, $out );
1448 foreach $file ( @{ $options->{ "files" } } )
1450 $data_in = Maasha::Common::read_open( $file );
1452 while ( $line = <$data_in> )
1456 next if $line =~ /^#/;
1458 @fields = split /\t/, $line;
1460 $record->{ "REC_TYPE" } = "BLAST";
1461 $record->{ "Q_ID" } = $fields[ 0 ];
1462 $record->{ "S_ID" } = $fields[ 1 ];
1463 $record->{ "IDENT" } = $fields[ 2 ];
1464 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1465 $record->{ "MISMATCHES" } = $fields[ 4 ];
1466 $record->{ "GAPS" } = $fields[ 5 ];
1467 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1468 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1469 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1470 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1471 $record->{ "E_VAL" } = $fields[ 10 ];
1472 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1474 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1476 $record->{ "STRAND" } = '-';
1478 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1482 $record->{ "STRAND" } = '+';
1485 put_record( $record, $out );
1487 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1497 close $data_in if $data_in;
1501 sub script_read_embl
1503 # Martin A. Hansen, August 2007.
1507 my ( $in, # handle to in stream
1508 $out, # handle to out stream
1509 $options, # options hash
1514 my ( %options2, $file, $data_in, $num, $entry, $record );
1516 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1517 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1518 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1520 while ( $record = get_record( $in ) ) {
1521 put_record( $record, $out );
1526 foreach $file ( @{ $options->{ "files" } } )
1528 $data_in = Maasha::Common::read_open( $file );
1530 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1532 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1534 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1536 $record_copy = dclone $record;
1538 delete $record_copy->{ "FT" };
1540 put_record( $record_copy, $out );
1542 delete $record_copy->{ "SEQ" };
1544 foreach $feat ( keys %{ $record->{ "FT" } } )
1546 $record_copy->{ "FEAT_TYPE" } = $feat;
1548 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1550 foreach $qual ( keys %{ $feat2 } )
1552 $qual_val = join "; ", @{ $feat2->{ $qual } };
1557 $record_copy->{ $qual } = $qual_val;
1560 put_record( $record_copy, $out );
1564 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1574 close $data_in if $data_in;
1578 sub script_read_stockholm
1580 # Martin A. Hansen, August 2007.
1582 # Read Stockholm format.
1584 my ( $in, # handle to in stream
1585 $out, # handle to out stream
1586 $options, # options hash
1591 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1593 while ( $record = get_record( $in ) ) {
1594 put_record( $record, $out );
1599 foreach $file ( @{ $options->{ "files" } } )
1601 $data_in = Maasha::Common::read_open( $file );
1603 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1605 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1609 foreach $key ( keys %{ $record->{ "GF" } } ) {
1610 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1613 $record_anno->{ "ALIGN" } = $num;
1615 put_record( $record_anno, $out );
1617 foreach $seq ( @{ $record->{ "ALIGN" } } )
1619 undef $record_align;
1622 SEQ_NAME => $seq->[ 0 ],
1626 put_record( $record_align, $out );
1629 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1639 close $data_in if $data_in;
1643 sub script_read_phastcons
1645 # Martin A. Hansen, December 2007.
1647 # Read PhastCons format.
1649 my ( $in, # handle to in stream
1650 $out, # handle to out stream
1651 $options, # options hash
1656 my ( $data_in, $file, $num, $entry, @records, $record );
1658 $options->{ "min" } ||= 10;
1659 $options->{ "dist" } ||= 25;
1660 $options->{ "threshold" } ||= 0.8;
1661 $options->{ "gap" } ||= 5;
1663 while ( $record = get_record( $in ) ) {
1664 put_record( $record, $out );
1669 foreach $file ( @{ $options->{ "files" } } )
1671 $data_in = Maasha::Common::read_open( $file );
1673 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1675 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1677 foreach $record ( @records )
1679 $record->{ "REC_TYPE" } = "BED";
1680 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1682 put_record( $record, $out );
1684 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1695 close $data_in if $data_in;
1699 sub script_read_soft
1701 # Martin A. Hansen, December 2007.
1704 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1706 my ( $in, # handle to in stream
1707 $out, # handle to out stream
1708 $options, # options hash
1713 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1715 while ( $record = get_record( $in ) ) {
1716 put_record( $record, $out );
1721 foreach $file ( @{ $options->{ "files" } } )
1723 $soft_index = Maasha::NCBI::soft_index_file( $file );
1725 $fh = Maasha::Common::read_open( $file );
1727 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1729 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1731 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1733 $old_end = $platforms[ -1 ]->[ 2 ];
1735 foreach $sample ( @samples )
1737 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1739 foreach $record ( @{ $records } )
1741 put_record( $record, $out );
1743 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1748 $old_end = $sample->[ 2 ];
1756 close $data_in if $data_in;
1763 # Martin A. Hansen, February 2008.
1766 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1768 my ( $in, # handle to in stream
1769 $out, # handle to out stream
1770 $options, # options hash
1775 my ( $data_in, $file, $fh, $num, $record, $entry );
1777 while ( $record = get_record( $in ) ) {
1778 put_record( $record, $out );
1783 foreach $file ( @{ $options->{ "files" } } )
1785 $fh = Maasha::Common::read_open( $file );
1787 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1789 put_record( $entry, $out );
1791 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1801 close $data_in if $data_in;
1805 sub script_read_2bit
1807 # Martin A. Hansen, March 2008.
1809 # Read sequences from 2bit file.
1811 my ( $in, # handle to in stream
1812 $out, # handle to out stream
1813 $options, # options hash
1818 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1820 $mask = 1 if not $options->{ "no_mask" };
1822 while ( $record = get_record( $in ) ) {
1823 put_record( $record, $out );
1828 foreach $file ( @{ $options->{ "files" } } )
1830 $data_in = Maasha::Common::read_open( $file );
1832 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1834 foreach $line ( @{ $toc } )
1836 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1837 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1838 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1840 put_record( $record, $out );
1842 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1852 close $data_in if $data_in;
1856 sub script_read_solexa
1858 # Martin A. Hansen, March 2008.
1860 # Read Solexa sequence reads from file.
1862 my ( $in, # handle to in stream
1863 $out, # handle to out stream
1864 $options, # options hash
1869 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1871 $options->{ "quality" } ||= 20;
1873 while ( $record = get_record( $in ) ) {
1874 put_record( $record, $out );
1879 foreach $file ( @{ $options->{ "files" } } )
1881 $data_in = Maasha::Common::read_open( $file );
1882 $base_name = Maasha::Common::get_basename( $file );
1883 $base_name =~ s/\..*//;
1887 while ( $line = <$data_in> )
1889 @fields = split /:/, $line;
1890 @seqs = split //, $fields[ 5 ];
1891 @scores = split / /, $fields[ -1 ];
1893 for ( $i = 0; $i < @scores; $i++ ) {
1894 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1897 $seq = join "", @seqs;
1899 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1900 $record->{ "SEQ" } = $seq;
1901 $record->{ "SEQ_LEN" } = length $seq;
1902 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1904 put_record( $record, $out );
1906 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1917 close $data_in if $data_in;
1921 sub script_read_solid
1923 # Martin A. Hansen, April 2008.
1925 # Read Solid sequence from file.
1927 my ( $in, # handle to in stream
1928 $out, # handle to out stream
1929 $options, # options hash
1934 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1936 $options->{ "quality" } ||= 15;
1938 while ( $record = get_record( $in ) ) {
1939 put_record( $record, $out );
1944 foreach $file ( @{ $options->{ "files" } } )
1946 $data_in = Maasha::Common::read_open( $file );
1948 while ( $line = <$data_in> )
1952 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1954 @scores = split /,/, $seq_qual;
1955 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1957 for ( $i = 0; $i < @seqs; $i++ ) {
1958 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1962 SEQ_NAME => $seq_name,
1964 SEQ_QUAL => $seq_qual,
1965 SEQ_LEN => length $seq_cs,
1966 SEQ => join( "", @seqs ),
1967 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1970 put_record( $record, $out );
1972 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1982 close $data_in if $data_in;
1986 sub script_read_mysql
1988 # Martin A. Hansen, May 2008.
1990 # Read a MySQL query into stream.
1992 my ( $in, # handle to in stream
1993 $out, # handle to out stream
1994 $options, # options hash
1999 my ( $record, $dbh, $results );
2001 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2002 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2004 while ( $record = get_record( $in ) ) {
2005 put_record( $record, $out );
2008 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2010 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2012 Maasha::SQL::disconnect( $dbh );
2014 map { put_record( $_ ) } @{ $results };
2018 sub script_format_genome
2020 # Martin A. Hansen, Juli 2008.
2022 # Format a genome to speficed formats.
2024 my ( $in, # handle to in stream
2025 $out, # handle to out stream
2026 $options, # options hash
2031 my ( $dir, $genome, $fasta_dir, $fh_out, $record, $format, $index );
2033 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2034 $genome = $options->{ 'genome' };
2036 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2037 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2038 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2040 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2042 $fasta_dir = "$dir/genomes/$genome/fasta";
2044 elsif ( grep { $_ =~ /fasta/i } @{ $options->{ "formats" } } )
2046 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2048 $fasta_dir = "$dir/genomes/$genome/fasta";
2050 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2054 $fasta_dir = $BP_TMP;
2056 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2059 while ( $record = get_record( $in ) )
2061 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2062 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
2065 put_record( $record, $out ) if not $options->{ "no_stream" };
2068 foreach $format ( @{ $options->{ 'formats' } } )
2070 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2071 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2072 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2073 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2076 close $fh_out if $fh_out;
2080 sub script_length_seq
2082 # Martin A. Hansen, August 2007.
2084 # Determine the length of sequences in stream.
2086 my ( $in, # handle to in stream
2087 $out, # handle to out stream
2088 $options, # options hash
2093 my ( $record, $total );
2095 while ( $record = get_record( $in ) )
2097 if ( $record->{ "SEQ" } )
2099 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2100 $total += $record->{ "SEQ_LEN" };
2103 put_record( $record, $out ) if not $options->{ "no_stream" };
2106 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2110 sub script_uppercase_seq
2112 # Martin A. Hansen, August 2007.
2114 # Uppercases sequences in stream.
2116 my ( $in, # handle to in stream
2117 $out, # handle to out stream
2124 while ( $record = get_record( $in ) )
2126 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2128 put_record( $record, $out );
2133 sub script_shuffle_seq
2135 # Martin A. Hansen, December 2007.
2137 # Shuffle sequences in stream.
2139 my ( $in, # handle to in stream
2140 $out, # handle to out stream
2147 while ( $record = get_record( $in ) )
2149 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2151 put_record( $record, $out );
2156 sub script_analyze_seq
2158 # Martin A. Hansen, August 2007.
2160 # Analyze sequence composition of sequences in stream.
2162 my ( $in, # handle to in stream
2163 $out, # handle to out stream
2168 my ( $record, $analysis );
2170 while ( $record = get_record( $in ) )
2172 if ( $record->{ "SEQ" } )
2174 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2176 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2179 put_record( $record, $out );
2184 sub script_analyze_tags
2186 # Martin A. Hansen, August 2008.
2188 # Analyze sequence tags in stream.
2190 my ( $in, # handle to in stream
2191 $out, # handle to out stream
2196 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2198 while ( $record = get_record( $in ) )
2200 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2202 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2206 $len_hash{ length( $record->{ "SEQ" } ) }++;
2207 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2210 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2212 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2216 $len_hash{ $record->{ "BED_LEN" } }++;
2217 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2222 foreach $key ( sort { $a <=> $b } keys %len_hash )
2224 $tag_record->{ "TAG_LEN" } = $key;
2225 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2226 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2228 put_record( $tag_record, $out );
2233 sub script_complexity_seq
2235 # Martin A. Hansen, May 2008.
2237 # Generates an index calculated as the most common di-residue over
2238 # the sequence length for all sequences in stream.
2240 my ( $in, # handle to in stream
2241 $out, # handle to out stream
2246 my ( $record, $index );
2248 while ( $record = get_record( $in ) )
2250 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2252 put_record( $record, $out );
2257 sub script_oligo_freq
2259 # Martin A. Hansen, August 2007.
2261 # Determine the length of sequences in stream.
2263 my ( $in, # handle to in stream
2264 $out, # handle to out stream
2265 $options, # options hash
2270 my ( $record, %oligos, @freq_table );
2272 $options->{ "word_size" } ||= 7;
2274 while ( $record = get_record( $in ) )
2276 if ( $record->{ "SEQ" } )
2278 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2280 if ( not $options->{ "all" } )
2282 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2284 map { put_record( $_, $out ) } @freq_table;
2290 put_record( $record, $out );
2293 if ( $options->{ "all" } )
2295 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2297 map { put_record( $_, $out ) } @freq_table;
2302 sub script_create_weight_matrix
2304 # Martin A. Hansen, August 2007.
2306 # Creates a weight matrix from an alignmnet.
2308 my ( $in, # handle to in stream
2309 $out, # handle to out stream
2310 $options, # options hash
2315 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2319 while ( $record = get_record( $in ) )
2321 if ( $record->{ "SEQ" } )
2323 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2325 $res = substr $record->{ "SEQ" }, $i, 1;
2327 $freq_hash{ $i }{ $res }++;
2328 $res_hash{ $res } = 1;
2335 put_record( $record, $out );
2339 foreach $res ( sort keys %res_hash )
2343 $record->{ "V0" } = $res;
2345 for ( $i = 0; $i < keys %freq_hash; $i++ )
2347 $freq = $freq_hash{ $i }{ $res } || 0;
2349 if ( $options->{ "percent" } ) {
2350 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2353 $record->{ "V" . ( $i + 1 ) } = $freq;
2356 put_record( $record, $out );
2361 sub script_calc_bit_scores
2363 # Martin A. Hansen, March 2007.
2365 # Calculates the bit scores for each position from an alignmnet in the stream.
2367 my ( $in, # handle to in stream
2368 $out, # handle to out stream
2373 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2377 while ( $record = get_record( $in ) )
2379 if ( $record->{ "SEQ" } )
2381 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2383 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2385 $res = substr $record->{ "SEQ" }, $i, 1;
2387 next if $res =~ /-|_|~|\./;
2389 $freq_hash{ $i }{ $res }++;
2396 put_record( $record, $out );
2402 if ( $type eq "protein" ) {
2408 for ( $i = 0; $i < keys %freq_hash; $i++ )
2410 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2412 $bit_diff = $bit_max - $bit_height;
2414 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2417 put_record( $record, $out );
2421 sub script_reverse_seq
2423 # Martin A. Hansen, August 2007.
2425 # Reverse sequence in record.
2427 my ( $in, # handle to in stream
2428 $out, # handle to out stream
2435 while ( $record = get_record( $in ) )
2437 if ( $record->{ "SEQ" } ) {
2438 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2441 put_record( $record, $out );
2446 sub script_complement_seq
2448 # Martin A. Hansen, August 2007.
2450 # Complement sequence in record.
2452 my ( $in, # handle to in stream
2453 $out, # handle to out stream
2458 my ( $record, $type );
2460 while ( $record = get_record( $in ) )
2462 if ( $record->{ "SEQ" } )
2465 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2468 if ( $type eq "rna" ) {
2469 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2470 } elsif ( $type eq "dna" ) {
2471 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2475 put_record( $record, $out );
2480 sub script_remove_indels
2482 # Martin A. Hansen, August 2007.
2484 # Remove indels from sequences in stream.
2486 my ( $in, # handle to in stream
2487 $out, # handle to out stream
2494 while ( $record = get_record( $in ) )
2496 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2498 put_record( $record, $out );
2503 sub script_transliterate_seq
2505 # Martin A. Hansen, August 2007.
2507 # Transliterate chars from sequence in record.
2509 my ( $in, # handle to in stream
2510 $out, # handle to out stream
2511 $options, # options hash
2516 my ( $record, $search, $replace, $delete );
2518 $search = $options->{ "search" } || "";
2519 $replace = $options->{ "replace" } || "";
2520 $delete = $options->{ "delete" } || "";
2522 while ( $record = get_record( $in ) )
2524 if ( $record->{ "SEQ" } )
2526 if ( $search and $replace ) {
2527 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2528 } elsif ( $delete ) {
2529 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2533 put_record( $record, $out );
2538 sub script_transliterate_vals
2540 # Martin A. Hansen, April 2008.
2542 # Transliterate chars from values in record.
2544 my ( $in, # handle to in stream
2545 $out, # handle to out stream
2546 $options, # options hash
2551 my ( $record, $search, $replace, $delete, $key );
2553 $search = $options->{ "search" } || "";
2554 $replace = $options->{ "replace" } || "";
2555 $delete = $options->{ "delete" } || "";
2557 while ( $record = get_record( $in ) )
2559 foreach $key ( @{ $options->{ "keys" } } )
2561 if ( exists $record->{ $key } )
2563 if ( $search and $replace ) {
2564 eval "\$record->{ $key } =~ tr/$search/$replace/";
2565 } elsif ( $delete ) {
2566 eval "\$record->{ $key } =~ tr/$delete//d";
2571 put_record( $record, $out );
2576 sub script_translate_seq
2578 # Martin A. Hansen, February 2008.
2580 # Translate DNA sequence into protein sequence.
2582 my ( $in, # handle to in stream
2583 $out, # handle to out stream
2584 $options, # options hash
2589 my ( $record, $frame, %new_record );
2591 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2593 while ( $record = get_record( $in ) )
2595 if ( $record->{ "SEQ" } )
2597 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2599 foreach $frame ( @{ $options->{ "frames" } } )
2601 %new_record = %{ $record };
2603 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2604 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2605 $new_record{ "FRAME" } = $frame;
2607 put_record( \%new_record, $out );
2613 put_record( $record, $out );
2619 sub script_extract_seq
2621 # Martin A. Hansen, August 2007.
2623 # Extract subsequences from sequences in record.
2625 my ( $in, # handle to in stream
2626 $out, # handle to out stream
2627 $options, # options hash
2632 my ( $beg, $end, $len, $record );
2634 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2637 $beg = $options->{ "beg" } - 1; # correcting for start offset
2640 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2642 } elsif ( defined $options->{ "end" } ) {
2643 $end = $options->{ "end" } - 1; # correcting for start offset
2646 $len = $options->{ "len" };
2648 # print "beg->$beg, end->$end, len->$len\n";
2650 while ( $record = get_record( $in ) )
2652 if ( $record->{ "SEQ" } )
2654 if ( defined $beg and defined $end )
2656 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2657 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2659 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2662 elsif ( defined $beg and defined $len )
2664 if ( $len > length $record->{ "SEQ" } ) {
2665 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2667 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2670 elsif ( defined $beg )
2672 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2676 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2678 put_record( $record, $out );
2683 sub script_get_genome_seq
2685 # Martin A. Hansen, December 2007.
2687 # Gets a subsequence from a genome.
2689 my ( $in, # handle to in stream
2690 $out, # handle to out stream
2691 $options, # options hash
2696 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2698 $options->{ "flank" } ||= 0;
2700 if ( $options->{ "genome" } )
2702 $genome = $options->{ "genome" };
2704 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2705 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2707 $fh = Maasha::Common::read_open( $genome_file );
2708 $index = Maasha::Fasta::index_retrieve( $index_file );
2710 shift @{ $index }; # Get rid of the file size info
2712 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2714 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2716 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2718 $beg = $index_beg + $options->{ "beg" } - 1;
2720 if ( $options->{ "len" } ) {
2721 $len = $options->{ "len" };
2722 } elsif ( $options->{ "end" } ) {
2723 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2726 $beg -= $options->{ "flank" };
2727 $len += 2 * $options->{ "flank" };
2729 if ( $beg <= $index_beg )
2731 $len -= $index_beg - $beg;
2735 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2737 next if $beg > $index_beg + $index_len;
2739 $record->{ "CHR" } = $options->{ "chr" };
2740 $record->{ "CHR_BEG" } = $beg - $index_beg;
2741 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2743 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2744 $record->{ "SEQ_LEN" } = $len;
2746 put_record( $record, $out );
2750 while ( $record = get_record( $in ) )
2752 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2754 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2756 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2758 $beg = $record->{ "CHR_BEG" } + $index_beg;
2759 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2761 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2763 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2765 $beg = $record->{ "S_BEG" } + $index_beg;
2766 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2768 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2770 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2772 $beg = $record->{ "S_BEG" } + $index_beg;
2773 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2776 $beg -= $options->{ "flank" };
2777 $len += 2 * $options->{ "flank" };
2779 if ( $beg <= $index_beg )
2781 $len -= $index_beg - $beg;
2785 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2787 next if $beg > $index_beg + $index_len;
2789 $record->{ "CHR_BEG" } = $beg - $index_beg;
2790 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2792 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2794 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2796 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2797 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2800 if ( $options->{ "mask" } )
2802 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2804 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2806 @begs = split ",", $record->{ "Q_BEGS" };
2807 @lens = split ",", $record->{ "BLOCKSIZES" };
2809 for ( $i = 0; $i < @begs; $i++ ) {
2810 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2816 put_record( $record, $out );
2823 sub script_get_genome_align
2825 # Martin A. Hansen, April 2008.
2827 # Gets a subalignment from a multiple genome alignment.
2829 my ( $in, # handle to in stream
2830 $out, # handle to out stream
2831 $options, # options hash
2836 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2838 $options->{ "strand" } ||= "+";
2842 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2844 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2846 $beg = $options->{ "beg" } - 1;
2848 if ( $options->{ "end" } ) {
2849 $end = $options->{ "end" };
2850 } elsif ( $options->{ "len" } ) {
2851 $end = $beg + $options->{ "len" };
2854 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2856 foreach $entry ( @{ $align } )
2858 $entry->{ "CHR" } = $record->{ "CHR" };
2859 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2860 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2861 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2862 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2863 $entry->{ "SCORE" } = $record->{ "SCORE" };
2865 put_record( $entry, $out );
2869 while ( $record = get_record( $in ) )
2871 if ( $record->{ "REC_TYPE" } eq "BED" )
2873 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2875 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2877 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2879 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2881 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2884 foreach $entry ( @{ $align } )
2886 $entry->{ "CHR" } = $record->{ "CHR" };
2887 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2888 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2889 $entry->{ "STRAND" } = $record->{ "STRAND" };
2890 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2891 $entry->{ "SCORE" } = $record->{ "SCORE" };
2893 put_record( $entry, $out );
2901 sub script_get_genome_phastcons
2903 # Martin A. Hansen, February 2008.
2905 # Get phastcons scores from genome intervals.
2907 my ( $in, # handle to in stream
2908 $out, # handle to out stream
2909 $options, # options hash
2914 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2916 $options->{ "flank" } ||= 0;
2918 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2919 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2921 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2922 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2924 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2926 $options->{ "beg" } -= 1; # request is 1-based
2927 $options->{ "end" } -= 1; # request is 1-based
2929 if ( $options->{ "len" } ) {
2930 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2933 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2935 $record->{ "CHR" } = $options->{ "chr" };
2936 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2937 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2939 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2940 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2942 put_record( $record, $out );
2945 while ( $record = get_record( $in ) )
2947 if ( $record->{ "REC_TYPE" } eq "BED" )
2949 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2951 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2953 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2955 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2957 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2960 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2961 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2963 put_record( $record, $out );
2966 close $fh_phastcons if $fh_phastcons;
2972 # Martin A. Hansen, December 2007.
2974 # Folds sequences in stream into secondary structures.
2976 my ( $in, # handle to in stream
2977 $out, # handle to out stream
2982 my ( $record, $type, $struct, $index );
2984 while ( $record = get_record( $in ) )
2986 if ( $record->{ "SEQ" } )
2989 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2992 if ( $type ne "protein" )
2994 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2995 $record->{ "SEC_STRUCT" } = $struct;
2996 $record->{ "FREE_ENERGY" } = $index;
2997 $record->{ "SCORE" } = abs int $index;
2998 $record->{ "SIZE" } = length $struct;
2999 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3003 put_record( $record, $out );
3008 sub script_split_seq
3010 # Martin A. Hansen, August 2007.
3012 # Split a sequence in stream into words.
3014 my ( $in, # handle to in stream
3015 $out, # handle to out stream
3016 $options, # options hash
3021 my ( $record, $new_record, $i, $subseq, %lookup );
3023 $options->{ "word_size" } ||= 7;
3025 while ( $record = get_record( $in ) )
3027 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3029 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3031 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3033 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3035 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3036 $new_record->{ "SEQ" } = $subseq;
3038 put_record( $new_record, $out );
3040 $lookup{ $subseq } = 1;
3044 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3045 $new_record->{ "SEQ" } = $subseq;
3047 put_record( $new_record, $out );
3053 put_record( $record, $out );
3059 sub script_split_bed
3061 # Martin A. Hansen, June 2008.
3063 # Split a BED record into overlapping windows.
3065 my ( $in, # handle to in stream
3066 $out, # handle to out stream
3067 $options, # options hash
3072 my ( $record, $new_record, $i );
3074 $options->{ "window_size" } ||= 20;
3075 $options->{ "step_size" } ||= 1;
3077 while ( $record = get_record( $in ) )
3079 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3081 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3083 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3085 $new_record->{ "REC_TYPE" } = "BED";
3086 $new_record->{ "CHR" } = $record->{ "CHR" };
3087 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3088 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3089 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3090 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3091 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3092 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3094 put_record( $new_record, $out );
3099 put_record( $record, $out );
3105 sub script_align_seq
3107 # Martin A. Hansen, August 2007.
3109 # Align sequences in stream.
3111 my ( $in, # handle to in stream
3112 $out, # handle to out stream
3117 my ( $record, @entries, $entry );
3119 while ( $record = get_record( $in ) )
3121 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3122 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3123 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3124 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3126 put_record( $record, $out );
3130 @entries = Maasha::Align::align( \@entries );
3132 foreach $entry ( @entries )
3134 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3137 SEQ_NAME => $entry->[ SEQ_NAME ],
3138 SEQ => $entry->[ SEQ ],
3141 put_record( $record, $out );
3149 # Martin A. Hansen, February 2008.
3151 # Using the first sequence in stream as reference, tile
3152 # all subsequent sequences based on pairwise alignments.
3154 my ( $in, # handle to in stream
3155 $out, # handle to out stream
3156 $options, # options hash
3161 my ( $record, $first, $ref_entry, @entries );
3165 while ( $record = get_record( $in ) )
3167 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3171 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3177 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3182 put_record( $record, $out );
3186 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3188 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3192 sub script_invert_align
3194 # Martin A. Hansen, February 2008.
3196 # Inverts an alignment showing only non-mathing residues
3197 # using the first sequence as reference.
3199 my ( $in, # handle to in stream
3200 $out, # handle to out stream
3201 $options, # options hash
3206 my ( $record, @entries );
3208 while ( $record = get_record( $in ) )
3210 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3212 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3216 put_record( $record, $out );
3220 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3222 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3226 sub script_patscan_seq
3228 # Martin A. Hansen, August 2007.
3230 # Locates patterns in sequences using scan_for_matches.
3232 my ( $in, # handle to in stream
3233 $out, # handle to out stream
3234 $options, # options hash
3239 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3241 if ( $options->{ "patterns" } ) {
3242 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3243 } elsif ( -f $options->{ "patterns_in" } ) {
3244 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3247 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3249 push @args, "-c" if $options->{ "comp" };
3250 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3251 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3253 $seq_file = "$BP_TMP/patscan.seq";
3254 $pat_file = "$BP_TMP/patscan.pat";
3255 $out_file = "$BP_TMP/patscan.out";
3257 $fh_out = Maasha::Common::write_open( $seq_file );
3261 while ( $record = get_record( $in ) )
3263 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3265 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3267 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3269 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3274 # put_record( $record, $out );
3279 $arg = join " ", @args;
3280 $arg .= " -p" if $type eq "protein";
3282 foreach $pattern ( @{ $patterns } )
3284 $fh_out = Maasha::Common::write_open( $pat_file );
3286 print $fh_out "$pattern\n";
3290 if ( $options->{ 'genome' } ) {
3291 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3292 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3294 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3295 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3298 $fh_in = Maasha::Common::read_open( $out_file );
3300 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3302 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3304 if ( $options->{ 'genome' } )
3306 $result->{ "CHR" } = $result->{ "S_ID" };
3307 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3308 $result->{ "CHR_END" } = $result->{ "S_END" };
3310 delete $result->{ "S_ID" };
3311 delete $result->{ "S_BEG" };
3312 delete $result->{ "S_END" };
3316 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3319 put_record( $result, $out );
3331 sub script_create_blast_db
3333 # Martin A. Hansen, September 2007.
3335 # Creates a NCBI BLAST database with formatdb
3337 my ( $in, # handle to in stream
3338 $out, # handle to out stream
3339 $options, # options hash
3344 my ( $fh, $seq_type, $path, $record );
3346 $path = $options->{ "database" };
3348 $fh = Maasha::Common::write_open( $path );
3350 while ( $record = get_record( $in ) )
3352 put_record( $record, $out ) if not $options->{ "no_stream" };
3354 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3356 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3358 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3364 if ( $seq_type eq "protein" ) {
3365 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3367 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3374 sub script_blast_seq
3376 # Martin A. Hansen, September 2007.
3378 # BLASTs sequences in stream against a given database.
3380 my ( $in, # handle to in stream
3381 $out, # handle to out stream
3382 $options, # options hash
3387 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3389 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3390 $options->{ "filter" } = "F";
3391 $options->{ "filter" } = "T" if $options->{ "filter" };
3392 $options->{ "cpus" } ||= 1;
3394 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna"if $options->{ 'genome' };
3396 $tmp_in = "$BP_TMP/blast_query.seq";
3397 $tmp_out = "$BP_TMP/blast.result";
3399 $fh_out = Maasha::Common::write_open( $tmp_in );
3401 while ( $record = get_record( $in ) )
3403 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3405 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3407 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3410 put_record( $record, $out );
3415 if ( -f $options->{ 'database' } . ".phr" ) {
3416 $s_type = "protein";
3418 $s_type = "nucleotide";
3421 if ( not $options->{ 'program' } )
3423 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3424 $options->{ 'program' } = "blastn";
3425 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3426 $options->{ 'program' } = "blastp";
3427 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3428 $options->{ 'program' } = "blastx";
3429 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3430 $options->{ 'program' } = "tblastn";
3434 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 );
3438 $fh_out = Maasha::Common::read_open( $tmp_out );
3442 while ( $line = <$fh_out> )
3446 next if $line =~ /^#/;
3448 @fields = split /\s+/, $line;
3450 $record->{ "REC_TYPE" } = "BLAST";
3451 $record->{ "Q_ID" } = $fields[ 0 ];
3452 $record->{ "S_ID" } = $fields[ 1 ];
3453 $record->{ "IDENT" } = $fields[ 2 ];
3454 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3455 $record->{ "MISMATCHES" } = $fields[ 4 ];
3456 $record->{ "GAPS" } = $fields[ 5 ];
3457 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3458 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3459 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3460 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3461 $record->{ "E_VAL" } = $fields[ 10 ];
3462 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3464 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3466 $record->{ "STRAND" } = '-';
3468 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3472 $record->{ "STRAND" } = '+';
3475 put_record( $record, $out );
3486 # Martin A. Hansen, August 2007.
3488 # BLATs sequences in stream against a given genome.
3490 my ( $in, # handle to in stream
3491 $out, # handle to out stream
3492 $options, # options hash
3497 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3499 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3501 $options->{ 'tile_size' } ||= 11;
3502 $options->{ 'one_off' } ||= 0;
3503 $options->{ 'min_identity' } ||= 90;
3504 $options->{ 'min_score' } ||= 0;
3505 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3507 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3508 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3509 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3510 $blat_args .= " -minScore=$options->{ 'min_score' }";
3511 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3512 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3514 $query_file = "$BP_TMP/blat.seq";
3516 $fh_out = Maasha::Common::write_open( $query_file );
3518 while ( $record = get_record( $in ) )
3520 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3522 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3523 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3526 put_record( $record, $out );
3531 $blat_args .= " -t=dnax" if $type eq "protein";
3532 $blat_args .= " -q=$type";
3534 $result_file = "$BP_TMP/blat.psl";
3536 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3540 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3542 map { put_record( $_, $out ) } @{ $entries };
3544 unlink $result_file;
3548 sub script_match_seq
3550 # Martin A. Hansen, August 2007.
3552 # BLATs sequences in stream against a given genome.
3554 my ( $in, # handle to in stream
3555 $out, # handle to out stream
3556 $options, # options hash
3561 my ( $record, @entries, $results );
3563 $options->{ "word_size" } ||= 20;
3564 $options->{ "direction" } ||= "both";
3566 while ( $record = get_record( $in ) )
3568 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3569 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3572 put_record( $record, $out );
3575 if ( @entries == 1 )
3577 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3579 map { put_record( $_, $out ) } @{ $results };
3581 elsif ( @entries == 2 )
3583 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3585 map { put_record( $_, $out ) } @{ $results };
3590 sub script_create_vmatch_index
3592 # Martin A. Hansen, January 2008.
3594 # Create a vmatch index from sequences in the stream.
3596 my ( $in, # handle to in stream
3597 $out, # handle to out stream
3598 $options, # options hash
3603 my ( $record, $file_tmp, $fh_tmp, $type );
3605 if ( $options->{ "index_name" } )
3607 $file_tmp = $options->{ 'index_name' };
3608 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3611 while ( $record = get_record( $in ) )
3613 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3615 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3617 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3620 put_record( $record, $out ) if not $options->{ "no_stream" };
3623 if ( $options->{ "index_name" } )
3627 if ( $type eq "protein" ) {
3628 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3630 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3638 sub script_create_fixedstep_index
3640 # Martin A. Hansen, January 2008.
3642 # Create a fixedStep index from records in the stream.
3644 my ( $in, # handle to in stream
3645 $out, # handle to out stream
3646 $options, # options hash
3651 my ( $record, $file_tmp, $fh_tmp, $vals, $index );
3653 $file_tmp = "$BP_TMP/fixedstep.tmp";
3655 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3657 while ( $record = get_record( $in ) )
3659 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
3661 print $fh_tmp "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
3663 $vals = $record->{ 'VALS' };
3667 print $fh_tmp "$vals\n";
3673 $index = Maasha::UCSC::fixedstep_index_create( $file_tmp );
3677 Maasha::UCSC::fixedstep_index_store( $options->{ 'index_name' }, $index );
3681 sub script_vmatch_seq
3683 # Martin A. Hansen, August 2007.
3685 # Vmatches sequences in stream against a given genome.
3687 my ( $in, # handle to in stream
3688 $out, # handle to out stream
3689 $options, # options hash
3694 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3696 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3698 if ( $options->{ "index_name" } )
3700 @index_files = $options->{ "index_name" };
3704 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3706 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3708 @index_files = sort keys %hash;
3711 while ( $record = get_record( $in ) )
3713 push @records, $record;
3715 put_record( $record, $out );
3718 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3722 $fh_in = Maasha::Common::read_open( $result_file );
3724 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3725 put_record( $record, $out );
3730 unlink $result_file;
3734 sub script_write_fasta
3736 # Martin A. Hansen, August 2007.
3738 # Write FASTA entries from sequences in stream.
3740 my ( $in, # handle to in stream
3741 $out, # handle to out stream
3742 $options, # options hash
3747 my ( $record, $fh );
3749 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3751 while ( $record = get_record( $in ) )
3753 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3754 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3757 put_record( $record, $out ) if not $options->{ "no_stream" };
3764 sub script_write_align
3766 # Martin A. Hansen, August 2007.
3768 # Write pretty alignments aligned sequences in stream.
3770 my ( $in, # handle to in stream
3771 $out, # handle to out stream
3772 $options, # options hash
3777 my ( $fh, $record, @entries );
3779 $fh = write_stream( $options->{ "data_out" } ) ;
3781 while ( $record = get_record( $in ) )
3783 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3784 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3787 put_record( $record, $out ) if not $options->{ "no_stream" };
3790 if ( scalar( @entries ) == 2 ) {
3791 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3792 } elsif ( scalar ( @entries ) > 2 ) {
3793 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3800 sub script_write_blast
3802 # Martin A. Hansen, November 2007.
3804 # Write data in blast table format (-m8 and 9).
3806 my ( $in, # handle to in stream
3807 $out, # handle to out stream
3808 $options, # options hash
3813 my ( $fh, $record, $first );
3815 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3819 while ( $record = get_record( $in ) )
3821 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3823 if ( $options->{ "comment" } and $first )
3825 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";
3830 if ( $record->{ "STRAND" } eq "-" ) {
3831 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3834 print $fh join( "\t",
3835 $record->{ "Q_ID" },
3836 $record->{ "S_ID" },
3837 $record->{ "IDENT" },
3838 $record->{ "ALIGN_LEN" },
3839 $record->{ "MISMATCHES" },
3840 $record->{ "GAPS" },
3841 $record->{ "Q_BEG" } + 1,
3842 $record->{ "Q_END" } + 1,
3843 $record->{ "S_BEG" } + 1,
3844 $record->{ "S_END" } + 1,
3845 $record->{ "E_VAL" },
3846 $record->{ "BIT_SCORE" }
3850 put_record( $record, $out ) if not $options->{ "no_stream" };
3857 sub script_write_tab
3859 # Martin A. Hansen, August 2007.
3861 # Write data as table.
3863 my ( $in, # handle to in stream
3864 $out, # handle to out stream
3865 $options, # options hash
3870 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3872 $options->{ "delimit" } ||= "\t";
3874 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3876 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3878 while ( $record = get_record( $in ) )
3883 if ( $options->{ "keys" } )
3885 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3889 foreach $key ( @{ $options->{ "keys" } } )
3891 if ( exists $record->{ $key } )
3893 push @keys, $key if $options->{ "comment" };
3894 push @vals, $record->{ $key };
3901 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3903 next if exists $no_keys{ $key };
3905 push @keys, $key if $options->{ "comment" };
3906 push @vals, $record->{ $key };
3910 if ( @keys and $options->{ "comment" } )
3912 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3914 delete $options->{ "comment" };
3917 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3919 put_record( $record, $out ) if not $options->{ "no_stream" };
3926 sub script_write_bed
3928 # Martin A. Hansen, August 2007.
3930 # Write BED format for the UCSC genome browser using records in stream.
3932 my ( $in, # handle to in stream
3933 $out, # handle to out stream
3934 $options, # options hash
3939 my ( $fh, $record, $new_record );
3941 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3943 while ( $record = get_record( $in ) )
3945 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3947 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3949 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3951 $new_record->{ "CHR" } = $record->{ "S_ID" };
3952 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3953 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3954 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3955 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3956 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3958 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3960 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3962 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3964 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3966 $new_record->{ "CHR" } = $record->{ "S_ID" };
3967 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3968 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3969 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3970 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3971 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3973 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3975 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3977 $new_record->{ "CHR" } = $record->{ "S_ID" };
3978 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3979 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3980 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3981 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3982 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3984 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3986 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3988 Maasha::UCSC::bed_put_entry( $record, $fh );
3991 put_record( $record, $out ) if not $options->{ "no_stream" };
3998 sub script_write_psl
4000 # Martin A. Hansen, August 2007.
4002 # Write PSL output from stream.
4004 my ( $in, # handle to in stream
4005 $out, # handle to out stream
4006 $options, # options hash
4011 my ( $fh, $record, @output, $first );
4015 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4017 while ( $record = get_record( $in ) )
4019 put_record( $record, $out ) if not $options->{ "no_stream" };
4021 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4023 Maasha::UCSC::psl_put_header( $fh ) if $first;
4024 Maasha::UCSC::psl_put_entry( $record, $fh );
4033 sub script_write_fixedstep
4035 # Martin A. Hansen, Juli 2008.
4037 # Write fixedStep entries from recrods in the stream.
4039 my ( $in, # handle to in stream
4040 $out, # handle to out stream
4041 $options, # options hash
4046 my ( $fh, $record, $vals );
4048 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4050 while ( $record = get_record( $in ) )
4052 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4054 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4056 $vals = $record->{ 'VALS' };
4060 print $fh "$vals\n";
4063 put_record( $record, $out ) if not $options->{ "no_stream" };
4070 sub script_write_2bit
4072 # Martin A. Hansen, March 2008.
4074 # Write sequence entries from stream in 2bit format.
4076 my ( $in, # handle to in stream
4077 $out, # handle to out stream
4078 $options, # options hash
4083 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
4085 $mask = 1 if not $options->{ "no_mask" };
4087 $tmp_file = "$BP_TMP/write_2bit.fna";
4088 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4090 $fh_out = write_stream( $options->{ "data_out" } );
4092 while ( $record = get_record( $in ) )
4094 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4095 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
4098 put_record( $record, $out ) if not $options->{ "no_stream" };
4103 $fh_in = Maasha::Common::read_open( $tmp_file );
4105 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4114 sub script_write_solid
4116 # Martin A. Hansen, April 2008.
4118 # Write di-base encoded Solid sequence from entries in stream.
4120 my ( $in, # handle to in stream
4121 $out, # handle to out stream
4122 $options, # options hash
4127 my ( $record, $fh, $seq_cs );
4129 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4131 while ( $record = get_record( $in ) )
4133 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
4135 $seq_cs = Maasha::Solid::seq2color_space( uc $record->{ "SEQ" } );
4137 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
4140 put_record( $record, $out ) if not $options->{ "no_stream" };
4147 sub script_plot_seqlogo
4149 # Martin A. Hansen, August 2007.
4151 # Calculates and writes a sequence logo for alignments.
4153 my ( $in, # handle to in stream
4154 $out, # handle to out stream
4155 $options, # options hash
4160 my ( $record, @entries, $logo, $fh );
4162 while ( $record = get_record( $in ) )
4164 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4165 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4168 put_record( $record, $out ) if not $options->{ "no_stream" };
4171 $logo = Maasha::Plot::seq_logo( \@entries );
4173 $fh = write_stream( $options->{ "data_out" } );
4181 sub script_plot_phastcons_profiles
4183 # Martin A. Hansen, January 2008.
4185 # Plots PhastCons profiles.
4187 my ( $in, # handle to in stream
4188 $out, # handle to out stream
4189 $options, # options hash
4194 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4196 $options->{ "title" } ||= "PhastCons Profiles";
4198 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4199 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4201 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4202 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4204 while ( $record = get_record( $in ) )
4206 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4208 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4210 push @{ $AoA }, [ @{ $scores } ];
4213 put_record( $record, $out ) if not $options->{ "no_stream" };
4216 Maasha::UCSC::phastcons_normalize( $AoA );
4218 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4219 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4221 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4223 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4225 $fh = write_stream( $options->{ "data_out" } );
4227 print $fh "$_\n" foreach @{ $plot };
4233 sub script_analyze_bed
4235 # Martin A. Hansen, March 2008.
4237 # Analyze BED entries in stream.
4239 my ( $in, # handle to in stream
4240 $out, # handle to out stream
4241 $options, # options hash
4248 while ( $record = get_record( $in ) )
4250 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4252 put_record( $record, $out );
4257 sub script_analyze_vals
4259 # Martin A. Hansen, August 2007.
4261 # Analyze values for given keys in stream.
4263 my ( $in, # handle to in stream
4264 $out, # handle to out stream
4265 $options, # options hash
4270 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4272 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4274 while ( $record = get_record( $in ) )
4276 foreach $key ( keys %{ $record } )
4278 next if $options->{ "keys" } and not exists $key_hash{ $key };
4280 $analysis->{ $key }->{ "COUNT" }++;
4282 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4284 $analysis->{ $key }->{ "TYPE" } = "num";
4285 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4286 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4287 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4291 $len = length $record->{ $key };
4293 $analysis->{ $key }->{ "TYPE" } = "alph";
4294 $analysis->{ $key }->{ "SUM" } += $len;
4295 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4296 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4300 put_record( $record, $out ) if not $options->{ "no_stream" };
4303 foreach $key ( keys %{ $analysis } )
4305 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4306 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4309 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4319 if ( $options->{ "keys" } ) {
4320 @keys = @{ $options->{ "keys" } };
4322 @keys = keys %{ $analysis };
4325 foreach $key ( @keys )
4327 $keys .= sprintf "% 15s", $key;
4328 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4329 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4330 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4331 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4332 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4333 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4336 print $out "$keys\n";
4337 print $out "$types\n";
4338 print $out "$counts\n";
4339 print $out "$mins\n";
4340 print $out "$maxs\n";
4341 print $out "$sums\n";
4342 print $out "$means\n";
4346 sub script_head_records
4348 # Martin A. Hansen, August 2007.
4350 # Display the first sequences in stream.
4352 my ( $in, # handle to in stream
4353 $out, # handle to out stream
4354 $options, # options hash
4359 my ( $record, $count );
4361 $options->{ "num" } ||= 10;
4365 while ( $record = get_record( $in ) )
4369 put_record( $record, $out );
4371 last if $count == $options->{ "num" };
4376 sub script_remove_keys
4378 # Martin A. Hansen, August 2007.
4380 # Remove keys from stream.
4382 my ( $in, # handle to in stream
4383 $out, # handle to out stream
4384 $options, # options hash
4389 my ( $record, $new_record );
4391 while ( $record = get_record( $in ) )
4393 if ( $options->{ "keys" } )
4395 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4397 elsif ( $options->{ "save_keys" } )
4399 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4401 $record = $new_record;
4404 put_record( $record, $out ) if keys %{ $record };
4409 sub script_rename_keys
4411 # Martin A. Hansen, August 2007.
4413 # Rename keys in stream.
4415 my ( $in, # handle to in stream
4416 $out, # handle to out stream
4417 $options, # options hash
4424 while ( $record = get_record( $in ) )
4426 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4428 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4430 delete $record->{ $options->{ "keys" }->[ 0 ] };
4433 put_record( $record, $out );
4438 sub script_uniq_vals
4440 # Martin A. Hansen, August 2007.
4442 # Find unique values in stream.
4444 my ( $in, # handle to in stream
4445 $out, # handle to out stream
4446 $options, # options hash
4451 my ( %hash, $record );
4453 while ( $record = get_record( $in ) )
4455 if ( $record->{ $options->{ "key" } } )
4457 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4459 put_record( $record, $out );
4461 $hash{ $record->{ $options->{ "key" } } } = 1;
4463 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4465 put_record( $record, $out );
4469 $hash{ $record->{ $options->{ "key" } } } = 1;
4474 put_record( $record, $out );
4480 sub script_merge_vals
4482 # Martin A. Hansen, August 2007.
4484 # Rename keys in stream.
4486 my ( $in, # handle to in stream
4487 $out, # handle to out stream
4488 $options, # options hash
4493 my ( $record, @join, $i );
4495 $options->{ "delimit" } ||= '_';
4497 while ( $record = get_record( $in ) )
4499 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4501 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4503 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4504 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4507 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4510 put_record( $record, $out );
4517 # Martin A. Hansen, August 2007.
4519 # Grab for records in stream.
4521 my ( $in, # handle to in stream
4522 $out, # handle to out stream
4523 $options, # options hash
4528 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4530 if ( $options->{ "patterns" } )
4532 $patterns = [ split ",", $options->{ "patterns" } ];
4534 elsif ( -f $options->{ "patterns_in" } )
4536 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4538 elsif ( -f $options->{ "exact_in" } )
4540 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4542 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4547 if ( $options->{ "eval" } )
4549 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4557 while ( $record = get_record( $in ) )
4563 if ( $options->{ "keys" } )
4565 foreach $key ( @{ $options->{ "keys" } } )
4567 if ( exists $lookup_hash{ $record->{ $key } } )
4576 foreach $key ( keys %{ $record } )
4578 if ( not $options->{ "vals_only" } )
4580 if ( exists $lookup_hash{ $key } )
4587 if ( not $options->{ "keys_only" } )
4589 if ( exists $lookup_hash{ $record->{ $key } } )
4600 foreach $pattern ( @{ $patterns } )
4602 if ( $options->{ "keys" } )
4604 foreach $key ( @{ $options->{ "keys" } } )
4606 $pos = index $record->{ $key }, $pattern;
4608 goto FOUND if $pos >= 0;
4613 foreach $key ( keys %{ $record } )
4615 if ( not $options->{ "vals_only" } )
4617 $pos = index $key, $pattern;
4619 goto FOUND if $pos >= 0;
4622 if ( not $options->{ "keys_only" } )
4624 $pos = index $record->{ $key }, $pattern;
4626 goto FOUND if $pos >= 0;
4632 elsif ( $options->{ "regex" } )
4634 if ( $options->{ "keys" } )
4636 foreach $key ( @{ $options->{ "keys" } } )
4638 if ( $options->{ "case_insensitive" } ) {
4639 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4641 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4644 goto FOUND if $pos >= 0;
4649 foreach $key ( keys %{ $record } )
4651 if ( not $options->{ "vals_only" } )
4653 if ( $options->{ "case_insensitive" } ) {
4654 $pos = 1 if $key =~ /$options->{'regex'}/i;
4656 $pos = 1 if $key =~ /$options->{'regex'}/;
4659 goto FOUND if $pos >= 0;
4662 if ( not $options->{ "keys_only" } )
4664 if ( $options->{ "case_insensitive" } ) {
4665 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4667 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4670 goto FOUND if $pos >= 0;
4675 elsif ( $options->{ "eval" } )
4677 if ( defined $record->{ $key } )
4679 if ( $op eq "<" and $record->{ $key } < $val ) {
4680 $pos = 1 and goto FOUND;
4681 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4682 $pos = 1 and goto FOUND;
4683 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4684 $pos = 1 and goto FOUND;
4685 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4686 $pos = 1 and goto FOUND;
4687 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4688 $pos = 1 and goto FOUND;
4689 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4690 $pos = 1 and goto FOUND;
4691 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4692 $pos = 1 and goto FOUND;
4693 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4694 $pos = 1 and goto FOUND;
4701 if ( $pos >= 0 and not $options->{ "invert" } ) {
4702 put_record( $record, $out );
4703 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4704 put_record( $record, $out );
4712 # Martin A. Hansen, August 2007.
4714 # Evaluate extression for records in stream.
4716 my ( $in, # handle to in stream
4717 $out, # handle to out stream
4718 $options, # options hash
4723 my ( $record, $eval_key, $eval_val, $check, @keys );
4725 while ( $record = get_record( $in ) )
4727 if ( $options->{ "eval" } )
4729 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4737 @keys = split /\W+/, $eval_val;
4738 @keys = grep { ! /^\d+$/ } @keys;
4743 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4745 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4748 put_record( $record, $out );
4755 # Martin A. Hansen, June 2008.
4759 my ( $in, # handle to in stream
4760 $out, # handle to out stream
4761 $options, # options hash
4766 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4768 while ( $record = get_record( $in ) )
4772 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4774 push @rows, $record->{ $key };
4778 push @matrix, [ @rows ];
4783 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4785 foreach $row ( @matrix )
4787 for ( $i = 0; $i < @{ $row }; $i++ ) {
4788 $record->{ "V$i" } = $row->[ $i ];
4791 put_record( $record, $out );
4796 sub script_add_ident
4798 # Martin A. Hansen, May 2008.
4800 # Add a unique identifier to each record in stream.
4802 my ( $in, # handle to in stream
4803 $out, # handle to out stream
4804 $options, # options hash
4809 my ( $record, $key, $prefix, $i );
4811 $key = $options->{ "key" } || "ID";
4812 $prefix = $options->{ "prefix" } || "ID";
4816 while ( $record = get_record( $in ) )
4818 $record->{ $key } = sprintf( "$prefix%08d", $i );
4820 put_record( $record, $out );
4827 sub script_count_records
4829 # Martin A. Hansen, August 2007.
4831 # Count records in stream.
4833 my ( $in, # handle to in stream
4834 $out, # handle to out stream
4835 $options, # options hash
4840 my ( $record, $count, $result, $fh, $line );
4844 if ( $options->{ "no_stream" } )
4846 while ( $line = <$in> )
4850 $count++ if $line eq "---";
4855 while ( $record = get_record( $in ) )
4857 put_record( $record, $out );
4863 $result = { "count_records" => $count };
4865 $fh = write_stream( $options->{ "data_out" } );
4867 put_record( $result, $fh );
4873 sub script_random_records
4875 # Martin A. Hansen, August 2007.
4877 # Pick a number or random records from stream.
4879 my ( $in, # handle to in stream
4880 $out, # handle to out stream
4881 $options, # options hash
4886 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4888 $options->{ "num" } ||= 10;
4890 $tmp_file = "$BP_TMP/random_records.tmp";
4892 $fh_out = Maasha::Common::write_open( $tmp_file );
4896 while ( $record = get_record( $in ) )
4898 put_record( $record, $fh_out );
4908 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4910 while ( $i < $options->{ "num" } )
4912 $rand = int( rand( $count ) );
4914 if ( not exists $rand_hash{ $rand } )
4916 $rand_hash{ $rand } = 1;
4918 $max = $rand if $rand > $max;
4924 $fh_in = Maasha::Common::read_open( $tmp_file );
4928 while ( $record = get_record( $fh_in ) )
4930 put_record( $record, $out ) if exists $rand_hash{ $count };
4932 last if $count == $max;
4943 sub script_sort_records
4945 # Martin A. Hansen, August 2007.
4947 # Sort to sort records according to keys.
4949 my ( $in, # handle to in stream
4950 $out, # handle to out stream
4951 $options, # options hash
4956 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4958 foreach $key ( @{ $options->{ "keys" } } )
4960 if ( $key =~ s/n$// ) {
4961 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4963 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4967 $sort_str = join " or ", @sort_cmd;
4968 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4970 while ( $record = get_record( $in ) ) {
4971 push @records, $record;
4974 @records = sort $sort_sub @records;
4976 if ( $options->{ "reverse" } )
4978 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4979 put_record( $records[ $i ], $out );
4984 for ( $i = 0; $i < scalar @records; $i++ ) {
4985 put_record( $records[ $i ], $out );
4991 sub script_count_vals
4993 # Martin A. Hansen, August 2007.
4995 # Count records in stream.
4997 my ( $in, # handle to in stream
4998 $out, # handle to out stream
4999 $options, # options hash
5004 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5006 $tmp_file = "$BP_TMP/count_cache.tmp";
5008 $fh_out = Maasha::Common::write_open( $tmp_file );
5012 while ( $record = get_record( $in ) )
5014 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5016 push @records, $record;
5018 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5020 map { put_record( $_, $fh_out ) } @records;
5027 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5038 $fh_in = Maasha::Common::read_open( $tmp_file );
5040 while ( $record = get_record( $fh_in ) )
5042 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5044 put_record( $record, $out );
5046 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5054 foreach $record ( @records )
5056 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5058 put_record( $record, $out );
5065 sub script_plot_histogram
5067 # Martin A. Hansen, September 2007.
5069 # Plot a simple histogram for a given key using GNU plot.
5071 my ( $in, # handle to in stream
5072 $out, # handle to out stream
5073 $options, # options hash
5078 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5080 $options->{ "title" } ||= "Histogram";
5081 $options->{ "sort" } ||= "num";
5083 while ( $record = get_record( $in ) )
5085 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5087 put_record( $record, $out ) if not $options->{ "no_stream" };
5090 if ( $options->{ "sort" } eq "num" ) {
5091 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5093 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5096 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5098 $fh = write_stream( $options->{ "data_out" } );
5100 print $fh "$_\n" foreach @{ $result };
5106 sub script_plot_lendist
5108 # Martin A. Hansen, August 2007.
5110 # Plot length distribution using GNU plot.
5112 my ( $in, # handle to in stream
5113 $out, # handle to out stream
5114 $options, # options hash
5119 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5121 $options->{ "title" } ||= "Length Distribution";
5123 while ( $record = get_record( $in ) )
5125 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5127 put_record( $record, $out ) if not $options->{ "no_stream" };
5130 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5132 for ( $i = 0; $i < $max; $i++ ) {
5133 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5136 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5138 $fh = write_stream( $options->{ "data_out" } );
5140 print $fh "$_\n" foreach @{ $result };
5146 sub script_plot_chrdist
5148 # Martin A. Hansen, August 2007.
5150 # Plot chromosome distribution using GNU plot.
5152 my ( $in, # handle to in stream
5153 $out, # handle to out stream
5154 $options, # options hash
5159 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5161 $options->{ "title" } ||= "Chromosome Distribution";
5163 while ( $record = get_record( $in ) )
5165 if ( $record->{ "CHR" } ) { # generic
5166 $data_hash{ $record->{ "CHR" } }++;
5167 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5168 $data_hash{ $record->{ "S_ID" } }++;
5169 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5170 $data_hash{ $record->{ "S_ID" } }++;
5171 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5172 $data_hash{ $record->{ "S_ID" } }++;
5175 put_record( $record, $out ) if not $options->{ "no_stream" };
5178 foreach $elem ( keys %data_hash )
5182 $sort_key =~ s/chr//i;
5184 $sort_key =~ s/^X(.*)/99$1/;
5185 $sort_key =~ s/^Y(.*)/99$1/;
5186 $sort_key =~ s/^Z(.*)/999$1/;
5187 $sort_key =~ s/^M(.*)/9999$1/;
5188 $sort_key =~ s/^U(.*)/99999$1/;
5190 $count = $sort_key =~ tr/_//;
5192 $sort_key =~ s/_.*/"999999" x $count/ex;
5194 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5197 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5199 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5201 $fh = write_stream( $options->{ "data_out" } );
5203 print $fh "$_\n" foreach @{ $result };
5209 sub script_plot_karyogram
5211 # Martin A. Hansen, August 2007.
5213 # Plot hits on karyogram.
5215 my ( $in, # handle to in stream
5216 $out, # handle to out stream
5217 $options, # options hash
5222 my ( %options, $record, @data, $fh, $result, %data_hash );
5224 $options->{ "genome" } ||= "human";
5225 $options->{ "feat_color" } ||= "black";
5227 while ( $record = get_record( $in ) )
5229 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5231 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5234 put_record( $record, $out ) if not $options->{ "no_stream" };
5237 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5239 $fh = write_stream( $options->{ "data_out" } );
5247 sub script_plot_matches
5249 # Martin A. Hansen, August 2007.
5251 # Plot matches in 2D generating a dotplot.
5253 my ( $in, # handle to in stream
5254 $out, # handle to out stream
5255 $options, # options hash
5260 my ( $record, @data, $fh, $result, %data_hash );
5262 $options->{ "direction" } ||= "both";
5264 while ( $record = get_record( $in ) )
5266 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5267 push @data, $record;
5270 put_record( $record, $out ) if not $options->{ "no_stream" };
5273 $options->{ "title" } ||= "plot_matches";
5274 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5275 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5277 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5279 $fh = write_stream( $options->{ "data_out" } );
5281 print $fh "$_\n" foreach @{ $result };
5287 sub script_length_vals
5289 # Martin A. Hansen, August 2007.
5291 # Determine the length of the value for given keys.
5293 my ( $in, # handle to in stream
5294 $out, # handle to out stream
5295 $options, # options hash
5300 my ( $record, $key );
5302 while ( $record = get_record( $in ) )
5304 foreach $key ( @{ $options->{ "keys" } } )
5306 if ( $record->{ $key } ) {
5307 $record->{ $key . "_LEN" } = length $record->{ $key };
5311 put_record( $record, $out );
5318 # Martin A. Hansen, August 2007.
5320 # Calculates the sums for values of given keys.
5322 my ( $in, # handle to in stream
5323 $out, # handle to out stream
5324 $options, # options hash
5329 my ( $record, $key, %sum_hash, $fh );
5331 while ( $record = get_record( $in ) )
5333 foreach $key ( @{ $options->{ "keys" } } )
5335 if ( $record->{ $key } ) {
5336 $sum_hash{ $key } += $record->{ $key };
5340 put_record( $record, $out ) if not $options->{ "no_stream" };
5343 $fh = write_stream( $options->{ "data_out" } );
5345 foreach $key ( @{ $options->{ "keys" } } ) {
5346 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5353 sub script_mean_vals
5355 # Martin A. Hansen, August 2007.
5357 # Calculate the mean of values of given keys.
5359 my ( $in, # handle to in stream
5360 $out, # handle to out stream
5361 $options, # options hash
5366 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5368 while ( $record = get_record( $in ) )
5370 foreach $key ( @{ $options->{ "keys" } } )
5372 if ( $record->{ $key } )
5374 $sum_hash{ $key } += $record->{ $key };
5375 $count_hash{ $key }++;
5379 put_record( $record, $out ) if not $options->{ "no_stream" };
5382 $fh = write_stream( $options->{ "data_out" } );
5384 foreach $key ( @{ $options->{ "keys" } } )
5386 if ( $count_hash{ $key } ) {
5387 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5392 put_record( { $key . "_MEAN" => $mean } , $fh );
5399 sub script_median_vals
5401 # Martin A. Hansen, March 2008.
5403 # Calculate the median values of given keys.
5405 my ( $in, # handle to in stream
5406 $out, # handle to out stream
5407 $options, # options hash
5412 my ( $record, $key, %median_hash, $median, $fh );
5414 while ( $record = get_record( $in ) )
5416 foreach $key ( @{ $options->{ "keys" } } ) {
5417 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5420 put_record( $record, $out ) if not $options->{ "no_stream" };
5423 $fh = write_stream( $options->{ "data_out" } );
5425 foreach $key ( @{ $options->{ "keys" } } )
5427 if ( $median_hash{ $key } ) {
5428 $median = Maasha::Calc::median( $median_hash{ $key } );
5433 put_record( { $key . "_MEDIAN" => $median } , $fh );
5442 # Martin A. Hansen, February 2008.
5444 # Determine the maximum values of given keys.
5446 my ( $in, # handle to in stream
5447 $out, # handle to out stream
5448 $options, # options hash
5453 my ( $record, $key, $fh, %max_hash, $max_record );
5455 while ( $record = get_record( $in ) )
5457 foreach $key ( @{ $options->{ "keys" } } )
5459 if ( $record->{ $key } )
5461 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5465 put_record( $record, $out ) if not $options->{ "no_stream" };
5468 $fh = write_stream( $options->{ "data_out" } );
5470 foreach $key ( @{ $options->{ "keys" } } )
5472 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5475 put_record( $max_record, $fh );
5483 # Martin A. Hansen, February 2008.
5485 # Determine the minimum values of given keys.
5487 my ( $in, # handle to in stream
5488 $out, # handle to out stream
5489 $options, # options hash
5494 my ( $record, $key, $fh, %min_hash, $min_record );
5496 while ( $record = get_record( $in ) )
5498 foreach $key ( @{ $options->{ "keys" } } )
5500 if ( defined $record->{ $key } )
5502 if ( exists $min_hash{ $key } ) {
5503 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5505 $min_hash{ $key } = $record->{ $key };
5510 put_record( $record, $out ) if not $options->{ "no_stream" };
5513 $fh = write_stream( $options->{ "data_out" } );
5515 foreach $key ( @{ $options->{ "keys" } } )
5517 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5520 put_record( $min_record, $fh );
5526 sub script_upload_to_ucsc
5528 # Martin A. Hansen, August 2007.
5530 # Calculate the mean of values of given keys.
5532 my ( $in, # handle to in stream
5533 $out, # handle to out stream
5534 $options, # options hash
5539 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5540 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5542 $options->{ "short_label" } ||= $options->{ 'table' };
5543 $options->{ "long_label" } ||= $options->{ 'table' };
5544 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5545 $options->{ "priority" } ||= 1;
5546 $options->{ "visibility" } ||= "pack";
5547 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5548 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5550 $file = "$BP_TMP/ucsc_upload.tmp";
5558 if ( $options->{ 'wiggle' } )
5560 $options->{ "visibility" } = "full";
5562 while ( $record = get_record( $in ) )
5564 put_record( $record, $out ) if not $options->{ "no_stream" };
5566 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5567 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5568 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5570 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5572 $fh_out = $fh_hash{ $record->{ "CHR" } };
5574 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5577 map { close $_ } keys %fh_hash;
5579 $fh_out = Maasha::Common::write_open( $file );
5581 foreach $chr ( sort keys %fh_hash )
5583 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5585 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5589 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5591 $chr = $entry->{ 'CHR' };
5592 $beg = $entry->{ 'CHR_BEG' };
5593 $end = $entry->{ 'CHR_END' };
5594 $q_id = $entry->{ 'Q_ID' };
5596 if ( $q_id =~ /_(\d+)$/ ) {
5606 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5611 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5612 $block->[ $i ] += $clones;
5615 $max = Maasha::Calc::max( $max, $end );
5624 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5625 $block->[ $i ] += $clones;
5632 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5634 unlink "$BP_TMP/$chr";
5639 $wig_file = "$options->{ 'table' }.wig";
5640 $wib_file = "$options->{ 'table' }.wib";
5642 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5644 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5646 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5648 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5649 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5659 $fh_out = Maasha::Common::write_open( $file );
5661 while ( $record = get_record( $in ) )
5663 put_record( $record, $out ) if not $options->{ "no_stream" };
5665 if ( $record->{ "REC_TYPE" } eq "PSL" )
5667 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5668 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5672 $format = "PSL" if not $format;
5674 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5676 # chrom chromStart chromEnd name score strand size secStr conf
5678 print $fh_out join ( "\t",
5680 $record->{ "CHR_BEG" },
5681 $record->{ "CHR_END" } + 1,
5682 $record->{ "Q_ID" },
5683 $record->{ "SCORE" },
5684 $record->{ "STRAND" },
5685 $record->{ "SIZE" },
5686 $record->{ "SEC_STRUCT" },
5687 $record->{ "CONF" },
5690 $format = "BED_SS" if not $format;
5692 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5694 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5696 $format = "BED" if not $format;
5697 $columns = $record->{ "BED_COLS" } if not $columns;
5699 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5701 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5703 $format = "BED" if not $format;
5704 $columns = 6 if not $columns;
5706 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5708 $record->{ "CHR" } = $record->{ "S_ID" };
5709 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5710 $record->{ "CHR_END" } = $record->{ "S_END" };
5711 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5713 $format = "BED" if not $format;
5714 $columns = 6 if not $columns;
5716 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5718 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5720 $record->{ "CHR" } = $record->{ "S_ID" };
5721 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5722 $record->{ "CHR_END" } = $record->{ "S_END" };
5723 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5724 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5726 $format = "BED" if not $format;
5727 $columns = 6 if not $columns;
5729 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5732 if ( $i == $options->{ "chunk_size" } )
5736 if ( $format eq "BED" ) {
5737 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5738 } elsif ( $format eq "PSL" ) {
5739 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5748 $fh_out = Maasha::Common::write_open( $file );
5757 if ( exists $options->{ "database" } and $options->{ "table" } )
5759 if ( $format eq "BED" )
5761 $type = "bed $columns";
5763 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5765 elsif ( $format eq "BED_SS" )
5767 $options->{ "sec_struct" } = 1;
5769 $type = "sec_struct";
5771 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5773 elsif ( $format eq "PSL" )
5777 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5779 elsif ( $format eq "WIGGLE" )
5783 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5788 Maasha::UCSC::update_my_tracks( $options, $type );
5793 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5798 # Martin A. Hansen, July 2007.
5800 # Opens a stream to STDIN or a file,
5802 my ( $path, # path - OPTIONAL
5805 # Returns filehandle.
5809 if ( not -t STDIN ) {
5810 $fh = Maasha::Common::read_stdin();
5811 } elsif ( not $path ) {
5812 # Maasha::Common::error( qq(no data stream) );
5814 $fh = Maasha::Common::read_open( $path );
5817 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5825 # Martin A. Hansen, August 2007.
5827 # Opens a stream to STDOUT or a file.
5829 my ( $path, # path - OPTIONAL
5830 $gzip, # compress data - OPTIONAL
5833 # Returns filehandle.
5838 $fh = Maasha::Common::write_open( $path, $gzip );
5840 $fh = Maasha::Common::write_stdout();
5849 # Martin A. Hansen, July 2007.
5851 # Reads one record at a time and converts that record
5852 # to a Perl data structure (a hash) which is returned.
5857 # Returns data structure.
5859 my ( $block, @lines, $line, $key, $value, %record );
5861 local $/ = "\n---\n";
5867 return if not defined $block;
5869 @lines = split "\n", $block;
5871 foreach $line ( @lines )
5873 ( $key, $value ) = split ": ", $line;
5875 $record{ $key } = $value;
5878 return wantarray ? %record : \%record;
5884 # Martin A. Hansen, July 2007.
5886 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5888 my ( $data, # data structure
5889 $fh, # file handle - OPTIONAL
5894 if ( scalar keys %{ $data } )
5898 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5903 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5914 # Martin A. Hansen, November 2007.
5916 # Extracts files from an explicit GetOpt::Long argument
5917 # allowing for the use of glob. E.g.
5918 # --data_in=test.fna
5919 # --data_in=test.fna,test2.fna
5921 # --data_in=test.fna,/dir/*.fna
5923 my ( $option, # option from GetOpt::Long
5928 my ( $elem, @files );
5930 foreach $elem ( split ",", $option )
5934 } elsif ( $elem =~ /\*/ ) {
5935 push @files, glob( $elem );
5939 return wantarray ? @files : \@files;
5945 # Martin A. Hansen, April 2008.
5947 # Removes temporary directory and exits gracefully.
5948 # This subroutine is meant to be run always as the last
5949 # thing even if a script is dies or is interrupted
5952 my ( $sig, # signal from the %SIG
5955 # print STDERR "signal->$sig<-\n";
5963 if ( $sig =~ /MAASHA_ERROR/ ) {
5964 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5965 } elsif ( $sig eq "INT" ) {
5966 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5967 } elsif ( $sig eq "TERM" ) {
5968 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5970 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5973 # This is a really bad solution, potentially, anyone can include this module and set
5974 # the BP_TMP to point at any dir and thus take out the machine !!!
5976 Maasha::Common::dir_remove( $BP_TMP );
5985 # This is a really bad solution, potentially, anyone can include this module and set
5986 # the BP_TMP to point at any dir and thus take out the machine !!!
5988 Maasha::Common::dir_remove( $BP_TMP );
5992 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5999 sub script_read_soft
6001 # Martin A. Hansen, December 2007.
6004 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
6006 my ( $in, # handle to in stream
6007 $out, # handle to out stream
6008 $options, # options hash
6013 my ( $data_in, $file, $num, $records, $record );
6015 while ( $record = get_record( $in ) ) {
6016 put_record( $record, $out );
6021 foreach $file ( @{ $options->{ "files" } } )
6023 $records = Maasha::NCBI::soft_parse( $file );
6025 foreach $record ( @{ $records } )
6027 put_record( $record, $out );
6029 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
6037 close $data_in if $data_in;