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;
58 use vars qw( @ISA @EXPORT_OK );
62 @ISA = qw( Exporter );
78 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
81 $SIG{ '__DIE__' } = \&sig_handler;
82 $SIG{ 'INT' } = \&sig_handler;
83 $SIG{ 'TERM' } = \&sig_handler;
86 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
89 my ( $script, $BP_TMP );
91 $script = Maasha::Common::get_scriptname();
92 $BP_TMP = Maasha::Common::get_tmpdir();
95 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
98 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
99 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
101 $log_global->autoflush( 1 );
102 $log_local->autoflush( 1 );
104 &log( $log_global, $script, \@ARGV );
105 &log( $log_local, $script, \@ARGV );
111 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114 my $t0 = gettimeofday();
116 run_script( $script );
118 my $t1 = gettimeofday();
120 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
123 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128 # Martin A. Hansen, January 2008.
130 # Log messages to logfile.
132 my ( $fh, # filehandle to logfile
133 $script, # script name
134 $argv, # reference to @ARGV
139 my ( $time_stamp, $user );
141 $time_stamp = Maasha::Common::time_stamp();
143 $user = $ENV{ 'USER' };
145 $script = "biopieces" if $script eq "-e";
147 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
153 # Martin A. Hansen, August 2007.
155 # Run a specific script.
157 my ( $script, # script name
162 my ( $options, $in, $out );
164 $options = get_options( $script );
166 $options->{ "SCRIPT" } = $script;
168 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
169 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
172 $in = read_stream( $options->{ "stream_in" } );
173 $out = write_stream( $options->{ "stream_out" } );
175 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
176 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
177 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
178 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
179 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
180 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
181 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
182 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
183 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
184 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
185 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
186 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
187 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
188 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
189 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
190 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
191 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
192 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
193 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
194 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
195 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
196 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
197 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
198 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
199 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
200 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
201 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
202 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
203 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
204 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
205 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
206 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
207 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
208 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
209 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
211 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
212 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
213 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
214 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
215 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
216 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
217 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
218 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
219 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
220 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
221 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
222 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
223 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
224 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
225 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
226 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
227 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
228 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
229 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
230 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
231 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
232 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
233 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
234 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
235 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
236 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
237 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
238 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
239 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
240 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
241 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
242 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
243 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
244 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
245 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
246 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
247 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
248 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
249 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
250 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
251 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
252 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
253 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
254 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
255 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
256 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
257 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
258 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
259 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
260 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
261 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
262 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
263 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
264 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
265 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
267 close $in if defined $in;
274 # Martin A. Hansen, February 2008.
276 # Gets options from commandline and checks these vigerously.
278 my ( $script, # name of script
283 my ( %options, @options, $opt, @genomes );
285 if ( $script eq "print_usage" )
291 elsif ( $script eq "read_fasta" )
298 elsif ( $script eq "read_tab" )
309 elsif ( $script eq "read_psl" )
316 elsif ( $script eq "read_bed" )
323 elsif ( $script eq "read_fixedstep" )
330 elsif ( $script eq "read_blast_tab" )
337 elsif ( $script eq "read_embl" )
347 elsif ( $script eq "read_stockholm" )
354 elsif ( $script eq "read_phastcons" )
365 elsif ( $script eq "read_soft" )
372 elsif ( $script eq "read_gff" )
379 elsif ( $script eq "read_2bit" )
387 elsif ( $script eq "read_solexa" )
395 elsif ( $script eq "read_solid" )
403 elsif ( $script eq "read_mysql" )
412 elsif ( $script eq "format_genome" )
421 elsif ( $script eq "length_seq" )
428 elsif ( $script eq "oligo_freq" )
435 elsif ( $script eq "create_weight_matrix" )
441 elsif ( $script eq "transliterate_seq" )
449 elsif ( $script eq "transliterate_vals" )
458 elsif ( $script eq "translate_seq" )
464 elsif ( $script eq "extract_seq" )
472 elsif ( $script eq "get_genome_seq" )
484 elsif ( $script eq "get_genome_align" )
495 elsif ( $script eq "get_genome_phastcons" )
506 elsif ( $script eq "split_seq" )
513 elsif ( $script eq "split_bed" )
520 elsif ( $script eq "tile_seq" )
527 elsif ( $script eq "invert_align" )
533 elsif ( $script eq "patscan_seq" )
544 elsif ( $script eq "create_blast_db" )
551 elsif ( $script eq "blast_seq" )
563 elsif ( $script eq "blat_seq" )
575 elsif ( $script eq "soap_seq" )
586 elsif ( $script eq "match_seq" )
593 elsif ( $script eq "create_vmatch_index" )
601 elsif ( $script eq "vmatch_seq" )
612 elsif ( $script eq "write_fasta" )
621 elsif ( $script eq "write_align" )
631 elsif ( $script eq "write_blast" )
640 elsif ( $script eq "write_tab" )
652 elsif ( $script eq "write_bed" )
660 elsif ( $script eq "write_psl" )
668 elsif ( $script eq "write_fixedstep" )
676 elsif ( $script eq "write_2bit" )
684 elsif ( $script eq "write_solid" )
693 elsif ( $script eq "plot_seqlogo" )
700 elsif ( $script eq "plot_phastcons_profiles" )
715 elsif ( $script eq "analyze_vals" )
722 elsif ( $script eq "head_records" )
728 elsif ( $script eq "remove_keys" )
735 elsif ( $script eq "rename_keys" )
741 elsif ( $script eq "uniq_vals" )
748 elsif ( $script eq "merge_vals" )
755 elsif ( $script eq "merge_records" )
762 elsif ( $script eq "grab" )
777 elsif ( $script eq "compute" )
783 elsif ( $script eq "add_ident" )
790 elsif ( $script eq "count_records" )
797 elsif ( $script eq "random_records" )
803 elsif ( $script eq "sort_records" )
810 elsif ( $script eq "count_vals" )
816 elsif ( $script eq "plot_histogram" )
829 elsif ( $script eq "plot_lendist" )
841 elsif ( $script eq "plot_chrdist" )
852 elsif ( $script eq "plot_karyogram" )
861 elsif ( $script eq "plot_matches" )
873 elsif ( $script eq "length_vals" )
879 elsif ( $script eq "sum_vals" )
887 elsif ( $script eq "mean_vals" )
895 elsif ( $script eq "median_vals" )
903 elsif ( $script eq "max_vals" )
911 elsif ( $script eq "min_vals" )
919 elsif ( $script eq "upload_to_ucsc" )
946 # print STDERR Dumper( \@options );
953 # print STDERR Dumper( \%options );
955 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
956 return wantarray ? %options : \%options;
959 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
960 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
961 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
962 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
963 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
964 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
965 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
966 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
968 # ---- check arguments ----
970 if ( $options{ 'data_in' } )
972 $options{ "files" } = getopt_files( $options{ 'data_in' } );
974 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
977 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
979 # print STDERR Dumper( \%options );
981 foreach $opt ( keys %options )
983 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
985 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
987 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+$/ )
989 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
991 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
993 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
995 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
997 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
999 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
1001 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1003 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1005 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1007 elsif ( $opt eq "genome" and $script ne "format_genome" )
1009 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1010 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1012 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1013 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1016 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1018 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1020 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1022 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1024 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1026 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1030 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1031 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1032 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1033 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1034 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1035 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1036 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1037 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1038 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" };
1039 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1040 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" };
1042 if ( $script eq "upload_to_ucsc" )
1044 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1045 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1048 return wantarray ? %options : \%options;
1052 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1055 sub script_print_usage
1057 # Martin A. Hansen, January 2008.
1059 # Retrieves usage information from file and
1060 # prints this nicely formatted.
1062 my ( $in, # handle to in stream
1063 $out, # handle to out stream
1064 $options, # options hash
1069 my ( $file, $wiki, $lines );
1071 if ( $options->{ 'data_in' } ) {
1072 $file = $options->{ 'data_in' };
1074 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1077 $wiki = Maasha::Gwiki::gwiki_read( $file );
1079 if ( not $options->{ "help" } ) {
1080 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1083 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1085 print STDERR "$_\n" foreach @{ $lines };
1091 sub script_list_biopieces
1093 # Martin A. Hansen, January 2008.
1095 # Prints the synopsis from the usage for each of the biopieces.
1097 my ( $in, # handle to in stream
1098 $out, # handle to out stream
1099 $options, # options hash
1104 my ( @files, $file, $wiki, $program, $synopsis );
1106 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1108 foreach $file ( sort @files )
1110 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1114 $wiki = Maasha::Gwiki::gwiki_read( $file );
1116 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1117 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1119 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1120 $synopsis =~ s/!(\w)/$1/g;
1122 printf( "%-30s%s\n", $program, $synopsis );
1130 sub script_list_genomes
1132 # Martin A. Hansen, January 2008.
1134 # Prints the synopsis from the usage for each of the biopieces.
1136 my ( $in, # handle to in stream
1137 $out, # handle to out stream
1138 $options, # options hash
1143 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1145 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1147 foreach $genome ( @genomes )
1149 next if $genome =~ /\.$/;
1151 @formats = Maasha::Common::ls_dirs( $genome );
1153 foreach $format ( @formats )
1155 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1157 $hash{ $1 }{ $2 } = 1;
1166 map { push @row, $_ } sort keys %found;
1168 print join( "\t", @row ), "\n";
1170 foreach $genome ( sort keys %hash )
1174 foreach $format ( sort keys %found )
1176 if ( exists $hash{ $genome }{ $format } ) {
1183 print join( "\t", @row ), "\n";
1188 sub script_read_fasta
1190 # Martin A. Hansen, August 2007.
1192 # Read sequences from FASTA file.
1194 my ( $in, # handle to in stream
1195 $out, # handle to out stream
1196 $options, # options hash
1201 my ( $record, $file, $data_in, $entry, $num );
1203 while ( $record = get_record( $in ) ) {
1204 put_record( $record, $out );
1209 foreach $file ( @{ $options->{ "files" } } )
1211 $data_in = Maasha::Common::read_open( $file );
1213 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1215 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1218 SEQ_NAME => $entry->[ SEQ_NAME ],
1219 SEQ => $entry->[ SEQ ],
1220 SEQ_LEN => length $entry->[ SEQ ],
1223 put_record( $record, $out );
1226 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1236 close $data_in if $data_in;
1242 # Martin A. Hansen, August 2007.
1244 # Read table or table columns from stream or file.
1246 my ( $in, # handle to in stream
1247 $out, # handle to out stream
1248 $options, # options hash
1253 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1255 $options->{ 'delimit' } ||= '\s+';
1257 while ( $record = get_record( $in ) ) {
1258 put_record( $record, $out );
1261 $skip = $options->{ 'skip' } ||= 0;
1264 foreach $file ( @{ $options->{ "files" } } )
1266 $data_in = Maasha::Common::read_open( $file );
1268 while ( $line = <$data_in> )
1276 next if $line =~ /^#|^$/;
1283 @fields = split /$options->{'delimit'}/, $line;
1285 if ( $options->{ "cols" } ) {
1286 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1291 for ( $i = 0; $i < @fields2; $i++ )
1293 if ( $options->{ "keys" }->[ $i ] ) {
1294 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1296 $record->{ "V" . $i } = $fields2[ $i ];
1300 put_record( $record, $out );
1302 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1312 close $data_in if $data_in;
1318 # Martin A. Hansen, August 2007.
1320 # Read psl table from stream or file.
1322 my ( $in, # handle to in stream
1323 $out, # handle to out stream
1324 $options, # options hash
1329 my ( $record, @files, $file, $entries, $entry, $num );
1331 while ( $record = get_record( $in ) ) {
1332 put_record( $record, $out );
1337 foreach $file ( @{ $options->{ "files" } } )
1339 $entries = Maasha::UCSC::psl_get_entries( $file );
1341 foreach $entry ( @{ $entries } )
1343 put_record( $entry, $out );
1345 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1357 # Martin A. Hansen, August 2007.
1359 # Read bed table from stream or file.
1361 my ( $in, # handle to in stream
1362 $out, # handle to out stream
1363 $options, # options hash
1368 my ( $file, $record, $entry, $data_in, $num );
1370 while ( $record = get_record( $in ) ) {
1371 put_record( $record, $out );
1376 foreach $file ( @{ $options->{ "files" } } )
1378 $data_in = Maasha::Common::read_open( $file );
1380 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1382 put_record( $entry, $out );
1384 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1394 close $data_in if $data_in;
1398 sub script_read_fixedstep
1400 # Martin A. Hansen, Juli 2008.
1402 # Read fixedStep wiggle format from stream or file.
1404 my ( $in, # handle to in stream
1405 $out, # handle to out stream
1406 $options, # options hash
1411 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1413 while ( $record = get_record( $in ) ) {
1414 put_record( $record, $out );
1419 foreach $file ( @{ $options->{ "files" } } )
1421 $data_in = Maasha::Common::read_open( $file );
1423 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1425 $head = shift @{ $entry };
1427 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1429 $record->{ "CHR" } = $1;
1430 $record->{ "CHR_BEG" } = $2;
1431 $record->{ "STEP" } = $3;
1432 $record->{ "VALS" } = join ",", @{ $entry };
1435 put_record( $record, $out );
1437 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1447 close $data_in if $data_in;
1451 sub script_read_blast_tab
1453 # Martin A. Hansen, September 2007.
1455 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1457 my ( $in, # handle to in stream
1458 $out, # handle to out stream
1459 $options, # options hash
1464 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1466 while ( $record = get_record( $in ) ) {
1467 put_record( $record, $out );
1472 foreach $file ( @{ $options->{ "files" } } )
1474 $data_in = Maasha::Common::read_open( $file );
1476 while ( $line = <$data_in> )
1480 next if $line =~ /^#/;
1482 @fields = split /\t/, $line;
1484 $record->{ "REC_TYPE" } = "BLAST";
1485 $record->{ "Q_ID" } = $fields[ 0 ];
1486 $record->{ "S_ID" } = $fields[ 1 ];
1487 $record->{ "IDENT" } = $fields[ 2 ];
1488 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1489 $record->{ "MISMATCHES" } = $fields[ 4 ];
1490 $record->{ "GAPS" } = $fields[ 5 ];
1491 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1492 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1493 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1494 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1495 $record->{ "E_VAL" } = $fields[ 10 ];
1496 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1498 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1500 $record->{ "STRAND" } = '-';
1502 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1506 $record->{ "STRAND" } = '+';
1509 put_record( $record, $out );
1511 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1521 close $data_in if $data_in;
1525 sub script_read_embl
1527 # Martin A. Hansen, August 2007.
1531 my ( $in, # handle to in stream
1532 $out, # handle to out stream
1533 $options, # options hash
1538 my ( %options2, $file, $data_in, $num, $entry, $record );
1540 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1541 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1542 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1544 while ( $record = get_record( $in ) ) {
1545 put_record( $record, $out );
1550 foreach $file ( @{ $options->{ "files" } } )
1552 $data_in = Maasha::Common::read_open( $file );
1554 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1556 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1558 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1560 $record_copy = dclone $record;
1562 delete $record_copy->{ "FT" };
1564 put_record( $record_copy, $out );
1566 delete $record_copy->{ "SEQ" };
1568 foreach $feat ( keys %{ $record->{ "FT" } } )
1570 $record_copy->{ "FEAT_TYPE" } = $feat;
1572 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1574 foreach $qual ( keys %{ $feat2 } )
1576 $qual_val = join "; ", @{ $feat2->{ $qual } };
1581 $record_copy->{ $qual } = $qual_val;
1584 put_record( $record_copy, $out );
1588 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1598 close $data_in if $data_in;
1602 sub script_read_stockholm
1604 # Martin A. Hansen, August 2007.
1606 # Read Stockholm format.
1608 my ( $in, # handle to in stream
1609 $out, # handle to out stream
1610 $options, # options hash
1615 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1617 while ( $record = get_record( $in ) ) {
1618 put_record( $record, $out );
1623 foreach $file ( @{ $options->{ "files" } } )
1625 $data_in = Maasha::Common::read_open( $file );
1627 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1629 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1633 foreach $key ( keys %{ $record->{ "GF" } } ) {
1634 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1637 $record_anno->{ "ALIGN" } = $num;
1639 put_record( $record_anno, $out );
1641 foreach $seq ( @{ $record->{ "ALIGN" } } )
1643 undef $record_align;
1646 SEQ_NAME => $seq->[ 0 ],
1650 put_record( $record_align, $out );
1653 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1663 close $data_in if $data_in;
1667 sub script_read_phastcons
1669 # Martin A. Hansen, December 2007.
1671 # Read PhastCons format.
1673 my ( $in, # handle to in stream
1674 $out, # handle to out stream
1675 $options, # options hash
1680 my ( $data_in, $file, $num, $entry, @records, $record );
1682 $options->{ "min" } ||= 10;
1683 $options->{ "dist" } ||= 25;
1684 $options->{ "threshold" } ||= 0.8;
1685 $options->{ "gap" } ||= 5;
1687 while ( $record = get_record( $in ) ) {
1688 put_record( $record, $out );
1693 foreach $file ( @{ $options->{ "files" } } )
1695 $data_in = Maasha::Common::read_open( $file );
1697 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1699 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1701 foreach $record ( @records )
1703 $record->{ "REC_TYPE" } = "BED";
1704 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1706 put_record( $record, $out );
1708 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1719 close $data_in if $data_in;
1723 sub script_read_soft
1725 # Martin A. Hansen, December 2007.
1728 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1730 my ( $in, # handle to in stream
1731 $out, # handle to out stream
1732 $options, # options hash
1737 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1739 while ( $record = get_record( $in ) ) {
1740 put_record( $record, $out );
1745 foreach $file ( @{ $options->{ "files" } } )
1747 $soft_index = Maasha::NCBI::soft_index_file( $file );
1749 $fh = Maasha::Common::read_open( $file );
1751 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1753 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1755 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1757 $old_end = $platforms[ -1 ]->[ 2 ];
1759 foreach $sample ( @samples )
1761 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1763 foreach $record ( @{ $records } )
1765 put_record( $record, $out );
1767 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1772 $old_end = $sample->[ 2 ];
1780 close $data_in if $data_in;
1787 # Martin A. Hansen, February 2008.
1790 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1792 my ( $in, # handle to in stream
1793 $out, # handle to out stream
1794 $options, # options hash
1799 my ( $data_in, $file, $fh, $num, $record, $entry );
1801 while ( $record = get_record( $in ) ) {
1802 put_record( $record, $out );
1807 foreach $file ( @{ $options->{ "files" } } )
1809 $fh = Maasha::Common::read_open( $file );
1811 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1813 put_record( $entry, $out );
1815 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1825 close $data_in if $data_in;
1829 sub script_read_2bit
1831 # Martin A. Hansen, March 2008.
1833 # Read sequences from 2bit file.
1835 my ( $in, # handle to in stream
1836 $out, # handle to out stream
1837 $options, # options hash
1842 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1844 $mask = 1 if not $options->{ "no_mask" };
1846 while ( $record = get_record( $in ) ) {
1847 put_record( $record, $out );
1852 foreach $file ( @{ $options->{ "files" } } )
1854 $data_in = Maasha::Common::read_open( $file );
1856 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1858 foreach $line ( @{ $toc } )
1860 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1861 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1862 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1864 put_record( $record, $out );
1866 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1876 close $data_in if $data_in;
1880 sub script_read_solexa
1882 # Martin A. Hansen, March 2008.
1884 # Read Solexa sequence reads from file.
1886 my ( $in, # handle to in stream
1887 $out, # handle to out stream
1888 $options, # options hash
1893 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1895 $options->{ "quality" } ||= 20;
1897 while ( $record = get_record( $in ) ) {
1898 put_record( $record, $out );
1903 foreach $file ( @{ $options->{ "files" } } )
1905 $data_in = Maasha::Common::read_open( $file );
1907 while ( $entry = Maasha::Solexa::solexa_get_entry( $data_in ) )
1909 @seqs = split //, $entry->[ SEQ ];
1910 @scores = split /:/, $entry->[ SCORE ];
1912 for ( $i = 0; $i < @scores; $i++ ) {
1913 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1916 $record->{ "SEQ_NAME" } = $entry->[ SEQ_NAME ];
1917 $record->{ "SEQ" } = join "", @seqs;
1918 $record->{ "SEQ_LEN" } = scalar @seqs;
1919 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1921 put_record( $record, $out );
1923 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1933 close $data_in if $data_in;
1937 sub script_read_solid
1939 # Martin A. Hansen, April 2008.
1941 # Read Solid sequence from file.
1943 my ( $in, # handle to in stream
1944 $out, # handle to out stream
1945 $options, # options hash
1950 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1952 $options->{ "quality" } ||= 15;
1954 while ( $record = get_record( $in ) ) {
1955 put_record( $record, $out );
1960 foreach $file ( @{ $options->{ "files" } } )
1962 $data_in = Maasha::Common::read_open( $file );
1964 while ( $line = <$data_in> )
1968 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1970 @scores = split /,/, $seq_qual;
1971 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1973 for ( $i = 0; $i < @seqs; $i++ ) {
1974 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1978 SEQ_NAME => $seq_name,
1980 SEQ_QUAL => $seq_qual,
1981 SEQ_LEN => length $seq_cs,
1982 SEQ => join( "", @seqs ),
1983 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1986 put_record( $record, $out );
1988 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1998 close $data_in if $data_in;
2002 sub script_read_mysql
2004 # Martin A. Hansen, May 2008.
2006 # Read a MySQL query into stream.
2008 my ( $in, # handle to in stream
2009 $out, # handle to out stream
2010 $options, # options hash
2015 my ( $record, $dbh, $results );
2017 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2018 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2020 while ( $record = get_record( $in ) ) {
2021 put_record( $record, $out );
2024 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2026 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2028 Maasha::SQL::disconnect( $dbh );
2030 map { put_record( $_ ) } @{ $results };
2034 sub script_format_genome
2036 # Martin A. Hansen, Juli 2008.
2038 # Format a genome to speficed formats.
2040 my ( $in, # handle to in stream
2041 $out, # handle to out stream
2042 $options, # options hash
2047 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2049 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2050 $genome = $options->{ 'genome' };
2052 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2053 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2054 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2056 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2058 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2060 $fasta_dir = "$dir/genomes/$genome/fasta";
2064 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2066 $fasta_dir = "$dir/genomes/$genome/fasta";
2068 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2071 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2073 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2075 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2077 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2080 while ( $record = get_record( $in ) )
2082 if ( $fh_out and $entry = record2fasta( $record ) )
2084 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2086 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2088 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2090 $vals = $record->{ 'VALS' };
2094 print $fh_out "$vals\n";
2097 put_record( $record, $out ) if not $options->{ "no_stream" };
2100 foreach $format ( @{ $options->{ 'formats' } } )
2102 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2103 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2104 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2105 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2106 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2109 close $fh_out if $fh_out;
2113 sub script_length_seq
2115 # Martin A. Hansen, August 2007.
2117 # Determine the length of sequences in stream.
2119 my ( $in, # handle to in stream
2120 $out, # handle to out stream
2121 $options, # options hash
2126 my ( $record, $total );
2128 while ( $record = get_record( $in ) )
2130 if ( $record->{ "SEQ" } )
2132 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2133 $total += $record->{ "SEQ_LEN" };
2136 put_record( $record, $out ) if not $options->{ "no_stream" };
2139 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2143 sub script_uppercase_seq
2145 # Martin A. Hansen, August 2007.
2147 # Uppercases sequences in stream.
2149 my ( $in, # handle to in stream
2150 $out, # handle to out stream
2157 while ( $record = get_record( $in ) )
2159 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2161 put_record( $record, $out );
2166 sub script_shuffle_seq
2168 # Martin A. Hansen, December 2007.
2170 # Shuffle sequences in stream.
2172 my ( $in, # handle to in stream
2173 $out, # handle to out stream
2180 while ( $record = get_record( $in ) )
2182 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2184 put_record( $record, $out );
2189 sub script_analyze_seq
2191 # Martin A. Hansen, August 2007.
2193 # Analyze sequence composition of sequences in stream.
2195 my ( $in, # handle to in stream
2196 $out, # handle to out stream
2201 my ( $record, $analysis );
2203 while ( $record = get_record( $in ) )
2205 if ( $record->{ "SEQ" } )
2207 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2209 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2212 put_record( $record, $out );
2217 sub script_analyze_tags
2219 # Martin A. Hansen, August 2008.
2221 # Analyze sequence tags in stream.
2223 my ( $in, # handle to in stream
2224 $out, # handle to out stream
2229 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2231 while ( $record = get_record( $in ) )
2233 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2235 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2239 $len_hash{ length( $record->{ "SEQ" } ) }++;
2240 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2243 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2245 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2249 $len_hash{ $record->{ "BED_LEN" } }++;
2250 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2255 foreach $key ( sort { $a <=> $b } keys %len_hash )
2257 $tag_record->{ "TAG_LEN" } = $key;
2258 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2259 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2261 put_record( $tag_record, $out );
2266 sub script_complexity_seq
2268 # Martin A. Hansen, May 2008.
2270 # Generates an index calculated as the most common di-residue over
2271 # the sequence length for all sequences in stream.
2273 my ( $in, # handle to in stream
2274 $out, # handle to out stream
2279 my ( $record, $index );
2281 while ( $record = get_record( $in ) )
2283 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2285 put_record( $record, $out );
2290 sub script_oligo_freq
2292 # Martin A. Hansen, August 2007.
2294 # Determine the length of sequences in stream.
2296 my ( $in, # handle to in stream
2297 $out, # handle to out stream
2298 $options, # options hash
2303 my ( $record, %oligos, @freq_table );
2305 $options->{ "word_size" } ||= 7;
2307 while ( $record = get_record( $in ) )
2309 if ( $record->{ "SEQ" } )
2311 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2313 if ( not $options->{ "all" } )
2315 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2317 map { put_record( $_, $out ) } @freq_table;
2323 put_record( $record, $out );
2326 if ( $options->{ "all" } )
2328 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2330 map { put_record( $_, $out ) } @freq_table;
2335 sub script_create_weight_matrix
2337 # Martin A. Hansen, August 2007.
2339 # Creates a weight matrix from an alignmnet.
2341 my ( $in, # handle to in stream
2342 $out, # handle to out stream
2343 $options, # options hash
2348 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2352 while ( $record = get_record( $in ) )
2354 if ( $record->{ "SEQ" } )
2356 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2358 $res = substr $record->{ "SEQ" }, $i, 1;
2360 $freq_hash{ $i }{ $res }++;
2361 $res_hash{ $res } = 1;
2368 put_record( $record, $out );
2372 foreach $res ( sort keys %res_hash )
2376 $record->{ "V0" } = $res;
2378 for ( $i = 0; $i < keys %freq_hash; $i++ )
2380 $freq = $freq_hash{ $i }{ $res } || 0;
2382 if ( $options->{ "percent" } ) {
2383 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2386 $record->{ "V" . ( $i + 1 ) } = $freq;
2389 put_record( $record, $out );
2394 sub script_calc_bit_scores
2396 # Martin A. Hansen, March 2007.
2398 # Calculates the bit scores for each position from an alignmnet in the stream.
2400 my ( $in, # handle to in stream
2401 $out, # handle to out stream
2406 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2410 while ( $record = get_record( $in ) )
2412 if ( $record->{ "SEQ" } )
2414 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2416 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2418 $res = substr $record->{ "SEQ" }, $i, 1;
2420 next if $res =~ /-|_|~|\./;
2422 $freq_hash{ $i }{ $res }++;
2429 put_record( $record, $out );
2435 if ( $type eq "protein" ) {
2441 for ( $i = 0; $i < keys %freq_hash; $i++ )
2443 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2445 $bit_diff = $bit_max - $bit_height;
2447 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2450 put_record( $record, $out );
2454 sub script_reverse_seq
2456 # Martin A. Hansen, August 2007.
2458 # Reverse sequence in record.
2460 my ( $in, # handle to in stream
2461 $out, # handle to out stream
2468 while ( $record = get_record( $in ) )
2470 if ( $record->{ "SEQ" } ) {
2471 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2474 put_record( $record, $out );
2479 sub script_complement_seq
2481 # Martin A. Hansen, August 2007.
2483 # Complement sequence in record.
2485 my ( $in, # handle to in stream
2486 $out, # handle to out stream
2491 my ( $record, $type );
2493 while ( $record = get_record( $in ) )
2495 if ( $record->{ "SEQ" } )
2498 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2501 if ( $type eq "rna" ) {
2502 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2503 } elsif ( $type eq "dna" ) {
2504 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2508 put_record( $record, $out );
2513 sub script_remove_indels
2515 # Martin A. Hansen, August 2007.
2517 # Remove indels from sequences in stream.
2519 my ( $in, # handle to in stream
2520 $out, # handle to out stream
2527 while ( $record = get_record( $in ) )
2529 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2531 put_record( $record, $out );
2536 sub script_transliterate_seq
2538 # Martin A. Hansen, August 2007.
2540 # Transliterate chars from sequence in record.
2542 my ( $in, # handle to in stream
2543 $out, # handle to out stream
2544 $options, # options hash
2549 my ( $record, $search, $replace, $delete );
2551 $search = $options->{ "search" } || "";
2552 $replace = $options->{ "replace" } || "";
2553 $delete = $options->{ "delete" } || "";
2555 while ( $record = get_record( $in ) )
2557 if ( $record->{ "SEQ" } )
2559 if ( $search and $replace ) {
2560 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2561 } elsif ( $delete ) {
2562 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2566 put_record( $record, $out );
2571 sub script_transliterate_vals
2573 # Martin A. Hansen, April 2008.
2575 # Transliterate chars from values in record.
2577 my ( $in, # handle to in stream
2578 $out, # handle to out stream
2579 $options, # options hash
2584 my ( $record, $search, $replace, $delete, $key );
2586 $search = $options->{ "search" } || "";
2587 $replace = $options->{ "replace" } || "";
2588 $delete = $options->{ "delete" } || "";
2590 while ( $record = get_record( $in ) )
2592 foreach $key ( @{ $options->{ "keys" } } )
2594 if ( exists $record->{ $key } )
2596 if ( $search and $replace ) {
2597 eval "\$record->{ $key } =~ tr/$search/$replace/";
2598 } elsif ( $delete ) {
2599 eval "\$record->{ $key } =~ tr/$delete//d";
2604 put_record( $record, $out );
2609 sub script_translate_seq
2611 # Martin A. Hansen, February 2008.
2613 # Translate DNA sequence into protein sequence.
2615 my ( $in, # handle to in stream
2616 $out, # handle to out stream
2617 $options, # options hash
2622 my ( $record, $frame, %new_record );
2624 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2626 while ( $record = get_record( $in ) )
2628 if ( $record->{ "SEQ" } )
2630 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2632 foreach $frame ( @{ $options->{ "frames" } } )
2634 %new_record = %{ $record };
2636 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2637 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2638 $new_record{ "FRAME" } = $frame;
2640 put_record( \%new_record, $out );
2646 put_record( $record, $out );
2652 sub script_extract_seq
2654 # Martin A. Hansen, August 2007.
2656 # Extract subsequences from sequences in record.
2658 my ( $in, # handle to in stream
2659 $out, # handle to out stream
2660 $options, # options hash
2665 my ( $beg, $end, $len, $record );
2667 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2670 $beg = $options->{ "beg" } - 1; # correcting for start offset
2673 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2675 } elsif ( defined $options->{ "end" } ) {
2676 $end = $options->{ "end" } - 1; # correcting for start offset
2679 $len = $options->{ "len" };
2681 # print "beg->$beg, end->$end, len->$len\n";
2683 while ( $record = get_record( $in ) )
2685 if ( $record->{ "SEQ" } )
2687 if ( defined $beg and defined $end )
2689 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2690 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2692 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2695 elsif ( defined $beg and defined $len )
2697 if ( $len > length $record->{ "SEQ" } ) {
2698 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2700 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2703 elsif ( defined $beg )
2705 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2709 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2711 put_record( $record, $out );
2716 sub script_get_genome_seq
2718 # Martin A. Hansen, December 2007.
2720 # Gets a subsequence from a genome.
2722 my ( $in, # handle to in stream
2723 $out, # handle to out stream
2724 $options, # options hash
2729 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2731 $options->{ "flank" } ||= 0;
2733 if ( $options->{ "genome" } )
2735 $genome = $options->{ "genome" };
2737 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2738 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2740 $fh = Maasha::Common::read_open( $genome_file );
2741 $index = Maasha::Fasta::index_retrieve( $index_file );
2743 shift @{ $index }; # Get rid of the file size info
2745 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2747 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2749 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2751 $beg = $index_beg + $options->{ "beg" } - 1;
2753 if ( $options->{ "len" } ) {
2754 $len = $options->{ "len" };
2755 } elsif ( $options->{ "end" } ) {
2756 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2759 $beg -= $options->{ "flank" };
2760 $len += 2 * $options->{ "flank" };
2762 if ( $beg <= $index_beg )
2764 $len -= $index_beg - $beg;
2768 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2770 next if $beg > $index_beg + $index_len;
2772 $record->{ "CHR" } = $options->{ "chr" };
2773 $record->{ "CHR_BEG" } = $beg - $index_beg;
2774 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2776 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2777 $record->{ "SEQ_LEN" } = $len;
2779 put_record( $record, $out );
2783 while ( $record = get_record( $in ) )
2785 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2787 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2789 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2791 $beg = $record->{ "CHR_BEG" } + $index_beg;
2792 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2794 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2796 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2798 $beg = $record->{ "S_BEG" } + $index_beg;
2799 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2801 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2803 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2805 $beg = $record->{ "S_BEG" } + $index_beg;
2806 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2809 $beg -= $options->{ "flank" };
2810 $len += 2 * $options->{ "flank" };
2812 if ( $beg <= $index_beg )
2814 $len -= $index_beg - $beg;
2818 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2820 next if $beg > $index_beg + $index_len;
2822 $record->{ "CHR_BEG" } = $beg - $index_beg;
2823 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2825 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2827 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2829 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2830 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2833 if ( $options->{ "mask" } )
2835 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2837 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2839 @begs = split ",", $record->{ "Q_BEGS" };
2840 @lens = split ",", $record->{ "BLOCKSIZES" };
2842 for ( $i = 0; $i < @begs; $i++ ) {
2843 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2849 put_record( $record, $out );
2856 sub script_get_genome_align
2858 # Martin A. Hansen, April 2008.
2860 # Gets a subalignment from a multiple genome alignment.
2862 my ( $in, # handle to in stream
2863 $out, # handle to out stream
2864 $options, # options hash
2869 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2871 $options->{ "strand" } ||= "+";
2875 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2877 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2879 $beg = $options->{ "beg" } - 1;
2881 if ( $options->{ "end" } ) {
2882 $end = $options->{ "end" };
2883 } elsif ( $options->{ "len" } ) {
2884 $end = $beg + $options->{ "len" };
2887 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2889 foreach $entry ( @{ $align } )
2891 $entry->{ "CHR" } = $record->{ "CHR" };
2892 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2893 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2894 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2895 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2896 $entry->{ "SCORE" } = $record->{ "SCORE" };
2898 put_record( $entry, $out );
2902 while ( $record = get_record( $in ) )
2904 if ( $record->{ "REC_TYPE" } eq "BED" )
2906 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2908 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2910 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2912 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2914 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2917 foreach $entry ( @{ $align } )
2919 $entry->{ "CHR" } = $record->{ "CHR" };
2920 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2921 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2922 $entry->{ "STRAND" } = $record->{ "STRAND" };
2923 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2924 $entry->{ "SCORE" } = $record->{ "SCORE" };
2926 put_record( $entry, $out );
2934 sub script_get_genome_phastcons
2936 # Martin A. Hansen, February 2008.
2938 # Get phastcons scores from genome intervals.
2940 my ( $in, # handle to in stream
2941 $out, # handle to out stream
2942 $options, # options hash
2947 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2949 $options->{ "flank" } ||= 0;
2951 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2952 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2954 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2955 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2957 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2959 $options->{ "beg" } -= 1; # request is 1-based
2960 $options->{ "end" } -= 1; # request is 1-based
2962 if ( $options->{ "len" } ) {
2963 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2966 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2968 $record->{ "CHR" } = $options->{ "chr" };
2969 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2970 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2972 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2973 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2975 put_record( $record, $out );
2978 while ( $record = get_record( $in ) )
2980 if ( $record->{ "REC_TYPE" } eq "BED" )
2982 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2984 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2986 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2988 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2990 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2993 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2994 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2996 put_record( $record, $out );
2999 close $fh_phastcons if $fh_phastcons;
3005 # Martin A. Hansen, December 2007.
3007 # Folds sequences in stream into secondary structures.
3009 my ( $in, # handle to in stream
3010 $out, # handle to out stream
3015 my ( $record, $type, $struct, $index );
3017 while ( $record = get_record( $in ) )
3019 if ( $record->{ "SEQ" } )
3022 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3025 if ( $type ne "protein" )
3027 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3028 $record->{ "SEC_STRUCT" } = $struct;
3029 $record->{ "FREE_ENERGY" } = $index;
3030 $record->{ "SCORE" } = abs int $index;
3031 $record->{ "SIZE" } = length $struct;
3032 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3036 put_record( $record, $out );
3041 sub script_split_seq
3043 # Martin A. Hansen, August 2007.
3045 # Split a sequence in stream into words.
3047 my ( $in, # handle to in stream
3048 $out, # handle to out stream
3049 $options, # options hash
3054 my ( $record, $new_record, $i, $subseq, %lookup );
3056 $options->{ "word_size" } ||= 7;
3058 while ( $record = get_record( $in ) )
3060 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3062 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3064 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3066 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
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 );
3073 $lookup{ $subseq } = 1;
3077 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3078 $new_record->{ "SEQ" } = $subseq;
3080 put_record( $new_record, $out );
3086 put_record( $record, $out );
3092 sub script_split_bed
3094 # Martin A. Hansen, June 2008.
3096 # Split a BED record into overlapping windows.
3098 my ( $in, # handle to in stream
3099 $out, # handle to out stream
3100 $options, # options hash
3105 my ( $record, $new_record, $i );
3107 $options->{ "window_size" } ||= 20;
3108 $options->{ "step_size" } ||= 1;
3110 while ( $record = get_record( $in ) )
3112 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3114 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3116 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3118 $new_record->{ "REC_TYPE" } = "BED";
3119 $new_record->{ "CHR" } = $record->{ "CHR" };
3120 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3121 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3122 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3123 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3124 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3125 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3127 put_record( $new_record, $out );
3132 put_record( $record, $out );
3138 sub script_align_seq
3140 # Martin A. Hansen, August 2007.
3142 # Align sequences in stream.
3144 my ( $in, # handle to in stream
3145 $out, # handle to out stream
3150 my ( $record, @entries, $entry );
3152 while ( $record = get_record( $in ) )
3154 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3155 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3156 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3157 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3159 put_record( $record, $out );
3163 @entries = Maasha::Align::align( \@entries );
3165 foreach $entry ( @entries )
3167 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3170 SEQ_NAME => $entry->[ SEQ_NAME ],
3171 SEQ => $entry->[ SEQ ],
3174 put_record( $record, $out );
3182 # Martin A. Hansen, February 2008.
3184 # Using the first sequence in stream as reference, tile
3185 # all subsequent sequences based on pairwise alignments.
3187 my ( $in, # handle to in stream
3188 $out, # handle to out stream
3189 $options, # options hash
3194 my ( $record, $first, $ref_entry, @entries );
3198 while ( $record = get_record( $in ) )
3200 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3204 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3210 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3215 put_record( $record, $out );
3219 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3221 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3225 sub script_invert_align
3227 # Martin A. Hansen, February 2008.
3229 # Inverts an alignment showing only non-mathing residues
3230 # using the first sequence as reference.
3232 my ( $in, # handle to in stream
3233 $out, # handle to out stream
3234 $options, # options hash
3239 my ( $record, @entries );
3241 while ( $record = get_record( $in ) )
3243 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3245 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3249 put_record( $record, $out );
3253 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3255 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3259 sub script_patscan_seq
3261 # Martin A. Hansen, August 2007.
3263 # Locates patterns in sequences using scan_for_matches.
3265 my ( $in, # handle to in stream
3266 $out, # handle to out stream
3267 $options, # options hash
3272 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3274 if ( $options->{ "patterns" } ) {
3275 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3276 } elsif ( -f $options->{ "patterns_in" } ) {
3277 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3280 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3282 push @args, "-c" if $options->{ "comp" };
3283 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3284 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3286 $seq_file = "$BP_TMP/patscan.seq";
3287 $pat_file = "$BP_TMP/patscan.pat";
3288 $out_file = "$BP_TMP/patscan.out";
3290 $fh_out = Maasha::Common::write_open( $seq_file );
3294 while ( $record = get_record( $in ) )
3296 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3298 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3300 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3302 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3310 $arg = join " ", @args;
3311 $arg .= " -p" if $type eq "protein";
3313 foreach $pattern ( @{ $patterns } )
3315 $fh_out = Maasha::Common::write_open( $pat_file );
3317 print $fh_out "$pattern\n";
3321 if ( $options->{ 'genome' } ) {
3322 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3323 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3325 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3326 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3329 $fh_in = Maasha::Common::read_open( $out_file );
3331 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3333 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3335 if ( $options->{ 'genome' } )
3337 $result->{ "CHR" } = $result->{ "S_ID" };
3338 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3339 $result->{ "CHR_END" } = $result->{ "S_END" };
3341 delete $result->{ "S_ID" };
3342 delete $result->{ "S_BEG" };
3343 delete $result->{ "S_END" };
3347 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3350 put_record( $result, $out );
3362 sub script_create_blast_db
3364 # Martin A. Hansen, September 2007.
3366 # Creates a NCBI BLAST database with formatdb
3368 my ( $in, # handle to in stream
3369 $out, # handle to out stream
3370 $options, # options hash
3375 my ( $fh, $seq_type, $path, $record, $entry );
3377 $path = $options->{ "database" };
3379 $fh = Maasha::Common::write_open( $path );
3381 while ( $record = get_record( $in ) )
3383 put_record( $record, $out ) if not $options->{ "no_stream" };
3385 if ( $entry = record2fasta( $record ) )
3387 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3389 Maasha::Fasta::put_entry( $entry, $fh );
3395 if ( $seq_type eq "protein" ) {
3396 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3398 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3405 sub script_blast_seq
3407 # Martin A. Hansen, September 2007.
3409 # BLASTs sequences in stream against a given database.
3411 my ( $in, # handle to in stream
3412 $out, # handle to out stream
3413 $options, # options hash
3418 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3420 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3421 $options->{ "filter" } = "F";
3422 $options->{ "filter" } = "T" if $options->{ "filter" };
3423 $options->{ "cpus" } ||= 1;
3425 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3427 $tmp_in = "$BP_TMP/blast_query.seq";
3428 $tmp_out = "$BP_TMP/blast.result";
3430 $fh_out = Maasha::Common::write_open( $tmp_in );
3432 while ( $record = get_record( $in ) )
3434 if ( $entry = record2fasta( $record ) )
3436 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3438 Maasha::Fasta::put_entry( $entry, $fh_out );
3441 put_record( $record, $out );
3446 if ( -f $options->{ 'database' } . ".phr" ) {
3447 $s_type = "protein";
3449 $s_type = "nucleotide";
3452 if ( not $options->{ 'program' } )
3454 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3455 $options->{ 'program' } = "blastn";
3456 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3457 $options->{ 'program' } = "blastp";
3458 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3459 $options->{ 'program' } = "blastx";
3460 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3461 $options->{ 'program' } = "tblastn";
3465 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 );
3469 $fh_out = Maasha::Common::read_open( $tmp_out );
3473 while ( $line = <$fh_out> )
3477 next if $line =~ /^#/;
3479 @fields = split /\s+/, $line;
3481 $record->{ "REC_TYPE" } = "BLAST";
3482 $record->{ "Q_ID" } = $fields[ 0 ];
3483 $record->{ "S_ID" } = $fields[ 1 ];
3484 $record->{ "IDENT" } = $fields[ 2 ];
3485 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3486 $record->{ "MISMATCHES" } = $fields[ 4 ];
3487 $record->{ "GAPS" } = $fields[ 5 ];
3488 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3489 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3490 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3491 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3492 $record->{ "E_VAL" } = $fields[ 10 ];
3493 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3495 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3497 $record->{ "STRAND" } = '-';
3499 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3503 $record->{ "STRAND" } = '+';
3506 put_record( $record, $out );
3517 # Martin A. Hansen, August 2007.
3519 # BLATs sequences in stream against a given genome.
3521 my ( $in, # handle to in stream
3522 $out, # handle to out stream
3523 $options, # options hash
3528 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3530 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3532 $options->{ 'tile_size' } ||= 11;
3533 $options->{ 'one_off' } ||= 0;
3534 $options->{ 'min_identity' } ||= 90;
3535 $options->{ 'min_score' } ||= 0;
3536 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3538 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3539 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3540 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3541 $blat_args .= " -minScore=$options->{ 'min_score' }";
3542 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3543 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3545 $query_file = "$BP_TMP/blat.seq";
3547 $fh_out = Maasha::Common::write_open( $query_file );
3549 while ( $record = get_record( $in ) )
3551 if ( $entry = record2fasta( $record ) )
3553 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3554 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3557 put_record( $record, $out );
3562 $blat_args .= " -t=dnax" if $type eq "protein";
3563 $blat_args .= " -q=$type";
3565 $result_file = "$BP_TMP/blat.psl";
3567 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3571 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3573 map { put_record( $_, $out ) } @{ $entries };
3575 unlink $result_file;
3581 # Martin A. Hansen, July 2008.
3583 # soap sequences in stream against a given file or genome.
3585 my ( $in, # handle to in stream
3586 $out, # handle to out stream
3587 $options, # options hash
3592 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3594 $options->{ "seed_size" } ||= 10;
3595 $options->{ "mismatches" } ||= 2;
3596 $options->{ "gap_size" } ||= 0;
3597 $options->{ "cpus" } ||= 1;
3599 if ( $options->{ "genome" } ) {
3600 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3603 $tmp_in = "$BP_TMP/soap_query.seq";
3604 $tmp_out = "$BP_TMP/soap.result";
3606 $fh_out = Maasha::Common::write_open( $tmp_in );
3610 while ( $record = get_record( $in ) )
3612 if ( $entry = record2fasta( $record ) )
3614 Maasha::Fasta::put_entry( $entry, $fh_out );
3619 put_record( $record, $out );
3627 "-s $options->{ 'seed_size' }",
3630 "-v $options->{ 'mismatches' }",
3631 "-g $options->{ 'gap_size' }",
3632 "-p $options->{ 'cpus' }",
3633 "-d $options->{ 'in_file' }",
3637 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3639 Maasha::Common::run( "soap", $args, 1 );
3643 $fh_out = Maasha::Common::read_open( $tmp_out );
3647 while ( $line = <$fh_out> )
3651 @fields = split /\t/, $line;
3653 $record->{ "REC_TYPE" } = "SOAP";
3654 $record->{ "Q_ID" } = $fields[ 0 ];
3655 $record->{ "SCORE" } = $fields[ 3 ];
3656 $record->{ "STRAND" } = $fields[ 6 ];
3657 $record->{ "S_ID" } = $fields[ 7 ];
3658 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3659 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3661 put_record( $record, $out );
3671 sub script_match_seq
3673 # Martin A. Hansen, August 2007.
3675 # BLATs sequences in stream against a given genome.
3677 my ( $in, # handle to in stream
3678 $out, # handle to out stream
3679 $options, # options hash
3684 my ( $record, @entries, $results );
3686 $options->{ "word_size" } ||= 20;
3687 $options->{ "direction" } ||= "both";
3689 while ( $record = get_record( $in ) )
3691 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3692 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3695 put_record( $record, $out );
3698 if ( @entries == 1 )
3700 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3702 map { put_record( $_, $out ) } @{ $results };
3704 elsif ( @entries == 2 )
3706 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3708 map { put_record( $_, $out ) } @{ $results };
3713 sub script_create_vmatch_index
3715 # Martin A. Hansen, January 2008.
3717 # Create a vmatch index from sequences in the stream.
3719 my ( $in, # handle to in stream
3720 $out, # handle to out stream
3721 $options, # options hash
3726 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3728 if ( $options->{ "index_name" } )
3730 $file_tmp = $options->{ 'index_name' };
3731 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3734 while ( $record = get_record( $in ) )
3736 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3738 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3740 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3743 put_record( $record, $out ) if not $options->{ "no_stream" };
3746 if ( $options->{ "index_name" } )
3750 if ( $type eq "protein" ) {
3751 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3753 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3761 sub script_vmatch_seq
3763 # Martin A. Hansen, August 2007.
3765 # Vmatches sequences in stream against a given genome.
3767 my ( $in, # handle to in stream
3768 $out, # handle to out stream
3769 $options, # options hash
3774 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3776 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3778 if ( $options->{ "index_name" } )
3780 @index_files = $options->{ "index_name" };
3784 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3786 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3788 @index_files = sort keys %hash;
3791 while ( $record = get_record( $in ) )
3793 push @records, $record;
3795 put_record( $record, $out );
3798 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3802 $fh_in = Maasha::Common::read_open( $result_file );
3804 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3805 put_record( $record, $out );
3810 unlink $result_file;
3814 sub script_write_fasta
3816 # Martin A. Hansen, August 2007.
3818 # Write FASTA entries from sequences in stream.
3820 my ( $in, # handle to in stream
3821 $out, # handle to out stream
3822 $options, # options hash
3827 my ( $record, $fh, $entry );
3829 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3831 while ( $record = get_record( $in ) )
3833 if ( $entry = record2fasta( $record ) ) {
3834 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3837 put_record( $record, $out ) if not $options->{ "no_stream" };
3844 sub script_write_align
3846 # Martin A. Hansen, August 2007.
3848 # Write pretty alignments aligned sequences in stream.
3850 my ( $in, # handle to in stream
3851 $out, # handle to out stream
3852 $options, # options hash
3857 my ( $fh, $record, @entries );
3859 $fh = write_stream( $options->{ "data_out" } ) ;
3861 while ( $record = get_record( $in ) )
3863 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3864 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3867 put_record( $record, $out ) if not $options->{ "no_stream" };
3870 if ( scalar( @entries ) == 2 ) {
3871 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3872 } elsif ( scalar ( @entries ) > 2 ) {
3873 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3880 sub script_write_blast
3882 # Martin A. Hansen, November 2007.
3884 # Write data in blast table format (-m8 and 9).
3886 my ( $in, # handle to in stream
3887 $out, # handle to out stream
3888 $options, # options hash
3893 my ( $fh, $record, $first );
3895 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3899 while ( $record = get_record( $in ) )
3901 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3903 if ( $options->{ "comment" } and $first )
3905 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";
3910 if ( $record->{ "STRAND" } eq "-" ) {
3911 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3914 print $fh join( "\t",
3915 $record->{ "Q_ID" },
3916 $record->{ "S_ID" },
3917 $record->{ "IDENT" },
3918 $record->{ "ALIGN_LEN" },
3919 $record->{ "MISMATCHES" },
3920 $record->{ "GAPS" },
3921 $record->{ "Q_BEG" } + 1,
3922 $record->{ "Q_END" } + 1,
3923 $record->{ "S_BEG" } + 1,
3924 $record->{ "S_END" } + 1,
3925 $record->{ "E_VAL" },
3926 $record->{ "BIT_SCORE" }
3930 put_record( $record, $out ) if not $options->{ "no_stream" };
3937 sub script_write_tab
3939 # Martin A. Hansen, August 2007.
3941 # Write data as table.
3943 my ( $in, # handle to in stream
3944 $out, # handle to out stream
3945 $options, # options hash
3950 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3952 $options->{ "delimit" } ||= "\t";
3954 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3956 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3958 while ( $record = get_record( $in ) )
3963 if ( $options->{ "keys" } )
3965 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3969 foreach $key ( @{ $options->{ "keys" } } )
3971 if ( exists $record->{ $key } )
3973 push @keys, $key if $options->{ "comment" };
3974 push @vals, $record->{ $key };
3981 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3983 next if exists $no_keys{ $key };
3985 push @keys, $key if $options->{ "comment" };
3986 push @vals, $record->{ $key };
3990 if ( @keys and $options->{ "comment" } )
3992 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3994 delete $options->{ "comment" };
3997 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3999 put_record( $record, $out ) if not $options->{ "no_stream" };
4006 sub script_write_bed
4008 # Martin A. Hansen, August 2007.
4010 # Write BED format for the UCSC genome browser using records in stream.
4012 my ( $in, # handle to in stream
4013 $out, # handle to out stream
4014 $options, # options hash
4019 my ( $fh, $record, $new_record );
4021 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4023 while ( $record = get_record( $in ) )
4025 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4027 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4029 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4031 $new_record->{ "CHR" } = $record->{ "S_ID" };
4032 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4033 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4034 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4035 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4036 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4038 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4040 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4042 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4044 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4046 $new_record->{ "CHR" } = $record->{ "S_ID" };
4047 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4048 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4049 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4050 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4051 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4053 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4055 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4057 $new_record->{ "CHR" } = $record->{ "S_ID" };
4058 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4059 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4060 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4061 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4062 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4064 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4066 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4068 $new_record->{ "CHR" } = $record->{ "S_ID" };
4069 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4070 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4071 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4072 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4073 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4075 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4077 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4079 Maasha::UCSC::bed_put_entry( $record, $fh );
4082 put_record( $record, $out ) if not $options->{ "no_stream" };
4089 sub script_write_psl
4091 # Martin A. Hansen, August 2007.
4093 # Write PSL output from stream.
4095 my ( $in, # handle to in stream
4096 $out, # handle to out stream
4097 $options, # options hash
4102 my ( $fh, $record, @output, $first );
4106 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4108 while ( $record = get_record( $in ) )
4110 put_record( $record, $out ) if not $options->{ "no_stream" };
4112 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4114 Maasha::UCSC::psl_put_header( $fh ) if $first;
4115 Maasha::UCSC::psl_put_entry( $record, $fh );
4124 sub script_write_fixedstep
4126 # Martin A. Hansen, Juli 2008.
4128 # Write fixedStep entries from recrods in the stream.
4130 my ( $in, # handle to in stream
4131 $out, # handle to out stream
4132 $options, # options hash
4137 my ( $fh, $record, $vals );
4139 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4141 while ( $record = get_record( $in ) )
4143 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4145 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4147 $vals = $record->{ 'VALS' };
4151 print $fh "$vals\n";
4154 put_record( $record, $out ) if not $options->{ "no_stream" };
4161 sub script_write_2bit
4163 # Martin A. Hansen, March 2008.
4165 # Write sequence entries from stream in 2bit format.
4167 my ( $in, # handle to in stream
4168 $out, # handle to out stream
4169 $options, # options hash
4174 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4176 $mask = 1 if not $options->{ "no_mask" };
4178 $tmp_file = "$BP_TMP/write_2bit.fna";
4179 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4181 $fh_out = write_stream( $options->{ "data_out" } );
4183 while ( $record = get_record( $in ) )
4185 if ( $entry = record2fasta( $record ) ) {
4186 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4189 put_record( $record, $out ) if not $options->{ "no_stream" };
4194 $fh_in = Maasha::Common::read_open( $tmp_file );
4196 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4205 sub script_write_solid
4207 # Martin A. Hansen, April 2008.
4209 # Write di-base encoded Solid sequence from entries in stream.
4211 my ( $in, # handle to in stream
4212 $out, # handle to out stream
4213 $options, # options hash
4218 my ( $record, $fh, $entry );
4220 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4222 while ( $record = get_record( $in ) )
4224 if ( $entry = record2fasta( $record ) )
4226 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4228 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4231 put_record( $record, $out ) if not $options->{ "no_stream" };
4238 sub script_plot_seqlogo
4240 # Martin A. Hansen, August 2007.
4242 # Calculates and writes a sequence logo for alignments.
4244 my ( $in, # handle to in stream
4245 $out, # handle to out stream
4246 $options, # options hash
4251 my ( $record, @entries, $logo, $fh );
4253 while ( $record = get_record( $in ) )
4255 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4256 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4259 put_record( $record, $out ) if not $options->{ "no_stream" };
4262 $logo = Maasha::Plot::seq_logo( \@entries );
4264 $fh = write_stream( $options->{ "data_out" } );
4272 sub script_plot_phastcons_profiles
4274 # Martin A. Hansen, January 2008.
4276 # Plots PhastCons profiles.
4278 my ( $in, # handle to in stream
4279 $out, # handle to out stream
4280 $options, # options hash
4285 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4287 $options->{ "title" } ||= "PhastCons Profiles";
4289 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4290 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4292 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4293 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4295 while ( $record = get_record( $in ) )
4297 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4299 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4301 push @{ $AoA }, [ @{ $scores } ];
4304 put_record( $record, $out ) if not $options->{ "no_stream" };
4307 Maasha::UCSC::phastcons_normalize( $AoA );
4309 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4310 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4312 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4314 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4316 $fh = write_stream( $options->{ "data_out" } );
4318 print $fh "$_\n" foreach @{ $plot };
4324 sub script_analyze_bed
4326 # Martin A. Hansen, March 2008.
4328 # Analyze BED entries in stream.
4330 my ( $in, # handle to in stream
4331 $out, # handle to out stream
4332 $options, # options hash
4339 while ( $record = get_record( $in ) )
4341 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4343 put_record( $record, $out );
4348 sub script_analyze_vals
4350 # Martin A. Hansen, August 2007.
4352 # Analyze values for given keys in stream.
4354 my ( $in, # handle to in stream
4355 $out, # handle to out stream
4356 $options, # options hash
4361 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4363 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4365 while ( $record = get_record( $in ) )
4367 foreach $key ( keys %{ $record } )
4369 next if $options->{ "keys" } and not exists $key_hash{ $key };
4371 $analysis->{ $key }->{ "COUNT" }++;
4373 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4375 $analysis->{ $key }->{ "TYPE" } = "num";
4376 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4377 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4378 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4382 $len = length $record->{ $key };
4384 $analysis->{ $key }->{ "TYPE" } = "alph";
4385 $analysis->{ $key }->{ "SUM" } += $len;
4386 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4387 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4391 put_record( $record, $out ) if not $options->{ "no_stream" };
4394 foreach $key ( keys %{ $analysis } )
4396 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4397 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4400 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4410 if ( $options->{ "keys" } ) {
4411 @keys = @{ $options->{ "keys" } };
4413 @keys = keys %{ $analysis };
4416 foreach $key ( @keys )
4418 $keys .= sprintf "% 15s", $key;
4419 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4420 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4421 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4422 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4423 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4424 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4427 print $out "$keys\n";
4428 print $out "$types\n";
4429 print $out "$counts\n";
4430 print $out "$mins\n";
4431 print $out "$maxs\n";
4432 print $out "$sums\n";
4433 print $out "$means\n";
4437 sub script_head_records
4439 # Martin A. Hansen, August 2007.
4441 # Display the first sequences in stream.
4443 my ( $in, # handle to in stream
4444 $out, # handle to out stream
4445 $options, # options hash
4450 my ( $record, $count );
4452 $options->{ "num" } ||= 10;
4456 while ( $record = get_record( $in ) )
4460 put_record( $record, $out );
4462 last if $count == $options->{ "num" };
4467 sub script_remove_keys
4469 # Martin A. Hansen, August 2007.
4471 # Remove keys from stream.
4473 my ( $in, # handle to in stream
4474 $out, # handle to out stream
4475 $options, # options hash
4480 my ( $record, $new_record );
4482 while ( $record = get_record( $in ) )
4484 if ( $options->{ "keys" } )
4486 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4488 elsif ( $options->{ "save_keys" } )
4490 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4492 $record = $new_record;
4495 put_record( $record, $out ) if keys %{ $record };
4500 sub script_rename_keys
4502 # Martin A. Hansen, August 2007.
4504 # Rename keys in stream.
4506 my ( $in, # handle to in stream
4507 $out, # handle to out stream
4508 $options, # options hash
4515 while ( $record = get_record( $in ) )
4517 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4519 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4521 delete $record->{ $options->{ "keys" }->[ 0 ] };
4524 put_record( $record, $out );
4529 sub script_uniq_vals
4531 # Martin A. Hansen, August 2007.
4533 # Find unique values in stream.
4535 my ( $in, # handle to in stream
4536 $out, # handle to out stream
4537 $options, # options hash
4542 my ( %hash, $record );
4544 while ( $record = get_record( $in ) )
4546 if ( $record->{ $options->{ "key" } } )
4548 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4550 put_record( $record, $out );
4552 $hash{ $record->{ $options->{ "key" } } } = 1;
4554 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4556 put_record( $record, $out );
4560 $hash{ $record->{ $options->{ "key" } } } = 1;
4565 put_record( $record, $out );
4571 sub script_merge_vals
4573 # Martin A. Hansen, August 2007.
4575 # Rename keys in stream.
4577 my ( $in, # handle to in stream
4578 $out, # handle to out stream
4579 $options, # options hash
4584 my ( $record, @join, $i );
4586 $options->{ "delimit" } ||= '_';
4588 while ( $record = get_record( $in ) )
4590 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4592 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4594 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4595 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4598 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4601 put_record( $record, $out );
4606 sub script_merge_records
4608 # Martin A. Hansen, July 2008.
4610 # Merges records in the stream based on identical values of two given keys.
4612 my ( $in, # handle to in stream
4613 $out, # handle to out stream
4614 $options, # options hash
4619 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4620 $num1, $num2, $num, $cmp, $i );
4622 $merge = $options->{ "merge" } || "AandB";
4624 $file1 = "$BP_TMP/merge_records1.tmp";
4625 $file2 = "$BP_TMP/merge_records2.tmp";
4627 $fh1 = Maasha::Common::write_open( $file1 );
4628 $fh2 = Maasha::Common::write_open( $file2 );
4630 $key1 = $options->{ "keys" }->[ 0 ];
4631 $key2 = $options->{ "keys" }->[ 1 ];
4633 $num = $key2 =~ s/n$//;
4637 while ( $record = get_record( $in ) )
4639 if ( exists $record->{ $key1 } )
4642 @vals1 = $record->{ $key1 };
4644 delete $record->{ $key1 };
4646 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4648 print $fh1 join( "\t", @vals1 ), "\n";
4652 elsif ( exists $record->{ $key2 } )
4655 @vals2 = $record->{ $key2 };
4657 delete $record->{ $key2 };
4659 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4661 print $fh2 join( "\t", @vals2 ), "\n";
4672 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4673 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4677 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4678 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4681 $fh1 = Maasha::Common::read_open( $file1 );
4682 $fh2 = Maasha::Common::read_open( $file2 );
4684 @vals1 = Maasha::Common::get_fields( $fh1 );
4685 @vals2 = Maasha::Common::get_fields( $fh2 );
4687 while ( $num1 > 0 and $num2 > 0 )
4692 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4694 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4699 if ( $merge =~ /^(AorB|AnotB)$/ )
4701 for ( $i = 0; $i < @keys1; $i++ ) {
4702 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4705 put_record( $record, $out );
4708 @vals1 = Maasha::Common::get_fields( $fh1 );
4713 if ( $merge =~ /^(BorA|BnotA)$/ )
4715 for ( $i = 0; $i < @keys2; $i++ ) {
4716 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4719 put_record( $record, $out );
4722 @vals2 = Maasha::Common::get_fields( $fh2 );
4727 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4729 for ( $i = 0; $i < @keys1; $i++ ) {
4730 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4733 for ( $i = 1; $i < @keys2; $i++ ) {
4734 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4737 put_record( $record, $out );
4740 @vals1 = Maasha::Common::get_fields( $fh1 );
4741 @vals2 = Maasha::Common::get_fields( $fh2 );
4753 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4757 for ( $i = 0; $i < @keys1; $i++ ) {
4758 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4761 put_record( $record, $out );
4764 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4768 for ( $i = 0; $i < @keys2; $i++ ) {
4769 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4772 put_record( $record, $out );
4779 # Martin A. Hansen, August 2007.
4781 # Grab for records in stream.
4783 my ( $in, # handle to in stream
4784 $out, # handle to out stream
4785 $options, # options hash
4790 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4792 if ( $options->{ "patterns" } )
4794 $patterns = [ split ",", $options->{ "patterns" } ];
4796 elsif ( -f $options->{ "patterns_in" } )
4798 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4800 elsif ( -f $options->{ "exact_in" } )
4802 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4804 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4809 if ( $options->{ "eval" } )
4811 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4819 while ( $record = get_record( $in ) )
4825 if ( $options->{ "keys" } )
4827 foreach $key ( @{ $options->{ "keys" } } )
4829 if ( exists $lookup_hash{ $record->{ $key } } )
4838 foreach $key ( keys %{ $record } )
4840 if ( not $options->{ "vals_only" } )
4842 if ( exists $lookup_hash{ $key } )
4849 if ( not $options->{ "keys_only" } )
4851 if ( exists $lookup_hash{ $record->{ $key } } )
4862 foreach $pattern ( @{ $patterns } )
4864 if ( $options->{ "keys" } )
4866 foreach $key ( @{ $options->{ "keys" } } )
4868 $pos = index $record->{ $key }, $pattern;
4870 goto FOUND if $pos >= 0;
4875 foreach $key ( keys %{ $record } )
4877 if ( not $options->{ "vals_only" } )
4879 $pos = index $key, $pattern;
4881 goto FOUND if $pos >= 0;
4884 if ( not $options->{ "keys_only" } )
4886 $pos = index $record->{ $key }, $pattern;
4888 goto FOUND if $pos >= 0;
4894 elsif ( $options->{ "regex" } )
4896 if ( $options->{ "keys" } )
4898 foreach $key ( @{ $options->{ "keys" } } )
4900 if ( $options->{ "case_insensitive" } ) {
4901 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4903 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4906 goto FOUND if $pos >= 0;
4911 foreach $key ( keys %{ $record } )
4913 if ( not $options->{ "vals_only" } )
4915 if ( $options->{ "case_insensitive" } ) {
4916 $pos = 1 if $key =~ /$options->{'regex'}/i;
4918 $pos = 1 if $key =~ /$options->{'regex'}/;
4921 goto FOUND if $pos >= 0;
4924 if ( not $options->{ "keys_only" } )
4926 if ( $options->{ "case_insensitive" } ) {
4927 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4929 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4932 goto FOUND if $pos >= 0;
4937 elsif ( $options->{ "eval" } )
4939 if ( defined $record->{ $key } )
4941 if ( $op eq "<" and $record->{ $key } < $val ) {
4942 $pos = 1 and goto FOUND;
4943 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4944 $pos = 1 and goto FOUND;
4945 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4946 $pos = 1 and goto FOUND;
4947 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4948 $pos = 1 and goto FOUND;
4949 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4950 $pos = 1 and goto FOUND;
4951 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4952 $pos = 1 and goto FOUND;
4953 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4954 $pos = 1 and goto FOUND;
4955 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4956 $pos = 1 and goto FOUND;
4963 if ( $pos >= 0 and not $options->{ "invert" } ) {
4964 put_record( $record, $out );
4965 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4966 put_record( $record, $out );
4974 # Martin A. Hansen, August 2007.
4976 # Evaluate extression for records in stream.
4978 my ( $in, # handle to in stream
4979 $out, # handle to out stream
4980 $options, # options hash
4985 my ( $record, $eval_key, $eval_val, $check, @keys );
4987 while ( $record = get_record( $in ) )
4989 if ( $options->{ "eval" } )
4991 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4999 @keys = split /\W+/, $eval_val;
5000 @keys = grep { ! /^\d+$/ } @keys;
5005 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5007 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5010 put_record( $record, $out );
5017 # Martin A. Hansen, June 2008.
5021 my ( $in, # handle to in stream
5022 $out, # handle to out stream
5023 $options, # options hash
5028 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5030 while ( $record = get_record( $in ) )
5034 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5036 push @rows, $record->{ $key };
5040 push @matrix, [ @rows ];
5045 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5047 foreach $row ( @matrix )
5049 for ( $i = 0; $i < @{ $row }; $i++ ) {
5050 $record->{ "V$i" } = $row->[ $i ];
5053 put_record( $record, $out );
5058 sub script_add_ident
5060 # Martin A. Hansen, May 2008.
5062 # Add a unique identifier to each record in stream.
5064 my ( $in, # handle to in stream
5065 $out, # handle to out stream
5066 $options, # options hash
5071 my ( $record, $key, $prefix, $i );
5073 $key = $options->{ "key" } || "ID";
5074 $prefix = $options->{ "prefix" } || "ID";
5078 while ( $record = get_record( $in ) )
5080 $record->{ $key } = sprintf( "$prefix%08d", $i );
5082 put_record( $record, $out );
5089 sub script_count_records
5091 # Martin A. Hansen, August 2007.
5093 # Count records in stream.
5095 my ( $in, # handle to in stream
5096 $out, # handle to out stream
5097 $options, # options hash
5102 my ( $record, $count, $result, $fh, $line );
5106 if ( $options->{ "no_stream" } )
5108 while ( $line = <$in> )
5112 $count++ if $line eq "---";
5117 while ( $record = get_record( $in ) )
5119 put_record( $record, $out );
5125 $result = { "RECORDS_COUNT" => $count };
5127 $fh = write_stream( $options->{ "data_out" } );
5129 put_record( $result, $fh );
5135 sub script_random_records
5137 # Martin A. Hansen, August 2007.
5139 # Pick a number or random records from stream.
5141 my ( $in, # handle to in stream
5142 $out, # handle to out stream
5143 $options, # options hash
5148 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5150 $options->{ "num" } ||= 10;
5152 $tmp_file = "$BP_TMP/random_records.tmp";
5154 $fh_out = Maasha::Common::write_open( $tmp_file );
5158 while ( $record = get_record( $in ) )
5160 put_record( $record, $fh_out );
5170 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5172 while ( $i < $options->{ "num" } )
5174 $rand = int( rand( $count ) );
5176 if ( not exists $rand_hash{ $rand } )
5178 $rand_hash{ $rand } = 1;
5180 $max = $rand if $rand > $max;
5186 $fh_in = Maasha::Common::read_open( $tmp_file );
5190 while ( $record = get_record( $fh_in ) )
5192 put_record( $record, $out ) if exists $rand_hash{ $count };
5194 last if $count == $max;
5205 sub script_sort_records
5207 # Martin A. Hansen, August 2007.
5209 # Sort to sort records according to keys.
5211 my ( $in, # handle to in stream
5212 $out, # handle to out stream
5213 $options, # options hash
5218 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5220 foreach $key ( @{ $options->{ "keys" } } )
5222 if ( $key =~ s/n$// ) {
5223 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5225 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5229 $sort_str = join " or ", @sort_cmd;
5230 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5232 while ( $record = get_record( $in ) ) {
5233 push @records, $record;
5236 @records = sort $sort_sub @records;
5238 if ( $options->{ "reverse" } )
5240 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5241 put_record( $records[ $i ], $out );
5246 for ( $i = 0; $i < scalar @records; $i++ ) {
5247 put_record( $records[ $i ], $out );
5253 sub script_count_vals
5255 # Martin A. Hansen, August 2007.
5257 # Count records in stream.
5259 my ( $in, # handle to in stream
5260 $out, # handle to out stream
5261 $options, # options hash
5266 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5268 $tmp_file = "$BP_TMP/count_cache.tmp";
5270 $fh_out = Maasha::Common::write_open( $tmp_file );
5275 while ( $record = get_record( $in ) )
5277 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5279 push @records, $record;
5281 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5283 map { put_record( $_, $fh_out ) } @records;
5290 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5301 $fh_in = Maasha::Common::read_open( $tmp_file );
5303 while ( $record = get_record( $fh_in ) )
5305 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5307 put_record( $record, $out );
5309 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5317 foreach $record ( @records )
5319 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5321 put_record( $record, $out );
5328 sub script_plot_histogram
5330 # Martin A. Hansen, September 2007.
5332 # Plot a simple histogram for a given key using GNU plot.
5334 my ( $in, # handle to in stream
5335 $out, # handle to out stream
5336 $options, # options hash
5341 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5343 $options->{ "title" } ||= "Histogram";
5344 $options->{ "sort" } ||= "num";
5346 while ( $record = get_record( $in ) )
5348 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5350 put_record( $record, $out ) if not $options->{ "no_stream" };
5353 if ( $options->{ "sort" } eq "num" ) {
5354 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5356 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5359 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5361 $fh = write_stream( $options->{ "data_out" } );
5363 print $fh "$_\n" foreach @{ $result };
5369 sub script_plot_lendist
5371 # Martin A. Hansen, August 2007.
5373 # Plot length distribution using GNU plot.
5375 my ( $in, # handle to in stream
5376 $out, # handle to out stream
5377 $options, # options hash
5382 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5384 $options->{ "title" } ||= "Length Distribution";
5386 while ( $record = get_record( $in ) )
5388 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5390 put_record( $record, $out ) if not $options->{ "no_stream" };
5393 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5395 for ( $i = 0; $i < $max; $i++ ) {
5396 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5399 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5401 $fh = write_stream( $options->{ "data_out" } );
5403 print $fh "$_\n" foreach @{ $result };
5409 sub script_plot_chrdist
5411 # Martin A. Hansen, August 2007.
5413 # Plot chromosome distribution using GNU plot.
5415 my ( $in, # handle to in stream
5416 $out, # handle to out stream
5417 $options, # options hash
5422 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5424 $options->{ "title" } ||= "Chromosome Distribution";
5426 while ( $record = get_record( $in ) )
5428 if ( $record->{ "CHR" } ) { # generic
5429 $data_hash{ $record->{ "CHR" } }++;
5430 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5431 $data_hash{ $record->{ "S_ID" } }++;
5432 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5433 $data_hash{ $record->{ "S_ID" } }++;
5434 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5435 $data_hash{ $record->{ "S_ID" } }++;
5438 put_record( $record, $out ) if not $options->{ "no_stream" };
5441 foreach $elem ( keys %data_hash )
5445 $sort_key =~ s/chr//i;
5447 $sort_key =~ s/^X(.*)/99$1/;
5448 $sort_key =~ s/^Y(.*)/99$1/;
5449 $sort_key =~ s/^Z(.*)/999$1/;
5450 $sort_key =~ s/^M(.*)/9999$1/;
5451 $sort_key =~ s/^U(.*)/99999$1/;
5453 $count = $sort_key =~ tr/_//;
5455 $sort_key =~ s/_.*/"999999" x $count/ex;
5457 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5460 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5462 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5464 $fh = write_stream( $options->{ "data_out" } );
5466 print $fh "$_\n" foreach @{ $result };
5472 sub script_plot_karyogram
5474 # Martin A. Hansen, August 2007.
5476 # Plot hits on karyogram.
5478 my ( $in, # handle to in stream
5479 $out, # handle to out stream
5480 $options, # options hash
5485 my ( %options, $record, @data, $fh, $result, %data_hash );
5487 $options->{ "genome" } ||= "human";
5488 $options->{ "feat_color" } ||= "black";
5490 while ( $record = get_record( $in ) )
5492 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5494 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5497 put_record( $record, $out ) if not $options->{ "no_stream" };
5500 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5502 $fh = write_stream( $options->{ "data_out" } );
5510 sub script_plot_matches
5512 # Martin A. Hansen, August 2007.
5514 # Plot matches in 2D generating a dotplot.
5516 my ( $in, # handle to in stream
5517 $out, # handle to out stream
5518 $options, # options hash
5523 my ( $record, @data, $fh, $result, %data_hash );
5525 $options->{ "direction" } ||= "both";
5527 while ( $record = get_record( $in ) )
5529 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5530 push @data, $record;
5533 put_record( $record, $out ) if not $options->{ "no_stream" };
5536 $options->{ "title" } ||= "plot_matches";
5537 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5538 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5540 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5542 $fh = write_stream( $options->{ "data_out" } );
5544 print $fh "$_\n" foreach @{ $result };
5550 sub script_length_vals
5552 # Martin A. Hansen, August 2007.
5554 # Determine the length of the value for given keys.
5556 my ( $in, # handle to in stream
5557 $out, # handle to out stream
5558 $options, # options hash
5563 my ( $record, $key );
5565 while ( $record = get_record( $in ) )
5567 foreach $key ( @{ $options->{ "keys" } } )
5569 if ( $record->{ $key } ) {
5570 $record->{ $key . "_LEN" } = length $record->{ $key };
5574 put_record( $record, $out );
5581 # Martin A. Hansen, August 2007.
5583 # Calculates the sums for values of given keys.
5585 my ( $in, # handle to in stream
5586 $out, # handle to out stream
5587 $options, # options hash
5592 my ( $record, $key, %sum_hash, $fh );
5594 while ( $record = get_record( $in ) )
5596 foreach $key ( @{ $options->{ "keys" } } )
5598 if ( $record->{ $key } ) {
5599 $sum_hash{ $key } += $record->{ $key };
5603 put_record( $record, $out ) if not $options->{ "no_stream" };
5606 $fh = write_stream( $options->{ "data_out" } );
5608 foreach $key ( @{ $options->{ "keys" } } ) {
5609 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5616 sub script_mean_vals
5618 # Martin A. Hansen, August 2007.
5620 # Calculate the mean of values of given keys.
5622 my ( $in, # handle to in stream
5623 $out, # handle to out stream
5624 $options, # options hash
5629 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5631 while ( $record = get_record( $in ) )
5633 foreach $key ( @{ $options->{ "keys" } } )
5635 if ( $record->{ $key } )
5637 $sum_hash{ $key } += $record->{ $key };
5638 $count_hash{ $key }++;
5642 put_record( $record, $out ) if not $options->{ "no_stream" };
5645 $fh = write_stream( $options->{ "data_out" } );
5647 foreach $key ( @{ $options->{ "keys" } } )
5649 if ( $count_hash{ $key } ) {
5650 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5655 put_record( { $key . "_MEAN" => $mean } , $fh );
5662 sub script_median_vals
5664 # Martin A. Hansen, March 2008.
5666 # Calculate the median values of given keys.
5668 my ( $in, # handle to in stream
5669 $out, # handle to out stream
5670 $options, # options hash
5675 my ( $record, $key, %median_hash, $median, $fh );
5677 while ( $record = get_record( $in ) )
5679 foreach $key ( @{ $options->{ "keys" } } ) {
5680 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5683 put_record( $record, $out ) if not $options->{ "no_stream" };
5686 $fh = write_stream( $options->{ "data_out" } );
5688 foreach $key ( @{ $options->{ "keys" } } )
5690 if ( $median_hash{ $key } ) {
5691 $median = Maasha::Calc::median( $median_hash{ $key } );
5696 put_record( { $key . "_MEDIAN" => $median } , $fh );
5705 # Martin A. Hansen, February 2008.
5707 # Determine the maximum values of given keys.
5709 my ( $in, # handle to in stream
5710 $out, # handle to out stream
5711 $options, # options hash
5716 my ( $record, $key, $fh, %max_hash, $max_record );
5718 while ( $record = get_record( $in ) )
5720 foreach $key ( @{ $options->{ "keys" } } )
5722 if ( $record->{ $key } )
5724 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5728 put_record( $record, $out ) if not $options->{ "no_stream" };
5731 $fh = write_stream( $options->{ "data_out" } );
5733 foreach $key ( @{ $options->{ "keys" } } )
5735 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5738 put_record( $max_record, $fh );
5746 # Martin A. Hansen, February 2008.
5748 # Determine the minimum values of given keys.
5750 my ( $in, # handle to in stream
5751 $out, # handle to out stream
5752 $options, # options hash
5757 my ( $record, $key, $fh, %min_hash, $min_record );
5759 while ( $record = get_record( $in ) )
5761 foreach $key ( @{ $options->{ "keys" } } )
5763 if ( defined $record->{ $key } )
5765 if ( exists $min_hash{ $key } ) {
5766 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5768 $min_hash{ $key } = $record->{ $key };
5773 put_record( $record, $out ) if not $options->{ "no_stream" };
5776 $fh = write_stream( $options->{ "data_out" } );
5778 foreach $key ( @{ $options->{ "keys" } } )
5780 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5783 put_record( $min_record, $fh );
5789 sub script_upload_to_ucsc
5791 # Martin A. Hansen, August 2007.
5793 # Calculate the mean of values of given keys.
5795 my ( $in, # handle to in stream
5796 $out, # handle to out stream
5797 $options, # options hash
5802 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5803 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5805 $options->{ "short_label" } ||= $options->{ 'table' };
5806 $options->{ "long_label" } ||= $options->{ 'table' };
5807 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5808 $options->{ "priority" } ||= 1;
5809 $options->{ "visibility" } ||= "pack";
5810 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5811 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5813 $file = "$BP_TMP/ucsc_upload.tmp";
5821 if ( $options->{ 'wiggle' } )
5823 $options->{ "visibility" } = "full";
5825 while ( $record = get_record( $in ) )
5827 put_record( $record, $out ) if not $options->{ "no_stream" };
5829 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5830 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5831 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5833 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5835 $fh_out = $fh_hash{ $record->{ "CHR" } };
5837 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5840 map { close $_ } keys %fh_hash;
5842 $fh_out = Maasha::Common::write_open( $file );
5844 foreach $chr ( sort keys %fh_hash )
5846 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5848 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5852 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5854 $chr = $entry->{ 'CHR' };
5855 $beg = $entry->{ 'CHR_BEG' };
5856 $end = $entry->{ 'CHR_END' };
5857 $q_id = $entry->{ 'Q_ID' };
5859 if ( $options->{ "score" } ) {
5860 $clones = $entry->{ 'SCORE' };
5861 } if ( $q_id =~ /_(\d+)$/ ) {
5871 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5876 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5877 $block->[ $i ] += $clones;
5880 $max = Maasha::Calc::max( $max, $end );
5889 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5890 $block->[ $i ] += $clones;
5897 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out, $options->{ "log10" } );
5899 unlink "$BP_TMP/$chr";
5904 $wig_file = "$options->{ 'table' }.wig";
5905 $wib_file = "$options->{ 'table' }.wib";
5907 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5909 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5911 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5913 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5914 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5924 $fh_out = Maasha::Common::write_open( $file );
5926 while ( $record = get_record( $in ) )
5928 put_record( $record, $out ) if not $options->{ "no_stream" };
5930 if ( $record->{ "REC_TYPE" } eq "PSL" )
5932 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5933 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5937 $format = "PSL" if not $format;
5939 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5941 # chrom chromStart chromEnd name score strand size secStr conf
5943 print $fh_out join ( "\t",
5945 $record->{ "CHR_BEG" },
5946 $record->{ "CHR_END" } + 1,
5947 $record->{ "Q_ID" },
5948 $record->{ "SCORE" },
5949 $record->{ "STRAND" },
5950 $record->{ "SIZE" },
5951 $record->{ "SEC_STRUCT" },
5952 $record->{ "CONF" },
5955 $format = "BED_SS" if not $format;
5957 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5959 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5961 $format = "BED" if not $format;
5962 $columns = $record->{ "BED_COLS" } if not $columns;
5964 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5966 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5968 $format = "BED" if not $format;
5969 $columns = 6 if not $columns;
5971 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5973 $record->{ "CHR" } = $record->{ "S_ID" };
5974 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5975 $record->{ "CHR_END" } = $record->{ "S_END" };
5976 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5978 $format = "BED" if not $format;
5979 $columns = 6 if not $columns;
5981 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5983 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5985 $record->{ "CHR" } = $record->{ "S_ID" };
5986 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5987 $record->{ "CHR_END" } = $record->{ "S_END" };
5988 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5989 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5991 $format = "BED" if not $format;
5992 $columns = 6 if not $columns;
5994 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5997 if ( $i == $options->{ "chunk_size" } )
6001 if ( $format eq "BED" ) {
6002 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6003 } elsif ( $format eq "PSL" ) {
6004 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6013 $fh_out = Maasha::Common::write_open( $file );
6022 if ( exists $options->{ "database" } and $options->{ "table" } )
6024 if ( $format eq "BED" )
6026 $type = "bed $columns";
6028 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6030 elsif ( $format eq "BED_SS" )
6032 $options->{ "sec_struct" } = 1;
6034 $type = "sec_struct";
6036 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6038 elsif ( $format eq "PSL" )
6042 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6044 elsif ( $format eq "WIGGLE" )
6048 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6053 Maasha::UCSC::update_my_tracks( $options, $type );
6058 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6063 # Martin A. Hansen, July 2008.
6065 # Given a biopiece record converts it to a FASTA record.
6066 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6067 # tried in that order.
6069 my ( $record, # record
6074 my ( $seq_name, $seq );
6076 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6077 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6079 if ( defined $seq_name and defined $seq ) {
6080 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6089 # Martin A. Hansen, July 2007.
6091 # Opens a stream to STDIN or a file,
6093 my ( $path, # path - OPTIONAL
6096 # Returns filehandle.
6100 if ( not -t STDIN ) {
6101 $fh = Maasha::Common::read_stdin();
6102 } elsif ( not $path ) {
6103 # Maasha::Common::error( qq(no data stream) );
6105 $fh = Maasha::Common::read_open( $path );
6108 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6116 # Martin A. Hansen, August 2007.
6118 # Opens a stream to STDOUT or a file.
6120 my ( $path, # path - OPTIONAL
6121 $gzip, # compress data - OPTIONAL
6124 # Returns filehandle.
6129 $fh = Maasha::Common::write_open( $path, $gzip );
6131 $fh = Maasha::Common::write_stdout();
6140 # Martin A. Hansen, July 2007.
6142 # Reads one record at a time and converts that record
6143 # to a Perl data structure (a hash) which is returned.
6145 my ( $fh, # handle to stream
6150 my ( $block, @lines, $line, $key, $value, %record );
6152 local $/ = "\n---\n";
6158 return if not defined $block;
6160 @lines = split "\n", $block;
6162 foreach $line ( @lines )
6164 ( $key, $value ) = split ": ", $line, 2;
6166 $record{ $key } = $value;
6169 return wantarray ? %record : \%record;
6175 # Martin A. Hansen, July 2007.
6177 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6179 my ( $data, # data structure
6180 $fh, # file handle - OPTIONAL
6185 if ( scalar keys %{ $data } )
6189 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6194 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6205 # Martin A. Hansen, November 2007.
6207 # Extracts files from an explicit GetOpt::Long argument
6208 # allowing for the use of glob. E.g.
6209 # --data_in=test.fna
6210 # --data_in=test.fna,test2.fna
6212 # --data_in=test.fna,/dir/*.fna
6214 my ( $option, # option from GetOpt::Long
6219 my ( $elem, @files );
6221 foreach $elem ( split ",", $option )
6225 } elsif ( $elem =~ /\*/ ) {
6226 push @files, glob( $elem );
6230 return wantarray ? @files : \@files;
6236 # Martin A. Hansen, April 2008.
6238 # Removes temporary directory and exits gracefully.
6239 # This subroutine is meant to be run always as the last
6240 # thing even if a script is dies or is interrupted
6243 my ( $sig, # signal from the %SIG
6246 # print STDERR "signal->$sig<-\n";
6254 if ( $sig =~ /MAASHA_ERROR/ ) {
6255 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6256 } elsif ( $sig eq "INT" ) {
6257 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6258 } elsif ( $sig eq "TERM" ) {
6259 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6261 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6273 # Martin A. Hansen, July 2008.
6275 # Cleans out any unused temporary files and direcotries in BP_TMP.
6279 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6281 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6283 $curr_pid = Maasha::Common::get_processid();
6285 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6287 foreach $dir ( @dirs )
6289 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6295 if ( $user eq Maasha::Common::get_user() )
6297 if ( not Maasha::Common::process_running( $pid ) )
6299 # print STDERR "Removing stale dir: $dir\n";
6300 Maasha::Common::dir_remove( $dir );
6302 elsif ( $pid == $curr_pid )
6304 # print STDERR "Removing current dir: $dir\n";
6305 Maasha::Common::dir_remove( $dir );
6319 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<