1 package Maasha::Biopieces;
4 # Copyright (C) 2007-2008 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Routines for manipulation, parsing and emitting of human/machine readable biopieces records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
57 use vars qw( @ISA @EXPORT_OK );
61 @ISA = qw( Exporter );
76 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
79 $SIG{ '__DIE__' } = \&sig_handler;
80 $SIG{ 'INT' } = \&sig_handler;
81 $SIG{ 'TERM' } = \&sig_handler;
84 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
87 my ( $script, $BP_TMP );
89 $script = Maasha::Common::get_scriptname();
90 $BP_TMP = Maasha::Common::get_tmpdir();
93 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
96 my $log_global = Maasha::Common::append_open( "$ENV{ 'BP_LOG' }/biopieces.log" );
97 my $log_local = Maasha::Common::append_open( "$ENV{ 'HOME' }/.biopieces.log" );
99 $log_global->autoflush( 1 );
100 $log_local->autoflush( 1 );
102 &log( $log_global, $script, \@ARGV );
103 &log( $log_local, $script, \@ARGV );
109 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
112 my $t0 = gettimeofday();
114 run_script( $script );
116 my $t1 = gettimeofday();
118 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
121 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
126 # Martin A. Hansen, January 2008.
128 # Log messages to logfile.
130 my ( $fh, # filehandle to logfile
131 $script, # script name
132 $argv, # reference to @ARGV
137 my ( $time_stamp, $user );
139 $time_stamp = Maasha::Common::time_stamp();
141 $user = $ENV{ 'USER' };
143 $script = "biopieces" if $script eq "-e";
145 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
151 # Martin A. Hansen, August 2007.
153 # Run a specific script.
155 my ( $script, # script name
160 my ( $options, $in, $out );
162 $options = get_options( $script );
164 $options->{ "SCRIPT" } = $script;
166 if ( $script ne "list_biopieces" and $script ne "list_genomes" ) {
167 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
170 $in = read_stream( $options->{ "stream_in" } );
171 $out = write_stream( $options->{ "stream_out" } );
173 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
174 elsif ( $script eq "list_biopieces" ) { script_list_biopieces( $in, $out, $options ) }
175 elsif ( $script eq "list_genomes" ) { script_list_genomes( $in, $out, $options ) }
176 elsif ( $script eq "read_fasta" ) { script_read_fasta( $in, $out, $options ) }
177 elsif ( $script eq "read_tab" ) { script_read_tab( $in, $out, $options ) }
178 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
179 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
180 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
181 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
182 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
183 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
184 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
185 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
186 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
187 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
188 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
189 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
190 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
191 elsif ( $script eq "format_genome" ) { script_format_genome( $in, $out, $options ) }
192 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
193 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
194 elsif ( $script eq "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
195 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
196 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
197 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
198 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
199 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
200 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
201 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
202 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
203 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
204 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
205 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
206 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
207 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
208 elsif ( $script eq "get_genome_seq" ) { script_get_genome_seq( $in, $out, $options ) }
209 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
210 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
211 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
212 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
213 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
214 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
215 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
216 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
217 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
218 elsif ( $script eq "create_blast_db" ) { script_create_blast_db( $in, $out, $options ) }
219 elsif ( $script eq "blast_seq" ) { script_blast_seq( $in, $out, $options ) }
220 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
221 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
222 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
223 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
224 elsif ( $script eq "vmatch_seq" ) { script_vmatch_seq( $in, $out, $options ) }
225 elsif ( $script eq "write_fasta" ) { script_write_fasta( $in, $out, $options ) }
226 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
227 elsif ( $script eq "write_blast" ) { script_write_blast( $in, $out, $options ) }
228 elsif ( $script eq "write_tab" ) { script_write_tab( $in, $out, $options ) }
229 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
230 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
231 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
232 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
233 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
234 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
235 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
236 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
237 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
238 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
239 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
240 elsif ( $script eq "grab" ) { script_grab( $in, $out, $options ) }
241 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
242 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
243 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
244 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
245 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
246 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
247 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
248 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
249 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
250 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
251 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
252 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
253 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
254 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
255 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
256 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
257 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
258 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
259 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
260 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
261 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
262 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
263 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
265 close $in if defined $in;
272 # Martin A. Hansen, February 2008.
274 # Gets options from commandline and checks these vigerously.
276 my ( $script, # name of script
281 my ( %options, @options, $opt, @genomes );
283 if ( $script eq "print_usage" )
289 elsif ( $script eq "read_fasta" )
296 elsif ( $script eq "read_tab" )
307 elsif ( $script eq "read_psl" )
314 elsif ( $script eq "read_bed" )
321 elsif ( $script eq "read_fixedstep" )
328 elsif ( $script eq "read_blast_tab" )
335 elsif ( $script eq "read_embl" )
345 elsif ( $script eq "read_stockholm" )
352 elsif ( $script eq "read_phastcons" )
363 elsif ( $script eq "read_soft" )
370 elsif ( $script eq "read_gff" )
377 elsif ( $script eq "read_2bit" )
385 elsif ( $script eq "read_solexa" )
393 elsif ( $script eq "read_solid" )
401 elsif ( $script eq "read_mysql" )
410 elsif ( $script eq "format_genome" )
419 elsif ( $script eq "length_seq" )
426 elsif ( $script eq "oligo_freq" )
433 elsif ( $script eq "create_weight_matrix" )
439 elsif ( $script eq "transliterate_seq" )
447 elsif ( $script eq "transliterate_vals" )
456 elsif ( $script eq "translate_seq" )
462 elsif ( $script eq "extract_seq" )
470 elsif ( $script eq "get_genome_seq" )
482 elsif ( $script eq "get_genome_align" )
493 elsif ( $script eq "get_genome_phastcons" )
504 elsif ( $script eq "split_seq" )
511 elsif ( $script eq "split_bed" )
518 elsif ( $script eq "tile_seq" )
525 elsif ( $script eq "invert_align" )
531 elsif ( $script eq "patscan_seq" )
542 elsif ( $script eq "create_blast_db" )
549 elsif ( $script eq "blast_seq" )
561 elsif ( $script eq "blat_seq" )
573 elsif ( $script eq "soap_seq" )
584 elsif ( $script eq "match_seq" )
591 elsif ( $script eq "create_vmatch_index" )
599 elsif ( $script eq "vmatch_seq" )
610 elsif ( $script eq "write_fasta" )
619 elsif ( $script eq "write_align" )
629 elsif ( $script eq "write_blast" )
638 elsif ( $script eq "write_tab" )
650 elsif ( $script eq "write_bed" )
658 elsif ( $script eq "write_psl" )
666 elsif ( $script eq "write_fixedstep" )
674 elsif ( $script eq "write_2bit" )
682 elsif ( $script eq "write_solid" )
691 elsif ( $script eq "plot_seqlogo" )
698 elsif ( $script eq "plot_phastcons_profiles" )
713 elsif ( $script eq "analyze_vals" )
720 elsif ( $script eq "head_records" )
726 elsif ( $script eq "remove_keys" )
733 elsif ( $script eq "rename_keys" )
739 elsif ( $script eq "uniq_vals" )
746 elsif ( $script eq "merge_vals" )
753 elsif ( $script eq "merge_records" )
760 elsif ( $script eq "grab" )
775 elsif ( $script eq "compute" )
781 elsif ( $script eq "add_ident" )
788 elsif ( $script eq "count_records" )
795 elsif ( $script eq "random_records" )
801 elsif ( $script eq "sort_records" )
808 elsif ( $script eq "count_vals" )
814 elsif ( $script eq "plot_histogram" )
827 elsif ( $script eq "plot_lendist" )
839 elsif ( $script eq "plot_chrdist" )
850 elsif ( $script eq "plot_karyogram" )
859 elsif ( $script eq "plot_matches" )
871 elsif ( $script eq "length_vals" )
877 elsif ( $script eq "sum_vals" )
885 elsif ( $script eq "mean_vals" )
893 elsif ( $script eq "median_vals" )
901 elsif ( $script eq "max_vals" )
909 elsif ( $script eq "min_vals" )
917 elsif ( $script eq "upload_to_ucsc" )
942 # print STDERR Dumper( \@options );
949 # print STDERR Dumper( \%options );
951 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
952 return wantarray ? %options : \%options;
955 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
956 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
957 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
958 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
959 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
960 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
961 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
962 $options{ "formats" } = [ split ",", $options{ "formats" } ] if defined $options{ "formats" };
964 # ---- check arguments ----
966 if ( $options{ 'data_in' } )
968 $options{ "files" } = getopt_files( $options{ 'data_in' } );
970 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
973 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
975 # print STDERR Dumper( \%options );
977 foreach $opt ( keys %options )
979 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
981 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
983 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+$/ )
985 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
987 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
989 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
991 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
993 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
995 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
997 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
999 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
1001 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
1003 elsif ( $opt eq "genome" and $script ne "format_genome" )
1005 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1006 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
1008 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
1009 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
1012 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
1014 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
1016 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
1018 Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
1020 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
1022 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
1026 Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
1027 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
1028 Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
1029 Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
1030 Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
1031 Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index_name" };
1032 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
1033 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
1034 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" };
1035 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
1036 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" };
1038 if ( $script eq "upload_to_ucsc" )
1040 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
1041 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
1044 return wantarray ? %options : \%options;
1048 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1051 sub script_print_usage
1053 # Martin A. Hansen, January 2008.
1055 # Retrieves usage information from file and
1056 # prints this nicely formatted.
1058 my ( $in, # handle to in stream
1059 $out, # handle to out stream
1060 $options, # options hash
1065 my ( $file, $wiki, $lines );
1067 if ( $options->{ 'data_in' } ) {
1068 $file = $options->{ 'data_in' };
1070 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
1073 $wiki = Maasha::Gwiki::gwiki_read( $file );
1075 if ( not $options->{ "help" } ) {
1076 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|Synopsis|Usage|Options|Help/ } @{ $wiki };
1079 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
1081 print STDERR "$_\n" foreach @{ $lines };
1087 sub script_list_biopieces
1089 # Martin A. Hansen, January 2008.
1091 # Prints the synopsis from the usage for each of the biopieces.
1093 my ( $in, # handle to in stream
1094 $out, # handle to out stream
1095 $options, # options hash
1100 my ( @files, $file, $wiki, $program, $synopsis );
1102 @files = Maasha::Common::ls_files( "$ENV{ 'BP_DIR' }/bp_usage" );
1104 foreach $file ( sort @files )
1106 if ( $file =~ /\/([a-z0-9_]+)\.wiki$/ )
1110 $wiki = Maasha::Gwiki::gwiki_read( $file );
1112 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Synopsis/ } @{ $wiki };
1113 @{ $wiki } = grep { $_->[ 0 ]->{ 'FORMAT' } =~ /paragraph/ } @{ $wiki };
1115 $synopsis = $wiki->[ 0 ]->[ 0 ]->{ 'TEXT' };
1116 $synopsis =~ s/!(\w)/$1/g;
1118 printf( "%-30s%s\n", $program, $synopsis );
1126 sub script_list_genomes
1128 # Martin A. Hansen, January 2008.
1130 # Prints the synopsis from the usage for each of the biopieces.
1132 my ( $in, # handle to in stream
1133 $out, # handle to out stream
1134 $options, # options hash
1139 my ( @genomes, $genome, @formats, $format, %hash, %found, @row );
1141 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
1143 foreach $genome ( @genomes )
1145 next if $genome =~ /\.$/;
1147 @formats = Maasha::Common::ls_dirs( $genome );
1149 foreach $format ( @formats )
1151 if ( $format =~ /\/([^\/]+)\/(\w+)$/ )
1153 $hash{ $1 }{ $2 } = 1;
1162 map { push @row, $_ } sort keys %found;
1164 print join( "\t", @row ), "\n";
1166 foreach $genome ( sort keys %hash )
1170 foreach $format ( sort keys %found )
1172 if ( exists $hash{ $genome }{ $format } ) {
1179 print join( "\t", @row ), "\n";
1184 sub script_read_fasta
1186 # Martin A. Hansen, August 2007.
1188 # Read sequences from FASTA file.
1190 my ( $in, # handle to in stream
1191 $out, # handle to out stream
1192 $options, # options hash
1197 my ( $record, $file, $data_in, $entry, $num );
1199 while ( $record = get_record( $in ) ) {
1200 put_record( $record, $out );
1205 foreach $file ( @{ $options->{ "files" } } )
1207 $data_in = Maasha::Common::read_open( $file );
1209 while ( $entry = Maasha::Fasta::get_entry( $data_in ) )
1211 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1214 SEQ_NAME => $entry->[ SEQ_NAME ],
1215 SEQ => $entry->[ SEQ ],
1216 SEQ_LEN => length $entry->[ SEQ ],
1219 put_record( $record, $out );
1222 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1232 close $data_in if $data_in;
1238 # Martin A. Hansen, August 2007.
1240 # Read table or table columns from stream or file.
1242 my ( $in, # handle to in stream
1243 $out, # handle to out stream
1244 $options, # options hash
1249 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1251 $options->{ 'delimit' } ||= '\s+';
1253 while ( $record = get_record( $in ) ) {
1254 put_record( $record, $out );
1257 $skip = $options->{ 'skip' } ||= 0;
1260 foreach $file ( @{ $options->{ "files" } } )
1262 $data_in = Maasha::Common::read_open( $file );
1264 while ( $line = <$data_in> )
1272 next if $line =~ /^#|^$/;
1279 @fields = split /$options->{'delimit'}/, $line;
1281 if ( $options->{ "cols" } ) {
1282 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1287 for ( $i = 0; $i < @fields2; $i++ )
1289 if ( $options->{ "keys" }->[ $i ] ) {
1290 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1292 $record->{ "V" . $i } = $fields2[ $i ];
1296 put_record( $record, $out );
1298 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1308 close $data_in if $data_in;
1314 # Martin A. Hansen, August 2007.
1316 # Read psl table from stream or file.
1318 my ( $in, # handle to in stream
1319 $out, # handle to out stream
1320 $options, # options hash
1325 my ( $record, @files, $file, $entries, $entry, $num );
1327 while ( $record = get_record( $in ) ) {
1328 put_record( $record, $out );
1333 foreach $file ( @{ $options->{ "files" } } )
1335 $entries = Maasha::UCSC::psl_get_entries( $file );
1337 foreach $entry ( @{ $entries } )
1339 put_record( $entry, $out );
1341 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1353 # Martin A. Hansen, August 2007.
1355 # Read bed table from stream or file.
1357 my ( $in, # handle to in stream
1358 $out, # handle to out stream
1359 $options, # options hash
1364 my ( $file, $record, $entry, $data_in, $num );
1366 while ( $record = get_record( $in ) ) {
1367 put_record( $record, $out );
1372 foreach $file ( @{ $options->{ "files" } } )
1374 $data_in = Maasha::Common::read_open( $file );
1376 while ( $entry = Maasha::UCSC::bed_get_entry( $data_in ) )
1378 put_record( $entry, $out );
1380 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1390 close $data_in if $data_in;
1394 sub script_read_fixedstep
1396 # Martin A. Hansen, Juli 2008.
1398 # Read fixedStep wiggle format from stream or file.
1400 my ( $in, # handle to in stream
1401 $out, # handle to out stream
1402 $options, # options hash
1407 my ( $file, $record, $entry, $head, $chr, $chr_beg, $step, $data_in, $num );
1409 while ( $record = get_record( $in ) ) {
1410 put_record( $record, $out );
1415 foreach $file ( @{ $options->{ "files" } } )
1417 $data_in = Maasha::Common::read_open( $file );
1419 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1421 $head = shift @{ $entry };
1423 if ( $head =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1425 $record->{ "CHR" } = $1;
1426 $record->{ "CHR_BEG" } = $2;
1427 $record->{ "STEP" } = $3;
1428 $record->{ "VALS" } = join ",", @{ $entry };
1431 put_record( $record, $out );
1433 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1443 close $data_in if $data_in;
1447 sub script_read_blast_tab
1449 # Martin A. Hansen, September 2007.
1451 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1453 my ( $in, # handle to in stream
1454 $out, # handle to out stream
1455 $options, # options hash
1460 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1462 while ( $record = get_record( $in ) ) {
1463 put_record( $record, $out );
1468 foreach $file ( @{ $options->{ "files" } } )
1470 $data_in = Maasha::Common::read_open( $file );
1472 while ( $line = <$data_in> )
1476 next if $line =~ /^#/;
1478 @fields = split /\t/, $line;
1480 $record->{ "REC_TYPE" } = "BLAST";
1481 $record->{ "Q_ID" } = $fields[ 0 ];
1482 $record->{ "S_ID" } = $fields[ 1 ];
1483 $record->{ "IDENT" } = $fields[ 2 ];
1484 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1485 $record->{ "MISMATCHES" } = $fields[ 4 ];
1486 $record->{ "GAPS" } = $fields[ 5 ];
1487 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1488 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1489 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1490 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1491 $record->{ "E_VAL" } = $fields[ 10 ];
1492 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1494 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1496 $record->{ "STRAND" } = '-';
1498 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1502 $record->{ "STRAND" } = '+';
1505 put_record( $record, $out );
1507 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1517 close $data_in if $data_in;
1521 sub script_read_embl
1523 # Martin A. Hansen, August 2007.
1527 my ( $in, # handle to in stream
1528 $out, # handle to out stream
1529 $options, # options hash
1534 my ( %options2, $file, $data_in, $num, $entry, $record );
1536 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1537 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1538 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1540 while ( $record = get_record( $in ) ) {
1541 put_record( $record, $out );
1546 foreach $file ( @{ $options->{ "files" } } )
1548 $data_in = Maasha::Common::read_open( $file );
1550 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1552 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1554 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1556 $record_copy = dclone $record;
1558 delete $record_copy->{ "FT" };
1560 put_record( $record_copy, $out );
1562 delete $record_copy->{ "SEQ" };
1564 foreach $feat ( keys %{ $record->{ "FT" } } )
1566 $record_copy->{ "FEAT_TYPE" } = $feat;
1568 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1570 foreach $qual ( keys %{ $feat2 } )
1572 $qual_val = join "; ", @{ $feat2->{ $qual } };
1577 $record_copy->{ $qual } = $qual_val;
1580 put_record( $record_copy, $out );
1584 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1594 close $data_in if $data_in;
1598 sub script_read_stockholm
1600 # Martin A. Hansen, August 2007.
1602 # Read Stockholm format.
1604 my ( $in, # handle to in stream
1605 $out, # handle to out stream
1606 $options, # options hash
1611 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1613 while ( $record = get_record( $in ) ) {
1614 put_record( $record, $out );
1619 foreach $file ( @{ $options->{ "files" } } )
1621 $data_in = Maasha::Common::read_open( $file );
1623 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1625 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1629 foreach $key ( keys %{ $record->{ "GF" } } ) {
1630 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1633 $record_anno->{ "ALIGN" } = $num;
1635 put_record( $record_anno, $out );
1637 foreach $seq ( @{ $record->{ "ALIGN" } } )
1639 undef $record_align;
1642 SEQ_NAME => $seq->[ 0 ],
1646 put_record( $record_align, $out );
1649 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1659 close $data_in if $data_in;
1663 sub script_read_phastcons
1665 # Martin A. Hansen, December 2007.
1667 # Read PhastCons format.
1669 my ( $in, # handle to in stream
1670 $out, # handle to out stream
1671 $options, # options hash
1676 my ( $data_in, $file, $num, $entry, @records, $record );
1678 $options->{ "min" } ||= 10;
1679 $options->{ "dist" } ||= 25;
1680 $options->{ "threshold" } ||= 0.8;
1681 $options->{ "gap" } ||= 5;
1683 while ( $record = get_record( $in ) ) {
1684 put_record( $record, $out );
1689 foreach $file ( @{ $options->{ "files" } } )
1691 $data_in = Maasha::Common::read_open( $file );
1693 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1695 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1697 foreach $record ( @records )
1699 $record->{ "REC_TYPE" } = "BED";
1700 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1702 put_record( $record, $out );
1704 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1715 close $data_in if $data_in;
1719 sub script_read_soft
1721 # Martin A. Hansen, December 2007.
1724 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1726 my ( $in, # handle to in stream
1727 $out, # handle to out stream
1728 $options, # options hash
1733 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1735 while ( $record = get_record( $in ) ) {
1736 put_record( $record, $out );
1741 foreach $file ( @{ $options->{ "files" } } )
1743 $soft_index = Maasha::NCBI::soft_index_file( $file );
1745 $fh = Maasha::Common::read_open( $file );
1747 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1749 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1751 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1753 $old_end = $platforms[ -1 ]->[ 2 ];
1755 foreach $sample ( @samples )
1757 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1759 foreach $record ( @{ $records } )
1761 put_record( $record, $out );
1763 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1768 $old_end = $sample->[ 2 ];
1776 close $data_in if $data_in;
1783 # Martin A. Hansen, February 2008.
1786 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1788 my ( $in, # handle to in stream
1789 $out, # handle to out stream
1790 $options, # options hash
1795 my ( $data_in, $file, $fh, $num, $record, $entry );
1797 while ( $record = get_record( $in ) ) {
1798 put_record( $record, $out );
1803 foreach $file ( @{ $options->{ "files" } } )
1805 $fh = Maasha::Common::read_open( $file );
1807 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1809 put_record( $entry, $out );
1811 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1821 close $data_in if $data_in;
1825 sub script_read_2bit
1827 # Martin A. Hansen, March 2008.
1829 # Read sequences from 2bit file.
1831 my ( $in, # handle to in stream
1832 $out, # handle to out stream
1833 $options, # options hash
1838 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1840 $mask = 1 if not $options->{ "no_mask" };
1842 while ( $record = get_record( $in ) ) {
1843 put_record( $record, $out );
1848 foreach $file ( @{ $options->{ "files" } } )
1850 $data_in = Maasha::Common::read_open( $file );
1852 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1854 foreach $line ( @{ $toc } )
1856 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1857 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1858 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1860 put_record( $record, $out );
1862 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1872 close $data_in if $data_in;
1876 sub script_read_solexa
1878 # Martin A. Hansen, March 2008.
1880 # Read Solexa sequence reads from file.
1882 my ( $in, # handle to in stream
1883 $out, # handle to out stream
1884 $options, # options hash
1889 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1891 $options->{ "quality" } ||= 20;
1893 while ( $record = get_record( $in ) ) {
1894 put_record( $record, $out );
1899 foreach $file ( @{ $options->{ "files" } } )
1901 $data_in = Maasha::Common::read_open( $file );
1902 $base_name = Maasha::Common::get_basename( $file );
1903 $base_name =~ s/\..*//;
1907 while ( $line = <$data_in> )
1909 @fields = split /:/, $line;
1910 @seqs = split //, $fields[ 5 ];
1911 @scores = split / /, $fields[ -1 ];
1913 for ( $i = 0; $i < @scores; $i++ ) {
1914 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1917 $seq = join "", @seqs;
1919 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1920 $record->{ "SEQ" } = $seq;
1921 $record->{ "SEQ_LEN" } = length $seq;
1922 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", Maasha::Calc::mean( \@scores ) );
1924 put_record( $record, $out );
1926 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1937 close $data_in if $data_in;
1941 sub script_read_solid
1943 # Martin A. Hansen, April 2008.
1945 # Read Solid sequence from file.
1947 my ( $in, # handle to in stream
1948 $out, # handle to out stream
1949 $options, # options hash
1954 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1956 $options->{ "quality" } ||= 15;
1958 while ( $record = get_record( $in ) ) {
1959 put_record( $record, $out );
1964 foreach $file ( @{ $options->{ "files" } } )
1966 $data_in = Maasha::Common::read_open( $file );
1968 while ( $line = <$data_in> )
1972 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1974 @scores = split /,/, $seq_qual;
1975 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1977 for ( $i = 0; $i < @seqs; $i++ ) {
1978 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1982 SEQ_NAME => $seq_name,
1984 SEQ_QUAL => $seq_qual,
1985 SEQ_LEN => length $seq_cs,
1986 SEQ => join( "", @seqs ),
1987 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1990 put_record( $record, $out );
1992 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
2002 close $data_in if $data_in;
2006 sub script_read_mysql
2008 # Martin A. Hansen, May 2008.
2010 # Read a MySQL query into stream.
2012 my ( $in, # handle to in stream
2013 $out, # handle to out stream
2014 $options, # options hash
2019 my ( $record, $dbh, $results );
2021 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
2022 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
2024 while ( $record = get_record( $in ) ) {
2025 put_record( $record, $out );
2028 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
2030 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
2032 Maasha::SQL::disconnect( $dbh );
2034 map { put_record( $_ ) } @{ $results };
2038 sub script_format_genome
2040 # Martin A. Hansen, Juli 2008.
2042 # Format a genome to speficed formats.
2044 my ( $in, # handle to in stream
2045 $out, # handle to out stream
2046 $options, # options hash
2051 my ( $dir, $genome, $fasta_dir, $phastcons_dir, $vals, $fh_out, $record, $format, $index, $entry );
2053 $dir = $options->{ 'dir' } || $ENV{ 'BP_DATA' };
2054 $genome = $options->{ 'genome' };
2056 Maasha::Common::error( "Directory: $dir does not exist" ) if not -d $dir;
2057 Maasha::Common::dir_create_if_not_exists( "$dir/genomes" );
2058 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome" );
2060 if ( grep { $_ =~ /fasta|blast|vmatch/i } @{ $options->{ "formats" } } )
2062 if ( -f "$dir/genomes/$genome/fasta/$genome.fna" )
2064 $fasta_dir = "$dir/genomes/$genome/fasta";
2068 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/fasta" );
2070 $fasta_dir = "$dir/genomes/$genome/fasta";
2072 $fh_out = Maasha::Common::write_open( "$fasta_dir/$genome.fna" );
2075 elsif ( grep { $_ =~ /phastcons/i } @{ $options->{ "formats" } } )
2077 Maasha::Common::dir_create_if_not_exists( "$dir/genomes/$genome/phastcons" );
2079 $phastcons_dir = "$dir/genomes/$genome/phastcons";
2081 $fh_out = Maasha::Common::write_open( "$phastcons_dir/$genome.pp" );
2084 while ( $record = get_record( $in ) )
2086 if ( $fh_out and $entry = record2fasta( $record ) )
2088 Maasha::Fasta::put_entry( $entry, $fh_out, $options->{ "wrap" } );
2090 elsif ( $fh_out and $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
2092 print $fh_out "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
2094 $vals = $record->{ 'VALS' };
2098 print $fh_out "$vals\n";
2101 put_record( $record, $out ) if not $options->{ "no_stream" };
2104 foreach $format ( @{ $options->{ 'formats' } } )
2106 if ( $format =~ /^fasta$/i ) { Maasha::Fasta::fasta_index( "$fasta_dir/$genome.fna", "$dir/genomes/$genome/fasta/$genome.index" ) }
2107 elsif ( $format =~ /^blast$/i ) { Maasha::NCBI::blast_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/blast", "dna", $genome ) }
2108 elsif ( $format =~ /^blat$/i ) { print STDERR "BLAT FORMAT NOT IMPLEMENTED" }
2109 elsif ( $format =~ /^vmatch$/i ) { Maasha::Match::vmatch_index( "$genome.fna", $fasta_dir, "$dir/genomes/$genome/vmatch", $BP_TMP ) }
2110 elsif ( $format =~ /^phastcons$/i ) { Maasha::UCSC::phastcons_index( "$genome.pp", $phastcons_dir ) }
2113 close $fh_out if $fh_out;
2117 sub script_length_seq
2119 # Martin A. Hansen, August 2007.
2121 # Determine the length of sequences in stream.
2123 my ( $in, # handle to in stream
2124 $out, # handle to out stream
2125 $options, # options hash
2130 my ( $record, $total );
2132 while ( $record = get_record( $in ) )
2134 if ( $record->{ "SEQ" } )
2136 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2137 $total += $record->{ "SEQ_LEN" };
2140 put_record( $record, $out ) if not $options->{ "no_stream" };
2143 put_record( { TOTAL_SEQ_LEN => $total }, $out );
2147 sub script_uppercase_seq
2149 # Martin A. Hansen, August 2007.
2151 # Uppercases sequences in stream.
2153 my ( $in, # handle to in stream
2154 $out, # handle to out stream
2161 while ( $record = get_record( $in ) )
2163 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2165 put_record( $record, $out );
2170 sub script_shuffle_seq
2172 # Martin A. Hansen, December 2007.
2174 # Shuffle sequences in stream.
2176 my ( $in, # handle to in stream
2177 $out, # handle to out stream
2184 while ( $record = get_record( $in ) )
2186 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2188 put_record( $record, $out );
2193 sub script_analyze_seq
2195 # Martin A. Hansen, August 2007.
2197 # Analyze sequence composition of sequences in stream.
2199 my ( $in, # handle to in stream
2200 $out, # handle to out stream
2205 my ( $record, $analysis );
2207 while ( $record = get_record( $in ) )
2209 if ( $record->{ "SEQ" } )
2211 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2213 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2216 put_record( $record, $out );
2221 sub script_analyze_tags
2223 # Martin A. Hansen, August 2008.
2225 # Analyze sequence tags in stream.
2227 my ( $in, # handle to in stream
2228 $out, # handle to out stream
2233 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2235 while ( $record = get_record( $in ) )
2237 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2239 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2243 $len_hash{ length( $record->{ "SEQ" } ) }++;
2244 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2247 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2249 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2253 $len_hash{ $record->{ "BED_LEN" } }++;
2254 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2259 foreach $key ( sort { $a <=> $b } keys %len_hash )
2261 $tag_record->{ "TAG_LEN" } = $key;
2262 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2263 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2265 put_record( $tag_record, $out );
2270 sub script_complexity_seq
2272 # Martin A. Hansen, May 2008.
2274 # Generates an index calculated as the most common di-residue over
2275 # the sequence length for all sequences in stream.
2277 my ( $in, # handle to in stream
2278 $out, # handle to out stream
2283 my ( $record, $index );
2285 while ( $record = get_record( $in ) )
2287 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2289 put_record( $record, $out );
2294 sub script_oligo_freq
2296 # Martin A. Hansen, August 2007.
2298 # Determine the length of sequences in stream.
2300 my ( $in, # handle to in stream
2301 $out, # handle to out stream
2302 $options, # options hash
2307 my ( $record, %oligos, @freq_table );
2309 $options->{ "word_size" } ||= 7;
2311 while ( $record = get_record( $in ) )
2313 if ( $record->{ "SEQ" } )
2315 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2317 if ( not $options->{ "all" } )
2319 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2321 map { put_record( $_, $out ) } @freq_table;
2327 put_record( $record, $out );
2330 if ( $options->{ "all" } )
2332 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2334 map { put_record( $_, $out ) } @freq_table;
2339 sub script_create_weight_matrix
2341 # Martin A. Hansen, August 2007.
2343 # Creates a weight matrix from an alignmnet.
2345 my ( $in, # handle to in stream
2346 $out, # handle to out stream
2347 $options, # options hash
2352 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2356 while ( $record = get_record( $in ) )
2358 if ( $record->{ "SEQ" } )
2360 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2362 $res = substr $record->{ "SEQ" }, $i, 1;
2364 $freq_hash{ $i }{ $res }++;
2365 $res_hash{ $res } = 1;
2372 put_record( $record, $out );
2376 foreach $res ( sort keys %res_hash )
2380 $record->{ "V0" } = $res;
2382 for ( $i = 0; $i < keys %freq_hash; $i++ )
2384 $freq = $freq_hash{ $i }{ $res } || 0;
2386 if ( $options->{ "percent" } ) {
2387 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2390 $record->{ "V" . ( $i + 1 ) } = $freq;
2393 put_record( $record, $out );
2398 sub script_calc_bit_scores
2400 # Martin A. Hansen, March 2007.
2402 # Calculates the bit scores for each position from an alignmnet in the stream.
2404 my ( $in, # handle to in stream
2405 $out, # handle to out stream
2410 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2414 while ( $record = get_record( $in ) )
2416 if ( $record->{ "SEQ" } )
2418 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2420 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2422 $res = substr $record->{ "SEQ" }, $i, 1;
2424 next if $res =~ /-|_|~|\./;
2426 $freq_hash{ $i }{ $res }++;
2433 put_record( $record, $out );
2439 if ( $type eq "protein" ) {
2445 for ( $i = 0; $i < keys %freq_hash; $i++ )
2447 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2449 $bit_diff = $bit_max - $bit_height;
2451 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2454 put_record( $record, $out );
2458 sub script_reverse_seq
2460 # Martin A. Hansen, August 2007.
2462 # Reverse sequence in record.
2464 my ( $in, # handle to in stream
2465 $out, # handle to out stream
2472 while ( $record = get_record( $in ) )
2474 if ( $record->{ "SEQ" } ) {
2475 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2478 put_record( $record, $out );
2483 sub script_complement_seq
2485 # Martin A. Hansen, August 2007.
2487 # Complement sequence in record.
2489 my ( $in, # handle to in stream
2490 $out, # handle to out stream
2495 my ( $record, $type );
2497 while ( $record = get_record( $in ) )
2499 if ( $record->{ "SEQ" } )
2502 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2505 if ( $type eq "rna" ) {
2506 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2507 } elsif ( $type eq "dna" ) {
2508 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2512 put_record( $record, $out );
2517 sub script_remove_indels
2519 # Martin A. Hansen, August 2007.
2521 # Remove indels from sequences in stream.
2523 my ( $in, # handle to in stream
2524 $out, # handle to out stream
2531 while ( $record = get_record( $in ) )
2533 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2535 put_record( $record, $out );
2540 sub script_transliterate_seq
2542 # Martin A. Hansen, August 2007.
2544 # Transliterate chars from sequence in record.
2546 my ( $in, # handle to in stream
2547 $out, # handle to out stream
2548 $options, # options hash
2553 my ( $record, $search, $replace, $delete );
2555 $search = $options->{ "search" } || "";
2556 $replace = $options->{ "replace" } || "";
2557 $delete = $options->{ "delete" } || "";
2559 while ( $record = get_record( $in ) )
2561 if ( $record->{ "SEQ" } )
2563 if ( $search and $replace ) {
2564 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2565 } elsif ( $delete ) {
2566 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2570 put_record( $record, $out );
2575 sub script_transliterate_vals
2577 # Martin A. Hansen, April 2008.
2579 # Transliterate chars from values in record.
2581 my ( $in, # handle to in stream
2582 $out, # handle to out stream
2583 $options, # options hash
2588 my ( $record, $search, $replace, $delete, $key );
2590 $search = $options->{ "search" } || "";
2591 $replace = $options->{ "replace" } || "";
2592 $delete = $options->{ "delete" } || "";
2594 while ( $record = get_record( $in ) )
2596 foreach $key ( @{ $options->{ "keys" } } )
2598 if ( exists $record->{ $key } )
2600 if ( $search and $replace ) {
2601 eval "\$record->{ $key } =~ tr/$search/$replace/";
2602 } elsif ( $delete ) {
2603 eval "\$record->{ $key } =~ tr/$delete//d";
2608 put_record( $record, $out );
2613 sub script_translate_seq
2615 # Martin A. Hansen, February 2008.
2617 # Translate DNA sequence into protein sequence.
2619 my ( $in, # handle to in stream
2620 $out, # handle to out stream
2621 $options, # options hash
2626 my ( $record, $frame, %new_record );
2628 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2630 while ( $record = get_record( $in ) )
2632 if ( $record->{ "SEQ" } )
2634 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2636 foreach $frame ( @{ $options->{ "frames" } } )
2638 %new_record = %{ $record };
2640 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2641 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2642 $new_record{ "FRAME" } = $frame;
2644 put_record( \%new_record, $out );
2650 put_record( $record, $out );
2656 sub script_extract_seq
2658 # Martin A. Hansen, August 2007.
2660 # Extract subsequences from sequences in record.
2662 my ( $in, # handle to in stream
2663 $out, # handle to out stream
2664 $options, # options hash
2669 my ( $beg, $end, $len, $record );
2671 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2674 $beg = $options->{ "beg" } - 1; # correcting for start offset
2677 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2679 } elsif ( defined $options->{ "end" } ) {
2680 $end = $options->{ "end" } - 1; # correcting for start offset
2683 $len = $options->{ "len" };
2685 # print "beg->$beg, end->$end, len->$len\n";
2687 while ( $record = get_record( $in ) )
2689 if ( $record->{ "SEQ" } )
2691 if ( defined $beg and defined $end )
2693 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2694 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2696 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2699 elsif ( defined $beg and defined $len )
2701 if ( $len > length $record->{ "SEQ" } ) {
2702 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2704 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2707 elsif ( defined $beg )
2709 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2713 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2715 put_record( $record, $out );
2720 sub script_get_genome_seq
2722 # Martin A. Hansen, December 2007.
2724 # Gets a subsequence from a genome.
2726 my ( $in, # handle to in stream
2727 $out, # handle to out stream
2728 $options, # options hash
2733 my ( $record, $genome, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2735 $options->{ "flank" } ||= 0;
2737 if ( $options->{ "genome" } )
2739 $genome = $options->{ "genome" };
2741 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.fna";
2742 $index_file = "$ENV{ 'BP_DATA' }/genomes/$genome/fasta/$genome.index";
2744 $fh = Maasha::Common::read_open( $genome_file );
2745 $index = Maasha::Fasta::index_retrieve( $index_file );
2747 shift @{ $index }; # Get rid of the file size info
2749 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2751 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2753 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2755 $beg = $index_beg + $options->{ "beg" } - 1;
2757 if ( $options->{ "len" } ) {
2758 $len = $options->{ "len" };
2759 } elsif ( $options->{ "end" } ) {
2760 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2763 $beg -= $options->{ "flank" };
2764 $len += 2 * $options->{ "flank" };
2766 if ( $beg <= $index_beg )
2768 $len -= $index_beg - $beg;
2772 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2774 next if $beg > $index_beg + $index_len;
2776 $record->{ "CHR" } = $options->{ "chr" };
2777 $record->{ "CHR_BEG" } = $beg - $index_beg;
2778 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2780 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2781 $record->{ "SEQ_LEN" } = $len;
2783 put_record( $record, $out );
2787 while ( $record = get_record( $in ) )
2789 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2791 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2793 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2795 $beg = $record->{ "CHR_BEG" } + $index_beg;
2796 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2798 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2800 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2802 $beg = $record->{ "S_BEG" } + $index_beg;
2803 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2805 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2807 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2809 $beg = $record->{ "S_BEG" } + $index_beg;
2810 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2813 $beg -= $options->{ "flank" };
2814 $len += 2 * $options->{ "flank" };
2816 if ( $beg <= $index_beg )
2818 $len -= $index_beg - $beg;
2822 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2824 next if $beg > $index_beg + $index_len;
2826 $record->{ "CHR_BEG" } = $beg - $index_beg;
2827 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2829 $record->{ "SEQ" } = Maasha::Common::file_read( $fh, $beg, $len );
2831 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2833 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2834 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2837 if ( $options->{ "mask" } )
2839 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2841 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2843 @begs = split ",", $record->{ "Q_BEGS" };
2844 @lens = split ",", $record->{ "BLOCKSIZES" };
2846 for ( $i = 0; $i < @begs; $i++ ) {
2847 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2853 put_record( $record, $out );
2860 sub script_get_genome_align
2862 # Martin A. Hansen, April 2008.
2864 # Gets a subalignment from a multiple genome alignment.
2866 my ( $in, # handle to in stream
2867 $out, # handle to out stream
2868 $options, # options hash
2873 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2875 $options->{ "strand" } ||= "+";
2879 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2881 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2883 $beg = $options->{ "beg" } - 1;
2885 if ( $options->{ "end" } ) {
2886 $end = $options->{ "end" };
2887 } elsif ( $options->{ "len" } ) {
2888 $end = $beg + $options->{ "len" };
2891 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2893 foreach $entry ( @{ $align } )
2895 $entry->{ "CHR" } = $record->{ "CHR" };
2896 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2897 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2898 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2899 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2900 $entry->{ "SCORE" } = $record->{ "SCORE" };
2902 put_record( $entry, $out );
2906 while ( $record = get_record( $in ) )
2908 if ( $record->{ "REC_TYPE" } eq "BED" )
2910 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2912 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2914 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2916 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2918 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2921 foreach $entry ( @{ $align } )
2923 $entry->{ "CHR" } = $record->{ "CHR" };
2924 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2925 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2926 $entry->{ "STRAND" } = $record->{ "STRAND" };
2927 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2928 $entry->{ "SCORE" } = $record->{ "SCORE" };
2930 put_record( $entry, $out );
2938 sub script_get_genome_phastcons
2940 # Martin A. Hansen, February 2008.
2942 # Get phastcons scores from genome intervals.
2944 my ( $in, # handle to in stream
2945 $out, # handle to out stream
2946 $options, # options hash
2951 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2953 $options->{ "flank" } ||= 0;
2955 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2956 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2958 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2959 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2961 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2963 $options->{ "beg" } -= 1; # request is 1-based
2964 $options->{ "end" } -= 1; # request is 1-based
2966 if ( $options->{ "len" } ) {
2967 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2970 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2972 $record->{ "CHR" } = $options->{ "chr" };
2973 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2974 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2976 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2977 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2979 put_record( $record, $out );
2982 while ( $record = get_record( $in ) )
2984 if ( $record->{ "REC_TYPE" } eq "BED" )
2986 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2988 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2990 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2992 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2994 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2997 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2998 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
3000 put_record( $record, $out );
3003 close $fh_phastcons if $fh_phastcons;
3009 # Martin A. Hansen, December 2007.
3011 # Folds sequences in stream into secondary structures.
3013 my ( $in, # handle to in stream
3014 $out, # handle to out stream
3019 my ( $record, $type, $struct, $index );
3021 while ( $record = get_record( $in ) )
3023 if ( $record->{ "SEQ" } )
3026 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
3029 if ( $type ne "protein" )
3031 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
3032 $record->{ "SEC_STRUCT" } = $struct;
3033 $record->{ "FREE_ENERGY" } = $index;
3034 $record->{ "SCORE" } = abs int $index;
3035 $record->{ "SIZE" } = length $struct;
3036 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
3040 put_record( $record, $out );
3045 sub script_split_seq
3047 # Martin A. Hansen, August 2007.
3049 # Split a sequence in stream into words.
3051 my ( $in, # handle to in stream
3052 $out, # handle to out stream
3053 $options, # options hash
3058 my ( $record, $new_record, $i, $subseq, %lookup );
3060 $options->{ "word_size" } ||= 7;
3062 while ( $record = get_record( $in ) )
3064 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3066 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
3068 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
3070 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
3072 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3073 $new_record->{ "SEQ" } = $subseq;
3075 put_record( $new_record, $out );
3077 $lookup{ $subseq } = 1;
3081 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
3082 $new_record->{ "SEQ" } = $subseq;
3084 put_record( $new_record, $out );
3090 put_record( $record, $out );
3096 sub script_split_bed
3098 # Martin A. Hansen, June 2008.
3100 # Split a BED record into overlapping windows.
3102 my ( $in, # handle to in stream
3103 $out, # handle to out stream
3104 $options, # options hash
3109 my ( $record, $new_record, $i );
3111 $options->{ "window_size" } ||= 20;
3112 $options->{ "step_size" } ||= 1;
3114 while ( $record = get_record( $in ) )
3116 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3118 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
3120 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
3122 $new_record->{ "REC_TYPE" } = "BED";
3123 $new_record->{ "CHR" } = $record->{ "CHR" };
3124 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
3125 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
3126 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
3127 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
3128 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3129 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3131 put_record( $new_record, $out );
3136 put_record( $record, $out );
3142 sub script_align_seq
3144 # Martin A. Hansen, August 2007.
3146 # Align sequences in stream.
3148 my ( $in, # handle to in stream
3149 $out, # handle to out stream
3154 my ( $record, @entries, $entry );
3156 while ( $record = get_record( $in ) )
3158 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3159 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3160 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3161 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3163 put_record( $record, $out );
3167 @entries = Maasha::Align::align( \@entries );
3169 foreach $entry ( @entries )
3171 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3174 SEQ_NAME => $entry->[ SEQ_NAME ],
3175 SEQ => $entry->[ SEQ ],
3178 put_record( $record, $out );
3186 # Martin A. Hansen, February 2008.
3188 # Using the first sequence in stream as reference, tile
3189 # all subsequent sequences based on pairwise alignments.
3191 my ( $in, # handle to in stream
3192 $out, # handle to out stream
3193 $options, # options hash
3198 my ( $record, $first, $ref_entry, @entries );
3202 while ( $record = get_record( $in ) )
3204 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3208 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3214 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3219 put_record( $record, $out );
3223 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3225 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3229 sub script_invert_align
3231 # Martin A. Hansen, February 2008.
3233 # Inverts an alignment showing only non-mathing residues
3234 # using the first sequence as reference.
3236 my ( $in, # handle to in stream
3237 $out, # handle to out stream
3238 $options, # options hash
3243 my ( $record, @entries );
3245 while ( $record = get_record( $in ) )
3247 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3249 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3253 put_record( $record, $out );
3257 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3259 map { put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
3263 sub script_patscan_seq
3265 # Martin A. Hansen, August 2007.
3267 # Locates patterns in sequences using scan_for_matches.
3269 my ( $in, # handle to in stream
3270 $out, # handle to out stream
3271 $options, # options hash
3276 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3278 if ( $options->{ "patterns" } ) {
3279 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3280 } elsif ( -f $options->{ "patterns_in" } ) {
3281 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3284 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3286 push @args, "-c" if $options->{ "comp" };
3287 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3288 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3290 $seq_file = "$BP_TMP/patscan.seq";
3291 $pat_file = "$BP_TMP/patscan.pat";
3292 $out_file = "$BP_TMP/patscan.out";
3294 $fh_out = Maasha::Common::write_open( $seq_file );
3298 while ( $record = get_record( $in ) )
3300 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3302 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3304 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3306 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3314 $arg = join " ", @args;
3315 $arg .= " -p" if $type eq "protein";
3317 foreach $pattern ( @{ $patterns } )
3319 $fh_out = Maasha::Common::write_open( $pat_file );
3321 print $fh_out "$pattern\n";
3325 if ( $options->{ 'genome' } ) {
3326 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3327 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3329 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3330 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3333 $fh_in = Maasha::Common::read_open( $out_file );
3335 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
3337 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
3339 if ( $options->{ 'genome' } )
3341 $result->{ "CHR" } = $result->{ "S_ID" };
3342 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3343 $result->{ "CHR_END" } = $result->{ "S_END" };
3345 delete $result->{ "S_ID" };
3346 delete $result->{ "S_BEG" };
3347 delete $result->{ "S_END" };
3351 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3354 put_record( $result, $out );
3366 sub script_create_blast_db
3368 # Martin A. Hansen, September 2007.
3370 # Creates a NCBI BLAST database with formatdb
3372 my ( $in, # handle to in stream
3373 $out, # handle to out stream
3374 $options, # options hash
3379 my ( $fh, $seq_type, $path, $record, $entry );
3381 $path = $options->{ "database" };
3383 $fh = Maasha::Common::write_open( $path );
3385 while ( $record = get_record( $in ) )
3387 put_record( $record, $out ) if not $options->{ "no_stream" };
3389 if ( $entry = record2fasta( $record ) )
3391 $seq_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $seq_type;
3393 Maasha::Fasta::put_entry( $entry, $fh );
3399 if ( $seq_type eq "protein" ) {
3400 Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3402 Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3409 sub script_blast_seq
3411 # Martin A. Hansen, September 2007.
3413 # BLASTs sequences in stream against a given database.
3415 my ( $in, # handle to in stream
3416 $out, # handle to out stream
3417 $options, # options hash
3422 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry );
3424 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3425 $options->{ "filter" } = "F";
3426 $options->{ "filter" } = "T" if $options->{ "filter" };
3427 $options->{ "cpus" } ||= 1;
3429 $options->{ "database" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/blast/$options->{ 'genome' }.fna" if $options->{ 'genome' };
3431 $tmp_in = "$BP_TMP/blast_query.seq";
3432 $tmp_out = "$BP_TMP/blast.result";
3434 $fh_out = Maasha::Common::write_open( $tmp_in );
3436 while ( $record = get_record( $in ) )
3438 if ( $entry = record2fasta( $record ) )
3440 $q_type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $q_type;
3442 Maasha::Fasta::put_entry( $entry, $fh_out );
3445 put_record( $record, $out );
3450 if ( -f $options->{ 'database' } . ".phr" ) {
3451 $s_type = "protein";
3453 $s_type = "nucleotide";
3456 if ( not $options->{ 'program' } )
3458 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3459 $options->{ 'program' } = "blastn";
3460 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3461 $options->{ 'program' } = "blastp";
3462 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3463 $options->{ 'program' } = "blastx";
3464 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3465 $options->{ 'program' } = "tblastn";
3469 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 );
3473 $fh_out = Maasha::Common::read_open( $tmp_out );
3477 while ( $line = <$fh_out> )
3481 next if $line =~ /^#/;
3483 @fields = split /\s+/, $line;
3485 $record->{ "REC_TYPE" } = "BLAST";
3486 $record->{ "Q_ID" } = $fields[ 0 ];
3487 $record->{ "S_ID" } = $fields[ 1 ];
3488 $record->{ "IDENT" } = $fields[ 2 ];
3489 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3490 $record->{ "MISMATCHES" } = $fields[ 4 ];
3491 $record->{ "GAPS" } = $fields[ 5 ];
3492 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3493 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3494 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3495 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3496 $record->{ "E_VAL" } = $fields[ 10 ];
3497 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3499 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3501 $record->{ "STRAND" } = '-';
3503 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3507 $record->{ "STRAND" } = '+';
3510 put_record( $record, $out );
3521 # Martin A. Hansen, August 2007.
3523 # BLATs sequences in stream against a given genome.
3525 my ( $in, # handle to in stream
3526 $out, # handle to out stream
3527 $options, # options hash
3532 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
3534 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3536 $options->{ 'tile_size' } ||= 11;
3537 $options->{ 'one_off' } ||= 0;
3538 $options->{ 'min_identity' } ||= 90;
3539 $options->{ 'min_score' } ||= 0;
3540 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3542 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3543 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3544 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3545 $blat_args .= " -minScore=$options->{ 'min_score' }";
3546 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3547 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3549 $query_file = "$BP_TMP/blat.seq";
3551 $fh_out = Maasha::Common::write_open( $query_file );
3553 while ( $record = get_record( $in ) )
3555 if ( $entry = record2fasta( $record ) )
3557 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3558 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3561 put_record( $record, $out );
3566 $blat_args .= " -t=dnax" if $type eq "protein";
3567 $blat_args .= " -q=$type";
3569 $result_file = "$BP_TMP/blat.psl";
3571 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3575 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3577 map { put_record( $_, $out ) } @{ $entries };
3579 unlink $result_file;
3585 # Martin A. Hansen, July 2008.
3587 # soap sequences in stream against a given file or genome.
3589 my ( $in, # handle to in stream
3590 $out, # handle to out stream
3591 $options, # options hash
3596 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3598 $options->{ "seed_size" } ||= 10;
3599 $options->{ "mismatches" } ||= 2;
3600 $options->{ "gap_size" } ||= 0;
3601 $options->{ "cpus" } ||= 1;
3603 if ( $options->{ "genome" } ) {
3604 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3607 $tmp_in = "$BP_TMP/soap_query.seq";
3608 $tmp_out = "$BP_TMP/soap.result";
3610 $fh_out = Maasha::Common::write_open( $tmp_in );
3614 while ( $record = get_record( $in ) )
3616 if ( $entry = record2fasta( $record ) )
3618 Maasha::Fasta::put_entry( $entry, $fh_out );
3623 put_record( $record, $out );
3631 "-s $options->{ 'seed_size' }",
3634 "-v $options->{ 'mismatches' }",
3635 "-g $options->{ 'gap_size' }",
3636 "-p $options->{ 'cpus' }",
3637 "-d $options->{ 'in_file' }",
3641 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3643 Maasha::Common::run( "soap", $args, 1 );
3647 $fh_out = Maasha::Common::read_open( $tmp_out );
3651 while ( $line = <$fh_out> )
3655 @fields = split /\t/, $line;
3657 $record->{ "REC_TYPE" } = "SOAP";
3658 $record->{ "Q_ID" } = $fields[ 0 ];
3659 $record->{ "SCORE" } = $fields[ 3 ];
3660 $record->{ "STRAND" } = $fields[ 6 ];
3661 $record->{ "S_ID" } = $fields[ 7 ];
3662 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3663 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3665 put_record( $record, $out );
3675 sub script_match_seq
3677 # Martin A. Hansen, August 2007.
3679 # BLATs sequences in stream against a given genome.
3681 my ( $in, # handle to in stream
3682 $out, # handle to out stream
3683 $options, # options hash
3688 my ( $record, @entries, $results );
3690 $options->{ "word_size" } ||= 20;
3691 $options->{ "direction" } ||= "both";
3693 while ( $record = get_record( $in ) )
3695 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3696 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3699 put_record( $record, $out );
3702 if ( @entries == 1 )
3704 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3706 map { put_record( $_, $out ) } @{ $results };
3708 elsif ( @entries == 2 )
3710 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3712 map { put_record( $_, $out ) } @{ $results };
3717 sub script_create_vmatch_index
3719 # Martin A. Hansen, January 2008.
3721 # Create a vmatch index from sequences in the stream.
3723 my ( $in, # handle to in stream
3724 $out, # handle to out stream
3725 $options, # options hash
3730 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3732 if ( $options->{ "index_name" } )
3734 $file_tmp = $options->{ 'index_name' };
3735 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3738 while ( $record = get_record( $in ) )
3740 if ( $options->{ "index_name" } and $entry = record2fasta( $record ) )
3742 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3744 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3747 put_record( $record, $out ) if not $options->{ "no_stream" };
3750 if ( $options->{ "index_name" } )
3754 if ( $type eq "protein" ) {
3755 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3757 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3765 sub script_vmatch_seq
3767 # Martin A. Hansen, August 2007.
3769 # Vmatches sequences in stream against a given genome.
3771 my ( $in, # handle to in stream
3772 $out, # handle to out stream
3773 $options, # options hash
3778 my ( @index_files, @records, $result_file, $fh_in, $record, %hash );
3780 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3782 if ( $options->{ "index_name" } )
3784 @index_files = $options->{ "index_name" };
3788 @index_files = Maasha::Common::ls_files( "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/vmatch" );
3790 map { $_ =~ /^(.+)\.[a-z1]{3,4}$/; $hash{ $1 } = 1 } @index_files;
3792 @index_files = sort keys %hash;
3795 while ( $record = get_record( $in ) )
3797 push @records, $record;
3799 put_record( $record, $out );
3802 $result_file = Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3806 $fh_in = Maasha::Common::read_open( $result_file );
3808 while ( $record = Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3809 put_record( $record, $out );
3814 unlink $result_file;
3818 sub script_write_fasta
3820 # Martin A. Hansen, August 2007.
3822 # Write FASTA entries from sequences in stream.
3824 my ( $in, # handle to in stream
3825 $out, # handle to out stream
3826 $options, # options hash
3831 my ( $record, $fh, $entry );
3833 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3835 while ( $record = get_record( $in ) )
3837 if ( $entry = record2fasta( $record ) ) {
3838 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3841 put_record( $record, $out ) if not $options->{ "no_stream" };
3848 sub script_write_align
3850 # Martin A. Hansen, August 2007.
3852 # Write pretty alignments aligned sequences in stream.
3854 my ( $in, # handle to in stream
3855 $out, # handle to out stream
3856 $options, # options hash
3861 my ( $fh, $record, @entries );
3863 $fh = write_stream( $options->{ "data_out" } ) ;
3865 while ( $record = get_record( $in ) )
3867 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3868 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3871 put_record( $record, $out ) if not $options->{ "no_stream" };
3874 if ( scalar( @entries ) == 2 ) {
3875 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3876 } elsif ( scalar ( @entries ) > 2 ) {
3877 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3884 sub script_write_blast
3886 # Martin A. Hansen, November 2007.
3888 # Write data in blast table format (-m8 and 9).
3890 my ( $in, # handle to in stream
3891 $out, # handle to out stream
3892 $options, # options hash
3897 my ( $fh, $record, $first );
3899 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3903 while ( $record = get_record( $in ) )
3905 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3907 if ( $options->{ "comment" } and $first )
3909 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";
3914 if ( $record->{ "STRAND" } eq "-" ) {
3915 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3918 print $fh join( "\t",
3919 $record->{ "Q_ID" },
3920 $record->{ "S_ID" },
3921 $record->{ "IDENT" },
3922 $record->{ "ALIGN_LEN" },
3923 $record->{ "MISMATCHES" },
3924 $record->{ "GAPS" },
3925 $record->{ "Q_BEG" } + 1,
3926 $record->{ "Q_END" } + 1,
3927 $record->{ "S_BEG" } + 1,
3928 $record->{ "S_END" } + 1,
3929 $record->{ "E_VAL" },
3930 $record->{ "BIT_SCORE" }
3934 put_record( $record, $out ) if not $options->{ "no_stream" };
3941 sub script_write_tab
3943 # Martin A. Hansen, August 2007.
3945 # Write data as table.
3947 my ( $in, # handle to in stream
3948 $out, # handle to out stream
3949 $options, # options hash
3954 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3956 $options->{ "delimit" } ||= "\t";
3958 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3960 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3962 while ( $record = get_record( $in ) )
3967 if ( $options->{ "keys" } )
3969 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3973 foreach $key ( @{ $options->{ "keys" } } )
3975 if ( exists $record->{ $key } )
3977 push @keys, $key if $options->{ "comment" };
3978 push @vals, $record->{ $key };
3985 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3987 next if exists $no_keys{ $key };
3989 push @keys, $key if $options->{ "comment" };
3990 push @vals, $record->{ $key };
3994 if ( @keys and $options->{ "comment" } )
3996 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3998 delete $options->{ "comment" };
4001 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
4003 put_record( $record, $out ) if not $options->{ "no_stream" };
4010 sub script_write_bed
4012 # Martin A. Hansen, August 2007.
4014 # Write BED format for the UCSC genome browser using records in stream.
4016 my ( $in, # handle to in stream
4017 $out, # handle to out stream
4018 $options, # options hash
4023 my ( $fh, $record, $new_record );
4025 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4027 while ( $record = get_record( $in ) )
4029 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
4031 Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
4033 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
4035 $new_record->{ "CHR" } = $record->{ "S_ID" };
4036 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4037 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4038 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4039 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4040 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4042 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4044 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
4046 Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
4048 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
4050 $new_record->{ "CHR" } = $record->{ "S_ID" };
4051 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4052 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4053 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4054 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4055 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4057 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4059 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4061 $new_record->{ "CHR" } = $record->{ "S_ID" };
4062 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4063 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4064 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4065 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
4066 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4068 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4070 elsif ( $record->{ "REC_TYPE" } eq "SOAP" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
4072 $new_record->{ "CHR" } = $record->{ "S_ID" };
4073 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
4074 $new_record->{ "CHR_END" } = $record->{ "S_END" };
4075 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
4076 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
4077 $new_record->{ "STRAND" } = $record->{ "STRAND" };
4079 Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
4081 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
4083 Maasha::UCSC::bed_put_entry( $record, $fh );
4086 put_record( $record, $out ) if not $options->{ "no_stream" };
4093 sub script_write_psl
4095 # Martin A. Hansen, August 2007.
4097 # Write PSL output from stream.
4099 my ( $in, # handle to in stream
4100 $out, # handle to out stream
4101 $options, # options hash
4106 my ( $fh, $record, @output, $first );
4110 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4112 while ( $record = get_record( $in ) )
4114 put_record( $record, $out ) if not $options->{ "no_stream" };
4116 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
4118 Maasha::UCSC::psl_put_header( $fh ) if $first;
4119 Maasha::UCSC::psl_put_entry( $record, $fh );
4128 sub script_write_fixedstep
4130 # Martin A. Hansen, Juli 2008.
4132 # Write fixedStep entries from recrods in the stream.
4134 my ( $in, # handle to in stream
4135 $out, # handle to out stream
4136 $options, # options hash
4141 my ( $fh, $record, $vals );
4143 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4145 while ( $record = get_record( $in ) )
4147 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "STEP" } and $record->{ "VALS" } )
4149 print $fh "fixedStep chrom=$record->{ 'CHR' } start=$record->{ 'CHR_BEG' } step=$record->{ 'STEP' }\n";
4151 $vals = $record->{ 'VALS' };
4155 print $fh "$vals\n";
4158 put_record( $record, $out ) if not $options->{ "no_stream" };
4165 sub script_write_2bit
4167 # Martin A. Hansen, March 2008.
4169 # Write sequence entries from stream in 2bit format.
4171 my ( $in, # handle to in stream
4172 $out, # handle to out stream
4173 $options, # options hash
4178 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
4180 $mask = 1 if not $options->{ "no_mask" };
4182 $tmp_file = "$BP_TMP/write_2bit.fna";
4183 $fh_tmp = Maasha::Common::write_open( $tmp_file );
4185 $fh_out = write_stream( $options->{ "data_out" } );
4187 while ( $record = get_record( $in ) )
4189 if ( $entry = record2fasta( $record ) ) {
4190 Maasha::Fasta::put_entry( $entry, $fh_tmp );
4193 put_record( $record, $out ) if not $options->{ "no_stream" };
4198 $fh_in = Maasha::Common::read_open( $tmp_file );
4200 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
4209 sub script_write_solid
4211 # Martin A. Hansen, April 2008.
4213 # Write di-base encoded Solid sequence from entries in stream.
4215 my ( $in, # handle to in stream
4216 $out, # handle to out stream
4217 $options, # options hash
4222 my ( $record, $fh, $entry );
4224 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
4226 while ( $record = get_record( $in ) )
4228 if ( $entry = record2fasta( $record ) )
4230 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
4232 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
4235 put_record( $record, $out ) if not $options->{ "no_stream" };
4242 sub script_plot_seqlogo
4244 # Martin A. Hansen, August 2007.
4246 # Calculates and writes a sequence logo for alignments.
4248 my ( $in, # handle to in stream
4249 $out, # handle to out stream
4250 $options, # options hash
4255 my ( $record, @entries, $logo, $fh );
4257 while ( $record = get_record( $in ) )
4259 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4260 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4263 put_record( $record, $out ) if not $options->{ "no_stream" };
4266 $logo = Maasha::Plot::seq_logo( \@entries );
4268 $fh = write_stream( $options->{ "data_out" } );
4276 sub script_plot_phastcons_profiles
4278 # Martin A. Hansen, January 2008.
4280 # Plots PhastCons profiles.
4282 my ( $in, # handle to in stream
4283 $out, # handle to out stream
4284 $options, # options hash
4289 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4291 $options->{ "title" } ||= "PhastCons Profiles";
4293 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
4294 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4296 $index = Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4297 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
4299 while ( $record = get_record( $in ) )
4301 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4303 $scores = Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4305 push @{ $AoA }, [ @{ $scores } ];
4308 put_record( $record, $out ) if not $options->{ "no_stream" };
4311 Maasha::UCSC::phastcons_normalize( $AoA );
4313 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4314 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4316 $AoA = Maasha::Matrix::matrix_flip( $AoA );
4318 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4320 $fh = write_stream( $options->{ "data_out" } );
4322 print $fh "$_\n" foreach @{ $plot };
4328 sub script_analyze_bed
4330 # Martin A. Hansen, March 2008.
4332 # Analyze BED entries in stream.
4334 my ( $in, # handle to in stream
4335 $out, # handle to out stream
4336 $options, # options hash
4343 while ( $record = get_record( $in ) )
4345 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4347 put_record( $record, $out );
4352 sub script_analyze_vals
4354 # Martin A. Hansen, August 2007.
4356 # Analyze values for given keys in stream.
4358 my ( $in, # handle to in stream
4359 $out, # handle to out stream
4360 $options, # options hash
4365 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4367 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4369 while ( $record = get_record( $in ) )
4371 foreach $key ( keys %{ $record } )
4373 next if $options->{ "keys" } and not exists $key_hash{ $key };
4375 $analysis->{ $key }->{ "COUNT" }++;
4377 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
4379 $analysis->{ $key }->{ "TYPE" } = "num";
4380 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4381 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4382 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4386 $len = length $record->{ $key };
4388 $analysis->{ $key }->{ "TYPE" } = "alph";
4389 $analysis->{ $key }->{ "SUM" } += $len;
4390 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4391 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4395 put_record( $record, $out ) if not $options->{ "no_stream" };
4398 foreach $key ( keys %{ $analysis } )
4400 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4401 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
4404 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4414 if ( $options->{ "keys" } ) {
4415 @keys = @{ $options->{ "keys" } };
4417 @keys = keys %{ $analysis };
4420 foreach $key ( @keys )
4422 $keys .= sprintf "% 15s", $key;
4423 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4424 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4425 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4426 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4427 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4428 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4431 print $out "$keys\n";
4432 print $out "$types\n";
4433 print $out "$counts\n";
4434 print $out "$mins\n";
4435 print $out "$maxs\n";
4436 print $out "$sums\n";
4437 print $out "$means\n";
4441 sub script_head_records
4443 # Martin A. Hansen, August 2007.
4445 # Display the first sequences in stream.
4447 my ( $in, # handle to in stream
4448 $out, # handle to out stream
4449 $options, # options hash
4454 my ( $record, $count );
4456 $options->{ "num" } ||= 10;
4460 while ( $record = get_record( $in ) )
4464 put_record( $record, $out );
4466 last if $count == $options->{ "num" };
4471 sub script_remove_keys
4473 # Martin A. Hansen, August 2007.
4475 # Remove keys from stream.
4477 my ( $in, # handle to in stream
4478 $out, # handle to out stream
4479 $options, # options hash
4484 my ( $record, $new_record );
4486 while ( $record = get_record( $in ) )
4488 if ( $options->{ "keys" } )
4490 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4492 elsif ( $options->{ "save_keys" } )
4494 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4496 $record = $new_record;
4499 put_record( $record, $out ) if keys %{ $record };
4504 sub script_rename_keys
4506 # Martin A. Hansen, August 2007.
4508 # Rename keys in stream.
4510 my ( $in, # handle to in stream
4511 $out, # handle to out stream
4512 $options, # options hash
4519 while ( $record = get_record( $in ) )
4521 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4523 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4525 delete $record->{ $options->{ "keys" }->[ 0 ] };
4528 put_record( $record, $out );
4533 sub script_uniq_vals
4535 # Martin A. Hansen, August 2007.
4537 # Find unique values in stream.
4539 my ( $in, # handle to in stream
4540 $out, # handle to out stream
4541 $options, # options hash
4546 my ( %hash, $record );
4548 while ( $record = get_record( $in ) )
4550 if ( $record->{ $options->{ "key" } } )
4552 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4554 put_record( $record, $out );
4556 $hash{ $record->{ $options->{ "key" } } } = 1;
4558 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4560 put_record( $record, $out );
4564 $hash{ $record->{ $options->{ "key" } } } = 1;
4569 put_record( $record, $out );
4575 sub script_merge_vals
4577 # Martin A. Hansen, August 2007.
4579 # Rename keys in stream.
4581 my ( $in, # handle to in stream
4582 $out, # handle to out stream
4583 $options, # options hash
4588 my ( $record, @join, $i );
4590 $options->{ "delimit" } ||= '_';
4592 while ( $record = get_record( $in ) )
4594 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4596 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4598 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4599 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4602 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4605 put_record( $record, $out );
4610 sub script_merge_records
4612 # Martin A. Hansen, July 2008.
4614 # Merges records in the stream based on identical values of two given keys.
4616 my ( $in, # handle to in stream
4617 $out, # handle to out stream
4618 $options, # options hash
4623 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4624 $num1, $num2, $num, $cmp, $i );
4626 $merge = $options->{ "merge" } || "AandB";
4628 $file1 = "$BP_TMP/merge_records1.tmp";
4629 $file2 = "$BP_TMP/merge_records2.tmp";
4631 $fh1 = Maasha::Common::write_open( $file1 );
4632 $fh2 = Maasha::Common::write_open( $file2 );
4634 $key1 = $options->{ "keys" }->[ 0 ];
4635 $key2 = $options->{ "keys" }->[ 1 ];
4637 $num = $key2 =~ s/n$//;
4641 while ( $record = get_record( $in ) )
4643 if ( exists $record->{ $key1 } )
4646 @vals1 = $record->{ $key1 };
4648 delete $record->{ $key1 };
4650 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4652 print $fh1 join( "\t", @vals1 ), "\n";
4656 elsif ( exists $record->{ $key2 } )
4659 @vals2 = $record->{ $key2 };
4661 delete $record->{ $key2 };
4663 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4665 print $fh2 join( "\t", @vals2 ), "\n";
4676 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4677 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4681 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4682 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4685 $fh1 = Maasha::Common::read_open( $file1 );
4686 $fh2 = Maasha::Common::read_open( $file2 );
4688 @vals1 = Maasha::Common::get_fields( $fh1 );
4689 @vals2 = Maasha::Common::get_fields( $fh2 );
4691 while ( $num1 > 0 and $num2 > 0 )
4696 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4698 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4703 if ( $merge =~ /^(AorB|AnotB)$/ )
4705 for ( $i = 0; $i < @keys1; $i++ ) {
4706 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4709 put_record( $record, $out );
4712 @vals1 = Maasha::Common::get_fields( $fh1 );
4717 if ( $merge =~ /^(BorA|BnotA)$/ )
4719 for ( $i = 0; $i < @keys2; $i++ ) {
4720 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4723 put_record( $record, $out );
4726 @vals2 = Maasha::Common::get_fields( $fh2 );
4731 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4733 for ( $i = 0; $i < @keys1; $i++ ) {
4734 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4737 for ( $i = 1; $i < @keys2; $i++ ) {
4738 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4741 put_record( $record, $out );
4744 @vals1 = Maasha::Common::get_fields( $fh1 );
4745 @vals2 = Maasha::Common::get_fields( $fh2 );
4757 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4761 for ( $i = 0; $i < @keys1; $i++ ) {
4762 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4765 put_record( $record, $out );
4768 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4772 for ( $i = 0; $i < @keys2; $i++ ) {
4773 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4776 put_record( $record, $out );
4783 # Martin A. Hansen, August 2007.
4785 # Grab for records in stream.
4787 my ( $in, # handle to in stream
4788 $out, # handle to out stream
4789 $options, # options hash
4794 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4796 if ( $options->{ "patterns" } )
4798 $patterns = [ split ",", $options->{ "patterns" } ];
4800 elsif ( -f $options->{ "patterns_in" } )
4802 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4804 elsif ( -f $options->{ "exact_in" } )
4806 $patterns = Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4808 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4813 if ( $options->{ "eval" } )
4815 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4823 while ( $record = get_record( $in ) )
4829 if ( $options->{ "keys" } )
4831 foreach $key ( @{ $options->{ "keys" } } )
4833 if ( exists $lookup_hash{ $record->{ $key } } )
4842 foreach $key ( keys %{ $record } )
4844 if ( not $options->{ "vals_only" } )
4846 if ( exists $lookup_hash{ $key } )
4853 if ( not $options->{ "keys_only" } )
4855 if ( exists $lookup_hash{ $record->{ $key } } )
4866 foreach $pattern ( @{ $patterns } )
4868 if ( $options->{ "keys" } )
4870 foreach $key ( @{ $options->{ "keys" } } )
4872 $pos = index $record->{ $key }, $pattern;
4874 goto FOUND if $pos >= 0;
4879 foreach $key ( keys %{ $record } )
4881 if ( not $options->{ "vals_only" } )
4883 $pos = index $key, $pattern;
4885 goto FOUND if $pos >= 0;
4888 if ( not $options->{ "keys_only" } )
4890 $pos = index $record->{ $key }, $pattern;
4892 goto FOUND if $pos >= 0;
4898 elsif ( $options->{ "regex" } )
4900 if ( $options->{ "keys" } )
4902 foreach $key ( @{ $options->{ "keys" } } )
4904 if ( $options->{ "case_insensitive" } ) {
4905 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4907 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4910 goto FOUND if $pos >= 0;
4915 foreach $key ( keys %{ $record } )
4917 if ( not $options->{ "vals_only" } )
4919 if ( $options->{ "case_insensitive" } ) {
4920 $pos = 1 if $key =~ /$options->{'regex'}/i;
4922 $pos = 1 if $key =~ /$options->{'regex'}/;
4925 goto FOUND if $pos >= 0;
4928 if ( not $options->{ "keys_only" } )
4930 if ( $options->{ "case_insensitive" } ) {
4931 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4933 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4936 goto FOUND if $pos >= 0;
4941 elsif ( $options->{ "eval" } )
4943 if ( defined $record->{ $key } )
4945 if ( $op eq "<" and $record->{ $key } < $val ) {
4946 $pos = 1 and goto FOUND;
4947 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4948 $pos = 1 and goto FOUND;
4949 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4950 $pos = 1 and goto FOUND;
4951 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4952 $pos = 1 and goto FOUND;
4953 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4954 $pos = 1 and goto FOUND;
4955 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4956 $pos = 1 and goto FOUND;
4957 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4958 $pos = 1 and goto FOUND;
4959 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4960 $pos = 1 and goto FOUND;
4967 if ( $pos >= 0 and not $options->{ "invert" } ) {
4968 put_record( $record, $out );
4969 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4970 put_record( $record, $out );
4978 # Martin A. Hansen, August 2007.
4980 # Evaluate extression for records in stream.
4982 my ( $in, # handle to in stream
4983 $out, # handle to out stream
4984 $options, # options hash
4989 my ( $record, $eval_key, $eval_val, $check, @keys );
4991 while ( $record = get_record( $in ) )
4993 if ( $options->{ "eval" } )
4995 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
5003 @keys = split /\W+/, $eval_val;
5004 @keys = grep { ! /^\d+$/ } @keys;
5009 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
5011 $record->{ $eval_key } = eval "$eval_val" or Maasha::Common::error( "eval failed -> $@" );
5014 put_record( $record, $out );
5021 # Martin A. Hansen, June 2008.
5025 my ( $in, # handle to in stream
5026 $out, # handle to out stream
5027 $options, # options hash
5032 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
5034 while ( $record = get_record( $in ) )
5038 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
5040 push @rows, $record->{ $key };
5044 push @matrix, [ @rows ];
5049 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
5051 foreach $row ( @matrix )
5053 for ( $i = 0; $i < @{ $row }; $i++ ) {
5054 $record->{ "V$i" } = $row->[ $i ];
5057 put_record( $record, $out );
5062 sub script_add_ident
5064 # Martin A. Hansen, May 2008.
5066 # Add a unique identifier to each record in stream.
5068 my ( $in, # handle to in stream
5069 $out, # handle to out stream
5070 $options, # options hash
5075 my ( $record, $key, $prefix, $i );
5077 $key = $options->{ "key" } || "ID";
5078 $prefix = $options->{ "prefix" } || "ID";
5082 while ( $record = get_record( $in ) )
5084 $record->{ $key } = sprintf( "$prefix%08d", $i );
5086 put_record( $record, $out );
5093 sub script_count_records
5095 # Martin A. Hansen, August 2007.
5097 # Count records in stream.
5099 my ( $in, # handle to in stream
5100 $out, # handle to out stream
5101 $options, # options hash
5106 my ( $record, $count, $result, $fh, $line );
5110 if ( $options->{ "no_stream" } )
5112 while ( $line = <$in> )
5116 $count++ if $line eq "---";
5121 while ( $record = get_record( $in ) )
5123 put_record( $record, $out );
5129 $result = { "RECORDS_COUNT" => $count };
5131 $fh = write_stream( $options->{ "data_out" } );
5133 put_record( $result, $fh );
5139 sub script_random_records
5141 # Martin A. Hansen, August 2007.
5143 # Pick a number or random records from stream.
5145 my ( $in, # handle to in stream
5146 $out, # handle to out stream
5147 $options, # options hash
5152 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
5154 $options->{ "num" } ||= 10;
5156 $tmp_file = "$BP_TMP/random_records.tmp";
5158 $fh_out = Maasha::Common::write_open( $tmp_file );
5162 while ( $record = get_record( $in ) )
5164 put_record( $record, $fh_out );
5174 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
5176 while ( $i < $options->{ "num" } )
5178 $rand = int( rand( $count ) );
5180 if ( not exists $rand_hash{ $rand } )
5182 $rand_hash{ $rand } = 1;
5184 $max = $rand if $rand > $max;
5190 $fh_in = Maasha::Common::read_open( $tmp_file );
5194 while ( $record = get_record( $fh_in ) )
5196 put_record( $record, $out ) if exists $rand_hash{ $count };
5198 last if $count == $max;
5209 sub script_sort_records
5211 # Martin A. Hansen, August 2007.
5213 # Sort to sort records according to keys.
5215 my ( $in, # handle to in stream
5216 $out, # handle to out stream
5217 $options, # options hash
5222 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
5224 foreach $key ( @{ $options->{ "keys" } } )
5226 if ( $key =~ s/n$// ) {
5227 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
5229 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
5233 $sort_str = join " or ", @sort_cmd;
5234 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
5236 while ( $record = get_record( $in ) ) {
5237 push @records, $record;
5240 @records = sort $sort_sub @records;
5242 if ( $options->{ "reverse" } )
5244 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
5245 put_record( $records[ $i ], $out );
5250 for ( $i = 0; $i < scalar @records; $i++ ) {
5251 put_record( $records[ $i ], $out );
5257 sub script_count_vals
5259 # Martin A. Hansen, August 2007.
5261 # Count records in stream.
5263 my ( $in, # handle to in stream
5264 $out, # handle to out stream
5265 $options, # options hash
5270 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
5272 $tmp_file = "$BP_TMP/count_cache.tmp";
5274 $fh_out = Maasha::Common::write_open( $tmp_file );
5279 while ( $record = get_record( $in ) )
5281 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
5283 push @records, $record;
5285 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
5287 map { put_record( $_, $fh_out ) } @records;
5294 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5305 $fh_in = Maasha::Common::read_open( $tmp_file );
5307 while ( $record = get_record( $fh_in ) )
5309 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5311 put_record( $record, $out );
5313 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
5321 foreach $record ( @records )
5323 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
5325 put_record( $record, $out );
5332 sub script_plot_histogram
5334 # Martin A. Hansen, September 2007.
5336 # Plot a simple histogram for a given key using GNU plot.
5338 my ( $in, # handle to in stream
5339 $out, # handle to out stream
5340 $options, # options hash
5345 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5347 $options->{ "title" } ||= "Histogram";
5348 $options->{ "sort" } ||= "num";
5350 while ( $record = get_record( $in ) )
5352 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5354 put_record( $record, $out ) if not $options->{ "no_stream" };
5357 if ( $options->{ "sort" } eq "num" ) {
5358 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
5360 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
5363 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
5365 $fh = write_stream( $options->{ "data_out" } );
5367 print $fh "$_\n" foreach @{ $result };
5373 sub script_plot_lendist
5375 # Martin A. Hansen, August 2007.
5377 # Plot length distribution using GNU plot.
5379 my ( $in, # handle to in stream
5380 $out, # handle to out stream
5381 $options, # options hash
5386 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
5388 $options->{ "title" } ||= "Length Distribution";
5390 while ( $record = get_record( $in ) )
5392 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
5394 put_record( $record, $out ) if not $options->{ "no_stream" };
5397 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
5399 for ( $i = 0; $i < $max; $i++ ) {
5400 push @data_list, [ $i, $data_hash{ $i } || 0 ];
5403 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
5405 $fh = write_stream( $options->{ "data_out" } );
5407 print $fh "$_\n" foreach @{ $result };
5413 sub script_plot_chrdist
5415 # Martin A. Hansen, August 2007.
5417 # Plot chromosome distribution using GNU plot.
5419 my ( $in, # handle to in stream
5420 $out, # handle to out stream
5421 $options, # options hash
5426 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5428 $options->{ "title" } ||= "Chromosome Distribution";
5430 while ( $record = get_record( $in ) )
5432 if ( $record->{ "CHR" } ) { # generic
5433 $data_hash{ $record->{ "CHR" } }++;
5434 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5435 $data_hash{ $record->{ "S_ID" } }++;
5436 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5437 $data_hash{ $record->{ "S_ID" } }++;
5438 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5439 $data_hash{ $record->{ "S_ID" } }++;
5442 put_record( $record, $out ) if not $options->{ "no_stream" };
5445 foreach $elem ( keys %data_hash )
5449 $sort_key =~ s/chr//i;
5451 $sort_key =~ s/^X(.*)/99$1/;
5452 $sort_key =~ s/^Y(.*)/99$1/;
5453 $sort_key =~ s/^Z(.*)/999$1/;
5454 $sort_key =~ s/^M(.*)/9999$1/;
5455 $sort_key =~ s/^U(.*)/99999$1/;
5457 $count = $sort_key =~ tr/_//;
5459 $sort_key =~ s/_.*/"999999" x $count/ex;
5461 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5464 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5466 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
5468 $fh = write_stream( $options->{ "data_out" } );
5470 print $fh "$_\n" foreach @{ $result };
5476 sub script_plot_karyogram
5478 # Martin A. Hansen, August 2007.
5480 # Plot hits on karyogram.
5482 my ( $in, # handle to in stream
5483 $out, # handle to out stream
5484 $options, # options hash
5489 my ( %options, $record, @data, $fh, $result, %data_hash );
5491 $options->{ "genome" } ||= "human";
5492 $options->{ "feat_color" } ||= "black";
5494 while ( $record = get_record( $in ) )
5496 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5498 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5501 put_record( $record, $out ) if not $options->{ "no_stream" };
5504 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
5506 $fh = write_stream( $options->{ "data_out" } );
5514 sub script_plot_matches
5516 # Martin A. Hansen, August 2007.
5518 # Plot matches in 2D generating a dotplot.
5520 my ( $in, # handle to in stream
5521 $out, # handle to out stream
5522 $options, # options hash
5527 my ( $record, @data, $fh, $result, %data_hash );
5529 $options->{ "direction" } ||= "both";
5531 while ( $record = get_record( $in ) )
5533 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5534 push @data, $record;
5537 put_record( $record, $out ) if not $options->{ "no_stream" };
5540 $options->{ "title" } ||= "plot_matches";
5541 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5542 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5544 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5546 $fh = write_stream( $options->{ "data_out" } );
5548 print $fh "$_\n" foreach @{ $result };
5554 sub script_length_vals
5556 # Martin A. Hansen, August 2007.
5558 # Determine the length of the value for given keys.
5560 my ( $in, # handle to in stream
5561 $out, # handle to out stream
5562 $options, # options hash
5567 my ( $record, $key );
5569 while ( $record = get_record( $in ) )
5571 foreach $key ( @{ $options->{ "keys" } } )
5573 if ( $record->{ $key } ) {
5574 $record->{ $key . "_LEN" } = length $record->{ $key };
5578 put_record( $record, $out );
5585 # Martin A. Hansen, August 2007.
5587 # Calculates the sums for values of given keys.
5589 my ( $in, # handle to in stream
5590 $out, # handle to out stream
5591 $options, # options hash
5596 my ( $record, $key, %sum_hash, $fh );
5598 while ( $record = get_record( $in ) )
5600 foreach $key ( @{ $options->{ "keys" } } )
5602 if ( $record->{ $key } ) {
5603 $sum_hash{ $key } += $record->{ $key };
5607 put_record( $record, $out ) if not $options->{ "no_stream" };
5610 $fh = write_stream( $options->{ "data_out" } );
5612 foreach $key ( @{ $options->{ "keys" } } ) {
5613 put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5620 sub script_mean_vals
5622 # Martin A. Hansen, August 2007.
5624 # Calculate the mean of values of given keys.
5626 my ( $in, # handle to in stream
5627 $out, # handle to out stream
5628 $options, # options hash
5633 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5635 while ( $record = get_record( $in ) )
5637 foreach $key ( @{ $options->{ "keys" } } )
5639 if ( $record->{ $key } )
5641 $sum_hash{ $key } += $record->{ $key };
5642 $count_hash{ $key }++;
5646 put_record( $record, $out ) if not $options->{ "no_stream" };
5649 $fh = write_stream( $options->{ "data_out" } );
5651 foreach $key ( @{ $options->{ "keys" } } )
5653 if ( $count_hash{ $key } ) {
5654 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5659 put_record( { $key . "_MEAN" => $mean } , $fh );
5666 sub script_median_vals
5668 # Martin A. Hansen, March 2008.
5670 # Calculate the median values of given keys.
5672 my ( $in, # handle to in stream
5673 $out, # handle to out stream
5674 $options, # options hash
5679 my ( $record, $key, %median_hash, $median, $fh );
5681 while ( $record = get_record( $in ) )
5683 foreach $key ( @{ $options->{ "keys" } } ) {
5684 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5687 put_record( $record, $out ) if not $options->{ "no_stream" };
5690 $fh = write_stream( $options->{ "data_out" } );
5692 foreach $key ( @{ $options->{ "keys" } } )
5694 if ( $median_hash{ $key } ) {
5695 $median = Maasha::Calc::median( $median_hash{ $key } );
5700 put_record( { $key . "_MEDIAN" => $median } , $fh );
5709 # Martin A. Hansen, February 2008.
5711 # Determine the maximum values of given keys.
5713 my ( $in, # handle to in stream
5714 $out, # handle to out stream
5715 $options, # options hash
5720 my ( $record, $key, $fh, %max_hash, $max_record );
5722 while ( $record = get_record( $in ) )
5724 foreach $key ( @{ $options->{ "keys" } } )
5726 if ( $record->{ $key } )
5728 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5732 put_record( $record, $out ) if not $options->{ "no_stream" };
5735 $fh = write_stream( $options->{ "data_out" } );
5737 foreach $key ( @{ $options->{ "keys" } } )
5739 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5742 put_record( $max_record, $fh );
5750 # Martin A. Hansen, February 2008.
5752 # Determine the minimum values of given keys.
5754 my ( $in, # handle to in stream
5755 $out, # handle to out stream
5756 $options, # options hash
5761 my ( $record, $key, $fh, %min_hash, $min_record );
5763 while ( $record = get_record( $in ) )
5765 foreach $key ( @{ $options->{ "keys" } } )
5767 if ( defined $record->{ $key } )
5769 if ( exists $min_hash{ $key } ) {
5770 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5772 $min_hash{ $key } = $record->{ $key };
5777 put_record( $record, $out ) if not $options->{ "no_stream" };
5780 $fh = write_stream( $options->{ "data_out" } );
5782 foreach $key ( @{ $options->{ "keys" } } )
5784 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5787 put_record( $min_record, $fh );
5793 sub script_upload_to_ucsc
5795 # Martin A. Hansen, August 2007.
5797 # Calculate the mean of values of given keys.
5799 my ( $in, # handle to in stream
5800 $out, # handle to out stream
5801 $options, # options hash
5806 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5807 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5809 $options->{ "short_label" } ||= $options->{ 'table' };
5810 $options->{ "long_label" } ||= $options->{ 'table' };
5811 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5812 $options->{ "priority" } ||= 1;
5813 $options->{ "visibility" } ||= "pack";
5814 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5815 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5817 $file = "$BP_TMP/ucsc_upload.tmp";
5825 if ( $options->{ 'wiggle' } )
5827 $options->{ "visibility" } = "full";
5829 while ( $record = get_record( $in ) )
5831 put_record( $record, $out ) if not $options->{ "no_stream" };
5833 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5834 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5835 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5837 $fh_hash{ $record->{ "CHR" } } = Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5839 $fh_out = $fh_hash{ $record->{ "CHR" } };
5841 Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5844 map { close $_ } keys %fh_hash;
5846 $fh_out = Maasha::Common::write_open( $file );
5848 foreach $chr ( sort keys %fh_hash )
5850 Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5852 $fh_in = Maasha::Common::read_open( "$BP_TMP/$chr" );
5856 while ( $entry = Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5858 $chr = $entry->{ 'CHR' };
5859 $beg = $entry->{ 'CHR_BEG' };
5860 $end = $entry->{ 'CHR_END' };
5861 $q_id = $entry->{ 'Q_ID' };
5863 if ( $q_id =~ /_(\d+)$/ ) {
5873 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5878 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5879 $block->[ $i ] += $clones;
5882 $max = Maasha::Calc::max( $max, $end );
5891 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5892 $block->[ $i ] += $clones;
5899 Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5901 unlink "$BP_TMP/$chr";
5906 $wig_file = "$options->{ 'table' }.wig";
5907 $wib_file = "$options->{ 'table' }.wib";
5909 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5911 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5913 # Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5915 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5916 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5926 $fh_out = Maasha::Common::write_open( $file );
5928 while ( $record = get_record( $in ) )
5930 put_record( $record, $out ) if not $options->{ "no_stream" };
5932 if ( $record->{ "REC_TYPE" } eq "PSL" )
5934 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5935 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5939 $format = "PSL" if not $format;
5941 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5943 # chrom chromStart chromEnd name score strand size secStr conf
5945 print $fh_out join ( "\t",
5947 $record->{ "CHR_BEG" },
5948 $record->{ "CHR_END" } + 1,
5949 $record->{ "Q_ID" },
5950 $record->{ "SCORE" },
5951 $record->{ "STRAND" },
5952 $record->{ "SIZE" },
5953 $record->{ "SEC_STRUCT" },
5954 $record->{ "CONF" },
5957 $format = "BED_SS" if not $format;
5959 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5961 Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5963 $format = "BED" if not $format;
5964 $columns = $record->{ "BED_COLS" } if not $columns;
5966 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5968 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5970 $format = "BED" if not $format;
5971 $columns = 6 if not $columns;
5973 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5975 $record->{ "CHR" } = $record->{ "S_ID" };
5976 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5977 $record->{ "CHR_END" } = $record->{ "S_END" };
5978 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5980 $format = "BED" if not $format;
5981 $columns = 6 if not $columns;
5983 Maasha::UCSC::bed_put_entry( $record, $fh_out );
5985 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5987 $record->{ "CHR" } = $record->{ "S_ID" };
5988 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5989 $record->{ "CHR_END" } = $record->{ "S_END" };
5990 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5991 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5993 $format = "BED" if not $format;
5994 $columns = 6 if not $columns;
5996 Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5999 if ( $i == $options->{ "chunk_size" } )
6003 if ( $format eq "BED" ) {
6004 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6005 } elsif ( $format eq "PSL" ) {
6006 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6015 $fh_out = Maasha::Common::write_open( $file );
6024 if ( exists $options->{ "database" } and $options->{ "table" } )
6026 if ( $format eq "BED" )
6028 $type = "bed $columns";
6030 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6032 elsif ( $format eq "BED_SS" )
6034 $options->{ "sec_struct" } = 1;
6036 $type = "sec_struct";
6038 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
6040 elsif ( $format eq "PSL" )
6044 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
6046 elsif ( $format eq "WIGGLE" )
6050 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
6055 Maasha::UCSC::update_my_tracks( $options, $type );
6060 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
6065 # Martin A. Hansen, July 2008.
6067 # Given a biopiece record converts it to a FASTA record.
6068 # If no generic SEQ or SEQ_NAME is found, the Q_* and S_* are
6069 # tried in that order.
6071 my ( $record, # record
6076 my ( $seq_name, $seq );
6078 $seq_name = $record->{ "SEQ_NAME" } || $record->{ "Q_ID" } || $record->{ "S_ID" };
6079 $seq = $record->{ "SEQ" } || $record->{ "Q_SEQ" } || $record->{ "S_SEQ" };
6081 if ( defined $seq_name and defined $seq ) {
6082 return wantarray ? ( $seq_name, $seq ) : [ $seq_name, $seq ];
6091 # Martin A. Hansen, July 2007.
6093 # Opens a stream to STDIN or a file,
6095 my ( $path, # path - OPTIONAL
6098 # Returns filehandle.
6102 if ( not -t STDIN ) {
6103 $fh = Maasha::Common::read_stdin();
6104 } elsif ( not $path ) {
6105 # Maasha::Common::error( qq(no data stream) );
6107 $fh = Maasha::Common::read_open( $path );
6110 # $fh->autoflush(1) if $fh; # Disable file buffer for debugging.
6118 # Martin A. Hansen, August 2007.
6120 # Opens a stream to STDOUT or a file.
6122 my ( $path, # path - OPTIONAL
6123 $gzip, # compress data - OPTIONAL
6126 # Returns filehandle.
6131 $fh = Maasha::Common::write_open( $path, $gzip );
6133 $fh = Maasha::Common::write_stdout();
6142 # Martin A. Hansen, July 2007.
6144 # Reads one record at a time and converts that record
6145 # to a Perl data structure (a hash) which is returned.
6147 my ( $fh, # handle to stream
6152 my ( $block, @lines, $line, $key, $value, %record );
6154 local $/ = "\n---\n";
6160 return if not defined $block;
6162 @lines = split "\n", $block;
6164 foreach $line ( @lines )
6166 ( $key, $value ) = split ": ", $line, 2;
6168 $record{ $key } = $value;
6171 return wantarray ? %record : \%record;
6177 # Martin A. Hansen, July 2007.
6179 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
6181 my ( $data, # data structure
6182 $fh, # file handle - OPTIONAL
6187 if ( scalar keys %{ $data } )
6191 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
6196 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
6207 # Martin A. Hansen, November 2007.
6209 # Extracts files from an explicit GetOpt::Long argument
6210 # allowing for the use of glob. E.g.
6211 # --data_in=test.fna
6212 # --data_in=test.fna,test2.fna
6214 # --data_in=test.fna,/dir/*.fna
6216 my ( $option, # option from GetOpt::Long
6221 my ( $elem, @files );
6223 foreach $elem ( split ",", $option )
6227 } elsif ( $elem =~ /\*/ ) {
6228 push @files, glob( $elem );
6232 return wantarray ? @files : \@files;
6238 # Martin A. Hansen, April 2008.
6240 # Removes temporary directory and exits gracefully.
6241 # This subroutine is meant to be run always as the last
6242 # thing even if a script is dies or is interrupted
6245 my ( $sig, # signal from the %SIG
6248 # print STDERR "signal->$sig<-\n";
6256 if ( $sig =~ /MAASHA_ERROR/ ) {
6257 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
6258 } elsif ( $sig eq "INT" ) {
6259 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
6260 } elsif ( $sig eq "TERM" ) {
6261 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
6263 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
6275 # Martin A. Hansen, July 2008.
6277 # Cleans out any unused temporary files and direcotries in BP_TMP.
6281 my ( $tmpdir, @dirs, $curr_pid, $dir, $user, $sid, $pid );
6283 $tmpdir = $ENV{ 'BP_TMP' } || Maasha::Common::error( 'No BP_TMP variable in environment.' );
6285 $curr_pid = Maasha::Common::get_processid();
6287 @dirs = Maasha::Common::ls_dirs( $tmpdir );
6289 foreach $dir ( @dirs )
6291 if ( $dir =~ /^$tmpdir\/(.+)_(\d+)_(\d+)_bp_tmp$/ )
6297 if ( $user eq Maasha::Common::get_user() )
6299 if ( not Maasha::Common::process_running( $pid ) )
6301 # print STDERR "Removing stale dir: $dir\n";
6302 Maasha::Common::dir_remove( $dir );
6304 elsif ( $pid == $curr_pid )
6306 # print STDERR "Removing current dir: $dir\n";
6307 Maasha::Common::dir_remove( $dir );
6321 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<