3 # Copyright (C) 2007 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Stuff for interacting with UCSC genome browser
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 use vars qw ( @ISA @EXPORT );
61 @ISA = qw( Exporter );
64 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BED format <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
67 # http://genome.ucsc.edu/goldenPath/help/hgTracksHelp.html#BED
70 sub bed_entry_get_array
72 # Martin A. Hansen, September 2008.
74 # Reads a BED entry given a filehandle.
76 # This is a new _faster_ BED entry parser that
77 # uses arrays and not hashrefs.
79 # IMPORTANT! This function does not correct the
80 # BED_END position that is kept the way UCSC
83 my ( $fh, # file handle
84 $cols, # columns to read - OPTIONAL (3,4,5,6, or 12)
93 $line =~ tr/\n\r//d; # some people have carriage returns in their BED files -> Grrrr
95 return if not defined $line;
97 if ( not defined $cols ) {
98 $cols = 1 + $line =~ tr/\t//;
101 @entry = split "\t", $line, $cols + 1;
103 pop @entry if scalar @entry > $cols;
105 return wantarray ? @entry : \@entry;
111 # Martin A. Hansen, December 2007.
113 # Reads a bed entry given a filehandle.
115 my ( $fh, # file handle
116 $columns, # number of BED columns to read - OPTIONAL
121 my ( $line, @fields, %entry );
125 $line =~ tr/\n\r//d; # some people have carriage returns in their BED files -> Grrrr
127 return if not defined $line;
129 @fields = split "\t", $line;
131 $columns ||= scalar @fields;
136 "CHR" => $fields[ 0 ],
137 "CHR_BEG" => $fields[ 1 ],
138 "CHR_END" => $fields[ 2 ] - 1,
141 elsif ( $columns == 4 )
144 "CHR" => $fields[ 0 ],
145 "CHR_BEG" => $fields[ 1 ],
146 "CHR_END" => $fields[ 2 ] - 1,
147 "Q_ID" => $fields[ 3 ],
150 elsif ( $columns == 5 )
153 "CHR" => $fields[ 0 ],
154 "CHR_BEG" => $fields[ 1 ],
155 "CHR_END" => $fields[ 2 ] - 1,
156 "Q_ID" => $fields[ 3 ],
157 "SCORE" => $fields[ 4 ],
160 elsif ( $columns == 6 )
163 "CHR" => $fields[ 0 ],
164 "CHR_BEG" => $fields[ 1 ],
165 "CHR_END" => $fields[ 2 ] - 1,
166 "Q_ID" => $fields[ 3 ],
167 "SCORE" => $fields[ 4 ],
168 "STRAND" => $fields[ 5 ],
171 elsif ( $columns == 12 )
174 "CHR" => $fields[ 0 ],
175 "CHR_BEG" => $fields[ 1 ],
176 "CHR_END" => $fields[ 2 ] - 1,
177 "Q_ID" => $fields[ 3 ],
178 "SCORE" => $fields[ 4 ],
179 "STRAND" => $fields[ 5 ],
180 "THICK_BEG" => $fields[ 6 ],
181 "THICK_END" => $fields[ 7 ] - 1,
182 "COLOR" => $fields[ 8 ],
183 "BLOCK_COUNT" => $fields[ 9 ],
184 "BLOCK_LENS" => $fields[ 10 ],
185 "Q_BEGS" => $fields[ 11 ],
190 Maasha::Common::error( qq(Bad BED format in line->$line<-) );
193 $entry{ "REC_TYPE" } = "BED";
194 $entry{ "BED_LEN" } = $entry{ "CHR_END" } - $entry{ "CHR_BEG" } + 1;
195 $entry{ "BED_COLS" } = $columns;
197 return wantarray ? %entry : \%entry;
203 # Martin A. Hansen, January 2008.
205 # Given a path to a BED file, read in all entries
208 my ( $path, # full path to BED file
209 $columns, # number of columns in BED file - OPTIONAL (but is faster)
214 my ( $fh, $entry, @list );
216 $fh = Maasha::Common::read_open( $path );
218 while ( $entry = bed_get_entry( $fh ) ) {
224 return wantarray ? @list : \@list;
228 sub bed_entry_put_array
230 # Martin A. Hansen, Septermber 2008.
232 # Writes a BED entry array to file.
234 # IMPORTANT! This function does not correct the
235 # BED_END position that is assumed to be in the
236 # UCSC positions scheme.
239 $fh, # file handle - OPTIONAL
240 $cols, # number of columns in BED file - OPTIONAL
245 $fh = \*STDOUT if not defined $fh;
247 if ( defined $cols ) {
248 print $fh join( "\t", @{ $record }[ 0 .. $cols - 1 ] ), "\n";
250 print $fh join( "\t", @{ $record } ), "\n";
257 # Martin A. Hansen, Septermber 2007.
259 # Writes a BED entry to file.
261 # NB, this could really be more robust!?
263 my ( $record, # hashref
264 $fh, # file handle - OPTIONAL
265 $columns, # number of columns in BED file - OPTIONAL (but is faster)
272 $columns ||= 12; # max number of columns possible
276 push @fields, $record->{ "CHR" };
277 push @fields, $record->{ "CHR_BEG" };
278 push @fields, $record->{ "CHR_END" } + 1;
280 elsif ( $columns == 4 )
282 $record->{ "Q_ID" } =~ s/\s+/_/g;
284 push @fields, $record->{ "CHR" };
285 push @fields, $record->{ "CHR_BEG" };
286 push @fields, $record->{ "CHR_END" } + 1;
287 push @fields, $record->{ "Q_ID" };
289 elsif ( $columns == 5 )
291 $record->{ "Q_ID" } =~ s/\s+/_/g;
292 $record->{ "SCORE" } =~ s/\.\d*//;
294 push @fields, $record->{ "CHR" };
295 push @fields, $record->{ "CHR_BEG" };
296 push @fields, $record->{ "CHR_END" } + 1;
297 push @fields, $record->{ "Q_ID" };
298 push @fields, $record->{ "SCORE" };
300 elsif ( $columns == 6 )
302 $record->{ "Q_ID" } =~ s/\s+/_/g;
303 $record->{ "SCORE" } =~ s/\.\d*//;
305 push @fields, $record->{ "CHR" };
306 push @fields, $record->{ "CHR_BEG" };
307 push @fields, $record->{ "CHR_END" } + 1;
308 push @fields, $record->{ "Q_ID" };
309 push @fields, $record->{ "SCORE" };
310 push @fields, $record->{ "STRAND" };
314 $record->{ "Q_ID" } =~ s/\s+/_/g;
315 $record->{ "SCORE" } =~ s/\.\d*//;
317 push @fields, $record->{ "CHR" };
318 push @fields, $record->{ "CHR_BEG" };
319 push @fields, $record->{ "CHR_END" } + 1;
320 push @fields, $record->{ "Q_ID" };
321 push @fields, $record->{ "SCORE" };
322 push @fields, $record->{ "STRAND" };
323 push @fields, $record->{ "THICK_BEG" } if defined $record->{ "THICK_BEG" };
324 push @fields, $record->{ "THICK_END" } + 1 if defined $record->{ "THICK_END" };
325 push @fields, $record->{ "COLOR" } if defined $record->{ "COLOR" };
326 push @fields, $record->{ "BLOCK_COUNT" } if defined $record->{ "BLOCK_COUNT" };
327 push @fields, $record->{ "BLOCK_LENS" } if defined $record->{ "BLOCK_LENS" };
328 push @fields, $record->{ "Q_BEGS" } if defined $record->{ "Q_BEGS" };
332 print $fh join( "\t", @fields ), "\n";
334 print join( "\t", @fields ), "\n";
341 # Martin A. Hansen, January 2008.
343 # Write a list of BED entries.
345 my ( $entries, # list of entries,
346 $fh, # file handle - OPTIONAL
351 map { bed_put_entry( $_, $fh ) } @{ $entries };
357 # Martin A. Hansen, March 2008.
359 # Given a bed record, analysis this to give information
360 # about intron/exon sizes.
362 my ( $entry, # BED entry
367 my ( $i, @begs, @lens, $exon_max, $exon_min, $exon_len, $exon_tot, $intron_max, $intron_min, $intron_len, $intron_tot );
370 $exon_min = 9999999999;
372 $intron_min = 9999999999;
374 $entry->{ "EXONS" } = $entry->{ "BLOCK_COUNT" };
376 @begs = split /,/, $entry->{ "Q_BEGS" };
377 @lens = split /,/, $entry->{ "BLOCK_LENS" };
379 for ( $i = 0; $i < $entry->{ "BLOCK_COUNT" }; $i++ )
381 $exon_len = @lens[ $i ];
383 $entry->{ "EXON_LEN_$i" } = $exon_len;
385 $exon_max = $exon_len if $exon_len > $exon_max;
386 $exon_min = $exon_len if $exon_len < $exon_min;
388 $exon_tot += $exon_len;
391 $entry->{ "EXON_LEN_-1" } = $exon_len;
392 $entry->{ "EXON_MAX_LEN" } = $exon_max;
393 $entry->{ "EXON_MIN_LEN" } = $exon_min;
394 $entry->{ "EXON_MEAN_LEN" } = int( $exon_tot / $entry->{ "EXONS" } );
396 $entry->{ "INTRONS" } = $entry->{ "BLOCK_COUNT" } - 1;
397 $entry->{ "INTRONS" } = 0 if $entry->{ "INTRONS" } < 0;
399 if ( $entry->{ "INTRONS" } )
401 for ( $i = 1; $i < $entry->{ "BLOCK_COUNT" }; $i++ )
403 $intron_len = @begs[ $i ] - ( @begs[ $i - 1 ] + @lens[ $i - 1 ] );
405 $entry->{ "INTRON_LEN_" . ( $i - 1 ) } = $intron_len;
407 $intron_max = $intron_len if $intron_len > $intron_max;
408 $intron_min = $intron_len if $intron_len < $intron_min;
410 $intron_tot += $intron_len;
413 $entry->{ "INTRON_LEN_-1" } = $intron_len;
414 $entry->{ "INTRON_MAX_LEN" } = $intron_max;
415 $entry->{ "INTRON_MIN_LEN" } = $intron_min;
416 $entry->{ "INTRON_MEAN_LEN" } = int( $intron_tot / $entry->{ "INTRONS" } );
419 return wantarray ? %{ $entry } : $entry;
425 # Martin A. Hansen, Septermber 2008
427 # Sorts a BED file using the c program
428 # "bed_sort" specifing a sort mode:
430 # 1: chr AND chr_beg.
431 # 2: chr AND strand AND chr_beg.
433 # 4: strand AND chr_beg.
435 my ( $bed_file, # BED file to sort
436 $sort_mode, # See above.
437 $cols, # Number of columns in BED file
440 &Maasha::Common::run( "bed_sort", "--sort $sort_mode --cols $cols $bed_file" );
444 sub bed_split_to_files
446 # Martin A. Hansen, Septermber 2008
448 # Given a list of BED files, split these
449 # into temporary files based on the chromosome
450 # name. Returns a list of the temporary files.
452 my ( $bed_files, # list of BED files to split
453 $cols, # number of columns
454 $tmp_dir, # temporary directory
459 my ( $bed_file, $fh_in, $entry, $key, %fh_hash, @tmp_files );
461 foreach $bed_file ( @{ $bed_files } )
463 $fh_in = Maasha::Common::read_open( $bed_file );
465 while ( $entry = bed_entry_get_array( $fh_in, $cols ) )
467 $key = $entry->[ CHR ];
469 $fh_hash{ $key } = Maasha::Common::write_open( "$tmp_dir/$key.temp" ) if not exists $fh_hash{ $key };
471 bed_entry_put_array( $entry, $fh_hash{ $key } );
477 foreach $key ( sort keys %fh_hash )
479 push @tmp_files, "$tmp_dir/$key.temp";
481 close $fh_hash{ $key };
484 return wantarray ? @tmp_files : \@tmp_files;
488 sub bed_merge_entries
490 # Martin A. Hansen, February 2008.
492 # Merge a list of given BED entries in one big entry.
494 my ( $entries, # list of BED entries to be merged
499 my ( $i, @q_ids, @q_begs, @blocksizes, @new_q_begs, @new_blocksizes, %new_entry );
501 @{ $entries } = sort { $a->{ "CHR_BEG" } <=> $b->{ "CHR_BEG" } } @{ $entries };
503 for ( $i = 0; $i < @{ $entries }; $i++ )
505 Maasha::Common::error( qq(Attempted merge of BED entries from different chromosomes) ) if $entries->[ 0 ]->{ "CHR" } ne $entries->[ $i ]->{ "CHR" };
506 Maasha::Common::error( qq(Attempted merge of BED entries from different strands) ) if $entries->[ 0 ]->{ "STRAND" } ne $entries->[ $i ]->{ "STRAND" };
508 push @q_ids, $entries->[ $i ]->{ "Q_ID" } || sprintf( "ID%06d", $i );
510 if ( exists $entries->[ $i ]->{ "Q_BEGS" } )
512 @q_begs = split ",", $entries->[ $i ]->{ "Q_BEGS" };
513 @blocksizes = split ",", $entries->[ $i ]->{ "BLOCK_LENS" };
518 @blocksizes = $entries->[ $i ]->{ "CHR_END" } - $entries->[ $i ]->{ "CHR_BEG" } + 1;
521 map { $_ += $entries->[ $i ]->{ "CHR_BEG" } } @q_begs;
523 push @new_q_begs, @q_begs;
524 push @new_blocksizes, @blocksizes;
527 map { $_ -= $entries->[ 0 ]->{ "CHR_BEG" } } @new_q_begs;
530 CHR => $entries->[ 0 ]->{ "CHR" },
531 CHR_BEG => $entries->[ 0 ]->{ "CHR_BEG" },
532 CHR_END => $entries->[ -1 ]->{ "CHR_END" },
534 BED_LEN => $entries->[ -1 ]->{ "CHR_END" } - $entries->[ 0 ]->{ "CHR_BEG" } + 1,
536 Q_ID => join( ":", @q_ids ),
538 STRAND => $entries->[ 0 ]->{ "STRAND" } || "+",
539 THICK_BEG => $entries->[ 0 ]->{ "THICK_BEG" } || $entries->[ 0 ]->{ "CHR_BEG" },
540 THICK_END => $entries->[ -1 ]->{ "THICK_END" } || $entries->[ -1 ]->{ "CHR_END" },
542 BLOCK_COUNT => scalar @new_q_begs,
543 BLOCK_LENS => join( ",", @new_blocksizes ),
544 Q_BEGS => join( ",", @new_q_begs ),
547 return wantarray ? %new_entry : \%new_entry;
553 # Martin A. Hansen, February 2008.
555 # Splits a given BED entry into a list of blocks,
556 # which are returned. A list of 6 column BED entry is returned.
558 my ( $entry, # BED entry hashref
563 my ( @q_begs, @blocksizes, $block, @blocks, $i );
565 if ( exists $entry->{ "BLOCK_COUNT" } )
567 @q_begs = split ",", $entry->{ "Q_BEGS" };
568 @blocksizes = split ",", $entry->{ "BLOCK_LENS" };
570 for ( $i = 0; $i < @q_begs; $i++ )
574 $block->{ "CHR" } = $entry->{ "CHR" };
575 $block->{ "CHR_BEG" } = $entry->{ "CHR_BEG" } + $q_begs[ $i ];
576 $block->{ "CHR_END" } = $entry->{ "CHR_BEG" } + $q_begs[ $i ] + $blocksizes[ $i ] - 1;
577 $block->{ "Q_ID" } = $entry->{ "Q_ID" } . sprintf( "_%03d", $i );
578 $block->{ "SCORE" } = $entry->{ "SCORE" };
579 $block->{ "STRAND" } = $entry->{ "STRAND" };
580 $block->{ "BED_LEN" } = $block->{ "CHR_END" } - $block->{ "CHR_BEG" } + 1,
581 $block->{ "BED_COLS" } = 6;
582 $block->{ "REC_TYPE" } = "BED";
584 push @blocks, $block;
589 @blocks = @{ $entry };
592 return wantarray ? @blocks : \@blocks;
599 # Martin A. Hansen, February 2008.
601 # Checks if two BED entries overlap and
602 # return 1 if so - else 0;
604 my ( $entry1, # hashref
606 $no_strand, # don't check strand flag - OPTIONAL
611 return 0 if $entry1->{ "CHR" } ne $entry2->{ "CHR" };
612 return 0 if $entry1->{ "STRAND" } ne $entry2->{ "STRAND" };
614 if ( $entry1->{ "CHR_END" } < $entry2->{ "CHR_BEG" } or $entry1->{ "CHR_BEG" } > $entry2->{ "CHR_END" } ) {
622 sub bed_upload_to_ucsc
624 # Martin A. Hansen, September 2007.
626 # Upload a BED file to the UCSC database.
628 my ( $tmp_dir, # temporary directory
629 $file, # file to upload,
630 $options, # argument hashref
631 $append, # flag indicating table should be appended
636 my ( $args, $table, $sql_file, $fh_out, $fh_in );
639 $args = join " ", $options->{ "database" }, $options->{ "table" }, "-tmpDir=$tmp_dir", "-oldTable", $file;
641 $args = join " ", $options->{ "database" }, $options->{ "table" }, "-tmpDir=$tmp_dir", $file;
644 if ( $options->{ "table" } =~ /rnaSecStr/ )
646 $table = $options->{ "table" };
648 print qq(uploading secondary structure table:"$table"\n) if $options->{ "verbose" };
650 $sql_file = "$tmp_dir/upload_RNA_SS.sql";
652 $fh_out = Maasha::Common::write_open( $sql_file );
655 CREATE TABLE $table (
656 bin smallint not null, # Bin number for browser speedup
657 chrom varchar(255) not null, # Chromosome or FPC contig
658 chromStart int unsigned not null, # Start position in chromosome
659 chromEnd int unsigned not null, # End position in chromosome
660 name varchar(255) not null, # Name of item
661 score int unsigned not null, # Score from 0-1000
662 strand char(1) not null, # + or -
663 size int unsigned not null, # Size of element.
664 secStr longblob not null, # Parentheses and '.'s which define the secondary structure
665 conf longblob not null, # Confidence of secondary-structure annotation per position (0.0-1.0).
668 INDEX(chrom(8), bin),
669 INDEX(chrom(8), chromStart)
675 Maasha::Common::run( "hgLoadBed", "-notItemRgb -sqlTable=$sql_file $options->{ 'database' } $options->{ 'table' } -tmpDir=$tmp_dir $file > /dev/null 2>&1" );
681 Maasha::Common::run( "hgLoadBed", "$args > /dev/null 2>&1" );
686 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PSL format <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
691 # Martin A. Hansen, August 2008.
693 # Reads PSL next entry from a PSL file and returns a record.
695 my ( $fh, # file handle of PSL filefull path to PSL file
700 my ( $line, @fields, %record );
702 while ( $line = <$fh> )
706 @fields = split "\t", $line;
708 if ( scalar @fields == 21 )
712 MATCHES => $fields[ 0 ],
713 MISMATCHES => $fields[ 1 ],
714 REPMATCHES => $fields[ 2 ],
715 NCOUNT => $fields[ 3 ],
716 QNUMINSERT => $fields[ 4 ],
717 QBASEINSERT => $fields[ 5 ],
718 SNUMINSERT => $fields[ 6 ],
719 SBASEINSERT => $fields[ 7 ],
720 STRAND => $fields[ 8 ],
721 Q_ID => $fields[ 9 ],
722 Q_LEN => $fields[ 10 ],
723 Q_BEG => $fields[ 11 ],
724 Q_END => $fields[ 12 ] - 1,
725 S_ID => $fields[ 13 ],
726 S_LEN => $fields[ 14 ],
727 S_BEG => $fields[ 15 ],
728 S_END => $fields[ 16 ] - 1,
729 BLOCK_COUNT => $fields[ 17 ],
730 BLOCK_LENS => $fields[ 18 ],
731 Q_BEGS => $fields[ 19 ],
732 S_BEGS => $fields[ 20 ],
735 $record{ "SCORE" } = $record{ "MATCHES" } + int( $record{ "REPMATCHES" } / 2 ) - $record{ "MISMATCHES" } - $record{ "QNUMINSERT" } - $record{ "SNUMINSERT" };
736 $record{ "Q_SPAN" } = $fields[ 12 ] - $fields[ 11 ];
737 $record{ "S_SPAN" } = $fields[ 16 ] - $fields[ 15 ];
739 return wantarray ? %record : \%record;
749 # Martin A. Hansen, February 2008.
751 # Reads PSL entries and returns a list of records.
753 my ( $path, # full path to PSL file
758 my ( $fh, @lines, @fields, $i, %record, @records );
760 $fh = Maasha::Common::read_open( $path );
768 for ( $i = 5; $i < @lines; $i++ )
770 @fields = split "\t", $lines[ $i ];
772 Maasha::Common::error( qq(Bad PSL format in file "$path") ) if not @fields == 21;
778 MATCHES => $fields[ 0 ],
779 MISMATCHES => $fields[ 1 ],
780 REPMATCHES => $fields[ 2 ],
781 NCOUNT => $fields[ 3 ],
782 QNUMINSERT => $fields[ 4 ],
783 QBASEINSERT => $fields[ 5 ],
784 SNUMINSERT => $fields[ 6 ],
785 SBASEINSERT => $fields[ 7 ],
786 STRAND => $fields[ 8 ],
787 Q_ID => $fields[ 9 ],
788 Q_LEN => $fields[ 10 ],
789 Q_BEG => $fields[ 11 ],
790 Q_END => $fields[ 12 ] - 1,
791 S_ID => $fields[ 13 ],
792 S_LEN => $fields[ 14 ],
793 S_BEG => $fields[ 15 ],
794 S_END => $fields[ 16 ] - 1,
795 BLOCK_COUNT => $fields[ 17 ],
796 BLOCK_LENS => $fields[ 18 ],
797 Q_BEGS => $fields[ 19 ],
798 S_BEGS => $fields[ 20 ],
801 $record{ "SCORE" } = $record{ "MATCHES" } + int( $record{ "REPMATCHES" } / 2 ) - $record{ "MISMATCHES" } - $record{ "QNUMINSERT" } - $record{ "SNUMINSERT" };
803 push @records, { %record };
806 return wantarray ? @records : \@records;
812 # Martin A. Hansen, September 2007.
814 # Write a PSL header to file.
816 my ( $fh, # file handle - OPTIONAL
821 $fh = \*STDOUT if not $fh;
823 print $fh qq(psLayout version 3
824 match mis- rep. N's Q gap Q gap T gap T gap strand Q Q Q Q T T T T block blockSizes qStart match match count bases count bases name size start end name size start end count
825 ---------------------------------------------------------------------------------------------------------------------------------------------------------------
832 # Martin A. Hansen, September 2007.
834 # Write a PSL entry to file.
836 my ( $record, # hashref
837 $fh, # file handle - OPTIONAL
842 $fh = \*STDOUT if not $fh;
846 push @output, $record->{ "MATCHES" };
847 push @output, $record->{ "MISMATCHES" };
848 push @output, $record->{ "REPMATCHES" };
849 push @output, $record->{ "NCOUNT" };
850 push @output, $record->{ "QNUMINSERT" };
851 push @output, $record->{ "QBASEINSERT" };
852 push @output, $record->{ "SNUMINSERT" };
853 push @output, $record->{ "SBASEINSERT" };
854 push @output, $record->{ "STRAND" };
855 push @output, $record->{ "Q_ID" };
856 push @output, $record->{ "Q_LEN" };
857 push @output, $record->{ "Q_BEG" };
858 push @output, $record->{ "Q_END" } + 1;
859 push @output, $record->{ "S_ID" };
860 push @output, $record->{ "S_LEN" };
861 push @output, $record->{ "S_BEG" };
862 push @output, $record->{ "S_END" } + 1;
863 push @output, $record->{ "BLOCK_COUNT" };
864 push @output, $record->{ "BLOCK_LENS" };
865 push @output, $record->{ "Q_BEGS" };
866 push @output, $record->{ "S_BEGS" };
868 print $fh join( "\t", @output ), "\n";
872 sub psl_upload_to_ucsc
874 # Martin A. Hansen, September 2007.
876 # Upload a PSL file to the UCSC database.
878 my ( $file, # file to upload,
879 $options, # argument hashref
880 $append, # flag indicating table should be appended
888 $args = join " ", $options->{ "database" }, "-table=$options->{ 'table' }", "-clientLoad", "-append", $file;
890 $args = join " ", $options->{ "database" }, "-table=$options->{ 'table' }", "-clientLoad", $file;
893 Maasha::Common::run( "hgLoadPsl", "$args > /dev/null 2>&1" );
897 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRACK FILE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
900 sub ucsc_config_entry_get
902 # Martin A. Hansen, November 2008.
904 # Given a filehandle to a UCSC Genome Browser
905 # config file (.ra) get the next entry and
906 # return as a hash. Entries are separated by blank
907 # lines. # lines are skipped unless it is the lines:
911 my ( $fh, # file hanlde
916 my ( $line, %record );
918 while ( $line = <$fh> )
922 if ( $line =~ /Track added by 'upload_to_ucsc' (\S+) (\S+)/) {
923 $record{ 'date' } = $1;
924 $record{ 'time' } = $2;
925 } elsif ( $line =~ /^# date: (.+)/ ) {
926 $record{ 'date' } = $1;
927 } elsif ( $line =~ /^# time: (.+)/ ) {
928 $record{ 'time' } = $1;
929 } elsif ( $line =~ /^# (?:database:|Database) (.+)/ ) {
930 $record{ 'database' } = $1;
931 } elsif ( $line =~ /^#/ ) {
933 } elsif ( $line =~ /(\S+)\s+(.+)/ ) {
937 last if $line =~ /^$/ and exists $record{ "track" };
940 return undef if not exists $record{ "track" };
942 return wantarray ? %record : \%record;
946 sub ucsc_config_entry_put
948 # Martin A. Hansen, November 2008.
950 # Outputs a Biopiece record (a hashref)
951 # to a filehandle or STDOUT.
953 my ( $record, # hashref
954 $fh_out, # file handle
961 $fh_out ||= \*STDOUT;
963 ( $date, $time ) = split " ", Maasha::Common::time_stamp();
965 $record->{ "date" } ||= $date;
966 $record->{ "time" } ||= $time;
968 print $fh_out "\n# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n";
970 map { print $fh_out "# $_: $record->{ $_ }\n" if exists $record->{ $_ } } qw( date time database );
971 map { print $fh_out "$_ $record->{ $_ }\n" if exists $record->{ $_ } } qw( track
992 sub ucsc_update_config
994 # Martin A. Hansen, September 2007.
996 # Update the /home/user/ucsc/my_tracks.ra file and executes makeCustomTracks.pl
998 my ( $options, # hashref
1004 my ( $file, $entry, %new_entry, $fh_in, $fh_out, $was_found );
1006 $file = $ENV{ "HOME" } . "/ucsc/my_tracks.ra";
1008 Maasha::Filesys::file_copy( $file, "$file~" ); # create a backup
1011 database => $options->{ 'database' },
1012 track => $options->{ 'table' },
1013 shortLabel => $options->{ 'short_label' },
1014 longLabel => $options->{ 'long_label' },
1015 group => $options->{ 'group' },
1016 priority => $options->{ 'priority' },
1017 visibility => $options->{ 'visibility' },
1018 color => $options->{ 'color' },
1022 $new_entry{ 'useScore' } = 1 if $options->{ 'use_score' };
1023 $new_entry{ 'mafTrack' } = "multiz17way" if $type eq "type bed 6 +";
1024 $new_entry{ 'maxHeightPixels' } = "50:50:11" if $type eq "wig 0";
1026 $fh_in = Maasha::Filesys::file_read_open( "$file~" );
1027 $fh_out = Maasha::Filesys::file_write_open( $file );
1029 while ( $entry = ucsc_config_entry_get( $fh_in ) )
1031 if ( $entry->{ 'database' } eq $new_entry{ 'database' } and $entry->{ 'track' } eq $new_entry{ 'track' } )
1033 ucsc_config_entry_put( \%new_entry, $fh_out );
1039 ucsc_config_entry_put( $entry, $fh_out );
1043 ucsc_config_entry_put( \%new_entry, $fh_out ) if not $was_found;
1048 Maasha::Common::run( "ucscMakeTracks.pl", "-b > /dev/null 2>&1" );
1052 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> fixedStep format <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1055 sub fixedstep_get_entry
1057 # Martin A. Hansen, December 2007.
1059 # Given a file handle to a PhastCons file get the
1060 # next entry which is all the lines after a "fixedStep"
1061 # line and until the next "fixedStep" line or EOF.
1063 my ( $fh, # filehandle
1066 # Returns a list of lines
1068 my ( $entry, @lines );
1070 local $/ = "\nfixedStep ";
1076 @lines = split "\n", $entry;
1078 return if @lines == 0;
1080 $lines[ 0 ] =~ s/fixedStep?\s*//;
1082 return wantarray ? @lines : \@lines;
1086 sub fixedstep_index_create
1088 # Martin A. Hansen, January 2008.
1090 # Indexes a concatenated fixedStep file.
1091 # The index consists of a hash with chromosomes as keys,
1092 # and a list of [ chr_beg, next_chr_beg, chr_end, index_beg, index_len ] as values.
1094 my ( $path, # path to fixedStep file
1099 my ( $fh, $pos, $index_beg, $index_len, $entry, $locator, $chr, $step, $beg, $end, $len, %index, $i );
1101 $fh = Maasha::Common::read_open( $path );
1105 while ( $entry = Maasha::UCSC::fixedstep_get_entry( $fh ) )
1107 $locator = shift @{ $entry };
1109 if ( $locator =~ /chrom=([^ ]+) start=(\d+) step=(\d+)/ )
1112 $beg = $2 - 1; # fixedStep files are 1-based
1117 Maasha::Common::error( qq(Could not parse locator: $locator) );
1120 $pos += length( $locator ) + 11;
1124 # map { $pos += length( $_ ) + 1 } @{ $entry };
1126 $pos += 6 * scalar @{ $entry };
1128 $index_len = $pos - $index_beg;
1130 push @{ $index{ $chr } }, [ $beg, undef, $beg + scalar @{ $entry } - 1, $index_beg, $index_len ];
1135 foreach $chr ( keys %index )
1137 for ( $i = 0; $i < @{ $index{ $chr } } - 1; $i++ ) {
1138 $index{ $chr }->[ $i ]->[ FS_NEXT_CHR_BEG ] = $index{ $chr }->[ $i + 1 ]->[ 0 ];
1141 $index{ $chr }->[ -1 ]->[ FS_NEXT_CHR_BEG ] = $index{ $chr }->[ -1 ]->[ FS_CHR_END ] + 1;
1144 return wantarray ? %index : \%index;
1148 sub fixedstep_index_store
1150 # Martin A. Hansen, January 2008.
1152 # Writes a fixedStep index to binary file.
1154 my ( $path, # full path to file
1155 $index, # list with index
1160 Maasha::Common::file_store( $path, $index );
1164 sub fixedstep_index_retrieve
1166 # Martin A. Hansen, January 2008.
1168 # Retrieves a fixedStep index from binary file.
1170 my ( $path, # full path to file
1177 $index = Maasha::Common::file_retrieve( $path );
1179 return wantarray ? %{ $index } : $index;
1183 sub fixedstep_index_lookup
1185 # Martin A. Hansen, January 2008.
1187 # Retrieve fixedStep scores from a indexed
1188 # fixedStep file given a chromosome and
1189 # begin and end positions.
1191 my ( $index, # data structure
1192 $fh, # filehandle to datafile
1194 $chr_beg, # chromosome beg
1195 $chr_end, # chromosome end
1196 $flank, # include flanking region - OPTIONAL
1201 my ( $index_beg, $index_end, $i, $c, $beg, $end, @vals, $scores );
1208 # print "chr_beg->$chr_beg chr_end->$chr_end flank->$flank\n";
1210 if ( exists $index->{ $chr } )
1212 $index_beg = Maasha::Matrix::interval_search( $index->{ $chr }, 0, 1, $chr_beg );
1214 if ( $index_beg < 0 ) {
1215 Maasha::Common::error( qq(Index search failed - begin index position doesn't exists: $chr_beg) );
1218 if ( $chr_end < $index->{ $chr }->[ $index_beg ]->[ 1 ] )
1220 $index_end = $index_beg;
1224 $index_end = Maasha::Matrix::interval_search( $index->{ $chr }, 0, 1, $chr_end );
1226 if ( $index_end < 0 ) {
1227 Maasha::Common::error( qq(Index search failed - end index position doesn't exists: $chr_end) );
1231 map { $scores->[ $_ ] = 0 } 0 .. $chr_end - $chr_beg;
1233 if ( $index_beg == $index_end )
1235 $beg = Maasha::Calc::max( $chr_beg, $index->{ $chr }->[ $index_beg ]->[ FS_CHR_BEG ] );
1236 $end = Maasha::Calc::min( $chr_end, $index->{ $chr }->[ $index_end ]->[ FS_CHR_END ] );
1238 if ( $beg <= $index->{ $chr }->[ $index_beg ]->[ FS_CHR_END ] and $end >= $index->{ $chr }->[ $index_beg ]->[ FS_CHR_BEG ] )
1240 @vals = split "\n", Maasha::Common::file_read(
1242 $index->{ $chr }->[ $index_beg ]->[ FS_INDEX_BEG ] + 6 * ( $beg - $index->{ $chr }->[ $index_beg ]->[ FS_CHR_BEG ] ),
1243 6 * ( $end - $beg + 1 ),
1247 for ( $c = 0; $c < @vals; $c++ ) {
1248 $scores->[ $c + $beg - $chr_beg ] = $vals[ $c ];
1253 $beg = Maasha::Calc::max( $chr_beg, $index->{ $chr }->[ $index_beg ]->[ FS_CHR_BEG ] );
1255 # print Dumper( $beg, $index->{ $chr }->[ $index_beg ] );
1256 # print Dumper( "next", $index->{ $chr }->[ $index_beg ]->[ FS_NEXT_CHR_BEG ] );
1262 if ( $beg <= $index->{ $chr }->[ $index_beg ]->[ FS_CHR_END ] )
1264 @vals = split "\n", Maasha::Common::file_read(
1266 $index->{ $chr }->[ $index_beg ]->[ FS_INDEX_BEG ] + 6 * ( $beg - $index->{ $chr }->[ $index_beg ]->[ FS_CHR_BEG ] ),
1267 6 * ( $index->{ $chr }->[ $index_beg ]->[ FS_CHR_END ] - $beg + 1 ),
1270 for ( $c = 0; $c < @vals; $c++ ) {
1271 $scores->[ $c + $beg - $chr_beg ] = $vals[ $c ];
1275 $end = Maasha::Calc::min( $chr_end, $index->{ $chr }->[ $index_end ]->[ FS_CHR_END ] );
1277 if ( $end <= $index->{ $chr }->[ $index_end ]->[ FS_CHR_END ] )
1279 @vals = split "\n", Maasha::Common::file_read(
1281 $index->{ $chr }->[ $index_end ]->[ FS_INDEX_BEG ],
1282 6 * ( $end - $index->{ $chr }->[ $index_end ]->[ FS_CHR_BEG ] + 1 ),
1285 for ( $c = 0; $c < @vals; $c++ ) {
1286 $scores->[ $c + $index->{ $chr }->[ $index_end ]->[ FS_CHR_BEG ] - $chr_beg ] = $vals[ $c ];
1290 for ( $i = $index_beg + 1; $i <= $index_end - 1; $i++ )
1292 @vals = split "\n", Maasha::Common::file_read(
1294 $index->{ $chr }->[ $i ]->[ FS_INDEX_BEG ],
1295 6 * ( $index->{ $chr }->[ $i ]->[ FS_CHR_END ] - $index->{ $chr }->[ $i ]->[ FS_CHR_BEG ] + 1 ),
1298 for ( $c = 0; $c < @vals; $c++ ) {
1299 $scores->[ $c + $index->{ $chr }->[ $i ]->[ FS_CHR_BEG ] - $chr_beg ] = $vals[ $c ];
1306 Maasha::Common::error( qq(Chromosome "$chr" was not found in index) );
1309 return wantarray ? @{ $scores } : $scores;
1313 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PhastCons format <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1318 # Martin A. Hansen, July 2008
1320 # Create a fixedStep index for PhastCons data.
1322 my ( $file, # file to index
1323 $dir, # dir with file
1330 $index = fixedstep_index_create( "$dir/$file" );
1332 fixedstep_index_store( "$dir/$file.index", $index );
1336 sub phastcons_parse_entry
1338 # Martin A. Hansen, December 2007.
1340 # Given a PhastCons entry converts this to a
1341 # list of super blocks.
1343 my ( $lines, # list of lines
1344 $args, # argument hash
1349 my ( $info, $chr, $beg, $step, $i, $c, $j, @blocks, @super_blocks, @entries, $super_block, $block, @lens, @begs );
1351 $info = shift @{ $lines };
1353 if ( $info =~ /^chrom=([^ ]+) start=(\d+) step=(\d+)$/ )
1359 die qq(ERROR: step size $step != 1 -> problem!\n) if $step != 1; # in an ideal world should would be fixed ...
1364 while ( $i < @{ $lines } )
1366 if ( $lines->[ $i ] >= $args->{ "threshold" } )
1370 while ( $c < @{ $lines } )
1372 if ( $lines->[ $c ] < $args->{ "threshold" } )
1376 while ( $j < @{ $lines } and $lines->[ $j ] < $args->{ "threshold" } ) {
1380 if ( $j - $c > $args->{ "gap" } )
1382 if ( $c - $i >= $args->{ "min" } )
1386 CHR_BEG => $beg + $i - 1,
1387 CHR_END => $beg + $c - 2,
1405 if ( $c - $i >= $args->{ "min" } )
1409 CHR_BEG => $beg + $i - 1,
1410 CHR_END => $beg + $c - 2,
1425 while ( $i < @blocks )
1429 while ( $c < @blocks and $blocks[ $c ]->{ "CHR_BEG" } - $blocks[ $c - 1 ]->{ "CHR_END" } <= $args->{ "dist" } )
1434 push @super_blocks, [ @blocks[ $i .. $c - 1 ] ];
1439 foreach $super_block ( @super_blocks )
1441 foreach $block ( @{ $super_block } )
1443 push @begs, $block->{ "CHR_BEG" } - $super_block->[ 0 ]->{ "CHR_BEG" };
1444 push @lens, $block->{ "CHR_LEN" } - 1;
1450 CHR => $super_block->[ 0 ]->{ "CHR" },
1451 CHR_BEG => $super_block->[ 0 ]->{ "CHR_BEG" },
1452 CHR_END => $super_block->[ -1 ]->{ "CHR_END" },
1456 THICK_BEG => $super_block->[ 0 ]->{ "CHR_BEG" },
1457 THICK_END => $super_block->[ -1 ]->{ "CHR_END" } + 1,
1459 BLOCK_COUNT => scalar @{ $super_block },
1460 BLOCK_LENS => join( ",", @lens ),
1461 Q_BEGS => join( ",", @begs ),
1468 return wantarray ? @entries : \@entries;
1472 sub phastcons_normalize
1474 # Martin A. Hansen, January 2008.
1476 # Normalizes a list of lists with PhastCons scores,
1477 # in such a way that each list contains the same number
1478 # or PhastCons scores.
1480 my ( $AoA, # AoA with PhastCons scores
1485 my ( $list, $max, $min, $mean, $diff );
1490 foreach $list ( @{ $AoA } )
1492 $min = scalar @{ $list } if scalar @{ $list } < $min;
1493 $max = scalar @{ $list } if scalar @{ $list } > $max;
1496 $mean = int( ( $min + $max ) / 2 );
1498 # print STDERR "min->$min max->$max mean->$mean\n";
1500 foreach $list ( @{ $AoA } )
1502 $diff = scalar @{ $list } - $mean;
1504 phastcons_list_inflate( $list, abs( $diff ) ) if $diff < 0;
1505 phastcons_list_deflate( $list, $diff ) if $diff > 0;
1508 return wantarray ? @{ $AoA } : $AoA;
1512 sub phastcons_list_inflate
1514 # Martin A. Hansen, January 2008.
1516 # Inflates a list with a given number of elements
1517 # in such a way that the extra elements are introduced
1518 # evenly over the entire length of the list. The value
1519 # of the extra elements is based on a mean of the
1520 # adjacent elements.
1522 my ( $list, # list of elements
1523 $diff, # number of elements to introduce
1528 my ( $len, $space, $i, $pos );
1530 $len = scalar @{ $list };
1532 $space = $len / $diff;
1534 for ( $i = 0; $i < $diff; $i++ )
1536 $pos = int( ( $space / 2 ) + $i * $space );
1538 splice @{ $list }, $pos, 0, ( $list->[ $pos - 1 ] + $list->[ $pos + 1 ] ) / 2;
1539 # splice @{ $list }, $pos, 0, "X";
1542 die qq(ERROR: bad inflate\n) if scalar @{ $list } != $len + $diff;
1546 sub phastcons_list_deflate
1548 # Martin A. Hansen, January 2008.
1550 # Deflates a list by removing a given number of elements
1551 # evenly distributed over the entire list.
1553 my ( $list, # list of elements
1554 $diff, # number of elements to remove
1559 my ( $len, $space, $i, $pos );
1561 $len = scalar @{ $list };
1563 $space = ( $len - $diff ) / $diff;
1565 for ( $i = 0; $i < $diff; $i++ )
1567 $pos = int( ( $space / 2 ) + $i * $space );
1569 splice @{ $list }, $pos, 1;
1572 die qq(ERROR: bad deflate\n) if scalar @{ $list } != $len - $diff;
1578 # Martin A. Hansen, January 2008.
1580 # Given a normalized PhastCons matrix in an AoA,
1581 # calculate the mean for each column and return as a list.
1583 my ( $AoA, # AoA with normalized PhastCons scores
1590 $AoA = Maasha::Matrix::matrix_flip( $AoA );
1592 map { push @list, Maasha::Calc::mean( $_ ) } @{ $AoA };
1594 return wantarray ? @list : \@list;
1598 sub phastcons_median
1600 # Martin A. Hansen, January 2008.
1602 # Given a normalized PhastCons matrix in an AoA,
1603 # calculate the median for each column and return as a list.
1605 my ( $AoA, # AoA with normalized PhastCons scores
1612 $AoA = Maasha::Matrix::matrix_flip( $AoA );
1614 map { push @list, Maasha::Calc::median( $_ ) } @{ $AoA };
1616 return wantarray ? @list : \@list;
1620 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MULTIPLE ALIGNMENT FILES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1625 # Martin A. Hansen, April 2008.
1627 # Executes mafFrag to extract a subalignment from a multiz track
1628 # in the UCSC genome browser database.
1630 my ( $tmp_dir, # temporary directory
1631 $database, # genome database
1632 $table, # table with the multiz track
1634 $beg, # begin position
1635 $end, # end position
1639 # Returns a list of record
1641 my ( $tmp_file, $align );
1643 $tmp_file = "$tmp_dir/maf_extract.maf";
1645 Maasha::Common::run( "mafFrag", "$database $table $chr $beg $end $strand $tmp_file" );
1647 $align = maf_parse( $tmp_file );
1651 return wantarray ? @{ $align } : $align;
1657 # Martin A. Hansen, April 2008.
1660 my ( $path, # full path to MAF file
1663 # Returns a list of record.
1665 my ( $fh, $line, @fields, @align );
1667 $fh = Maasha::Common::read_open( $path );
1669 while ( $line = <$fh> )
1673 if ( $line =~ /^s/ )
1675 @fields = split / /, $line;
1678 SEQ_NAME => $fields[ 1 ],
1679 SEQ => $fields[ -1 ],
1681 ALIGN_LEN => length $fields[ -1 ],
1688 return wantarray ? @align : \@align;
1692 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> WIGGLE FORMAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1695 sub fixedstep_put_entry
1697 # Martin A. Hansen, April 2008.
1699 # Outputs a block of fixedStep values.
1700 # Used for outputting wiggle data.
1702 my ( $chr, # chromosome
1703 $beg, # start position
1704 $block, # list of scores
1705 $fh, # filehandle - OPTIONAL
1706 $log10, # flag indicating that log10 scores should be used
1711 $beg += 1; # fixedStep format is 1 based.
1715 print $fh "fixedStep chrom=$chr start=$beg step=1\n";
1718 map { printf( $fh "%f\n", Maasha::Calc::log10( $_ + 1 ) ) } @{ $block };
1720 map { printf( $fh "%d\n", ( $_ + 1 ) ) } @{ $block };
1725 sub wiggle_upload_to_ucsc
1727 # Martin A. Hansen, May 2008.
1729 # Upload a wiggle file to the UCSC database.
1731 my ( $tmp_dir, # temporary directory
1732 $wib_dir, # wib directory
1733 $wig_file, # file to upload,
1734 $options, # argument hashref
1741 # $args = join " ", "-tmpDir=$tmp_dir", "-pathPrefix=$wib_dir", $options->{ "database" }, $options->{ 'table' }, $wig_file;
1743 # Maasha::Common::run( "hgLoadWiggle", "$args > /dev/null 2>&1" );
1745 if ( $options->{ 'verbose' } ) {
1746 `cd $tmp_dir && hgLoadWiggle -tmpDir=$tmp_dir -pathPrefix=$wib_dir $options->{ 'database' } $options->{ 'table' } $wig_file`;
1748 `cd $tmp_dir && hgLoadWiggle -tmpDir=$tmp_dir -pathPrefix=$wib_dir $options->{ 'database' } $options->{ 'table' } $wig_file > /dev/null 2>&1`;
1753 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MySQL CONF <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1758 # Martin A. Hansen, May 2008
1760 # Fetches the MySQL database user name from the
1761 # .hg.conf file in the users home directory.
1765 my ( $fh, $line, $user );
1767 $fh = Maasha::Common::read_open( "$ENV{ 'HOME' }/.hg.conf" );
1769 while ( $line = <$fh> )
1773 if ( $line =~ /^db\.user=(.+)/ )
1787 sub ucsc_get_password
1789 # Martin A. Hansen, May 2008
1791 # Fetches the MySQL database password from the
1792 # .hg.conf file in the users home directory.
1796 my ( $fh, $line, $password );
1798 $fh = Maasha::Common::read_open( "$ENV{ 'HOME' }/.hg.conf" );
1800 while ( $line = <$fh> )
1804 if ( $line =~ /^db\.password=(.+)/ )
1818 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<