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" )
944 # print STDERR Dumper( \@options );
951 # print STDERR Dumper( \%options );
953 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
954 return wantarray ? %options : \%options;
957 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
958 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
959 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
960 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
961 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
962 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
963 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
964 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
966 # ---- check arguments ----
968 if ( $options{ 'data_in' } )
970 $options{ "files" } = getopt_files( $options{ 'data_in' } );
972 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
975 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
977 # print STDERR Dumper( \%options );
979 foreach $opt ( keys %options )
981 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
983 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
985 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+$/ )
987 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
989 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
991 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
993 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
995 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
997 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
999 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
1001 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1003 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1005 elsif ( $opt eq "genome" and $script ne "format_genome" )
1007 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1008 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1010 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1011 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1014 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1016 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1018 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1020 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1022 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1024 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1028 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1029 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1030 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1031 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1032 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1033 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1034 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1035 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1036 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" };
1037 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1038 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" };
1040 if ( $script eq "upload_to_ucsc" )
1042 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1043 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1046 return wantarray ? %options : \%options;
1050 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1053 sub script_print_usage
1055 # Martin A. Hansen, January 2008.
1057 # Retrieves usage information from file and
1058 # prints this nicely formatted.
1060 my ( $in, # handle to in stream
1061 $out, # handle to out stream
1062 $options, # options hash
1067 my ( $file, $wiki, $lines );
1069 if ( $options->{ 'data_in' } ) {
1070 $file = $options->{ 'data_in' };
1072 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1075 $wiki = Maasha::Gwiki::gwiki_read( $file );
1077 if ( not $options->{ "help" } ) {
1078 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1081 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1083 print STDERR "$_\n" foreach @{ $lines };
1089 sub script_list_biopieces
1091 # Martin A. Hansen, January 2008.
1093 # Prints the synopsis from the usage for each of the biopieces.
1095 my ( $in, # handle to in stream
1096 $out, # handle to out stream
1097 $options, # options hash
1102 my ( @files, $file, $wiki, $program, $synopsis );
1104 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1106 foreach $file ( sort @files )
1108 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1112 $wiki = Maasha::Gwiki::gwiki_read( $file );
1114 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1115 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1117 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1118 $synopsis =~ s/!(\w)/$1/g;
1120 printf( "%-30s%s\n", $program, $synopsis );
1128 sub script_list_genomes
1130 # Martin A. Hansen, January 2008.
1132 # Prints the synopsis from the usage for each of the biopieces.
1134 my ( $in, # handle to in stream
1135 $out, # handle to out stream
1136 $options, # options hash
1141 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1143 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1145 foreach $genome ( @genomes )
1147 next if $genome =~ /\.$/;
1149 @formats = Maasha::Common::ls_dirs( $genome );
1151 foreach $format ( @formats )
1153 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1155 $hash{ $1 }{ $2 } = 1;
1164 map { push @row, $_ } sort keys %found;
1166 print join( "\t", @row ), "\n";
1168 foreach $genome ( sort keys %hash )
1172 foreach $format ( sort keys %found )
1174 if ( exists $hash{ $genome }{ $format } ) {
1181 print join( "\t", @row ), "\n";
1186 sub script_read_fasta
1188 # Martin A. Hansen, August 2007.
1190 # Read sequences from FASTA file.
1192 my ( $in, # handle to in stream
1193 $out, # handle to out stream
1194 $options, # options hash
1199 my ( $record, $file, $data_in, $entry, $num );
1201 while ( $record = get_record( $in ) ) {
1202 put_record( $record, $out );
1207 foreach $file ( @{ $options->{ "files" } } )
1209 $data_in = Maasha::Common::read_open( $file );
1211 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1213 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1216 SEQ_NAME => $entry->[ SEQ_NAME ],
1217 SEQ => $entry->[ SEQ ],
1218 SEQ_LEN => length $entry->[ SEQ ],
1221 put_record( $record, $out );
1224 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1234 close $data_in if $data_in;
1240 # Martin A. Hansen, August 2007.
1242 # Read table or table columns from stream or file.
1244 my ( $in, # handle to in stream
1245 $out, # handle to out stream
1246 $options, # options hash
1251 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1253 $options->{ 'delimit' } ||= '\s+';
1255 while ( $record = get_record( $in ) ) {
1256 put_record( $record, $out );
1259 $skip = $options->{ 'skip' } ||= 0;
1262 foreach $file ( @{ $options->{ "files" } } )
1264 $data_in = Maasha::Common::read_open( $file );
1266 while ( $line = <$data_in> )
1274 next if $line =~ /^#|^$/;
1281 @fields = split /$options->{'delimit'}/, $line;
1283 if ( $options->{ "cols" } ) {
1284 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1289 for ( $i = 0; $i < @fields2; $i++ )
1291 if ( $options->{ "keys" }->[ $i ] ) {
1292 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1294 $record->{ "V" . $i } = $fields2[ $i ];
1298 put_record( $record, $out );
1300 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1310 close $data_in if $data_in;
1316 # Martin A. Hansen, August 2007.
1318 # Read psl table from stream or file.
1320 my ( $in, # handle to in stream
1321 $out, # handle to out stream
1322 $options, # options hash
1327 my ( $record, @files, $file, $entries, $entry, $num );
1329 while ( $record = get_record( $in ) ) {
1330 put_record( $record, $out );
1335 foreach $file ( @{ $options->{ "files" } } )
1337 $entries = Maasha::UCSC::psl_get_entries( $file );
1339 foreach $entry ( @{ $entries } )
1341 put_record( $entry, $out );
1343 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1355 # Martin A. Hansen, August 2007.
1357 # Read bed table from stream or file.
1359 my ( $in, # handle to in stream
1360 $out, # handle to out stream
1361 $options, # options hash
1366 my ( $file, $record, $entry, $data_in, $num );
1368 while ( $record = get_record( $in ) ) {
1369 put_record( $record, $out );
1374 foreach $file ( @{ $options->{ "files" } } )
1376 $data_in = Maasha::Common::read_open( $file );
1378 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1380 put_record( $entry, $out );
1382 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1392 close $data_in if $data_in;
1396 sub script_read_fixedstep
1398 # Martin A. Hansen, Juli 2008.
1400 # Read fixedStep wiggle format from stream or file.
1402 my ( $in, # handle to in stream
1403 $out, # handle to out stream
1404 $options, # options hash
1409 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1411 while ( $record = get_record( $in ) ) {
1412 put_record( $record, $out );
1417 foreach $file ( @{ $options->{ "files" } } )
1419 $data_in = Maasha::Common::read_open( $file );
1421 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1423 $head = shift @{ $entry };
1425 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1427 $record->{ "CHR" } = $1;
1428 $record->{ "CHR_BEG" } = $2;
1429 $record->{ "STEP" } = $3;
1430 $record->{ "VALS" } = join ",", @{ $entry };
1433 put_record( $record, $out );
1435 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1445 close $data_in if $data_in;
1449 sub script_read_blast_tab
1451 # Martin A. Hansen, September 2007.
1453 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1455 my ( $in, # handle to in stream
1456 $out, # handle to out stream
1457 $options, # options hash
1462 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1464 while ( $record = get_record( $in ) ) {
1465 put_record( $record, $out );
1470 foreach $file ( @{ $options->{ "files" } } )
1472 $data_in = Maasha::Common::read_open( $file );
1474 while ( $line = <$data_in> )
1478 next if $line =~ /^#/;
1480 @fields = split /\t/, $line;
1482 $record->{ "REC_TYPE" } = "BLAST";
1483 $record->{ "Q_ID" } = $fields[ 0 ];
1484 $record->{ "S_ID" } = $fields[ 1 ];
1485 $record->{ "IDENT" } = $fields[ 2 ];
1486 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1487 $record->{ "MISMATCHES" } = $fields[ 4 ];
1488 $record->{ "GAPS" } = $fields[ 5 ];
1489 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1490 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1491 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1492 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1493 $record->{ "E_VAL" } = $fields[ 10 ];
1494 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1496 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1498 $record->{ "STRAND" } = '-';
1500 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1504 $record->{ "STRAND" } = '+';
1507 put_record( $record, $out );
1509 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1519 close $data_in if $data_in;
1523 sub script_read_embl
1525 # Martin A. Hansen, August 2007.
1529 my ( $in, # handle to in stream
1530 $out, # handle to out stream
1531 $options, # options hash
1536 my ( %options2, $file, $data_in, $num, $entry, $record );
1538 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1539 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1540 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1542 while ( $record = get_record( $in ) ) {
1543 put_record( $record, $out );
1548 foreach $file ( @{ $options->{ "files" } } )
1550 $data_in = Maasha::Common::read_open( $file );
1552 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1554 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1556 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1558 $record_copy = dclone $record;
1560 delete $record_copy->{ "FT" };
1562 put_record( $record_copy, $out );
1564 delete $record_copy->{ "SEQ" };
1566 foreach $feat ( keys %{ $record->{ "FT" } } )
1568 $record_copy->{ "FEAT_TYPE" } = $feat;
1570 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1572 foreach $qual ( keys %{ $feat2 } )
1574 $qual_val = join "; ", @{ $feat2->{ $qual } };
1579 $record_copy->{ $qual } = $qual_val;
1582 put_record( $record_copy, $out );
1586 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1596 close $data_in if $data_in;
1600 sub script_read_stockholm
1602 # Martin A. Hansen, August 2007.
1604 # Read Stockholm format.
1606 my ( $in, # handle to in stream
1607 $out, # handle to out stream
1608 $options, # options hash
1613 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1615 while ( $record = get_record( $in ) ) {
1616 put_record( $record, $out );
1621 foreach $file ( @{ $options->{ "files" } } )
1623 $data_in = Maasha::Common::read_open( $file );
1625 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1627 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1631 foreach $key ( keys %{ $record->{ "GF" } } ) {
1632 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1635 $record_anno->{ "ALIGN" } = $num;
1637 put_record( $record_anno, $out );
1639 foreach $seq ( @{ $record->{ "ALIGN" } } )
1641 undef $record_align;
1644 SEQ_NAME => $seq->[ 0 ],
1648 put_record( $record_align, $out );
1651 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1661 close $data_in if $data_in;
1665 sub script_read_phastcons
1667 # Martin A. Hansen, December 2007.
1669 # Read PhastCons format.
1671 my ( $in, # handle to in stream
1672 $out, # handle to out stream
1673 $options, # options hash
1678 my ( $data_in, $file, $num, $entry, @records, $record );
1680 $options->{ "min" } ||= 10;
1681 $options->{ "dist" } ||= 25;
1682 $options->{ "threshold" } ||= 0.8;
1683 $options->{ "gap" } ||= 5;
1685 while ( $record = get_record( $in ) ) {
1686 put_record( $record, $out );
1691 foreach $file ( @{ $options->{ "files" } } )
1693 $data_in = Maasha::Common::read_open( $file );
1695 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1697 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1699 foreach $record ( @records )
1701 $record->{ "REC_TYPE" } = "BED";
1702 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1704 put_record( $record, $out );
1706 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1717 close $data_in if $data_in;
1721 sub script_read_soft
1723 # Martin A. Hansen, December 2007.
1726 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1728 my ( $in, # handle to in stream
1729 $out, # handle to out stream
1730 $options, # options hash
1735 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1737 while ( $record = get_record( $in ) ) {
1738 put_record( $record, $out );
1743 foreach $file ( @{ $options->{ "files" } } )
1745 $soft_index = Maasha::NCBI::soft_index_file( $file );
1747 $fh = Maasha::Common::read_open( $file );
1749 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1751 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1753 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1755 $old_end = $platforms[ -1 ]->[ 2 ];
1757 foreach $sample ( @samples )
1759 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1761 foreach $record ( @{ $records } )
1763 put_record( $record, $out );
1765 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1770 $old_end = $sample->[ 2 ];
1778 close $data_in if $data_in;
1785 # Martin A. Hansen, February 2008.
1788 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1790 my ( $in, # handle to in stream
1791 $out, # handle to out stream
1792 $options, # options hash
1797 my ( $data_in, $file, $fh, $num, $record, $entry );
1799 while ( $record = get_record( $in ) ) {
1800 put_record( $record, $out );
1805 foreach $file ( @{ $options->{ "files" } } )
1807 $fh = Maasha::Common::read_open( $file );
1809 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1811 put_record( $entry, $out );
1813 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1823 close $data_in if $data_in;
1827 sub script_read_2bit
1829 # Martin A. Hansen, March 2008.
1831 # Read sequences from 2bit file.
1833 my ( $in, # handle to in stream
1834 $out, # handle to out stream
1835 $options, # options hash
1840 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1842 $mask = 1 if not $options->{ "no_mask" };
1844 while ( $record = get_record( $in ) ) {
1845 put_record( $record, $out );
1850 foreach $file ( @{ $options->{ "files" } } )
1852 $data_in = Maasha::Common::read_open( $file );
1854 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1856 foreach $line ( @{ $toc } )
1858 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1859 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1860 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1862 put_record( $record, $out );
1864 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1874 close $data_in if $data_in;
1878 sub script_read_solexa
1880 # Martin A. Hansen, March 2008.
1882 # Read Solexa sequence reads from file.
1884 my ( $in, # handle to in stream
1885 $out, # handle to out stream
1886 $options, # options hash
1891 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1893 $options->{ "quality" } ||= 20;
1895 while ( $record = get_record( $in ) ) {
1896 put_record( $record, $out );
1901 foreach $file ( @{ $options->{ "files" } } )
1903 $data_in = Maasha::Common::read_open( $file );
1905 while ( $entry = Maasha::Solexa::solexa_get_entry( $data_in ) )
1907 @seqs = split //, $entry->[ SEQ ];
1908 @scores = split /:/, $entry->[ SCORE ];
1910 for ( $i = 0; $i < @scores; $i++ ) {
1911 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1914 $record->{ "SEQ_NAME" } = $entry->[ SEQ_NAME ];
1915 $record->{ "SEQ" } = join "", @seqs;
1916 $record->{ "SEQ_LEN" } = scalar @seqs;
1917 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1919 put_record( $record, $out );
1921 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1931 close $data_in if $data_in;
1935 sub script_read_solid
1937 # Martin A. Hansen, April 2008.
1939 # Read Solid sequence from file.
1941 my ( $in, # handle to in stream
1942 $out, # handle to out stream
1943 $options, # options hash
1948 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1950 $options->{ "quality" } ||= 15;
1952 while ( $record = get_record( $in ) ) {
1953 put_record( $record, $out );
1958 foreach $file ( @{ $options->{ "files" } } )
1960 $data_in = Maasha::Common::read_open( $file );
1962 while ( $line = <$data_in> )
1966 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1968 @scores = split /,/, $seq_qual;
1969 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1971 for ( $i = 0; $i < @seqs; $i++ ) {
1972 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1976 SEQ_NAME => $seq_name,
1978 SEQ_QUAL => $seq_qual,
1979 SEQ_LEN => length $seq_cs,
1980 SEQ => join( "", @seqs ),
1981 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1984 put_record( $record, $out );
1986 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1996 close $data_in if $data_in;
2000 sub script_read_mysql
2002 # Martin A. Hansen, May 2008.
2004 # Read a MySQL query into stream.
2006 my ( $in, # handle to in stream
2007 $out, # handle to out stream
2008 $options, # options hash
2013 my ( $record, $dbh, $results );
2015 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2016 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2018 while ( $record = get_record( $in ) ) {
2019 put_record( $record, $out );
2022 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2024 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2026 Maasha::SQL::disconnect( $dbh );
2028 map { put_record( $_ ) } @{ $results };
2032 sub script_format_genome
2034 # Martin A. Hansen, Juli 2008.
2036 # Format a genome to speficed formats.
2038 my ( $in, # handle to in stream
2039 $out, # handle to out stream
2040 $options, # options hash
2045 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2047 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2048 $genome = $options->{ 'genome' };
2050 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2051 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2052 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2054 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2056 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2058 $fasta_dir = "$dir/genomes/$genome/fasta";
2062 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2064 $fasta_dir = "$dir/genomes/$genome/fasta";
2066 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2069 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2071 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2073 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2075 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2078 while ( $record = get_record( $in ) )
2080 if ( $fh_out and $entry = record2fasta( $record ) )
2082 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2084 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2086 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2088 $vals = $record->{ 'VALS' };
2092 print $fh_out "$vals\n";
2095 put_record( $record, $out ) if not $options->{ "no_stream" };
2098 foreach $format ( @{ $options->{ 'formats' } } )
2100 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2101 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2102 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2103 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2104 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2107 close $fh_out if $fh_out;
2111 sub script_length_seq
2113 # Martin A. Hansen, August 2007.
2115 # Determine the length of sequences in stream.
2117 my ( $in, # handle to in stream
2118 $out, # handle to out stream
2119 $options, # options hash
2124 my ( $record, $total );
2126 while ( $record = get_record( $in ) )
2128 if ( $record->{ "SEQ" } )
2130 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2131 $total += $record->{ "SEQ_LEN" };
2134 put_record( $record, $out ) if not $options->{ "no_stream" };
2137 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2141 sub script_uppercase_seq
2143 # Martin A. Hansen, August 2007.
2145 # Uppercases sequences in stream.
2147 my ( $in, # handle to in stream
2148 $out, # handle to out stream
2155 while ( $record = get_record( $in ) )
2157 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2159 put_record( $record, $out );
2164 sub script_shuffle_seq
2166 # Martin A. Hansen, December 2007.
2168 # Shuffle sequences in stream.
2170 my ( $in, # handle to in stream
2171 $out, # handle to out stream
2178 while ( $record = get_record( $in ) )
2180 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2182 put_record( $record, $out );
2187 sub script_analyze_seq
2189 # Martin A. Hansen, August 2007.
2191 # Analyze sequence composition of sequences in stream.
2193 my ( $in, # handle to in stream
2194 $out, # handle to out stream
2199 my ( $record, $analysis );
2201 while ( $record = get_record( $in ) )
2203 if ( $record->{ "SEQ" } )
2205 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2207 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2210 put_record( $record, $out );
2215 sub script_analyze_tags
2217 # Martin A. Hansen, August 2008.
2219 # Analyze sequence tags in stream.
2221 my ( $in, # handle to in stream
2222 $out, # handle to out stream
2227 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2229 while ( $record = get_record( $in ) )
2231 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2233 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2237 $len_hash{ length( $record->{ "SEQ" } ) }++;
2238 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2241 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2243 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2247 $len_hash{ $record->{ "BED_LEN" } }++;
2248 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2253 foreach $key ( sort { $a <=> $b } keys %len_hash )
2255 $tag_record->{ "TAG_LEN" } = $key;
2256 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2257 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2259 put_record( $tag_record, $out );
2264 sub script_complexity_seq
2266 # Martin A. Hansen, May 2008.
2268 # Generates an index calculated as the most common di-residue over
2269 # the sequence length for all sequences in stream.
2271 my ( $in, # handle to in stream
2272 $out, # handle to out stream
2277 my ( $record, $index );
2279 while ( $record = get_record( $in ) )
2281 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2283 put_record( $record, $out );
2288 sub script_oligo_freq
2290 # Martin A. Hansen, August 2007.
2292 # Determine the length of sequences in stream.
2294 my ( $in, # handle to in stream
2295 $out, # handle to out stream
2296 $options, # options hash
2301 my ( $record, %oligos, @freq_table );
2303 $options->{ "word_size" } ||= 7;
2305 while ( $record = get_record( $in ) )
2307 if ( $record->{ "SEQ" } )
2309 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2311 if ( not $options->{ "all" } )
2313 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2315 map { put_record( $_, $out ) } @freq_table;
2321 put_record( $record, $out );
2324 if ( $options->{ "all" } )
2326 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2328 map { put_record( $_, $out ) } @freq_table;
2333 sub script_create_weight_matrix
2335 # Martin A. Hansen, August 2007.
2337 # Creates a weight matrix from an alignmnet.
2339 my ( $in, # handle to in stream
2340 $out, # handle to out stream
2341 $options, # options hash
2346 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2350 while ( $record = get_record( $in ) )
2352 if ( $record->{ "SEQ" } )
2354 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2356 $res = substr $record->{ "SEQ" }, $i, 1;
2358 $freq_hash{ $i }{ $res }++;
2359 $res_hash{ $res } = 1;
2366 put_record( $record, $out );
2370 foreach $res ( sort keys %res_hash )
2374 $record->{ "V0" } = $res;
2376 for ( $i = 0; $i < keys %freq_hash; $i++ )
2378 $freq = $freq_hash{ $i }{ $res } || 0;
2380 if ( $options->{ "percent" } ) {
2381 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2384 $record->{ "V" . ( $i + 1 ) } = $freq;
2387 put_record( $record, $out );
2392 sub script_calc_bit_scores
2394 # Martin A. Hansen, March 2007.
2396 # Calculates the bit scores for each position from an alignmnet in the stream.
2398 my ( $in, # handle to in stream
2399 $out, # handle to out stream
2404 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2408 while ( $record = get_record( $in ) )
2410 if ( $record->{ "SEQ" } )
2412 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2414 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2416 $res = substr $record->{ "SEQ" }, $i, 1;
2418 next if $res =~ /-|_|~|\./;
2420 $freq_hash{ $i }{ $res }++;
2427 put_record( $record, $out );
2433 if ( $type eq "protein" ) {
2439 for ( $i = 0; $i < keys %freq_hash; $i++ )
2441 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2443 $bit_diff = $bit_max - $bit_height;
2445 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2448 put_record( $record, $out );
2452 sub script_reverse_seq
2454 # Martin A. Hansen, August 2007.
2456 # Reverse sequence in record.
2458 my ( $in, # handle to in stream
2459 $out, # handle to out stream
2466 while ( $record = get_record( $in ) )
2468 if ( $record->{ "SEQ" } ) {
2469 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2472 put_record( $record, $out );
2477 sub script_complement_seq
2479 # Martin A. Hansen, August 2007.
2481 # Complement sequence in record.
2483 my ( $in, # handle to in stream
2484 $out, # handle to out stream
2489 my ( $record, $type );
2491 while ( $record = get_record( $in ) )
2493 if ( $record->{ "SEQ" } )
2496 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2499 if ( $type eq "rna" ) {
2500 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2501 } elsif ( $type eq "dna" ) {
2502 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2506 put_record( $record, $out );
2511 sub script_remove_indels
2513 # Martin A. Hansen, August 2007.
2515 # Remove indels from sequences in stream.
2517 my ( $in, # handle to in stream
2518 $out, # handle to out stream
2525 while ( $record = get_record( $in ) )
2527 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2529 put_record( $record, $out );
2534 sub script_transliterate_seq
2536 # Martin A. Hansen, August 2007.
2538 # Transliterate chars from sequence in record.
2540 my ( $in, # handle to in stream
2541 $out, # handle to out stream
2542 $options, # options hash
2547 my ( $record, $search, $replace, $delete );
2549 $search = $options->{ "search" } || "";
2550 $replace = $options->{ "replace" } || "";
2551 $delete = $options->{ "delete" } || "";
2553 while ( $record = get_record( $in ) )
2555 if ( $record->{ "SEQ" } )
2557 if ( $search and $replace ) {
2558 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2559 } elsif ( $delete ) {
2560 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2564 put_record( $record, $out );
2569 sub script_transliterate_vals
2571 # Martin A. Hansen, April 2008.
2573 # Transliterate chars from values in record.
2575 my ( $in, # handle to in stream
2576 $out, # handle to out stream
2577 $options, # options hash
2582 my ( $record, $search, $replace, $delete, $key );
2584 $search = $options->{ "search" } || "";
2585 $replace = $options->{ "replace" } || "";
2586 $delete = $options->{ "delete" } || "";
2588 while ( $record = get_record( $in ) )
2590 foreach $key ( @{ $options->{ "keys" } } )
2592 if ( exists $record->{ $key } )
2594 if ( $search and $replace ) {
2595 eval "\$record->{ $key } =~ tr/$search/$replace/";
2596 } elsif ( $delete ) {
2597 eval "\$record->{ $key } =~ tr/$delete//d";
2602 put_record( $record, $out );
2607 sub script_translate_seq
2609 # Martin A. Hansen, February 2008.
2611 # Translate DNA sequence into protein sequence.
2613 my ( $in, # handle to in stream
2614 $out, # handle to out stream
2615 $options, # options hash
2620 my ( $record, $frame, %new_record );
2622 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2624 while ( $record = get_record( $in ) )
2626 if ( $record->{ "SEQ" } )
2628 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2630 foreach $frame ( @{ $options->{ "frames" } } )
2632 %new_record = %{ $record };
2634 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2635 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2636 $new_record{ "FRAME" } = $frame;
2638 put_record( \%new_record, $out );
2644 put_record( $record, $out );
2650 sub script_extract_seq
2652 # Martin A. Hansen, August 2007.
2654 # Extract subsequences from sequences in record.
2656 my ( $in, # handle to in stream
2657 $out, # handle to out stream
2658 $options, # options hash
2663 my ( $beg, $end, $len, $record );
2665 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2668 $beg = $options->{ "beg" } - 1; # correcting for start offset
2671 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2673 } elsif ( defined $options->{ "end" } ) {
2674 $end = $options->{ "end" } - 1; # correcting for start offset
2677 $len = $options->{ "len" };
2679 # print "beg->$beg, end->$end, len->$len\n";
2681 while ( $record = get_record( $in ) )
2683 if ( $record->{ "SEQ" } )
2685 if ( defined $beg and defined $end )
2687 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2688 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2690 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2693 elsif ( defined $beg and defined $len )
2695 if ( $len > length $record->{ "SEQ" } ) {
2696 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2698 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2701 elsif ( defined $beg )
2703 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2707 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2709 put_record( $record, $out );
2714 sub script_get_genome_seq
2716 # Martin A. Hansen, December 2007.
2718 # Gets a subsequence from a genome.
2720 my ( $in, # handle to in stream
2721 $out, # handle to out stream
2722 $options, # options hash
2727 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2729 $options->{ "flank" } ||= 0;
2731 if ( $options->{ "genome" } )
2733 $genome = $options->{ "genome" };
2735 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2736 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2738 $fh = Maasha::Common::read_open( $genome_file );
2739 $index = Maasha::Fasta::index_retrieve( $index_file );
2741 shift @{ $index }; # Get rid of the file size info
2743 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2745 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2747 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2749 $beg = $index_beg + $options->{ "beg" } - 1;
2751 if ( $options->{ "len" } ) {
2752 $len = $options->{ "len" };
2753 } elsif ( $options->{ "end" } ) {
2754 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2757 $beg -= $options->{ "flank" };
2758 $len += 2 * $options->{ "flank" };
2760 if ( $beg <= $index_beg )
2762 $len -= $index_beg - $beg;
2766 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2768 next if $beg > $index_beg + $index_len;
2770 $record->{ "CHR" } = $options->{ "chr" };
2771 $record->{ "CHR_BEG" } = $beg - $index_beg;
2772 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2774 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2775 $record->{ "SEQ_LEN" } = $len;
2777 put_record( $record, $out );
2781 while ( $record = get_record( $in ) )
2783 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2785 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2787 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2789 $beg = $record->{ "CHR_BEG" } + $index_beg;
2790 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2792 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2794 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2796 $beg = $record->{ "S_BEG" } + $index_beg;
2797 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2799 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2801 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2803 $beg = $record->{ "S_BEG" } + $index_beg;
2804 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2807 $beg -= $options->{ "flank" };
2808 $len += 2 * $options->{ "flank" };
2810 if ( $beg <= $index_beg )
2812 $len -= $index_beg - $beg;
2816 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2818 next if $beg > $index_beg + $index_len;
2820 $record->{ "CHR_BEG" } = $beg - $index_beg;
2821 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2823 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2825 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2827 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2828 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2831 if ( $options->{ "mask" } )
2833 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2835 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2837 @begs = split ",", $record->{ "Q_BEGS" };
2838 @lens = split ",", $record->{ "BLOCKSIZES" };
2840 for ( $i = 0; $i < @begs; $i++ ) {
2841 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2847 put_record( $record, $out );
2854 sub script_get_genome_align
2856 # Martin A. Hansen, April 2008.
2858 # Gets a subalignment from a multiple genome alignment.
2860 my ( $in, # handle to in stream
2861 $out, # handle to out stream
2862 $options, # options hash
2867 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2869 $options->{ "strand" } ||= "+";
2873 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2875 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2877 $beg = $options->{ "beg" } - 1;
2879 if ( $options->{ "end" } ) {
2880 $end = $options->{ "end" };
2881 } elsif ( $options->{ "len" } ) {
2882 $end = $beg + $options->{ "len" };
2885 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2887 foreach $entry ( @{ $align } )
2889 $entry->{ "CHR" } = $record->{ "CHR" };
2890 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2891 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2892 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2893 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2894 $entry->{ "SCORE" } = $record->{ "SCORE" };
2896 put_record( $entry, $out );
2900 while ( $record = get_record( $in ) )
2902 if ( $record->{ "REC_TYPE" } eq "BED" )
2904 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2906 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2908 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2910 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2912 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2915 foreach $entry ( @{ $align } )
2917 $entry->{ "CHR" } = $record->{ "CHR" };
2918 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2919 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2920 $entry->{ "STRAND" } = $record->{ "STRAND" };
2921 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2922 $entry->{ "SCORE" } = $record->{ "SCORE" };
2924 put_record( $entry, $out );
2932 sub script_get_genome_phastcons
2934 # Martin A. Hansen, February 2008.
2936 # Get phastcons scores from genome intervals.
2938 my ( $in, # handle to in stream
2939 $out, # handle to out stream
2940 $options, # options hash
2945 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2947 $options->{ "flank" } ||= 0;
2949 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2950 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2952 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2953 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2955 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2957 $options->{ "beg" } -= 1; # request is 1-based
2958 $options->{ "end" } -= 1; # request is 1-based
2960 if ( $options->{ "len" } ) {
2961 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2964 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2966 $record->{ "CHR" } = $options->{ "chr" };
2967 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2968 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2970 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2971 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2973 put_record( $record, $out );
2976 while ( $record = get_record( $in ) )
2978 if ( $record->{ "REC_TYPE" } eq "BED" )
2980 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2982 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2984 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2986 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2988 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2991 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2992 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2994 put_record( $record, $out );
2997 close $fh_phastcons if $fh_phastcons;
3003 # Martin A. Hansen, December 2007.
3005 # Folds sequences in stream into secondary structures.
3007 my ( $in, # handle to in stream
3008 $out, # handle to out stream
3013 my ( $record, $type, $struct, $index );
3015 while ( $record = get_record( $in ) )
3017 if ( $record->{ "SEQ" } )
3020 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3023 if ( $type ne "protein" )
3025 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3026 $record->{ "SEC_STRUCT" } = $struct;
3027 $record->{ "FREE_ENERGY" } = $index;
3028 $record->{ "SCORE" } = abs int $index;
3029 $record->{ "SIZE" } = length $struct;
3030 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3034 put_record( $record, $out );
3039 sub script_split_seq
3041 # Martin A. Hansen, August 2007.
3043 # Split a sequence in stream into words.
3045 my ( $in, # handle to in stream
3046 $out, # handle to out stream
3047 $options, # options hash
3052 my ( $record, $new_record, $i, $subseq, %lookup );
3054 $options->{ "word_size" } ||= 7;
3056 while ( $record = get_record( $in ) )
3058 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3060 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3062 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3064 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3066 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3067 $new_record->{ "SEQ" } = $subseq;
3069 put_record( $new_record, $out );
3071 $lookup{ $subseq } = 1;
3075 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3076 $new_record->{ "SEQ" } = $subseq;
3078 put_record( $new_record, $out );
3084 put_record( $record, $out );
3090 sub script_split_bed
3092 # Martin A. Hansen, June 2008.
3094 # Split a BED record into overlapping windows.
3096 my ( $in, # handle to in stream
3097 $out, # handle to out stream
3098 $options, # options hash
3103 my ( $record, $new_record, $i );
3105 $options->{ "window_size" } ||= 20;
3106 $options->{ "step_size" } ||= 1;
3108 while ( $record = get_record( $in ) )
3110 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3112 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3114 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3116 $new_record->{ "REC_TYPE" } = "BED";
3117 $new_record->{ "CHR" } = $record->{ "CHR" };
3118 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3119 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3120 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3121 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3122 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3123 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3125 put_record( $new_record, $out );
3130 put_record( $record, $out );
3136 sub script_align_seq
3138 # Martin A. Hansen, August 2007.
3140 # Align sequences in stream.
3142 my ( $in, # handle to in stream
3143 $out, # handle to out stream
3148 my ( $record, @entries, $entry );
3150 while ( $record = get_record( $in ) )
3152 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3153 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3154 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3155 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3157 put_record( $record, $out );
3161 @entries = Maasha::Align::align( \@entries );
3163 foreach $entry ( @entries )
3165 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3168 SEQ_NAME => $entry->[ SEQ_NAME ],
3169 SEQ => $entry->[ SEQ ],
3172 put_record( $record, $out );
3180 # Martin A. Hansen, February 2008.
3182 # Using the first sequence in stream as reference, tile
3183 # all subsequent sequences based on pairwise alignments.
3185 my ( $in, # handle to in stream
3186 $out, # handle to out stream
3187 $options, # options hash
3192 my ( $record, $first, $ref_entry, @entries );
3196 while ( $record = get_record( $in ) )
3198 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3202 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3208 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3213 put_record( $record, $out );
3217 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3219 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3223 sub script_invert_align
3225 # Martin A. Hansen, February 2008.
3227 # Inverts an alignment showing only non-mathing residues
3228 # using the first sequence as reference.
3230 my ( $in, # handle to in stream
3231 $out, # handle to out stream
3232 $options, # options hash
3237 my ( $record, @entries );
3239 while ( $record = get_record( $in ) )
3241 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3243 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3247 put_record( $record, $out );
3251 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3253 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3257 sub script_patscan_seq
3259 # Martin A. Hansen, August 2007.
3261 # Locates patterns in sequences using scan_for_matches.
3263 my ( $in, # handle to in stream
3264 $out, # handle to out stream
3265 $options, # options hash
3270 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3272 if ( $options->{ "patterns" } ) {
3273 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3274 } elsif ( -f $options->{ "patterns_in" } ) {
3275 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3278 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3280 push @args, "-c" if $options->{ "comp" };
3281 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3282 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3284 $seq_file = "$BP_TMP/patscan.seq";
3285 $pat_file = "$BP_TMP/patscan.pat";
3286 $out_file = "$BP_TMP/patscan.out";
3288 $fh_out = Maasha::Common::write_open( $seq_file );
3292 while ( $record = get_record( $in ) )
3294 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3296 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3298 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3300 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3308 $arg = join " ", @args;
3309 $arg .= " -p" if $type eq "protein";
3311 foreach $pattern ( @{ $patterns } )
3313 $fh_out = Maasha::Common::write_open( $pat_file );
3315 print $fh_out "$pattern\n";
3319 if ( $options->{ 'genome' } ) {
3320 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3321 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3323 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3324 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3327 $fh_in = Maasha::Common::read_open( $out_file );
3329 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3331 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3333 if ( $options->{ 'genome' } )
3335 $result->{ "CHR" } = $result->{ "S_ID" };
3336 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3337 $result->{ "CHR_END" } = $result->{ "S_END" };
3339 delete $result->{ "S_ID" };
3340 delete $result->{ "S_BEG" };
3341 delete $result->{ "S_END" };
3345 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3348 put_record( $result, $out );
3360 sub script_create_blast_db
3362 # Martin A. Hansen, September 2007.
3364 # Creates a NCBI BLAST database with formatdb
3366 my ( $in, # handle to in stream
3367 $out, # handle to out stream
3368 $options, # options hash
3373 my ( $fh, $seq_type, $path, $record, $entry );
3375 $path = $options->{ "database" };
3377 $fh = Maasha::Common::write_open( $path );
3379 while ( $record = get_record( $in ) )
3381 put_record( $record, $out ) if not $options->{ "no_stream" };
3383 if ( $entry = record2fasta( $record ) )
3385 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3387 Maasha::Fasta::put_entry( $entry, $fh );
3393 if ( $seq_type eq "protein" ) {
3394 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3396 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3403 sub script_blast_seq
3405 # Martin A. Hansen, September 2007.
3407 # BLASTs sequences in stream against a given database.
3409 my ( $in, # handle to in stream
3410 $out, # handle to out stream
3411 $options, # options hash
3416 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3418 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3419 $options->{ "filter" } = "F";
3420 $options->{ "filter" } = "T" if $options->{ "filter" };
3421 $options->{ "cpus" } ||= 1;
3423 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3425 $tmp_in = "$BP_TMP/blast_query.seq";
3426 $tmp_out = "$BP_TMP/blast.result";
3428 $fh_out = Maasha::Common::write_open( $tmp_in );
3430 while ( $record = get_record( $in ) )
3432 if ( $entry = record2fasta( $record ) )
3434 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3436 Maasha::Fasta::put_entry( $entry, $fh_out );
3439 put_record( $record, $out );
3444 if ( -f $options->{ 'database' } . ".phr" ) {
3445 $s_type = "protein";
3447 $s_type = "nucleotide";
3450 if ( not $options->{ 'program' } )
3452 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3453 $options->{ 'program' } = "blastn";
3454 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3455 $options->{ 'program' } = "blastp";
3456 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3457 $options->{ 'program' } = "blastx";
3458 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3459 $options->{ 'program' } = "tblastn";
3463 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 );
3467 $fh_out = Maasha::Common::read_open( $tmp_out );
3471 while ( $line = <$fh_out> )
3475 next if $line =~ /^#/;
3477 @fields = split /\s+/, $line;
3479 $record->{ "REC_TYPE" } = "BLAST";
3480 $record->{ "Q_ID" } = $fields[ 0 ];
3481 $record->{ "S_ID" } = $fields[ 1 ];
3482 $record->{ "IDENT" } = $fields[ 2 ];
3483 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3484 $record->{ "MISMATCHES" } = $fields[ 4 ];
3485 $record->{ "GAPS" } = $fields[ 5 ];
3486 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3487 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3488 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3489 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3490 $record->{ "E_VAL" } = $fields[ 10 ];
3491 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3493 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3495 $record->{ "STRAND" } = '-';
3497 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3501 $record->{ "STRAND" } = '+';
3504 put_record( $record, $out );
3515 # Martin A. Hansen, August 2007.
3517 # BLATs sequences in stream against a given genome.
3519 my ( $in, # handle to in stream
3520 $out, # handle to out stream
3521 $options, # options hash
3526 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3528 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3530 $options->{ 'tile_size' } ||= 11;
3531 $options->{ 'one_off' } ||= 0;
3532 $options->{ 'min_identity' } ||= 90;
3533 $options->{ 'min_score' } ||= 0;
3534 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3536 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3537 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3538 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3539 $blat_args .= " -minScore=$options->{ 'min_score' }";
3540 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3541 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3543 $query_file = "$BP_TMP/blat.seq";
3545 $fh_out = Maasha::Common::write_open( $query_file );
3547 while ( $record = get_record( $in ) )
3549 if ( $entry = record2fasta( $record ) )
3551 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3552 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3555 put_record( $record, $out );
3560 $blat_args .= " -t=dnax" if $type eq "protein";
3561 $blat_args .= " -q=$type";
3563 $result_file = "$BP_TMP/blat.psl";
3565 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3569 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3571 map { put_record( $_, $out ) } @{ $entries };
3573 unlink $result_file;
3579 # Martin A. Hansen, July 2008.
3581 # soap sequences in stream against a given file or genome.
3583 my ( $in, # handle to in stream
3584 $out, # handle to out stream
3585 $options, # options hash
3590 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3592 $options->{ "seed_size" } ||= 10;
3593 $options->{ "mismatches" } ||= 2;
3594 $options->{ "gap_size" } ||= 0;
3595 $options->{ "cpus" } ||= 1;
3597 if ( $options->{ "genome" } ) {
3598 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3601 $tmp_in = "$BP_TMP/soap_query.seq";
3602 $tmp_out = "$BP_TMP/soap.result";
3604 $fh_out = Maasha::Common::write_open( $tmp_in );
3608 while ( $record = get_record( $in ) )
3610 if ( $entry = record2fasta( $record ) )
3612 Maasha::Fasta::put_entry( $entry, $fh_out );
3617 put_record( $record, $out );
3625 "-s $options->{ 'seed_size' }",
3628 "-v $options->{ 'mismatches' }",
3629 "-g $options->{ 'gap_size' }",
3630 "-p $options->{ 'cpus' }",
3631 "-d $options->{ 'in_file' }",
3635 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3637 Maasha::Common::run( "soap", $args, 1 );
3641 $fh_out = Maasha::Common::read_open( $tmp_out );
3645 while ( $line = <$fh_out> )
3649 @fields = split /\t/, $line;
3651 $record->{ "REC_TYPE" } = "SOAP";
3652 $record->{ "Q_ID" } = $fields[ 0 ];
3653 $record->{ "SCORE" } = $fields[ 3 ];
3654 $record->{ "STRAND" } = $fields[ 6 ];
3655 $record->{ "S_ID" } = $fields[ 7 ];
3656 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3657 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3659 put_record( $record, $out );
3669 sub script_match_seq
3671 # Martin A. Hansen, August 2007.
3673 # BLATs sequences in stream against a given genome.
3675 my ( $in, # handle to in stream
3676 $out, # handle to out stream
3677 $options, # options hash
3682 my ( $record, @entries, $results );
3684 $options->{ "word_size" } ||= 20;
3685 $options->{ "direction" } ||= "both";
3687 while ( $record = get_record( $in ) )
3689 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3690 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3693 put_record( $record, $out );
3696 if ( @entries == 1 )
3698 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3700 map { put_record( $_, $out ) } @{ $results };
3702 elsif ( @entries == 2 )
3704 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3706 map { put_record( $_, $out ) } @{ $results };
3711 sub script_create_vmatch_index
3713 # Martin A. Hansen, January 2008.
3715 # Create a vmatch index from sequences in the stream.
3717 my ( $in, # handle to in stream
3718 $out, # handle to out stream
3719 $options, # options hash
3724 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3726 if ( $options->{ "index_name" } )
3728 $file_tmp = $options->{ 'index_name' };
3729 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3732 while ( $record = get_record( $in ) )
3734 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3736 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3738 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3741 put_record( $record, $out ) if not $options->{ "no_stream" };
3744 if ( $options->{ "index_name" } )
3748 if ( $type eq "protein" ) {
3749 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3751 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3759 sub script_vmatch_seq
3761 # Martin A. Hansen, August 2007.
3763 # Vmatches sequences in stream against a given genome.
3765 my ( $in, # handle to in stream
3766 $out, # handle to out stream
3767 $options, # options hash
3772 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3774 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3776 if ( $options->{ "index_name" } )
3778 @index_files = $options->{ "index_name" };
3782 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3784 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3786 @index_files = sort keys %hash;
3789 while ( $record = get_record( $in ) )
3791 push @records, $record;
3793 put_record( $record, $out );
3796 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3800 $fh_in = Maasha::Common::read_open( $result_file );
3802 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3803 put_record( $record, $out );
3808 unlink $result_file;
3812 sub script_write_fasta
3814 # Martin A. Hansen, August 2007.
3816 # Write FASTA entries from sequences in stream.
3818 my ( $in, # handle to in stream
3819 $out, # handle to out stream
3820 $options, # options hash
3825 my ( $record, $fh, $entry );
3827 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3829 while ( $record = get_record( $in ) )
3831 if ( $entry = record2fasta( $record ) ) {
3832 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3835 put_record( $record, $out ) if not $options->{ "no_stream" };
3842 sub script_write_align
3844 # Martin A. Hansen, August 2007.
3846 # Write pretty alignments aligned sequences in stream.
3848 my ( $in, # handle to in stream
3849 $out, # handle to out stream
3850 $options, # options hash
3855 my ( $fh, $record, @entries );
3857 $fh = write_stream( $options->{ "data_out" } ) ;
3859 while ( $record = get_record( $in ) )
3861 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3862 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3865 put_record( $record, $out ) if not $options->{ "no_stream" };
3868 if ( scalar( @entries ) == 2 ) {
3869 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3870 } elsif ( scalar ( @entries ) > 2 ) {
3871 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3878 sub script_write_blast
3880 # Martin A. Hansen, November 2007.
3882 # Write data in blast table format (-m8 and 9).
3884 my ( $in, # handle to in stream
3885 $out, # handle to out stream
3886 $options, # options hash
3891 my ( $fh, $record, $first );
3893 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3897 while ( $record = get_record( $in ) )
3899 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3901 if ( $options->{ "comment" } and $first )
3903 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";
3908 if ( $record->{ "STRAND" } eq "-" ) {
3909 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3912 print $fh join( "\t",
3913 $record->{ "Q_ID" },
3914 $record->{ "S_ID" },
3915 $record->{ "IDENT" },
3916 $record->{ "ALIGN_LEN" },
3917 $record->{ "MISMATCHES" },
3918 $record->{ "GAPS" },
3919 $record->{ "Q_BEG" } + 1,
3920 $record->{ "Q_END" } + 1,
3921 $record->{ "S_BEG" } + 1,
3922 $record->{ "S_END" } + 1,
3923 $record->{ "E_VAL" },
3924 $record->{ "BIT_SCORE" }
3928 put_record( $record, $out ) if not $options->{ "no_stream" };
3935 sub script_write_tab
3937 # Martin A. Hansen, August 2007.
3939 # Write data as table.
3941 my ( $in, # handle to in stream
3942 $out, # handle to out stream
3943 $options, # options hash
3948 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3950 $options->{ "delimit" } ||= "\t";
3952 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3954 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3956 while ( $record = get_record( $in ) )
3961 if ( $options->{ "keys" } )
3963 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3967 foreach $key ( @{ $options->{ "keys" } } )
3969 if ( exists $record->{ $key } )
3971 push @keys, $key if $options->{ "comment" };
3972 push @vals, $record->{ $key };
3979 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3981 next if exists $no_keys{ $key };
3983 push @keys, $key if $options->{ "comment" };
3984 push @vals, $record->{ $key };
3988 if ( @keys and $options->{ "comment" } )
3990 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3992 delete $options->{ "comment" };
3995 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3997 put_record( $record, $out ) if not $options->{ "no_stream" };
4004 sub script_write_bed
4006 # Martin A. Hansen, August 2007.
4008 # Write BED format for the UCSC genome browser using records in stream.
4010 my ( $in, # handle to in stream
4011 $out, # handle to out stream
4012 $options, # options hash
4017 my ( $fh, $record, $new_record );
4019 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4021 while ( $record = get_record( $in ) )
4023 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4025 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4027 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4029 $new_record->{ "CHR" } = $record->{ "S_ID" };
4030 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4031 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4032 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4033 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4034 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4036 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4038 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4040 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4042 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4044 $new_record->{ "CHR" } = $record->{ "S_ID" };
4045 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4046 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4047 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4048 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4049 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4051 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4053 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4055 $new_record->{ "CHR" } = $record->{ "S_ID" };
4056 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4057 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4058 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4059 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4060 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4062 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4064 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4066 $new_record->{ "CHR" } = $record->{ "S_ID" };
4067 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4068 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4069 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4070 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4071 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4073 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4075 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4077 Maasha::UCSC::bed_put_entry( $record, $fh );
4080 put_record( $record, $out ) if not $options->{ "no_stream" };
4087 sub script_write_psl
4089 # Martin A. Hansen, August 2007.
4091 # Write PSL output from stream.
4093 my ( $in, # handle to in stream
4094 $out, # handle to out stream
4095 $options, # options hash
4100 my ( $fh, $record, @output, $first );
4104 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4106 while ( $record = get_record( $in ) )
4108 put_record( $record, $out ) if not $options->{ "no_stream" };
4110 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4112 Maasha::UCSC::psl_put_header( $fh ) if $first;
4113 Maasha::UCSC::psl_put_entry( $record, $fh );
4122 sub script_write_fixedstep
4124 # Martin A. Hansen, Juli 2008.
4126 # Write fixedStep entries from recrods in the stream.
4128 my ( $in, # handle to in stream
4129 $out, # handle to out stream
4130 $options, # options hash
4135 my ( $fh, $record, $vals );
4137 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4139 while ( $record = get_record( $in ) )
4141 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4143 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4145 $vals = $record->{ 'VALS' };
4149 print $fh "$vals\n";
4152 put_record( $record, $out ) if not $options->{ "no_stream" };
4159 sub script_write_2bit
4161 # Martin A. Hansen, March 2008.
4163 # Write sequence entries from stream in 2bit format.
4165 my ( $in, # handle to in stream
4166 $out, # handle to out stream
4167 $options, # options hash
4172 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4174 $mask = 1 if not $options->{ "no_mask" };
4176 $tmp_file = "$BP_TMP/write_2bit.fna";
4177 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4179 $fh_out = write_stream( $options->{ "data_out" } );
4181 while ( $record = get_record( $in ) )
4183 if ( $entry = record2fasta( $record ) ) {
4184 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4187 put_record( $record, $out ) if not $options->{ "no_stream" };
4192 $fh_in = Maasha::Common::read_open( $tmp_file );
4194 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4203 sub script_write_solid
4205 # Martin A. Hansen, April 2008.
4207 # Write di-base encoded Solid sequence from entries in stream.
4209 my ( $in, # handle to in stream
4210 $out, # handle to out stream
4211 $options, # options hash
4216 my ( $record, $fh, $entry );
4218 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4220 while ( $record = get_record( $in ) )
4222 if ( $entry = record2fasta( $record ) )
4224 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4226 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4229 put_record( $record, $out ) if not $options->{ "no_stream" };
4236 sub script_plot_seqlogo
4238 # Martin A. Hansen, August 2007.
4240 # Calculates and writes a sequence logo for alignments.
4242 my ( $in, # handle to in stream
4243 $out, # handle to out stream
4244 $options, # options hash
4249 my ( $record, @entries, $logo, $fh );
4251 while ( $record = get_record( $in ) )
4253 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4254 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4257 put_record( $record, $out ) if not $options->{ "no_stream" };
4260 $logo = Maasha::Plot::seq_logo( \@entries );
4262 $fh = write_stream( $options->{ "data_out" } );
4270 sub script_plot_phastcons_profiles
4272 # Martin A. Hansen, January 2008.
4274 # Plots PhastCons profiles.
4276 my ( $in, # handle to in stream
4277 $out, # handle to out stream
4278 $options, # options hash
4283 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4285 $options->{ "title" } ||= "PhastCons Profiles";
4287 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4288 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4290 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4291 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4293 while ( $record = get_record( $in ) )
4295 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4297 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4299 push @{ $AoA }, [ @{ $scores } ];
4302 put_record( $record, $out ) if not $options->{ "no_stream" };
4305 Maasha::UCSC::phastcons_normalize( $AoA );
4307 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4308 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4310 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4312 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4314 $fh = write_stream( $options->{ "data_out" } );
4316 print $fh "$_\n" foreach @{ $plot };
4322 sub script_analyze_bed
4324 # Martin A. Hansen, March 2008.
4326 # Analyze BED entries in stream.
4328 my ( $in, # handle to in stream
4329 $out, # handle to out stream
4330 $options, # options hash
4337 while ( $record = get_record( $in ) )
4339 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4341 put_record( $record, $out );
4346 sub script_analyze_vals
4348 # Martin A. Hansen, August 2007.
4350 # Analyze values for given keys in stream.
4352 my ( $in, # handle to in stream
4353 $out, # handle to out stream
4354 $options, # options hash
4359 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4361 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4363 while ( $record = get_record( $in ) )
4365 foreach $key ( keys %{ $record } )
4367 next if $options->{ "keys" } and not exists $key_hash{ $key };
4369 $analysis->{ $key }->{ "COUNT" }++;
4371 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4373 $analysis->{ $key }->{ "TYPE" } = "num";
4374 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4375 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4376 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4380 $len = length $record->{ $key };
4382 $analysis->{ $key }->{ "TYPE" } = "alph";
4383 $analysis->{ $key }->{ "SUM" } += $len;
4384 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4385 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4389 put_record( $record, $out ) if not $options->{ "no_stream" };
4392 foreach $key ( keys %{ $analysis } )
4394 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4395 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4398 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4408 if ( $options->{ "keys" } ) {
4409 @keys = @{ $options->{ "keys" } };
4411 @keys = keys %{ $analysis };
4414 foreach $key ( @keys )
4416 $keys .= sprintf "% 15s", $key;
4417 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4418 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4419 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4420 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4421 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4422 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4425 print $out "$keys\n";
4426 print $out "$types\n";
4427 print $out "$counts\n";
4428 print $out "$mins\n";
4429 print $out "$maxs\n";
4430 print $out "$sums\n";
4431 print $out "$means\n";
4435 sub script_head_records
4437 # Martin A. Hansen, August 2007.
4439 # Display the first sequences in stream.
4441 my ( $in, # handle to in stream
4442 $out, # handle to out stream
4443 $options, # options hash
4448 my ( $record, $count );
4450 $options->{ "num" } ||= 10;
4454 while ( $record = get_record( $in ) )
4458 put_record( $record, $out );
4460 last if $count == $options->{ "num" };
4465 sub script_remove_keys
4467 # Martin A. Hansen, August 2007.
4469 # Remove keys from stream.
4471 my ( $in, # handle to in stream
4472 $out, # handle to out stream
4473 $options, # options hash
4478 my ( $record, $new_record );
4480 while ( $record = get_record( $in ) )
4482 if ( $options->{ "keys" } )
4484 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4486 elsif ( $options->{ "save_keys" } )
4488 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4490 $record = $new_record;
4493 put_record( $record, $out ) if keys %{ $record };
4498 sub script_rename_keys
4500 # Martin A. Hansen, August 2007.
4502 # Rename keys in stream.
4504 my ( $in, # handle to in stream
4505 $out, # handle to out stream
4506 $options, # options hash
4513 while ( $record = get_record( $in ) )
4515 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4517 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4519 delete $record->{ $options->{ "keys" }->[ 0 ] };
4522 put_record( $record, $out );
4527 sub script_uniq_vals
4529 # Martin A. Hansen, August 2007.
4531 # Find unique values in stream.
4533 my ( $in, # handle to in stream
4534 $out, # handle to out stream
4535 $options, # options hash
4540 my ( %hash, $record );
4542 while ( $record = get_record( $in ) )
4544 if ( $record->{ $options->{ "key" } } )
4546 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4548 put_record( $record, $out );
4550 $hash{ $record->{ $options->{ "key" } } } = 1;
4552 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4554 put_record( $record, $out );
4558 $hash{ $record->{ $options->{ "key" } } } = 1;
4563 put_record( $record, $out );
4569 sub script_merge_vals
4571 # Martin A. Hansen, August 2007.
4573 # Rename keys in stream.
4575 my ( $in, # handle to in stream
4576 $out, # handle to out stream
4577 $options, # options hash
4582 my ( $record, @join, $i );
4584 $options->{ "delimit" } ||= '_';
4586 while ( $record = get_record( $in ) )
4588 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4590 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4592 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4593 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4596 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4599 put_record( $record, $out );
4604 sub script_merge_records
4606 # Martin A. Hansen, July 2008.
4608 # Merges records in the stream based on identical values of two given keys.
4610 my ( $in, # handle to in stream
4611 $out, # handle to out stream
4612 $options, # options hash
4617 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4618 $num1, $num2, $num, $cmp, $i );
4620 $merge = $options->{ "merge" } || "AandB";
4622 $file1 = "$BP_TMP/merge_records1.tmp";
4623 $file2 = "$BP_TMP/merge_records2.tmp";
4625 $fh1 = Maasha::Common::write_open( $file1 );
4626 $fh2 = Maasha::Common::write_open( $file2 );
4628 $key1 = $options->{ "keys" }->[ 0 ];
4629 $key2 = $options->{ "keys" }->[ 1 ];
4631 $num = $key2 =~ s/n$//;
4635 while ( $record = get_record( $in ) )
4637 if ( exists $record->{ $key1 } )
4640 @vals1 = $record->{ $key1 };
4642 delete $record->{ $key1 };
4644 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4646 print $fh1 join( "\t", @vals1 ), "\n";
4650 elsif ( exists $record->{ $key2 } )
4653 @vals2 = $record->{ $key2 };
4655 delete $record->{ $key2 };
4657 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4659 print $fh2 join( "\t", @vals2 ), "\n";
4670 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4671 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4675 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4676 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4679 $fh1 = Maasha::Common::read_open( $file1 );
4680 $fh2 = Maasha::Common::read_open( $file2 );
4682 @vals1 = Maasha::Common::get_fields( $fh1 );
4683 @vals2 = Maasha::Common::get_fields( $fh2 );
4685 while ( $num1 > 0 and $num2 > 0 )
4690 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4692 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4697 if ( $merge =~ /^(AorB|AnotB)$/ )
4699 for ( $i = 0; $i < @keys1; $i++ ) {
4700 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4703 put_record( $record, $out );
4706 @vals1 = Maasha::Common::get_fields( $fh1 );
4711 if ( $merge =~ /^(BorA|BnotA)$/ )
4713 for ( $i = 0; $i < @keys2; $i++ ) {
4714 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4717 put_record( $record, $out );
4720 @vals2 = Maasha::Common::get_fields( $fh2 );
4725 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4727 for ( $i = 0; $i < @keys1; $i++ ) {
4728 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4731 for ( $i = 1; $i < @keys2; $i++ ) {
4732 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4735 put_record( $record, $out );
4738 @vals1 = Maasha::Common::get_fields( $fh1 );
4739 @vals2 = Maasha::Common::get_fields( $fh2 );
4751 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4755 for ( $i = 0; $i < @keys1; $i++ ) {
4756 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4759 put_record( $record, $out );
4762 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4766 for ( $i = 0; $i < @keys2; $i++ ) {
4767 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4770 put_record( $record, $out );
4777 # Martin A. Hansen, August 2007.
4779 # Grab for records in stream.
4781 my ( $in, # handle to in stream
4782 $out, # handle to out stream
4783 $options, # options hash
4788 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4790 if ( $options->{ "patterns" } )
4792 $patterns = [ split ",", $options->{ "patterns" } ];
4794 elsif ( -f $options->{ "patterns_in" } )
4796 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4798 elsif ( -f $options->{ "exact_in" } )
4800 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4802 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4807 if ( $options->{ "eval" } )
4809 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4817 while ( $record = get_record( $in ) )
4823 if ( $options->{ "keys" } )
4825 foreach $key ( @{ $options->{ "keys" } } )
4827 if ( exists $lookup_hash{ $record->{ $key } } )
4836 foreach $key ( keys %{ $record } )
4838 if ( not $options->{ "vals_only" } )
4840 if ( exists $lookup_hash{ $key } )
4847 if ( not $options->{ "keys_only" } )
4849 if ( exists $lookup_hash{ $record->{ $key } } )
4860 foreach $pattern ( @{ $patterns } )
4862 if ( $options->{ "keys" } )
4864 foreach $key ( @{ $options->{ "keys" } } )
4866 $pos = index $record->{ $key }, $pattern;
4868 goto FOUND if $pos >= 0;
4873 foreach $key ( keys %{ $record } )
4875 if ( not $options->{ "vals_only" } )
4877 $pos = index $key, $pattern;
4879 goto FOUND if $pos >= 0;
4882 if ( not $options->{ "keys_only" } )
4884 $pos = index $record->{ $key }, $pattern;
4886 goto FOUND if $pos >= 0;
4892 elsif ( $options->{ "regex" } )
4894 if ( $options->{ "keys" } )
4896 foreach $key ( @{ $options->{ "keys" } } )
4898 if ( $options->{ "case_insensitive" } ) {
4899 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4901 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4904 goto FOUND if $pos >= 0;
4909 foreach $key ( keys %{ $record } )
4911 if ( not $options->{ "vals_only" } )
4913 if ( $options->{ "case_insensitive" } ) {
4914 $pos = 1 if $key =~ /$options->{'regex'}/i;
4916 $pos = 1 if $key =~ /$options->{'regex'}/;
4919 goto FOUND if $pos >= 0;
4922 if ( not $options->{ "keys_only" } )
4924 if ( $options->{ "case_insensitive" } ) {
4925 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4927 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4930 goto FOUND if $pos >= 0;
4935 elsif ( $options->{ "eval" } )
4937 if ( defined $record->{ $key } )
4939 if ( $op eq "<" and $record->{ $key } < $val ) {
4940 $pos = 1 and goto FOUND;
4941 } elsif ( $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 "eq" and $record->{ $key } eq $val ) {
4952 $pos = 1 and goto FOUND;
4953 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4954 $pos = 1 and goto FOUND;
4961 if ( $pos >= 0 and not $options->{ "invert" } ) {
4962 put_record( $record, $out );
4963 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4964 put_record( $record, $out );
4972 # Martin A. Hansen, August 2007.
4974 # Evaluate extression for records in stream.
4976 my ( $in, # handle to in stream
4977 $out, # handle to out stream
4978 $options, # options hash
4983 my ( $record, $eval_key, $eval_val, $check, @keys );
4985 while ( $record = get_record( $in ) )
4987 if ( $options->{ "eval" } )
4989 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4997 @keys = split /\W+/, $eval_val;
4998 @keys = grep { ! /^\d+$/ } @keys;
5003 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5005 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5008 put_record( $record, $out );
5015 # Martin A. Hansen, June 2008.
5019 my ( $in, # handle to in stream
5020 $out, # handle to out stream
5021 $options, # options hash
5026 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5028 while ( $record = get_record( $in ) )
5032 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5034 push @rows, $record->{ $key };
5038 push @matrix, [ @rows ];
5043 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5045 foreach $row ( @matrix )
5047 for ( $i = 0; $i < @{ $row }; $i++ ) {
5048 $record->{ "V$i" } = $row->[ $i ];
5051 put_record( $record, $out );
5056 sub script_add_ident
5058 # Martin A. Hansen, May 2008.
5060 # Add a unique identifier to each record in stream.
5062 my ( $in, # handle to in stream
5063 $out, # handle to out stream
5064 $options, # options hash
5069 my ( $record, $key, $prefix, $i );
5071 $key = $options->{ "key" } || "ID";
5072 $prefix = $options->{ "prefix" } || "ID";
5076 while ( $record = get_record( $in ) )
5078 $record->{ $key } = sprintf( "$prefix%08d", $i );
5080 put_record( $record, $out );
5087 sub script_count_records
5089 # Martin A. Hansen, August 2007.
5091 # Count records in stream.
5093 my ( $in, # handle to in stream
5094 $out, # handle to out stream
5095 $options, # options hash
5100 my ( $record, $count, $result, $fh, $line );
5104 if ( $options->{ "no_stream" } )
5106 while ( $line = <$in> )
5110 $count++ if $line eq "---";
5115 while ( $record = get_record( $in ) )
5117 put_record( $record, $out );
5123 $result = { "RECORDS_COUNT" => $count };
5125 $fh = write_stream( $options->{ "data_out" } );
5127 put_record( $result, $fh );
5133 sub script_random_records
5135 # Martin A. Hansen, August 2007.
5137 # Pick a number or random records from stream.
5139 my ( $in, # handle to in stream
5140 $out, # handle to out stream
5141 $options, # options hash
5146 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5148 $options->{ "num" } ||= 10;
5150 $tmp_file = "$BP_TMP/random_records.tmp";
5152 $fh_out = Maasha::Common::write_open( $tmp_file );
5156 while ( $record = get_record( $in ) )
5158 put_record( $record, $fh_out );
5168 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5170 while ( $i < $options->{ "num" } )
5172 $rand = int( rand( $count ) );
5174 if ( not exists $rand_hash{ $rand } )
5176 $rand_hash{ $rand } = 1;
5178 $max = $rand if $rand > $max;
5184 $fh_in = Maasha::Common::read_open( $tmp_file );
5188 while ( $record = get_record( $fh_in ) )
5190 put_record( $record, $out ) if exists $rand_hash{ $count };
5192 last if $count == $max;
5203 sub script_sort_records
5205 # Martin A. Hansen, August 2007.
5207 # Sort to sort records according to keys.
5209 my ( $in, # handle to in stream
5210 $out, # handle to out stream
5211 $options, # options hash
5216 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5218 foreach $key ( @{ $options->{ "keys" } } )
5220 if ( $key =~ s/n$// ) {
5221 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5223 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5227 $sort_str = join " or ", @sort_cmd;
5228 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5230 while ( $record = get_record( $in ) ) {
5231 push @records, $record;
5234 @records = sort $sort_sub @records;
5236 if ( $options->{ "reverse" } )
5238 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5239 put_record( $records[ $i ], $out );
5244 for ( $i = 0; $i < scalar @records; $i++ ) {
5245 put_record( $records[ $i ], $out );
5251 sub script_count_vals
5253 # Martin A. Hansen, August 2007.
5255 # Count records in stream.
5257 my ( $in, # handle to in stream
5258 $out, # handle to out stream
5259 $options, # options hash
5264 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5266 $tmp_file = "$BP_TMP/count_cache.tmp";
5268 $fh_out = Maasha::Common::write_open( $tmp_file );
5273 while ( $record = get_record( $in ) )
5275 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5277 push @records, $record;
5279 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5281 map { put_record( $_, $fh_out ) } @records;
5288 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5299 $fh_in = Maasha::Common::read_open( $tmp_file );
5301 while ( $record = get_record( $fh_in ) )
5303 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5305 put_record( $record, $out );
5307 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5315 foreach $record ( @records )
5317 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5319 put_record( $record, $out );
5326 sub script_plot_histogram
5328 # Martin A. Hansen, September 2007.
5330 # Plot a simple histogram for a given key using GNU plot.
5332 my ( $in, # handle to in stream
5333 $out, # handle to out stream
5334 $options, # options hash
5339 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5341 $options->{ "title" } ||= "Histogram";
5342 $options->{ "sort" } ||= "num";
5344 while ( $record = get_record( $in ) )
5346 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5348 put_record( $record, $out ) if not $options->{ "no_stream" };
5351 if ( $options->{ "sort" } eq "num" ) {
5352 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5354 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5357 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5359 $fh = write_stream( $options->{ "data_out" } );
5361 print $fh "$_\n" foreach @{ $result };
5367 sub script_plot_lendist
5369 # Martin A. Hansen, August 2007.
5371 # Plot length distribution using GNU plot.
5373 my ( $in, # handle to in stream
5374 $out, # handle to out stream
5375 $options, # options hash
5380 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5382 $options->{ "title" } ||= "Length Distribution";
5384 while ( $record = get_record( $in ) )
5386 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5388 put_record( $record, $out ) if not $options->{ "no_stream" };
5391 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5393 for ( $i = 0; $i < $max; $i++ ) {
5394 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5397 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5399 $fh = write_stream( $options->{ "data_out" } );
5401 print $fh "$_\n" foreach @{ $result };
5407 sub script_plot_chrdist
5409 # Martin A. Hansen, August 2007.
5411 # Plot chromosome distribution using GNU plot.
5413 my ( $in, # handle to in stream
5414 $out, # handle to out stream
5415 $options, # options hash
5420 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5422 $options->{ "title" } ||= "Chromosome Distribution";
5424 while ( $record = get_record( $in ) )
5426 if ( $record->{ "CHR" } ) { # generic
5427 $data_hash{ $record->{ "CHR" } }++;
5428 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5429 $data_hash{ $record->{ "S_ID" } }++;
5430 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5431 $data_hash{ $record->{ "S_ID" } }++;
5432 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5433 $data_hash{ $record->{ "S_ID" } }++;
5436 put_record( $record, $out ) if not $options->{ "no_stream" };
5439 foreach $elem ( keys %data_hash )
5443 $sort_key =~ s/chr//i;
5445 $sort_key =~ s/^X(.*)/99$1/;
5446 $sort_key =~ s/^Y(.*)/99$1/;
5447 $sort_key =~ s/^Z(.*)/999$1/;
5448 $sort_key =~ s/^M(.*)/9999$1/;
5449 $sort_key =~ s/^U(.*)/99999$1/;
5451 $count = $sort_key =~ tr/_//;
5453 $sort_key =~ s/_.*/"999999" x $count/ex;
5455 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5458 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5460 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5462 $fh = write_stream( $options->{ "data_out" } );
5464 print $fh "$_\n" foreach @{ $result };
5470 sub script_plot_karyogram
5472 # Martin A. Hansen, August 2007.
5474 # Plot hits on karyogram.
5476 my ( $in, # handle to in stream
5477 $out, # handle to out stream
5478 $options, # options hash
5483 my ( %options, $record, @data, $fh, $result, %data_hash );
5485 $options->{ "genome" } ||= "human";
5486 $options->{ "feat_color" } ||= "black";
5488 while ( $record = get_record( $in ) )
5490 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5492 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5495 put_record( $record, $out ) if not $options->{ "no_stream" };
5498 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5500 $fh = write_stream( $options->{ "data_out" } );
5508 sub script_plot_matches
5510 # Martin A. Hansen, August 2007.
5512 # Plot matches in 2D generating a dotplot.
5514 my ( $in, # handle to in stream
5515 $out, # handle to out stream
5516 $options, # options hash
5521 my ( $record, @data, $fh, $result, %data_hash );
5523 $options->{ "direction" } ||= "both";
5525 while ( $record = get_record( $in ) )
5527 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5528 push @data, $record;
5531 put_record( $record, $out ) if not $options->{ "no_stream" };
5534 $options->{ "title" } ||= "plot_matches";
5535 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5536 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5538 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5540 $fh = write_stream( $options->{ "data_out" } );
5542 print $fh "$_\n" foreach @{ $result };
5548 sub script_length_vals
5550 # Martin A. Hansen, August 2007.
5552 # Determine the length of the value for given keys.
5554 my ( $in, # handle to in stream
5555 $out, # handle to out stream
5556 $options, # options hash
5561 my ( $record, $key );
5563 while ( $record = get_record( $in ) )
5565 foreach $key ( @{ $options->{ "keys" } } )
5567 if ( $record->{ $key } ) {
5568 $record->{ $key . "_LEN" } = length $record->{ $key };
5572 put_record( $record, $out );
5579 # Martin A. Hansen, August 2007.
5581 # Calculates the sums for values of given keys.
5583 my ( $in, # handle to in stream
5584 $out, # handle to out stream
5585 $options, # options hash
5590 my ( $record, $key, %sum_hash, $fh );
5592 while ( $record = get_record( $in ) )
5594 foreach $key ( @{ $options->{ "keys" } } )
5596 if ( $record->{ $key } ) {
5597 $sum_hash{ $key } += $record->{ $key };
5601 put_record( $record, $out ) if not $options->{ "no_stream" };
5604 $fh = write_stream( $options->{ "data_out" } );
5606 foreach $key ( @{ $options->{ "keys" } } ) {
5607 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5614 sub script_mean_vals
5616 # Martin A. Hansen, August 2007.
5618 # Calculate the mean of values of given keys.
5620 my ( $in, # handle to in stream
5621 $out, # handle to out stream
5622 $options, # options hash
5627 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5629 while ( $record = get_record( $in ) )
5631 foreach $key ( @{ $options->{ "keys" } } )
5633 if ( $record->{ $key } )
5635 $sum_hash{ $key } += $record->{ $key };
5636 $count_hash{ $key }++;
5640 put_record( $record, $out ) if not $options->{ "no_stream" };
5643 $fh = write_stream( $options->{ "data_out" } );
5645 foreach $key ( @{ $options->{ "keys" } } )
5647 if ( $count_hash{ $key } ) {
5648 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5653 put_record( { $key . "_MEAN" => $mean } , $fh );
5660 sub script_median_vals
5662 # Martin A. Hansen, March 2008.
5664 # Calculate the median values of given keys.
5666 my ( $in, # handle to in stream
5667 $out, # handle to out stream
5668 $options, # options hash
5673 my ( $record, $key, %median_hash, $median, $fh );
5675 while ( $record = get_record( $in ) )
5677 foreach $key ( @{ $options->{ "keys" } } ) {
5678 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5681 put_record( $record, $out ) if not $options->{ "no_stream" };
5684 $fh = write_stream( $options->{ "data_out" } );
5686 foreach $key ( @{ $options->{ "keys" } } )
5688 if ( $median_hash{ $key } ) {
5689 $median = Maasha::Calc::median( $median_hash{ $key } );
5694 put_record( { $key . "_MEDIAN" => $median } , $fh );
5703 # Martin A. Hansen, February 2008.
5705 # Determine the maximum values of given keys.
5707 my ( $in, # handle to in stream
5708 $out, # handle to out stream
5709 $options, # options hash
5714 my ( $record, $key, $fh, %max_hash, $max_record );
5716 while ( $record = get_record( $in ) )
5718 foreach $key ( @{ $options->{ "keys" } } )
5720 if ( $record->{ $key } )
5722 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5726 put_record( $record, $out ) if not $options->{ "no_stream" };
5729 $fh = write_stream( $options->{ "data_out" } );
5731 foreach $key ( @{ $options->{ "keys" } } )
5733 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5736 put_record( $max_record, $fh );
5744 # Martin A. Hansen, February 2008.
5746 # Determine the minimum values of given keys.
5748 my ( $in, # handle to in stream
5749 $out, # handle to out stream
5750 $options, # options hash
5755 my ( $record, $key, $fh, %min_hash, $min_record );
5757 while ( $record = get_record( $in ) )
5759 foreach $key ( @{ $options->{ "keys" } } )
5761 if ( defined $record->{ $key } )
5763 if ( exists $min_hash{ $key } ) {
5764 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5766 $min_hash{ $key } = $record->{ $key };
5771 put_record( $record, $out ) if not $options->{ "no_stream" };
5774 $fh = write_stream( $options->{ "data_out" } );
5776 foreach $key ( @{ $options->{ "keys" } } )
5778 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5781 put_record( $min_record, $fh );
5787 sub script_upload_to_ucsc
5789 # Martin A. Hansen, August 2007.
5791 # Calculate the mean of values of given keys.
5793 my ( $in, # handle to in stream
5794 $out, # handle to out stream
5795 $options, # options hash
5800 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5801 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5803 $options->{ "short_label" } ||= $options->{ 'table' };
5804 $options->{ "long_label" } ||= $options->{ 'table' };
5805 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5806 $options->{ "priority" } ||= 1;
5807 $options->{ "visibility" } ||= "pack";
5808 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5809 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5811 $file = "$BP_TMP/ucsc_upload.tmp";
5819 if ( $options->{ 'wiggle' } )
5821 $options->{ "visibility" } = "full";
5823 while ( $record = get_record( $in ) )
5825 put_record( $record, $out ) if not $options->{ "no_stream" };
5827 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5828 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5829 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5831 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5833 $fh_out = $fh_hash{ $record->{ "CHR" } };
5835 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5838 map { close $_ } keys %fh_hash;
5840 $fh_out = Maasha::Common::write_open( $file );
5842 foreach $chr ( sort keys %fh_hash )
5844 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5846 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5850 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5852 $chr = $entry->{ 'CHR' };
5853 $beg = $entry->{ 'CHR_BEG' };
5854 $end = $entry->{ 'CHR_END' };
5855 $q_id = $entry->{ 'Q_ID' };
5857 if ( $q_id =~ /_(\d+)$/ ) {
5867 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5872 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5873 $block->[ $i ] += $clones;
5876 $max = Maasha::Calc::max( $max, $end );
5885 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5886 $block->[ $i ] += $clones;
5893 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5895 unlink "$BP_TMP/$chr";
5900 $wig_file = "$options->{ 'table' }.wig";
5901 $wib_file = "$options->{ 'table' }.wib";
5903 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5905 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5907 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5909 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5910 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5920 $fh_out = Maasha::Common::write_open( $file );
5922 while ( $record = get_record( $in ) )
5924 put_record( $record, $out ) if not $options->{ "no_stream" };
5926 if ( $record->{ "REC_TYPE" } eq "PSL" )
5928 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5929 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5933 $format = "PSL" if not $format;
5935 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5937 # chrom chromStart chromEnd name score strand size secStr conf
5939 print $fh_out join ( "\t",
5941 $record->{ "CHR_BEG" },
5942 $record->{ "CHR_END" } + 1,
5943 $record->{ "Q_ID" },
5944 $record->{ "SCORE" },
5945 $record->{ "STRAND" },
5946 $record->{ "SIZE" },
5947 $record->{ "SEC_STRUCT" },
5948 $record->{ "CONF" },
5951 $format = "BED_SS" if not $format;
5953 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5955 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5957 $format = "BED" if not $format;
5958 $columns = $record->{ "BED_COLS" } if not $columns;
5960 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5962 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5964 $format = "BED" if not $format;
5965 $columns = 6 if not $columns;
5967 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5969 $record->{ "CHR" } = $record->{ "S_ID" };
5970 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5971 $record->{ "CHR_END" } = $record->{ "S_END" };
5972 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5974 $format = "BED" if not $format;
5975 $columns = 6 if not $columns;
5977 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5979 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5981 $record->{ "CHR" } = $record->{ "S_ID" };
5982 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5983 $record->{ "CHR_END" } = $record->{ "S_END" };
5984 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5985 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5987 $format = "BED" if not $format;
5988 $columns = 6 if not $columns;
5990 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5993 if ( $i == $options->{ "chunk_size" } )
5997 if ( $format eq "BED" ) {
5998 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5999 } elsif ( $format eq "PSL" ) {
6000 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6009 $fh_out = Maasha::Common::write_open( $file );
6018 if ( exists $options->{ "database" } and $options->{ "table" } )
6020 if ( $format eq "BED" )
6022 $type = "bed $columns";
6024 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6026 elsif ( $format eq "BED_SS" )
6028 $options->{ "sec_struct" } = 1;
6030 $type = "sec_struct";
6032 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6034 elsif ( $format eq "PSL" )
6038 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6040 elsif ( $format eq "WIGGLE" )
6044 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6049 Maasha::UCSC::update_my_tracks( $options, $type );
6054 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6059 # Martin A. Hansen, July 2008.
6061 # Given a biopiece record converts it to a FASTA record.
6062 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6063 # tried in that order.
6065 my ( $record, # record
6070 my ( $seq_name, $seq );
6072 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6073 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6075 if ( defined $seq_name and defined $seq ) {
6076 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6085 # Martin A. Hansen, July 2007.
6087 # Opens a stream to STDIN or a file,
6089 my ( $path, # path - OPTIONAL
6092 # Returns filehandle.
6096 if ( not -t STDIN ) {
6097 $fh = Maasha::Common::read_stdin();
6098 } elsif ( not $path ) {
6099 # Maasha::Common::error( qq(no data stream) );
6101 $fh = Maasha::Common::read_open( $path );
6104 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6112 # Martin A. Hansen, August 2007.
6114 # Opens a stream to STDOUT or a file.
6116 my ( $path, # path - OPTIONAL
6117 $gzip, # compress data - OPTIONAL
6120 # Returns filehandle.
6125 $fh = Maasha::Common::write_open( $path, $gzip );
6127 $fh = Maasha::Common::write_stdout();
6136 # Martin A. Hansen, July 2007.
6138 # Reads one record at a time and converts that record
6139 # to a Perl data structure (a hash) which is returned.
6141 my ( $fh, # handle to stream
6146 my ( $block, @lines, $line, $key, $value, %record );
6148 local $/ = "\n---\n";
6154 return if not defined $block;
6156 @lines = split "\n", $block;
6158 foreach $line ( @lines )
6160 ( $key, $value ) = split ": ", $line, 2;
6162 $record{ $key } = $value;
6165 return wantarray ? %record : \%record;
6171 # Martin A. Hansen, July 2007.
6173 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6175 my ( $data, # data structure
6176 $fh, # file handle - OPTIONAL
6181 if ( scalar keys %{ $data } )
6185 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6190 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6201 # Martin A. Hansen, November 2007.
6203 # Extracts files from an explicit GetOpt::Long argument
6204 # allowing for the use of glob. E.g.
6205 # --data_in=test.fna
6206 # --data_in=test.fna,test2.fna
6208 # --data_in=test.fna,/dir/*.fna
6210 my ( $option, # option from GetOpt::Long
6215 my ( $elem, @files );
6217 foreach $elem ( split ",", $option )
6221 } elsif ( $elem =~ /\*/ ) {
6222 push @files, glob( $elem );
6226 return wantarray ? @files : \@files;
6232 # Martin A. Hansen, April 2008.
6234 # Removes temporary directory and exits gracefully.
6235 # This subroutine is meant to be run always as the last
6236 # thing even if a script is dies or is interrupted
6239 my ( $sig, # signal from the %SIG
6242 # print STDERR "signal->$sig<-\n";
6250 if ( $sig =~ /MAASHA_ERROR/ ) {
6251 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6252 } elsif ( $sig eq "INT" ) {
6253 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6254 } elsif ( $sig eq "TERM" ) {
6255 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6257 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6269 # Martin A. Hansen, July 2008.
6271 # Cleans out any unused temporary files and direcotries in BP_TMP.
6275 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6277 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6279 $curr_pid = Maasha::Common::get_processid();
6281 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6283 foreach $dir ( @dirs )
6285 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6291 if ( $user eq Maasha::Common::get_user() )
6293 if ( not Maasha::Common::process_running( $pid ) )
6295 # print STDERR "Removing stale dir: $dir\n";
6296 Maasha::Common::dir_remove( $dir );
6298 elsif ( $pid == $curr_pid )
6300 # print STDERR "Removing current dir: $dir\n";
6301 Maasha::Common::dir_remove( $dir );
6315 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<