1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
57 use vars qw( @ISA @EXPORT_OK );
61 @ISA = qw( Exporter );
76 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
79 $SIG{ '__DIE__' } = \&sig_handler;
80 $SIG{ 'INT' } = \&sig_handler;
81 $SIG{ 'TERM' } = \&sig_handler;
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87 my ( $script, $BP_TMP );
89 $script = Maasha::Common::get_scriptname();
90 $BP_TMP = Maasha::Common::get_tmpdir();
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
96 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
97 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
99 $log_global->autoflush( 1 );
100 $log_local->autoflush( 1 );
102 &log( $log_global, $script, \@ARGV );
103 &log( $log_local, $script, \@ARGV );
109 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
112 my $t0 = gettimeofday();
114 run_script( $script );
116 my $t1 = gettimeofday();
118 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
121 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
126 # Martin A. Hansen, January 2008.
128 # Log messages to logfile.
130 my ( $fh, # filehandle to logfile
131 $script, # script name
132 $argv, # reference to @ARGV
137 my ( $time_stamp, $user );
139 $time_stamp = Maasha::Common::time_stamp();
141 $user = $ENV{ 'USER' };
143 $script = "biopieces" if $script eq "-e";
145 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
151 # Martin A. Hansen, August 2007.
153 # Run a specific script.
155 my ( $script, # script name
160 my ( $options, $in, $out );
162 $options = get_options( $script );
164 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
165 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
168 $in = read_stream( $options->{ "stream_in" } );
169 $out = write_stream( $options->{ "stream_out" } );
171 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
172 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
173 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
174 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
175 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
176 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
177 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
178 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
179 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
181 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
182 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
183 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
184 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
185 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
186 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
187 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
188 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
189 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
190 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
191 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
192 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
193 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
194 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
195 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
196 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
197 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
198 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
199 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
200 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
201 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
202 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
203 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
204 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
205 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
206 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
207 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
209 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
210 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
211 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
212 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
213 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
214 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
215 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
216 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
217 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
218 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
219 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
220 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
221 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
222 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
223 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
224 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
225 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
226 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
227 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
228 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
229 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
230 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
231 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
232 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
233 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
234 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
235 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
236 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
237 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
238 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
239 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
240 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
241 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
242 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
243 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
244 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
245 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
246 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
247 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
248 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
249 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
250 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
251 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
252 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
253 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
254 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
255 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
256 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
257 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
258 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
259 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
260 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
262 close $in if defined $in;
265 # unset status - missing
266 # write log file - missing
272 # Martin A. Hansen, February 2008.
274 # Gets options from commandline and checks these vigerously.
276 my ( $script, # name of script
281 my ( %options, @options, $opt, @genomes );
283 if ( $script eq "print_usage" )
289 elsif ( $script eq "read_fasta" )
296 elsif ( $script eq "read_tab" )
307 elsif ( $script eq "read_psl" )
314 elsif ( $script eq "read_bed" )
321 elsif ( $script eq "read_fixedstep" )
328 elsif ( $script eq "read_blast_tab" )
335 elsif ( $script eq "read_embl" )
345 elsif ( $script eq "read_stockholm" )
352 elsif ( $script eq "read_phastcons" )
363 elsif ( $script eq "read_soft" )
370 elsif ( $script eq "read_gff" )
377 elsif ( $script eq "read_2bit" )
385 elsif ( $script eq "read_solexa" )
393 elsif ( $script eq "read_solid" )
401 elsif ( $script eq "read_mysql" )
410 elsif ( $script eq "format_genome" )
419 elsif ( $script eq "length_seq" )
426 elsif ( $script eq "oligo_freq" )
433 elsif ( $script eq "create_weight_matrix" )
439 elsif ( $script eq "transliterate_seq" )
447 elsif ( $script eq "transliterate_vals" )
456 elsif ( $script eq "translate_seq" )
462 elsif ( $script eq "extract_seq" )
470 elsif ( $script eq "get_genome_seq" )
482 elsif ( $script eq "get_genome_align" )
493 elsif ( $script eq "get_genome_phastcons" )
504 elsif ( $script eq "split_seq" )
511 elsif ( $script eq "split_bed" )
518 elsif ( $script eq "tile_seq" )
525 elsif ( $script eq "invert_align" )
531 elsif ( $script eq "patscan_seq" )
542 elsif ( $script eq "create_blast_db" )
549 elsif ( $script eq "blast_seq" )
561 elsif ( $script eq "blat_seq" )
573 elsif ( $script eq "soap_seq" )
581 elsif ( $script eq "match_seq" )
588 elsif ( $script eq "create_vmatch_index" )
596 elsif ( $script eq "vmatch_seq" )
607 elsif ( $script eq "write_fasta" )
616 elsif ( $script eq "write_align" )
626 elsif ( $script eq "write_blast" )
635 elsif ( $script eq "write_tab" )
647 elsif ( $script eq "write_bed" )
655 elsif ( $script eq "write_psl" )
663 elsif ( $script eq "write_fixedstep" )
671 elsif ( $script eq "write_2bit" )
679 elsif ( $script eq "write_solid" )
688 elsif ( $script eq "plot_seqlogo" )
695 elsif ( $script eq "plot_phastcons_profiles" )
710 elsif ( $script eq "analyze_vals" )
717 elsif ( $script eq "head_records" )
723 elsif ( $script eq "remove_keys" )
730 elsif ( $script eq "rename_keys" )
736 elsif ( $script eq "uniq_vals" )
743 elsif ( $script eq "merge_vals" )
750 elsif ( $script eq "grab" )
765 elsif ( $script eq "compute" )
771 elsif ( $script eq "add_ident" )
778 elsif ( $script eq "count_records" )
785 elsif ( $script eq "random_records" )
791 elsif ( $script eq "sort_records" )
798 elsif ( $script eq "count_vals" )
804 elsif ( $script eq "plot_histogram" )
817 elsif ( $script eq "plot_lendist" )
829 elsif ( $script eq "plot_chrdist" )
840 elsif ( $script eq "plot_karyogram" )
849 elsif ( $script eq "plot_matches" )
861 elsif ( $script eq "length_vals" )
867 elsif ( $script eq "sum_vals" )
875 elsif ( $script eq "mean_vals" )
883 elsif ( $script eq "median_vals" )
891 elsif ( $script eq "max_vals" )
899 elsif ( $script eq "min_vals" )
907 elsif ( $script eq "upload_to_ucsc" )
932 # print STDERR Dumper( \@options );
939 $options{ "script" } = $script;
941 # print STDERR Dumper( \%options );
943 if ( -t STDIN && scalar( keys %options ) == 1 or $options{ "help" } ) {
944 return wantarray ? %options : \%options;
947 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
948 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
949 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
950 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
951 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
952 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
953 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
954 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
956 # ---- check arguments ----
958 if ( $options{ 'data_in' } )
960 $options{ "files" } = getopt_files( $options{ 'data_in' } );
962 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
965 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
967 # print STDERR Dumper( \%options );
969 foreach $opt ( keys %options )
971 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
973 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
975 elsif ( $opt =~ /beg|end|word_size|wrap|chunk_size|tile_size|len|prefix_length|num|skip|cpus|window_size|step_size/ and $options{ $opt } !~ /^\d+$/ )
977 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
979 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
981 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
983 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
985 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
987 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
989 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
991 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
993 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
995 elsif ( $opt eq "genome" and $script ne "format_genome" )
997 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
998 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1000 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1001 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1004 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1006 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1008 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1010 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1014 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1015 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1016 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1017 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1018 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1019 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1020 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1021 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1022 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /format_genome|get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
1023 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1024 Maasha::Common::error( qq(no --keys speficied) ) if $script =~ /sort_records|count_vals|sum_vals|mean_vals|median_vals|length_vals/ and not $options{ "keys" };
1026 if ( $script eq "upload_to_ucsc" )
1028 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1029 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1032 return wantarray ? %options : \%options;
1036 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1039 sub script_print_usage
1041 # Martin A. Hansen, January 2008.
1043 # Retrieves usage information from file and
1044 # prints this nicely formatted.
1046 my ( $in, # handle to in stream
1047 $out, # handle to out stream
1048 $options, # options hash
1053 my ( $file, $wiki, $lines );
1055 if ( $options->{ 'data_in' } ) {
1056 $file = $options->{ 'data_in' };
1058 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'script' }, ".wiki";
1061 $wiki = Maasha::Gwiki::gwiki_read( $file );
1063 if ( not $options->{ "help" } ) {
1064 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1067 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1069 print STDERR "$_\n" foreach @{ $lines };
1075 sub script_list_biopieces
1077 # Martin A. Hansen, January 2008.
1079 # Prints the synopsis from the usage for each of the biopieces.
1081 my ( $in, # handle to in stream
1082 $out, # handle to out stream
1083 $options, # options hash
1088 my ( @files, $file, $wiki, $program, $synopsis );
1090 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1092 foreach $file ( sort @files )
1094 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1098 $wiki = Maasha::Gwiki::gwiki_read( $file );
1100 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1101 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1103 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1105 printf( "%-30s%s\n", $program, $synopsis );
1113 sub script_list_genomes
1115 # Martin A. Hansen, January 2008.
1117 # Prints the synopsis from the usage for each of the biopieces.
1119 my ( $in, # handle to in stream
1120 $out, # handle to out stream
1121 $options, # options hash
1126 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1128 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1130 foreach $genome ( @genomes )
1132 next if $genome =~ /\.$/;
1134 @formats = Maasha::Common::ls_dirs( $genome );
1136 foreach $format ( @formats )
1138 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1140 $hash{ $1 }{ $2 } = 1;
1149 map { push @row, $_ } sort keys %found;
1151 print join( "\t", @row ), "\n";
1153 foreach $genome ( sort keys %hash )
1157 foreach $format ( sort keys %found )
1159 if ( exists $hash{ $genome }{ $format } ) {
1166 print join( "\t", @row ), "\n";
1171 sub script_read_fasta
1173 # Martin A. Hansen, August 2007.
1175 # Read sequences from FASTA file.
1177 my ( $in, # handle to in stream
1178 $out, # handle to out stream
1179 $options, # options hash
1184 my ( $record, $file, $data_in, $entry, $num );
1186 while ( $record = get_record( $in ) ) {
1187 put_record( $record, $out );
1192 foreach $file ( @{ $options->{ "files" } } )
1194 $data_in = Maasha::Common::read_open( $file );
1196 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1198 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1201 SEQ_NAME => $entry->[ SEQ_NAME ],
1202 SEQ => $entry->[ SEQ ],
1203 SEQ_LEN => length $entry->[ SEQ ],
1206 put_record( $record, $out );
1209 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1219 close $data_in if $data_in;
1225 # Martin A. Hansen, August 2007.
1227 # Read table or table columns from stream or file.
1229 my ( $in, # handle to in stream
1230 $out, # handle to out stream
1231 $options, # options hash
1236 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1238 $options->{ 'delimit' } ||= '\s+';
1240 while ( $record = get_record( $in ) ) {
1241 put_record( $record, $out );
1244 $skip = $options->{ 'skip' } ||= 0;
1247 foreach $file ( @{ $options->{ "files" } } )
1249 $data_in = Maasha::Common::read_open( $file );
1251 while ( $line = <$data_in> )
1259 next if $line =~ /^#|^$/;
1266 @fields = split /$options->{'delimit'}/, $line;
1268 if ( $options->{ "cols" } ) {
1269 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1274 for ( $i = 0; $i < @fields2; $i++ )
1276 if ( $options->{ "keys" }->[ $i ] ) {
1277 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1279 $record->{ "V" . $i } = $fields2[ $i ];
1283 put_record( $record, $out );
1285 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1295 close $data_in if $data_in;
1301 # Martin A. Hansen, August 2007.
1303 # Read psl table from stream or file.
1305 my ( $in, # handle to in stream
1306 $out, # handle to out stream
1307 $options, # options hash
1312 my ( $record, @files, $file, $entries, $entry, $num );
1314 while ( $record = get_record( $in ) ) {
1315 put_record( $record, $out );
1320 foreach $file ( @{ $options->{ "files" } } )
1322 $entries = Maasha::UCSC::psl_get_entries( $file );
1324 foreach $entry ( @{ $entries } )
1326 put_record( $entry, $out );
1328 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1340 # Martin A. Hansen, August 2007.
1342 # Read bed table from stream or file.
1344 my ( $in, # handle to in stream
1345 $out, # handle to out stream
1346 $options, # options hash
1351 my ( $file, $record, $entry, $data_in, $num );
1353 while ( $record = get_record( $in ) ) {
1354 put_record( $record, $out );
1359 foreach $file ( @{ $options->{ "files" } } )
1361 $data_in = Maasha::Common::read_open( $file );
1363 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1365 put_record( $entry, $out );
1367 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1377 close $data_in if $data_in;
1381 sub script_read_fixedstep
1383 # Martin A. Hansen, Juli 2008.
1385 # Read fixedStep wiggle format from stream or file.
1387 my ( $in, # handle to in stream
1388 $out, # handle to out stream
1389 $options, # options hash
1394 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1396 while ( $record = get_record( $in ) ) {
1397 put_record( $record, $out );
1402 foreach $file ( @{ $options->{ "files" } } )
1404 $data_in = Maasha::Common::read_open( $file );
1406 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1408 $head = shift @{ $entry };
1410 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1412 $record->{ "CHR" } = $1;
1413 $record->{ "CHR_BEG" } = $2;
1414 $record->{ "STEP" } = $3;
1415 $record->{ "VALS" } = join ",", @{ $entry };
1418 put_record( $record, $out );
1420 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1430 close $data_in if $data_in;
1434 sub script_read_blast_tab
1436 # Martin A. Hansen, September 2007.
1438 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1440 my ( $in, # handle to in stream
1441 $out, # handle to out stream
1442 $options, # options hash
1447 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1449 while ( $record = get_record( $in ) ) {
1450 put_record( $record, $out );
1455 foreach $file ( @{ $options->{ "files" } } )
1457 $data_in = Maasha::Common::read_open( $file );
1459 while ( $line = <$data_in> )
1463 next if $line =~ /^#/;
1465 @fields = split /\t/, $line;
1467 $record->{ "REC_TYPE" } = "BLAST";
1468 $record->{ "Q_ID" } = $fields[ 0 ];
1469 $record->{ "S_ID" } = $fields[ 1 ];
1470 $record->{ "IDENT" } = $fields[ 2 ];
1471 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1472 $record->{ "MISMATCHES" } = $fields[ 4 ];
1473 $record->{ "GAPS" } = $fields[ 5 ];
1474 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1475 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1476 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1477 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1478 $record->{ "E_VAL" } = $fields[ 10 ];
1479 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1481 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1483 $record->{ "STRAND" } = '-';
1485 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1489 $record->{ "STRAND" } = '+';
1492 put_record( $record, $out );
1494 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1504 close $data_in if $data_in;
1508 sub script_read_embl
1510 # Martin A. Hansen, August 2007.
1514 my ( $in, # handle to in stream
1515 $out, # handle to out stream
1516 $options, # options hash
1521 my ( %options2, $file, $data_in, $num, $entry, $record );
1523 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1524 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1525 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1527 while ( $record = get_record( $in ) ) {
1528 put_record( $record, $out );
1533 foreach $file ( @{ $options->{ "files" } } )
1535 $data_in = Maasha::Common::read_open( $file );
1537 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1539 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1541 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1543 $record_copy = dclone $record;
1545 delete $record_copy->{ "FT" };
1547 put_record( $record_copy, $out );
1549 delete $record_copy->{ "SEQ" };
1551 foreach $feat ( keys %{ $record->{ "FT" } } )
1553 $record_copy->{ "FEAT_TYPE" } = $feat;
1555 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1557 foreach $qual ( keys %{ $feat2 } )
1559 $qual_val = join "; ", @{ $feat2->{ $qual } };
1564 $record_copy->{ $qual } = $qual_val;
1567 put_record( $record_copy, $out );
1571 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1581 close $data_in if $data_in;
1585 sub script_read_stockholm
1587 # Martin A. Hansen, August 2007.
1589 # Read Stockholm format.
1591 my ( $in, # handle to in stream
1592 $out, # handle to out stream
1593 $options, # options hash
1598 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1600 while ( $record = get_record( $in ) ) {
1601 put_record( $record, $out );
1606 foreach $file ( @{ $options->{ "files" } } )
1608 $data_in = Maasha::Common::read_open( $file );
1610 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1612 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1616 foreach $key ( keys %{ $record->{ "GF" } } ) {
1617 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1620 $record_anno->{ "ALIGN" } = $num;
1622 put_record( $record_anno, $out );
1624 foreach $seq ( @{ $record->{ "ALIGN" } } )
1626 undef $record_align;
1629 SEQ_NAME => $seq->[ 0 ],
1633 put_record( $record_align, $out );
1636 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1646 close $data_in if $data_in;
1650 sub script_read_phastcons
1652 # Martin A. Hansen, December 2007.
1654 # Read PhastCons format.
1656 my ( $in, # handle to in stream
1657 $out, # handle to out stream
1658 $options, # options hash
1663 my ( $data_in, $file, $num, $entry, @records, $record );
1665 $options->{ "min" } ||= 10;
1666 $options->{ "dist" } ||= 25;
1667 $options->{ "threshold" } ||= 0.8;
1668 $options->{ "gap" } ||= 5;
1670 while ( $record = get_record( $in ) ) {
1671 put_record( $record, $out );
1676 foreach $file ( @{ $options->{ "files" } } )
1678 $data_in = Maasha::Common::read_open( $file );
1680 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1682 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1684 foreach $record ( @records )
1686 $record->{ "REC_TYPE" } = "BED";
1687 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1689 put_record( $record, $out );
1691 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1702 close $data_in if $data_in;
1706 sub script_read_soft
1708 # Martin A. Hansen, December 2007.
1711 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1713 my ( $in, # handle to in stream
1714 $out, # handle to out stream
1715 $options, # options hash
1720 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1722 while ( $record = get_record( $in ) ) {
1723 put_record( $record, $out );
1728 foreach $file ( @{ $options->{ "files" } } )
1730 $soft_index = Maasha::NCBI::soft_index_file( $file );
1732 $fh = Maasha::Common::read_open( $file );
1734 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1736 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1738 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1740 $old_end = $platforms[ -1 ]->[ 2 ];
1742 foreach $sample ( @samples )
1744 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1746 foreach $record ( @{ $records } )
1748 put_record( $record, $out );
1750 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1755 $old_end = $sample->[ 2 ];
1763 close $data_in if $data_in;
1770 # Martin A. Hansen, February 2008.
1773 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1775 my ( $in, # handle to in stream
1776 $out, # handle to out stream
1777 $options, # options hash
1782 my ( $data_in, $file, $fh, $num, $record, $entry );
1784 while ( $record = get_record( $in ) ) {
1785 put_record( $record, $out );
1790 foreach $file ( @{ $options->{ "files" } } )
1792 $fh = Maasha::Common::read_open( $file );
1794 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1796 put_record( $entry, $out );
1798 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1808 close $data_in if $data_in;
1812 sub script_read_2bit
1814 # Martin A. Hansen, March 2008.
1816 # Read sequences from 2bit file.
1818 my ( $in, # handle to in stream
1819 $out, # handle to out stream
1820 $options, # options hash
1825 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1827 $mask = 1 if not $options->{ "no_mask" };
1829 while ( $record = get_record( $in ) ) {
1830 put_record( $record, $out );
1835 foreach $file ( @{ $options->{ "files" } } )
1837 $data_in = Maasha::Common::read_open( $file );
1839 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1841 foreach $line ( @{ $toc } )
1843 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1844 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1845 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1847 put_record( $record, $out );
1849 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1859 close $data_in if $data_in;
1863 sub script_read_solexa
1865 # Martin A. Hansen, March 2008.
1867 # Read Solexa sequence reads from file.
1869 my ( $in, # handle to in stream
1870 $out, # handle to out stream
1871 $options, # options hash
1876 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1878 $options->{ "quality" } ||= 20;
1880 while ( $record = get_record( $in ) ) {
1881 put_record( $record, $out );
1886 foreach $file ( @{ $options->{ "files" } } )
1888 $data_in = Maasha::Common::read_open( $file );
1889 $base_name = Maasha::Common::get_basename( $file );
1890 $base_name =~ s/\..*//;
1894 while ( $line = <$data_in> )
1896 @fields = split /:/, $line;
1897 @seqs = split //, $fields[ 5 ];
1898 @scores = split / /, $fields[ -1 ];
1900 for ( $i = 0; $i < @scores; $i++ ) {
1901 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1904 $seq = join "", @seqs;
1906 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1907 $record->{ "SEQ" } = $seq;
1908 $record->{ "SEQ_LEN" } = length $seq;
1909 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1911 put_record( $record, $out );
1913 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1924 close $data_in if $data_in;
1928 sub script_read_solid
1930 # Martin A. Hansen, April 2008.
1932 # Read Solid sequence from file.
1934 my ( $in, # handle to in stream
1935 $out, # handle to out stream
1936 $options, # options hash
1941 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1943 $options->{ "quality" } ||= 15;
1945 while ( $record = get_record( $in ) ) {
1946 put_record( $record, $out );
1951 foreach $file ( @{ $options->{ "files" } } )
1953 $data_in = Maasha::Common::read_open( $file );
1955 while ( $line = <$data_in> )
1959 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1961 @scores = split /,/, $seq_qual;
1962 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1964 for ( $i = 0; $i < @seqs; $i++ ) {
1965 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1969 SEQ_NAME => $seq_name,
1971 SEQ_QUAL => $seq_qual,
1972 SEQ_LEN => length $seq_cs,
1973 SEQ => join( "", @seqs ),
1974 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1977 put_record( $record, $out );
1979 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1989 close $data_in if $data_in;
1993 sub script_read_mysql
1995 # Martin A. Hansen, May 2008.
1997 # Read a MySQL query into stream.
1999 my ( $in, # handle to in stream
2000 $out, # handle to out stream
2001 $options, # options hash
2006 my ( $record, $dbh, $results );
2008 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2009 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2011 while ( $record = get_record( $in ) ) {
2012 put_record( $record, $out );
2015 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2017 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2019 Maasha::SQL::disconnect( $dbh );
2021 map { put_record( $_ ) } @{ $results };
2025 sub script_format_genome
2027 # Martin A. Hansen, Juli 2008.
2029 # Format a genome to speficed formats.
2031 my ( $in, # handle to in stream
2032 $out, # handle to out stream
2033 $options, # options hash
2038 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index );
2040 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2041 $genome = $options->{ 'genome' };
2043 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2044 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2045 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2047 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2049 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2051 $fasta_dir = "$dir/genomes/$genome/fasta";
2055 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2057 $fasta_dir = "$dir/genomes/$genome/fasta";
2059 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2062 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2064 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2066 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2068 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2071 while ( $record = get_record( $in ) )
2073 if ( $fh_out and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2075 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, $options->{ "wrap" } );
2077 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2079 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2081 $vals = $record->{ 'VALS' };
2085 print $fh_out "$vals\n";
2088 put_record( $record, $out ) if not $options->{ "no_stream" };
2091 foreach $format ( @{ $options->{ 'formats' } } )
2093 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2094 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2095 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2096 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2097 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2100 close $fh_out if $fh_out;
2104 sub script_length_seq
2106 # Martin A. Hansen, August 2007.
2108 # Determine the length of sequences in stream.
2110 my ( $in, # handle to in stream
2111 $out, # handle to out stream
2112 $options, # options hash
2117 my ( $record, $total );
2119 while ( $record = get_record( $in ) )
2121 if ( $record->{ "SEQ" } )
2123 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2124 $total += $record->{ "SEQ_LEN" };
2127 put_record( $record, $out ) if not $options->{ "no_stream" };
2130 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2134 sub script_uppercase_seq
2136 # Martin A. Hansen, August 2007.
2138 # Uppercases sequences in stream.
2140 my ( $in, # handle to in stream
2141 $out, # handle to out stream
2148 while ( $record = get_record( $in ) )
2150 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2152 put_record( $record, $out );
2157 sub script_shuffle_seq
2159 # Martin A. Hansen, December 2007.
2161 # Shuffle sequences in stream.
2163 my ( $in, # handle to in stream
2164 $out, # handle to out stream
2171 while ( $record = get_record( $in ) )
2173 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2175 put_record( $record, $out );
2180 sub script_analyze_seq
2182 # Martin A. Hansen, August 2007.
2184 # Analyze sequence composition of sequences in stream.
2186 my ( $in, # handle to in stream
2187 $out, # handle to out stream
2192 my ( $record, $analysis );
2194 while ( $record = get_record( $in ) )
2196 if ( $record->{ "SEQ" } )
2198 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2200 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2203 put_record( $record, $out );
2208 sub script_analyze_tags
2210 # Martin A. Hansen, August 2008.
2212 # Analyze sequence tags in stream.
2214 my ( $in, # handle to in stream
2215 $out, # handle to out stream
2220 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2222 while ( $record = get_record( $in ) )
2224 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2226 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2230 $len_hash{ length( $record->{ "SEQ" } ) }++;
2231 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2234 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2236 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2240 $len_hash{ $record->{ "BED_LEN" } }++;
2241 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2246 foreach $key ( sort { $a <=> $b } keys %len_hash )
2248 $tag_record->{ "TAG_LEN" } = $key;
2249 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2250 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2252 put_record( $tag_record, $out );
2257 sub script_complexity_seq
2259 # Martin A. Hansen, May 2008.
2261 # Generates an index calculated as the most common di-residue over
2262 # the sequence length for all sequences in stream.
2264 my ( $in, # handle to in stream
2265 $out, # handle to out stream
2270 my ( $record, $index );
2272 while ( $record = get_record( $in ) )
2274 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2276 put_record( $record, $out );
2281 sub script_oligo_freq
2283 # Martin A. Hansen, August 2007.
2285 # Determine the length of sequences in stream.
2287 my ( $in, # handle to in stream
2288 $out, # handle to out stream
2289 $options, # options hash
2294 my ( $record, %oligos, @freq_table );
2296 $options->{ "word_size" } ||= 7;
2298 while ( $record = get_record( $in ) )
2300 if ( $record->{ "SEQ" } )
2302 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2304 if ( not $options->{ "all" } )
2306 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2308 map { put_record( $_, $out ) } @freq_table;
2314 put_record( $record, $out );
2317 if ( $options->{ "all" } )
2319 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2321 map { put_record( $_, $out ) } @freq_table;
2326 sub script_create_weight_matrix
2328 # Martin A. Hansen, August 2007.
2330 # Creates a weight matrix from an alignmnet.
2332 my ( $in, # handle to in stream
2333 $out, # handle to out stream
2334 $options, # options hash
2339 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2343 while ( $record = get_record( $in ) )
2345 if ( $record->{ "SEQ" } )
2347 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2349 $res = substr $record->{ "SEQ" }, $i, 1;
2351 $freq_hash{ $i }{ $res }++;
2352 $res_hash{ $res } = 1;
2359 put_record( $record, $out );
2363 foreach $res ( sort keys %res_hash )
2367 $record->{ "V0" } = $res;
2369 for ( $i = 0; $i < keys %freq_hash; $i++ )
2371 $freq = $freq_hash{ $i }{ $res } || 0;
2373 if ( $options->{ "percent" } ) {
2374 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2377 $record->{ "V" . ( $i + 1 ) } = $freq;
2380 put_record( $record, $out );
2385 sub script_calc_bit_scores
2387 # Martin A. Hansen, March 2007.
2389 # Calculates the bit scores for each position from an alignmnet in the stream.
2391 my ( $in, # handle to in stream
2392 $out, # handle to out stream
2397 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2401 while ( $record = get_record( $in ) )
2403 if ( $record->{ "SEQ" } )
2405 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2407 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2409 $res = substr $record->{ "SEQ" }, $i, 1;
2411 next if $res =~ /-|_|~|\./;
2413 $freq_hash{ $i }{ $res }++;
2420 put_record( $record, $out );
2426 if ( $type eq "protein" ) {
2432 for ( $i = 0; $i < keys %freq_hash; $i++ )
2434 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2436 $bit_diff = $bit_max - $bit_height;
2438 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2441 put_record( $record, $out );
2445 sub script_reverse_seq
2447 # Martin A. Hansen, August 2007.
2449 # Reverse sequence in record.
2451 my ( $in, # handle to in stream
2452 $out, # handle to out stream
2459 while ( $record = get_record( $in ) )
2461 if ( $record->{ "SEQ" } ) {
2462 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2465 put_record( $record, $out );
2470 sub script_complement_seq
2472 # Martin A. Hansen, August 2007.
2474 # Complement sequence in record.
2476 my ( $in, # handle to in stream
2477 $out, # handle to out stream
2482 my ( $record, $type );
2484 while ( $record = get_record( $in ) )
2486 if ( $record->{ "SEQ" } )
2489 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2492 if ( $type eq "rna" ) {
2493 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2494 } elsif ( $type eq "dna" ) {
2495 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2499 put_record( $record, $out );
2504 sub script_remove_indels
2506 # Martin A. Hansen, August 2007.
2508 # Remove indels from sequences in stream.
2510 my ( $in, # handle to in stream
2511 $out, # handle to out stream
2518 while ( $record = get_record( $in ) )
2520 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2522 put_record( $record, $out );
2527 sub script_transliterate_seq
2529 # Martin A. Hansen, August 2007.
2531 # Transliterate chars from sequence in record.
2533 my ( $in, # handle to in stream
2534 $out, # handle to out stream
2535 $options, # options hash
2540 my ( $record, $search, $replace, $delete );
2542 $search = $options->{ "search" } || "";
2543 $replace = $options->{ "replace" } || "";
2544 $delete = $options->{ "delete" } || "";
2546 while ( $record = get_record( $in ) )
2548 if ( $record->{ "SEQ" } )
2550 if ( $search and $replace ) {
2551 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2552 } elsif ( $delete ) {
2553 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2557 put_record( $record, $out );
2562 sub script_transliterate_vals
2564 # Martin A. Hansen, April 2008.
2566 # Transliterate chars from values in record.
2568 my ( $in, # handle to in stream
2569 $out, # handle to out stream
2570 $options, # options hash
2575 my ( $record, $search, $replace, $delete, $key );
2577 $search = $options->{ "search" } || "";
2578 $replace = $options->{ "replace" } || "";
2579 $delete = $options->{ "delete" } || "";
2581 while ( $record = get_record( $in ) )
2583 foreach $key ( @{ $options->{ "keys" } } )
2585 if ( exists $record->{ $key } )
2587 if ( $search and $replace ) {
2588 eval "\$record->{ $key } =~ tr/$search/$replace/";
2589 } elsif ( $delete ) {
2590 eval "\$record->{ $key } =~ tr/$delete//d";
2595 put_record( $record, $out );
2600 sub script_translate_seq
2602 # Martin A. Hansen, February 2008.
2604 # Translate DNA sequence into protein sequence.
2606 my ( $in, # handle to in stream
2607 $out, # handle to out stream
2608 $options, # options hash
2613 my ( $record, $frame, %new_record );
2615 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2617 while ( $record = get_record( $in ) )
2619 if ( $record->{ "SEQ" } )
2621 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2623 foreach $frame ( @{ $options->{ "frames" } } )
2625 %new_record = %{ $record };
2627 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2628 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2629 $new_record{ "FRAME" } = $frame;
2631 put_record( \%new_record, $out );
2637 put_record( $record, $out );
2643 sub script_extract_seq
2645 # Martin A. Hansen, August 2007.
2647 # Extract subsequences from sequences in record.
2649 my ( $in, # handle to in stream
2650 $out, # handle to out stream
2651 $options, # options hash
2656 my ( $beg, $end, $len, $record );
2658 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2661 $beg = $options->{ "beg" } - 1; # correcting for start offset
2664 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2666 } elsif ( defined $options->{ "end" } ) {
2667 $end = $options->{ "end" } - 1; # correcting for start offset
2670 $len = $options->{ "len" };
2672 # print "beg->$beg, end->$end, len->$len\n";
2674 while ( $record = get_record( $in ) )
2676 if ( $record->{ "SEQ" } )
2678 if ( defined $beg and defined $end )
2680 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2681 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2683 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2686 elsif ( defined $beg and defined $len )
2688 if ( $len > length $record->{ "SEQ" } ) {
2689 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2691 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2694 elsif ( defined $beg )
2696 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2700 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2702 put_record( $record, $out );
2707 sub script_get_genome_seq
2709 # Martin A. Hansen, December 2007.
2711 # Gets a subsequence from a genome.
2713 my ( $in, # handle to in stream
2714 $out, # handle to out stream
2715 $options, # options hash
2720 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2722 $options->{ "flank" } ||= 0;
2724 if ( $options->{ "genome" } )
2726 $genome = $options->{ "genome" };
2728 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2729 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2731 $fh = Maasha::Common::read_open( $genome_file );
2732 $index = Maasha::Fasta::index_retrieve( $index_file );
2734 shift @{ $index }; # Get rid of the file size info
2736 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2738 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2740 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2742 $beg = $index_beg + $options->{ "beg" } - 1;
2744 if ( $options->{ "len" } ) {
2745 $len = $options->{ "len" };
2746 } elsif ( $options->{ "end" } ) {
2747 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2750 $beg -= $options->{ "flank" };
2751 $len += 2 * $options->{ "flank" };
2753 if ( $beg <= $index_beg )
2755 $len -= $index_beg - $beg;
2759 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2761 next if $beg > $index_beg + $index_len;
2763 $record->{ "CHR" } = $options->{ "chr" };
2764 $record->{ "CHR_BEG" } = $beg - $index_beg;
2765 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2767 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2768 $record->{ "SEQ_LEN" } = $len;
2770 put_record( $record, $out );
2774 while ( $record = get_record( $in ) )
2776 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2778 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2780 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2782 $beg = $record->{ "CHR_BEG" } + $index_beg;
2783 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2785 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2787 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2789 $beg = $record->{ "S_BEG" } + $index_beg;
2790 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2792 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2794 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2796 $beg = $record->{ "S_BEG" } + $index_beg;
2797 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2800 $beg -= $options->{ "flank" };
2801 $len += 2 * $options->{ "flank" };
2803 if ( $beg <= $index_beg )
2805 $len -= $index_beg - $beg;
2809 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2811 next if $beg > $index_beg + $index_len;
2813 $record->{ "CHR_BEG" } = $beg - $index_beg;
2814 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2816 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2818 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2820 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2821 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2824 if ( $options->{ "mask" } )
2826 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2828 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2830 @begs = split ",", $record->{ "Q_BEGS" };
2831 @lens = split ",", $record->{ "BLOCKSIZES" };
2833 for ( $i = 0; $i < @begs; $i++ ) {
2834 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2840 put_record( $record, $out );
2847 sub script_get_genome_align
2849 # Martin A. Hansen, April 2008.
2851 # Gets a subalignment from a multiple genome alignment.
2853 my ( $in, # handle to in stream
2854 $out, # handle to out stream
2855 $options, # options hash
2860 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2862 $options->{ "strand" } ||= "+";
2866 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2868 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2870 $beg = $options->{ "beg" } - 1;
2872 if ( $options->{ "end" } ) {
2873 $end = $options->{ "end" };
2874 } elsif ( $options->{ "len" } ) {
2875 $end = $beg + $options->{ "len" };
2878 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2880 foreach $entry ( @{ $align } )
2882 $entry->{ "CHR" } = $record->{ "CHR" };
2883 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2884 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2885 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2886 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2887 $entry->{ "SCORE" } = $record->{ "SCORE" };
2889 put_record( $entry, $out );
2893 while ( $record = get_record( $in ) )
2895 if ( $record->{ "REC_TYPE" } eq "BED" )
2897 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2899 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2901 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2903 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2905 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2908 foreach $entry ( @{ $align } )
2910 $entry->{ "CHR" } = $record->{ "CHR" };
2911 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2912 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2913 $entry->{ "STRAND" } = $record->{ "STRAND" };
2914 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2915 $entry->{ "SCORE" } = $record->{ "SCORE" };
2917 put_record( $entry, $out );
2925 sub script_get_genome_phastcons
2927 # Martin A. Hansen, February 2008.
2929 # Get phastcons scores from genome intervals.
2931 my ( $in, # handle to in stream
2932 $out, # handle to out stream
2933 $options, # options hash
2938 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2940 $options->{ "flank" } ||= 0;
2942 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2943 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2945 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2946 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2948 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2950 $options->{ "beg" } -= 1; # request is 1-based
2951 $options->{ "end" } -= 1; # request is 1-based
2953 if ( $options->{ "len" } ) {
2954 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2957 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2959 $record->{ "CHR" } = $options->{ "chr" };
2960 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2961 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2963 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2964 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2966 put_record( $record, $out );
2969 while ( $record = get_record( $in ) )
2971 if ( $record->{ "REC_TYPE" } eq "BED" )
2973 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2975 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2977 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2979 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2981 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2984 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2985 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2987 put_record( $record, $out );
2990 close $fh_phastcons if $fh_phastcons;
2996 # Martin A. Hansen, December 2007.
2998 # Folds sequences in stream into secondary structures.
3000 my ( $in, # handle to in stream
3001 $out, # handle to out stream
3006 my ( $record, $type, $struct, $index );
3008 while ( $record = get_record( $in ) )
3010 if ( $record->{ "SEQ" } )
3013 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3016 if ( $type ne "protein" )
3018 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3019 $record->{ "SEC_STRUCT" } = $struct;
3020 $record->{ "FREE_ENERGY" } = $index;
3021 $record->{ "SCORE" } = abs int $index;
3022 $record->{ "SIZE" } = length $struct;
3023 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3027 put_record( $record, $out );
3032 sub script_split_seq
3034 # Martin A. Hansen, August 2007.
3036 # Split a sequence in stream into words.
3038 my ( $in, # handle to in stream
3039 $out, # handle to out stream
3040 $options, # options hash
3045 my ( $record, $new_record, $i, $subseq, %lookup );
3047 $options->{ "word_size" } ||= 7;
3049 while ( $record = get_record( $in ) )
3051 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3053 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3055 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3057 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3059 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3060 $new_record->{ "SEQ" } = $subseq;
3062 put_record( $new_record, $out );
3064 $lookup{ $subseq } = 1;
3068 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3069 $new_record->{ "SEQ" } = $subseq;
3071 put_record( $new_record, $out );
3077 put_record( $record, $out );
3083 sub script_split_bed
3085 # Martin A. Hansen, June 2008.
3087 # Split a BED record into overlapping windows.
3089 my ( $in, # handle to in stream
3090 $out, # handle to out stream
3091 $options, # options hash
3096 my ( $record, $new_record, $i );
3098 $options->{ "window_size" } ||= 20;
3099 $options->{ "step_size" } ||= 1;
3101 while ( $record = get_record( $in ) )
3103 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3105 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3107 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3109 $new_record->{ "REC_TYPE" } = "BED";
3110 $new_record->{ "CHR" } = $record->{ "CHR" };
3111 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3112 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3113 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3114 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3115 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3116 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3118 put_record( $new_record, $out );
3123 put_record( $record, $out );
3129 sub script_align_seq
3131 # Martin A. Hansen, August 2007.
3133 # Align sequences in stream.
3135 my ( $in, # handle to in stream
3136 $out, # handle to out stream
3141 my ( $record, @entries, $entry );
3143 while ( $record = get_record( $in ) )
3145 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3146 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3147 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3148 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3150 put_record( $record, $out );
3154 @entries = Maasha::Align::align( \@entries );
3156 foreach $entry ( @entries )
3158 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3161 SEQ_NAME => $entry->[ SEQ_NAME ],
3162 SEQ => $entry->[ SEQ ],
3165 put_record( $record, $out );
3173 # Martin A. Hansen, February 2008.
3175 # Using the first sequence in stream as reference, tile
3176 # all subsequent sequences based on pairwise alignments.
3178 my ( $in, # handle to in stream
3179 $out, # handle to out stream
3180 $options, # options hash
3185 my ( $record, $first, $ref_entry, @entries );
3189 while ( $record = get_record( $in ) )
3191 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3195 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3201 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3206 put_record( $record, $out );
3210 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3212 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3216 sub script_invert_align
3218 # Martin A. Hansen, February 2008.
3220 # Inverts an alignment showing only non-mathing residues
3221 # using the first sequence as reference.
3223 my ( $in, # handle to in stream
3224 $out, # handle to out stream
3225 $options, # options hash
3230 my ( $record, @entries );
3232 while ( $record = get_record( $in ) )
3234 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3236 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3240 put_record( $record, $out );
3244 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3246 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3250 sub script_patscan_seq
3252 # Martin A. Hansen, August 2007.
3254 # Locates patterns in sequences using scan_for_matches.
3256 my ( $in, # handle to in stream
3257 $out, # handle to out stream
3258 $options, # options hash
3263 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3265 if ( $options->{ "patterns" } ) {
3266 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3267 } elsif ( -f $options->{ "patterns_in" } ) {
3268 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3271 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3273 push @args, "-c" if $options->{ "comp" };
3274 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3275 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3277 $seq_file = "$BP_TMP/patscan.seq";
3278 $pat_file = "$BP_TMP/patscan.pat";
3279 $out_file = "$BP_TMP/patscan.out";
3281 $fh_out = Maasha::Common::write_open( $seq_file );
3285 while ( $record = get_record( $in ) )
3287 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3289 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3291 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3293 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3298 # put_record( $record, $out );
3303 $arg = join " ", @args;
3304 $arg .= " -p" if $type eq "protein";
3306 foreach $pattern ( @{ $patterns } )
3308 $fh_out = Maasha::Common::write_open( $pat_file );
3310 print $fh_out "$pattern\n";
3314 if ( $options->{ 'genome' } ) {
3315 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3316 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3318 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3319 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3322 $fh_in = Maasha::Common::read_open( $out_file );
3324 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3326 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3328 if ( $options->{ 'genome' } )
3330 $result->{ "CHR" } = $result->{ "S_ID" };
3331 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3332 $result->{ "CHR_END" } = $result->{ "S_END" };
3334 delete $result->{ "S_ID" };
3335 delete $result->{ "S_BEG" };
3336 delete $result->{ "S_END" };
3340 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3343 put_record( $result, $out );
3355 sub script_create_blast_db
3357 # Martin A. Hansen, September 2007.
3359 # Creates a NCBI BLAST database with formatdb
3361 my ( $in, # handle to in stream
3362 $out, # handle to out stream
3363 $options, # options hash
3368 my ( $fh, $seq_type, $path, $record );
3370 $path = $options->{ "database" };
3372 $fh = Maasha::Common::write_open( $path );
3374 while ( $record = get_record( $in ) )
3376 put_record( $record, $out ) if not $options->{ "no_stream" };
3378 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3380 $seq_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3382 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3388 if ( $seq_type eq "protein" ) {
3389 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3391 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3398 sub script_blast_seq
3400 # Martin A. Hansen, September 2007.
3402 # BLASTs sequences in stream against a given database.
3404 my ( $in, # handle to in stream
3405 $out, # handle to out stream
3406 $options, # options hash
3411 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3413 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3414 $options->{ "filter" } = "F";
3415 $options->{ "filter" } = "T" if $options->{ "filter" };
3416 $options->{ "cpus" } ||= 1;
3418 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3420 $tmp_in = "$BP_TMP/blast_query.seq";
3421 $tmp_out = "$BP_TMP/blast.result";
3423 $fh_out = Maasha::Common::write_open( $tmp_in );
3425 while ( $record = get_record( $in ) )
3427 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3429 $q_type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3431 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3434 put_record( $record, $out );
3439 if ( -f $options->{ 'database' } . ".phr" ) {
3440 $s_type = "protein";
3442 $s_type = "nucleotide";
3445 if ( not $options->{ 'program' } )
3447 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3448 $options->{ 'program' } = "blastn";
3449 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3450 $options->{ 'program' } = "blastp";
3451 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3452 $options->{ 'program' } = "blastx";
3453 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3454 $options->{ 'program' } = "tblastn";
3458 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 );
3462 $fh_out = Maasha::Common::read_open( $tmp_out );
3466 while ( $line = <$fh_out> )
3470 next if $line =~ /^#/;
3472 @fields = split /\s+/, $line;
3474 $record->{ "REC_TYPE" } = "BLAST";
3475 $record->{ "Q_ID" } = $fields[ 0 ];
3476 $record->{ "S_ID" } = $fields[ 1 ];
3477 $record->{ "IDENT" } = $fields[ 2 ];
3478 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3479 $record->{ "MISMATCHES" } = $fields[ 4 ];
3480 $record->{ "GAPS" } = $fields[ 5 ];
3481 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3482 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3483 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3484 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3485 $record->{ "E_VAL" } = $fields[ 10 ];
3486 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3488 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3490 $record->{ "STRAND" } = '-';
3492 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3496 $record->{ "STRAND" } = '+';
3499 put_record( $record, $out );
3510 # Martin A. Hansen, August 2007.
3512 # BLATs sequences in stream against a given genome.
3514 my ( $in, # handle to in stream
3515 $out, # handle to out stream
3516 $options, # options hash
3521 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3523 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3525 $options->{ 'tile_size' } ||= 11;
3526 $options->{ 'one_off' } ||= 0;
3527 $options->{ 'min_identity' } ||= 90;
3528 $options->{ 'min_score' } ||= 0;
3529 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3531 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3532 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3533 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3534 $blat_args .= " -minScore=$options->{ 'min_score' }";
3535 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3536 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3538 $query_file = "$BP_TMP/blat.seq";
3540 $fh_out = Maasha::Common::write_open( $query_file );
3542 while ( $record = get_record( $in ) )
3544 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3546 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3547 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3550 put_record( $record, $out );
3555 $blat_args .= " -t=dnax" if $type eq "protein";
3556 $blat_args .= " -q=$type";
3558 $result_file = "$BP_TMP/blat.psl";
3560 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3564 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3566 map { put_record( $_, $out ) } @{ $entries };
3568 unlink $result_file;
3574 # Martin A. Hansen, July 2008.
3576 # soap sequences in stream against a given file or genome.
3578 my ( $in, # handle to in stream
3579 $out, # handle to out stream
3580 $options, # options hash
3585 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3587 $options->{ "cpus" } ||= 1;
3589 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3591 $tmp_in = "$BP_TMP/soap_query.seq";
3592 $tmp_out = "$BP_TMP/soap.result";
3594 $fh_out = Maasha::Common::write_open( $tmp_in );
3596 while ( $record = get_record( $in ) )
3598 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3599 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3602 put_record( $record, $out );
3607 Maasha::Common::run( "soap", "-r 2 -a $tmp_in -p $options->{ 'cpus' } -d $options->{ 'in_file' } -o $tmp_out > /dev/null 2>&1", 1 );
3611 $fh_out = Maasha::Common::read_open( $tmp_out );
3615 while ( $line = <$fh_out> )
3619 @fields = split /\t/, $line;
3621 $record->{ "REC_TYPE" } = "SOAP";
3622 $record->{ "Q_ID" } = $fields[ 0 ];
3623 $record->{ "SCORE" } = $fields[ 3 ];
3624 $record->{ "STRAND" } = $fields[ 6 ];
3625 $record->{ "S_ID" } = $fields[ 7 ];
3626 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is one based
3627 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3629 put_record( $record, $out );
3638 sub script_match_seq
3640 # Martin A. Hansen, August 2007.
3642 # BLATs sequences in stream against a given genome.
3644 my ( $in, # handle to in stream
3645 $out, # handle to out stream
3646 $options, # options hash
3651 my ( $record, @entries, $results );
3653 $options->{ "word_size" } ||= 20;
3654 $options->{ "direction" } ||= "both";
3656 while ( $record = get_record( $in ) )
3658 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3659 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3662 put_record( $record, $out );
3665 if ( @entries == 1 )
3667 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3669 map { put_record( $_, $out ) } @{ $results };
3671 elsif ( @entries == 2 )
3673 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3675 map { put_record( $_, $out ) } @{ $results };
3680 sub script_create_vmatch_index
3682 # Martin A. Hansen, January 2008.
3684 # Create a vmatch index from sequences in the stream.
3686 my ( $in, # handle to in stream
3687 $out, # handle to out stream
3688 $options, # options hash
3693 my ( $record, $file_tmp, $fh_tmp, $type );
3695 if ( $options->{ "index_name" } )
3697 $file_tmp = $options->{ 'index_name' };
3698 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3701 while ( $record = get_record( $in ) )
3703 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3705 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3707 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3710 put_record( $record, $out ) if not $options->{ "no_stream" };
3713 if ( $options->{ "index_name" } )
3717 if ( $type eq "protein" ) {
3718 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3720 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3728 sub script_vmatch_seq
3730 # Martin A. Hansen, August 2007.
3732 # Vmatches sequences in stream against a given genome.
3734 my ( $in, # handle to in stream
3735 $out, # handle to out stream
3736 $options, # options hash
3741 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3743 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3745 if ( $options->{ "index_name" } )
3747 @index_files = $options->{ "index_name" };
3751 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3753 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3755 @index_files = sort keys %hash;
3758 while ( $record = get_record( $in ) )
3760 push @records, $record;
3762 put_record( $record, $out );
3765 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3769 $fh_in = Maasha::Common::read_open( $result_file );
3771 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3772 put_record( $record, $out );
3777 unlink $result_file;
3781 sub script_write_fasta
3783 # Martin A. Hansen, August 2007.
3785 # Write FASTA entries from sequences in stream.
3787 my ( $in, # handle to in stream
3788 $out, # handle to out stream
3789 $options, # options hash
3794 my ( $record, $fh );
3796 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3798 while ( $record = get_record( $in ) )
3800 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3801 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3804 put_record( $record, $out ) if not $options->{ "no_stream" };
3811 sub script_write_align
3813 # Martin A. Hansen, August 2007.
3815 # Write pretty alignments aligned sequences in stream.
3817 my ( $in, # handle to in stream
3818 $out, # handle to out stream
3819 $options, # options hash
3824 my ( $fh, $record, @entries );
3826 $fh = write_stream( $options->{ "data_out" } ) ;
3828 while ( $record = get_record( $in ) )
3830 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3831 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3834 put_record( $record, $out ) if not $options->{ "no_stream" };
3837 if ( scalar( @entries ) == 2 ) {
3838 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3839 } elsif ( scalar ( @entries ) > 2 ) {
3840 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3847 sub script_write_blast
3849 # Martin A. Hansen, November 2007.
3851 # Write data in blast table format (-m8 and 9).
3853 my ( $in, # handle to in stream
3854 $out, # handle to out stream
3855 $options, # options hash
3860 my ( $fh, $record, $first );
3862 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3866 while ( $record = get_record( $in ) )
3868 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3870 if ( $options->{ "comment" } and $first )
3872 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";
3877 if ( $record->{ "STRAND" } eq "-" ) {
3878 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3881 print $fh join( "\t",
3882 $record->{ "Q_ID" },
3883 $record->{ "S_ID" },
3884 $record->{ "IDENT" },
3885 $record->{ "ALIGN_LEN" },
3886 $record->{ "MISMATCHES" },
3887 $record->{ "GAPS" },
3888 $record->{ "Q_BEG" } + 1,
3889 $record->{ "Q_END" } + 1,
3890 $record->{ "S_BEG" } + 1,
3891 $record->{ "S_END" } + 1,
3892 $record->{ "E_VAL" },
3893 $record->{ "BIT_SCORE" }
3897 put_record( $record, $out ) if not $options->{ "no_stream" };
3904 sub script_write_tab
3906 # Martin A. Hansen, August 2007.
3908 # Write data as table.
3910 my ( $in, # handle to in stream
3911 $out, # handle to out stream
3912 $options, # options hash
3917 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3919 $options->{ "delimit" } ||= "\t";
3921 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3923 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3925 while ( $record = get_record( $in ) )
3930 if ( $options->{ "keys" } )
3932 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3936 foreach $key ( @{ $options->{ "keys" } } )
3938 if ( exists $record->{ $key } )
3940 push @keys, $key if $options->{ "comment" };
3941 push @vals, $record->{ $key };
3948 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3950 next if exists $no_keys{ $key };
3952 push @keys, $key if $options->{ "comment" };
3953 push @vals, $record->{ $key };
3957 if ( @keys and $options->{ "comment" } )
3959 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3961 delete $options->{ "comment" };
3964 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3966 put_record( $record, $out ) if not $options->{ "no_stream" };
3973 sub script_write_bed
3975 # Martin A. Hansen, August 2007.
3977 # Write BED format for the UCSC genome browser using records in stream.
3979 my ( $in, # handle to in stream
3980 $out, # handle to out stream
3981 $options, # options hash
3986 my ( $fh, $record, $new_record );
3988 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3990 while ( $record = get_record( $in ) )
3992 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3994 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3996 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3998 $new_record->{ "CHR" } = $record->{ "S_ID" };
3999 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4000 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4001 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4002 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4003 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4005 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4007 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4009 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4011 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4013 $new_record->{ "CHR" } = $record->{ "S_ID" };
4014 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4015 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4016 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4017 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4018 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4020 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4022 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4024 $new_record->{ "CHR" } = $record->{ "S_ID" };
4025 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4026 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4027 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4028 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4029 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4031 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4033 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4035 $new_record->{ "CHR" } = $record->{ "S_ID" };
4036 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4037 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4038 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4039 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4040 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4042 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4044 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4046 Maasha::UCSC::bed_put_entry( $record, $fh );
4049 put_record( $record, $out ) if not $options->{ "no_stream" };
4056 sub script_write_psl
4058 # Martin A. Hansen, August 2007.
4060 # Write PSL output from stream.
4062 my ( $in, # handle to in stream
4063 $out, # handle to out stream
4064 $options, # options hash
4069 my ( $fh, $record, @output, $first );
4073 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4075 while ( $record = get_record( $in ) )
4077 put_record( $record, $out ) if not $options->{ "no_stream" };
4079 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4081 Maasha::UCSC::psl_put_header( $fh ) if $first;
4082 Maasha::UCSC::psl_put_entry( $record, $fh );
4091 sub script_write_fixedstep
4093 # Martin A. Hansen, Juli 2008.
4095 # Write fixedStep entries from recrods in the stream.
4097 my ( $in, # handle to in stream
4098 $out, # handle to out stream
4099 $options, # options hash
4104 my ( $fh, $record, $vals );
4106 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4108 while ( $record = get_record( $in ) )
4110 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4112 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4114 $vals = $record->{ 'VALS' };
4118 print $fh "$vals\n";
4121 put_record( $record, $out ) if not $options->{ "no_stream" };
4128 sub script_write_2bit
4130 # Martin A. Hansen, March 2008.
4132 # Write sequence entries from stream in 2bit format.
4134 my ( $in, # handle to in stream
4135 $out, # handle to out stream
4136 $options, # options hash
4141 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
4143 $mask = 1 if not $options->{ "no_mask" };
4145 $tmp_file = "$BP_TMP/write_2bit.fna";
4146 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4148 $fh_out = write_stream( $options->{ "data_out" } );
4150 while ( $record = get_record( $in ) )
4152 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4153 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
4156 put_record( $record, $out ) if not $options->{ "no_stream" };
4161 $fh_in = Maasha::Common::read_open( $tmp_file );
4163 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4172 sub script_write_solid
4174 # Martin A. Hansen, April 2008.
4176 # Write di-base encoded Solid sequence from entries in stream.
4178 my ( $in, # handle to in stream
4179 $out, # handle to out stream
4180 $options, # options hash
4185 my ( $record, $fh, $seq_cs );
4187 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4189 while ( $record = get_record( $in ) )
4191 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
4193 $seq_cs = Maasha::Solid::seq2color_space( uc $record->{ "SEQ" } );
4195 Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
4198 put_record( $record, $out ) if not $options->{ "no_stream" };
4205 sub script_plot_seqlogo
4207 # Martin A. Hansen, August 2007.
4209 # Calculates and writes a sequence logo for alignments.
4211 my ( $in, # handle to in stream
4212 $out, # handle to out stream
4213 $options, # options hash
4218 my ( $record, @entries, $logo, $fh );
4220 while ( $record = get_record( $in ) )
4222 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4223 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4226 put_record( $record, $out ) if not $options->{ "no_stream" };
4229 $logo = Maasha::Plot::seq_logo( \@entries );
4231 $fh = write_stream( $options->{ "data_out" } );
4239 sub script_plot_phastcons_profiles
4241 # Martin A. Hansen, January 2008.
4243 # Plots PhastCons profiles.
4245 my ( $in, # handle to in stream
4246 $out, # handle to out stream
4247 $options, # options hash
4252 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4254 $options->{ "title" } ||= "PhastCons Profiles";
4256 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4257 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4259 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4260 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4262 while ( $record = get_record( $in ) )
4264 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4266 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4268 push @{ $AoA }, [ @{ $scores } ];
4271 put_record( $record, $out ) if not $options->{ "no_stream" };
4274 Maasha::UCSC::phastcons_normalize( $AoA );
4276 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4277 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4279 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4281 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4283 $fh = write_stream( $options->{ "data_out" } );
4285 print $fh "$_\n" foreach @{ $plot };
4291 sub script_analyze_bed
4293 # Martin A. Hansen, March 2008.
4295 # Analyze BED entries in stream.
4297 my ( $in, # handle to in stream
4298 $out, # handle to out stream
4299 $options, # options hash
4306 while ( $record = get_record( $in ) )
4308 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4310 put_record( $record, $out );
4315 sub script_analyze_vals
4317 # Martin A. Hansen, August 2007.
4319 # Analyze values for given keys in stream.
4321 my ( $in, # handle to in stream
4322 $out, # handle to out stream
4323 $options, # options hash
4328 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4330 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4332 while ( $record = get_record( $in ) )
4334 foreach $key ( keys %{ $record } )
4336 next if $options->{ "keys" } and not exists $key_hash{ $key };
4338 $analysis->{ $key }->{ "COUNT" }++;
4340 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4342 $analysis->{ $key }->{ "TYPE" } = "num";
4343 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4344 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4345 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4349 $len = length $record->{ $key };
4351 $analysis->{ $key }->{ "TYPE" } = "alph";
4352 $analysis->{ $key }->{ "SUM" } += $len;
4353 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4354 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4358 put_record( $record, $out ) if not $options->{ "no_stream" };
4361 foreach $key ( keys %{ $analysis } )
4363 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4364 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4367 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4377 if ( $options->{ "keys" } ) {
4378 @keys = @{ $options->{ "keys" } };
4380 @keys = keys %{ $analysis };
4383 foreach $key ( @keys )
4385 $keys .= sprintf "% 15s", $key;
4386 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4387 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4388 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4389 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4390 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4391 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4394 print $out "$keys\n";
4395 print $out "$types\n";
4396 print $out "$counts\n";
4397 print $out "$mins\n";
4398 print $out "$maxs\n";
4399 print $out "$sums\n";
4400 print $out "$means\n";
4404 sub script_head_records
4406 # Martin A. Hansen, August 2007.
4408 # Display the first sequences in stream.
4410 my ( $in, # handle to in stream
4411 $out, # handle to out stream
4412 $options, # options hash
4417 my ( $record, $count );
4419 $options->{ "num" } ||= 10;
4423 while ( $record = get_record( $in ) )
4427 put_record( $record, $out );
4429 last if $count == $options->{ "num" };
4434 sub script_remove_keys
4436 # Martin A. Hansen, August 2007.
4438 # Remove keys from stream.
4440 my ( $in, # handle to in stream
4441 $out, # handle to out stream
4442 $options, # options hash
4447 my ( $record, $new_record );
4449 while ( $record = get_record( $in ) )
4451 if ( $options->{ "keys" } )
4453 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4455 elsif ( $options->{ "save_keys" } )
4457 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4459 $record = $new_record;
4462 put_record( $record, $out ) if keys %{ $record };
4467 sub script_rename_keys
4469 # Martin A. Hansen, August 2007.
4471 # Rename keys in stream.
4473 my ( $in, # handle to in stream
4474 $out, # handle to out stream
4475 $options, # options hash
4482 while ( $record = get_record( $in ) )
4484 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4486 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4488 delete $record->{ $options->{ "keys" }->[ 0 ] };
4491 put_record( $record, $out );
4496 sub script_uniq_vals
4498 # Martin A. Hansen, August 2007.
4500 # Find unique values in stream.
4502 my ( $in, # handle to in stream
4503 $out, # handle to out stream
4504 $options, # options hash
4509 my ( %hash, $record );
4511 while ( $record = get_record( $in ) )
4513 if ( $record->{ $options->{ "key" } } )
4515 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4517 put_record( $record, $out );
4519 $hash{ $record->{ $options->{ "key" } } } = 1;
4521 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4523 put_record( $record, $out );
4527 $hash{ $record->{ $options->{ "key" } } } = 1;
4532 put_record( $record, $out );
4538 sub script_merge_vals
4540 # Martin A. Hansen, August 2007.
4542 # Rename keys in stream.
4544 my ( $in, # handle to in stream
4545 $out, # handle to out stream
4546 $options, # options hash
4551 my ( $record, @join, $i );
4553 $options->{ "delimit" } ||= '_';
4555 while ( $record = get_record( $in ) )
4557 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4559 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4561 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4562 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4565 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4568 put_record( $record, $out );
4575 # Martin A. Hansen, August 2007.
4577 # Grab for records in stream.
4579 my ( $in, # handle to in stream
4580 $out, # handle to out stream
4581 $options, # options hash
4586 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4588 if ( $options->{ "patterns" } )
4590 $patterns = [ split ",", $options->{ "patterns" } ];
4592 elsif ( -f $options->{ "patterns_in" } )
4594 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4596 elsif ( -f $options->{ "exact_in" } )
4598 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4600 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4605 if ( $options->{ "eval" } )
4607 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4615 while ( $record = get_record( $in ) )
4621 if ( $options->{ "keys" } )
4623 foreach $key ( @{ $options->{ "keys" } } )
4625 if ( exists $lookup_hash{ $record->{ $key } } )
4634 foreach $key ( keys %{ $record } )
4636 if ( not $options->{ "vals_only" } )
4638 if ( exists $lookup_hash{ $key } )
4645 if ( not $options->{ "keys_only" } )
4647 if ( exists $lookup_hash{ $record->{ $key } } )
4658 foreach $pattern ( @{ $patterns } )
4660 if ( $options->{ "keys" } )
4662 foreach $key ( @{ $options->{ "keys" } } )
4664 $pos = index $record->{ $key }, $pattern;
4666 goto FOUND if $pos >= 0;
4671 foreach $key ( keys %{ $record } )
4673 if ( not $options->{ "vals_only" } )
4675 $pos = index $key, $pattern;
4677 goto FOUND if $pos >= 0;
4680 if ( not $options->{ "keys_only" } )
4682 $pos = index $record->{ $key }, $pattern;
4684 goto FOUND if $pos >= 0;
4690 elsif ( $options->{ "regex" } )
4692 if ( $options->{ "keys" } )
4694 foreach $key ( @{ $options->{ "keys" } } )
4696 if ( $options->{ "case_insensitive" } ) {
4697 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4699 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4702 goto FOUND if $pos >= 0;
4707 foreach $key ( keys %{ $record } )
4709 if ( not $options->{ "vals_only" } )
4711 if ( $options->{ "case_insensitive" } ) {
4712 $pos = 1 if $key =~ /$options->{'regex'}/i;
4714 $pos = 1 if $key =~ /$options->{'regex'}/;
4717 goto FOUND if $pos >= 0;
4720 if ( not $options->{ "keys_only" } )
4722 if ( $options->{ "case_insensitive" } ) {
4723 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4725 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4728 goto FOUND if $pos >= 0;
4733 elsif ( $options->{ "eval" } )
4735 if ( defined $record->{ $key } )
4737 if ( $op eq "<" and $record->{ $key } < $val ) {
4738 $pos = 1 and goto FOUND;
4739 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4740 $pos = 1 and goto FOUND;
4741 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4742 $pos = 1 and goto FOUND;
4743 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4744 $pos = 1 and goto FOUND;
4745 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4746 $pos = 1 and goto FOUND;
4747 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4748 $pos = 1 and goto FOUND;
4749 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4750 $pos = 1 and goto FOUND;
4751 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4752 $pos = 1 and goto FOUND;
4759 if ( $pos >= 0 and not $options->{ "invert" } ) {
4760 put_record( $record, $out );
4761 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4762 put_record( $record, $out );
4770 # Martin A. Hansen, August 2007.
4772 # Evaluate extression for records in stream.
4774 my ( $in, # handle to in stream
4775 $out, # handle to out stream
4776 $options, # options hash
4781 my ( $record, $eval_key, $eval_val, $check, @keys );
4783 while ( $record = get_record( $in ) )
4785 if ( $options->{ "eval" } )
4787 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4795 @keys = split /\W+/, $eval_val;
4796 @keys = grep { ! /^\d+$/ } @keys;
4801 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4803 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
4806 put_record( $record, $out );
4813 # Martin A. Hansen, June 2008.
4817 my ( $in, # handle to in stream
4818 $out, # handle to out stream
4819 $options, # options hash
4824 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4826 while ( $record = get_record( $in ) )
4830 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4832 push @rows, $record->{ $key };
4836 push @matrix, [ @rows ];
4841 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4843 foreach $row ( @matrix )
4845 for ( $i = 0; $i < @{ $row }; $i++ ) {
4846 $record->{ "V$i" } = $row->[ $i ];
4849 put_record( $record, $out );
4854 sub script_add_ident
4856 # Martin A. Hansen, May 2008.
4858 # Add a unique identifier to each record in stream.
4860 my ( $in, # handle to in stream
4861 $out, # handle to out stream
4862 $options, # options hash
4867 my ( $record, $key, $prefix, $i );
4869 $key = $options->{ "key" } || "ID";
4870 $prefix = $options->{ "prefix" } || "ID";
4874 while ( $record = get_record( $in ) )
4876 $record->{ $key } = sprintf( "$prefix%08d", $i );
4878 put_record( $record, $out );
4885 sub script_count_records
4887 # Martin A. Hansen, August 2007.
4889 # Count records in stream.
4891 my ( $in, # handle to in stream
4892 $out, # handle to out stream
4893 $options, # options hash
4898 my ( $record, $count, $result, $fh, $line );
4902 if ( $options->{ "no_stream" } )
4904 while ( $line = <$in> )
4908 $count++ if $line eq "---";
4913 while ( $record = get_record( $in ) )
4915 put_record( $record, $out );
4921 $result = { "count_records" => $count };
4923 $fh = write_stream( $options->{ "data_out" } );
4925 put_record( $result, $fh );
4931 sub script_random_records
4933 # Martin A. Hansen, August 2007.
4935 # Pick a number or random records from stream.
4937 my ( $in, # handle to in stream
4938 $out, # handle to out stream
4939 $options, # options hash
4944 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4946 $options->{ "num" } ||= 10;
4948 $tmp_file = "$BP_TMP/random_records.tmp";
4950 $fh_out = Maasha::Common::write_open( $tmp_file );
4954 while ( $record = get_record( $in ) )
4956 put_record( $record, $fh_out );
4966 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4968 while ( $i < $options->{ "num" } )
4970 $rand = int( rand( $count ) );
4972 if ( not exists $rand_hash{ $rand } )
4974 $rand_hash{ $rand } = 1;
4976 $max = $rand if $rand > $max;
4982 $fh_in = Maasha::Common::read_open( $tmp_file );
4986 while ( $record = get_record( $fh_in ) )
4988 put_record( $record, $out ) if exists $rand_hash{ $count };
4990 last if $count == $max;
5001 sub script_sort_records
5003 # Martin A. Hansen, August 2007.
5005 # Sort to sort records according to keys.
5007 my ( $in, # handle to in stream
5008 $out, # handle to out stream
5009 $options, # options hash
5014 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5016 foreach $key ( @{ $options->{ "keys" } } )
5018 if ( $key =~ s/n$// ) {
5019 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5021 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5025 $sort_str = join " or ", @sort_cmd;
5026 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5028 while ( $record = get_record( $in ) ) {
5029 push @records, $record;
5032 @records = sort $sort_sub @records;
5034 if ( $options->{ "reverse" } )
5036 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5037 put_record( $records[ $i ], $out );
5042 for ( $i = 0; $i < scalar @records; $i++ ) {
5043 put_record( $records[ $i ], $out );
5049 sub script_count_vals
5051 # Martin A. Hansen, August 2007.
5053 # Count records in stream.
5055 my ( $in, # handle to in stream
5056 $out, # handle to out stream
5057 $options, # options hash
5062 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5064 $tmp_file = "$BP_TMP/count_cache.tmp";
5066 $fh_out = Maasha::Common::write_open( $tmp_file );
5070 while ( $record = get_record( $in ) )
5072 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5074 push @records, $record;
5076 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5078 map { put_record( $_, $fh_out ) } @records;
5085 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5096 $fh_in = Maasha::Common::read_open( $tmp_file );
5098 while ( $record = get_record( $fh_in ) )
5100 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5102 put_record( $record, $out );
5104 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5112 foreach $record ( @records )
5114 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5116 put_record( $record, $out );
5123 sub script_plot_histogram
5125 # Martin A. Hansen, September 2007.
5127 # Plot a simple histogram for a given key using GNU plot.
5129 my ( $in, # handle to in stream
5130 $out, # handle to out stream
5131 $options, # options hash
5136 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5138 $options->{ "title" } ||= "Histogram";
5139 $options->{ "sort" } ||= "num";
5141 while ( $record = get_record( $in ) )
5143 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5145 put_record( $record, $out ) if not $options->{ "no_stream" };
5148 if ( $options->{ "sort" } eq "num" ) {
5149 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5151 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5154 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5156 $fh = write_stream( $options->{ "data_out" } );
5158 print $fh "$_\n" foreach @{ $result };
5164 sub script_plot_lendist
5166 # Martin A. Hansen, August 2007.
5168 # Plot length distribution using GNU plot.
5170 my ( $in, # handle to in stream
5171 $out, # handle to out stream
5172 $options, # options hash
5177 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5179 $options->{ "title" } ||= "Length Distribution";
5181 while ( $record = get_record( $in ) )
5183 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5185 put_record( $record, $out ) if not $options->{ "no_stream" };
5188 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5190 for ( $i = 0; $i < $max; $i++ ) {
5191 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5194 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5196 $fh = write_stream( $options->{ "data_out" } );
5198 print $fh "$_\n" foreach @{ $result };
5204 sub script_plot_chrdist
5206 # Martin A. Hansen, August 2007.
5208 # Plot chromosome distribution using GNU plot.
5210 my ( $in, # handle to in stream
5211 $out, # handle to out stream
5212 $options, # options hash
5217 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5219 $options->{ "title" } ||= "Chromosome Distribution";
5221 while ( $record = get_record( $in ) )
5223 if ( $record->{ "CHR" } ) { # generic
5224 $data_hash{ $record->{ "CHR" } }++;
5225 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5226 $data_hash{ $record->{ "S_ID" } }++;
5227 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5228 $data_hash{ $record->{ "S_ID" } }++;
5229 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5230 $data_hash{ $record->{ "S_ID" } }++;
5233 put_record( $record, $out ) if not $options->{ "no_stream" };
5236 foreach $elem ( keys %data_hash )
5240 $sort_key =~ s/chr//i;
5242 $sort_key =~ s/^X(.*)/99$1/;
5243 $sort_key =~ s/^Y(.*)/99$1/;
5244 $sort_key =~ s/^Z(.*)/999$1/;
5245 $sort_key =~ s/^M(.*)/9999$1/;
5246 $sort_key =~ s/^U(.*)/99999$1/;
5248 $count = $sort_key =~ tr/_//;
5250 $sort_key =~ s/_.*/"999999" x $count/ex;
5252 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5255 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5257 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5259 $fh = write_stream( $options->{ "data_out" } );
5261 print $fh "$_\n" foreach @{ $result };
5267 sub script_plot_karyogram
5269 # Martin A. Hansen, August 2007.
5271 # Plot hits on karyogram.
5273 my ( $in, # handle to in stream
5274 $out, # handle to out stream
5275 $options, # options hash
5280 my ( %options, $record, @data, $fh, $result, %data_hash );
5282 $options->{ "genome" } ||= "human";
5283 $options->{ "feat_color" } ||= "black";
5285 while ( $record = get_record( $in ) )
5287 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5289 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5292 put_record( $record, $out ) if not $options->{ "no_stream" };
5295 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5297 $fh = write_stream( $options->{ "data_out" } );
5305 sub script_plot_matches
5307 # Martin A. Hansen, August 2007.
5309 # Plot matches in 2D generating a dotplot.
5311 my ( $in, # handle to in stream
5312 $out, # handle to out stream
5313 $options, # options hash
5318 my ( $record, @data, $fh, $result, %data_hash );
5320 $options->{ "direction" } ||= "both";
5322 while ( $record = get_record( $in ) )
5324 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5325 push @data, $record;
5328 put_record( $record, $out ) if not $options->{ "no_stream" };
5331 $options->{ "title" } ||= "plot_matches";
5332 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5333 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5335 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5337 $fh = write_stream( $options->{ "data_out" } );
5339 print $fh "$_\n" foreach @{ $result };
5345 sub script_length_vals
5347 # Martin A. Hansen, August 2007.
5349 # Determine the length of the value for given keys.
5351 my ( $in, # handle to in stream
5352 $out, # handle to out stream
5353 $options, # options hash
5358 my ( $record, $key );
5360 while ( $record = get_record( $in ) )
5362 foreach $key ( @{ $options->{ "keys" } } )
5364 if ( $record->{ $key } ) {
5365 $record->{ $key . "_LEN" } = length $record->{ $key };
5369 put_record( $record, $out );
5376 # Martin A. Hansen, August 2007.
5378 # Calculates the sums for values of given keys.
5380 my ( $in, # handle to in stream
5381 $out, # handle to out stream
5382 $options, # options hash
5387 my ( $record, $key, %sum_hash, $fh );
5389 while ( $record = get_record( $in ) )
5391 foreach $key ( @{ $options->{ "keys" } } )
5393 if ( $record->{ $key } ) {
5394 $sum_hash{ $key } += $record->{ $key };
5398 put_record( $record, $out ) if not $options->{ "no_stream" };
5401 $fh = write_stream( $options->{ "data_out" } );
5403 foreach $key ( @{ $options->{ "keys" } } ) {
5404 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5411 sub script_mean_vals
5413 # Martin A. Hansen, August 2007.
5415 # Calculate the mean of values of given keys.
5417 my ( $in, # handle to in stream
5418 $out, # handle to out stream
5419 $options, # options hash
5424 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5426 while ( $record = get_record( $in ) )
5428 foreach $key ( @{ $options->{ "keys" } } )
5430 if ( $record->{ $key } )
5432 $sum_hash{ $key } += $record->{ $key };
5433 $count_hash{ $key }++;
5437 put_record( $record, $out ) if not $options->{ "no_stream" };
5440 $fh = write_stream( $options->{ "data_out" } );
5442 foreach $key ( @{ $options->{ "keys" } } )
5444 if ( $count_hash{ $key } ) {
5445 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5450 put_record( { $key . "_MEAN" => $mean } , $fh );
5457 sub script_median_vals
5459 # Martin A. Hansen, March 2008.
5461 # Calculate the median values of given keys.
5463 my ( $in, # handle to in stream
5464 $out, # handle to out stream
5465 $options, # options hash
5470 my ( $record, $key, %median_hash, $median, $fh );
5472 while ( $record = get_record( $in ) )
5474 foreach $key ( @{ $options->{ "keys" } } ) {
5475 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5478 put_record( $record, $out ) if not $options->{ "no_stream" };
5481 $fh = write_stream( $options->{ "data_out" } );
5483 foreach $key ( @{ $options->{ "keys" } } )
5485 if ( $median_hash{ $key } ) {
5486 $median = Maasha::Calc::median( $median_hash{ $key } );
5491 put_record( { $key . "_MEDIAN" => $median } , $fh );
5500 # Martin A. Hansen, February 2008.
5502 # Determine the maximum values of given keys.
5504 my ( $in, # handle to in stream
5505 $out, # handle to out stream
5506 $options, # options hash
5511 my ( $record, $key, $fh, %max_hash, $max_record );
5513 while ( $record = get_record( $in ) )
5515 foreach $key ( @{ $options->{ "keys" } } )
5517 if ( $record->{ $key } )
5519 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5523 put_record( $record, $out ) if not $options->{ "no_stream" };
5526 $fh = write_stream( $options->{ "data_out" } );
5528 foreach $key ( @{ $options->{ "keys" } } )
5530 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5533 put_record( $max_record, $fh );
5541 # Martin A. Hansen, February 2008.
5543 # Determine the minimum values of given keys.
5545 my ( $in, # handle to in stream
5546 $out, # handle to out stream
5547 $options, # options hash
5552 my ( $record, $key, $fh, %min_hash, $min_record );
5554 while ( $record = get_record( $in ) )
5556 foreach $key ( @{ $options->{ "keys" } } )
5558 if ( defined $record->{ $key } )
5560 if ( exists $min_hash{ $key } ) {
5561 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5563 $min_hash{ $key } = $record->{ $key };
5568 put_record( $record, $out ) if not $options->{ "no_stream" };
5571 $fh = write_stream( $options->{ "data_out" } );
5573 foreach $key ( @{ $options->{ "keys" } } )
5575 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5578 put_record( $min_record, $fh );
5584 sub script_upload_to_ucsc
5586 # Martin A. Hansen, August 2007.
5588 # Calculate the mean of values of given keys.
5590 my ( $in, # handle to in stream
5591 $out, # handle to out stream
5592 $options, # options hash
5597 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5598 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5600 $options->{ "short_label" } ||= $options->{ 'table' };
5601 $options->{ "long_label" } ||= $options->{ 'table' };
5602 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5603 $options->{ "priority" } ||= 1;
5604 $options->{ "visibility" } ||= "pack";
5605 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5606 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5608 $file = "$BP_TMP/ucsc_upload.tmp";
5616 if ( $options->{ 'wiggle' } )
5618 $options->{ "visibility" } = "full";
5620 while ( $record = get_record( $in ) )
5622 put_record( $record, $out ) if not $options->{ "no_stream" };
5624 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5625 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5626 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5628 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5630 $fh_out = $fh_hash{ $record->{ "CHR" } };
5632 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5635 map { close $_ } keys %fh_hash;
5637 $fh_out = Maasha::Common::write_open( $file );
5639 foreach $chr ( sort keys %fh_hash )
5641 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5643 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5647 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5649 $chr = $entry->{ 'CHR' };
5650 $beg = $entry->{ 'CHR_BEG' };
5651 $end = $entry->{ 'CHR_END' };
5652 $q_id = $entry->{ 'Q_ID' };
5654 if ( $q_id =~ /_(\d+)$/ ) {
5664 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5669 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5670 $block->[ $i ] += $clones;
5673 $max = Maasha::Calc::max( $max, $end );
5682 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5683 $block->[ $i ] += $clones;
5690 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5692 unlink "$BP_TMP/$chr";
5697 $wig_file = "$options->{ 'table' }.wig";
5698 $wib_file = "$options->{ 'table' }.wib";
5700 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5702 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5704 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5706 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5707 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5717 $fh_out = Maasha::Common::write_open( $file );
5719 while ( $record = get_record( $in ) )
5721 put_record( $record, $out ) if not $options->{ "no_stream" };
5723 if ( $record->{ "REC_TYPE" } eq "PSL" )
5725 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5726 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5730 $format = "PSL" if not $format;
5732 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5734 # chrom chromStart chromEnd name score strand size secStr conf
5736 print $fh_out join ( "\t",
5738 $record->{ "CHR_BEG" },
5739 $record->{ "CHR_END" } + 1,
5740 $record->{ "Q_ID" },
5741 $record->{ "SCORE" },
5742 $record->{ "STRAND" },
5743 $record->{ "SIZE" },
5744 $record->{ "SEC_STRUCT" },
5745 $record->{ "CONF" },
5748 $format = "BED_SS" if not $format;
5750 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5752 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5754 $format = "BED" if not $format;
5755 $columns = $record->{ "BED_COLS" } if not $columns;
5757 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5759 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5761 $format = "BED" if not $format;
5762 $columns = 6 if not $columns;
5764 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5766 $record->{ "CHR" } = $record->{ "S_ID" };
5767 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5768 $record->{ "CHR_END" } = $record->{ "S_END" };
5769 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5771 $format = "BED" if not $format;
5772 $columns = 6 if not $columns;
5774 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5776 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5778 $record->{ "CHR" } = $record->{ "S_ID" };
5779 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5780 $record->{ "CHR_END" } = $record->{ "S_END" };
5781 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5782 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5784 $format = "BED" if not $format;
5785 $columns = 6 if not $columns;
5787 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5790 if ( $i == $options->{ "chunk_size" } )
5794 if ( $format eq "BED" ) {
5795 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5796 } elsif ( $format eq "PSL" ) {
5797 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5806 $fh_out = Maasha::Common::write_open( $file );
5815 if ( exists $options->{ "database" } and $options->{ "table" } )
5817 if ( $format eq "BED" )
5819 $type = "bed $columns";
5821 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5823 elsif ( $format eq "BED_SS" )
5825 $options->{ "sec_struct" } = 1;
5827 $type = "sec_struct";
5829 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5831 elsif ( $format eq "PSL" )
5835 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5837 elsif ( $format eq "WIGGLE" )
5841 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5846 Maasha::UCSC::update_my_tracks( $options, $type );
5851 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5856 # Martin A. Hansen, July 2007.
5858 # Opens a stream to STDIN or a file,
5860 my ( $path, # path - OPTIONAL
5863 # Returns filehandle.
5867 if ( not -t STDIN ) {
5868 $fh = Maasha::Common::read_stdin();
5869 } elsif ( not $path ) {
5870 # Maasha::Common::error( qq(no data stream) );
5872 $fh = Maasha::Common::read_open( $path );
5875 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
5883 # Martin A. Hansen, August 2007.
5885 # Opens a stream to STDOUT or a file.
5887 my ( $path, # path - OPTIONAL
5888 $gzip, # compress data - OPTIONAL
5891 # Returns filehandle.
5896 $fh = Maasha::Common::write_open( $path, $gzip );
5898 $fh = Maasha::Common::write_stdout();
5907 # Martin A. Hansen, July 2007.
5909 # Reads one record at a time and converts that record
5910 # to a Perl data structure (a hash) which is returned.
5915 # Returns data structure.
5917 my ( $block, @lines, $line, $key, $value, %record );
5919 local $/ = "\n---\n";
5925 return if not defined $block;
5927 @lines = split "\n", $block;
5929 foreach $line ( @lines )
5931 ( $key, $value ) = split ": ", $line;
5933 $record{ $key } = $value;
5936 return wantarray ? %record : \%record;
5942 # Martin A. Hansen, July 2007.
5944 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5946 my ( $data, # data structure
5947 $fh, # file handle - OPTIONAL
5952 if ( scalar keys %{ $data } )
5956 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5961 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5972 # Martin A. Hansen, November 2007.
5974 # Extracts files from an explicit GetOpt::Long argument
5975 # allowing for the use of glob. E.g.
5976 # --data_in=test.fna
5977 # --data_in=test.fna,test2.fna
5979 # --data_in=test.fna,/dir/*.fna
5981 my ( $option, # option from GetOpt::Long
5986 my ( $elem, @files );
5988 foreach $elem ( split ",", $option )
5992 } elsif ( $elem =~ /\*/ ) {
5993 push @files, glob( $elem );
5997 return wantarray ? @files : \@files;
6003 # Martin A. Hansen, April 2008.
6005 # Removes temporary directory and exits gracefully.
6006 # This subroutine is meant to be run always as the last
6007 # thing even if a script is dies or is interrupted
6010 my ( $sig, # signal from the %SIG
6013 # print STDERR "signal->$sig<-\n";
6021 if ( $sig =~ /MAASHA_ERROR/ ) {
6022 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6023 } elsif ( $sig eq "INT" ) {
6024 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6025 } elsif ( $sig eq "TERM" ) {
6026 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6028 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6031 # This is a really bad solution, potentially, anyone can include this module and set
6032 # the BP_TMP to point at any dir and thus take out the machine !!!
6034 Maasha::Common::dir_remove( $BP_TMP );
6043 # This is a really bad solution, potentially, anyone can include this module and set
6044 # the BP_TMP to point at any dir and thus take out the machine !!!
6046 Maasha::Common::dir_remove( $BP_TMP );
6050 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6057 sub script_read_soft
6059 # Martin A. Hansen, December 2007.
6062 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
6064 my ( $in, # handle to in stream
6065 $out, # handle to out stream
6066 $options, # options hash
6071 my ( $data_in, $file, $num, $records, $record );
6073 while ( $record = get_record( $in ) ) {
6074 put_record( $record, $out );
6079 foreach $file ( @{ $options->{ "files" } } )
6081 $records = Maasha::NCBI::soft_parse( $file );
6083 foreach $record ( @{ $records } )
6085 put_record( $record, $out );
6087 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
6095 close $data_in if $data_in;