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 "shuffle_seq" ) { script_shuffle_seq( $in, $out, $options ) }
137 elsif ( $script eq "analyze_seq" ) { script_analyze_seq( $in, $out, $options ) }
138 elsif ( $script eq "analyze_tags" ) { script_analyze_tags( $in, $out, $options ) }
139 elsif ( $script eq "complexity_seq" ) { script_complexity_seq( $in, $out, $options ) }
140 elsif ( $script eq "oligo_freq" ) { script_oligo_freq( $in, $out, $options ) }
141 elsif ( $script eq "create_weight_matrix" ) { script_create_weight_matrix( $in, $out, $options ) }
142 elsif ( $script eq "calc_bit_scores" ) { script_calc_bit_scores( $in, $out, $options ) }
143 elsif ( $script eq "calc_fixedstep" ) { script_calc_fixedstep( $in, $out, $options ) }
144 elsif ( $script eq "reverse_seq" ) { script_reverse_seq( $in, $out, $options ) }
145 elsif ( $script eq "complement_seq" ) { script_complement_seq( $in, $out, $options ) }
146 elsif ( $script eq "remove_indels" ) { script_remove_indels( $in, $out, $options ) }
147 elsif ( $script eq "transliterate_seq" ) { script_transliterate_seq( $in, $out, $options ) }
148 elsif ( $script eq "transliterate_vals" ) { script_transliterate_vals( $in, $out, $options ) }
149 elsif ( $script eq "translate_seq" ) { script_translate_seq( $in, $out, $options ) }
150 elsif ( $script eq "extract_seq" ) { script_extract_seq( $in, $out, $options ) }
151 elsif ( $script eq "get_genome_align" ) { script_get_genome_align( $in, $out, $options ) }
152 elsif ( $script eq "get_genome_phastcons" ) { script_get_genome_phastcons( $in, $out, $options ) }
153 elsif ( $script eq "fold_seq" ) { script_fold_seq( $in, $out, $options ) }
154 elsif ( $script eq "split_seq" ) { script_split_seq( $in, $out, $options ) }
155 elsif ( $script eq "split_bed" ) { script_split_bed( $in, $out, $options ) }
156 elsif ( $script eq "align_seq" ) { script_align_seq( $in, $out, $options ) }
157 elsif ( $script eq "tile_seq" ) { script_tile_seq( $in, $out, $options ) }
158 elsif ( $script eq "invert_align" ) { script_invert_align( $in, $out, $options ) }
159 elsif ( $script eq "patscan_seq" ) { script_patscan_seq( $in, $out, $options ) }
160 elsif ( $script eq "blat_seq" ) { script_blat_seq( $in, $out, $options ) }
161 elsif ( $script eq "soap_seq" ) { script_soap_seq( $in, $out, $options ) }
162 elsif ( $script eq "match_seq" ) { script_match_seq( $in, $out, $options ) }
163 elsif ( $script eq "create_vmatch_index" ) { script_create_vmatch_index( $in, $out, $options ) }
164 elsif ( $script eq "write_align" ) { script_write_align( $in, $out, $options ) }
165 elsif ( $script eq "write_bed" ) { script_write_bed( $in, $out, $options ) }
166 elsif ( $script eq "write_psl" ) { script_write_psl( $in, $out, $options ) }
167 elsif ( $script eq "write_fixedstep" ) { script_write_fixedstep( $in, $out, $options ) }
168 elsif ( $script eq "write_2bit" ) { script_write_2bit( $in, $out, $options ) }
169 elsif ( $script eq "write_solid" ) { script_write_solid( $in, $out, $options ) }
170 elsif ( $script eq "write_ucsc_config" ) { script_write_ucsc_config( $in, $out, $options ) }
171 elsif ( $script eq "head_records" ) { script_head_records( $in, $out, $options ) }
172 elsif ( $script eq "remove_keys" ) { script_remove_keys( $in, $out, $options ) }
173 elsif ( $script eq "remove_adaptor" ) { script_remove_adaptor( $in, $out, $options ) }
174 elsif ( $script eq "remove_mysql_tables" ) { script_remove_mysql_tables( $in, $out, $options ) }
175 elsif ( $script eq "remove_ucsc_tracks" ) { script_remove_ucsc_tracks( $in, $out, $options ) }
176 elsif ( $script eq "rename_keys" ) { script_rename_keys( $in, $out, $options ) }
177 elsif ( $script eq "uniq_vals" ) { script_uniq_vals( $in, $out, $options ) }
178 elsif ( $script eq "merge_vals" ) { script_merge_vals( $in, $out, $options ) }
179 elsif ( $script eq "merge_records" ) { script_merge_records( $in, $out, $options ) }
180 elsif ( $script eq "compute" ) { script_compute( $in, $out, $options ) }
181 elsif ( $script eq "flip_tab" ) { script_flip_tab( $in, $out, $options ) }
182 elsif ( $script eq "add_ident" ) { script_add_ident( $in, $out, $options ) }
183 elsif ( $script eq "count_records" ) { script_count_records( $in, $out, $options ) }
184 elsif ( $script eq "random_records" ) { script_random_records( $in, $out, $options ) }
185 elsif ( $script eq "sort_records" ) { script_sort_records( $in, $out, $options ) }
186 elsif ( $script eq "count_vals" ) { script_count_vals( $in, $out, $options ) }
187 elsif ( $script eq "plot_histogram" ) { script_plot_histogram( $in, $out, $options ) }
188 elsif ( $script eq "plot_lendist" ) { script_plot_lendist( $in, $out, $options ) }
189 elsif ( $script eq "plot_chrdist" ) { script_plot_chrdist( $in, $out, $options ) }
190 elsif ( $script eq "plot_karyogram" ) { script_plot_karyogram( $in, $out, $options ) }
191 elsif ( $script eq "plot_matches" ) { script_plot_matches( $in, $out, $options ) }
192 elsif ( $script eq "plot_seqlogo" ) { script_plot_seqlogo( $in, $out, $options ) }
193 elsif ( $script eq "plot_phastcons_profiles" ) { script_plot_phastcons_profiles( $in, $out, $options ) }
194 elsif ( $script eq "analyze_bed" ) { script_analyze_bed( $in, $out, $options ) }
195 elsif ( $script eq "analyze_vals" ) { script_analyze_vals( $in, $out, $options ) }
196 elsif ( $script eq "length_vals" ) { script_length_vals( $in, $out, $options ) }
197 elsif ( $script eq "sum_vals" ) { script_sum_vals( $in, $out, $options ) }
198 elsif ( $script eq "mean_vals" ) { script_mean_vals( $in, $out, $options ) }
199 elsif ( $script eq "median_vals" ) { script_median_vals( $in, $out, $options ) }
200 elsif ( $script eq "max_vals" ) { script_max_vals( $in, $out, $options ) }
201 elsif ( $script eq "min_vals" ) { script_min_vals( $in, $out, $options ) }
202 elsif ( $script eq "upload_to_ucsc" ) { script_upload_to_ucsc( $in, $out, $options ) }
204 close $in if defined $in;
207 $t1 = gettimeofday();
209 print STDERR "Program: $script" . ( " " x ( 25 - length( $script ) ) ) . sprintf( "Run time: %.4f\n", ( $t1 - $t0 ) ) if $options->{ 'verbose' };
215 # Martin A. Hansen, February 2008.
217 # Gets options from commandline and checks these vigerously.
219 my ( $script, # name of script
224 my ( %options, @options, $opt, @genomes, $real );
226 if ( $script eq "print_usage" )
232 elsif ( $script eq "read_psl" )
239 elsif ( $script eq "read_bed" )
248 elsif ( $script eq "read_fixedstep" )
255 elsif ( $script eq "read_blast_tab" )
262 elsif ( $script eq "read_embl" )
272 elsif ( $script eq "read_stockholm" )
279 elsif ( $script eq "read_phastcons" )
290 elsif ( $script eq "read_soft" )
298 elsif ( $script eq "read_gff" )
305 elsif ( $script eq "read_2bit" )
313 elsif ( $script eq "read_solexa" )
322 elsif ( $script eq "read_solid" )
330 elsif ( $script eq "read_mysql" )
339 elsif ( $script eq "read_ucsc_config" )
346 elsif ( $script eq "assemble_tag_contigs" )
352 elsif ( $script eq "calc_fixedstep" )
358 elsif ( $script eq "length_seq" )
365 elsif ( $script eq "oligo_freq" )
372 elsif ( $script eq "create_weight_matrix" )
378 elsif ( $script eq "transliterate_seq" )
386 elsif ( $script eq "transliterate_vals" )
395 elsif ( $script eq "translate_seq" )
401 elsif ( $script eq "extract_seq" )
409 elsif ( $script eq "get_genome_align" )
420 elsif ( $script eq "get_genome_phastcons" )
431 elsif ( $script eq "split_seq" )
438 elsif ( $script eq "split_bed" )
445 elsif ( $script eq "tile_seq" )
452 elsif ( $script eq "invert_align" )
458 elsif ( $script eq "patscan_seq" )
469 elsif ( $script eq "blat_seq" )
481 elsif ( $script eq "soap_seq" )
492 elsif ( $script eq "match_seq" )
499 elsif ( $script eq "create_vmatch_index" )
507 elsif ( $script eq "write_align" )
517 elsif ( $script eq "write_bed" )
527 elsif ( $script eq "write_psl" )
535 elsif ( $script eq "write_fixedstep" )
543 elsif ( $script eq "write_2bit" )
551 elsif ( $script eq "write_solid" )
560 elsif ( $script eq "write_ucsc_config" )
567 elsif ( $script eq "plot_seqlogo" )
574 elsif ( $script eq "plot_phastcons_profiles" )
589 elsif ( $script eq "analyze_vals" )
596 elsif ( $script eq "head_records" )
602 elsif ( $script eq "remove_keys" )
609 elsif ( $script eq "remove_adaptor" )
618 elsif ( $script eq "remove_mysql_tables" )
629 elsif ( $script eq "remove_ucsc_tracks" )
641 elsif ( $script eq "rename_keys" )
647 elsif ( $script eq "uniq_vals" )
654 elsif ( $script eq "merge_vals" )
661 elsif ( $script eq "merge_records" )
668 elsif ( $script eq "compute" )
674 elsif ( $script eq "add_ident" )
681 elsif ( $script eq "count_records" )
688 elsif ( $script eq "random_records" )
694 elsif ( $script eq "sort_records" )
701 elsif ( $script eq "count_vals" )
707 elsif ( $script eq "plot_histogram" )
720 elsif ( $script eq "plot_lendist" )
732 elsif ( $script eq "plot_chrdist" )
743 elsif ( $script eq "plot_karyogram" )
752 elsif ( $script eq "plot_matches" )
764 elsif ( $script eq "length_vals" )
770 elsif ( $script eq "sum_vals" )
778 elsif ( $script eq "mean_vals" )
786 elsif ( $script eq "median_vals" )
794 elsif ( $script eq "max_vals" )
802 elsif ( $script eq "min_vals" )
810 elsif ( $script eq "upload_to_ucsc" )
834 # print STDERR Dumper( \@options );
841 # print STDERR Dumper( \%options );
843 if ( -t STDIN && scalar( keys %options ) == 0 or $options{ "help" } ) {
844 return wantarray ? %options : \%options;
847 $options{ "cols" } = [ split ",", $options{ "cols" } ] if defined $options{ "cols" };
848 $options{ "keys" } = [ split ",", $options{ "keys" } ] if defined $options{ "keys" };
849 $options{ "no_keys" } = [ split ",", $options{ "no_keys" } ] if defined $options{ "no_keys" };
850 $options{ "save_keys" } = [ split ",", $options{ "save_keys" } ] if defined $options{ "save_keys" };
851 $options{ "quals" } = [ split ",", $options{ "quals" } ] if defined $options{ "quals" };
852 $options{ "feats" } = [ split ",", $options{ "feats" } ] if defined $options{ "feats" };
853 $options{ "frames" } = [ split ",", $options{ "frames" } ] if defined $options{ "frames" };
854 $options{ "samples" } = [ split ",", $options{ "samples" } ] if defined $options{ "samples" };
855 $options{ "tables" } = [ split ",", $options{ "tables" } ] if defined $options{ "tables" };
856 $options{ "tracks" } = [ split ",", $options{ "tracks" } ] if defined $options{ "tracks" };
858 # ---- check arguments ----
860 if ( $options{ 'data_in' } )
862 $options{ "files" } = Maasha::Biopieces::getopt_files( $options{ 'data_in' } );
864 Maasha::Common::error( qq(Argument to --data_in must be a valid file or fileglob expression) ) if scalar @{ $options{ "files" } } == 0;
867 map { Maasha::Common::error( qq(Argument to --cols must be a whole numbers - not "$_") ) if $_ !~ /^\d+$/ } @{ $options{ "cols" } } if $options{ "cols" };
869 # print STDERR Dumper( \%options );
871 $real = "beg|end|word_size|wrap|tile_size|len|prefix_length|mismatches|offset|num|skip|cpus|window_size|step_size";
873 foreach $opt ( keys %options )
875 if ( $opt =~ /stream_in|pattern_in|exact_in/ and not -f $options{ $opt } )
877 Maasha::Common::error( qq(Argument to --$opt must be a valid file or fileglob expression - not "$options{ $opt }") );
879 elsif ( $opt =~ /$real/ and $options{ $opt } !~ /^\d+$/ )
881 Maasha::Common::error( qq(Argument to --$opt must be a whole number - not "$options{ $opt }") );
883 elsif ( $opt =~ /max_hits|max_hits|max_misses|dist|edit_dist|flank|gap|hamming_dist|priority/ and $options{ $opt } !~ /^-?\d+$/ )
885 Maasha::Common::error( qq(Argument to --$opt must be an integer - not "$options{ $opt }") );
887 elsif ( $opt =~ /identity|threshold/ and $options{ $opt } !~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/ )
889 Maasha::Common::error( qq(Argument to --$opt must be a decimal number - not "$options{ $opt }") );
891 elsif ( $opt =~ /e_val/ and $options{ $opt } !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
893 Maasha::Common::error( qq(Argument to --$opt must be a float - not "$options{ $opt }") );
895 elsif ( $opt =~ /strand/ and $options{ $opt } !~ /^(\+|-)$/ )
897 Maasha::Common::error( qq(Argument to --$opt must be "+" or "-" - not "$options{ $opt }") );
899 elsif ( $opt eq "genome" )
901 @genomes = Maasha::Common::ls_dirs( "$ENV{ 'BP_DATA' }/genomes" );
902 map { $_ =~ s/.*\/(.+)$/$1/ } @genomes;
904 if ( not grep { $_ =~ /^$options{ $opt }$/ } @genomes ) {
905 Maasha::Common::error( qq(Genome $options{ $opt } not found in "$ENV{ 'BP_DATA' }/genomes/") );
908 elsif ( $opt eq "terminal" and not $options{ $opt } =~ /^(svg|post|dumb|x11)/ )
910 Maasha::Common::error( qq(Bad --$opt argument "$options{ $opt }") );
912 elsif ( $opt eq "table" and $options{ $opt } =~ /(-|\.)/ )
914 Maasha::Common::error( qq(Character '$1' is not allowed in table name: $options{ $opt }) );
916 elsif ( $opt eq "merge" and $options{ $opt } !~ /^(AandB|AorB|BorA|AnotB|BnotA)$/ )
918 Maasha::Common::error( qq(Argument to --$opt must be AandB, AorB, BorA, AnotB, or BnotA - not "$options{ $opt }") );
920 elsif ( $opt eq "format" and $script eq "read_solexa" and $options{ $opt } !~ /octal|decimal/ )
922 Maasha::Common::error( qq(Argument to --$opt must be octal or decimal - not "$options{ $opt }") );
924 elsif ( $opt eq "remove" and $script eq "remove_adaptor" and $options{ $opt } !~ /before|after|skip/ )
926 Maasha::Common::error( qq(Argument to --$opt must be before, after, or skip - not "$options{ $opt }") );
930 Maasha::Common::error( qq(no --database specified) ) if $script eq "remove_ucsc_tracks" and not $options{ "database" };
931 Maasha::Common::error( qq(no --index_name specified) ) if $script =~ /create_vmatch_index/ and not $options{ "index_name" };
932 Maasha::Common::error( qq(no --in_file or --genome specified) ) if $script eq "soap_seq" and not $options{ "genome" } and not $options{ "in_file" };
933 Maasha::Common::error( qq(both --in_file and --genome specified) ) if $script eq "soap_seq" and $options{ "genome" } and $options{ "in_file" };
934 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" };
935 Maasha::Common::error( qq(no --key specified) ) if $script =~ /plot_lendist|plot_histogram/ and not $options{ "key" };
936 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" };
938 if ( $script eq "upload_to_ucsc" )
940 Maasha::Common::error( qq(no --database specified) ) if not $options{ "database" };
941 Maasha::Common::error( qq(no --table specified) ) if not $options{ "table" };
944 return wantarray ? %options : \%options;
948 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SCRIPTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
951 sub script_print_usage
953 # Martin A. Hansen, January 2008.
955 # Retrieves usage information from file and
956 # prints this nicely formatted.
958 my ( $in, # handle to in stream
959 $out, # handle to out stream
960 $options, # options hash
965 my ( $file, $wiki, $lines );
967 if ( $options->{ 'data_in' } ) {
968 $file = $options->{ 'data_in' };
970 $file = join "", $ENV{ 'BP_DIR' }, "/bp_usage/", $options->{ 'SCRIPT' }, ".wiki";
973 $wiki = Maasha::Gwiki::gwiki_read( $file );
975 ( $wiki->[ 2 ], $wiki->[ 3 ], $wiki->[ 0 ], $wiki->[ 1 ] ) = ( $wiki->[ 0 ], $wiki->[ 1 ], $wiki->[ 2 ], $wiki->[ 3 ] );
977 if ( not $options->{ "help" } ) {
978 @{ $wiki } = grep { $_->[ 0 ]->{ 'SECTION' } =~ /Biopiece|summary|Usage|Options|Help/ } @{ $wiki };
981 $lines = Maasha::Gwiki::gwiki2ascii( $wiki );
983 print STDERR "$_\n" foreach @{ $lines };
991 # Martin A. Hansen, August 2007.
993 # Read psl table from stream or file.
995 my ( $in, # handle to in stream
996 $out, # handle to out stream
997 $options, # options hash
1002 my ( $record, $file, $data_in, $num );
1004 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1005 Maasha::Biopieces::put_record( $record, $out );
1010 foreach $file ( @{ $options->{ "files" } } )
1012 $data_in = Maasha::Common::read_open( $file );
1014 while ( $record = Maasha::UCSC::psl_get_entry( $data_in ) )
1016 Maasha::Biopieces::put_record( $record, $out );
1018 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1030 # Martin A. Hansen, August 2007.
1032 # Read bed table from stream or file.
1034 my ( $in, # handle to in stream
1035 $out, # handle to out stream
1036 $options, # options hash
1041 my ( $cols, $file, $record, $bed_entry, $data_in, $num );
1043 $cols = $options->{ 'cols' }->[ 0 ];
1045 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1046 Maasha::Biopieces::put_record( $record, $out );
1051 foreach $file ( @{ $options->{ "files" } } )
1053 $data_in = Maasha::Common::read_open( $file );
1055 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $data_in, $cols, $options->{ 'check' } ) )
1057 $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry );
1059 Maasha::Biopieces::put_record( $record, $out );
1061 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1071 close $data_in if $data_in;
1075 sub script_read_fixedstep
1077 # Martin A. Hansen, Juli 2008.
1079 # Read fixedstep wiggle format from stream or file.
1081 my ( $in, # handle to in stream
1082 $out, # handle to out stream
1083 $options, # options hash
1088 my ( $file, $record, $entry, $data_in, $num );
1090 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1091 Maasha::Biopieces::put_record( $record, $out );
1096 foreach $file ( @{ $options->{ "files" } } )
1098 $data_in = Maasha::Common::read_open( $file );
1100 while ( $entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $data_in ) )
1102 $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $entry );
1104 Maasha::Biopieces::put_record( $record, $out );
1106 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1116 close $data_in if $data_in;
1120 sub script_read_blast_tab
1122 # Martin A. Hansen, September 2007.
1124 # Read tabular BLAST output from NCBI blast run with -m8 or -m9.
1126 my ( $in, # handle to in stream
1127 $out, # handle to out stream
1128 $options, # options hash
1133 my ( $file, $line, @fields, $strand, $record, $data_in, $num );
1135 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1136 Maasha::Biopieces::put_record( $record, $out );
1141 foreach $file ( @{ $options->{ "files" } } )
1143 $data_in = Maasha::Common::read_open( $file );
1145 while ( $line = <$data_in> )
1149 next if $line =~ /^#/;
1151 @fields = split /\t/, $line;
1153 $record->{ "REC_TYPE" } = "BLAST";
1154 $record->{ "Q_ID" } = $fields[ 0 ];
1155 $record->{ "S_ID" } = $fields[ 1 ];
1156 $record->{ "IDENT" } = $fields[ 2 ];
1157 $record->{ "ALIGN_LEN" } = $fields[ 3 ];
1158 $record->{ "MISMATCHES" } = $fields[ 4 ];
1159 $record->{ "GAPS" } = $fields[ 5 ];
1160 $record->{ "Q_BEG" } = $fields[ 6 ] - 1; # BLAST is 1-based
1161 $record->{ "Q_END" } = $fields[ 7 ] - 1; # BLAST is 1-based
1162 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # BLAST is 1-based
1163 $record->{ "S_END" } = $fields[ 9 ] - 1; # BLAST is 1-based
1164 $record->{ "E_VAL" } = $fields[ 10 ];
1165 $record->{ "BIT_SCORE" } = $fields[ 11 ];
1167 if ( $record->{ "S_BEG" } > $record->{ "S_END" } )
1169 $record->{ "STRAND" } = '-';
1171 ( $record->{ "S_BEG" }, $record->{ "S_END" } ) = ( $record->{ "S_END" }, $record->{ "S_BEG" } );
1175 $record->{ "STRAND" } = '+';
1178 Maasha::Biopieces::put_record( $record, $out );
1180 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1190 close $data_in if $data_in;
1194 sub script_read_embl
1196 # Martin A. Hansen, August 2007.
1200 my ( $in, # handle to in stream
1201 $out, # handle to out stream
1202 $options, # options hash
1207 my ( %options2, $file, $data_in, $num, $entry, $record );
1209 map { $options2{ "keys" }{ $_ } = 1 } @{ $options->{ "keys" } };
1210 map { $options2{ "feats" }{ $_ } = 1 } @{ $options->{ "feats" } };
1211 map { $options2{ "quals" }{ $_ } = 1 } @{ $options->{ "quals" } };
1213 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1214 Maasha::Biopieces::put_record( $record, $out );
1219 foreach $file ( @{ $options->{ "files" } } )
1221 $data_in = Maasha::Common::read_open( $file );
1223 while ( $entry = Maasha::EMBL::get_embl_entry( $data_in ) )
1225 $record = Maasha::EMBL::parse_embl_entry( $entry, \%options2 );
1227 my ( $feat, $feat2, $qual, $qual_val, $record_copy );
1229 $record_copy = dclone $record;
1231 delete $record_copy->{ "FT" };
1233 Maasha::Biopieces::put_record( $record_copy, $out );
1235 delete $record_copy->{ "SEQ" };
1237 foreach $feat ( keys %{ $record->{ "FT" } } )
1239 $record_copy->{ "FEAT_TYPE" } = $feat;
1241 foreach $feat2 ( @{ $record->{ "FT" }->{ $feat } } )
1243 foreach $qual ( keys %{ $feat2 } )
1245 $qual_val = join "; ", @{ $feat2->{ $qual } };
1250 $record_copy->{ $qual } = $qual_val;
1253 Maasha::Biopieces::put_record( $record_copy, $out );
1257 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1267 close $data_in if $data_in;
1271 sub script_read_stockholm
1273 # Martin A. Hansen, August 2007.
1275 # Read Stockholm format.
1277 my ( $in, # handle to in stream
1278 $out, # handle to out stream
1279 $options, # options hash
1284 my ( $data_in, $file, $num, $entry, $record, $record_anno, $record_align, $key, $seq );
1286 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1287 Maasha::Biopieces::put_record( $record, $out );
1292 foreach $file ( @{ $options->{ "files" } } )
1294 $data_in = Maasha::Common::read_open( $file );
1296 while ( $entry = Maasha::Stockholm::get_stockholm_entry( $data_in ) )
1298 $record = Maasha::Stockholm::parse_stockholm_entry( $entry );
1302 foreach $key ( keys %{ $record->{ "GF" } } ) {
1303 $record_anno->{ $key } = $record->{ "GF" }->{ $key };
1306 $record_anno->{ "ALIGN" } = $num;
1308 Maasha::Biopieces::put_record( $record_anno, $out );
1310 foreach $seq ( @{ $record->{ "ALIGN" } } )
1312 undef $record_align;
1315 SEQ_NAME => $seq->[ 0 ],
1319 Maasha::Biopieces::put_record( $record_align, $out );
1322 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1332 close $data_in if $data_in;
1336 sub script_read_phastcons
1338 # Martin A. Hansen, December 2007.
1340 # Read PhastCons format.
1342 my ( $in, # handle to in stream
1343 $out, # handle to out stream
1344 $options, # options hash
1349 my ( $data_in, $file, $num, $entry, @records, $record );
1351 $options->{ "min" } ||= 10;
1352 $options->{ "dist" } ||= 25;
1353 $options->{ "threshold" } ||= 0.8;
1354 $options->{ "gap" } ||= 5;
1356 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1357 Maasha::Biopieces::put_record( $record, $out );
1362 foreach $file ( @{ $options->{ "files" } } )
1364 $data_in = Maasha::Common::read_open( $file );
1366 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $data_in ) )
1368 @records = Maasha::UCSC::phastcons_parse_entry( $entry, $options );
1370 foreach $record ( @records )
1372 $record->{ "REC_TYPE" } = "BED";
1373 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
1375 Maasha::Biopieces::put_record( $record, $out );
1377 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1388 close $data_in if $data_in;
1392 sub script_read_soft
1394 # Martin A. Hansen, December 2007.
1397 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1399 my ( $in, # handle to in stream
1400 $out, # handle to out stream
1401 $options, # options hash
1406 my ( $data_in, $file, $num, $records, $record, $soft_index, $fh, @platforms, $plat_table, @samples, $sample, $old_end, $skip );
1408 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1409 Maasha::Biopieces::put_record( $record, $out );
1414 foreach $file ( @{ $options->{ "files" } } )
1416 print STDERR "Creating index for file: $file\n" if $options->{ "verbose" };
1418 $soft_index = Maasha::NCBI::soft_index_file( $file );
1420 $fh = Maasha::Common::read_open( $file );
1422 @platforms = grep { $_->{ "SECTION" } =~ /PLATFORM/ } @{ $soft_index };
1424 print STDERR "Getting platform tables for file: $file\n" if $options->{ "verbose" };
1426 $plat_table = Maasha::NCBI::soft_get_platform( $fh, $platforms[ 0 ]->{ "LINE_BEG" }, $platforms[ -1 ]->{ "LINE_END" } );
1428 @samples = grep { $_->{ "SECTION" } =~ /SAMPLE/ } @{ $soft_index };
1430 $old_end = $platforms[ -1 ]->{ "LINE_END" };
1432 foreach $sample ( @samples )
1435 $skip = 1 if ( $options->{ "samples" } and grep { $sample->{ "SECTION" } !~ /$_/ } @{ $options->{ "samples" } } );
1437 print STDERR "Getting samples for dataset: $sample->{ 'SECTION' }\n" if $options->{ "verbose" } and not $skip;
1439 $records = Maasha::NCBI::soft_get_sample( $fh, $plat_table, $sample->{ "LINE_BEG" } - $old_end - 1, $sample->{ "LINE_END" } - $old_end - 1, $skip );
1441 foreach $record ( @{ $records } )
1443 Maasha::Biopieces::put_record( $record, $out );
1445 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1450 $old_end = $sample->{ "LINE_END" };
1458 close $data_in if $data_in;
1465 # Martin A. Hansen, February 2008.
1468 # http://www.ncbi.nlm.nih.gov/geo/info/soft2.html
1470 my ( $in, # handle to in stream
1471 $out, # handle to out stream
1472 $options, # options hash
1477 my ( $data_in, $file, $fh, $num, $record, $entry );
1479 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1480 Maasha::Biopieces::put_record( $record, $out );
1485 foreach $file ( @{ $options->{ "files" } } )
1487 $fh = Maasha::Common::read_open( $file );
1489 while ( $entry = Maasha::GFF::get_entry( $fh ) )
1491 Maasha::Biopieces::put_record( $entry, $out );
1493 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1503 close $data_in if $data_in;
1507 sub script_read_2bit
1509 # Martin A. Hansen, March 2008.
1511 # Read sequences from 2bit file.
1513 my ( $in, # handle to in stream
1514 $out, # handle to out stream
1515 $options, # options hash
1520 my ( $record, $file, $data_in, $mask, $toc, $line, $num );
1522 $mask = 1 if not $options->{ "no_mask" };
1524 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1525 Maasha::Biopieces::put_record( $record, $out );
1530 foreach $file ( @{ $options->{ "files" } } )
1532 $data_in = Maasha::Common::read_open( $file );
1534 $toc = Maasha::TwoBit::twobit_get_TOC( $data_in );
1536 foreach $line ( @{ $toc } )
1538 $record->{ "SEQ_NAME" } = $line->[ 0 ];
1539 $record->{ "SEQ" } = Maasha::TwoBit::twobit_get_seq( $data_in, $line->[ 1 ], undef, undef, $mask );
1540 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1542 Maasha::Biopieces::put_record( $record, $out );
1544 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1554 close $data_in if $data_in;
1558 sub script_read_solexa
1560 # Martin A. Hansen, March 2008.
1562 # Read Solexa sequence reads from file.
1564 my ( $in, # handle to in stream
1565 $out, # handle to out stream
1566 $options, # options hash
1571 my ( $record, $file, $data_in, $entry, $num, @seqs, @scores, $i );
1573 $options->{ "format" } ||= "octal";
1574 $options->{ "quality" } ||= 20;
1576 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1577 Maasha::Biopieces::put_record( $record, $out );
1582 foreach $file ( @{ $options->{ "files" } } )
1584 $data_in = Maasha::Common::read_open( $file );
1586 if ( $options->{ "format" } eq "octal" )
1588 while ( $entry = Maasha::Solexa::solexa_get_entry_octal( $data_in ) )
1590 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1592 Maasha::Biopieces::put_record( $record, $out );
1594 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1601 while ( $entry = Maasha::Solexa::solexa_get_entry_decimal( $data_in ) )
1603 $record = Maasha::Solexa::solexa2biopiece( $entry, $options->{ "quality" } );
1605 Maasha::Biopieces::put_record( $record, $out );
1607 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1618 close $data_in if $data_in;
1622 sub script_read_solid
1624 # Martin A. Hansen, April 2008.
1626 # Read Solid sequence from file.
1628 my ( $in, # handle to in stream
1629 $out, # handle to out stream
1630 $options, # options hash
1635 my ( $record, $file, $data_in, $line, $num, $seq_name, $seq_cs, $seq_qual, @scores, @seqs, $i );
1637 $options->{ "quality" } ||= 15;
1639 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1640 Maasha::Biopieces::put_record( $record, $out );
1645 foreach $file ( @{ $options->{ "files" } } )
1647 $data_in = Maasha::Common::read_open( $file );
1649 while ( $line = <$data_in> )
1653 ( $seq_name, $seq_cs, $seq_qual ) = split /\t/, $line;
1655 @scores = split /,/, $seq_qual;
1656 @seqs = split //, Maasha::Solid::color_space2seq( $seq_cs );
1658 for ( $i = 0; $i < @seqs; $i++ ) {
1659 $seqs[ $i ] = lc $seqs[ $i ] if $scores[ $i ] < $options->{ "quality" };
1663 REC_TYPE => 'SOLID',
1664 SEQ_NAME => $seq_name,
1666 SEQ_QUAL => join( ";", @scores ),
1667 SEQ_LEN => length $seq_cs,
1668 SEQ => join( "", @seqs ),
1669 SCORE_MEAN => sprintf( "%.2f", Maasha::Calc::mean( \@scores ) ),
1672 Maasha::Biopieces::put_record( $record, $out );
1674 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1684 close $data_in if $data_in;
1688 sub script_read_mysql
1690 # Martin A. Hansen, May 2008.
1692 # Read a MySQL query into stream.
1694 my ( $in, # handle to in stream
1695 $out, # handle to out stream
1696 $options, # options hash
1701 my ( $record, $dbh, $results );
1703 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
1704 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
1706 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1707 Maasha::Biopieces::put_record( $record, $out );
1710 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
1712 $results = Maasha::SQL::query_hashref_list( $dbh, $options->{ "query" } );
1714 Maasha::SQL::disconnect( $dbh );
1716 map { Maasha::Biopieces::put_record( $_ ) } @{ $results };
1720 sub script_read_ucsc_config
1722 # Martin A. Hansen, November 2008.
1724 # Read track entries from UCSC Genome Browser '.ra' files.
1726 my ( $in, # handle to in stream
1727 $out, # handle to out stream
1728 $options, # options hash
1733 my ( $record, $file, $data_in, $entry, $num );
1735 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
1736 Maasha::Biopieces::put_record( $record, $out );
1741 foreach $file ( @{ $options->{ "files" } } )
1743 $data_in = Maasha::Common::read_open( $file );
1745 while ( $record = Maasha::UCSC::ucsc_config_entry_get( $data_in ) )
1747 $record->{ 'REC_TYPE' } = "UCSC Config";
1749 Maasha::Biopieces::put_record( $record, $out );
1751 goto NUM if $options->{ "num" } and $num == $options->{ "num" };
1761 close $data_in if $data_in;
1765 sub script_assemble_tag_contigs
1767 # Martin A. Hansen, November 2008.
1769 # Assemble tags from the stream into
1772 # The current implementation is quite
1773 # slow because of heavy use of temporary
1776 my ( $in, # handle to in stream
1777 $out, # handle to out stream
1778 $options, # options hash
1783 my ( $bed_file, $tag_file, $fh_in, $fh_out, $cols, $record, $bed_entry, $file_hash, $chr, $strand );
1785 $bed_file = "$BP_TMP/assemble_tag_contigs.bed";
1786 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
1787 $cols = 6; # we only need the first 6 BED columns
1789 while ( $record = Maasha::Biopieces::get_record( $in ) )
1791 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) )
1793 $strand = $record->{ 'STRAND' } || '+';
1795 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, $cols, $options->{ 'check' } );
1801 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP, $cols );
1805 foreach $chr ( sort keys %{ $file_hash } )
1807 $bed_file = $file_hash->{ $chr };
1808 $tag_file = "$bed_file.tc";
1810 Maasha::Common::run( "bed2tag_contigs", "< $bed_file > $tag_file" );
1812 $fh_in = Maasha::Filesys::file_read_open( $tag_file );
1814 while ( $bed_entry = Maasha::UCSC::BED::bed_entry_get( $fh_in, $cols, $options->{ 'check' } ) )
1816 if ( $record = Maasha::UCSC::BED::bed2biopiece( $bed_entry ) ) {
1817 Maasha::Biopieces::put_record( $record, $out );
1829 sub script_length_seq
1831 # Martin A. Hansen, August 2007.
1833 # Determine the length of sequences in stream.
1835 my ( $in, # handle to in stream
1836 $out, # handle to out stream
1837 $options, # options hash
1842 my ( $record, $total );
1844 while ( $record = Maasha::Biopieces::get_record( $in ) )
1846 if ( $record->{ "SEQ" } )
1848 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
1849 $total += $record->{ "SEQ_LEN" };
1852 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
1855 Maasha::Biopieces::put_record( { TOTAL_SEQ_LEN => $total }, $out );
1859 sub script_shuffle_seq
1861 # Martin A. Hansen, December 2007.
1863 # Shuffle sequences in stream.
1865 my ( $in, # handle to in stream
1866 $out, # handle to out stream
1873 while ( $record = Maasha::Biopieces::get_record( $in ) )
1875 $record->{ "SEQ" } = Maasha::Seq::seq_shuffle( $record->{ "SEQ" } ) if $record->{ "SEQ" };
1877 Maasha::Biopieces::put_record( $record, $out );
1882 sub script_analyze_seq
1884 # Martin A. Hansen, August 2007.
1886 # Analyze sequence composition of sequences in stream.
1888 my ( $in, # handle to in stream
1889 $out, # handle to out stream
1894 my ( $record, $analysis );
1896 while ( $record = Maasha::Biopieces::get_record( $in ) )
1898 if ( $record->{ "SEQ" } )
1900 $analysis = Maasha::Seq::seq_analyze( $record->{ "SEQ" } );
1902 map { $record->{ $_ } = $analysis->{ $_ } } keys %{ $analysis };
1905 Maasha::Biopieces::put_record( $record, $out );
1910 sub script_analyze_tags
1912 # Martin A. Hansen, August 2008.
1914 # Analyze sequence tags in stream.
1916 my ( $in, # handle to in stream
1917 $out, # handle to out stream
1922 my ( $record, $analysis, %len_hash, %clone_hash, $clones, $key, $tag_record );
1924 while ( $record = Maasha::Biopieces::get_record( $in ) )
1926 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
1928 if ( $record->{ "SEQ_NAME" } =~ /_(\d+)$/ )
1932 $len_hash{ length( $record->{ "SEQ" } ) }++;
1933 $clone_hash{ length( $record->{ "SEQ" } ) } += $clones;
1936 elsif ( $record->{ "Q_ID" } and $record->{ "BED_LEN" } )
1938 if ( $record->{ "Q_ID" } =~ /_(\d+)$/ )
1942 $len_hash{ $record->{ "BED_LEN" } }++;
1943 $clone_hash{ $record->{ "BED_LEN" } } += $clones;
1948 foreach $key ( sort { $a <=> $b } keys %len_hash )
1950 $tag_record->{ "TAG_LEN" } = $key;
1951 $tag_record->{ "TAG_COUNT" } = $len_hash{ $key };
1952 $tag_record->{ "TAG_CLONES" } = $clone_hash{ $key };
1954 Maasha::Biopieces::put_record( $tag_record, $out );
1959 sub script_complexity_seq
1961 # Martin A. Hansen, May 2008.
1963 # Generates an index calculated as the most common di-residue over
1964 # the sequence length for all sequences in stream.
1966 my ( $in, # handle to in stream
1967 $out, # handle to out stream
1972 my ( $record, $index );
1974 while ( $record = Maasha::Biopieces::get_record( $in ) )
1976 $record->{ "SEQ_COMPLEXITY" } = sprintf( "%.2f", Maasha::Seq::seq_complexity( $record->{ "SEQ" } ) ) if $record->{ "SEQ" };
1978 Maasha::Biopieces::put_record( $record, $out );
1983 sub script_oligo_freq
1985 # Martin A. Hansen, August 2007.
1987 # Determine the length of sequences in stream.
1989 my ( $in, # handle to in stream
1990 $out, # handle to out stream
1991 $options, # options hash
1996 my ( $record, %oligos, @freq_table );
1998 $options->{ "word_size" } ||= 7;
2000 while ( $record = Maasha::Biopieces::get_record( $in ) )
2002 if ( $record->{ "SEQ" } )
2004 map { $oligos{ $_ }++ } Maasha::Seq::seq2oligos( \$record->{ "SEQ" }, $options->{ "word_size" } );
2006 if ( not $options->{ "all" } )
2008 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2010 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
2016 Maasha::Biopieces::put_record( $record, $out );
2019 if ( $options->{ "all" } )
2021 @freq_table = Maasha::Seq::oligo_freq( \%oligos );
2023 map { Maasha::Biopieces::put_record( $_, $out ) } @freq_table;
2028 sub script_create_weight_matrix
2030 # Martin A. Hansen, August 2007.
2032 # Creates a weight matrix from an alignmnet.
2034 my ( $in, # handle to in stream
2035 $out, # handle to out stream
2036 $options, # options hash
2041 my ( $record, $count, $i, $res, %freq_hash, %res_hash, $freq );
2045 while ( $record = Maasha::Biopieces::get_record( $in ) )
2047 if ( $record->{ "SEQ" } )
2049 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2051 $res = substr $record->{ "SEQ" }, $i, 1;
2053 $freq_hash{ $i }{ $res }++;
2054 $res_hash{ $res } = 1;
2061 Maasha::Biopieces::put_record( $record, $out );
2065 foreach $res ( sort keys %res_hash )
2069 $record->{ "V0" } = $res;
2071 for ( $i = 0; $i < keys %freq_hash; $i++ )
2073 $freq = $freq_hash{ $i }{ $res } || 0;
2075 if ( $options->{ "percent" } ) {
2076 $freq = sprintf( "%.0f", 100 * $freq / $count ) if $freq > 0;
2079 $record->{ "V" . ( $i + 1 ) } = $freq;
2082 Maasha::Biopieces::put_record( $record, $out );
2087 sub script_calc_bit_scores
2089 # Martin A. Hansen, March 2007.
2091 # Calculates the bit scores for each position from an alignmnet in the stream.
2093 my ( $in, # handle to in stream
2094 $out, # handle to out stream
2099 my ( $record, $type, $count, $i, $res, %freq_hash, $bit_max, $bit_height, $bit_diff );
2103 while ( $record = Maasha::Biopieces::get_record( $in ) )
2105 if ( $record->{ "SEQ" } )
2107 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2109 for ( $i = 0; $i < length $record->{ "SEQ" }; $i++ )
2111 $res = substr $record->{ "SEQ" }, $i, 1;
2113 next if $res =~ /-|_|~|\./;
2115 $freq_hash{ $i }{ $res }++;
2122 Maasha::Biopieces::put_record( $record, $out );
2128 if ( $type eq "protein" ) {
2134 for ( $i = 0; $i < keys %freq_hash; $i++ )
2136 $bit_height = Maasha::Seq::seqlogo_calc_bit_height( $freq_hash{ $i }, $count );
2138 $bit_diff = $bit_max - $bit_height;
2140 $record->{ "V" . ( $i ) } = sprintf( "%.2f", $bit_diff );
2143 Maasha::Biopieces::put_record( $record, $out );
2147 sub script_calc_fixedstep
2149 # Martin A. Hansen, September 2008.
2151 # Calculates fixedstep entries from data in the stream.
2153 my ( $in, # handle to in stream
2154 $out, # handle to out stream
2155 $options, # options hash
2160 my ( $bed_file, $fh_in, $fh_out, $record, $file_hash, $chr, $bed_entry, $fixedstep_file, $fixedstep_entry );
2162 $bed_file = "$BP_TMP/calc_fixedstep.bed";
2163 $fh_out = Maasha::Filesys::file_write_open( $bed_file );
2165 while ( $record = Maasha::Biopieces::get_record( $in ) )
2167 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record ) ) {
2168 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh_out, undef, $options->{ 'check' } );
2174 $file_hash = Maasha::UCSC::BED::bed_file_split_on_chr( $bed_file, $BP_TMP );
2178 foreach $chr ( sort keys %{ $file_hash } )
2180 $bed_file = $file_hash->{ $chr };
2182 $fixedstep_file = Maasha::UCSC::Wiggle::fixedstep_calc( $bed_file, $chr, $options->{ 'use_score' }, $options->{ 'use_log10' } );
2184 #$fixedstep_file = "$bed_file.fixedstep";
2186 # Maasha::Common::run( "bed2fixedstep", "< $bed_file > $fixedstep_file" );
2188 $fh_in = Maasha::Filesys::file_read_open( $fixedstep_file );
2190 while ( $fixedstep_entry = Maasha::UCSC::Wiggle::fixedstep_entry_get( $fh_in ) )
2192 if ( $record = Maasha::UCSC::Wiggle::fixedstep2biopiece( $fixedstep_entry ) ) {
2193 Maasha::Biopieces::put_record( $record, $out );
2200 unlink $fixedstep_file;
2205 sub script_reverse_seq
2207 # Martin A. Hansen, August 2007.
2209 # Reverse sequence in record.
2211 my ( $in, # handle to in stream
2212 $out, # handle to out stream
2219 while ( $record = Maasha::Biopieces::get_record( $in ) )
2221 if ( $record->{ "SEQ" } ) {
2222 $record->{ "SEQ" } = reverse $record->{ "SEQ" };
2225 Maasha::Biopieces::put_record( $record, $out );
2230 sub script_complement_seq
2232 # Martin A. Hansen, August 2007.
2234 # Complement sequence in record.
2236 my ( $in, # handle to in stream
2237 $out, # handle to out stream
2242 my ( $record, $type );
2244 while ( $record = Maasha::Biopieces::get_record( $in ) )
2246 if ( $record->{ "SEQ" } )
2249 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2252 if ( $type eq "rna" ) {
2253 Maasha::Seq::rna_comp( \$record->{ "SEQ" } );
2254 } elsif ( $type eq "dna" ) {
2255 Maasha::Seq::dna_comp( \$record->{ "SEQ" } );
2259 Maasha::Biopieces::put_record( $record, $out );
2264 sub script_remove_indels
2266 # Martin A. Hansen, August 2007.
2268 # Remove indels from sequences in stream.
2270 my ( $in, # handle to in stream
2271 $out, # handle to out stream
2278 while ( $record = Maasha::Biopieces::get_record( $in ) )
2280 $record->{ 'SEQ' } =~ tr/-~.//d if $record->{ "SEQ" };
2282 Maasha::Biopieces::put_record( $record, $out );
2287 sub script_transliterate_seq
2289 # Martin A. Hansen, August 2007.
2291 # Transliterate chars from sequence in record.
2293 my ( $in, # handle to in stream
2294 $out, # handle to out stream
2295 $options, # options hash
2300 my ( $record, $search, $replace, $delete );
2302 $search = $options->{ "search" } || "";
2303 $replace = $options->{ "replace" } || "";
2304 $delete = $options->{ "delete" } || "";
2306 while ( $record = Maasha::Biopieces::get_record( $in ) )
2308 if ( $record->{ "SEQ" } )
2310 if ( $search and $replace ) {
2311 eval "\$record->{ 'SEQ' } =~ tr/$search/$replace/";
2312 } elsif ( $delete ) {
2313 eval "\$record->{ 'SEQ' } =~ tr/$delete//d";
2317 Maasha::Biopieces::put_record( $record, $out );
2322 sub script_transliterate_vals
2324 # Martin A. Hansen, April 2008.
2326 # Transliterate chars from values in record.
2328 my ( $in, # handle to in stream
2329 $out, # handle to out stream
2330 $options, # options hash
2335 my ( $record, $search, $replace, $delete, $key );
2337 $search = $options->{ "search" } || "";
2338 $replace = $options->{ "replace" } || "";
2339 $delete = $options->{ "delete" } || "";
2341 while ( $record = Maasha::Biopieces::get_record( $in ) )
2343 foreach $key ( @{ $options->{ "keys" } } )
2345 if ( exists $record->{ $key } )
2347 if ( $search and $replace ) {
2348 eval "\$record->{ $key } =~ tr/$search/$replace/";
2349 } elsif ( $delete ) {
2350 eval "\$record->{ $key } =~ tr/$delete//d";
2355 Maasha::Biopieces::put_record( $record, $out );
2360 sub script_translate_seq
2362 # Martin A. Hansen, February 2008.
2364 # Translate DNA sequence into protein sequence.
2366 my ( $in, # handle to in stream
2367 $out, # handle to out stream
2368 $options, # options hash
2373 my ( $record, $frame, %new_record );
2375 $options->{ "frames" } ||= [ 1, 2, 3, -1, -2, -3 ];
2377 while ( $record = Maasha::Biopieces::get_record( $in ) )
2379 if ( $record->{ "SEQ" } )
2381 if ( Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) eq "dna" )
2383 foreach $frame ( @{ $options->{ "frames" } } )
2385 %new_record = %{ $record };
2387 $new_record{ "SEQ" } = Maasha::Seq::translate( $record->{ "SEQ" }, $frame );
2388 $new_record{ "SEQ_LEN" } = length $new_record{ "SEQ" };
2389 $new_record{ "FRAME" } = $frame;
2391 Maasha::Biopieces::put_record( \%new_record, $out );
2397 Maasha::Biopieces::put_record( $record, $out );
2403 sub script_extract_seq
2405 # Martin A. Hansen, August 2007.
2407 # Extract subsequences from sequences in record.
2409 my ( $in, # handle to in stream
2410 $out, # handle to out stream
2411 $options, # options hash
2416 my ( $beg, $end, $len, $record );
2418 if ( not defined $options->{ "beg" } or $options->{ "beg" } < 0 ) {
2421 $beg = $options->{ "beg" } - 1; # correcting for start offset
2424 if ( defined $options->{ "end" } and $options->{ "end" } - 1 < $beg ) {
2426 } elsif ( defined $options->{ "end" } ) {
2427 $end = $options->{ "end" } - 1; # correcting for start offset
2430 $len = $options->{ "len" };
2432 # print "beg->$beg, end->$end, len->$len\n";
2434 while ( $record = Maasha::Biopieces::get_record( $in ) )
2436 if ( $record->{ "SEQ" } )
2438 if ( defined $beg and defined $end )
2440 if ( $end - $beg + 1 > length $record->{ "SEQ" } ) {
2441 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2443 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $end - $beg + 1;
2446 elsif ( defined $beg and defined $len )
2448 if ( $len > length $record->{ "SEQ" } ) {
2449 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2451 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg, $len;
2454 elsif ( defined $beg )
2456 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $beg;
2460 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
2462 Maasha::Biopieces::put_record( $record, $out );
2467 sub script_get_genome_align
2469 # Martin A. Hansen, April 2008.
2471 # Gets a subalignment from a multiple genome alignment.
2473 my ( $in, # handle to in stream
2474 $out, # handle to out stream
2475 $options, # options hash
2480 my ( $record, $maf_track, $align, $align_num, $beg, $end, $len, $entry );
2482 $options->{ "strand" } ||= "+";
2486 $maf_track = Maasha::Config::maf_track( $options->{ "genome" } );
2488 if ( $options->{ "chr" } and $options->{ "beg" } and ( $options->{ "end" } or $options->{ "len" } ) )
2490 $beg = $options->{ "beg" } - 1;
2492 if ( $options->{ "end" } ) {
2493 $end = $options->{ "end" };
2494 } elsif ( $options->{ "len" } ) {
2495 $end = $beg + $options->{ "len" };
2498 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $options->{ "chr" }, $beg, $end, $options->{ "strand" } );
2500 foreach $entry ( @{ $align } )
2502 $entry->{ "CHR" } = $record->{ "CHR" };
2503 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2504 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2505 $entry->{ "STRAND" } = $record->{ "STRAND" } || '+';
2506 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2507 $entry->{ "SCORE" } = $record->{ "SCORE" };
2509 Maasha::Biopieces::put_record( $entry, $out );
2513 while ( $record = Maasha::Biopieces::get_record( $in ) )
2515 if ( $record->{ "REC_TYPE" } eq "BED" )
2517 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $record->{ "STRAND" } );
2519 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" )
2521 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" } + 1, $record->{ "STRAND" } );
2523 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2525 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2527 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2529 $align = Maasha::UCSC::maf_extract( $BP_TMP, $options->{ "genome" }, $maf_track, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $record->{ "STRAND" } );
2532 foreach $entry ( @{ $align } )
2534 $entry->{ "CHR" } = $record->{ "CHR" };
2535 $entry->{ "CHR_BEG" } = $record->{ "CHR_BEG" };
2536 $entry->{ "CHR_END" } = $record->{ "CHR_END" };
2537 $entry->{ "STRAND" } = $record->{ "STRAND" };
2538 $entry->{ "Q_ID" } = $record->{ "Q_ID" };
2539 $entry->{ "SCORE" } = $record->{ "SCORE" };
2541 Maasha::Biopieces::put_record( $entry, $out );
2549 sub script_get_genome_phastcons
2551 # Martin A. Hansen, February 2008.
2553 # Get phastcons scores from genome intervals.
2555 my ( $in, # handle to in stream
2556 $out, # handle to out stream
2557 $options, # options hash
2562 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $scores, $record );
2564 $options->{ "flank" } ||= 0;
2566 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
2567 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
2569 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
2570 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
2572 if ( defined $options->{ "chr" } and defined $options->{ "beg" } and ( defined $options->{ "end" } or defined $options->{ "len" } ) )
2574 $options->{ "beg" } -= 1; # request is 1-based
2575 $options->{ "end" } -= 1; # request is 1-based
2577 if ( $options->{ "len" } ) {
2578 $options->{ "end" } = $options->{ "beg" } + $options->{ "len" } - 1;
2581 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $options->{ "chr" }, $options->{ "beg" }, $options->{ "end" }, $options->{ "flank" } );
2583 $record->{ "CHR" } = $options->{ "chr" };
2584 $record->{ "CHR_BEG" } = $options->{ "beg" } - $options->{ "flank" };
2585 $record->{ "CHR_END" } = $options->{ "end" } + $options->{ "flank" };
2587 $record->{ "PHASTCONS" } = join ",", @{ $scores };
2588 $record->{ "PHAST_COUNT" } = scalar @{ $scores }; # DEBUG
2590 Maasha::Biopieces::put_record( $record, $out );
2593 while ( $record = Maasha::Biopieces::get_record( $in ) )
2595 if ( $record->{ "REC_TYPE" } eq "BED" )
2597 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" }, $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "flank" } );
2599 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
2601 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2603 elsif ( $record->{ "REC_TYPE" } eq "BLAST" )
2605 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "S_ID" }, $record->{ "S_BEG" }, $record->{ "S_END" }, $options->{ "flank" } );
2608 $record->{ "PHASTCONS" } = join ",", @{ $scores } if @{ $scores };
2609 # $record->{ "PHAST_COUNT" } = @{ $scores } if @{ $scores }; # DEBUG
2611 Maasha::Biopieces::put_record( $record, $out );
2614 close $fh_phastcons if $fh_phastcons;
2620 # Martin A. Hansen, December 2007.
2622 # Folds sequences in stream into secondary structures.
2624 my ( $in, # handle to in stream
2625 $out, # handle to out stream
2630 my ( $record, $type, $struct, $index );
2632 while ( $record = Maasha::Biopieces::get_record( $in ) )
2634 if ( $record->{ "SEQ" } )
2637 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } );
2640 if ( $type ne "protein" )
2642 ( $struct, $index ) = Maasha::Seq::fold_struct_rnafold( $record->{ "SEQ" } );
2643 $record->{ "SEC_STRUCT" } = $struct;
2644 $record->{ "FREE_ENERGY" } = $index;
2645 $record->{ "SCORE" } = abs int $index;
2646 $record->{ "SIZE" } = length $struct;
2647 $record->{ "CONF" } = "1," x $record->{ "SIZE" };
2651 Maasha::Biopieces::put_record( $record, $out );
2656 sub script_split_seq
2658 # Martin A. Hansen, August 2007.
2660 # Split a sequence in stream into words.
2662 my ( $in, # handle to in stream
2663 $out, # handle to out stream
2664 $options, # options hash
2669 my ( $record, $new_record, $i, $subseq, %lookup );
2671 $options->{ "word_size" } ||= 7;
2673 while ( $record = Maasha::Biopieces::get_record( $in ) )
2675 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2677 for ( $i = 0; $i < length( $record->{ "SEQ" } ) - $options->{ "word_size" } + 1; $i++ )
2679 $subseq = substr $record->{ "SEQ" }, $i, $options->{ "word_size" };
2681 if ( $options->{ "uniq" } and not $lookup{ $subseq } )
2683 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2684 $new_record->{ "SEQ" } = $subseq;
2686 Maasha::Biopieces::put_record( $new_record, $out );
2688 $lookup{ $subseq } = 1;
2692 $new_record->{ "SEQ_NAME" } = $record->{ "SEQ_NAME" } . "[" . ( $i + 1 ) . "-" . ( $i + $options->{ "word_size" } ) . "]";
2693 $new_record->{ "SEQ" } = $subseq;
2695 Maasha::Biopieces::put_record( $new_record, $out );
2701 Maasha::Biopieces::put_record( $record, $out );
2707 sub script_split_bed
2709 # Martin A. Hansen, June 2008.
2711 # Split a BED record into overlapping windows.
2713 my ( $in, # handle to in stream
2714 $out, # handle to out stream
2715 $options, # options hash
2720 my ( $record, $new_record, $i );
2722 $options->{ "window_size" } ||= 20;
2723 $options->{ "step_size" } ||= 1;
2725 while ( $record = Maasha::Biopieces::get_record( $in ) )
2727 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
2729 $record->{ "BED_LEN" } = $record->{ "CHR_END" } - $record->{ "CHR_BEG" } + 1;
2731 for ( $i = 0; $i < $record->{ "BED_LEN" } - $options->{ "window_size" }; $i += $options->{ "step_size" } )
2733 $new_record->{ "REC_TYPE" } = "BED";
2734 $new_record->{ "CHR" } = $record->{ "CHR" };
2735 $new_record->{ "CHR_BEG" } = $record->{ "CHR_BEG" } + $i;
2736 $new_record->{ "CHR_END" } = $record->{ "CHR_BEG" } + $i + $options->{ "window_size" };
2737 $new_record->{ "BED_LEN" } = $options->{ "window_size" };
2738 $new_record->{ "Q_ID" } = $record->{ "Q_ID" } . "_$i";
2739 $new_record->{ "SCORE" } = $record->{ "SCORE" };
2740 $new_record->{ "STRAND" } = $record->{ "STRAND" };
2742 Maasha::Biopieces::put_record( $new_record, $out );
2747 Maasha::Biopieces::put_record( $record, $out );
2753 sub script_align_seq
2755 # Martin A. Hansen, August 2007.
2757 # Align sequences in stream.
2759 my ( $in, # handle to in stream
2760 $out, # handle to out stream
2765 my ( $record, @entries, $entry );
2767 while ( $record = Maasha::Biopieces::get_record( $in ) )
2769 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
2770 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2771 } elsif ( $record->{ "Q_ID" } and $record->{ "SEQ" } ) {
2772 push @entries, [ $record->{ "Q_ID" }, $record->{ "SEQ" } ];
2774 Maasha::Biopieces::put_record( $record, $out );
2778 @entries = Maasha::Align::align( \@entries );
2780 foreach $entry ( @entries )
2782 if ( $entry->[ SEQ_NAME ] and $entry->[ SEQ ] )
2785 SEQ_NAME => $entry->[ SEQ_NAME ],
2786 SEQ => $entry->[ SEQ ],
2789 Maasha::Biopieces::put_record( $record, $out );
2797 # Martin A. Hansen, February 2008.
2799 # Using the first sequence in stream as reference, tile
2800 # all subsequent sequences based on pairwise alignments.
2802 my ( $in, # handle to in stream
2803 $out, # handle to out stream
2804 $options, # options hash
2809 my ( $record, $first, $ref_entry, @entries );
2813 while ( $record = Maasha::Biopieces::get_record( $in ) )
2815 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2819 $ref_entry = [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2825 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2830 Maasha::Biopieces::put_record( $record, $out );
2834 @entries = Maasha::Align::align_tile( $ref_entry, \@entries, $options );
2836 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2840 sub script_invert_align
2842 # Martin A. Hansen, February 2008.
2844 # Inverts an alignment showing only non-mathing residues
2845 # using the first sequence as reference.
2847 my ( $in, # handle to in stream
2848 $out, # handle to out stream
2849 $options, # options hash
2854 my ( $record, @entries );
2856 while ( $record = Maasha::Biopieces::get_record( $in ) )
2858 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } )
2860 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
2864 Maasha::Biopieces::put_record( $record, $out );
2868 Maasha::Align::align_invert( \@entries, $options->{ "soft" } );
2870 map { Maasha::Biopieces::put_record( { SEQ_NAME => $_->[ SEQ_NAME ], SEQ => $_->[ SEQ ] }, $out ) } @entries;
2874 sub script_patscan_seq
2876 # Martin A. Hansen, August 2007.
2878 # Locates patterns in sequences using scan_for_matches.
2880 my ( $in, # handle to in stream
2881 $out, # handle to out stream
2882 $options, # options hash
2887 my ( $genome_file, @args, $arg, $type, $seq_file, $pat_file, $out_file, $fh_in, $fh_out, $record, $patterns, $pattern, $entry, $result, %head_hash, $i );
2889 if ( $options->{ "patterns" } ) {
2890 $patterns = Maasha::Patscan::parse_patterns( $options->{ "patterns" } );
2891 } elsif ( -f $options->{ "patterns_in" } ) {
2892 $patterns = Maasha::Patscan::read_patterns( $options->{ "patterns_in" } );
2895 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna" if $options->{ 'genome' };
2897 push @args, "-c" if $options->{ "comp" };
2898 push @args, "-m $options->{ 'max_hits' }" if $options->{ 'max_hits' };
2899 push @args, "-n $options->{ 'max_misses' }" if $options->{ 'max_hits' };
2901 $seq_file = "$BP_TMP/patscan.seq";
2902 $pat_file = "$BP_TMP/patscan.pat";
2903 $out_file = "$BP_TMP/patscan.out";
2905 $fh_out = Maasha::Common::write_open( $seq_file );
2909 while ( $record = Maasha::Biopieces::get_record( $in ) )
2911 if ( $record->{ "SEQ" } and $record->{ "SEQ_NAME" } )
2913 $type = Maasha::Seq::seq_guess_type( $record->{ "SEQ" } ) if not $type;
2915 Maasha::Fasta::put_entry( [ $i, $record->{ "SEQ" } ], $fh_out );
2917 $head_hash{ $i } = $record->{ "SEQ_NAME" };
2925 $arg = join " ", @args;
2926 $arg .= " -p" if $type eq "protein";
2928 foreach $pattern ( @{ $patterns } )
2930 $fh_out = Maasha::Common::write_open( $pat_file );
2932 print $fh_out "$pattern\n";
2936 if ( $options->{ 'genome' } ) {
2937 `scan_for_matches $arg $pat_file < $genome_file > $out_file`;
2938 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $genome_file > $out_file" );
2940 `scan_for_matches $arg $pat_file < $seq_file > $out_file`;
2941 # Maasha::Common::run( "scan_for_matches", "$arg $pat_file < $seq_file > $out_file" );
2944 $fh_in = Maasha::Common::read_open( $out_file );
2946 while ( $entry = Maasha::Fasta::get_entry( $fh_in ) )
2948 $result = Maasha::Patscan::parse_scan_result( $entry, $pattern );
2950 if ( $options->{ 'genome' } )
2952 $result->{ "CHR" } = $result->{ "S_ID" };
2953 $result->{ "CHR_BEG" } = $result->{ "S_BEG" };
2954 $result->{ "CHR_END" } = $result->{ "S_END" };
2956 delete $result->{ "S_ID" };
2957 delete $result->{ "S_BEG" };
2958 delete $result->{ "S_END" };
2962 $result->{ "S_ID" } = $head_hash{ $result->{ "S_ID" } };
2965 Maasha::Biopieces::put_record( $result, $out );
2979 # Martin A. Hansen, August 2007.
2981 # BLATs sequences in stream against a given genome.
2983 my ( $in, # handle to in stream
2984 $out, # handle to out stream
2985 $options, # options hash
2990 my ( $blat_args, $genome_file, $query_file, $fh_in, $fh_out, $type, $record, $result_file, $entries, $entry );
2992 $genome_file = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
2994 $options->{ 'tile_size' } ||= 11;
2995 $options->{ 'one_off' } ||= 0;
2996 $options->{ 'min_identity' } ||= 90;
2997 $options->{ 'min_score' } ||= 0;
2998 $options->{ 'step_size' } ||= $options->{ 'tile_size' };
3000 $blat_args .= " -tileSize=$options->{ 'tile_size' }";
3001 $blat_args .= " -oneOff=$options->{ 'one_off' }";
3002 $blat_args .= " -minIdentity=$options->{ 'min_identity' }";
3003 $blat_args .= " -minScore=$options->{ 'min_score' }";
3004 $blat_args .= " -stepSize=$options->{ 'step_size' }";
3005 # $blat_args .= " -ooc=" . Maasha::Config::genome_blat_ooc( $options->{ "genome" }, 11 ) if $options->{ 'ooc' };
3007 $query_file = "$BP_TMP/blat.seq";
3009 $fh_out = Maasha::Common::write_open( $query_file );
3011 while ( $record = Maasha::Biopieces::get_record( $in ) )
3013 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3015 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not $type;
3016 Maasha::Fasta::put_entry( $entry, $fh_out, 80 );
3019 Maasha::Biopieces::put_record( $record, $out );
3024 $blat_args .= " -t=dnax" if $type eq "protein";
3025 $blat_args .= " -q=$type";
3027 $result_file = "$BP_TMP/blat.psl";
3029 Maasha::Common::run( "blat", "$genome_file $query_file $blat_args $result_file > /dev/null 2>&1" );
3033 $entries = Maasha::UCSC::psl_get_entries( $result_file );
3035 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $entries };
3037 unlink $result_file;
3043 # Martin A. Hansen, July 2008.
3045 # soap sequences in stream against a given file or genome.
3047 my ( $in, # handle to in stream
3048 $out, # handle to out stream
3049 $options, # options hash
3054 my ( $genome, $tmp_in, $tmp_out, $fh_in, $fh_out, $record, $line, @fields, $entry, $count, $args );
3056 $options->{ "seed_size" } ||= 10;
3057 $options->{ "mismatches" } ||= 2;
3058 $options->{ "gap_size" } ||= 0;
3059 $options->{ "cpus" } ||= 1;
3061 if ( $options->{ "genome" } ) {
3062 $options->{ "in_file" } = "$ENV{ 'BP_DATA' }/genomes/$options->{ 'genome' }/fasta/$options->{ 'genome' }.fna";
3065 $tmp_in = "$BP_TMP/soap_query.seq";
3066 $tmp_out = "$BP_TMP/soap.result";
3068 $fh_out = Maasha::Common::write_open( $tmp_in );
3072 while ( $record = Maasha::Biopieces::get_record( $in ) )
3074 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3076 Maasha::Fasta::put_entry( $entry, $fh_out );
3081 Maasha::Biopieces::put_record( $record, $out );
3089 "-s $options->{ 'seed_size' }",
3092 "-v $options->{ 'mismatches' }",
3093 "-g $options->{ 'gap_size' }",
3094 "-p $options->{ 'cpus' }",
3095 "-d $options->{ 'in_file' }",
3099 $args .= " > /dev/null 2>&1" if not $options->{ 'verbose' };
3101 Maasha::Common::run( "soap", $args, 1 );
3105 $fh_out = Maasha::Common::read_open( $tmp_out );
3109 while ( $line = <$fh_out> )
3113 @fields = split /\t/, $line;
3115 $record->{ "REC_TYPE" } = "SOAP";
3116 $record->{ "Q_ID" } = $fields[ 0 ];
3117 $record->{ "SCORE" } = $fields[ 3 ];
3118 $record->{ "STRAND" } = $fields[ 6 ];
3119 $record->{ "S_ID" } = $fields[ 7 ];
3120 $record->{ "S_BEG" } = $fields[ 8 ] - 1; # soap is 1-based
3121 $record->{ "S_END" } = $fields[ 8 ] + $fields[ 5 ] - 2;
3123 Maasha::Biopieces::put_record( $record, $out );
3133 sub script_match_seq
3135 # Martin A. Hansen, August 2007.
3137 # BLATs sequences in stream against a given genome.
3139 my ( $in, # handle to in stream
3140 $out, # handle to out stream
3141 $options, # options hash
3146 my ( $record, @entries, $results );
3148 $options->{ "word_size" } ||= 20;
3149 $options->{ "direction" } ||= "both";
3151 while ( $record = Maasha::Biopieces::get_record( $in ) )
3153 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3154 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3157 Maasha::Biopieces::put_record( $record, $out );
3160 if ( @entries == 1 )
3162 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 0 ] ], $options, $BP_TMP );
3164 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3166 elsif ( @entries == 2 )
3168 $results = Maasha::Match::match_mummer( [ $entries[ 0 ] ], [ $entries[ 1 ] ], $options, $BP_TMP );
3170 map { Maasha::Biopieces::put_record( $_, $out ) } @{ $results };
3175 sub script_create_vmatch_index
3177 # Martin A. Hansen, January 2008.
3179 # Create a vmatch index from sequences in the stream.
3181 my ( $in, # handle to in stream
3182 $out, # handle to out stream
3183 $options, # options hash
3188 my ( $record, $file_tmp, $fh_tmp, $type, $entry );
3190 if ( $options->{ "index_name" } )
3192 $file_tmp = $options->{ 'index_name' };
3193 $fh_tmp = Maasha::Common::write_open( $file_tmp );
3196 while ( $record = Maasha::Biopieces::get_record( $in ) )
3198 if ( $options->{ "index_name" } and $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3200 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3202 $type = Maasha::Seq::seq_guess_type( $entry->[ SEQ ] ) if not defined $type;
3205 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3208 if ( $options->{ "index_name" } )
3212 if ( $type eq "protein" ) {
3213 Maasha::Common::run( "mkvtree", "-db $file_tmp -protein -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3215 Maasha::Common::run( "mkvtree", "-db $file_tmp -dna -pl $options->{ 'prefix_length' } -allout -indexname $file_tmp > /dev/null 2>&1" );
3223 sub script_write_align
3225 # Martin A. Hansen, August 2007.
3227 # Write pretty alignments aligned sequences in stream.
3229 my ( $in, # handle to in stream
3230 $out, # handle to out stream
3231 $options, # options hash
3236 my ( $fh, $record, @entries );
3238 $fh = write_stream( $options->{ "data_out" } ) ;
3240 while ( $record = Maasha::Biopieces::get_record( $in ) )
3242 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3243 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3246 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3249 if ( scalar( @entries ) == 2 ) {
3250 Maasha::Align::align_print_pairwise( $entries[ 0 ], $entries[ 1 ], $fh, $options->{ "wrap" } );
3251 } elsif ( scalar ( @entries ) > 2 ) {
3252 Maasha::Align::align_print_multi( \@entries, $fh, $options->{ "wrap" }, $options->{ "no_ruler" }, $options->{ "no_consensus" } );
3259 sub script_write_bed
3261 # Martin A. Hansen, August 2007.
3263 # Write BED format for the UCSC genome browser using records in stream.
3265 my ( $in, # handle to in stream
3266 $out, # handle to out stream
3267 $options, # options hash
3272 my ( $cols, $fh, $record, $bed_entry, $new_record );
3274 $cols = $options->{ 'cols' }->[ 0 ];
3276 $fh = Maasha::Biopieces::write_stream( $options->{ 'data_out' }, $options->{ 'compress' } );
3278 while ( $record = Maasha::Biopieces::get_record( $in ) )
3280 $record = Maasha::UCSC::psl2record( $record ) if $record->{ 'tBaseInsert' }; # Dirty addition to allow Affy data from MySQL to be dumped
3282 if ( $bed_entry = Maasha::UCSC::BED::biopiece2bed( $record, $cols ) ) {
3283 Maasha::UCSC::BED::bed_entry_put( $bed_entry, $fh, $cols, $options->{ 'check' } );
3286 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3293 sub script_write_psl
3295 # Martin A. Hansen, August 2007.
3297 # Write PSL output from stream.
3299 my ( $in, # handle to in stream
3300 $out, # handle to out stream
3301 $options, # options hash
3306 my ( $fh, $record, @output, $first );
3310 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3312 while ( $record = Maasha::Biopieces::get_record( $in ) )
3314 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3316 if ( $record->{ "REC_TYPE" } and $record->{ "REC_TYPE" } eq "PSL" )
3318 Maasha::UCSC::psl_put_header( $fh ) if $first;
3319 Maasha::UCSC::psl_put_entry( $record, $fh );
3328 sub script_write_fixedstep
3330 # Martin A. Hansen, Juli 2008.
3332 # Write fixedStep entries from recrods in the stream.
3334 my ( $in, # handle to in stream
3335 $out, # handle to out stream
3336 $options, # options hash
3341 my ( $fh, $record, $entry );
3343 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3345 while ( $record = Maasha::Biopieces::get_record( $in ) )
3347 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
3348 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh );
3351 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3358 sub script_write_2bit
3360 # Martin A. Hansen, March 2008.
3362 # Write sequence entries from stream in 2bit format.
3364 my ( $in, # handle to in stream
3365 $out, # handle to out stream
3366 $options, # options hash
3371 my ( $record, $mask, $tmp_file, $fh_tmp, $fh_in, $fh_out, $entry );
3373 $mask = 1 if not $options->{ "no_mask" };
3375 $tmp_file = "$BP_TMP/write_2bit.fna";
3376 $fh_tmp = Maasha::Common::write_open( $tmp_file );
3378 $fh_out = write_stream( $options->{ "data_out" } );
3380 while ( $record = Maasha::Biopieces::get_record( $in ) )
3382 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) ) {
3383 Maasha::Fasta::put_entry( $entry, $fh_tmp );
3386 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3391 $fh_in = Maasha::Common::read_open( $tmp_file );
3393 Maasha::TwoBit::fasta2twobit( $fh_in, $fh_out, $mask );
3402 sub script_write_solid
3404 # Martin A. Hansen, April 2008.
3406 # Write di-base encoded Solid sequence from entries in stream.
3408 my ( $in, # handle to in stream
3409 $out, # handle to out stream
3410 $options, # options hash
3415 my ( $record, $fh, $entry );
3417 $fh = write_stream( $options->{ "data_out" }, $options->{ "compress" } );
3419 while ( $record = Maasha::Biopieces::get_record( $in ) )
3421 if ( $entry = Maasha::Fasta::biopiece2fasta( $record ) )
3423 $entry->[ SEQ ] = Maasha::Solid::seq2color_space( uc $entry->[ SEQ ] );
3425 Maasha::Fasta::put_entry( $entry, $fh, $options->{ "wrap" } );
3428 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3435 sub script_write_ucsc_config
3437 # Martin A. Hansen, November 2008.
3439 # Write UCSC Genome Broser configuration (.ra file type) from
3440 # records in the stream.
3442 my ( $in, # handle to in stream
3443 $out, # handle to out stream
3444 $options, # options hash
3449 my ( $record, $fh );
3451 $fh = write_stream( $options->{ "data_out" } );
3453 while ( $record = Maasha::Biopieces::get_record( $in ) )
3455 Maasha::UCSC::ucsc_config_entry_put( $record, $fh ) if $record->{ "REC_TYPE" } eq "UCSC Config";
3457 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3464 sub script_plot_seqlogo
3466 # Martin A. Hansen, August 2007.
3468 # Calculates and writes a sequence logo for alignments.
3470 my ( $in, # handle to in stream
3471 $out, # handle to out stream
3472 $options, # options hash
3477 my ( $record, @entries, $logo, $fh );
3479 while ( $record = Maasha::Biopieces::get_record( $in ) )
3481 if ( $record->{ "SEQ_NAME" } and $record->{ "SEQ" } ) {
3482 push @entries, [ $record->{ "SEQ_NAME" }, $record->{ "SEQ" } ];
3485 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3488 $logo = Maasha::Plot::seq_logo( \@entries );
3490 $fh = write_stream( $options->{ "data_out" } );
3498 sub script_plot_phastcons_profiles
3500 # Martin A. Hansen, January 2008.
3502 # Plots PhastCons profiles.
3504 my ( $in, # handle to in stream
3505 $out, # handle to out stream
3506 $options, # options hash
3511 my ( $phastcons_file, $phastcons_index, $index, $fh_phastcons, $record, $scores, $AoA, $plot, $fh );
3513 $options->{ "title" } ||= "PhastCons Profiles";
3515 $phastcons_file = Maasha::Config::genome_phastcons( $options->{ "genome" } );
3516 $phastcons_index = Maasha::Config::genome_phastcons_index( $options->{ "genome" } );
3518 $index = Maasha::UCSC::fixedstep_index_retrieve( $phastcons_index );
3519 $fh_phastcons = Maasha::Common::read_open( $phastcons_file );
3521 while ( $record = Maasha::Biopieces::get_record( $in ) )
3523 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
3525 $scores = Maasha::UCSC::fixedstep_index_lookup( $index, $fh_phastcons, $record->{ "CHR" },
3526 $record->{ "CHR_BEG" },
3527 $record->{ "CHR_END" },
3528 $options->{ "flank" } );
3530 push @{ $AoA }, [ @{ $scores } ];
3533 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3536 Maasha::UCSC::phastcons_normalize( $AoA );
3538 $AoA = [ [ Maasha::UCSC::phastcons_mean( $AoA ) ] ] if $options->{ "mean" };
3539 $AoA = [ [ Maasha::UCSC::phastcons_median( $AoA ) ] ] if $options->{ "median" };
3541 $AoA = Maasha::Matrix::matrix_flip( $AoA );
3543 $plot = Maasha::Plot::lineplot_simple( $AoA, $options, $BP_TMP );
3545 $fh = write_stream( $options->{ "data_out" } );
3547 print $fh "$_\n" foreach @{ $plot };
3553 sub script_analyze_bed
3555 # Martin A. Hansen, March 2008.
3557 # Analyze BED entries in stream.
3559 my ( $in, # handle to in stream
3560 $out, # handle to out stream
3561 $options, # options hash
3568 while ( $record = Maasha::Biopieces::get_record( $in ) )
3570 $record = Maasha::UCSC::bed_analyze( $record ) if $record->{ "REC_TYPE" } eq "BED";
3572 Maasha::Biopieces::put_record( $record, $out );
3577 sub script_analyze_vals
3579 # Martin A. Hansen, August 2007.
3581 # Analyze values for given keys in stream.
3583 my ( $in, # handle to in stream
3584 $out, # handle to out stream
3585 $options, # options hash
3590 my ( $record, $key, @keys, %key_hash, $analysis, $len );
3592 map { $key_hash{ $_ } = 1 } @{ $options->{ "keys" } };
3594 while ( $record = Maasha::Biopieces::get_record( $in ) )
3596 foreach $key ( keys %{ $record } )
3598 next if $options->{ "keys" } and not exists $key_hash{ $key };
3600 $analysis->{ $key }->{ "COUNT" }++;
3602 if ( Maasha::Calc::is_a_number( $record->{ $key } ) )
3604 $analysis->{ $key }->{ "TYPE" } = "num";
3605 $analysis->{ $key }->{ "SUM" } += $record->{ $key };
3606 $analysis->{ $key }->{ "MAX" } = $record->{ $key } if $record->{ $key } > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3607 $analysis->{ $key }->{ "MIN" } = $record->{ $key } if $record->{ $key } < $analysis->{ $key }->{ "MIN" } or not $analysis->{ $key }->{ "MIN" };
3611 $len = length $record->{ $key };
3613 $analysis->{ $key }->{ "TYPE" } = "alph";
3614 $analysis->{ $key }->{ "SUM" } += $len;
3615 $analysis->{ $key }->{ "MAX" } = $len if $len > $analysis->{ $key }->{ "MAX" } or not $analysis->{ $key }->{ "MAX" };
3616 $analysis->{ $key }->{ "MIN" } = $len if $len < $analysis->{ $key }->{ "MIM" } or not $analysis->{ $key }->{ "MIN" };
3620 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
3623 foreach $key ( keys %{ $analysis } )
3625 $analysis->{ $key }->{ "MEAN" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" } / $analysis->{ $key }->{ "COUNT" };
3626 $analysis->{ $key }->{ "SUM" } = sprintf "%.2f", $analysis->{ $key }->{ "SUM" };
3629 my ( $keys, $types, $counts, $mins, $maxs, $sums, $means );
3639 if ( $options->{ "keys" } ) {
3640 @keys = @{ $options->{ "keys" } };
3642 @keys = keys %{ $analysis };
3645 foreach $key ( @keys )
3647 $keys .= sprintf "% 15s", $key;
3648 $types .= sprintf "% 15s", $analysis->{ $key }->{ "TYPE" };
3649 $counts .= sprintf "% 15s", $analysis->{ $key }->{ "COUNT" };
3650 $mins .= sprintf "% 15s", $analysis->{ $key }->{ "MIN" };
3651 $maxs .= sprintf "% 15s", $analysis->{ $key }->{ "MAX" };
3652 $sums .= sprintf "% 15s", $analysis->{ $key }->{ "SUM" };
3653 $means .= sprintf "% 15s", $analysis->{ $key }->{ "MEAN" };
3656 print $out "$keys\n";
3657 print $out "$types\n";
3658 print $out "$counts\n";
3659 print $out "$mins\n";
3660 print $out "$maxs\n";
3661 print $out "$sums\n";
3662 print $out "$means\n";
3666 sub script_head_records
3668 # Martin A. Hansen, August 2007.
3670 # Display the first sequences in stream.
3672 my ( $in, # handle to in stream
3673 $out, # handle to out stream
3674 $options, # options hash
3679 my ( $record, $count );
3681 $options->{ "num" } ||= 10;
3685 while ( $record = Maasha::Biopieces::get_record( $in ) )
3689 Maasha::Biopieces::put_record( $record, $out );
3691 last if $count == $options->{ "num" };
3696 sub script_remove_keys
3698 # Martin A. Hansen, August 2007.
3700 # Remove keys from stream.
3702 my ( $in, # handle to in stream
3703 $out, # handle to out stream
3704 $options, # options hash
3709 my ( $record, $new_record );
3711 while ( $record = Maasha::Biopieces::get_record( $in ) )
3713 if ( $options->{ "keys" } )
3715 map { delete $record->{ $_ } } @{ $options->{ "keys" } };
3717 elsif ( $options->{ "save_keys" } )
3719 map { $new_record->{ $_ } = $record->{ $_ } if exists $record->{ $_ } } @{ $options->{ "save_keys" } };
3721 $record = $new_record;
3724 Maasha::Biopieces::put_record( $record, $out ) if keys %{ $record };
3729 sub script_remove_adaptor
3731 # Martin A. Hansen, August 2008.
3733 # Find and remove adaptor from sequences in the stream.
3735 my ( $in, # handle to in stream
3736 $out, # handle to out stream
3737 $options, # options hash
3742 my ( $record, $adaptor, $seq, $adaptor_len, $seq_len, $offset, $max_match, $max_mismatch, $pos );
3744 $options->{ "remove" } ||= "after";
3746 $max_mismatch = $options->{ "mismatches" } || 0;
3747 $offset = $options->{ "offset" };
3749 if ( not defined $offset ) {
3755 $adaptor = uc $options->{ "adaptor" };
3756 $adaptor_len = length $adaptor;
3758 while ( $record = Maasha::Biopieces::get_record( $in ) )
3760 if ( $record->{ "SEQ" } )
3762 $seq = uc $record->{ "SEQ" };
3763 $seq_len = length $seq;
3765 $pos = Maasha::Common::index_m( $seq, $adaptor, $seq_len, $adaptor_len, $offset, $max_mismatch );
3767 $record->{ "ADAPTOR_POS" } = $pos;
3769 if ( $pos >= 0 and $options->{ "remove" } ne "skip" )
3771 if ( $options->{ "remove" } eq "after" )
3773 $record->{ "SEQ" } = substr $record->{ "SEQ" }, 0, $pos;
3774 $record->{ "SEQ_LEN" } = $pos;
3778 $record->{ "SEQ" } = substr $record->{ "SEQ" }, $pos + $adaptor_len;
3779 $record->{ "SEQ_LEN" } = length $record->{ "SEQ" };
3783 Maasha::Biopieces::put_record( $record, $out );
3787 Maasha::Biopieces::put_record( $record, $out );
3793 sub script_remove_mysql_tables
3795 # Martin A. Hansen, November 2008.
3797 # Remove MySQL tables from values in stream.
3799 my ( $in, # handle to in stream
3800 $out, # handle to out stream
3801 $options, # options hash
3806 my ( $record, %table_hash, $dbh, $table );
3808 $options->{ "user" } ||= Maasha::UCSC::ucsc_get_user();
3809 $options->{ "password" } ||= Maasha::UCSC::ucsc_get_password();
3811 map { $table_hash{ $_ } = 1 } @{ $options->{ 'tables' } };
3813 while ( $record = Maasha::Biopieces::get_record( $in ) )
3815 map { $table_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3817 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3820 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3822 foreach $table ( sort keys %table_hash )
3824 if ( Maasha::SQL::table_exists( $dbh, $table ) )
3826 print STDERR qq(Removing table "$table" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3827 Maasha::SQL::delete_table( $dbh, $table );
3828 print STDERR "done.\n" if $options->{ 'verbose' };
3832 print STDERR qq(WARNING: table "$table" not found in database "$options->{ 'database' }\n");
3836 Maasha::SQL::disconnect( $dbh );
3840 sub script_remove_ucsc_tracks
3842 # Martin A. Hansen, November 2008.
3844 # Remove track from MySQL tables and config file.
3846 my ( $in, # handle to in stream
3847 $out, # handle to out stream
3848 $options, # options hash
3853 my ( $record, %track_hash, $fh_in, $fh_out, $track, @tracks, @new_tracks, $dbh );
3855 $options->{ 'user' } ||= Maasha::UCSC::ucsc_get_user();
3856 $options->{ 'password' } ||= Maasha::UCSC::ucsc_get_password();
3857 $options->{ 'config_file' } ||= "$ENV{ 'HOME' }/ucsc/my_tracks.ra";
3859 map { $track_hash{ $_ } = 1 } @{ $options->{ 'tracks' } };
3861 while ( $record = Maasha::Biopieces::get_record( $in ) )
3863 map { $track_hash{ $record->{ $_ } } = 1 } @{ $options->{ 'keys' } };
3865 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ 'no_stream' };
3868 $fh_in = Maasha::Filesys::file_read_open( $options->{ 'config_file' } );
3870 while ( $track = Maasha::UCSC::ucsc_config_entry_get( $fh_in ) ) {
3871 push @tracks, $track;
3876 foreach $track ( @tracks )
3878 if ( $track->{ 'database' } eq $options->{ 'database' } and exists $track_hash{ $track->{ 'track' } } ) {
3879 print STDERR qq(Removing track "$track->{ 'track' }" from config file.\n) if $options->{ 'verbose' };
3881 push @new_tracks, $track;
3885 rename "$options->{ 'config_file' }", "$options->{ 'config_file' }~";
3887 $fh_out = Maasha::Filesys::file_write_open( $options->{ 'config_file' } );
3889 map { Maasha::UCSC::ucsc_config_entry_put( $_, $fh_out ) } @new_tracks;
3893 # ---- locate track in database ----
3895 $dbh = Maasha::SQL::connect( $options->{ "database" }, $options->{ "user" }, $options->{ "password" } );
3897 foreach $track ( sort keys %track_hash )
3899 if ( Maasha::SQL::table_exists( $dbh, $track ) )
3901 print STDERR qq(Removing table "$track" from database "$options->{ 'database' }" ... ) if $options->{ 'verbose' };
3902 Maasha::SQL::delete_table( $dbh, $track );
3903 print STDERR "done.\n" if $options->{ 'verbose' };
3907 print STDERR qq(WARNING: table "$track" not found in database "$options->{ 'database' }\n");
3911 Maasha::SQL::disconnect( $dbh );
3913 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
3917 sub script_rename_keys
3919 # Martin A. Hansen, August 2007.
3921 # Rename keys in stream.
3923 my ( $in, # handle to in stream
3924 $out, # handle to out stream
3925 $options, # options hash
3932 while ( $record = Maasha::Biopieces::get_record( $in ) )
3934 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
3936 $record->{ $options->{ "keys" }->[ 1 ] } = $record->{ $options->{ "keys" }->[ 0 ] };
3938 delete $record->{ $options->{ "keys" }->[ 0 ] };
3941 Maasha::Biopieces::put_record( $record, $out );
3946 sub script_uniq_vals
3948 # Martin A. Hansen, August 2007.
3950 # Find unique values in stream.
3952 my ( $in, # handle to in stream
3953 $out, # handle to out stream
3954 $options, # options hash
3959 my ( %hash, $record );
3961 while ( $record = Maasha::Biopieces::get_record( $in ) )
3963 if ( $record->{ $options->{ "key" } } )
3965 if ( not $hash{ $record->{ $options->{ "key" } } } and not $options->{ "invert" } )
3967 Maasha::Biopieces::put_record( $record, $out );
3969 $hash{ $record->{ $options->{ "key" } } } = 1;
3971 elsif ( $hash{ $record->{ $options->{ "key" } } } and $options->{ "invert" } )
3973 Maasha::Biopieces::put_record( $record, $out );
3977 $hash{ $record->{ $options->{ "key" } } } = 1;
3982 Maasha::Biopieces::put_record( $record, $out );
3988 sub script_merge_vals
3990 # Martin A. Hansen, August 2007.
3992 # Rename keys in stream.
3994 my ( $in, # handle to in stream
3995 $out, # handle to out stream
3996 $options, # options hash
4001 my ( $record, @join, $i );
4003 $options->{ "delimit" } ||= '_';
4005 while ( $record = Maasha::Biopieces::get_record( $in ) )
4007 if ( exists $record->{ $options->{ "keys" }->[ 0 ] } )
4009 @join = $record->{ $options->{ "keys" }->[ 0 ] };
4011 for ( $i = 1; $i < @{ $options->{ "keys" } }; $i++ ) {
4012 push @join, $record->{ $options->{ "keys" }->[ $i ] } if exists $record->{ $options->{ "keys" }->[ $i ] };
4015 $record->{ $options->{ "keys" }->[ 0 ] } = join $options->{ "delimit" }, @join;
4018 Maasha::Biopieces::put_record( $record, $out );
4023 sub script_merge_records
4025 # Martin A. Hansen, July 2008.
4027 # Merges records in the stream based on identical values of two given keys.
4029 my ( $in, # handle to in stream
4030 $out, # handle to out stream
4031 $options, # options hash
4036 my ( $merge, $record, $file1, $file2, $fh1, $fh2, $key1, $key2, @keys1, @keys2, @vals1, @vals2,
4037 $num1, $num2, $num, $cmp, $i );
4039 $merge = $options->{ "merge" } || "AandB";
4041 $file1 = "$BP_TMP/merge_records1.tmp";
4042 $file2 = "$BP_TMP/merge_records2.tmp";
4044 $fh1 = Maasha::Common::write_open( $file1 );
4045 $fh2 = Maasha::Common::write_open( $file2 );
4047 $key1 = $options->{ "keys" }->[ 0 ];
4048 $key2 = $options->{ "keys" }->[ 1 ];
4050 $num = $key2 =~ s/n$//;
4054 while ( $record = Maasha::Biopieces::get_record( $in ) )
4056 if ( exists $record->{ $key1 } )
4059 @vals1 = $record->{ $key1 };
4061 delete $record->{ $key1 };
4063 map { push @keys1, $_; push @vals1, $record->{ $_ } } keys %{ $record };
4065 print $fh1 join( "\t", @vals1 ), "\n";
4069 elsif ( exists $record->{ $key2 } )
4072 @vals2 = $record->{ $key2 };
4074 delete $record->{ $key2 };
4076 map { push @keys2, $_; push @vals2, $record->{ $_ } } keys %{ $record };
4078 print $fh2 join( "\t", @vals2 ), "\n";
4089 Maasha::Common::run( "sort", "-k 1,1n $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4090 Maasha::Common::run( "sort", "-k 1,1n $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4094 Maasha::Common::run( "sort", "-k 1,1 $file1 > $file1.sort" ) and rename "$file1.sort", $file1;
4095 Maasha::Common::run( "sort", "-k 1,1 $file2 > $file2.sort" ) and rename "$file2.sort", $file2;
4098 $fh1 = Maasha::Common::read_open( $file1 );
4099 $fh2 = Maasha::Common::read_open( $file2 );
4101 @vals1 = Maasha::Common::get_fields( $fh1 );
4102 @vals2 = Maasha::Common::get_fields( $fh2 );
4104 while ( $num1 > 0 and $num2 > 0 )
4109 $cmp = $vals1[ 0 ] <=> $vals2[ 0 ];
4111 $cmp = $vals1[ 0 ] cmp $vals2[ 0 ];
4116 if ( $merge =~ /^(AorB|AnotB)$/ )
4118 for ( $i = 0; $i < @keys1; $i++ ) {
4119 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4122 Maasha::Biopieces::put_record( $record, $out );
4125 @vals1 = Maasha::Common::get_fields( $fh1 );
4130 if ( $merge =~ /^(BorA|BnotA)$/ )
4132 for ( $i = 0; $i < @keys2; $i++ ) {
4133 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4136 Maasha::Biopieces::put_record( $record, $out );
4139 @vals2 = Maasha::Common::get_fields( $fh2 );
4144 if ( $merge =~ /^(AandB|AorB|BorA)$/ )
4146 for ( $i = 0; $i < @keys1; $i++ ) {
4147 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4150 for ( $i = 1; $i < @keys2; $i++ ) {
4151 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4154 Maasha::Biopieces::put_record( $record, $out );
4157 @vals1 = Maasha::Common::get_fields( $fh1 );
4158 @vals2 = Maasha::Common::get_fields( $fh2 );
4170 if ( $num1 > 0 and $merge =~ /^(AorB|AnotB)$/ )
4174 for ( $i = 0; $i < @keys1; $i++ ) {
4175 $record->{ $keys1[ $i ] } = $vals1[ $i ];
4178 Maasha::Biopieces::put_record( $record, $out );
4181 if ( $num2 > 0 and $merge =~ /^(BorA|BnotA)$/ )
4185 for ( $i = 0; $i < @keys2; $i++ ) {
4186 $record->{ $keys2[ $i ] } = $vals2[ $i ];
4189 Maasha::Biopieces::put_record( $record, $out );
4196 # Martin A. Hansen, August 2007.
4198 # Evaluate extression for records in stream.
4200 my ( $in, # handle to in stream
4201 $out, # handle to out stream
4202 $options, # options hash
4207 my ( $record, $eval_key, @keys, $eval_val );
4209 while ( $record = Maasha::Biopieces::get_record( $in ) )
4211 if ( $options->{ "eval" } )
4213 if ( $options->{ "eval" } =~ /^(\S+)\s*=\s*(.+)$/ )
4220 @keys = split /\s+|\+|-|\*|\/|\*\*/, $eval_val;
4222 @keys = grep { exists $record->{ $_ } } @keys;
4225 map { $eval_val =~ s/\Q$_\E/$record->{ $_ }/g } @keys;
4227 $record->{ $eval_key } = eval "$eval_val";
4228 Maasha::Common::error( qq(eval "$eval_key = $eval_val" failed -> $@) ) if $@;
4232 Maasha::Common::error( qq(Bad compute expression: "$options->{ 'eval' }"\n) );
4236 Maasha::Biopieces::put_record( $record, $out );
4243 # Martin A. Hansen, June 2008.
4247 my ( $in, # handle to in stream
4248 $out, # handle to out stream
4249 $options, # options hash
4254 my ( $record, $key, $A, $B, @rows, @matrix, $row, $i );
4256 while ( $record = Maasha::Biopieces::get_record( $in ) )
4260 foreach $key ( sort { $A = $a; $B = $b; $A =~ s/^V(\d+)$/$1/; $B =~ s/^V(\d+)$/$1/; $A <=> $B } keys %{ $record } )
4262 push @rows, $record->{ $key };
4266 push @matrix, [ @rows ];
4271 @matrix = Maasha::Matrix::matrix_flip( \@matrix );
4273 foreach $row ( @matrix )
4275 for ( $i = 0; $i < @{ $row }; $i++ ) {
4276 $record->{ "V$i" } = $row->[ $i ];
4279 Maasha::Biopieces::put_record( $record, $out );
4284 sub script_add_ident
4286 # Martin A. Hansen, May 2008.
4288 # Add a unique identifier to each record in stream.
4290 my ( $in, # handle to in stream
4291 $out, # handle to out stream
4292 $options, # options hash
4297 my ( $record, $key, $prefix, $i );
4299 $key = $options->{ "key" } || "ID";
4300 $prefix = $options->{ "prefix" } || "ID";
4304 while ( $record = Maasha::Biopieces::get_record( $in ) )
4306 $record->{ $key } = sprintf( "$prefix%08d", $i );
4308 Maasha::Biopieces::put_record( $record, $out );
4315 sub script_count_records
4317 # Martin A. Hansen, August 2007.
4319 # Count records in stream.
4321 my ( $in, # handle to in stream
4322 $out, # handle to out stream
4323 $options, # options hash
4328 my ( $record, $count, $result, $fh, $line );
4332 if ( $options->{ "no_stream" } )
4334 while ( $line = <$in> )
4338 $count++ if $line eq "---";
4343 while ( $record = Maasha::Biopieces::get_record( $in ) )
4345 Maasha::Biopieces::put_record( $record, $out );
4351 $result = { "RECORDS_COUNT" => $count };
4353 $fh = write_stream( $options->{ "data_out" } );
4355 Maasha::Biopieces::put_record( $result, $fh );
4361 sub script_random_records
4363 # Martin A. Hansen, August 2007.
4365 # Pick a number or random records from stream.
4367 my ( $in, # handle to in stream
4368 $out, # handle to out stream
4369 $options, # options hash
4374 my ( $record, $tmp_file, $fh_out, $fh_in, $count, $i, %rand_hash, $rand, $max );
4376 $options->{ "num" } ||= 10;
4378 $tmp_file = "$BP_TMP/random_records.tmp";
4380 $fh_out = Maasha::Common::write_open( $tmp_file );
4384 while ( $record = Maasha::Biopieces::get_record( $in ) )
4386 Maasha::Biopieces::put_record( $record, $fh_out );
4396 Maasha::Common::error( qq(Requested random records > records in stream) ) if $options->{ "num" } > $count;
4398 while ( $i < $options->{ "num" } )
4400 $rand = int( rand( $count ) );
4402 if ( not exists $rand_hash{ $rand } )
4404 $rand_hash{ $rand } = 1;
4406 $max = $rand if $rand > $max;
4412 $fh_in = Maasha::Common::read_open( $tmp_file );
4416 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4418 Maasha::Biopieces::put_record( $record, $out ) if exists $rand_hash{ $count };
4420 last if $count == $max;
4431 sub script_sort_records
4433 # Martin A. Hansen, August 2007.
4435 # Sort to sort records according to keys.
4437 my ( $in, # handle to in stream
4438 $out, # handle to out stream
4439 $options, # options hash
4444 my ( @keys, $key, @sort_cmd, $sort_str, $sort_sub, @records, $record, $i );
4446 foreach $key ( @{ $options->{ "keys" } } )
4448 if ( $key =~ s/n$// ) {
4449 push @sort_cmd, qq(\$a->{ "$key" } <=> \$b->{ "$key" });
4451 push @sort_cmd, qq(\$a->{ "$key" } cmp \$b->{ "$key" });
4455 $sort_str = join " or ", @sort_cmd;
4456 $sort_sub = eval "sub { $sort_str }"; # NB security issue!
4458 while ( $record = Maasha::Biopieces::get_record( $in ) ) {
4459 push @records, $record;
4462 @records = sort $sort_sub @records;
4464 if ( $options->{ "reverse" } )
4466 for ( $i = scalar @records - 1; $i >= 0; $i-- ) {
4467 Maasha::Biopieces::put_record( $records[ $i ], $out );
4472 for ( $i = 0; $i < scalar @records; $i++ ) {
4473 Maasha::Biopieces::put_record( $records[ $i ], $out );
4479 sub script_count_vals
4481 # Martin A. Hansen, August 2007.
4483 # Count records in stream.
4485 my ( $in, # handle to in stream
4486 $out, # handle to out stream
4487 $options, # options hash
4492 my ( $num, $record, %count_hash, @records, $tmp_file, $fh_out, $fh_in, $cache );
4494 $tmp_file = "$BP_TMP/count_cache.tmp";
4496 $fh_out = Maasha::Common::write_open( $tmp_file );
4501 while ( $record = Maasha::Biopieces::get_record( $in ) )
4503 map { $count_hash{ $_ }{ $record->{ $_ } }++ if exists $record->{ $_ } } @{ $options->{ "keys" } };
4505 push @records, $record;
4507 if ( scalar @records > 5_000_000 ) # too many records to hold in memory - use disk cache
4509 map { Maasha::Biopieces::put_record( $_, $fh_out ) } @records;
4516 print STDERR "verbose: records read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4527 $fh_in = Maasha::Common::read_open( $tmp_file );
4529 while ( $record = Maasha::Biopieces::get_record( $fh_in ) )
4531 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4533 Maasha::Biopieces::put_record( $record, $out );
4535 print STDERR "verbose: cache read $num\n" if ( $options->{ 'verbose' } and ( $num % 1_000_000 ) == 0 );
4543 foreach $record ( @records )
4545 map { $record->{ $_ . "_COUNT" } = $count_hash{ $_ }{ $record->{ $_ } } if exists $record->{ $_ } } @{ $options->{ "keys" } };
4547 Maasha::Biopieces::put_record( $record, $out );
4554 sub script_plot_histogram
4556 # Martin A. Hansen, September 2007.
4558 # Plot a simple histogram for a given key using GNU plot.
4560 my ( $in, # handle to in stream
4561 $out, # handle to out stream
4562 $options, # options hash
4567 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4569 $options->{ "title" } ||= "Histogram";
4570 $options->{ "sort" } ||= "num";
4572 while ( $record = Maasha::Biopieces::get_record( $in ) )
4574 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4576 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4579 if ( $options->{ "sort" } eq "num" ) {
4580 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort { $a <=> $b } keys %data_hash;
4582 map { push @data_list, [ $_, $data_hash{ $_ } ] } sort keys %data_hash;
4585 $result = Maasha::Plot::histogram_simple( \@data_list, $options );
4587 $fh = write_stream( $options->{ "data_out" } );
4589 print $fh "$_\n" foreach @{ $result };
4595 sub script_plot_lendist
4597 # Martin A. Hansen, August 2007.
4599 # Plot length distribution using GNU plot.
4601 my ( $in, # handle to in stream
4602 $out, # handle to out stream
4603 $options, # options hash
4608 my ( $record, %data_hash, $max, @data_list, $i, $result, $fh );
4610 $options->{ "title" } ||= "Length Distribution";
4612 while ( $record = Maasha::Biopieces::get_record( $in ) )
4614 $data_hash{ $record->{ $options->{ "key" } } }++ if defined $record->{ $options->{ "key" } };
4616 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4619 $max = Maasha::Calc::list_max( [ keys %data_hash ] );
4621 for ( $i = 0; $i < $max; $i++ ) {
4622 push @data_list, [ $i, $data_hash{ $i } || 0 ];
4625 $result = Maasha::Plot::histogram_lendist( \@data_list, $options );
4627 $fh = write_stream( $options->{ "data_out" } );
4629 print $fh "$_\n" foreach @{ $result };
4635 sub script_plot_chrdist
4637 # Martin A. Hansen, August 2007.
4639 # Plot chromosome distribution using GNU plot.
4641 my ( $in, # handle to in stream
4642 $out, # handle to out stream
4643 $options, # options hash
4648 my ( $record, %data_hash, @data_list, $elem, $sort_key, $count, $result, $fh );
4650 $options->{ "title" } ||= "Chromosome Distribution";
4652 while ( $record = Maasha::Biopieces::get_record( $in ) )
4654 if ( $record->{ "CHR" } ) { # generic
4655 $data_hash{ $record->{ "CHR" } }++;
4656 } elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "S_ID" } =~ /^chr/i ) { # patscan
4657 $data_hash{ $record->{ "S_ID" } }++;
4658 } elsif ( $record->{ "REC_TYPE" } eq "PSL" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAT / PSL
4659 $data_hash{ $record->{ "S_ID" } }++;
4660 } elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/i ) { # BLAST
4661 $data_hash{ $record->{ "S_ID" } }++;
4664 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4667 foreach $elem ( keys %data_hash )
4671 $sort_key =~ s/chr//i;
4673 $sort_key =~ s/^X(.*)/99$1/;
4674 $sort_key =~ s/^Y(.*)/99$1/;
4675 $sort_key =~ s/^Z(.*)/999$1/;
4676 $sort_key =~ s/^M(.*)/9999$1/;
4677 $sort_key =~ s/^U(.*)/99999$1/;
4679 $count = $sort_key =~ tr/_//;
4681 $sort_key =~ s/_.*/"999999" x $count/ex;
4683 push @data_list, [ $elem, $data_hash{ $elem }, $sort_key ];
4686 @data_list = sort { $a->[ 2 ] <=> $b->[ 2 ] } @data_list;
4688 $result = Maasha::Plot::histogram_chrdist( \@data_list, $options );
4690 $fh = write_stream( $options->{ "data_out" } );
4692 print $fh "$_\n" foreach @{ $result };
4698 sub script_plot_karyogram
4700 # Martin A. Hansen, August 2007.
4702 # Plot hits on karyogram.
4704 my ( $in, # handle to in stream
4705 $out, # handle to out stream
4706 $options, # options hash
4711 my ( %options, $record, @data, $fh, $result, %data_hash );
4713 $options->{ "genome" } ||= "human";
4714 $options->{ "feat_color" } ||= "black";
4716 while ( $record = Maasha::Biopieces::get_record( $in ) )
4718 if ( $record->{ "CHR" } and $record->{ "CHR_BEG" } and $record->{ "CHR_END" } )
4720 push @{ $data_hash{ $record->{ "CHR" } } }, [ $record->{ "CHR_BEG" }, $record->{ "CHR_END" }, $options->{ "feat_color" } ];
4723 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4726 $result = Maasha::Plot::karyogram( \%data_hash, \%options );
4728 $fh = write_stream( $options->{ "data_out" } );
4736 sub script_plot_matches
4738 # Martin A. Hansen, August 2007.
4740 # Plot matches in 2D generating a dotplot.
4742 my ( $in, # handle to in stream
4743 $out, # handle to out stream
4744 $options, # options hash
4749 my ( $record, @data, $fh, $result, %data_hash );
4751 $options->{ "direction" } ||= "both";
4753 while ( $record = Maasha::Biopieces::get_record( $in ) )
4755 if ( defined $record->{ "Q_BEG" } and defined $record->{ "S_BEG" } and $record->{ "Q_END" } and $record->{ "S_END" } ) {
4756 push @data, $record;
4759 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4762 $options->{ "title" } ||= "plot_matches";
4763 $options->{ "xlabel" } ||= $data[ 0 ]->{ "Q_ID" };
4764 $options->{ "ylabel" } ||= $data[ 0 ]->{ "S_ID" };
4766 $result = Maasha::Plot::dotplot_matches( \@data, $options, $BP_TMP );
4768 $fh = write_stream( $options->{ "data_out" } );
4770 print $fh "$_\n" foreach @{ $result };
4776 sub script_length_vals
4778 # Martin A. Hansen, August 2007.
4780 # Determine the length of the value for given keys.
4782 my ( $in, # handle to in stream
4783 $out, # handle to out stream
4784 $options, # options hash
4789 my ( $record, $key );
4791 while ( $record = Maasha::Biopieces::get_record( $in ) )
4793 foreach $key ( @{ $options->{ "keys" } } )
4795 if ( $record->{ $key } ) {
4796 $record->{ $key . "_LEN" } = length $record->{ $key };
4800 Maasha::Biopieces::put_record( $record, $out );
4807 # Martin A. Hansen, August 2007.
4809 # Calculates the sums for values of given keys.
4811 my ( $in, # handle to in stream
4812 $out, # handle to out stream
4813 $options, # options hash
4818 my ( $record, $key, %sum_hash, $fh );
4820 while ( $record = Maasha::Biopieces::get_record( $in ) )
4822 foreach $key ( @{ $options->{ "keys" } } )
4824 if ( $record->{ $key } ) {
4825 $sum_hash{ $key } += $record->{ $key };
4829 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4832 $fh = write_stream( $options->{ "data_out" } );
4834 foreach $key ( @{ $options->{ "keys" } } ) {
4835 Maasha::Biopieces::put_record( { $key . "_SUM" => $sum_hash{ $key } || 0 } , $fh );
4842 sub script_mean_vals
4844 # Martin A. Hansen, August 2007.
4846 # Calculate the mean of values of given keys.
4848 my ( $in, # handle to in stream
4849 $out, # handle to out stream
4850 $options, # options hash
4855 my ( $record, $key, %sum_hash, %count_hash, $mean, $fh );
4857 while ( $record = Maasha::Biopieces::get_record( $in ) )
4859 foreach $key ( @{ $options->{ "keys" } } )
4861 if ( $record->{ $key } )
4863 $sum_hash{ $key } += $record->{ $key };
4864 $count_hash{ $key }++;
4868 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4871 $fh = write_stream( $options->{ "data_out" } );
4873 foreach $key ( @{ $options->{ "keys" } } )
4875 if ( $count_hash{ $key } ) {
4876 $mean = sprintf( "%.2f", ( $sum_hash{ $key } / $count_hash{ $key } ) );
4881 Maasha::Biopieces::put_record( { $key . "_MEAN" => $mean } , $fh );
4888 sub script_median_vals
4890 # Martin A. Hansen, March 2008.
4892 # Calculate the median values of given keys.
4894 my ( $in, # handle to in stream
4895 $out, # handle to out stream
4896 $options, # options hash
4901 my ( $record, $key, %median_hash, $median, $fh );
4903 while ( $record = Maasha::Biopieces::get_record( $in ) )
4905 foreach $key ( @{ $options->{ "keys" } } ) {
4906 push @{ $median_hash{ $key } }, $record->{ $key } if defined $record->{ $key };
4909 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4912 $fh = write_stream( $options->{ "data_out" } );
4914 foreach $key ( @{ $options->{ "keys" } } )
4916 if ( $median_hash{ $key } ) {
4917 $median = Maasha::Calc::median( $median_hash{ $key } );
4922 Maasha::Biopieces::put_record( { $key . "_MEDIAN" => $median } , $fh );
4931 # Martin A. Hansen, February 2008.
4933 # Determine the maximum values of given keys.
4935 my ( $in, # handle to in stream
4936 $out, # handle to out stream
4937 $options, # options hash
4942 my ( $record, $key, $fh, %max_hash, $max_record );
4944 while ( $record = Maasha::Biopieces::get_record( $in ) )
4946 foreach $key ( @{ $options->{ "keys" } } )
4948 if ( $record->{ $key } )
4950 $max_hash{ $key } = $record->{ $key } if $record->{ $key } > $max_hash{ $key };
4954 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
4957 $fh = write_stream( $options->{ "data_out" } );
4959 foreach $key ( @{ $options->{ "keys" } } )
4961 $max_record->{ $key . "_MAX" } = $max_hash{ $key };
4964 Maasha::Biopieces::put_record( $max_record, $fh );
4972 # Martin A. Hansen, February 2008.
4974 # Determine the minimum values of given keys.
4976 my ( $in, # handle to in stream
4977 $out, # handle to out stream
4978 $options, # options hash
4983 my ( $record, $key, $fh, %min_hash, $min_record );
4985 while ( $record = Maasha::Biopieces::get_record( $in ) )
4987 foreach $key ( @{ $options->{ "keys" } } )
4989 if ( defined $record->{ $key } )
4991 if ( exists $min_hash{ $key } ) {
4992 $min_hash{ $key } = $record->{ $key } if $record->{ $key } < $min_hash{ $key };
4994 $min_hash{ $key } = $record->{ $key };
4999 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
5002 $fh = write_stream( $options->{ "data_out" } );
5004 foreach $key ( @{ $options->{ "keys" } } )
5006 $min_record->{ $key . "_MIN" } = $min_hash{ $key };
5009 Maasha::Biopieces::put_record( $min_record, $fh );
5015 sub script_upload_to_ucsc
5017 # Martin A. Hansen, August 2007.
5019 # Calculate the mean of values of given keys.
5021 # This routine has developed into the most ugly hack. Do something!
5023 my ( $in, # handle to in stream
5024 $out, # handle to out stream
5025 $options, # options hash
5030 my ( $record, $file, $wib_file, $wig_file, $wib_dir, $fh_out, $i, $first, $format, $type, $columns, $append, $entry );
5032 $options->{ "short_label" } ||= $options->{ 'table' };
5033 $options->{ "long_label" } ||= $options->{ 'table' };
5034 $options->{ "group" } ||= $ENV{ "LOGNAME" };
5035 $options->{ "priority" } ||= 1;
5036 $options->{ "visibility" } ||= "pack";
5037 $options->{ "color" } ||= join( ",", int( rand( 255 ) ), int( rand( 255 ) ), int( rand( 255 ) ) );
5038 $options->{ "chunk_size" } ||= 10_000_000_000; # Due to 32-bit UCSC compilation really large tables cannot be loaded in one go.
5040 $file = "$BP_TMP/ucsc_upload.tmp";
5045 $fh_out = Maasha::Common::write_open( $file );
5047 while ( $record = Maasha::Biopieces::get_record( $in ) )
5049 Maasha::Biopieces::put_record( $record, $out ) if not $options->{ "no_stream" };
5051 if ( $record->{ "REC_TYPE" } eq "fixed_step" )
5055 if ( $entry = Maasha::UCSC::Wiggle::biopiece2fixedstep( $record ) ) {
5056 Maasha::UCSC::Wiggle::fixedstep_entry_put( $entry, $fh_out );
5059 elsif ( $record->{ "REC_TYPE" } eq "PSL" )
5063 Maasha::UCSC::psl_put_header( $fh_out ) if $first;
5064 Maasha::UCSC::psl_put_entry( $record, $fh_out );
5068 elsif ( $record->{ "REC_TYPE" } eq "BED" and $record->{ "SEC_STRUCT" } )
5070 # chrom chromStart chromEnd name score strand size secStr conf
5074 print $fh_out join ( "\t",
5076 $record->{ "CHR_BEG" },
5077 $record->{ "CHR_END" } + 1,
5078 $record->{ "Q_ID" },
5079 $record->{ "SCORE" },
5080 $record->{ "STRAND" },
5081 $record->{ "SIZE" },
5082 $record->{ "SEC_STRUCT" },
5083 $record->{ "CONF" },
5086 elsif ( $record->{ "REC_TYPE" } eq "BED" )
5089 $columns = $record->{ "BED_COLS" };
5091 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
5092 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
5095 elsif ( $record->{ "REC_TYPE" } eq "PATSCAN" and $record->{ "CHR" } )
5100 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
5101 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
5104 elsif ( $record->{ "REC_TYPE" } eq "BLAST" and $record->{ "S_ID" } =~ /^chr/ )
5109 $record->{ "SCORE" } = $record->{ "BIT_SCORE" } * 1000;
5111 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
5112 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
5115 elsif ( $record->{ "REC_TYPE" } eq "VMATCH" and $record->{ "S_ID" } =~ /^chr/i )
5120 if ( $entry = Maasha::UCSC::BED::biopiece2bed( $record, $columns ) ) {
5121 Maasha::UCSC::BED::bed_entry_put( $entry, $fh_out, $columns, $options->{ 'check' } );
5125 if ( $i == $options->{ "chunk_size" } )
5129 if ( $format eq "BED" ) {
5130 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5131 } elsif ( $format eq "PSL" ) {
5132 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5141 $fh_out = Maasha::Common::write_open( $file );
5149 if ( exists $options->{ "database" } and $options->{ "table" } )
5151 if ( $format eq "BED" )
5153 $type = "bed $columns";
5155 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5157 elsif ( $format eq "BED_SS" )
5159 $type = "type bed 6 +";
5161 Maasha::UCSC::bed_upload_to_ucsc( $BP_TMP, $file, $options, $append );
5163 elsif ( $format eq "PSL" )
5167 Maasha::UCSC::psl_upload_to_ucsc( $file, $options, $append );
5169 elsif ( $format eq "WIGGLE" )
5171 $options->{ "visibility" } = "full";
5173 $wig_file = "$options->{ 'table' }.wig";
5174 $wib_file = "$options->{ 'table' }.wib";
5176 $wib_dir = "$ENV{ 'HOME' }/ucsc/wib";
5178 Maasha::Common::dir_create_if_not_exists( $wib_dir );
5180 if ( $options->{ 'verbose' } ) {
5181 `cd $BP_TMP && wigEncode $file $wig_file $wib_file`;
5183 `cd $BP_TMP && wigEncode $file $wig_file $wib_file > /dev/null 2>&1`;
5186 Maasha::Common::run( "mv", "$BP_TMP/$wib_file $wib_dir" );
5194 Maasha::UCSC::wiggle_upload_to_ucsc( $BP_TMP, $wib_dir, $file, $options );
5199 Maasha::UCSC::ucsc_update_config( $options, $type );
5204 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<