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 biotool records.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
44 use Maasha::Stockholm;
56 use vars qw( @ISA @EXPORT_OK );
60 @ISA = qw( Exporter );
75 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SIGNAL HANDLER <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
78 $SIG{ '__DIE__' } = \&sig_handler;
79 $SIG{ 'INT' } = \&sig_handler;
80 $SIG{ 'TERM' } = \&sig_handler;
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
86 my ( $script, $BP_TMP );
88 $script = &Maasha::Common::get_scriptname();
89 $BP_TMP = &Maasha::Common::get_tmpdir();
92 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOG <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
95 my $log_fh = &Maasha::Common::append_open( $ENV{ "LOG_DIR" } . "/biopieces.log" );
97 $log_fh->autoflush( 1 );
99 &log( $log_fh, $script, \@ARGV );
104 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
107 my $t0 = gettimeofday();
109 &run_script( $script );
111 my $t1 = gettimeofday();
113 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) );
116 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
121 # Martin A. Hansen, January 2008.
123 # Log messages to logfile.
125 my ( $fh, # filehandle to logfile
126 $script, # script name
127 $argv, # reference to @ARGV
132 my ( $time_stamp, $user );
134 $time_stamp = &Maasha::Common::time_stamp();
136 $user = $ENV{ "USER" };
138 $script = "biopieces" if $script eq "-e";
140 print $fh "$time_stamp\t$user\t$script ", join( " ", @{ $argv } ), "\n";
146 # Martin A. Hansen, August 2007.
148 # Run a specific script.
150 my ( $script, # script name
155 my ( $options, $in, $out );
157 &script_list_biopieces( $ENV{ 'INST_DIR'} . "/biopieces/usage/" ) if $script eq "list_biopieces";
159 &script_print_usage( $ENV{ 'INST_DIR'} . "/biopieces/usage/$script" ) if -t STDIN and not @ARGV;
161 $options = &get_options( $script );
163 $in = &read_stream( $options->{ "stream_in" } );
164 $out = &write_stream( $options->{ "stream_out" } );
166 if ( $script eq "read_fasta" ) { &script_read_fasta( $in, $out, $options ) }
167 elsif ( $script eq "read_align" ) { &script_read_align( $in, $out, $options ) }
168 elsif ( $script eq "read_tab" ) { &script_read_tab( $in, $out, $options ) }
169 elsif ( $script eq "read_psl" ) { &script_read_psl( $in, $out, $options ) }
170 elsif ( $script eq "read_bed" ) { &script_read_bed( $in, $out, $options ) }
171 elsif ( $script eq "read_blast_tab" ) { &script_read_blast_tab( $in, $out, $options ) }
172 elsif ( $script eq "read_embl" ) { &script_read_embl( $in, $out, $options ) }
173 elsif ( $script eq "read_stockholm" ) { &script_read_stockholm( $in, $out, $options ) }
174 elsif ( $script eq "read_phastcons" ) { &script_read_phastcons( $in, $out, $options ) }
175 elsif ( $script eq "read_soft" ) { &script_read_soft( $in, $out, $options ) }
176 elsif ( $script eq "read_gff" ) { &script_read_gff( $in, $out, $options ) }
177 elsif ( $script eq "read_2bit" ) { &script_read_2bit( $in, $out, $options ) }
178 elsif ( $script eq "read_solexa" ) { &script_read_solexa( $in, $out, $options ) }
179 elsif ( $script eq "read_solid" ) { &script_read_solid( $in, $out, $options ) }
180 elsif ( $script eq "read_mysql" ) { &script_read_mysql( $in, $out, $options ) }
181 elsif ( $script eq "count_seq" ) { &script_count_seq( $in, $out, $options ) }
182 elsif ( $script eq "length_seq" ) { &script_length_seq( $in, $out, $options ) }
183 elsif ( $script eq "uppercase_seq" ) { &script_uppercase_seq( $in, $out, $options ) }
184 elsif ( $script eq "shuffle_seq" ) { &script_shuffle_seq( $in, $out, $options ) }
185 elsif ( $script eq "analyze_seq" ) { &script_analyze_seq( $in, $out, $options ) }
186 elsif ( $script eq "analyze_tags" ) { &script_analyze_tags( $in, $out, $options ) }
187 elsif ( $script eq "complexity_seq" ) { &script_complexity_seq( $in, $out, $options ) }
188 elsif ( $script eq "oligo_freq" ) { &script_oligo_freq( $in, $out, $options ) }
189 elsif ( $script eq "create_weight_matrix" ) { &script_create_weight_matrix( $in, $out, $options ) }
190 elsif ( $script eq "calc_bit_scores" ) { &script_calc_bit_scores( $in, $out, $options ) }
191 elsif ( $script eq "reverse_seq" ) { &script_reverse_seq( $in, $out, $options ) }
192 elsif ( $script eq "complement_seq" ) { &script_complement_seq( $in, $out, $options ) }
193 elsif ( $script eq "remove_indels" ) { &script_remove_indels( $in, $out, $options ) }
194 elsif ( $script eq "transliterate_seq" ) { &script_transliterate_seq( $in, $out, $options ) }
195 elsif ( $script eq "transliterate_vals" ) { &script_transliterate_vals( $in, $out, $options ) }
196 elsif ( $script eq "translate_seq" ) { &script_translate_seq( $in, $out, $options ) }
197 elsif ( $script eq "extract_seq" ) { &script_extract_seq( $in, $out, $options ) }
198 elsif ( $script eq "get_genome_seq" ) { &script_get_genome_seq( $in, $out, $options ) }
199 elsif ( $script eq "get_genome_align" ) { &script_get_genome_align( $in, $out, $options ) }
200 elsif ( $script eq "get_genome_phastcons" ) { &script_get_genome_phastcons( $in, $out, $options ) }
201 elsif ( $script eq "fold_seq" ) { &script_fold_seq( $in, $out, $options ) }
202 elsif ( $script eq "split_seq" ) { &script_split_seq( $in, $out, $options ) }
203 elsif ( $script eq "split_bed" ) { &script_split_bed( $in, $out, $options ) }
204 elsif ( $script eq "align_seq" ) { &script_align_seq( $in, $out, $options ) }
205 elsif ( $script eq "tile_seq" ) { &script_tile_seq( $in, $out, $options ) }
206 elsif ( $script eq "invert_align" ) { &script_invert_align( $in, $out, $options ) }
207 elsif ( $script eq "patscan_seq" ) { &script_patscan_seq( $in, $out, $options ) }
208 elsif ( $script eq "create_blast_db" ) { &script_create_blast_db( $in, $out, $options ) }
209 elsif ( $script eq "blast_seq" ) { &script_blast_seq( $in, $out, $options ) }
210 elsif ( $script eq "blat_seq" ) { &script_blat_seq( $in, $out, $options ) }
211 elsif ( $script eq "match_seq" ) { &script_match_seq( $in, $out, $options ) }
212 elsif ( $script eq "create_vmatch_index" ) { &script_create_vmatch_index( $in, $out, $options ) }
213 elsif ( $script eq "vmatch_seq" ) { &script_vmatch_seq( $in, $out, $options ) }
214 elsif ( $script eq "write_fasta" ) { &script_write_fasta( $in, $out, $options, $options ) }
215 elsif ( $script eq "write_align" ) { &script_write_align( $in, $out, $options ) }
216 elsif ( $script eq "write_blast" ) { &script_write_blast( $in, $out, $options ) }
217 elsif ( $script eq "write_tab" ) { &script_write_tab( $in, $out, $options ) }
218 elsif ( $script eq "write_bed" ) { &script_write_bed( $in, $out, $options ) }
219 elsif ( $script eq "write_psl" ) { &script_write_psl( $in, $out, $options ) }
220 elsif ( $script eq "write_2bit" ) { &script_write_2bit( $in, $out, $options, $options ) }
221 elsif ( $script eq "write_solid" ) { &script_write_solid( $in, $out, $options, $options ) }
222 elsif ( $script eq "head_records" ) { &script_head_records( $in, $out, $options ) }
223 elsif ( $script eq "remove_keys" ) { &script_remove_keys( $in, $out, $options ) }
224 elsif ( $script eq "rename_keys" ) { &script_rename_keys( $in, $out, $options ) }
225 elsif ( $script eq "uniq_vals" ) { &script_uniq_vals( $in, $out, $options ) }
226 elsif ( $script eq "merge_vals" ) { &script_merge_vals( $in, $out, $options ) }
227 elsif ( $script eq "grab" ) { &script_grab( $in, $out, $options ) }
228 elsif ( $script eq "compute" ) { &script_compute( $in, $out, $options ) }
229 elsif ( $script eq "flip_tab" ) { &script_flip_tab( $in, $out, $options ) }
230 elsif ( $script eq "add_ident" ) { &script_add_ident( $in, $out, $options ) }
231 elsif ( $script eq "count_records" ) { &script_count_records( $in, $out, $options ) }
232 elsif ( $script eq "random_records" ) { &script_random_records( $in, $out, $options ) }
233 elsif ( $script eq "sort_records" ) { &script_sort_records( $in, $out, $options ) }
234 elsif ( $script eq "count_vals" ) { &script_count_vals( $in, $out, $options ) }
235 elsif ( $script eq "plot_histogram" ) { &script_plot_histogram( $in, $out, $options ) }
236 elsif ( $script eq "plot_lendist" ) { &script_plot_lendist( $in, $out, $options ) }
237 elsif ( $script eq "plot_chrdist" ) { &script_plot_chrdist( $in, $out, $options ) }
238 elsif ( $script eq "plot_karyogram" ) { &script_plot_karyogram( $in, $out, $options ) }
239 elsif ( $script eq "plot_matches" ) { &script_plot_matches( $in, $out, $options ) }
240 elsif ( $script eq "plot_seqlogo" ) { &script_plot_seqlogo( $in, $out, $options ) }
241 elsif ( $script eq "plot_phastcons_profiles" ) { &script_plot_phastcons_profiles( $in, $out, $options ) }
242 elsif ( $script eq "analyze_bed" ) { &script_analyze_bed( $in, $out, $options ) }
243 elsif ( $script eq "analyze_vals" ) { &script_analyze_vals( $in, $out, $options ) }
244 elsif ( $script eq "length_vals" ) { &script_length_vals( $in, $out, $options ) }
245 elsif ( $script eq "sum_vals" ) { &script_sum_vals( $in, $out, $options ) }
246 elsif ( $script eq "mean_vals" ) { &script_mean_vals( $in, $out, $options ) }
247 elsif ( $script eq "median_vals" ) { &script_median_vals( $in, $out, $options ) }
248 elsif ( $script eq "max_vals" ) { &script_max_vals( $in, $out, $options ) }
249 elsif ( $script eq "min_vals" ) { &script_min_vals( $in, $out, $options ) }
250 elsif ( $script eq "upload_to_ucsc" ) { &script_upload_to_ucsc( $in, $out, $options ) }
252 close $in if defined $in;
255 # unset status - missing
256 # write log file - missing
262 # Martin A. Hansen, February 2008.
264 # Gets options from commandline and checks these vigerously.
266 my ( $script, # name of script
271 my ( %options, @options, $opt, @genomes );
273 if ( $script eq "read_fasta" )
280 elsif ( $script eq "read_align" )
287 elsif ( $script eq "read_tab" )
298 elsif ( $script eq "read_psl" )
305 elsif ( $script eq "read_bed" )
312 elsif ( $script eq "read_blast_tab" )
319 elsif ( $script eq "read_embl" )
329 elsif ( $script eq "read_stockholm" )
336 elsif ( $script eq "read_phastcons" )
347 elsif ( $script eq "read_soft" )
354 elsif ( $script eq "read_gff" )
361 elsif ( $script eq "read_2bit" )
369 elsif ( $script eq "read_solexa" )
377 elsif ( $script eq "read_solid" )
385 elsif ( $script eq "read_mysql" )
394 elsif ( $script eq "count_seq" )
401 elsif ( $script eq "length_seq" )
408 elsif ( $script eq "oligo_freq" )
415 elsif ( $script eq "create_weight_matrix" )
421 elsif ( $script eq "transliterate_seq" )
429 elsif ( $script eq "transliterate_vals" )
438 elsif ( $script eq "translate_seq" )
444 elsif ( $script eq "extract_seq" )
452 elsif ( $script eq "get_genome_seq" )
464 elsif ( $script eq "get_genome_align" )
475 elsif ( $script eq "get_genome_phastcons" )
486 elsif ( $script eq "split_seq" )
493 elsif ( $script eq "split_bed" )
500 elsif ( $script eq "tile_seq" )
507 elsif ( $script eq "invert_align" )
513 elsif ( $script eq "patscan_seq" )
524 elsif ( $script eq "create_blast_db" )
531 elsif ( $script eq "blast_seq" )
543 elsif ( $script eq "blat_seq" )
555 elsif ( $script eq "match_seq" )
562 elsif ( $script eq "create_vmatch_index" )
570 elsif ( $script eq "vmatch_seq" )
581 elsif ( $script eq "write_fasta" )
590 elsif ( $script eq "write_align" )
600 elsif ( $script eq "write_blast" )
609 elsif ( $script eq "write_tab" )
621 elsif ( $script eq "write_bed" )
629 elsif ( $script eq "write_psl" )
637 elsif ( $script eq "write_2bit" )
645 elsif ( $script eq "write_solid" )
654 elsif ( $script eq "plot_seqlogo" )
661 elsif ( $script eq "plot_phastcons_profiles" )
676 elsif ( $script eq "analyze_vals" )
683 elsif ( $script eq "head_records" )
689 elsif ( $script eq "remove_keys" )
696 elsif ( $script eq "rename_keys" )
702 elsif ( $script eq "uniq_vals" )
709 elsif ( $script eq "merge_vals" )
716 elsif ( $script eq "grab" )
731 elsif ( $script eq "compute" )
737 elsif ( $script eq "add_ident" )
744 elsif ( $script eq "count_records" )
751 elsif ( $script eq "random_records" )
757 elsif ( $script eq "sort_records" )
764 elsif ( $script eq "count_vals" )
770 elsif ( $script eq "plot_histogram" )
783 elsif ( $script eq "plot_lendist" )
795 elsif ( $script eq "plot_chrdist" )
806 elsif ( $script eq "plot_karyogram" )
815 elsif ( $script eq "plot_matches" )
827 elsif ( $script eq "length_vals" )
833 elsif ( $script eq "sum_vals" )
841 elsif ( $script eq "mean_vals" )
849 elsif ( $script eq "median_vals" )
857 elsif ( $script eq "max_vals" )
865 elsif ( $script eq "min_vals" )
873 elsif ( $script eq "upload_to_ucsc" )
897 # print STDERR Dumper( \@options );
904 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
905 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
906 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
907 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
908 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
909 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
910 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
912 # ---- check arguments ----
914 if ( $options{ 'data_in' } )
916 $options{ "files" } = &getopt_files( $options{ 'data_in' } );
918 &Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
921 map { &Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
923 # print STDERR Dumper( \%options );
925 foreach $opt ( keys %options )
927 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
929 &Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
931 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+$/ )
933 &Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
935 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
937 &Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
939 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
941 &Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
943 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
945 &Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
947 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
949 &Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
951 elsif ( $opt eq "genome" )
953 @genomes = &Maasha::Config::genomes();
955 if ( not grep $options{ $opt }, @genomes ) {
956 &Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'INST_DIR' }/conf/genomes.conf") );
959 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb)/ )
961 &Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
963 elsif ( $opt eq "table" and $options{ $opt } =~ /-\./ )
965 &Maasha::Common::error( qq(Character '$options{ $opt }' is not allowed in table names) );
969 &Maasha::Common::error( qq(no --database specified) ) if $script eq "create_blast_db" and not $options{ "database" };
970 &Maasha::Common::error( qq(no --index_name specified) ) if $script eq "create_vmatch_index" and not $options{ "index_name" };
971 &Maasha::Common::error( qq(no --database or --genome specified) ) if $script eq "blast_seq" and not $options{ "genome" } and not $options{ "database" };
972 &Maasha::Common::error( qq(both --database and --genome specified) ) if $script eq "blast_seq" and $options{ "genome" } and $options{ "database" };
973 &Maasha::Common::error( qq(no --index_name or --genome specified) ) if $script eq "vmatch_seq" and not $options{ "genome" } and not $options{ "index_name" };
974 &Maasha::Common::error( qq(both --index and --genome specified) ) if $script eq "vmatch_seq" and $options{ "genome" } and $options{ "index" };
975 &Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_seq|get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
976 &Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
977 &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" };
979 if ( $script eq "upload_to_ucsc" )
981 &Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
982 &Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
985 return wantarray ? %options : \%options;
989 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
992 sub script_print_usage
994 # Martin A. Hansen, January 2008.
996 # Retrieves usage information from file and
997 # prints this nicely formatted.
999 my ( $path, # full path to usage file
1004 my ( $script, $fh, $line, @lines, @list, %hash, $key );
1006 $script = ( split "/", $path )[ -1 ];
1008 $fh = &Maasha::Common::read_open( $path );
1010 push @list, "Program name";
1012 $hash{ "Program name" } = [ $script ];
1014 while ( $line = <$fh> )
1018 $line =~ s/\$script/$script/g;
1020 if ( $line =~ /^([^:]+):\s+(.+)$/ )
1022 push @list, $1 if not exists $hash{ $1 };
1023 push @{ $hash{ $1 } }, $2;
1031 foreach $key ( @list )
1033 if ( scalar @{ $hash{ $key } } == 1 )
1035 @lines = &Maasha::Common::wrap_line( $hash{ $key }->[ 0 ], 80 );
1037 printf( "%-15s%s\n", "$key:", shift @lines );
1039 map { printf( "%-15s%s\n", "", $_ ) } @lines;
1047 map { print " $_\n" } @{ $hash{ $key } };
1057 sub script_list_biopieces
1059 # Martin A. Hansen, January 2008.
1061 # Prints the description from the usage for each of the biopieces.
1063 my ( $path, # full path to usage directory
1068 my ( @files, $file, $fh, $line, @lines, $program );
1070 @files = &Maasha::Common::ls_files( $path );
1072 foreach $file ( sort @files )
1074 $program = ( split "/", $file )[ -1 ];
1076 $fh = &Maasha::Common::read_open( $file );
1078 while ( $line = <$fh> )
1082 if ( $line =~ /^Description:\s+(.+)/ )
1084 @lines = &Maasha::Common::wrap_line( $1, 60 );
1086 printf( "%-30s%s\n", $program, shift @lines );
1088 map { printf( "%-30s%s\n", "", $_ ) } @lines;
1099 sub script_read_fasta
1101 # Martin A. Hansen, August 2007.
1103 # Read sequences from FASTA file.
1105 my ( $in, # handle to in stream
1106 $out, # handle to out stream
1107 $options, # options hash
1112 my ( $record, $file, $data_in, $entry, $num );
1114 while ( $record = &get_record( $in ) ) {
1115 &put_record( $record, $out );
1120 foreach $file ( @{ $options->{ "files" } } )
1122 $data_in = &Maasha::Common::read_open( $file );
1124 while ( $entry = &Maasha::Fasta::get_entry( $data_in ) )
1126 if ( defined $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1129 SEQ_NAME => $entry->[ SEQ_NAME ],
1130 SEQ => $entry->[ SEQ ],
1131 SEQ_LEN => length $entry->[ SEQ ],
1134 &put_record( $record, $out );
1137 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1147 close $data_in if $data_in;
1151 sub script_read_align
1153 # Martin A. Hansen, August 2007.
1155 # Read aligned sequences from FASTA file.
1157 my ( $in, # handle to in stream
1158 $out, # handle to out stream
1159 $options, # options hash
1164 my ( $entry, $record, $file, $data_in, $num );
1166 while ( $record = &get_record( $in ) ) {
1167 &put_record( $record, $out );
1172 foreach $file ( @{ $options->{ "files" } } )
1174 $data_in = &Maasha::Common::read_open( $file );
1176 while ( $entry = &Maasha::Fasta::get_entry( $data_in ) )
1178 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
1182 SEQ_NAME => $entry->[ SEQ_NAME ],
1183 SEQ => $entry->[ SEQ ],
1184 ALIGN_LEN => length $entry->[ SEQ ],
1187 &put_record( $record, $out );
1190 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1200 close $data_in if $data_in;
1206 # Martin A. Hansen, August 2007.
1208 # Read table or table columns from stream or file.
1210 my ( $in, # handle to in stream
1211 $out, # handle to out stream
1212 $options, # options hash
1217 my ( $file, $line, @fields, @fields2, $i, $record, $data_in, $skip, $num );
1219 $options->{ 'delimit' } ||= '\s+';
1221 while ( $record = &get_record( $in ) ) {
1222 &put_record( $record, $out );
1225 $skip = $options->{ 'skip' } ||= 0;
1228 foreach $file ( @{ $options->{ "files" } } )
1230 $data_in = &Maasha::Common::read_open( $file );
1232 while ( $line = <$data_in> )
1240 next if $line =~ /^#|^$/;
1247 @fields = split /$options->{'delimit'}/, $line;
1249 if ( $options->{ "cols" } ) {
1250 map { push @fields2, $fields[ $_ ] } @{ $options->{ "cols" } };
1255 for ( $i = 0; $i < @fields2; $i++ )
1257 if ( $options->{ "keys" }->[ $i ] ) {
1258 $record->{ $options->{ "keys" }->[ $i ] } = $fields2[ $i ];
1260 $record->{ "V" . $i } = $fields2[ $i ];
1264 &put_record( $record, $out );
1266 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1276 close $data_in if $data_in;
1282 # Martin A. Hansen, August 2007.
1284 # Read psl table from stream or file.
1286 my ( $in, # handle to in stream
1287 $out, # handle to out stream
1288 $options, # options hash
1293 my ( $record, @files, $file, $entries, $entry, $num );
1295 while ( $record = &get_record( $in ) ) {
1296 &put_record( $record, $out );
1301 foreach $file ( @{ $options->{ "files" } } )
1303 $entries = &Maasha::UCSC::psl_get_entries( $file );
1305 foreach $entry ( @{ $entries } )
1307 &put_record( $entry, $out );
1309 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1321 # Martin A. Hansen, August 2007.
1323 # Read bed table from stream or file.
1325 my ( $in, # handle to in stream
1326 $out, # handle to out stream
1327 $options, # options hash
1332 my ( $file, $record, $entry, $data_in, $num );
1334 while ( $record = &get_record( $in ) ) {
1335 &put_record( $record, $out );
1340 foreach $file ( @{ $options->{ "files" } } )
1342 $data_in = &Maasha::Common::read_open( $file );
1344 while ( $entry = &Maasha::UCSC::bed_get_entry( $data_in ) )
1346 &put_record( $entry, $out );
1348 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1358 close $data_in if $data_in;
1362 sub script_read_blast_tab
1364 # Martin A. Hansen, September 2007.
1366 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1368 my ( $in, # handle to in stream
1369 $out, # handle to out stream
1370 $options, # options hash
1375 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1377 while ( $record = &get_record( $in ) ) {
1378 &put_record( $record, $out );
1383 foreach $file ( @{ $options->{ "files" } } )
1385 $data_in = &Maasha::Common::read_open( $file );
1387 while ( $line = <$data_in> )
1391 next if $line =~ /^#/;
1393 @fields = split /\t/, $line;
1395 $record->{ "REC_TYPE" } = "BLAST";
1396 $record->{ "Q_ID" } = $fields[ 0 ];
1397 $record->{ "S_ID" } = $fields[ 1 ];
1398 $record->{ "IDENT" } = $fields[ 2 ];
1399 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1400 $record->{ "MISMATCHES" } = $fields[ 4 ];
1401 $record->{ "GAPS" } = $fields[ 5 ];
1402 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1403 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1404 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1405 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1406 $record->{ "E_VAL" } = $fields[ 10 ];
1407 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1409 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1411 $record->{ "STRAND" } = '-';
1413 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1417 $record->{ "STRAND" } = '+';
1420 &put_record( $record, $out );
1422 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1432 close $data_in if $data_in;
1436 sub script_read_embl
1438 # Martin A. Hansen, August 2007.
1442 my ( $in, # handle to in stream
1443 $out, # handle to out stream
1444 $options, # options hash
1449 my ( %options2, $file, $data_in, $num, $entry, $record );
1451 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1452 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1453 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1455 while ( $record = &get_record( $in ) ) {
1456 &put_record( $record, $out );
1461 foreach $file ( @{ $options->{ "files" } } )
1463 $data_in = &Maasha::Common::read_open( $file );
1465 while ( $entry = &Maasha::EMBL::get_embl_entry( $data_in ) )
1467 $record = &Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1469 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1471 $record_copy = dclone $record;
1473 delete $record_copy->{ "FT" };
1475 &put_record( $record_copy, $out );
1477 delete $record_copy->{ "SEQ" };
1479 foreach $feat ( keys %{ $record->{ "FT" } } )
1481 $record_copy->{ "FEAT_TYPE" } = $feat;
1483 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1485 foreach $qual ( keys %{ $feat2 } )
1487 $qual_val = join "; ", @{ $feat2->{ $qual } };
1492 $record_copy->{ $qual } = $qual_val;
1495 &put_record( $record_copy, $out );
1499 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1509 close $data_in if $data_in;
1513 sub script_read_stockholm
1515 # Martin A. Hansen, August 2007.
1517 # Read Stockholm format.
1519 my ( $in, # handle to in stream
1520 $out, # handle to out stream
1521 $options, # options hash
1526 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1528 while ( $record = &get_record( $in ) ) {
1529 &put_record( $record, $out );
1534 foreach $file ( @{ $options->{ "files" } } )
1536 $data_in = &Maasha::Common::read_open( $file );
1538 while ( $entry = &Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1540 $record = &Maasha::Stockholm::parse_stockholm_entry( $entry );
1544 foreach $key ( keys %{ $record->{ "GF" } } ) {
1545 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1548 $record_anno->{ "ALIGN" } = $num;
1550 &put_record( $record_anno, $out );
1552 foreach $seq ( @{ $record->{ "ALIGN" } } )
1554 undef $record_align;
1558 SEQ_NAME => $seq->[ 0 ],
1562 &put_record( $record_align, $out );
1565 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1575 close $data_in if $data_in;
1579 sub script_read_phastcons
1581 # Martin A. Hansen, December 2007.
1583 # Read PhastCons format.
1585 my ( $in, # handle to in stream
1586 $out, # handle to out stream
1587 $options, # options hash
1592 my ( $data_in, $file, $num, $entry, @records, $record );
1594 $options->{ "min" } ||= 10;
1595 $options->{ "dist" } ||= 25;
1596 $options->{ "threshold" } ||= 0.8;
1597 $options->{ "gap" } ||= 5;
1599 while ( $record = &get_record( $in ) ) {
1600 &put_record( $record, $out );
1605 foreach $file ( @{ $options->{ "files" } } )
1607 $data_in = &Maasha::Common::read_open( $file );
1609 while ( $entry = &Maasha::UCSC::phastcons_get_entry( $data_in ) )
1611 @records = &Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1613 foreach $record ( @records )
1615 $record->{ "REC_TYPE" } = "BED";
1616 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1618 &put_record( $record, $out );
1620 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1631 close $data_in if $data_in;
1635 sub script_read_soft
1637 # Martin A. Hansen, December 2007.
1640 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1642 my ( $in, # handle to in stream
1643 $out, # handle to out stream
1644 $options, # options hash
1649 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end );
1651 while ( $record = &get_record( $in ) ) {
1652 &put_record( $record, $out );
1657 foreach $file ( @{ $options->{ "files" } } )
1659 $soft_index = &Maasha::NCBI::soft_index_file( $file );
1661 $fh = &Maasha::Common::read_open( $file );
1663 @platforms = grep { $_->[ 0 ] =~ /PLATFORM/ } @{ $soft_index };
1665 $plat_table = &Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->[ 1 ], $platforms[ -1 ]->[ 2 ] );
1667 @samples = grep { $_->[ 0 ] =~ /SAMPLE/ } @{ $soft_index };
1669 $old_end = $platforms[ -1 ]->[ 2 ];
1671 foreach $sample ( @samples )
1673 $records = &Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->[ 1 ] - $old_end - 1, $sample->[ 2 ] - $old_end - 1 );
1675 foreach $record ( @{ $records } )
1677 &put_record( $record, $out );
1679 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1684 $old_end = $sample->[ 2 ];
1692 close $data_in if $data_in;
1699 # Martin A. Hansen, February 2008.
1702 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1704 my ( $in, # handle to in stream
1705 $out, # handle to out stream
1706 $options, # options hash
1711 my ( $data_in, $file, $fh, $num, $record, $entry );
1713 while ( $record = &get_record( $in ) ) {
1714 &put_record( $record, $out );
1719 foreach $file ( @{ $options->{ "files" } } )
1721 $fh = &Maasha::Common::read_open( $file );
1723 while ( $entry = &Maasha::GFF::get_entry( $fh ) )
1725 &put_record( $entry, $out );
1727 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1737 close $data_in if $data_in;
1741 sub script_read_2bit
1743 # Martin A. Hansen, March 2008.
1745 # Read sequences from 2bit file.
1747 my ( $in, # handle to in stream
1748 $out, # handle to out stream
1749 $options, # options hash
1754 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1756 $mask = 1 if not $options->{ "no_mask" };
1758 while ( $record = &get_record( $in ) ) {
1759 &put_record( $record, $out );
1764 foreach $file ( @{ $options->{ "files" } } )
1766 $data_in = &Maasha::Common::read_open( $file );
1768 $toc = &Maasha::TwoBit::twobit_get_TOC( $data_in );
1770 foreach $line ( @{ $toc } )
1772 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1773 $record->{ "SEQ" } = &Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1774 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1776 &put_record( $record, $out );
1778 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1788 close $data_in if $data_in;
1792 sub script_read_solexa
1794 # Martin A. Hansen, March 2008.
1796 # Read Solexa sequence reads from file.
1798 my ( $in, # handle to in stream
1799 $out, # handle to out stream
1800 $options, # options hash
1805 my ( $record, $file, $base_name, $data_in, $line, $num, @fields, @seqs, @scores, $i, $seq, $seq_count );
1807 $options->{ "quality" } ||= 20;
1809 while ( $record = &get_record( $in ) ) {
1810 &put_record( $record, $out );
1815 foreach $file ( @{ $options->{ "files" } } )
1817 $data_in = &Maasha::Common::read_open( $file );
1818 $base_name = &Maasha::Common::get_basename( $file );
1819 $base_name =~ s/\..*//;
1823 while ( $line = <$data_in> )
1825 @fields = split /:/, $line;
1826 @seqs = split //, $fields[ 5 ];
1827 @scores = split / /, $fields[ -1 ];
1829 for ( $i = 0; $i < @scores; $i++ ) {
1830 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1833 $seq = join "", @seqs;
1835 $record->{ "SEQ_NAME" } = sprintf( "%s_ID%08d", $base_name, $seq_count );
1836 $record->{ "SEQ" } = $seq;
1837 $record->{ "SEQ_LEN" } = length $seq;
1838 $record->{ "SCORE_MEAN" } = sprintf ( "%.2f", &Maasha::Calc::mean( \@scores ) );
1840 &put_record( $record, $out );
1842 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1853 close $data_in if $data_in;
1857 sub script_read_solid
1859 # Martin A. Hansen, April 2008.
1861 # Read Solid sequence from file.
1863 my ( $in, # handle to in stream
1864 $out, # handle to out stream
1865 $options, # options hash
1870 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1872 $options->{ "quality" } ||= 15;
1874 while ( $record = &get_record( $in ) ) {
1875 &put_record( $record, $out );
1880 foreach $file ( @{ $options->{ "files" } } )
1882 $data_in = &Maasha::Common::read_open( $file );
1884 while ( $line = <$data_in> )
1888 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1890 @scores = split /,/, $seq_qual;
1891 @seqs = split //, &Maasha::Solid::color_space2seq( $seq_cs );
1893 for ( $i = 0; $i < @seqs; $i++ ) {
1894 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1898 SEQ_NAME => $seq_name,
1900 SEQ_QUAL => $seq_qual,
1901 SEQ_LEN => length $seq_cs,
1902 SEQ => join( "", @seqs ),
1903 SCORE_MEAN => sprintf( "%.2f", &Maasha::Calc::mean( \@scores ) ),
1906 &put_record( $record, $out );
1908 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1918 close $data_in if $data_in;
1922 sub script_read_mysql
1924 # Martin A. Hansen, May 2008.
1926 # Read a MySQL query into stream.
1928 my ( $in, # handle to in stream
1929 $out, # handle to out stream
1930 $options, # options hash
1935 my ( $record, $dbh, $results );
1937 $options->{ "user" } ||= &Maasha::UCSC::ucsc_get_user();
1938 $options->{ "password" } ||= &Maasha::UCSC::ucsc_get_password();
1940 while ( $record = &get_record( $in ) ) {
1941 &put_record( $record, $out );
1944 $dbh = &Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1946 $results = &Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1948 &Maasha::SQL::disconnect( $dbh );
1950 map { &put_record( $_ ) } @{ $results };
1954 sub script_count_seq
1956 # Martin A. Hansen, August 2007.
1958 # Count sequences in stream.
1960 my ( $in, # handle to in stream
1961 $out, # handle to out stream
1962 $options, # options hash
1967 my ( $record, $count, $result, $fh );
1971 while ( $record = &get_record( $in ) )
1973 $count++ if $record->{ "SEQ" };
1975 &put_record( $record, $out ) if not $options->{ "no_stream" };
1978 $result = { "count_seq" => $count };
1980 $fh = &write_stream( $options->{ "data_out" } );
1982 &put_record( $result, $fh );
1988 sub script_length_seq
1990 # Martin A. Hansen, August 2007.
1992 # Determine the length of sequences in stream.
1994 my ( $in, # handle to in stream
1995 $out, # handle to out stream
1996 $options, # options hash
2001 my ( $record, $total );
2003 while ( $record = &get_record( $in ) )
2005 if ( $record->{ "SEQ" } )
2007 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2008 $total += $record->{ "SEQ_LEN" };
2011 &put_record( $record, $out ) if not $options->{ "no_stream" };
2014 &put_record( { TOTAL_SEQ_LEN => $total }, $out );
2018 sub script_uppercase_seq
2020 # Martin A. Hansen, August 2007.
2022 # Uppercases sequences in stream.
2024 my ( $in, # handle to in stream
2025 $out, # handle to out stream
2032 while ( $record = &get_record( $in ) )
2034 $record->{ "SEQ" } = uc $record->{ "SEQ" } if $record->{ "SEQ" };
2036 &put_record( $record, $out );
2041 sub script_shuffle_seq
2043 # Martin A. Hansen, December 2007.
2045 # Shuffle sequences in stream.
2047 my ( $in, # handle to in stream
2048 $out, # handle to out stream
2055 while ( $record = &get_record( $in ) )
2057 $record->{ "SEQ" } = &Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
2059 &put_record( $record, $out );
2064 sub script_analyze_seq
2066 # Martin A. Hansen, August 2007.
2068 # Analyze sequence composition of sequences in stream.
2070 my ( $in, # handle to in stream
2071 $out, # handle to out stream
2076 my ( $record, $analysis );
2078 while ( $record = &get_record( $in ) )
2080 if ( $record->{ "SEQ" } )
2082 $analysis = &Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
2084 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
2087 &put_record( $record, $out );
2092 sub script_analyze_tags
2094 # Martin A. Hansen, August 2008.
2096 # Analyze sequence tags in stream.
2098 my ( $in, # handle to in stream
2099 $out, # handle to out stream
2104 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
2106 while ( $record = &get_record( $in ) )
2108 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2110 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
2114 $len_hash{ length( $record->{ "SEQ" } ) }++;
2115 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
2118 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
2120 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
2124 $len_hash{ $record->{ "BED_LEN" } }++;
2125 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
2130 foreach $key ( sort { $a <=> $b } keys %len_hash )
2132 $tag_record->{ "TAG_LEN" } = $key;
2133 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
2134 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
2136 &put_record( $tag_record, $out );
2141 sub script_complexity_seq
2143 # Martin A. Hansen, May 2008.
2145 # Generates an index calculated as the most common di-residue over
2146 # the sequence length for all sequences in stream.
2148 my ( $in, # handle to in stream
2149 $out, # handle to out stream
2154 my ( $record, $index );
2156 while ( $record = &get_record( $in ) )
2158 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", &Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
2160 &put_record( $record, $out );
2165 sub script_oligo_freq
2167 # Martin A. Hansen, August 2007.
2169 # Determine the length of sequences in stream.
2171 my ( $in, # handle to in stream
2172 $out, # handle to out stream
2173 $options, # options hash
2178 my ( $record, %oligos, @freq_table );
2180 $options->{ "word_size" } ||= 7;
2182 while ( $record = &get_record( $in ) )
2184 if ( $record->{ "SEQ" } )
2186 map { $oligos{ $_ }++ } &Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2188 if ( not $options->{ "all" } )
2190 @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
2192 map { &put_record( $_, $out ) } @freq_table;
2198 &put_record( $record, $out );
2201 if ( $options->{ "all" } )
2203 @freq_table = &Maasha::Seq::oligo_freq( \%oligos );
2205 map { &put_record( $_, $out ) } @freq_table;
2210 sub script_create_weight_matrix
2212 # Martin A. Hansen, August 2007.
2214 # Creates a weight matrix from an alignmnet.
2216 my ( $in, # handle to in stream
2217 $out, # handle to out stream
2218 $options, # options hash
2223 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2227 while ( $record = &get_record( $in ) )
2229 if ( $record->{ "SEQ" } )
2231 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2233 $res = substr $record->{ "SEQ" }, $i, 1;
2235 $freq_hash{ $i }{ $res }++;
2236 $res_hash{ $res } = 1;
2243 &put_record( $record, $out );
2247 foreach $res ( sort keys %res_hash )
2251 $record->{ "V0" } = $res;
2253 for ( $i = 0; $i < keys %freq_hash; $i++ )
2255 $freq = $freq_hash{ $i }{ $res } || 0;
2257 if ( $options->{ "percent" } ) {
2258 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2261 $record->{ "V" . ( $i + 1 ) } = $freq;
2264 &put_record( $record, $out );
2269 sub script_calc_bit_scores
2271 # Martin A. Hansen, March 2007.
2273 # Calculates the bit scores for each position from an alignmnet in the stream.
2275 my ( $in, # handle to in stream
2276 $out, # handle to out stream
2281 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2285 while ( $record = &get_record( $in ) )
2287 if ( $record->{ "SEQ" } )
2289 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2291 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2293 $res = substr $record->{ "SEQ" }, $i, 1;
2295 next if $res =~ /-|_|~|\./;
2297 $freq_hash{ $i }{ $res }++;
2304 &put_record( $record, $out );
2310 if ( $type eq "protein" ) {
2316 for ( $i = 0; $i < keys %freq_hash; $i++ )
2318 $bit_height = &Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2320 $bit_diff = $bit_max - $bit_height;
2322 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2325 &put_record( $record, $out );
2329 sub script_reverse_seq
2331 # Martin A. Hansen, August 2007.
2333 # Reverse sequence in record.
2335 my ( $in, # handle to in stream
2336 $out, # handle to out stream
2343 while ( $record = &get_record( $in ) )
2345 if ( $record->{ "SEQ" } ) {
2346 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2349 &put_record( $record, $out );
2354 sub script_complement_seq
2356 # Martin A. Hansen, August 2007.
2358 # Complement sequence in record.
2360 my ( $in, # handle to in stream
2361 $out, # handle to out stream
2366 my ( $record, $type );
2368 while ( $record = &get_record( $in ) )
2370 if ( $record->{ "SEQ" } )
2373 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2376 if ( $type eq "rna" ) {
2377 &Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2378 } elsif ( $type eq "dna" ) {
2379 &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2383 &put_record( $record, $out );
2388 sub script_remove_indels
2390 # Martin A. Hansen, August 2007.
2392 # Remove indels from sequences in stream.
2394 my ( $in, # handle to in stream
2395 $out, # handle to out stream
2402 while ( $record = &get_record( $in ) )
2404 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2406 &put_record( $record, $out );
2411 sub script_transliterate_seq
2413 # Martin A. Hansen, August 2007.
2415 # Transliterate chars from sequence in record.
2417 my ( $in, # handle to in stream
2418 $out, # handle to out stream
2419 $options, # options hash
2424 my ( $record, $search, $replace, $delete );
2426 $search = $options->{ "search" } || "";
2427 $replace = $options->{ "replace" } || "";
2428 $delete = $options->{ "delete" } || "";
2430 while ( $record = &get_record( $in ) )
2432 if ( $record->{ "SEQ" } )
2434 if ( $search and $replace ) {
2435 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2436 } elsif ( $delete ) {
2437 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2441 &put_record( $record, $out );
2446 sub script_transliterate_vals
2448 # Martin A. Hansen, April 2008.
2450 # Transliterate chars from values in record.
2452 my ( $in, # handle to in stream
2453 $out, # handle to out stream
2454 $options, # options hash
2459 my ( $record, $search, $replace, $delete, $key );
2461 $search = $options->{ "search" } || "";
2462 $replace = $options->{ "replace" } || "";
2463 $delete = $options->{ "delete" } || "";
2465 while ( $record = &get_record( $in ) )
2467 foreach $key ( @{ $options->{ "keys" } } )
2469 if ( exists $record->{ $key } )
2471 if ( $search and $replace ) {
2472 eval "\$record->{ $key } =~ tr/$search/$replace/";
2473 } elsif ( $delete ) {
2474 eval "\$record->{ $key } =~ tr/$delete//d";
2479 &put_record( $record, $out );
2484 sub script_translate_seq
2486 # Martin A. Hansen, February 2008.
2488 # Translate DNA sequence into protein sequence.
2490 my ( $in, # handle to in stream
2491 $out, # handle to out stream
2492 $options, # options hash
2497 my ( $record, $frame, %new_record );
2499 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2501 while ( $record = &get_record( $in ) )
2503 if ( $record->{ "SEQ" } )
2505 if ( &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2507 foreach $frame ( @{ $options->{ "frames" } } )
2509 %new_record = %{ $record };
2511 $new_record{ "SEQ" } = &Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2512 $new_record{ "SEQ_LEN" } = length $record->{ "SEQ" };
2513 $new_record{ "FRAME" } = $frame;
2515 &put_record( \%new_record, $out );
2521 &put_record( $record, $out );
2527 sub script_extract_seq
2529 # Martin A. Hansen, August 2007.
2531 # Extract subsequences from sequences in record.
2533 my ( $in, # handle to in stream
2534 $out, # handle to out stream
2535 $options, # options hash
2540 my ( $beg, $end, $len, $record );
2542 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2545 $beg = $options->{ "beg" } - 1; # correcting for start offset
2548 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2550 } elsif ( defined $options->{ "end" } ) {
2551 $end = $options->{ "end" } - 1; # correcting for start offset
2554 $len = $options->{ "len" };
2556 # print "beg->$beg, end->$end, len->$len\n";
2558 while ( $record = &get_record( $in ) )
2560 if ( $record->{ "SEQ" } )
2562 if ( defined $beg and defined $end )
2564 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2565 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2567 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2570 elsif ( defined $beg and defined $len )
2572 if ( $len > length $record->{ "SEQ" } ) {
2573 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2575 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2578 elsif ( defined $beg )
2580 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2584 &put_record( $record, $out );
2589 sub script_get_genome_seq
2591 # Martin A. Hansen, December 2007.
2593 # Gets a subsequence from a genome.
2595 my ( $in, # handle to in stream
2596 $out, # handle to out stream
2597 $options, # options hash
2602 my ( $record, $genome_file, $index_file, $index, $fh, $index_head, $index_beg, $index_len, $beg, $len, %lookup_hash, @begs, @lens, $i );
2604 $options->{ "flank" } ||= 0;
2606 if ( $options->{ "genome" } )
2608 $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } );
2609 $index_file = &Maasha::Config::genome_fasta_index( $options->{ 'genome' } );
2611 $fh = &Maasha::Common::read_open( $genome_file );
2612 $index = &Maasha::Fasta::index_retrieve( $index_file );
2614 shift @{ $index }; # Get rid of the file size info
2616 map { $lookup_hash{ $_->[ 0 ] } = [ $_->[ 1 ], $_->[ 2 ] ] } @{ $index };
2618 if ( exists $lookup_hash{ $options->{ "chr" } } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2620 ( $index_beg, $index_len ) = @{ $lookup_hash{ $options->{ "chr" } } };
2622 $beg = $index_beg + $options->{ "beg" } - 1;
2624 if ( $options->{ "len" } ) {
2625 $len = $options->{ "len" };
2626 } elsif ( $options->{ "end" } ) {
2627 $len = ( $options->{ "end" } - $options->{ "beg" } + 1 );
2630 $beg -= $options->{ "flank" };
2631 $len += 2 * $options->{ "flank" };
2633 if ( $beg <= $index_beg )
2635 $len -= $index_beg - $beg;
2639 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2641 next if $beg > $index_beg + $index_len;
2643 $record->{ "CHR" } = $options->{ "chr" };
2644 $record->{ "CHR_BEG" } = $beg - $index_beg;
2645 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2647 $record->{ "SEQ" } = &Maasha::Common::file_read( $fh, $beg, $len );
2648 $record->{ "SEQ_LEN" } = $len;
2650 &put_record( $record, $out );
2654 while ( $record = &get_record( $in ) )
2656 if ( $options->{ "genome" } and not $record->{ "SEQ" } )
2658 if ( $record->{ "REC_TYPE" } eq "BED" and exists $lookup_hash{ $record->{ "CHR" } } )
2660 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "CHR" } } };
2662 $beg = $record->{ "CHR_BEG" } + $index_beg;
2663 $len = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2665 elsif ( $record->{ "REC_TYPE" } eq "PSL" and exists $lookup_hash{ $record->{ "S_ID" } } )
2667 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2669 $beg = $record->{ "S_BEG" } + $index_beg;
2670 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2672 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and exists $lookup_hash{ $record->{ "S_ID" } } )
2674 ( $index_beg, $index_len ) = @{ $lookup_hash{ $record->{ "S_ID" } } };
2676 $beg = $record->{ "S_BEG" } + $index_beg;
2677 $len = $record->{ "S_END" } - $record->{ "S_BEG" } + 1;
2680 $beg -= $options->{ "flank" };
2681 $len += 2 * $options->{ "flank" };
2683 if ( $beg <= $index_beg )
2685 $len -= $index_beg - $beg;
2689 $len = $index_beg + $index_len - $beg if $beg + $len > $index_beg + $index_len;
2691 next if $beg > $index_beg + $index_len;
2693 $record->{ "CHR_BEG" } = $beg - $index_beg;
2694 $record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $len - 1;
2696 $record->{ "SEQ" } = &Maasha::Common::file_read( $fh, $beg, $len );
2698 if ( $record->{ "STRAND" } and $record->{ "STRAND" } eq "-" )
2700 &Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2701 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2704 if ( $options->{ "mask" } )
2706 if ( $record->{ "BLOCKCOUNT" } > 1 ) # uppercase hit block segments and lowercase the rest.
2708 $record->{ "SEQ" } = lc $record->{ "SEQ" };
2710 @begs = split ",", $record->{ "Q_BEGS" };
2711 @lens = split ",", $record->{ "BLOCKSIZES" };
2713 for ( $i = 0; $i < @begs; $i++ ) {
2714 substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ], uc substr $record->{ "SEQ" }, $begs[ $i ], $lens[ $i ];
2720 &put_record( $record, $out );
2727 sub script_get_genome_align
2729 # Martin A. Hansen, April 2008.
2731 # Gets a subalignment from a multiple genome alignment.
2733 my ( $in, # handle to in stream
2734 $out, # handle to out stream
2735 $options, # options hash
2740 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2742 $options->{ "strand" } ||= "+";
2746 $maf_track = &Maasha::Config::maf_track( $options->{ "genome" } );
2748 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2750 $beg = $options->{ "beg" } - 1;
2752 if ( $options->{ "end" } ) {
2753 $end = $options->{ "end" };
2754 } elsif ( $options->{ "len" } ) {
2755 $end = $beg + $options->{ "len" };
2758 $align = &Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2760 foreach $entry ( @{ $align } )
2762 $entry->{ "ALIGN" } = $align_num;
2763 $entry->{ "CHR" } = $record->{ "CHR" };
2764 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2765 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2766 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2767 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2768 $entry->{ "SCORE" } = $record->{ "SCORE" };
2770 &put_record( $entry, $out );
2774 while ( $record = &get_record( $in ) )
2776 if ( $record->{ "REC_TYPE" } eq "BED" )
2778 $align = &Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2780 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2782 $align = &Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2784 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2786 $align = &Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2789 foreach $entry ( @{ $align } )
2791 $entry->{ "ALIGN" } = $align_num;
2792 $entry->{ "CHR" } = $record->{ "CHR" };
2793 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2794 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2795 $entry->{ "STRAND" } = $record->{ "STRAND" };
2796 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2797 $entry->{ "SCORE" } = $record->{ "SCORE" };
2799 &put_record( $entry, $out );
2807 sub script_get_genome_phastcons
2809 # Martin A. Hansen, February 2008.
2811 # Get phastcons scores from genome intervals.
2813 my ( $in, # handle to in stream
2814 $out, # handle to out stream
2815 $options, # options hash
2820 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2822 $options->{ "flank" } ||= 0;
2824 $phastcons_file = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
2825 $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2827 $index = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
2828 $fh_phastcons = &Maasha::Common::read_open( $phastcons_file );
2830 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2832 $options->{ "beg" } -= 1; # request is 1-based
2833 $options->{ "end" } -= 1; # request is 1-based
2835 if ( $options->{ "len" } ) {
2836 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2839 $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2841 $record->{ "CHR" } = $options->{ "chr" };
2842 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2843 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2845 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2846 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2848 &put_record( $record, $out );
2851 while ( $record = &get_record( $in ) )
2853 if ( $record->{ "REC_TYPE" } eq "BED" )
2855 $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2857 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2859 $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2861 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2863 $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2866 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2867 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2869 &put_record( $record, $out );
2872 close $fh_phastcons if $fh_phastcons;
2878 # Martin A. Hansen, December 2007.
2880 # Folds sequences in stream into secondary structures.
2882 my ( $in, # handle to in stream
2883 $out, # handle to out stream
2888 my ( $record, $type, $struct, $index );
2890 while ( $record = &get_record( $in ) )
2892 if ( $record->{ "SEQ" } )
2895 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2898 if ( $type ne "protein" )
2900 ( $struct, $index ) = &Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2901 $record->{ "SEC_STRUCT" } = $struct;
2902 $record->{ "FREE_ENERGY" } = $index;
2903 $record->{ "SCORE" } = abs int $index;
2904 $record->{ "SIZE" } = length $struct;
2905 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2909 &put_record( $record, $out );
2914 sub script_split_seq
2916 # Martin A. Hansen, August 2007.
2918 # Split a sequence in stream into words.
2920 my ( $in, # handle to in stream
2921 $out, # handle to out stream
2922 $options, # options hash
2927 my ( $record, $new_record, $i, $subseq, %lookup );
2929 $options->{ "word_size" } ||= 7;
2931 while ( $record = &get_record( $in ) )
2933 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2935 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2937 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2939 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2941 $new_record->{ "REC_TYPE" } = "SPLIT";
2942 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2943 $new_record->{ "SEQ" } = $subseq;
2945 &put_record( $new_record, $out );
2947 $lookup{ $subseq } = 1;
2951 $new_record->{ "REC_TYPE" } = "SPLIT";
2952 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2953 $new_record->{ "SEQ" } = $subseq;
2955 &put_record( $new_record, $out );
2961 &put_record( $record, $out );
2967 sub script_split_bed
2969 # Martin A. Hansen, June 2008.
2971 # Split a BED record into overlapping windows.
2973 my ( $in, # handle to in stream
2974 $out, # handle to out stream
2975 $options, # options hash
2980 my ( $record, $new_record, $i );
2982 $options->{ "window_size" } ||= 20;
2983 $options->{ "step_size" } ||= 1;
2985 while ( $record = &get_record( $in ) )
2987 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2989 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2991 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2993 $new_record->{ "REC_TYPE" } = "BED";
2994 $new_record->{ "CHR" } = $record->{ "CHR" };
2995 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2996 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2997 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2998 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2999 $new_record->{ "SCORE" } = $record->{ "SCORE" };
3000 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3002 &put_record( $new_record, $out );
3007 &put_record( $record, $out );
3013 sub script_align_seq
3015 # Martin A. Hansen, August 2007.
3017 # Align sequences in stream.
3019 my ( $in, # handle to in stream
3020 $out, # handle to out stream
3025 my ( $record, @entries, $entry );
3027 while ( $record = &get_record( $in ) )
3029 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3030 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3031 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
3032 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
3034 &put_record( $record, $out );
3038 @entries = &Maasha::Align::align( \@entries );
3040 foreach $entry ( @entries )
3042 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
3046 SEQ_NAME => $entry->[ SEQ_NAME ],
3047 SEQ => $entry->[ SEQ ],
3050 &put_record( $record, $out );
3058 # Martin A. Hansen, February 2008.
3060 # Using the first sequence in stream as reference, tile
3061 # all subsequent sequences based on pairwise alignments.
3063 my ( $in, # handle to in stream
3064 $out, # handle to out stream
3065 $options, # options hash
3070 my ( $record, $first, $ref_entry, @entries );
3074 while ( $record = &get_record( $in ) )
3076 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3080 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3086 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3091 &put_record( $record, $out );
3095 @entries = &Maasha::Align::align_tile( $ref_entry, \@entries, $options );
3097 map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
3101 sub script_invert_align
3103 # Martin A. Hansen, February 2008.
3105 # Inverts an alignment showing only non-mathing residues
3106 # using the first sequence as reference.
3108 my ( $in, # handle to in stream
3109 $out, # handle to out stream
3110 $options, # options hash
3115 my ( $record, @entries );
3117 while ( $record = &get_record( $in ) )
3119 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } and $record->{ "ALIGN" } )
3121 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3125 &put_record( $record, $out );
3129 &Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
3131 map { &put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ], ALIGN => 1 }, $out ) } @entries;
3135 sub script_patscan_seq
3137 # Martin A. Hansen, August 2007.
3139 # Locates patterns in sequences using scan_for_matches.
3141 my ( $in, # handle to in stream
3142 $out, # handle to out stream
3143 $options, # options hash
3148 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
3150 if ( $options->{ "patterns" } ) {
3151 $patterns = &Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
3152 } elsif ( -f $options->{ "patterns_in" } ) {
3153 $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
3156 $genome_file = &Maasha::Config::genome_fasta( $options->{ 'genome' } ) if $options->{ 'genome' };
3158 push @args, "-c" if $options->{ "comp" };
3159 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
3160 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
3162 $seq_file = "$BP_TMP/patscan.seq";
3163 $pat_file = "$BP_TMP/patscan.pat";
3164 $out_file = "$BP_TMP/patscan.out";
3166 $fh_out = &Maasha::Common::write_open( $seq_file );
3170 while ( $record = &get_record( $in ) )
3172 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3174 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3176 &Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
3178 $head_hash{ $i } = $record->{ "SEQ_NAME" };
3183 # &put_record( $record, $out );
3188 $arg = join " ", @args;
3189 $arg .= " -p" if $type eq "protein";
3191 foreach $pattern ( @{ $patterns } )
3193 $fh_out = &Maasha::Common::write_open( $pat_file );
3195 print $fh_out "$pattern\n";
3199 if ( $options->{ 'genome' } ) {
3200 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
3201 # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
3203 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
3204 # &Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
3207 $fh_in = &Maasha::Common::read_open( $out_file );
3209 while ( $entry = &Maasha::Fasta::get_entry( $fh_in ) )
3211 $result = &Maasha::Patscan::parse_scan_result( $entry, $pattern );
3213 if ( $options->{ 'genome' } )
3215 $result->{ "CHR" } = $result->{ "S_ID" };
3216 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
3217 $result->{ "CHR_END" } = $result->{ "S_END" };
3219 delete $result->{ "S_ID" };
3220 delete $result->{ "S_BEG" };
3221 delete $result->{ "S_END" };
3225 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
3228 &put_record( $result, $out );
3240 sub script_create_blast_db
3242 # Martin A. Hansen, September 2007.
3244 # Creates a NCBI BLAST database with formatdb
3246 my ( $in, # handle to in stream
3247 $out, # handle to out stream
3248 $options, # options hash
3253 my ( $fh, $seq_type, $path, $record );
3255 $path = $options->{ "database" };
3257 $fh = &Maasha::Common::write_open( $path );
3259 while ( $record = &get_record( $in ) )
3261 &put_record( $record, $out ) if not $options->{ "no_stream" };
3263 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
3265 $seq_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $seq_type;
3267 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh );
3273 if ( $seq_type eq "protein" ) {
3274 &Maasha::Common::run( "formatdb", "-p T -i $path -t $options->{ 'database' }" );
3276 &Maasha::Common::run( "formatdb", "-p F -i $path -t $options->{ 'database' }" );
3283 sub script_blast_seq
3285 # Martin A. Hansen, September 2007.
3287 # BLASTs sequences in stream against a given database.
3289 my ( $in, # handle to in stream
3290 $out, # handle to out stream
3291 $options, # options hash
3296 my ( $genome, $q_type, $s_type, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields );
3298 $options->{ "e_val" } = 10 if not defined $options->{ "e_val" };
3299 $options->{ "filter" } = "F";
3300 $options->{ "filter" } = "T" if $options->{ "filter" };
3301 $options->{ "cpus" } ||= 1;
3303 $options->{ "database" } = &Maasha::Config::genome_blast( $options->{ 'genome' } ) if $options->{ 'genome' };
3305 $tmp_in = "$BP_TMP/blast_query.seq";
3306 $tmp_out = "$BP_TMP/blast.result";
3308 $fh_out = &Maasha::Common::write_open( $tmp_in );
3310 while ( $record = &get_record( $in ) )
3312 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3314 $q_type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $q_type;
3316 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out );
3319 &put_record( $record, $out );
3324 if ( -f $options->{ 'database' } . ".phr" ) {
3325 $s_type = "protein";
3327 $s_type = "nucleotide";
3330 if ( not $options->{ 'program' } )
3332 if ( $q_type ne "protein" and $s_type ne "protein" ) {
3333 $options->{ 'program' } = "blastn";
3334 } elsif ( $q_type eq "protein" and $s_type eq "protein" ) {
3335 $options->{ 'program' } = "blastp";
3336 } elsif ( $q_type ne "protein" and $s_type eq "protein" ) {
3337 $options->{ 'program' } = "blastx";
3338 } elsif ( $q_type eq "protein" and $s_type ne "protein" ) {
3339 $options->{ 'program' } = "tblastn";
3343 &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 );
3347 $fh_out = &Maasha::Common::read_open( $tmp_out );
3351 while ( $line = <$fh_out> )
3355 next if $line =~ /^#/;
3357 @fields = split /\s+/, $line;
3359 $record->{ "REC_TYPE" } = "BLAST";
3360 $record->{ "Q_ID" } = $fields[ 0 ];
3361 $record->{ "S_ID" } = $fields[ 1 ];
3362 $record->{ "IDENT" } = $fields[ 2 ];
3363 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
3364 $record->{ "MISMATCHES" } = $fields[ 4 ];
3365 $record->{ "GAPS" } = $fields[ 5 ];
3366 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
3367 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
3368 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
3369 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
3370 $record->{ "E_VAL" } = $fields[ 10 ];
3371 $record->{ "BIT_SCORE" } = $fields[ 11 ];
3373 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
3375 $record->{ "STRAND" } = '-';
3377 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3381 $record->{ "STRAND" } = '+';
3384 &put_record( $record, $out );
3395 # Martin A. Hansen, August 2007.
3397 # BLATs sequences in stream against a given genome.
3399 my ( $in, # handle to in stream
3400 $out, # handle to out stream
3401 $options, # options hash
3406 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries );
3408 $genome_file = &Maasha::Config::genome_fasta( $options->{ "genome" } );
3410 $options->{ 'tile_size' } ||= 11;
3411 $options->{ 'one_off' } ||= 0;
3412 $options->{ 'min_identity' } ||= 90;
3413 $options->{ 'min_score' } ||= 0;
3414 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3416 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3417 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3418 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3419 $blat_args .= " -minScore=$options->{ 'min_score' }";
3420 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3421 $blat_args .= " -ooc=" . &Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3423 $query_file = "$BP_TMP/blat.seq";
3425 $fh_out = &Maasha::Common::write_open( $query_file );
3427 while ( $record = &get_record( $in ) )
3429 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3431 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_out, 80 );
3432 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3435 &put_record( $record, $out );
3440 $blat_args .= " -t=dnax" if $type eq "protein";
3441 $blat_args .= " -q=$type";
3443 $result_file = "$BP_TMP/blat.psl";
3445 &Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3449 $entries = &Maasha::UCSC::psl_get_entries( $result_file );
3451 map { &put_record( $_, $out ) } @{ $entries };
3453 unlink $result_file;
3457 sub script_match_seq
3459 # Martin A. Hansen, August 2007.
3461 # BLATs sequences in stream against a given genome.
3463 my ( $in, # handle to in stream
3464 $out, # handle to out stream
3465 $options, # options hash
3470 my ( $record, @entries, $results );
3472 $options->{ "word_size" } ||= 20;
3473 $options->{ "direction" } ||= "both";
3475 while ( $record = &get_record( $in ) )
3477 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3478 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3481 &put_record( $record, $out );
3484 if ( @entries == 1 )
3486 $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3488 map { &put_record( $_, $out ) } @{ $results };
3490 elsif ( @entries == 2 )
3492 $results = &Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3494 map { &put_record( $_, $out ) } @{ $results };
3499 sub script_create_vmatch_index
3501 # Martin A. Hansen, January 2008.
3503 # Create a vmatch index from sequences in the stream.
3505 my ( $in, # handle to in stream
3506 $out, # handle to out stream
3507 $options, # options hash
3512 my ( $record, $file_tmp, $fh_tmp, $type );
3514 if ( $options->{ "index_name" } )
3516 $file_tmp = $options->{ 'index_name' };
3517 $fh_tmp = &Maasha::Common::write_open( $file_tmp );
3520 while ( $record = &get_record( $in ) )
3522 if ( $options->{ "index_name" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3524 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3526 $type = &Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
3529 &put_record( $record, $out ) if not $options->{ "no_stream" };
3532 if ( $options->{ "index_name" } )
3536 if ( $type eq "protein" ) {
3537 &Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3539 &Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3547 sub script_vmatch_seq
3549 # Martin A. Hansen, August 2007.
3551 # Vmatches sequences in stream against a given genome.
3553 my ( $in, # handle to in stream
3554 $out, # handle to out stream
3555 $options, # options hash
3560 my ( @index_files, @records, $result_file, $fh_in, $record );
3562 $options->{ 'count' } = 1 if $options->{ 'max_hits' };
3564 if ( $options->{ "index_name" } ) {
3565 @index_files = $options->{ "index_name" };
3567 @index_files = &Maasha::Config::genome_vmatch( $options->{ "genome" } );
3570 while ( $record = &get_record( $in ) )
3572 push @records, $record;
3574 &put_record( $record, $out );
3577 $result_file = &Maasha::Match::match_vmatch( $BP_TMP, \@records, \@index_files, $options );
3581 $fh_in = &Maasha::Common::read_open( $result_file );
3583 while ( $record = &Maasha::Match::vmatch_get_entry( $fh_in ) ) {
3584 &put_record( $record, $out );
3589 unlink $result_file;
3593 sub script_write_fasta
3595 # Martin A. Hansen, August 2007.
3597 # Write FASTA entries from sequences in stream.
3599 my ( $in, # handle to in stream
3600 $out, # handle to out stream
3601 $options, # options hash
3606 my ( $record, $fh );
3608 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3610 while ( $record = &get_record( $in ) )
3612 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3613 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh, $options->{ "wrap" } );
3616 &put_record( $record, $out ) if not $options->{ "no_stream" };
3623 sub script_write_align
3625 # Martin A. Hansen, August 2007.
3627 # Write pretty alignments aligned sequences in stream.
3629 my ( $in, # handle to in stream
3630 $out, # handle to out stream
3631 $options, # options hash
3636 my ( $fh, $record, $align, $old_align, @entries );
3638 $fh = &write_stream( $options->{ "data_out" } ) ;
3640 while ( $record = &get_record( $in ) )
3642 if ( $record->{ "ALIGN" } and $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3644 $align = $record->{ "ALIGN" };
3646 if ( not $old_align )
3648 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3650 $old_align = $align;
3652 elsif ( $align == $old_align )
3654 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3658 if ( scalar( @entries ) == 2 ) {
3659 &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3660 } elsif ( scalar ( @entries ) > 2 ) {
3661 &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3665 $old_align = $align;
3669 &put_record( $record, $out ) if not $options->{ "no_stream" };
3672 if ( scalar( @entries ) == 2 ) {
3673 &Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3674 } elsif ( scalar ( @entries ) > 2 ) {
3675 &Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3682 sub script_write_blast
3684 # Martin A. Hansen, November 2007.
3686 # Write data in blast table format (-m8 and 9).
3688 my ( $in, # handle to in stream
3689 $out, # handle to out stream
3690 $options, # options hash
3695 my ( $fh, $record, $first );
3697 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } ) ;
3701 while ( $record = &get_record( $in ) )
3703 if ( $record->{ "REC_TYPE" } eq "BLAST" )
3705 if ( $options->{ "comment" } and $first )
3707 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";
3712 if ( $record->{ "STRAND" } eq "-" ) {
3713 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
3716 print $fh join( "\t",
3717 $record->{ "Q_ID" },
3718 $record->{ "S_ID" },
3719 $record->{ "IDENT" },
3720 $record->{ "ALIGN_LEN" },
3721 $record->{ "MISMATCHES" },
3722 $record->{ "GAPS" },
3723 $record->{ "Q_BEG" } + 1,
3724 $record->{ "Q_END" } + 1,
3725 $record->{ "S_BEG" } + 1,
3726 $record->{ "S_END" } + 1,
3727 $record->{ "E_VAL" },
3728 $record->{ "BIT_SCORE" }
3732 &put_record( $record, $out ) if not $options->{ "no_stream" };
3739 sub script_write_tab
3741 # Martin A. Hansen, August 2007.
3743 # Write data as table.
3745 my ( $in, # handle to in stream
3746 $out, # handle to out stream
3747 $options, # options hash
3752 my ( $fh, $record, $key, @keys, @vals, $ok, %no_keys, $A, $B );
3754 $options->{ "delimit" } ||= "\t";
3756 map { $no_keys{ $_ } = 1 } @{ $options->{ "no_keys" } };
3758 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3760 while ( $record = &get_record( $in ) )
3765 if ( $options->{ "keys" } )
3767 map { $ok = 0 if not exists $record->{ $_ } } @{ $options->{ "keys" } };
3771 foreach $key ( @{ $options->{ "keys" } } )
3773 if ( exists $record->{ $key } )
3775 push @keys, $key if $options->{ "comment" };
3776 push @vals, $record->{ $key };
3783 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
3785 next if exists $no_keys{ $key };
3787 push @keys, $key if $options->{ "comment" };
3788 push @vals, $record->{ $key };
3792 if ( @keys and $options->{ "comment" } )
3794 print $fh "#", join( $options->{ "delimit" }, @keys ), "\n";
3796 delete $options->{ "comment" };
3799 print $fh join( $options->{ "delimit" }, @vals ), "\n" if @vals;
3801 &put_record( $record, $out ) if not $options->{ "no_stream" };
3808 sub script_write_bed
3810 # Martin A. Hansen, August 2007.
3812 # Write BED format for the UCSC genome browser using records in stream.
3814 # Crude - needs lots of work!
3816 my ( $in, # handle to in stream
3817 $out, # handle to out stream
3818 $options, # options hash
3823 my ( $fh, $record, $new_record );
3825 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3827 while ( $record = &get_record( $in ) )
3829 if ( $record->{ "REC_TYPE" } eq "BED" ) # ---- Hits from BED ----
3831 &Maasha::UCSC::bed_put_entry( $record, $fh, $record->{ "BED_COLS" } );
3833 elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAT (PSL) ----
3835 $new_record->{ "CHR" } = $record->{ "S_ID" };
3836 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3837 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3838 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3839 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999;
3840 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3842 &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3844 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } ) # ---- Hits from patscan_seq ----
3846 &Maasha::UCSC::bed_put_entry( $record, $fh, 6 );
3848 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from BLAST ----
3850 $new_record->{ "CHR" } = $record->{ "S_ID" };
3851 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3852 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3853 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3854 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3855 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3857 &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3859 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i ) # ---- Hits from Vmatch ----
3861 $new_record->{ "CHR" } = $record->{ "S_ID" };
3862 $new_record->{ "CHR_BEG" } = $record->{ "S_BEG" };
3863 $new_record->{ "CHR_END" } = $record->{ "S_END" };
3864 $new_record->{ "Q_ID" } = $record->{ "Q_ID" };
3865 $new_record->{ "SCORE" } = $record->{ "SCORE" } || 999; # or use E_VAL somehow
3866 $new_record->{ "STRAND" } = $record->{ "STRAND" };
3868 &Maasha::UCSC::bed_put_entry( $new_record, $fh, 6 );
3870 elsif ( $record->{ "CHR" } and defined $record->{ "CHR_BEG" } and $record->{ "CHR_END" } ) # ---- Generic data from tables ----
3872 &Maasha::UCSC::bed_put_entry( $record, $fh );
3875 &put_record( $record, $out ) if not $options->{ "no_stream" };
3882 sub script_write_psl
3884 # Martin A. Hansen, August 2007.
3886 # Write PSL output from stream.
3888 my ( $in, # handle to in stream
3889 $out, # handle to out stream
3890 $options, # options hash
3895 my ( $fh, $record, @output, $first );
3899 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3901 while ( $record = &get_record( $in ) )
3903 &put_record( $record, $out ) if not $options->{ "no_stream" };
3905 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3907 &Maasha::UCSC::psl_put_header( $fh ) if $first;
3908 &Maasha::UCSC::psl_put_entry( $record, $fh );
3917 sub script_write_2bit
3919 # Martin A. Hansen, March 2008.
3921 # Write sequence entries from stream in 2bit format.
3923 my ( $in, # handle to in stream
3924 $out, # handle to out stream
3925 $options, # options hash
3930 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out );
3932 $mask = 1 if not $options->{ "no_mask" };
3934 $tmp_file = "$BP_TMP/write_2bit.fna";
3935 $fh_tmp = &Maasha::Common::write_open( $tmp_file );
3937 $fh_out = &write_stream( $options->{ "data_out" } );
3939 while ( $record = &get_record( $in ) )
3941 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3942 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ], $fh_tmp );
3945 &put_record( $record, $out ) if not $options->{ "no_stream" };
3950 $fh_in = &Maasha::Common::read_open( $tmp_file );
3952 &Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3961 sub script_write_solid
3963 # Martin A. Hansen, April 2008.
3965 # Write di-base encoded Solid sequence from entries in stream.
3967 my ( $in, # handle to in stream
3968 $out, # handle to out stream
3969 $options, # options hash
3974 my ( $record, $fh, $seq_cs );
3976 $fh = &write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3978 while ( $record = &get_record( $in ) )
3980 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
3982 $seq_cs = &Maasha::Solid::seq2color_space( $record->{ "SEQ" } );
3984 &Maasha::Fasta::put_entry( [ $record->{ "SEQ_NAME" }, $seq_cs ], $fh, $options->{ "wrap" } );
3987 &put_record( $record, $out ) if not $options->{ "no_stream" };
3994 sub script_plot_seqlogo
3996 # Martin A. Hansen, August 2007.
3998 # Calculates and writes a sequence logo for alignments.
4000 my ( $in, # handle to in stream
4001 $out, # handle to out stream
4002 $options, # options hash
4007 my ( $record, @entries, $logo, $fh );
4009 while ( $record = &get_record( $in ) )
4011 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
4012 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
4015 &put_record( $record, $out ) if not $options->{ "no_stream" };
4018 $logo = &Maasha::Plot::seq_logo( \@entries );
4020 $fh = &write_stream( $options->{ "data_out" } );
4028 sub script_plot_phastcons_profiles
4030 # Martin A. Hansen, January 2008.
4032 # Plots PhastCons profiles.
4034 my ( $in, # handle to in stream
4035 $out, # handle to out stream
4036 $options, # options hash
4041 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
4043 $options->{ "title" } ||= "PhastCons Profiles";
4045 $phastcons_file = &Maasha::Config::genome_phastcons( $options->{ "genome" } );
4046 $phastcons_index = &Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
4048 $index = &Maasha::UCSC::phastcons_index_retrieve( $phastcons_index );
4049 $fh_phastcons = &Maasha::Common::read_open( $phastcons_file );
4051 while ( $record = &get_record( $in ) )
4053 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4055 $scores = &Maasha::UCSC::phastcons_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
4057 push @{ $AoA }, [ @{ $scores } ];
4060 &put_record( $record, $out ) if not $options->{ "no_stream" };
4063 &Maasha::UCSC::phastcons_normalize( $AoA );
4065 $AoA = [ [ &Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
4066 $AoA = [ [ &Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
4068 $AoA = &Maasha::Matrix::matrix_flip( $AoA );
4070 $plot = &Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
4072 $fh = &write_stream( $options->{ "data_out" } );
4074 print $fh "$_\n" foreach @{ $plot };
4080 sub script_analyze_bed
4082 # Martin A. Hansen, March 2008.
4084 # Analyze BED entries in stream.
4086 my ( $in, # handle to in stream
4087 $out, # handle to out stream
4088 $options, # options hash
4095 while ( $record = &get_record( $in ) )
4097 $record = &Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
4099 &put_record( $record, $out );
4104 sub script_analyze_vals
4106 # Martin A. Hansen, August 2007.
4108 # Analyze values for given keys in stream.
4110 my ( $in, # handle to in stream
4111 $out, # handle to out stream
4112 $options, # options hash
4117 my ( $record, $key, @keys, %key_hash, $analysis, $len );
4119 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
4121 while ( $record = &get_record( $in ) )
4123 foreach $key ( keys %{ $record } )
4125 next if $options->{ "keys" } and not exists $key_hash{ $key };
4127 $analysis->{ $key }->{ "COUNT" }++;
4129 if ( &Maasha::Calc::is_a_number( $record->{ $key } ) )
4131 $analysis->{ $key }->{ "TYPE" } = "num";
4132 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
4133 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4134 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
4138 $len = length $record->{ $key };
4140 $analysis->{ $key }->{ "TYPE" } = "alph";
4141 $analysis->{ $key }->{ "SUM" } += $len;
4142 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
4143 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
4147 &put_record( $record, $out ) if not $options->{ "no_stream" };
4150 foreach $key ( keys %{ $analysis } )
4152 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
4153 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUN" };
4156 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
4166 if ( $options->{ "keys" } ) {
4167 @keys = @{ $options->{ "keys" } };
4169 @keys = keys %{ $analysis };
4172 foreach $key ( @keys )
4174 $keys .= sprintf "% 15s", $key;
4175 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
4176 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
4177 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
4178 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
4179 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
4180 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
4183 print $out "$keys\n";
4184 print $out "$types\n";
4185 print $out "$counts\n";
4186 print $out "$mins\n";
4187 print $out "$maxs\n";
4188 print $out "$sums\n";
4189 print $out "$means\n";
4193 sub script_head_records
4195 # Martin A. Hansen, August 2007.
4197 # Display the first sequences in stream.
4199 my ( $in, # handle to in stream
4200 $out, # handle to out stream
4201 $options, # options hash
4206 my ( $record, $count );
4208 $options->{ "num" } ||= 10;
4212 while ( $record = &get_record( $in ) )
4216 &put_record( $record, $out );
4218 last if $count == $options->{ "num" };
4223 sub script_remove_keys
4225 # Martin A. Hansen, August 2007.
4227 # Remove keys from stream.
4229 my ( $in, # handle to in stream
4230 $out, # handle to out stream
4231 $options, # options hash
4236 my ( $record, $new_record );
4238 while ( $record = &get_record( $in ) )
4240 if ( $options->{ "keys" } )
4242 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
4244 elsif ( $options->{ "save_keys" } )
4246 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
4248 $record = $new_record;
4251 &put_record( $record, $out ) if keys %{ $record };
4256 sub script_rename_keys
4258 # Martin A. Hansen, August 2007.
4260 # Rename keys in stream.
4262 my ( $in, # handle to in stream
4263 $out, # handle to out stream
4264 $options, # options hash
4271 while ( $record = &get_record( $in ) )
4273 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4275 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
4277 delete $record->{ $options->{ "keys" }->[ 0 ] };
4280 &put_record( $record, $out );
4285 sub script_uniq_vals
4287 # Martin A. Hansen, August 2007.
4289 # Find unique values in stream.
4291 my ( $in, # handle to in stream
4292 $out, # handle to out stream
4293 $options, # options hash
4298 my ( %hash, $record );
4300 while ( $record = &get_record( $in ) )
4302 if ( $record->{ $options->{ "key" } } )
4304 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
4306 &put_record( $record, $out );
4308 $hash{ $record->{ $options->{ "key" } } } = 1;
4310 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
4312 &put_record( $record, $out );
4316 $hash{ $record->{ $options->{ "key" } } } = 1;
4321 &put_record( $record, $out );
4327 sub script_merge_vals
4329 # Martin A. Hansen, August 2007.
4331 # Rename keys in stream.
4333 my ( $in, # handle to in stream
4334 $out, # handle to out stream
4335 $options, # options hash
4340 my ( $record, @join, $i );
4342 $options->{ "delimit" } ||= '_';
4344 while ( $record = &get_record( $in ) )
4346 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4348 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4350 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4351 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4354 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4357 &put_record( $record, $out );
4364 # Martin A. Hansen, August 2007.
4366 # Grab for records in stream.
4368 my ( $in, # handle to in stream
4369 $out, # handle to out stream
4370 $options, # options hash
4375 my ( $patterns, $pattern, $record, $key, $pos, $op, $val, %lookup_hash );
4377 if ( $options->{ "patterns" } )
4379 $patterns = [ split ",", $options->{ "patterns" } ];
4381 elsif ( -f $options->{ "patterns_in" } )
4383 $patterns = &Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
4385 elsif ( -f $options->{ "exact_in" } )
4387 $patterns = &Maasha::Patscan::read_patterns( $options->{ "exact_in" } );
4389 map { $lookup_hash{ $_ } = 1 } @{ $patterns };
4394 if ( $options->{ "eval" } )
4396 if ( $options->{ "eval" } =~ /^([^><=! ]+)\s*(>=|<=|>|<|=|!=|eq|ne)\s*(.+)$/ )
4404 while ( $record = &get_record( $in ) )
4410 if ( $options->{ "keys" } )
4412 foreach $key ( @{ $options->{ "keys" } } )
4414 if ( exists $lookup_hash{ $record->{ $key } } )
4423 foreach $key ( keys %{ $record } )
4425 if ( not $options->{ "vals_only" } )
4427 if ( exists $lookup_hash{ $key } )
4434 if ( not $options->{ "keys_only" } )
4436 if ( exists $lookup_hash{ $record->{ $key } } )
4447 foreach $pattern ( @{ $patterns } )
4449 if ( $options->{ "keys" } )
4451 foreach $key ( @{ $options->{ "keys" } } )
4453 $pos = index $record->{ $key }, $pattern;
4455 goto FOUND if $pos >= 0;
4460 foreach $key ( keys %{ $record } )
4462 if ( not $options->{ "vals_only" } )
4464 $pos = index $key, $pattern;
4466 goto FOUND if $pos >= 0;
4469 if ( not $options->{ "keys_only" } )
4471 $pos = index $record->{ $key }, $pattern;
4473 goto FOUND if $pos >= 0;
4479 elsif ( $options->{ "regex" } )
4481 if ( $options->{ "keys" } )
4483 foreach $key ( @{ $options->{ "keys" } } )
4485 if ( $options->{ "case_insensitive" } ) {
4486 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4488 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4491 goto FOUND if $pos >= 0;
4496 foreach $key ( keys %{ $record } )
4498 if ( not $options->{ "vals_only" } )
4500 if ( $options->{ "case_insensitive" } ) {
4501 $pos = 1 if $key =~ /$options->{'regex'}/i;
4503 $pos = 1 if $key =~ /$options->{'regex'}/;
4506 goto FOUND if $pos >= 0;
4509 if ( not $options->{ "keys_only" } )
4511 if ( $options->{ "case_insensitive" } ) {
4512 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/i;
4514 $pos = 1 if $record->{ $key } =~ /$options->{'regex'}/;
4517 goto FOUND if $pos >= 0;
4522 elsif ( $options->{ "eval" } )
4524 if ( defined $record->{ $key } )
4526 if ( $op eq "<" and $record->{ $key } < $val ) {
4527 $pos = 1 and goto FOUND;
4528 } elsif ( $op eq ">" and $record->{ $key } > $val ) {
4529 $pos = 1 and goto FOUND;
4530 } elsif ( $op eq ">=" and $record->{ $key } >= $val ) {
4531 $pos = 1 and goto FOUND;
4532 } elsif ( $op eq "<=" and $record->{ $key } <= $val ) {
4533 $pos = 1 and goto FOUND;
4534 } elsif ( $op eq "=" and $record->{ $key } == $val ) {
4535 $pos = 1 and goto FOUND;
4536 } elsif ( $op eq "!=" and $record->{ $key } != $val ) {
4537 $pos = 1 and goto FOUND;
4538 } elsif ( $op eq "eq" and $record->{ $key } eq $val ) {
4539 $pos = 1 and goto FOUND;
4540 } elsif ( $op eq "ne" and $record->{ $key } ne $val ) {
4541 $pos = 1 and goto FOUND;
4548 if ( $pos >= 0 and not $options->{ "invert" } ) {
4549 &put_record( $record, $out );
4550 } elsif ( $pos < 0 and $options->{ "invert" } ) {
4551 &put_record( $record, $out );
4559 # Martin A. Hansen, August 2007.
4561 # Evaluate extression for records in stream.
4563 my ( $in, # handle to in stream
4564 $out, # handle to out stream
4565 $options, # options hash
4570 my ( $record, $eval_key, $eval_val, $check, @keys );
4572 while ( $record = &get_record( $in ) )
4574 if ( $options->{ "eval" } )
4576 if ( $options->{ "eval" } =~ /^(.+)\s*=\s*(.+)$/ )
4584 @keys = split /\W+/, $eval_val;
4585 @keys = grep { ! /^\d+$/ } @keys;
4590 map { $eval_val =~ s/$_/$record->{ $_ }/g } @keys;
4592 $record->{ $eval_key } = eval "$eval_val" or &Maasha::Common::error( "eval failed -> $@" );
4595 &put_record( $record, $out );
4602 # Martin A. Hansen, June 2008.
4606 my ( $in, # handle to in stream
4607 $out, # handle to out stream
4608 $options, # options hash
4613 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4615 while ( $record = &get_record( $in ) )
4619 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4621 push @rows, $record->{ $key };
4625 push @matrix, [ @rows ];
4630 @matrix = &Maasha::Matrix::matrix_flip( \@matrix );
4632 foreach $row ( @matrix )
4634 for ( $i = 0; $i < @{ $row }; $i++ ) {
4635 $record->{ "V$i" } = $row->[ $i ];
4638 &put_record( $record, $out );
4643 sub script_add_ident
4645 # Martin A. Hansen, May 2008.
4647 # Add a unique identifier to each record in stream.
4649 my ( $in, # handle to in stream
4650 $out, # handle to out stream
4651 $options, # options hash
4656 my ( $record, $key, $prefix, $i );
4658 $key = $options->{ "key" } || "ID";
4659 $prefix = $options->{ "prefix" } || "ID";
4663 while ( $record = &get_record( $in ) )
4665 $record->{ $key } = sprintf( "$prefix%08d", $i );
4667 &put_record( $record, $out );
4674 sub script_count_records
4676 # Martin A. Hansen, August 2007.
4678 # Count records in stream.
4680 my ( $in, # handle to in stream
4681 $out, # handle to out stream
4682 $options, # options hash
4687 my ( $record, $count, $result, $fh, $line );
4691 if ( $options->{ "no_stream" } )
4693 while ( $line = <$in> )
4697 $count++ if $line eq "---";
4702 while ( $record = &get_record( $in ) )
4704 &put_record( $record, $out );
4710 $result = { "count_records" => $count };
4712 $fh = &write_stream( $options->{ "data_out" } );
4714 &put_record( $result, $fh );
4720 sub script_random_records
4722 # Martin A. Hansen, August 2007.
4724 # Pick a number or random records from stream.
4726 my ( $in, # handle to in stream
4727 $out, # handle to out stream
4728 $options, # options hash
4733 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4735 $options->{ "num" } ||= 10;
4737 $tmp_file = "$BP_TMP/random_records.tmp";
4739 $fh_out = &Maasha::Common::write_open( $tmp_file );
4743 while ( $record = &get_record( $in ) )
4745 &put_record( $record, $fh_out );
4755 &Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4757 while ( $i < $options->{ "num" } )
4759 $rand = int( rand( $count ) );
4761 if ( not exists $rand_hash{ $rand } )
4763 $rand_hash{ $rand } = 1;
4765 $max = $rand if $rand > $max;
4771 $fh_in = &Maasha::Common::read_open( $tmp_file );
4775 while ( $record = &get_record( $fh_in ) )
4777 &put_record( $record, $out ) if exists $rand_hash{ $count };
4779 last if $count == $max;
4790 sub script_sort_records
4792 # Martin A. Hansen, August 2007.
4794 # Sort to sort records according to keys.
4796 my ( $in, # handle to in stream
4797 $out, # handle to out stream
4798 $options, # options hash
4803 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4805 foreach $key ( @{ $options->{ "keys" } } )
4807 if ( $key =~ s/n$// ) {
4808 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4810 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4814 $sort_str = join " or ", @sort_cmd;
4815 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4817 while ( $record = &get_record( $in ) ) {
4818 push @records, $record;
4821 @records = sort $sort_sub @records;
4823 if ( $options->{ "reverse" } )
4825 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4826 &put_record( $records[ $i ], $out );
4831 for ( $i = 0; $i < scalar @records; $i++ ) {
4832 &put_record( $records[ $i ], $out );
4838 sub script_count_vals
4840 # Martin A. Hansen, August 2007.
4842 # Count records in stream.
4844 my ( $in, # handle to in stream
4845 $out, # handle to out stream
4846 $options, # options hash
4851 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4853 $tmp_file = "$BP_TMP/count_cache.tmp";
4855 $fh_out = &Maasha::Common::write_open( $tmp_file );
4859 while ( $record = &get_record( $in ) )
4861 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4863 push @records, $record;
4865 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4867 map { &put_record( $_, $fh_out ) } @records;
4874 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4885 $fh_in = &Maasha::Common::read_open( $tmp_file );
4887 while ( $record = &get_record( $fh_in ) )
4889 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4891 &put_record( $record, $out );
4893 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4901 foreach $record ( @records )
4903 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4905 &put_record( $record, $out );
4912 sub script_plot_histogram
4914 # Martin A. Hansen, September 2007.
4916 # Plot a simple histogram for a given key using GNU plot.
4918 my ( $in, # handle to in stream
4919 $out, # handle to out stream
4920 $options, # options hash
4925 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4927 $options->{ "title" } ||= "Histogram";
4928 $options->{ "sort" } ||= "num";
4930 while ( $record = &get_record( $in ) )
4932 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4934 &put_record( $record, $out ) if not $options->{ "no_stream" };
4937 if ( $options->{ "sort" } eq "num" ) {
4938 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4940 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4943 $result = &Maasha::Plot::histogram_simple( \@data_list, $options );
4945 $fh = &write_stream( $options->{ "data_out" } );
4947 print $fh "$_\n" foreach @{ $result };
4953 sub script_plot_lendist
4955 # Martin A. Hansen, August 2007.
4957 # Plot length distribution using GNU plot.
4959 my ( $in, # handle to in stream
4960 $out, # handle to out stream
4961 $options, # options hash
4966 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4968 $options->{ "title" } ||= "Length Distribution";
4970 while ( $record = &get_record( $in ) )
4972 $data_hash{ $record->{ $options->{ "key" } } }++ if $record->{ $options->{ "key" } };
4974 &put_record( $record, $out ) if not $options->{ "no_stream" };
4977 $max = &Maasha::Calc::list_max( [ keys %data_hash ] );
4979 for ( $i = 0; $i < $max; $i++ ) {
4980 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4983 $result = &Maasha::Plot::histogram_lendist( \@data_list, $options );
4985 $fh = &write_stream( $options->{ "data_out" } );
4987 print $fh "$_\n" foreach @{ $result };
4993 sub script_plot_chrdist
4995 # Martin A. Hansen, August 2007.
4997 # Plot chromosome distribution using GNU plot.
4999 my ( $in, # handle to in stream
5000 $out, # handle to out stream
5001 $options, # options hash
5006 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
5008 $options->{ "title" } ||= "Chromosome Distribution";
5010 while ( $record = &get_record( $in ) )
5012 if ( $record->{ "CHR" } ) { # generic
5013 $data_hash{ $record->{ "CHR" } }++;
5014 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
5015 $data_hash{ $record->{ "S_ID" } }++;
5016 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
5017 $data_hash{ $record->{ "S_ID" } }++;
5018 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
5019 $data_hash{ $record->{ "S_ID" } }++;
5022 &put_record( $record, $out ) if not $options->{ "no_stream" };
5025 foreach $elem ( keys %data_hash )
5029 $sort_key =~ s/chr//i;
5031 $sort_key =~ s/^X(.*)/99$1/;
5032 $sort_key =~ s/^Y(.*)/99$1/;
5033 $sort_key =~ s/^Z(.*)/999$1/;
5034 $sort_key =~ s/^M(.*)/9999$1/;
5035 $sort_key =~ s/^U(.*)/99999$1/;
5037 $count = $sort_key =~ tr/_//;
5039 $sort_key =~ s/_.*/"999999" x $count/ex;
5041 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
5044 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
5046 $result = &Maasha::Plot::histogram_chrdist( \@data_list, $options );
5048 $fh = &write_stream( $options->{ "data_out" } );
5050 print $fh "$_\n" foreach @{ $result };
5056 sub script_plot_karyogram
5058 # Martin A. Hansen, August 2007.
5060 # Plot hits on karyogram.
5062 my ( $in, # handle to in stream
5063 $out, # handle to out stream
5064 $options, # options hash
5069 my ( %options, $record, @data, $fh, $result, %data_hash );
5071 $options->{ "genome" } ||= "human";
5072 $options->{ "feat_color" } ||= "black";
5074 while ( $record = &get_record( $in ) )
5076 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
5078 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
5081 &put_record( $record, $out ) if not $options->{ "no_stream" };
5084 $result = &Maasha::Plot::karyogram( \%data_hash, \%options );
5086 $fh = &write_stream( $options->{ "data_out" } );
5094 sub script_plot_matches
5096 # Martin A. Hansen, August 2007.
5098 # Plot matches in 2D generating a dotplot.
5100 my ( $in, # handle to in stream
5101 $out, # handle to out stream
5102 $options, # options hash
5107 my ( $record, @data, $fh, $result, %data_hash );
5109 $options->{ "direction" } ||= "both";
5111 while ( $record = &get_record( $in ) )
5113 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
5114 push @data, $record;
5117 &put_record( $record, $out ) if not $options->{ "no_stream" };
5120 $options->{ "title" } ||= "plot_matches";
5121 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
5122 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
5124 $result = &Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
5126 $fh = &write_stream( $options->{ "data_out" } );
5128 print $fh "$_\n" foreach @{ $result };
5134 sub script_length_vals
5136 # Martin A. Hansen, August 2007.
5138 # Determine the length of the value for given keys.
5140 my ( $in, # handle to in stream
5141 $out, # handle to out stream
5142 $options, # options hash
5147 my ( $record, $key );
5149 while ( $record = &get_record( $in ) )
5151 foreach $key ( @{ $options->{ "keys" } } )
5153 if ( $record->{ $key } ) {
5154 $record->{ $key . "_LEN" } = length $record->{ $key };
5158 &put_record( $record, $out );
5165 # Martin A. Hansen, August 2007.
5167 # Calculates the sums for values of given keys.
5169 my ( $in, # handle to in stream
5170 $out, # handle to out stream
5171 $options, # options hash
5176 my ( $record, $key, %sum_hash, $fh );
5178 while ( $record = &get_record( $in ) )
5180 foreach $key ( @{ $options->{ "keys" } } )
5182 if ( $record->{ $key } ) {
5183 $sum_hash{ $key } += $record->{ $key };
5187 &put_record( $record, $out ) if not $options->{ "no_stream" };
5190 $fh = &write_stream( $options->{ "data_out" } );
5192 foreach $key ( @{ $options->{ "keys" } } ) {
5193 &put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
5200 sub script_mean_vals
5202 # Martin A. Hansen, August 2007.
5204 # Calculate the mean of values of given keys.
5206 my ( $in, # handle to in stream
5207 $out, # handle to out stream
5208 $options, # options hash
5213 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
5215 while ( $record = &get_record( $in ) )
5217 foreach $key ( @{ $options->{ "keys" } } )
5219 if ( $record->{ $key } )
5221 $sum_hash{ $key } += $record->{ $key };
5222 $count_hash{ $key }++;
5226 &put_record( $record, $out ) if not $options->{ "no_stream" };
5229 $fh = &write_stream( $options->{ "data_out" } );
5231 foreach $key ( @{ $options->{ "keys" } } )
5233 if ( $count_hash{ $key } ) {
5234 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
5239 &put_record( { $key . "_MEAN" => $mean } , $fh );
5246 sub script_median_vals
5248 # Martin A. Hansen, March 2008.
5250 # Calculate the median values of given keys.
5252 my ( $in, # handle to in stream
5253 $out, # handle to out stream
5254 $options, # options hash
5259 my ( $record, $key, %median_hash, $median, $fh );
5261 while ( $record = &get_record( $in ) )
5263 foreach $key ( @{ $options->{ "keys" } } ) {
5264 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
5267 &put_record( $record, $out ) if not $options->{ "no_stream" };
5270 $fh = &write_stream( $options->{ "data_out" } );
5272 foreach $key ( @{ $options->{ "keys" } } )
5274 if ( $median_hash{ $key } ) {
5275 $median = &Maasha::Calc::median( $median_hash{ $key } );
5280 &put_record( { $key . "_MEDIAN" => $median } , $fh );
5289 # Martin A. Hansen, February 2008.
5291 # Determine the maximum values of given keys.
5293 my ( $in, # handle to in stream
5294 $out, # handle to out stream
5295 $options, # options hash
5300 my ( $record, $key, $fh, %max_hash, $max_record );
5302 while ( $record = &get_record( $in ) )
5304 foreach $key ( @{ $options->{ "keys" } } )
5306 if ( $record->{ $key } )
5308 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
5312 &put_record( $record, $out ) if not $options->{ "no_stream" };
5315 $fh = &write_stream( $options->{ "data_out" } );
5317 foreach $key ( @{ $options->{ "keys" } } )
5319 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
5322 &put_record( $max_record, $fh );
5330 # Martin A. Hansen, February 2008.
5332 # Determine the minimum values of given keys.
5334 my ( $in, # handle to in stream
5335 $out, # handle to out stream
5336 $options, # options hash
5341 my ( $record, $key, $fh, %min_hash, $min_record );
5343 while ( $record = &get_record( $in ) )
5345 foreach $key ( @{ $options->{ "keys" } } )
5347 if ( defined $record->{ $key } )
5349 if ( exists $min_hash{ $key } ) {
5350 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
5352 $min_hash{ $key } = $record->{ $key };
5357 &put_record( $record, $out ) if not $options->{ "no_stream" };
5360 $fh = &write_stream( $options->{ "data_out" } );
5362 foreach $key ( @{ $options->{ "keys" } } )
5364 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5367 &put_record( $min_record, $fh );
5373 sub script_upload_to_ucsc
5375 # Martin A. Hansen, August 2007.
5377 # Calculate the mean of values of given keys.
5379 my ( $in, # handle to in stream
5380 $out, # handle to out stream
5381 $options, # options hash
5386 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_in, $fh_out, $i, $first, $format, $args, $type, $columns, $append, %fh_hash,
5387 $chr, $beg, $end, $block, $line, $max, $beg_block, $entry, $q_id, $clones );
5389 $options->{ "short_label" } ||= $options->{ 'table' };
5390 $options->{ "long_label" } ||= $options->{ 'table' };
5391 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5392 $options->{ "priority" } ||= 1;
5393 $options->{ "visibility" } ||= "pack";
5394 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5395 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5397 $file = "$BP_TMP/ucsc_upload.tmp";
5405 if ( $options->{ 'wiggle' } )
5407 $options->{ "visibility" } = "full";
5409 while ( $record = &get_record( $in ) )
5411 &put_record( $record, $out ) if not $options->{ "no_stream" };
5413 $record->{ "CHR" } = $record->{ "S_ID" } if not defined $record->{ "CHR" };
5414 $record->{ "CHR_BEG" } = $record->{ "S_BEG" } if not defined $record->{ "CHR_BEG" };
5415 $record->{ "CHR_END" } = $record->{ "S_END" } if not defined $record->{ "CHR_END" };
5417 $fh_hash{ $record->{ "CHR" } } = &Maasha::Common::write_open( "$BP_TMP/$record->{ 'CHR' }" ) if not exists $fh_hash{ $record->{ "CHR" } };
5419 $fh_out = $fh_hash{ $record->{ "CHR" } };
5421 &Maasha::UCSC::bed_put_entry( $record, $fh_out, 5 );
5424 map { close $_ } keys %fh_hash;
5426 $fh_out = &Maasha::Common::write_open( $file );
5428 foreach $chr ( sort keys %fh_hash )
5430 &Maasha::Common::run( "bedSort", "$BP_TMP/$chr $BP_TMP/$chr" );
5432 $fh_in = &Maasha::Common::read_open( "$BP_TMP/$chr" );
5436 while ( $entry = &Maasha::UCSC::bed_get_entry( $fh_in, 5 ) )
5438 $chr = $entry->{ 'CHR' };
5439 $beg = $entry->{ 'CHR_BEG' };
5440 $end = $entry->{ 'CHR_END' };
5441 $q_id = $entry->{ 'Q_ID' };
5443 if ( $q_id =~ /_(\d+)$/ ) {
5453 &Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5458 for ( $i = $beg - $beg_block; $i < ( $beg - $beg_block ) + ( $end - $beg ); $i++ ) {
5459 $block->[ $i ] += $clones;
5462 $max = &Maasha::Calc::max( $max, $end );
5471 for ( $i = 0; $i < ( $end - $beg ); $i++ ) {
5472 $block->[ $i ] += $clones;
5479 &Maasha::UCSC::fixedstep_put_entry( $chr, $beg_block, $block, $fh_out );
5481 unlink "$BP_TMP/$chr";
5486 $wig_file = "$options->{ 'table' }.wig";
5487 $wib_file = "$options->{ 'table' }.wib";
5489 $wib_dir = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'database' }/wib";
5491 &Maasha::Common::dir_create_if_not_exists( $wib_dir );
5493 # &Maasha::Common::run( "wigEncode", "$file $wig_file $wib_file > /dev/null 2>&1" );
5495 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5496 &Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5506 $fh_out = &Maasha::Common::write_open( $file );
5508 while ( $record = &get_record( $in ) )
5510 &put_record( $record, $out ) if not $options->{ "no_stream" };
5512 if ( $record->{ "REC_TYPE" } eq "PSL" )
5514 &Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5515 &Maasha::UCSC::psl_put_entry( $record, $fh_out );
5519 $format = "PSL" if not $format;
5521 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5523 # chrom chromStart chromEnd name score strand size secStr conf
5525 print $fh_out join ( "\t",
5527 $record->{ "CHR_BEG" },
5528 $record->{ "CHR_END" } + 1,
5529 $record->{ "Q_ID" },
5530 $record->{ "SCORE" },
5531 $record->{ "STRAND" },
5532 $record->{ "SIZE" },
5533 $record->{ "SEC_STRUCT" },
5534 $record->{ "CONF" },
5537 $format = "BED_SS" if not $format;
5539 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5541 &Maasha::UCSC::bed_put_entry( $record, $fh_out, $record->{ "BED_COLS" } );
5543 $format = "BED" if not $format;
5544 $columns = $record->{ "BED_COLS" } if not $columns;
5546 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5548 &Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5550 $format = "BED" if not $format;
5551 $columns = 6 if not $columns;
5553 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5555 $record->{ "CHR" } = $record->{ "S_ID" };
5556 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5557 $record->{ "CHR_END" } = $record->{ "S_END" };
5558 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5560 $format = "BED" if not $format;
5561 $columns = 6 if not $columns;
5563 &Maasha::UCSC::bed_put_entry( $record, $fh_out );
5565 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5567 $record->{ "CHR" } = $record->{ "S_ID" };
5568 $record->{ "CHR_BEG" } = $record->{ "S_BEG" };
5569 $record->{ "CHR_END" } = $record->{ "S_END" };
5570 $record->{ "SCORE" } = $record->{ "SCORE" } || 999;
5571 $record->{ "SCORE" } = int( $record->{ "SCORE" } );
5573 $format = "BED" if not $format;
5574 $columns = 6 if not $columns;
5576 &Maasha::UCSC::bed_put_entry( $record, $fh_out, 6 );
5579 if ( $i == $options->{ "chunk_size" } )
5583 if ( $format eq "BED" ) {
5584 &Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5585 } elsif ( $format eq "PSL" ) {
5586 &Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5595 $fh_out = &Maasha::Common::write_open( $file );
5604 if ( exists $options->{ "database" } and $options->{ "table" } )
5606 if ( $format eq "BED" )
5608 $type = "bed $columns";
5610 &Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5612 elsif ( $format eq "BED_SS" )
5614 $options->{ "sec_struct" } = 1;
5616 $type = "sec_struct";
5618 &Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5620 elsif ( $format eq "PSL" )
5624 &Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5626 elsif ( $format eq "WIGGLE" )
5630 &Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5635 &Maasha::UCSC::update_my_tracks( $options, $type );
5640 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5645 # Martin A. Hansen, July 2007.
5647 # Opens a stream to STDIN or a file,
5649 my ( $path, # path - OPTIONAL
5652 # Returns filehandle.
5656 if ( not -t STDIN ) {
5657 $fh = &Maasha::Common::read_stdin();
5658 } elsif ( not $path ) {
5659 # &Maasha::Common::error( qq(no data stream) );
5661 $fh = &Maasha::Common::read_open( $path );
5664 # $fh->autoflush(1) if $fh;
5672 # Martin A. Hansen, August 2007.
5674 # Opens a stream to STDOUT or a file.
5676 my ( $path, # path - OPTIONAL
5677 $gzip, # compress data - OPTIONAL
5680 # Returns filehandle.
5685 $fh = &Maasha::Common::write_open( $path, $gzip );
5687 $fh = &Maasha::Common::write_stdout();
5696 # Martin A. Hansen, July 2007.
5698 # Reads one record at a time and converts that record
5699 # to a Perl data structure (a hash) which is returned.
5704 # Returns data structure.
5706 my ( $block, @lines, $line, $key, $value, %record );
5708 local $/ = "\n---\n";
5714 return if not defined $block;
5716 @lines = split "\n", $block;
5718 foreach $line ( @lines )
5720 ( $key, $value ) = split ": ", $line;
5722 $record{ $key } = $value;
5725 return wantarray ? %record : \%record;
5731 # Martin A. Hansen, July 2007.
5733 # Given a Perl datastructure (a hash ref) emits this to STDOUT or a filehandle.
5735 my ( $data, # data structure
5736 $fh, # file handle - OPTIONAL
5741 if ( scalar keys %{ $data } )
5745 map { print $fh "$_: $data->{ $_ }\n" } keys %{ $data };
5750 map { print "$_: $data->{ $_ }\n" } keys %{ $data };
5761 # Martin A. Hansen, November 2007.
5763 # Extracts files from an explicit GetOpt::Long argument
5764 # allowing for the use of glob. E.g.
5765 # --data_in=test.fna
5766 # --data_in=test.fna,test2.fna
5768 # --data_in=test.fna,/dir/*.fna
5770 my ( $option, # option from GetOpt::Long
5775 my ( $elem, @files );
5777 foreach $elem ( split ",", $option )
5781 } elsif ( $elem =~ /\*/ ) {
5782 push @files, glob( $elem );
5786 return wantarray ? @files : \@files;
5792 # Martin A. Hansen, April 2008.
5794 # Removes temporary directory and exits gracefully.
5795 # This subroutine is meant to be run always as the last
5796 # thing even if a script is dies or is interrupted
5799 my ( $sig, # signal from the %SIG
5802 # print STDERR "signal->$sig<-\n";
5810 if ( $sig =~ /MAASHA_ERROR/ ) {
5811 print STDERR "\nProgram '$script' had an error" . " - Please wait for temporary data to be removed\n";
5812 } elsif ( $sig eq "INT" ) {
5813 print STDERR "\nProgram '$script' interrupted (ctrl-c was pressed)" . " - Please wait for temporary data to be removed\n";
5814 } elsif ( $sig eq "TERM" ) {
5815 print STDERR "\nProgram '$script' terminated (someone used kill?)" . " - Please wait for temporary data to be removed\n";
5817 print STDERR "\nProgram '$script' died->$sig" . " - Please wait for temporary data to be removed\n";
5820 # This is a really bad solution, potentially, anyone can include this module and set
5821 # the BP_TMP to point at any dir and thus take out the machine !!!
5823 &Maasha::Common::dir_remove( $BP_TMP );
5832 # This is a really bad solution, potentially, anyone can include this module and set
5833 # the BP_TMP to point at any dir and thus take out the machine !!!
5835 &Maasha::Common::dir_remove( $BP_TMP );
5839 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5846 sub script_read_soft
5848 # Martin A. Hansen, December 2007.
5851 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
5853 my ( $in, # handle to in stream
5854 $out, # handle to out stream
5855 $options, # options hash
5860 my ( $data_in, $file, $num, $records, $record );
5862 while ( $record = &get_record( $in ) ) {
5863 &put_record( $record, $out );
5868 foreach $file ( @{ $options->{ "files" } } )
5870 $records = &Maasha::NCBI::soft_parse( $file );
5872 foreach $record ( @{ $records } )
5874 &put_record( $record, $out );
5876 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
5884 close $data_in if $data_in;