1 package Maasha::BioRun;
4 # Copyright (C) 2007-2009 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 that contains Biopieces which are run.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Getopt::Long qw( :config bundling );
35 use Time::HiRes qw( gettimeofday );
36 use Storable qw( dclone );
37 use Maasha::Biopieces;
46 use Maasha::Stockholm;
52 use Maasha::UCSC::BED;
53 use Maasha::UCSC::Wiggle;
62 use vars qw( @ISA @EXPORT_OK );
66 @ISA = qw( Exporter );
74 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> GLOBALS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
77 my ( $script, $BP_TMP );
79 $script = Maasha::Common::get_scriptname();
80 $BP_TMP = Maasha::Common::get_tmpdir();
83 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RUN SCRIPT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
86 run_script( $script );
89 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SUBROUTINES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
94 # Martin A. Hansen, August 2007.
96 # Run a specific script.
98 my ( $script, # script name
103 my ( $t0, $t1, $options, $in, $out );
105 Maasha::Biopieces::log_biopiece();
107 $t0 = gettimeofday();
109 $options = get_options( $script );
111 $options->{ "SCRIPT" } = $script;
113 $script = "print_usage" if ( -t STDIN and keys %{ $options } <= 1 or $options->{ 'help' } );
115 $in = Maasha::Biopieces::read_stream( $options->{ "stream_in" } );
116 $out = Maasha::Biopieces::write_stream( $options->{ "stream_out" } );
118 if ( $script eq "print_usage" ) { script_print_usage( $in, $out, $options ) }
119 elsif ( $script eq "read_psl" ) { script_read_psl( $in, $out, $options ) }
120 elsif ( $script eq "read_bed" ) { script_read_bed( $in, $out, $options ) }
121 elsif ( $script eq "read_fixedstep" ) { script_read_fixedstep( $in, $out, $options ) }
122 elsif ( $script eq "read_blast_tab" ) { script_read_blast_tab( $in, $out, $options ) }
123 elsif ( $script eq "read_embl" ) { script_read_embl( $in, $out, $options ) }
124 elsif ( $script eq "read_stockholm" ) { script_read_stockholm( $in, $out, $options ) }
125 elsif ( $script eq "read_phastcons" ) { script_read_phastcons( $in, $out, $options ) }
126 elsif ( $script eq "read_soft" ) { script_read_soft( $in, $out, $options ) }
127 elsif ( $script eq "read_gff" ) { script_read_gff( $in, $out, $options ) }
128 elsif ( $script eq "read_2bit" ) { script_read_2bit( $in, $out, $options ) }
129 elsif ( $script eq "read_solexa" ) { script_read_solexa( $in, $out, $options ) }
130 elsif ( $script eq "read_solid" ) { script_read_solid( $in, $out, $options ) }
131 elsif ( $script eq "read_mysql" ) { script_read_mysql( $in, $out, $options ) }
132 elsif ( $script eq "read_ucsc_config" ) { script_read_ucsc_config( $in, $out, $options ) }
133 elsif ( $script eq "assemble_tag_contigs" ) { script_assemble_tag_contigs( $in, $out, $options ) }
134 elsif ( $script eq "length_seq" ) { script_length_seq( $in, $out, $options ) }
135 elsif ( $script eq "uppercase_seq" ) { script_uppercase_seq( $in, $out, $options ) }
136 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
137 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
138 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
139 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
140 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
141 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
142 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
143 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
144 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
145 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
146 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
147 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
148 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
149 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
150 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
151 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
152 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
153 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
154 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
155 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
156 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
157 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
158 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
159 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
160 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
161 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
162 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
163 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
164 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
165 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
166 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
167 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
168 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
169 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
170 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
171 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
172 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
173 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
174 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
175 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
176 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
177 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
178 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
179 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
180 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
181 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
182 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
183 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
184 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
185 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
186 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
187 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
188 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
189 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
190 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
191 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
192 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
193 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
194 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
195 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
196 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
198 close $in if defined $in;
201 $t1 = gettimeofday();
203 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
209 # Martin A. Hansen, February 2008.
211 # Gets options from commandline and checks these vigerously.
213 my ( $script, # name of script
218 my ( %options, @options, $opt, @genomes, $real );
220 if ( $script eq "print_usage" )
226 elsif ( $script eq "read_psl" )
233 elsif ( $script eq "read_bed" )
242 elsif ( $script eq "read_fixedstep" )
249 elsif ( $script eq "read_blast_tab" )
256 elsif ( $script eq "read_embl" )
266 elsif ( $script eq "read_stockholm" )
273 elsif ( $script eq "read_phastcons" )
284 elsif ( $script eq "read_soft" )
292 elsif ( $script eq "read_gff" )
299 elsif ( $script eq "read_2bit" )
307 elsif ( $script eq "read_solexa" )
316 elsif ( $script eq "read_solid" )
324 elsif ( $script eq "read_mysql" )
333 elsif ( $script eq "read_ucsc_config" )
340 elsif ( $script eq "assemble_tag_contigs" )
346 elsif ( $script eq "calc_fixedstep" )
352 elsif ( $script eq "length_seq" )
359 elsif ( $script eq "oligo_freq" )
366 elsif ( $script eq "create_weight_matrix" )
372 elsif ( $script eq "transliterate_seq" )
380 elsif ( $script eq "transliterate_vals" )
389 elsif ( $script eq "translate_seq" )
395 elsif ( $script eq "get_genome_align" )
406 elsif ( $script eq "get_genome_phastcons" )
417 elsif ( $script eq "split_seq" )
424 elsif ( $script eq "split_bed" )
431 elsif ( $script eq "tile_seq" )
438 elsif ( $script eq "invert_align" )
444 elsif ( $script eq "patscan_seq" )
455 elsif ( $script eq "blat_seq" )
467 elsif ( $script eq "soap_seq" )
478 elsif ( $script eq "match_seq" )
485 elsif ( $script eq "create_vmatch_index" )
493 elsif ( $script eq "write_align" )
503 elsif ( $script eq "write_bed" )
513 elsif ( $script eq "write_psl" )
521 elsif ( $script eq "write_fixedstep" )
529 elsif ( $script eq "write_2bit" )
537 elsif ( $script eq "write_solid" )
546 elsif ( $script eq "write_ucsc_config" )
553 elsif ( $script eq "plot_seqlogo" )
560 elsif ( $script eq "plot_phastcons_profiles" )
575 elsif ( $script eq "analyze_vals" )
582 elsif ( $script eq "head_records" )
588 elsif ( $script eq "remove_keys" )
595 elsif ( $script eq "remove_adaptor" )
604 elsif ( $script eq "remove_mysql_tables" )
615 elsif ( $script eq "remove_ucsc_tracks" )
627 elsif ( $script eq "rename_keys" )
633 elsif ( $script eq "uniq_vals" )
640 elsif ( $script eq "merge_vals" )
647 elsif ( $script eq "merge_records" )
654 elsif ( $script eq "compute" )
660 elsif ( $script eq "count_records" )
667 elsif ( $script eq "random_records" )
673 elsif ( $script eq "sort_records" )
680 elsif ( $script eq "count_vals" )
686 elsif ( $script eq "plot_histogram" )
699 elsif ( $script eq "plot_lendist" )
711 elsif ( $script eq "plot_chrdist" )
722 elsif ( $script eq "plot_karyogram" )
731 elsif ( $script eq "plot_matches" )
743 elsif ( $script eq "length_vals" )
749 elsif ( $script eq "sum_vals" )
757 elsif ( $script eq "mean_vals" )
765 elsif ( $script eq "median_vals" )
773 elsif ( $script eq "max_vals" )
781 elsif ( $script eq "min_vals" )
789 elsif ( $script eq "upload_to_ucsc" )
813 # print STDERR Dumper( \@options );
820 # print STDERR Dumper( \%options );
822 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
823 return wantarray ? %options : \%options;
826 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
827 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
828 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
829 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
830 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
831 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
832 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
833 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
834 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
835 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
837 # ---- check arguments ----
839 if ( $options{ 'data_in' } )
841 $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
843 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
846 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
848 # print STDERR Dumper( \%options );
850 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
852 foreach $opt ( keys %options )
854 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
856 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
858 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
860 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
862 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
864 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
866 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
868 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
870 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
872 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
874 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
876 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
878 elsif ( $opt eq "genome" )
880 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
881 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
883 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
884 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
887 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
889 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
891 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
893 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
895 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
897 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
899 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
901 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
903 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
905 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
909 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
910 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
911 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
912 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
913 Maasha::Common::error( qq(no --genome specified) ) if $script =~ /get_genome_align|get_genome_phastcons|blat_seq|plot_phastcons_profiles|plot_karyogram/ and not $options{ "genome" };
914 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
915 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" };
917 if ( $script eq "upload_to_ucsc" )
919 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
920 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
923 return wantarray ? %options : \%options;
927 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
930 sub script_print_usage
932 # Martin A. Hansen, January 2008.
934 # Retrieves usage information from file and
935 # prints this nicely formatted.
937 my ( $in, # handle to in stream
938 $out, # handle to out stream
939 $options, # options hash
944 my ( $file, $wiki, $lines );
946 if ( $options->{ 'data_in' } ) {
947 $file = $options->{ 'data_in' };
949 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
952 $wiki = Maasha::Gwiki::gwiki_read( $file );
954 ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
956 if ( not $options->{ "help" } ) {
957 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
960 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
962 print STDERR "$_\n" foreach @{ $lines };
970 # Martin A. Hansen, August 2007.
972 # Read psl table from stream or file.
974 my ( $in, # handle to in stream
975 $out, # handle to out stream
976 $options, # options hash
981 my ( $record, $file, $data_in, $num );
983 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
984 Maasha::Biopieces::put_record( $record, $out );
989 foreach $file ( @{ $options->{ "files" } } )
991 $data_in = Maasha::Common::read_open( $file );
993 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
995 Maasha::Biopieces::put_record( $record, $out );
997 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1009 # Martin A. Hansen, August 2007.
1011 # Read bed table from stream or file.
1013 my ( $in, # handle to in stream
1014 $out, # handle to out stream
1015 $options, # options hash
1020 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1022 $cols = $options->{ 'cols' }->[ 0 ];
1024 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1025 Maasha::Biopieces::put_record( $record, $out );
1030 foreach $file ( @{ $options->{ "files" } } )
1032 $data_in = Maasha::Common::read_open( $file );
1034 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1036 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1038 Maasha::Biopieces::put_record( $record, $out );
1040 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1050 close $data_in if $data_in;
1054 sub script_read_fixedstep
1056 # Martin A. Hansen, Juli 2008.
1058 # Read fixedstep wiggle format from stream or file.
1060 my ( $in, # handle to in stream
1061 $out, # handle to out stream
1062 $options, # options hash
1067 my ( $file, $record, $entry, $data_in, $num );
1069 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1070 Maasha::Biopieces::put_record( $record, $out );
1075 foreach $file ( @{ $options->{ "files" } } )
1077 $data_in = Maasha::Common::read_open( $file );
1079 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1081 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1083 Maasha::Biopieces::put_record( $record, $out );
1085 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1095 close $data_in if $data_in;
1099 sub script_read_blast_tab
1101 # Martin A. Hansen, September 2007.
1103 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1105 my ( $in, # handle to in stream
1106 $out, # handle to out stream
1107 $options, # options hash
1112 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1114 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1115 Maasha::Biopieces::put_record( $record, $out );
1120 foreach $file ( @{ $options->{ "files" } } )
1122 $data_in = Maasha::Common::read_open( $file );
1124 while ( $line = <$data_in> )
1128 next if $line =~ /^#/;
1130 @fields = split /\t/, $line;
1132 $record->{ "REC_TYPE" } = "BLAST";
1133 $record->{ "Q_ID" } = $fields[ 0 ];
1134 $record->{ "S_ID" } = $fields[ 1 ];
1135 $record->{ "IDENT" } = $fields[ 2 ];
1136 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1137 $record->{ "MISMATCHES" } = $fields[ 4 ];
1138 $record->{ "GAPS" } = $fields[ 5 ];
1139 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1140 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1141 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1142 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1143 $record->{ "E_VAL" } = $fields[ 10 ];
1144 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1146 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1148 $record->{ "STRAND" } = '-';
1150 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1154 $record->{ "STRAND" } = '+';
1157 Maasha::Biopieces::put_record( $record, $out );
1159 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1169 close $data_in if $data_in;
1173 sub script_read_embl
1175 # Martin A. Hansen, August 2007.
1179 my ( $in, # handle to in stream
1180 $out, # handle to out stream
1181 $options, # options hash
1186 my ( %options2, $file, $data_in, $num, $entry, $record );
1188 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1189 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1190 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1192 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1193 Maasha::Biopieces::put_record( $record, $out );
1198 foreach $file ( @{ $options->{ "files" } } )
1200 $data_in = Maasha::Common::read_open( $file );
1202 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1204 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1206 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1208 $record_copy = dclone $record;
1210 delete $record_copy->{ "FT" };
1212 Maasha::Biopieces::put_record( $record_copy, $out );
1214 delete $record_copy->{ "SEQ" };
1216 foreach $feat ( keys %{ $record->{ "FT" } } )
1218 $record_copy->{ "FEAT_TYPE" } = $feat;
1220 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1222 foreach $qual ( keys %{ $feat2 } )
1224 $qual_val = join "; ", @{ $feat2->{ $qual } };
1229 $record_copy->{ $qual } = $qual_val;
1232 Maasha::Biopieces::put_record( $record_copy, $out );
1236 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1246 close $data_in if $data_in;
1250 sub script_read_stockholm
1252 # Martin A. Hansen, August 2007.
1254 # Read Stockholm format.
1256 my ( $in, # handle to in stream
1257 $out, # handle to out stream
1258 $options, # options hash
1263 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1265 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1266 Maasha::Biopieces::put_record( $record, $out );
1271 foreach $file ( @{ $options->{ "files" } } )
1273 $data_in = Maasha::Common::read_open( $file );
1275 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1277 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1281 foreach $key ( keys %{ $record->{ "GF" } } ) {
1282 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1285 $record_anno->{ "ALIGN" } = $num;
1287 Maasha::Biopieces::put_record( $record_anno, $out );
1289 foreach $seq ( @{ $record->{ "ALIGN" } } )
1291 undef $record_align;
1294 SEQ_NAME => $seq->[ 0 ],
1298 Maasha::Biopieces::put_record( $record_align, $out );
1301 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1311 close $data_in if $data_in;
1315 sub script_read_phastcons
1317 # Martin A. Hansen, December 2007.
1319 # Read PhastCons format.
1321 my ( $in, # handle to in stream
1322 $out, # handle to out stream
1323 $options, # options hash
1328 my ( $data_in, $file, $num, $entry, @records, $record );
1330 $options->{ "min" } ||= 10;
1331 $options->{ "dist" } ||= 25;
1332 $options->{ "threshold" } ||= 0.8;
1333 $options->{ "gap" } ||= 5;
1335 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1336 Maasha::Biopieces::put_record( $record, $out );
1341 foreach $file ( @{ $options->{ "files" } } )
1343 $data_in = Maasha::Common::read_open( $file );
1345 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1347 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1349 foreach $record ( @records )
1351 $record->{ "REC_TYPE" } = "BED";
1352 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1354 Maasha::Biopieces::put_record( $record, $out );
1356 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1367 close $data_in if $data_in;
1371 sub script_read_soft
1373 # Martin A. Hansen, December 2007.
1376 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1378 my ( $in, # handle to in stream
1379 $out, # handle to out stream
1380 $options, # options hash
1385 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1387 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1388 Maasha::Biopieces::put_record( $record, $out );
1393 foreach $file ( @{ $options->{ "files" } } )
1395 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1397 $soft_index = Maasha::NCBI::soft_index_file( $file );
1399 $fh = Maasha::Common::read_open( $file );
1401 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1403 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1405 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1407 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1409 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1411 foreach $sample ( @samples )
1414 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1416 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1418 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1420 foreach $record ( @{ $records } )
1422 Maasha::Biopieces::put_record( $record, $out );
1424 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1429 $old_end = $sample->{ "LINE_END" };
1437 close $data_in if $data_in;
1444 # Martin A. Hansen, February 2008.
1447 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1449 my ( $in, # handle to in stream
1450 $out, # handle to out stream
1451 $options, # options hash
1456 my ( $data_in, $file, $fh, $num, $record, $entry );
1458 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1459 Maasha::Biopieces::put_record( $record, $out );
1464 foreach $file ( @{ $options->{ "files" } } )
1466 $fh = Maasha::Common::read_open( $file );
1468 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1470 Maasha::Biopieces::put_record( $entry, $out );
1472 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1482 close $data_in if $data_in;
1486 sub script_read_2bit
1488 # Martin A. Hansen, March 2008.
1490 # Read sequences from 2bit file.
1492 my ( $in, # handle to in stream
1493 $out, # handle to out stream
1494 $options, # options hash
1499 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1501 $mask = 1 if not $options->{ "no_mask" };
1503 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1504 Maasha::Biopieces::put_record( $record, $out );
1509 foreach $file ( @{ $options->{ "files" } } )
1511 $data_in = Maasha::Common::read_open( $file );
1513 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1515 foreach $line ( @{ $toc } )
1517 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1518 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1519 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1521 Maasha::Biopieces::put_record( $record, $out );
1523 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1533 close $data_in if $data_in;
1537 sub script_read_solexa
1539 # Martin A. Hansen, March 2008.
1541 # Read Solexa sequence reads from file.
1543 my ( $in, # handle to in stream
1544 $out, # handle to out stream
1545 $options, # options hash
1550 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1552 $options->{ "format" } ||= "octal";
1553 $options->{ "quality" } ||= 20;
1555 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1556 Maasha::Biopieces::put_record( $record, $out );
1561 foreach $file ( @{ $options->{ "files" } } )
1563 $data_in = Maasha::Common::read_open( $file );
1565 if ( $options->{ "format" } eq "octal" )
1567 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1569 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1571 Maasha::Biopieces::put_record( $record, $out );
1573 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1580 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1582 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1584 Maasha::Biopieces::put_record( $record, $out );
1586 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1597 close $data_in if $data_in;
1601 sub script_read_solid
1603 # Martin A. Hansen, April 2008.
1605 # Read Solid sequence from file.
1607 my ( $in, # handle to in stream
1608 $out, # handle to out stream
1609 $options, # options hash
1614 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1616 $options->{ "quality" } ||= 15;
1618 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1619 Maasha::Biopieces::put_record( $record, $out );
1624 foreach $file ( @{ $options->{ "files" } } )
1626 $data_in = Maasha::Common::read_open( $file );
1628 while ( $line = <$data_in> )
1632 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1634 @scores = split /,/, $seq_qual;
1635 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1637 for ( $i = 0; $i < @seqs; $i++ ) {
1638 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1642 REC_TYPE => 'SOLID',
1643 SEQ_NAME => $seq_name,
1645 SEQ_QUAL => join( ";", @scores ),
1646 SEQ_LEN => length $seq_cs,
1647 SEQ => join( "", @seqs ),
1648 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1651 Maasha::Biopieces::put_record( $record, $out );
1653 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1663 close $data_in if $data_in;
1667 sub script_read_mysql
1669 # Martin A. Hansen, May 2008.
1671 # Read a MySQL query into stream.
1673 my ( $in, # handle to in stream
1674 $out, # handle to out stream
1675 $options, # options hash
1680 my ( $record, $dbh, $results );
1682 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1683 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1685 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1686 Maasha::Biopieces::put_record( $record, $out );
1689 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1691 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1693 Maasha::SQL::disconnect( $dbh );
1695 map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1699 sub script_read_ucsc_config
1701 # Martin A. Hansen, November 2008.
1703 # Read track entries from UCSC Genome Browser '.ra' files.
1705 my ( $in, # handle to in stream
1706 $out, # handle to out stream
1707 $options, # options hash
1712 my ( $record, $file, $data_in, $entry, $num );
1714 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1715 Maasha::Biopieces::put_record( $record, $out );
1720 foreach $file ( @{ $options->{ "files" } } )
1722 $data_in = Maasha::Common::read_open( $file );
1724 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
1726 $record->{ 'REC_TYPE' } = "UCSC Config";
1728 Maasha::Biopieces::put_record( $record, $out );
1730 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1740 close $data_in if $data_in;
1744 sub script_assemble_tag_contigs
1746 # Martin A. Hansen, November 2008.
1748 # Assemble tags from the stream into
1751 # The current implementation is quite
1752 # slow because of heavy use of temporary
1755 my ( $in, # handle to in stream
1756 $out, # handle to out stream
1757 $options, # options hash
1762 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
1764 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
1765 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
1766 $cols = 6; # we only need the first 6 BED columns
1768 while ( $record = Maasha::Biopieces::get_record( $in ) )
1770 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
1772 $strand = $record->{ 'STRAND' } || '+';
1774 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
1780 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
1784 foreach $chr ( sort keys %{ $file_hash } )
1786 $bed_file = $file_hash->{ $chr };
1787 $tag_file = "$bed_file.tc";
1789 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
1791 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
1793 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
1795 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
1796 Maasha::Biopieces::put_record( $record, $out );
1808 sub script_length_seq
1810 # Martin A. Hansen, August 2007.
1812 # Determine the length of sequences in stream.
1814 my ( $in, # handle to in stream
1815 $out, # handle to out stream
1816 $options, # options hash
1821 my ( $record, $total );
1823 while ( $record = Maasha::Biopieces::get_record( $in ) )
1825 if ( $record->{ "SEQ" } )
1827 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1828 $total += $record->{ "SEQ_LEN" };
1831 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1834 Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1838 sub script_analyze_seq
1840 # Martin A. Hansen, August 2007.
1842 # Analyze sequence composition of sequences in stream.
1844 my ( $in, # handle to in stream
1845 $out, # handle to out stream
1850 my ( $record, $analysis );
1852 while ( $record = Maasha::Biopieces::get_record( $in ) )
1854 if ( $record->{ "SEQ" } )
1856 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1858 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1861 Maasha::Biopieces::put_record( $record, $out );
1866 sub script_analyze_tags
1868 # Martin A. Hansen, August 2008.
1870 # Analyze sequence tags in stream.
1872 my ( $in, # handle to in stream
1873 $out, # handle to out stream
1878 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
1880 while ( $record = Maasha::Biopieces::get_record( $in ) )
1882 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
1884 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
1888 $len_hash{ length( $record->{ "SEQ" } ) }++;
1889 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
1892 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
1894 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
1898 $len_hash{ $record->{ "BED_LEN" } }++;
1899 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
1904 foreach $key ( sort { $a <=> $b } keys %len_hash )
1906 $tag_record->{ "TAG_LEN" } = $key;
1907 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
1908 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
1910 Maasha::Biopieces::put_record( $tag_record, $out );
1915 sub script_complexity_seq
1917 # Martin A. Hansen, May 2008.
1919 # Generates an index calculated as the most common di-residue over
1920 # the sequence length for all sequences in stream.
1922 my ( $in, # handle to in stream
1923 $out, # handle to out stream
1928 my ( $record, $index );
1930 while ( $record = Maasha::Biopieces::get_record( $in ) )
1932 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1934 Maasha::Biopieces::put_record( $record, $out );
1939 sub script_oligo_freq
1941 # Martin A. Hansen, August 2007.
1943 # Determine the length of sequences in stream.
1945 my ( $in, # handle to in stream
1946 $out, # handle to out stream
1947 $options, # options hash
1952 my ( $record, %oligos, @freq_table );
1954 $options->{ "word_size" } ||= 7;
1956 while ( $record = Maasha::Biopieces::get_record( $in ) )
1958 if ( $record->{ "SEQ" } )
1960 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
1962 if ( not $options->{ "all" } )
1964 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1966 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1972 Maasha::Biopieces::put_record( $record, $out );
1975 if ( $options->{ "all" } )
1977 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
1979 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
1984 sub script_create_weight_matrix
1986 # Martin A. Hansen, August 2007.
1988 # Creates a weight matrix from an alignmnet.
1990 my ( $in, # handle to in stream
1991 $out, # handle to out stream
1992 $options, # options hash
1997 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2001 while ( $record = Maasha::Biopieces::get_record( $in ) )
2003 if ( $record->{ "SEQ" } )
2005 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2007 $res = substr $record->{ "SEQ" }, $i, 1;
2009 $freq_hash{ $i }{ $res }++;
2010 $res_hash{ $res } = 1;
2017 Maasha::Biopieces::put_record( $record, $out );
2021 foreach $res ( sort keys %res_hash )
2025 $record->{ "V0" } = $res;
2027 for ( $i = 0; $i < keys %freq_hash; $i++ )
2029 $freq = $freq_hash{ $i }{ $res } || 0;
2031 if ( $options->{ "percent" } ) {
2032 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2035 $record->{ "V" . ( $i + 1 ) } = $freq;
2038 Maasha::Biopieces::put_record( $record, $out );
2043 sub script_calc_bit_scores
2045 # Martin A. Hansen, March 2007.
2047 # Calculates the bit scores for each position from an alignmnet in the stream.
2049 my ( $in, # handle to in stream
2050 $out, # handle to out stream
2055 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2059 while ( $record = Maasha::Biopieces::get_record( $in ) )
2061 if ( $record->{ "SEQ" } )
2063 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2065 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2067 $res = substr $record->{ "SEQ" }, $i, 1;
2069 next if $res =~ /-|_|~|\./;
2071 $freq_hash{ $i }{ $res }++;
2078 Maasha::Biopieces::put_record( $record, $out );
2084 if ( $type eq "protein" ) {
2090 for ( $i = 0; $i < keys %freq_hash; $i++ )
2092 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2094 $bit_diff = $bit_max - $bit_height;
2096 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2099 Maasha::Biopieces::put_record( $record, $out );
2103 sub script_calc_fixedstep
2105 # Martin A. Hansen, September 2008.
2107 # Calculates fixedstep entries from data in the stream.
2109 my ( $in, # handle to in stream
2110 $out, # handle to out stream
2111 $options, # options hash
2116 my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2118 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2119 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2121 while ( $record = Maasha::Biopieces::get_record( $in ) )
2123 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2124 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2130 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2134 foreach $chr ( sort keys %{ $file_hash } )
2136 $bed_file = $file_hash->{ $chr };
2138 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );
2140 #$fixedstep_file = "$bed_file.fixedstep";
2142 # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2144 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2146 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2148 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2149 Maasha::Biopieces::put_record( $record, $out );
2156 unlink $fixedstep_file;
2161 sub script_remove_indels
2163 # Martin A. Hansen, August 2007.
2165 # Remove indels from sequences in stream.
2167 my ( $in, # handle to in stream
2168 $out, # handle to out stream
2175 while ( $record = Maasha::Biopieces::get_record( $in ) )
2177 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2179 Maasha::Biopieces::put_record( $record, $out );
2184 sub script_transliterate_seq
2186 # Martin A. Hansen, August 2007.
2188 # Transliterate chars from sequence in record.
2190 my ( $in, # handle to in stream
2191 $out, # handle to out stream
2192 $options, # options hash
2197 my ( $record, $search, $replace, $delete );
2199 $search = $options->{ "search" } || "";
2200 $replace = $options->{ "replace" } || "";
2201 $delete = $options->{ "delete" } || "";
2203 while ( $record = Maasha::Biopieces::get_record( $in ) )
2205 if ( $record->{ "SEQ" } )
2207 if ( $search and $replace ) {
2208 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2209 } elsif ( $delete ) {
2210 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2214 Maasha::Biopieces::put_record( $record, $out );
2219 sub script_transliterate_vals
2221 # Martin A. Hansen, April 2008.
2223 # Transliterate chars from values in record.
2225 my ( $in, # handle to in stream
2226 $out, # handle to out stream
2227 $options, # options hash
2232 my ( $record, $search, $replace, $delete, $key );
2234 $search = $options->{ "search" } || "";
2235 $replace = $options->{ "replace" } || "";
2236 $delete = $options->{ "delete" } || "";
2238 while ( $record = Maasha::Biopieces::get_record( $in ) )
2240 foreach $key ( @{ $options->{ "keys" } } )
2242 if ( exists $record->{ $key } )
2244 if ( $search and $replace ) {
2245 eval "\$record->{ $key } =~ tr/$search/$replace/";
2246 } elsif ( $delete ) {
2247 eval "\$record->{ $key } =~ tr/$delete//d";
2252 Maasha::Biopieces::put_record( $record, $out );
2257 sub script_translate_seq
2259 # Martin A. Hansen, February 2008.
2261 # Translate DNA sequence into protein sequence.
2263 my ( $in, # handle to in stream
2264 $out, # handle to out stream
2265 $options, # options hash
2270 my ( $record, $frame, %new_record );
2272 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2274 while ( $record = Maasha::Biopieces::get_record( $in ) )
2276 if ( $record->{ "SEQ" } )
2278 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2280 foreach $frame ( @{ $options->{ "frames" } } )
2282 %new_record = %{ $record };
2284 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2285 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2286 $new_record{ "FRAME" } = $frame;
2288 Maasha::Biopieces::put_record( \%new_record, $out );
2294 Maasha::Biopieces::put_record( $record, $out );
2300 sub script_get_genome_align
2302 # Martin A. Hansen, April 2008.
2304 # Gets a subalignment from a multiple genome alignment.
2306 my ( $in, # handle to in stream
2307 $out, # handle to out stream
2308 $options, # options hash
2313 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2315 $options->{ "strand" } ||= "+";
2319 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2321 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2323 $beg = $options->{ "beg" } - 1;
2325 if ( $options->{ "end" } ) {
2326 $end = $options->{ "end" };
2327 } elsif ( $options->{ "len" } ) {
2328 $end = $beg + $options->{ "len" };
2331 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2333 foreach $entry ( @{ $align } )
2335 $entry->{ "CHR" } = $record->{ "CHR" };
2336 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2337 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2338 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2339 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2340 $entry->{ "SCORE" } = $record->{ "SCORE" };
2342 Maasha::Biopieces::put_record( $entry, $out );
2346 while ( $record = Maasha::Biopieces::get_record( $in ) )
2348 if ( $record->{ "REC_TYPE" } eq "BED" )
2350 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2352 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
2354 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
2356 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2358 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2360 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2362 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2365 foreach $entry ( @{ $align } )
2367 $entry->{ "CHR" } = $record->{ "CHR" };
2368 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2369 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2370 $entry->{ "STRAND" } = $record->{ "STRAND" };
2371 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2372 $entry->{ "SCORE" } = $record->{ "SCORE" };
2374 Maasha::Biopieces::put_record( $entry, $out );
2382 sub script_get_genome_phastcons
2384 # Martin A. Hansen, February 2008.
2386 # Get phastcons scores from genome intervals.
2388 my ( $in, # handle to in stream
2389 $out, # handle to out stream
2390 $options, # options hash
2395 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2397 $options->{ "flank" } ||= 0;
2399 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2400 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2402 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2403 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2405 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2407 $options->{ "beg" } -= 1; # request is 1-based
2408 $options->{ "end" } -= 1; # request is 1-based
2410 if ( $options->{ "len" } ) {
2411 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2414 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2416 $record->{ "CHR" } = $options->{ "chr" };
2417 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2418 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2420 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2421 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2423 Maasha::Biopieces::put_record( $record, $out );
2426 while ( $record = Maasha::Biopieces::get_record( $in ) )
2428 if ( $record->{ "REC_TYPE" } eq "BED" )
2430 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2432 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2434 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2436 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2438 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2441 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2442 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2444 Maasha::Biopieces::put_record( $record, $out );
2447 close $fh_phastcons if $fh_phastcons;
2453 # Martin A. Hansen, December 2007.
2455 # Folds sequences in stream into secondary structures.
2457 my ( $in, # handle to in stream
2458 $out, # handle to out stream
2463 my ( $record, $type, $struct, $index );
2465 while ( $record = Maasha::Biopieces::get_record( $in ) )
2467 if ( $record->{ "SEQ" } )
2470 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2473 if ( $type ne "protein" )
2475 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2476 $record->{ "SEC_STRUCT" } = $struct;
2477 $record->{ "FREE_ENERGY" } = $index;
2478 $record->{ "SCORE" } = abs int $index;
2479 $record->{ "SIZE" } = length $struct;
2480 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2484 Maasha::Biopieces::put_record( $record, $out );
2489 sub script_split_seq
2491 # Martin A. Hansen, August 2007.
2493 # Split a sequence in stream into words.
2495 my ( $in, # handle to in stream
2496 $out, # handle to out stream
2497 $options, # options hash
2502 my ( $record, $new_record, $i, $subseq, %lookup );
2504 $options->{ "word_size" } ||= 7;
2506 while ( $record = Maasha::Biopieces::get_record( $in ) )
2508 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2510 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2512 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2514 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2516 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2517 $new_record->{ "SEQ" } = $subseq;
2519 Maasha::Biopieces::put_record( $new_record, $out );
2521 $lookup{ $subseq } = 1;
2525 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2526 $new_record->{ "SEQ" } = $subseq;
2528 Maasha::Biopieces::put_record( $new_record, $out );
2534 Maasha::Biopieces::put_record( $record, $out );
2540 sub script_split_bed
2542 # Martin A. Hansen, June 2008.
2544 # Split a BED record into overlapping windows.
2546 my ( $in, # handle to in stream
2547 $out, # handle to out stream
2548 $options, # options hash
2553 my ( $record, $new_record, $i );
2555 $options->{ "window_size" } ||= 20;
2556 $options->{ "step_size" } ||= 1;
2558 while ( $record = Maasha::Biopieces::get_record( $in ) )
2560 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2562 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2564 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2566 $new_record->{ "REC_TYPE" } = "BED";
2567 $new_record->{ "CHR" } = $record->{ "CHR" };
2568 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2569 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2570 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2571 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2572 $new_record->{ "SCORE" } = $record->{ "SCORE" };
2573 $new_record->{ "STRAND" } = $record->{ "STRAND" };
2575 Maasha::Biopieces::put_record( $new_record, $out );
2580 Maasha::Biopieces::put_record( $record, $out );
2588 # Martin A. Hansen, February 2008.
2590 # Using the first sequence in stream as reference, tile
2591 # all subsequent sequences based on pairwise alignments.
2593 my ( $in, # handle to in stream
2594 $out, # handle to out stream
2595 $options, # options hash
2600 my ( $record, $first, $ref_entry, @entries );
2604 while ( $record = Maasha::Biopieces::get_record( $in ) )
2606 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2610 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2616 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2621 Maasha::Biopieces::put_record( $record, $out );
2625 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2627 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2631 sub script_invert_align
2633 # Martin A. Hansen, February 2008.
2635 # Inverts an alignment showing only non-mathing residues
2636 # using the first sequence as reference.
2638 my ( $in, # handle to in stream
2639 $out, # handle to out stream
2640 $options, # options hash
2645 my ( $record, @entries );
2647 while ( $record = Maasha::Biopieces::get_record( $in ) )
2649 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2651 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2655 Maasha::Biopieces::put_record( $record, $out );
2659 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2661 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2665 sub script_patscan_seq
2667 # Martin A. Hansen, August 2007.
2669 # Locates patterns in sequences using scan_for_matches.
2671 my ( $in, # handle to in stream
2672 $out, # handle to out stream
2673 $options, # options hash
2678 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2680 if ( $options->{ "patterns" } ) {
2681 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2682 } elsif ( -f $options->{ "patterns_in" } ) {
2683 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2686 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2688 push @args, "-c" if $options->{ "comp" };
2689 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
2690 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2692 $seq_file = "$BP_TMP/patscan.seq";
2693 $pat_file = "$BP_TMP/patscan.pat";
2694 $out_file = "$BP_TMP/patscan.out";
2696 $fh_out = Maasha::Common::write_open( $seq_file );
2700 while ( $record = Maasha::Biopieces::get_record( $in ) )
2702 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2704 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2706 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2708 $head_hash{ $i } = $record->{ "SEQ_NAME" };
2716 $arg = join " ", @args;
2717 $arg .= " -p" if $type eq "protein";
2719 foreach $pattern ( @{ $patterns } )
2721 $fh_out = Maasha::Common::write_open( $pat_file );
2723 print $fh_out "$pattern\n";
2727 if ( $options->{ 'genome' } ) {
2728 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2729 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2731 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2732 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2735 $fh_in = Maasha::Common::read_open( $out_file );
2737 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2739 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2741 if ( $options->{ 'genome' } )
2743 $result->{ "CHR" } = $result->{ "S_ID" };
2744 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
2745 $result->{ "CHR_END" } = $result->{ "S_END" };
2747 delete $result->{ "S_ID" };
2748 delete $result->{ "S_BEG" };
2749 delete $result->{ "S_END" };
2753 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2756 Maasha::Biopieces::put_record( $result, $out );
2770 # Martin A. Hansen, August 2007.
2772 # BLATs sequences in stream against a given genome.
2774 my ( $in, # handle to in stream
2775 $out, # handle to out stream
2776 $options, # options hash
2781 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
2783 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2785 $options->{ 'tile_size' } ||= 11;
2786 $options->{ 'one_off' } ||= 0;
2787 $options->{ 'min_identity' } ||= 90;
2788 $options->{ 'min_score' } ||= 0;
2789 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
2791 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
2792 $blat_args .= " -oneOff=$options->{ 'one_off' }";
2793 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
2794 $blat_args .= " -minScore=$options->{ 'min_score' }";
2795 $blat_args .= " -stepSize=$options->{ 'step_size' }";
2796 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
2798 $query_file = "$BP_TMP/blat.seq";
2800 $fh_out = Maasha::Common::write_open( $query_file );
2802 while ( $record = Maasha::Biopieces::get_record( $in ) )
2804 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2806 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
2807 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
2810 Maasha::Biopieces::put_record( $record, $out );
2815 $blat_args .= " -t=dnax" if $type eq "protein";
2816 $blat_args .= " -q=$type";
2818 $result_file = "$BP_TMP/blat.psl";
2820 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
2824 $entries = Maasha::UCSC::psl_get_entries( $result_file );
2826 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $entries };
2828 unlink $result_file;
2834 # Martin A. Hansen, July 2008.
2836 # soap sequences in stream against a given file or genome.
2838 my ( $in, # handle to in stream
2839 $out, # handle to out stream
2840 $options, # options hash
2845 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
2847 $options->{ "seed_size" } ||= 10;
2848 $options->{ "mismatches" } ||= 2;
2849 $options->{ "gap_size" } ||= 0;
2850 $options->{ "cpus" } ||= 1;
2852 if ( $options->{ "genome" } ) {
2853 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2856 $tmp_in = "$BP_TMP/soap_query.seq";
2857 $tmp_out = "$BP_TMP/soap.result";
2859 $fh_out = Maasha::Common::write_open( $tmp_in );
2863 while ( $record = Maasha::Biopieces::get_record( $in ) )
2865 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2867 Maasha::Fasta::put_entry( $entry, $fh_out );
2872 Maasha::Biopieces::put_record( $record, $out );
2880 "-s $options->{ 'seed_size' }",
2883 "-v $options->{ 'mismatches' }",
2884 "-g $options->{ 'gap_size' }",
2885 "-p $options->{ 'cpus' }",
2886 "-d $options->{ 'in_file' }",
2890 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
2892 Maasha::Common::run( "soap", $args, 1 );
2896 $fh_out = Maasha::Common::read_open( $tmp_out );
2900 while ( $line = <$fh_out> )
2904 @fields = split /\t/, $line;
2906 $record->{ "REC_TYPE" } = "SOAP";
2907 $record->{ "Q_ID" } = $fields[ 0 ];
2908 $record->{ "SCORE" } = $fields[ 3 ];
2909 $record->{ "STRAND" } = $fields[ 6 ];
2910 $record->{ "S_ID" } = $fields[ 7 ];
2911 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
2912 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
2914 Maasha::Biopieces::put_record( $record, $out );
2924 sub script_match_seq
2926 # Martin A. Hansen, August 2007.
2928 # BLATs sequences in stream against a given genome.
2930 my ( $in, # handle to in stream
2931 $out, # handle to out stream
2932 $options, # options hash
2937 my ( $record, @entries, $results );
2939 $options->{ "word_size" } ||= 20;
2940 $options->{ "direction" } ||= "both";
2942 while ( $record = Maasha::Biopieces::get_record( $in ) )
2944 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2945 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2948 Maasha::Biopieces::put_record( $record, $out );
2951 if ( @entries == 1 )
2953 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
2955 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
2957 elsif ( @entries == 2 )
2959 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
2961 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
2966 sub script_create_vmatch_index
2968 # Martin A. Hansen, January 2008.
2970 # Create a vmatch index from sequences in the stream.
2972 my ( $in, # handle to in stream
2973 $out, # handle to out stream
2974 $options, # options hash
2979 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
2981 if ( $options->{ "index_name" } )
2983 $file_tmp = $options->{ 'index_name' };
2984 $fh_tmp = Maasha::Common::write_open( $file_tmp );
2987 while ( $record = Maasha::Biopieces::get_record( $in ) )
2989 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
2991 Maasha::Fasta::put_entry( $entry, $fh_tmp );
2993 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
2996 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
2999 if ( $options->{ "index_name" } )
3003 if ( $type eq "protein" ) {
3004 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3006 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3014 sub script_write_align
3016 # Martin A. Hansen, August 2007.
3018 # Write pretty alignments aligned sequences in stream.
3020 my ( $in, # handle to in stream
3021 $out, # handle to out stream
3022 $options, # options hash
3027 my ( $fh, $record, @entries );
3029 $fh = write_stream( $options->{ "data_out" } ) ;
3031 while ( $record = Maasha::Biopieces::get_record( $in ) )
3033 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3034 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3037 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3040 if ( scalar( @entries ) == 2 ) {
3041 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3042 } elsif ( scalar ( @entries ) > 2 ) {
3043 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3050 sub script_write_bed
3052 # Martin A. Hansen, August 2007.
3054 # Write BED format for the UCSC genome browser using records in stream.
3056 my ( $in, # handle to in stream
3057 $out, # handle to out stream
3058 $options, # options hash
3063 my ( $cols, $fh, $record, $bed_entry, $new_record );
3065 $cols = $options->{ 'cols' }->[ 0 ];
3067 $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
3069 while ( $record = Maasha::Biopieces::get_record( $in ) )
3071 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
3073 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
3074 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
3077 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3084 sub script_write_psl
3086 # Martin A. Hansen, August 2007.
3088 # Write PSL output from stream.
3090 my ( $in, # handle to in stream
3091 $out, # handle to out stream
3092 $options, # options hash
3097 my ( $fh, $record, @output, $first );
3101 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3103 while ( $record = Maasha::Biopieces::get_record( $in ) )
3105 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3107 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3109 Maasha::UCSC::psl_put_header( $fh ) if $first;
3110 Maasha::UCSC::psl_put_entry( $record, $fh );
3119 sub script_write_fixedstep
3121 # Martin A. Hansen, Juli 2008.
3123 # Write fixedStep entries from recrods in the stream.
3125 my ( $in, # handle to in stream
3126 $out, # handle to out stream
3127 $options, # options hash
3132 my ( $fh, $record, $entry );
3134 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3136 while ( $record = Maasha::Biopieces::get_record( $in ) )
3138 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3139 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
3142 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3149 sub script_write_2bit
3151 # Martin A. Hansen, March 2008.
3153 # Write sequence entries from stream in 2bit format.
3155 my ( $in, # handle to in stream
3156 $out, # handle to out stream
3157 $options, # options hash
3162 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
3164 $mask = 1 if not $options->{ "no_mask" };
3166 $tmp_file = "$BP_TMP/write_2bit.fna";
3167 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3169 $fh_out = write_stream( $options->{ "data_out" } );
3171 while ( $record = Maasha::Biopieces::get_record( $in ) )
3173 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
3174 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3177 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3182 $fh_in = Maasha::Common::read_open( $tmp_file );
3184 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3193 sub script_write_solid
3195 # Martin A. Hansen, April 2008.
3197 # Write di-base encoded Solid sequence from entries in stream.
3199 my ( $in, # handle to in stream
3200 $out, # handle to out stream
3201 $options, # options hash
3206 my ( $record, $fh, $entry );
3208 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3210 while ( $record = Maasha::Biopieces::get_record( $in ) )
3212 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3214 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
3216 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3219 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3226 sub script_write_ucsc_config
3228 # Martin A. Hansen, November 2008.
3230 # Write UCSC Genome Broser configuration (.ra file type) from
3231 # records in the stream.
3233 my ( $in, # handle to in stream
3234 $out, # handle to out stream
3235 $options, # options hash
3240 my ( $record, $fh );
3242 $fh = write_stream( $options->{ "data_out" } );
3244 while ( $record = Maasha::Biopieces::get_record( $in ) )
3246 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
3248 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3255 sub script_plot_seqlogo
3257 # Martin A. Hansen, August 2007.
3259 # Calculates and writes a sequence logo for alignments.
3261 my ( $in, # handle to in stream
3262 $out, # handle to out stream
3263 $options, # options hash
3268 my ( $record, @entries, $logo, $fh );
3270 while ( $record = Maasha::Biopieces::get_record( $in ) )
3272 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3273 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3276 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3279 $logo = Maasha::Plot::seq_logo( \@entries );
3281 $fh = write_stream( $options->{ "data_out" } );
3289 sub script_plot_phastcons_profiles
3291 # Martin A. Hansen, January 2008.
3293 # Plots PhastCons profiles.
3295 my ( $in, # handle to in stream
3296 $out, # handle to out stream
3297 $options, # options hash
3302 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3304 $options->{ "title" } ||= "PhastCons Profiles";
3306 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3307 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3309 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3310 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3312 while ( $record = Maasha::Biopieces::get_record( $in ) )
3314 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3316 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
3317 $record->{ "CHR_BEG" },
3318 $record->{ "CHR_END" },
3319 $options->{ "flank" } );
3321 push @{ $AoA }, [ @{ $scores } ];
3324 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3327 Maasha::UCSC::phastcons_normalize( $AoA );
3329 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3330 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3332 $AoA = Maasha::Matrix::matrix_flip( $AoA );
3334 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3336 $fh = write_stream( $options->{ "data_out" } );
3338 print $fh "$_\n" foreach @{ $plot };
3344 sub script_analyze_bed
3346 # Martin A. Hansen, March 2008.
3348 # Analyze BED entries in stream.
3350 my ( $in, # handle to in stream
3351 $out, # handle to out stream
3352 $options, # options hash
3359 while ( $record = Maasha::Biopieces::get_record( $in ) )
3361 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3363 Maasha::Biopieces::put_record( $record, $out );
3368 sub script_analyze_vals
3370 # Martin A. Hansen, August 2007.
3372 # Analyze values for given keys in stream.
3374 my ( $in, # handle to in stream
3375 $out, # handle to out stream
3376 $options, # options hash
3381 my ( $record, $key, @keys, %key_hash, $analysis, $len );
3383 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3385 while ( $record = Maasha::Biopieces::get_record( $in ) )
3387 foreach $key ( keys %{ $record } )
3389 next if $options->{ "keys" } and not exists $key_hash{ $key };
3391 $analysis->{ $key }->{ "COUNT" }++;
3393 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
3395 $analysis->{ $key }->{ "TYPE" } = "num";
3396 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
3397 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3398 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
3402 $len = length $record->{ $key };
3404 $analysis->{ $key }->{ "TYPE" } = "alph";
3405 $analysis->{ $key }->{ "SUM" } += $len;
3406 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3407 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
3411 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3414 foreach $key ( keys %{ $analysis } )
3416 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
3417 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
3420 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
3430 if ( $options->{ "keys" } ) {
3431 @keys = @{ $options->{ "keys" } };
3433 @keys = keys %{ $analysis };
3436 foreach $key ( @keys )
3438 $keys .= sprintf "% 15s", $key;
3439 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
3440 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
3441 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
3442 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
3443 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
3444 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
3447 print $out "$keys\n";
3448 print $out "$types\n";
3449 print $out "$counts\n";
3450 print $out "$mins\n";
3451 print $out "$maxs\n";
3452 print $out "$sums\n";
3453 print $out "$means\n";
3457 sub script_head_records
3459 # Martin A. Hansen, August 2007.
3461 # Display the first sequences in stream.
3463 my ( $in, # handle to in stream
3464 $out, # handle to out stream
3465 $options, # options hash
3470 my ( $record, $count );
3472 $options->{ "num" } ||= 10;
3476 while ( $record = Maasha::Biopieces::get_record( $in ) )
3480 Maasha::Biopieces::put_record( $record, $out );
3482 last if $count == $options->{ "num" };
3487 sub script_remove_keys
3489 # Martin A. Hansen, August 2007.
3491 # Remove keys from stream.
3493 my ( $in, # handle to in stream
3494 $out, # handle to out stream
3495 $options, # options hash
3500 my ( $record, $new_record );
3502 while ( $record = Maasha::Biopieces::get_record( $in ) )
3504 if ( $options->{ "keys" } )
3506 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
3508 elsif ( $options->{ "save_keys" } )
3510 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
3512 $record = $new_record;
3515 Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
3520 sub script_remove_adaptor
3522 # Martin A. Hansen, August 2008.
3524 # Find and remove adaptor from sequences in the stream.
3526 my ( $in, # handle to in stream
3527 $out, # handle to out stream
3528 $options, # options hash
3533 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
3535 $options->{ "remove" } ||= "after";
3537 $max_mismatch = $options->{ "mismatches" } || 0;
3538 $offset = $options->{ "offset" };
3540 if ( not defined $offset ) {
3546 $adaptor = uc $options->{ "adaptor" };
3547 $adaptor_len = length $adaptor;
3549 while ( $record = Maasha::Biopieces::get_record( $in ) )
3551 if ( $record->{ "SEQ" } )
3553 $seq = uc $record->{ "SEQ" };
3554 $seq_len = length $seq;
3556 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
3558 $record->{ "ADAPTOR_POS" } = $pos;
3560 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
3562 if ( $options->{ "remove" } eq "after" )
3564 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
3565 $record->{ "SEQ_LEN" } = $pos;
3569 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
3570 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3574 Maasha::Biopieces::put_record( $record, $out );
3578 Maasha::Biopieces::put_record( $record, $out );
3584 sub script_remove_mysql_tables
3586 # Martin A. Hansen, November 2008.
3588 # Remove MySQL tables from values in stream.
3590 my ( $in, # handle to in stream
3591 $out, # handle to out stream
3592 $options, # options hash
3597 my ( $record, %table_hash, $dbh, $table );
3599 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
3600 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
3602 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
3604 while ( $record = Maasha::Biopieces::get_record( $in ) )
3606 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3608 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3611 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3613 foreach $table ( sort keys %table_hash )
3615 if ( Maasha::SQL::table_exists( $dbh, $table ) )
3617 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3618 Maasha::SQL::delete_table( $dbh, $table );
3619 print STDERR "done.\n" if $options->{ 'verbose' };
3623 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
3627 Maasha::SQL::disconnect( $dbh );
3631 sub script_remove_ucsc_tracks
3633 # Martin A. Hansen, November 2008.
3635 # Remove track from MySQL tables and config file.
3637 my ( $in, # handle to in stream
3638 $out, # handle to out stream
3639 $options, # options hash
3644 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
3646 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
3647 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
3648 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
3650 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
3652 while ( $record = Maasha::Biopieces::get_record( $in ) )
3654 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3656 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3659 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
3661 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
3662 push @tracks, $track;
3667 foreach $track ( @tracks )
3669 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
3670 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
3672 push @new_tracks, $track;
3676 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
3678 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
3680 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
3684 # ---- locate track in database ----
3686 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3688 foreach $track ( sort keys %track_hash )
3690 if ( Maasha::SQL::table_exists( $dbh, $track ) )
3692 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3693 Maasha::SQL::delete_table( $dbh, $track );
3694 print STDERR "done.\n" if $options->{ 'verbose' };
3698 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
3702 Maasha::SQL::disconnect( $dbh );
3704 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
3708 sub script_rename_keys
3710 # Martin A. Hansen, August 2007.
3712 # Rename keys in stream.
3714 my ( $in, # handle to in stream
3715 $out, # handle to out stream
3716 $options, # options hash
3723 while ( $record = Maasha::Biopieces::get_record( $in ) )
3725 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3727 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
3729 delete $record->{ $options->{ "keys" }->[ 0 ] };
3732 Maasha::Biopieces::put_record( $record, $out );
3737 sub script_uniq_vals
3739 # Martin A. Hansen, August 2007.
3741 # Find unique values in stream.
3743 my ( $in, # handle to in stream
3744 $out, # handle to out stream
3745 $options, # options hash
3750 my ( %hash, $record );
3752 while ( $record = Maasha::Biopieces::get_record( $in ) )
3754 if ( $record->{ $options->{ "key" } } )
3756 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3758 Maasha::Biopieces::put_record( $record, $out );
3760 $hash{ $record->{ $options->{ "key" } } } = 1;
3762 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3764 Maasha::Biopieces::put_record( $record, $out );
3768 $hash{ $record->{ $options->{ "key" } } } = 1;
3773 Maasha::Biopieces::put_record( $record, $out );
3779 sub script_merge_vals
3781 # Martin A. Hansen, August 2007.
3783 # Rename keys in stream.
3785 my ( $in, # handle to in stream
3786 $out, # handle to out stream
3787 $options, # options hash
3792 my ( $record, @join, $i );
3794 $options->{ "delimit" } ||= '_';
3796 while ( $record = Maasha::Biopieces::get_record( $in ) )
3798 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3800 @join = $record->{ $options->{ "keys" }->[ 0 ] };
3802 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
3803 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
3806 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
3809 Maasha::Biopieces::put_record( $record, $out );
3814 sub script_merge_records
3816 # Martin A. Hansen, July 2008.
3818 # Merges records in the stream based on identical values of two given keys.
3820 my ( $in, # handle to in stream
3821 $out, # handle to out stream
3822 $options, # options hash
3827 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
3828 $num1, $num2, $num, $cmp, $i );
3830 $merge = $options->{ "merge" } || "AandB";
3832 $file1 = "$BP_TMP/merge_records1.tmp";
3833 $file2 = "$BP_TMP/merge_records2.tmp";
3835 $fh1 = Maasha::Common::write_open( $file1 );
3836 $fh2 = Maasha::Common::write_open( $file2 );
3838 $key1 = $options->{ "keys" }->[ 0 ];
3839 $key2 = $options->{ "keys" }->[ 1 ];
3841 $num = $key2 =~ s/n$//;
3845 while ( $record = Maasha::Biopieces::get_record( $in ) )
3847 if ( exists $record->{ $key1 } )
3850 @vals1 = $record->{ $key1 };
3852 delete $record->{ $key1 };
3854 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
3856 print $fh1 join( "\t", @vals1 ), "\n";
3860 elsif ( exists $record->{ $key2 } )
3863 @vals2 = $record->{ $key2 };
3865 delete $record->{ $key2 };
3867 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
3869 print $fh2 join( "\t", @vals2 ), "\n";
3880 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3881 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3885 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
3886 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
3889 $fh1 = Maasha::Common::read_open( $file1 );
3890 $fh2 = Maasha::Common::read_open( $file2 );
3892 @vals1 = Maasha::Common::get_fields( $fh1 );
3893 @vals2 = Maasha::Common::get_fields( $fh2 );
3895 while ( $num1 > 0 and $num2 > 0 )
3900 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
3902 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
3907 if ( $merge =~ /^(AorB|AnotB)$/ )
3909 for ( $i = 0; $i < @keys1; $i++ ) {
3910 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3913 Maasha::Biopieces::put_record( $record, $out );
3916 @vals1 = Maasha::Common::get_fields( $fh1 );
3921 if ( $merge =~ /^(BorA|BnotA)$/ )
3923 for ( $i = 0; $i < @keys2; $i++ ) {
3924 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3927 Maasha::Biopieces::put_record( $record, $out );
3930 @vals2 = Maasha::Common::get_fields( $fh2 );
3935 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
3937 for ( $i = 0; $i < @keys1; $i++ ) {
3938 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3941 for ( $i = 1; $i < @keys2; $i++ ) {
3942 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3945 Maasha::Biopieces::put_record( $record, $out );
3948 @vals1 = Maasha::Common::get_fields( $fh1 );
3949 @vals2 = Maasha::Common::get_fields( $fh2 );
3961 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
3965 for ( $i = 0; $i < @keys1; $i++ ) {
3966 $record->{ $keys1[ $i ] } = $vals1[ $i ];
3969 Maasha::Biopieces::put_record( $record, $out );
3972 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
3976 for ( $i = 0; $i < @keys2; $i++ ) {
3977 $record->{ $keys2[ $i ] } = $vals2[ $i ];
3980 Maasha::Biopieces::put_record( $record, $out );
3987 # Martin A. Hansen, August 2007.
3989 # Evaluate extression for records in stream.
3991 my ( $in, # handle to in stream
3992 $out, # handle to out stream
3993 $options, # options hash
3998 my ( $record, $eval_key, @keys, $eval_val );
4000 while ( $record = Maasha::Biopieces::get_record( $in ) )
4002 if ( $options->{ "eval" } )
4004 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
4011 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
4013 @keys = grep { exists $record->{ $_ } } @keys;
4016 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
4018 $record->{ $eval_key } = eval "$eval_val";
4019 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
4023 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
4027 Maasha::Biopieces::put_record( $record, $out );
4034 # Martin A. Hansen, June 2008.
4038 my ( $in, # handle to in stream
4039 $out, # handle to out stream
4040 $options, # options hash
4045 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4047 while ( $record = Maasha::Biopieces::get_record( $in ) )
4051 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4053 push @rows, $record->{ $key };
4057 push @matrix, [ @rows ];
4062 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4064 foreach $row ( @matrix )
4066 for ( $i = 0; $i < @{ $row }; $i++ ) {
4067 $record->{ "V$i" } = $row->[ $i ];
4070 Maasha::Biopieces::put_record( $record, $out );
4075 sub script_count_records
4077 # Martin A. Hansen, August 2007.
4079 # Count records in stream.
4081 my ( $in, # handle to in stream
4082 $out, # handle to out stream
4083 $options, # options hash
4088 my ( $record, $count, $result, $fh, $line );
4092 if ( $options->{ "no_stream" } )
4094 while ( $line = <$in> )
4098 $count++ if $line eq "---";
4103 while ( $record = Maasha::Biopieces::get_record( $in ) )
4105 Maasha::Biopieces::put_record( $record, $out );
4111 $result = { "RECORDS_COUNT" => $count };
4113 $fh = write_stream( $options->{ "data_out" } );
4115 Maasha::Biopieces::put_record( $result, $fh );
4121 sub script_random_records
4123 # Martin A. Hansen, August 2007.
4125 # Pick a number or random records from stream.
4127 my ( $in, # handle to in stream
4128 $out, # handle to out stream
4129 $options, # options hash
4134 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4136 $options->{ "num" } ||= 10;
4138 $tmp_file = "$BP_TMP/random_records.tmp";
4140 $fh_out = Maasha::Common::write_open( $tmp_file );
4144 while ( $record = Maasha::Biopieces::get_record( $in ) )
4146 Maasha::Biopieces::put_record( $record, $fh_out );
4156 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4158 while ( $i < $options->{ "num" } )
4160 $rand = int( rand( $count ) );
4162 if ( not exists $rand_hash{ $rand } )
4164 $rand_hash{ $rand } = 1;
4166 $max = $rand if $rand > $max;
4172 $fh_in = Maasha::Common::read_open( $tmp_file );
4176 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4178 Maasha::Biopieces::put_record( $record, $out ) if exists $rand_hash{ $count };
4180 last if $count == $max;
4191 sub script_sort_records
4193 # Martin A. Hansen, August 2007.
4195 # Sort to sort records according to keys.
4197 my ( $in, # handle to in stream
4198 $out, # handle to out stream
4199 $options, # options hash
4204 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4206 foreach $key ( @{ $options->{ "keys" } } )
4208 if ( $key =~ s/n$// ) {
4209 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4211 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4215 $sort_str = join " or ", @sort_cmd;
4216 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4218 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
4219 push @records, $record;
4222 @records = sort $sort_sub @records;
4224 if ( $options->{ "reverse" } )
4226 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4227 Maasha::Biopieces::put_record( $records[ $i ], $out );
4232 for ( $i = 0; $i < scalar @records; $i++ ) {
4233 Maasha::Biopieces::put_record( $records[ $i ], $out );
4239 sub script_count_vals
4241 # Martin A. Hansen, August 2007.
4243 # Count records in stream.
4245 my ( $in, # handle to in stream
4246 $out, # handle to out stream
4247 $options, # options hash
4252 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4254 $tmp_file = "$BP_TMP/count_cache.tmp";
4256 $fh_out = Maasha::Common::write_open( $tmp_file );
4261 while ( $record = Maasha::Biopieces::get_record( $in ) )
4263 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4265 push @records, $record;
4267 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4269 map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
4276 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4287 $fh_in = Maasha::Common::read_open( $tmp_file );
4289 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4291 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4293 Maasha::Biopieces::put_record( $record, $out );
4295 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4303 foreach $record ( @records )
4305 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4307 Maasha::Biopieces::put_record( $record, $out );
4314 sub script_plot_histogram
4316 # Martin A. Hansen, September 2007.
4318 # Plot a simple histogram for a given key using GNU plot.
4320 my ( $in, # handle to in stream
4321 $out, # handle to out stream
4322 $options, # options hash
4327 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4329 $options->{ "title" } ||= "Histogram";
4330 $options->{ "sort" } ||= "num";
4332 while ( $record = Maasha::Biopieces::get_record( $in ) )
4334 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4336 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4339 if ( $options->{ "sort" } eq "num" ) {
4340 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4342 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4345 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4347 $fh = write_stream( $options->{ "data_out" } );
4349 print $fh "$_\n" foreach @{ $result };
4355 sub script_plot_lendist
4357 # Martin A. Hansen, August 2007.
4359 # Plot length distribution using GNU plot.
4361 my ( $in, # handle to in stream
4362 $out, # handle to out stream
4363 $options, # options hash
4368 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4370 $options->{ "title" } ||= "Length Distribution";
4372 while ( $record = Maasha::Biopieces::get_record( $in ) )
4374 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4376 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4379 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4381 for ( $i = 0; $i < $max; $i++ ) {
4382 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4385 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4387 $fh = write_stream( $options->{ "data_out" } );
4389 print $fh "$_\n" foreach @{ $result };
4395 sub script_plot_chrdist
4397 # Martin A. Hansen, August 2007.
4399 # Plot chromosome distribution using GNU plot.
4401 my ( $in, # handle to in stream
4402 $out, # handle to out stream
4403 $options, # options hash
4408 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4410 $options->{ "title" } ||= "Chromosome Distribution";
4412 while ( $record = Maasha::Biopieces::get_record( $in ) )
4414 if ( $record->{ "CHR" } ) { # generic
4415 $data_hash{ $record->{ "CHR" } }++;
4416 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
4417 $data_hash{ $record->{ "S_ID" } }++;
4418 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
4419 $data_hash{ $record->{ "S_ID" } }++;
4420 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
4421 $data_hash{ $record->{ "S_ID" } }++;
4424 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4427 foreach $elem ( keys %data_hash )
4431 $sort_key =~ s/chr//i;
4433 $sort_key =~ s/^X(.*)/99$1/;
4434 $sort_key =~ s/^Y(.*)/99$1/;
4435 $sort_key =~ s/^Z(.*)/999$1/;
4436 $sort_key =~ s/^M(.*)/9999$1/;
4437 $sort_key =~ s/^U(.*)/99999$1/;
4439 $count = $sort_key =~ tr/_//;
4441 $sort_key =~ s/_.*/"999999" x $count/ex;
4443 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4446 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4448 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4450 $fh = write_stream( $options->{ "data_out" } );
4452 print $fh "$_\n" foreach @{ $result };
4458 sub script_plot_karyogram
4460 # Martin A. Hansen, August 2007.
4462 # Plot hits on karyogram.
4464 my ( $in, # handle to in stream
4465 $out, # handle to out stream
4466 $options, # options hash
4471 my ( %options, $record, @data, $fh, $result, %data_hash );
4473 $options->{ "genome" } ||= "human";
4474 $options->{ "feat_color" } ||= "black";
4476 while ( $record = Maasha::Biopieces::get_record( $in ) )
4478 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4480 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4483 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4486 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4488 $fh = write_stream( $options->{ "data_out" } );
4496 sub script_plot_matches
4498 # Martin A. Hansen, August 2007.
4500 # Plot matches in 2D generating a dotplot.
4502 my ( $in, # handle to in stream
4503 $out, # handle to out stream
4504 $options, # options hash
4509 my ( $record, @data, $fh, $result, %data_hash );
4511 $options->{ "direction" } ||= "both";
4513 while ( $record = Maasha::Biopieces::get_record( $in ) )
4515 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4516 push @data, $record;
4519 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4522 $options->{ "title" } ||= "plot_matches";
4523 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4524 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4526 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4528 $fh = write_stream( $options->{ "data_out" } );
4530 print $fh "$_\n" foreach @{ $result };
4536 sub script_length_vals
4538 # Martin A. Hansen, August 2007.
4540 # Determine the length of the value for given keys.
4542 my ( $in, # handle to in stream
4543 $out, # handle to out stream
4544 $options, # options hash
4549 my ( $record, $key );
4551 while ( $record = Maasha::Biopieces::get_record( $in ) )
4553 foreach $key ( @{ $options->{ "keys" } } )
4555 if ( $record->{ $key } ) {
4556 $record->{ $key . "_LEN" } = length $record->{ $key };
4560 Maasha::Biopieces::put_record( $record, $out );
4567 # Martin A. Hansen, August 2007.
4569 # Calculates the sums for values of given keys.
4571 my ( $in, # handle to in stream
4572 $out, # handle to out stream
4573 $options, # options hash
4578 my ( $record, $key, %sum_hash, $fh );
4580 while ( $record = Maasha::Biopieces::get_record( $in ) )
4582 foreach $key ( @{ $options->{ "keys" } } )
4584 if ( $record->{ $key } ) {
4585 $sum_hash{ $key } += $record->{ $key };
4589 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4592 $fh = write_stream( $options->{ "data_out" } );
4594 foreach $key ( @{ $options->{ "keys" } } ) {
4595 Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
4602 sub script_mean_vals
4604 # Martin A. Hansen, August 2007.
4606 # Calculate the mean of values of given keys.
4608 my ( $in, # handle to in stream
4609 $out, # handle to out stream
4610 $options, # options hash
4615 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
4617 while ( $record = Maasha::Biopieces::get_record( $in ) )
4619 foreach $key ( @{ $options->{ "keys" } } )
4621 if ( $record->{ $key } )
4623 $sum_hash{ $key } += $record->{ $key };
4624 $count_hash{ $key }++;
4628 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4631 $fh = write_stream( $options->{ "data_out" } );
4633 foreach $key ( @{ $options->{ "keys" } } )
4635 if ( $count_hash{ $key } ) {
4636 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
4641 Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
4648 sub script_median_vals
4650 # Martin A. Hansen, March 2008.
4652 # Calculate the median values of given keys.
4654 my ( $in, # handle to in stream
4655 $out, # handle to out stream
4656 $options, # options hash
4661 my ( $record, $key, %median_hash, $median, $fh );
4663 while ( $record = Maasha::Biopieces::get_record( $in ) )
4665 foreach $key ( @{ $options->{ "keys" } } ) {
4666 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
4669 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4672 $fh = write_stream( $options->{ "data_out" } );
4674 foreach $key ( @{ $options->{ "keys" } } )
4676 if ( $median_hash{ $key } ) {
4677 $median = Maasha::Calc::median( $median_hash{ $key } );
4682 Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
4691 # Martin A. Hansen, February 2008.
4693 # Determine the maximum values of given keys.
4695 my ( $in, # handle to in stream
4696 $out, # handle to out stream
4697 $options, # options hash
4702 my ( $record, $key, $fh, %max_hash, $max_record );
4704 while ( $record = Maasha::Biopieces::get_record( $in ) )
4706 foreach $key ( @{ $options->{ "keys" } } )
4708 if ( $record->{ $key } )
4710 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
4714 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4717 $fh = write_stream( $options->{ "data_out" } );
4719 foreach $key ( @{ $options->{ "keys" } } )
4721 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
4724 Maasha::Biopieces::put_record( $max_record, $fh );
4732 # Martin A. Hansen, February 2008.
4734 # Determine the minimum values of given keys.
4736 my ( $in, # handle to in stream
4737 $out, # handle to out stream
4738 $options, # options hash
4743 my ( $record, $key, $fh, %min_hash, $min_record );
4745 while ( $record = Maasha::Biopieces::get_record( $in ) )
4747 foreach $key ( @{ $options->{ "keys" } } )
4749 if ( defined $record->{ $key } )
4751 if ( exists $min_hash{ $key } ) {
4752 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
4754 $min_hash{ $key } = $record->{ $key };
4759 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4762 $fh = write_stream( $options->{ "data_out" } );
4764 foreach $key ( @{ $options->{ "keys" } } )
4766 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
4769 Maasha::Biopieces::put_record( $min_record, $fh );
4775 sub script_upload_to_ucsc
4777 # Martin A. Hansen, August 2007.
4779 # Calculate the mean of values of given keys.
4781 # This routine has developed into the most ugly hack. Do something!
4783 my ( $in, # handle to in stream
4784 $out, # handle to out stream
4785 $options, # options hash
4790 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
4792 $options->{ "short_label" } ||= $options->{ 'table' };
4793 $options->{ "long_label" } ||= $options->{ 'table' };
4794 $options->{ "group" } ||= $ENV{ "LOGNAME" };
4795 $options->{ "priority" } ||= 1;
4796 $options->{ "visibility" } ||= "pack";
4797 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
4798 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
4800 $file = "$BP_TMP/ucsc_upload.tmp";
4805 $fh_out = Maasha::Common::write_open( $file );
4807 while ( $record = Maasha::Biopieces::get_record( $in ) )
4809 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4811 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
4815 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
4816 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
4819 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
4823 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
4824 Maasha::UCSC::psl_put_entry( $record, $fh_out );
4828 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
4830 # chrom chromStart chromEnd name score strand size secStr conf
4834 print $fh_out join ( "\t",
4836 $record->{ "CHR_BEG" },
4837 $record->{ "CHR_END" } + 1,
4838 $record->{ "Q_ID" },
4839 $record->{ "SCORE" },
4840 $record->{ "STRAND" },
4841 $record->{ "SIZE" },
4842 $record->{ "SEC_STRUCT" },
4843 $record->{ "CONF" },
4846 elsif ( $record->{ "REC_TYPE" } eq "BED" )
4849 $columns = $record->{ "BED_COLS" };
4851 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4852 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4855 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
4860 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4861 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4864 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
4869 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
4871 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4872 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4875 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
4880 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
4881 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
4885 if ( $i == $options->{ "chunk_size" } )
4889 if ( $format eq "BED" ) {
4890 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4891 } elsif ( $format eq "PSL" ) {
4892 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
4901 $fh_out = Maasha::Common::write_open( $file );
4909 if ( exists $options->{ "database" } and $options->{ "table" } )
4911 if ( $format eq "BED" )
4913 $type = "bed $columns";
4915 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4917 elsif ( $format eq "BED_SS" )
4919 $type = "type bed 6 +";
4921 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
4923 elsif ( $format eq "PSL" )
4927 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
4929 elsif ( $format eq "WIGGLE" )
4931 $options->{ "visibility" } = "full";
4933 $wig_file = "$options->{ 'table' }.wig";
4934 $wib_file = "$options->{ 'table' }.wib";
4936 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
4938 Maasha::Common::dir_create_if_not_exists( $wib_dir );
4940 if ( $options->{ 'verbose' } ) {
4941 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
4943 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
4946 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
4954 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
4959 Maasha::UCSC::ucsc_update_config( $options, $type );
4964 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<