-
Notifications
You must be signed in to change notification settings - Fork 2
/
bb.topoview
executable file
·977 lines (861 loc) · 36.2 KB
/
bb.topoview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
#!/usr/bin/perl
#----------------------------------------------------------#
# Author: Douglas Senalik [email protected] #
#----------------------------------------------------------#
# "Black Box" program series
# version 1.0 - September 9, 2014
# version 2.0 - November 4, 2017 - update from fb_shmiggle to topoview database files
my $version = '2.0';
=bb
Data formatter and indexer for TopoView GBrowse track
=cut bb
# version 1.0 based on script from http://flybase.org/static_pages/docs/software/topoview.html
# 2009-2010 Victor Strelets, FlyBase.org
# version 2.0 based on coverage_to_topoview.pl program included with GBrowse
use strict;
use warnings;
use Getopt::Long; # for getting command line parameters
use BerkeleyDB; # installable with cpan, also first need sudo apt-get install libdb5.3-dev
use File::Spec; # path manipulation
use File::Basename; # replaces system call to basename in original code
############################################################
# configuration variables
############################################################
my %LogTabs= ( 0 => 0, 1 => 0 ); # a cache of previously calculated log values
my $defaultfilesmask = '*.wig';
my $defaultcrop = "250";
my $do_only_subset;
my $do_only_chr;
my $defaultlognum = 10;
my $datfilename = "data.cat";
my $bdbfilename = "index.bdbhash";
############################################################
# global variables
############################################################
(my $prognopath = $0) =~ s/^.*[\/\\]//;
my $log2 = log(2);
# %ResIndexHash in the original code was commented as global
my %ResIndexHash;
my %bdb_hash; # the same as above but for version 2
# but these others were just floating there (use strict was not used)
my @SubsetNames;
my $signal;
my $max_signal;
my $COV; # file handle
############################################################
# command line parameters
############################################################
my $indir; # input directory name
my $outdir; # output directory name
my $filesmask = $defaultfilesmask;
my $apply_log; # apply a log base 2 transformation
my $log_magnifier= 1.0; # scaling
my $crop = $defaultcrop; # values larger than this are made equal to this
my $lognum = -1; # how many chromosomes to print to log -1=all, 0=none, >=1=this many
my $shmiggle = 0;
my $force = 0; # make index if already exists
my $help; # print help and exit
my $quiet; # only show errors
my $debug; # print extra debugging information
GetOptions (
"indirectory=s" => \$indir, # string
"outdirectory=s" => \$outdir, # string
"mask=s" => \$filesmask, # string
"log" => \$apply_log, # flag
"magnify|scale=s"=> \$log_magnifier, # flag
"crop=s" => \$crop, # string
"part:i" => \$lognum, # flag or integer
"shmiggle" => \$shmiggle, # flag
"force" => \$force, # flag
"help" => \$help, # flag
"quiet" => \$quiet, # flag
"debug" => \$debug); # flag
# debug implies not quiet
if ( $debug ) { $quiet = 0; }
if ( $lognum eq '' ) { $lognum = $defaultlognum; }
unless ( $indir ) { $help = 1; }
unless ( $outdir ) { $outdir = $indir; }
############################################################
# print help screen
############################################################
if ( $help )
{
print "$prognopath version $version
Required parameters:
--indirectory=xxx directory containing files
Optional parameters:
--outdirectory=xxx location of generated index files. If not
specified, will be --indirectory. This will
be created if it does not exist
--mask=xxx wiggle file mask, default = \"$defaultfilesmask\"
another common option would be \"*.bed\" or \"*.bed.gz\"
--log apply log base 2 transformation
--magnify=xxx apply this scaling factor to values,
done after log transform if using
--log, default = 1.0
e.g. value of 10 makes values 10x larger
--scale=xxx synonym for --magnify
--crop=xxx after applying --log and --magnify, if used,
this will be the maximum value returned,
default=$defaultcrop or use 0 to disable
--part[=xxx] print this many chromosomes to log
0=none, >=1=that many, default number=$defaultlognum
but if not specified, then all chromosomes are printed
--force overwrite existing database if it already exists
--shmiggle use the old legacy code for the fb_shmiggle glyph
instead of the current topoview glyph
--help print this screen
--quiet only print error messages
--debug print extra debugging information
";
exit 1;
} # if ( $help )
############################################################
# main
############################################################
unless ( -d $indir ) { die "Error, --indirectory \"$indir\" does not exist\n"; }
if ( ( not $quiet ) and ( $apply_log ) )
{ print "Will apply log(2) transformation of data\n"; }
unless ( $quiet ) { print "Indexing $filesmask files\n"; }
if ( $shmiggle ) # legacy version, now obsolete
{ shmiggleindexfeatdir ( $indir, $outdir, $filesmask ); }
else # current version based on GBrowse coverage_to_topoview.pl program
{ indextopoview ( $indir, $outdir, $filesmask ); }
############################################################
# cleanup and end program
############################################################
unless ( $quiet ) { tlog( "$0 Done" ); }
exit 0;
############################################################
sub debugmsg { my ( $text, $noreturn, $nolinenum ) = @_;
############################################################
if ( $debug )
{
my ($package, $filename, $line, $sub) = caller(0);
unless ( $nolinenum ) { $text = "Line $line: " . $text; }
if ( ! ( $noreturn ) ) { $text .= "\n"; }
print $text;
} # if ( $debug )
} # sub debugmsg
###############################################################
sub tlog { my ( $prefix, $suffix, $noreturn ) = @_;
###############################################################
$prefix //= '';
$suffix //= '';
@_ = localtime(time);
my $t = $prefix . ' '
. sprintf("%04d/%02d/%02d %02d:%02d:%02d", $_[5]+1900, $_[4]+1, $_[3], @_[2,1,0])
. ' ' . $suffix;
$t =~ s/^ +//;
$t =~ s/ +$//;
print $t;
unless ( $noreturn ) { print "\n"; }
} # sub tlog
###############################################################
sub commify {
###############################################################
# http://perldoc.perl.org/perlfaq5.html#How-can-I-output-my-numbers-with-commas
local $_ = shift;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
} # commify
###############################################################
sub stdopen { my ( $mode, $filename, $extratext ) = @_;
###############################################################
# a replacement for the three-parameter open which also allows
# the use of "-" as the file name to mean STDIN or STDOUT
my $fh; # the file handle
if ( $filename eq "-" ) # only exact match to "-" has special meaning
{
if ( $mode =~ m/>/ )
{ $fh = *STDOUT }
else
{ $fh = *STDIN }
}
else
{
# supplemental passed text for error messages, need one more space
if ( defined $extratext )
{ $extratext .= " " }
else
{ $extratext = "" }
my $text; # this is only used for error message
if ( $mode =~ m/^\+?>>/ ) # ">>" or "+>>"
{ $text = "append" }
elsif ( $mode =~ m/^\+?>/ ) # ">" or "+>"
{ $text = "output" }
elsif ( $mode =~ m/^\+?</ ) # "<" or "+<"
{ $text = "input" }
elsif ( $mode eq "-|" )
{ $text = "piped input" }
elsif ( $mode eq "|-" )
{ $text = "piped output" }
else
{ die "Error, unsupported file mode \"$mode\" specified to stdopen( $mode, $filename, $extratext )\n"; }
# if file name ends in ".gz", gzip compression is assumed, and handle it transparently
if ( $filename =~ m/\.gz$/ )
{
if ( $mode =~ m/^>$/ ) # output mode
{ $mode = "|-"; $filename = "gzip -c > \"$filename\""; }
elsif ( $mode =~ m/^<$/ ) # input mode
{ $mode = "-|"; $filename = "gunzip -c \"$filename\""; }
else
{ die "Error, can't handle gzip compression with mode \"$mode\" for file \"filename\"\n"; }
} # if gzip compressed file
open ( $fh, $mode, $filename ) or die ( "Error opening ${extratext}file \"$filename\" for $text: $!\n" );
}
# return the opened file handle to the caller
return $fh;
} # sub stdopen
###############################################################
sub stdclose { my ( $fh ) = @_;
###############################################################
# same as built-in close, except in case of STDIN or STDOUT,
# and in this case the file handle is not closed
unless ( fileno($fh) <= 2 ) # if file number is this low, is stdin or stdout or stderr
{ close ( $fh ) or die ( "Error closing file handle: $!\n" ); }
} # sub stdclose
###############################################################
sub indextopoview { my ( $dir, $outdir, $mask ) = @_;
###############################################################
my @files = sort( glob ( File::Spec->catfile( $dir, $mask ) ) );
debugmsg( scalar(@files)." files found to index: ".join("\t", @files) );
unless ( $quiet ) { print "Found ".commify(scalar @files)." files to process in directory \"$dir\" matching \"$mask\"\n"; }
unless ( @files ) { die "No files found in directory \"$dir\" with mask \"$mask\", terminating\n"; }
if ( ! -d $outdir )
{
# coverage_to_topoview.pl used system mkdir -p $outdir, but
# here only allow one level of creation for "safety"
unless ( $quiet ) { print "Creating output directory \"$outdir\"\n"; }
mkdir( $outdir ) or die "Error creating output directory \"$outdir\": $!\n";
}
# database file names and check for already existing
my $localdatfilename = File::Spec->catfile( $outdir, $datfilename );
my $localbdbfilename = File::Spec->catfile( $outdir, $bdbfilename );
debugmsg ( "Database files: \"$localdatfilename\" and \"$localbdbfilename\"" );
if ( ( -e $localdatfilename ) or ( -e $localbdbfilename ) )
{
unless ( $force )
{
unless ( $quiet ) { print "Found existing index, no changes made\n"; }
return;
}
unless ( $quiet ) { print "Old index exists. Removing it and generating new index\n"; }
if ( -e $localdatfilename ) { unlink $localdatfilename or die "Error removing old file \"$localdatfilename\": $!\n"; }
if ( -e $localbdbfilename ) { unlink $localbdbfilename or die "Error removing old file \"$localbdbfilename\": $!\n"; }
}
open( $COV, '>', $localdatfilename ) or die "Cannot open \"$localdatfilename\": $!";
tie(%bdb_hash, "BerkeleyDB::Hash", -Filename => $localbdbfilename, -Flags => DB_CREATE);
$max_signal = 0; # global in the legacy version
@SubsetNames = (); # global in the legacy version
for my $file ( @files )
{
my $id = verifyfiletype( $file ); # will die if invalid
debugmsg( "Processing file \"$file\"" );
indexCoverageFile( $file, $id );
}
$bdb_hash{'subsets'} = join( "\t", @SubsetNames );
$bdb_hash{'max_signal'} = $max_signal;
my @all_keys = keys %bdb_hash;
if ( $debug )
{
for my $kkey ( sort @all_keys )
{ print "\"$kkey\" => \"" . $bdb_hash{$kkey} . "\"\n"; }
}
untie %bdb_hash;
chmod( 0666, $localbdbfilename ); # ! sometimes very important
close( $COV );
} # sub indextopoview
############################################################
sub verifyfiletype { my ( $file ) = @_;
############################################################
# returns the track id if found
my $checknlines = 9; # number of lines of file to check
my $INF = stdopen( '<', $file );
my $nlines = 0;
my $id;
while ( my $aline = <$INF> )
{
$nlines++;
last if ( $nlines > $checknlines );
$aline =~ s/[\r\n]//g;
if ( $aline =~ m/^track/ ) # e.g. track type=wiggle_0 name="somename"
{
if ( $aline =~ m/name=["']([^"]+)["']/i )
{ $id = $1; }
}
else
{
my @cols = split( /\t/, $aline );
if ( scalar(@cols) != 4 )
{ die "Invalid input file \"$file\": expected four columns, found ".scalar(@cols)." columns line $nlines\n"; }
for my $i ( 1, 2, 3 )
{
unless ( is_numeric($cols[$i]) )
{ die "Invalid input file \"$file\": column ".($i+1)." \"$cols[$i]\" is not numeric line $nlines\n"; }
}
}
}
close( $INF );
return( $id ); # may be undefined
} # sub verifyfiletype
sub is_numeric {
no warnings;
return defined eval { $_[0] == 0 };
} # sub is_numeric
#*************************************************************************
sub shmiggleindexfeatdir { my ( $dir, $outdir, $mask ) = @_;
#*************************************************************************
my $ntoprint = $lognum;
# original code ignored $mask, here it is replaced with a glob
my @files = glob ( File::Spec->catfile( $dir, $mask ) );
unless ( $quiet ) { print "Found ".commify(scalar @files)." files to process in directory \"$dir\" matching \"$mask\"\n"; }
unless ( @files ) { die "No files found in directory \"$dir\" with mask \"$mask\", terminating\n"; }
# original version, deactivated but left for reference
#local(*D); opendir(D, $dir) || warn "can't open $dir";
#my @files= grep( /\.(cov|wig)/i, readdir(D));
#closedir(D);
# system("rm $datfilename") if -e $datfilename; # replace with Perl version below
# unlink($bdbfilename) if( -e $bdbfilename ); # why was this unlink and the one above was rm?
my $localdatfilename = File::Spec->catfile( $outdir, $datfilename );
my $localbdbfilename = File::Spec->catfile( $outdir, $bdbfilename );
if ( ( -e $localdatfilename ) or ( -e $localbdbfilename ) )
{
unless ( $force )
{
unless ( $quiet ) { print "Found existing index, no changes made\n"; }
return;
}
unless ( $quiet ) { print "Old index exists. Removing it and generating new index\n"; }
if ( -e $localdatfilename ) { unlink $localdatfilename or die "Error removing old file \"$localdatfilename\": $!\n"; }
if ( -e $localbdbfilename ) { unlink $localbdbfilename or die "Error removing old file \"$localbdbfilename\": $!\n"; }
}
open(OUTDATF,'>'.$localdatfilename) || die "Cannot open \"$localdatfilename\" for output: $!\n";
%ResIndexHash= (); # !!GLOBAL
tie %ResIndexHash, "BerkeleyDB::Hash", -Filename => $localbdbfilename, -Flags => DB_CREATE;
$max_signal = 0; # global
@SubsetNames= ();
foreach my $file (sort @files)
{
unless ( $quiet ) { tlog( "Indexing file \"$file\"" ); }
shmiggleIndexCoverageFile($file); # coverage files are in fact wiggle files..
}
$ResIndexHash{'subsets'}= join("\t",@SubsetNames); # record subsets, just in case..
$ResIndexHash{'max_signal'}= $max_signal;
my @all_keys= keys %ResIndexHash;
foreach my $kkey ( sort @all_keys )
{
if ( $ntoprint )
{
print "\t$kkey => ".$ResIndexHash{$kkey}."\n";
$ntoprint--;
unless ( $ntoprint ) { print "\tRemainder not shown...\n"; last; }
}
}
if ( $max_signal > 10000 ) { print "WARNING: max_signal=$max_signal - TOO HIGH! Re-run with '-log' option\n"; }
if ( $max_signal <= 0 ) { print "WARNING: max_signal=$max_signal - TOO LOW! Probably bad input\n"; }
untie %ResIndexHash;
chmod(0666,$localbdbfilename); # ! sometimes very important
close(OUTDATF);
return;
} # sub shmiggleindexfeatdir
#***********************************************************
sub indexCoverageFile { my ( $file, $id ) = @_;
#***********************************************************
my $ntoprint = $lognum;
my $INF = stdopen( '<', $file );
# identifier is based on file name without path or extension,
# unless a name is explicitly passed in as $id
my $SubsetName = $id?$id:basename( $file, '.wig.gz', '.wig.bz2', '.wig', '.bed.gz', '.bed.bz2', '.bed' );
unless ( $quiet )
{ tlog( "Identifier=\"$SubsetName\" File=\"$file\""); }
push( @SubsetNames, $SubsetName );
my $old_ref = '';
my @offsets = ();
my $step = 1000;
my $coordstep = 5000;
my $counter = 0;
my $offset = tell($COV);
$bdb_hash{$SubsetName} = $offset; # record offset where new subset data starts
my $old_signal = 0;
my $old_coord = -200000;
my $start = 0;
my $lastRecordedCoord = -200000;
my @signals;
while ( my $aline = <$INF> )
{
$offset = tell($COV);
$aline =~ s/[\r\n]//g;
next if ( ( $aline =~ m/^$/ ) or ( $aline =~ m/^\#/ ) or ( $aline =~ m/^track/ ) );
my ($ref,$start,$end,$signal) = split( /\t/, $aline );
$signal = modifySignal( $signal );
$start += 1; # zero-based, half-open coords in BED
$signal = 0 if $signal < 0;
# New chromosome
if ( $ref ne $old_ref )
{
if ( $ntoprint )
{
print "chromosome = \"$ref\"";
$ntoprint--;
unless ( $ntoprint ) { print " ... Remainder not shown"; }
print "\n";
}
dumpOffsets( $start, $SubsetName . ':' . $old_ref, @offsets )
unless $old_ref eq ''; # previous subset:arm
$old_ref = $ref;
print $COV "# subset=$SubsetName chromosome=$old_ref\n";
$offset = tell($COV);
$bdb_hash{ $SubsetName . ':' . $old_ref } =
$offset; # record offset where new subset:arm data starts
@offsets = ("-200000\t$offset");
print $COV "-200000\t0\n"; # insert one fictive zero read
$offset = tell($COV);
print $COV "0\t0\n"; # insert one more fictive zero read
push( @offsets, "0\t$offset" );
$counter = 0;
$old_signal = 0;
$old_coord = 0;
$lastRecordedCoord = 0;
} # if ( $ref ne $old_ref )
# fill in holes in coverage with 0
if ($start > $old_coord+1 && $old_signal > 0)
{
print $COV join("\t",++$old_coord,0), "\n";
$old_coord++;
$counter++;
$offset = tell($COV);
$old_signal = 0;
}
if ( $signal == $old_signal)
{
$old_coord = $end;
next;
}
$max_signal = $signal if $max_signal < $signal;
if ( $counter++ > $step
|| $start - $lastRecordedCoord > $coordstep )
{
push( @offsets, "$start\t$offset" );
$counter = 0;
$lastRecordedCoord = $start;
}
$old_coord = $end;
$old_signal = $signal;
print $COV join("\t", $start, $signal), "\n";
}
# don't forget to dump offsets data on file end..
dumpOffsets( $start,$SubsetName . ':' . $old_ref, @offsets )
unless $old_ref eq ''; # previous subset:arm
stdclose($INF);
return;
} # sub indexCoverageFile
#*************************************************************************
sub shmiggleIndexCoverageFile { my ( $file ) = @_;
#*************************************************************************
# to reduce amount of output, only print first $lognum $chromosome
my $ntoprint = $lognum;
# modifies global $max_signal
if( $debug && defined $do_only_subset )
{ return unless $file=~/^${do_only_subset}\./; }
my $zcat= get_zcat($file);
local(*INF);
open(INF,"$zcat $file |") || die "Can't open \"$file\" for input: $!\n";
my $SubsetName= ($file=~/^([^\.]+)\./) ? $1 : $file;
push(@SubsetNames,$SubsetName);
my $chromosome= "";
my @offsets= ();
# following setting is very important for performance (in some cases)
# value 1000 (otherwise good) on K.White dataset was causing start of reading 100K before the actually required point..
my $step= 1000; # step in coverage file lines ()signal reads to save start-offset
my $coordstep= 20000; # step in coords to save start-offset
my $counter= 0;
my $offset= tell(OUTDATF);
$ResIndexHash{$SubsetName}= $offset; # record offset where new subset data starts
my $old_signal= 0;
my $oldcoord= -200000;
my $FileFormat= 1;
my $StartCoord= 0;
my $lastRecordedCoord= -200000;
debugmsg ( "Indexing file \"$file\" SubsetName=\"$SubsetName\" offset=$offset" );
my $nlines = 0;
my $nchr = 0;
while( (my $str= <INF>) )
{
$nlines++;
$offset= tell(OUTDATF);
# correct variant of GEO preferred subset spec
if( $str=~m/^(track[ \t]+type=wiggle_0)\s*\n$/i ) # new subset starting
{
$str= $1 . ' name="' . $SubsetName ."\"\n";
debugmsg ( "Match at A str=\"$str\"" );
}
# following is a GEO preferred subset spec
if( $str=~m/^track[ \t]+type=wiggle_0[ \t]+name="([^"]+)"/i ) # new subset starting
{
$FileFormat= 4;
$SubsetName= $1;
#$chromosome= "";
debugmsg ( "Match at B format=$FileFormat" );
next; # because it is not a signal, should not be printed in this data loop
}
# fix for K.White files
elsif( $str=~m/^track[ \t]+type=bedGraph[ \t]+name="([^"]+)"/i ) # new subset starting
{
$FileFormat= 4;
$SubsetName=~s/_(combined|coverage)$//i; $SubsetName=~s/_(combined|coverage)$//i; $SubsetName=~s/^G[A-Z]{2}\d+[_\-]//;
#$chromosome= "";
debugmsg ( "Match at C format=$FileFormat" );
next; # because it is not a signal, should not be printed in this data loop
}
elsif( $str=~m/^variableStep[ \t]+(chr(om(osome)?)?|arm)=(\w+)/i ) # potentially new arm starting
{
$FileFormat= 4;
my $new_chromosome= $4;
# VCRU disable this next line from the original code, don't modify any names!
# $new_chromosome=~s/^chr(omosome)?//i;
debugmsg ( "Match at D format=$FileFormat" );
if( $new_chromosome ne $chromosome )
{
debugmsg ( "New chromosome \"$new_chromosome\" starting line $nlines" );
$nchr++;
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
$chromosome= $new_chromosome;
print OUTDATF "# subset=$SubsetName chromosome=$chromosome\n";
$offset= tell(OUTDATF);
$ResIndexHash{$SubsetName.':'.$chromosome}= $offset; # record offset where new subset:arm data starts
@offsets= ("-200000\t$offset");
print OUTDATF "-200000\t0\n"; # insert one fictive zero read
$offset= tell(OUTDATF);
print OUTDATF "0\t0\n"; # insert one more fictive zero read
push(@offsets,"0\t$offset");
if ( $ntoprint )
{
print "\t\t$SubsetName:$chromosome\n";
$ntoprint--;
unless ( $ntoprint ) { print "\t\tRemainder not shown...\n"; }
}
$counter= 0; $old_signal= 0; $oldcoord= 0; $lastRecordedCoord= 0;
}
next; # because it is not a signal, should not be printed in this data loop
}
elsif( $str=~m/^FixedStep[ \t]+(chr(om(osome)?)?|arm)=(\w+)[ \t]+Start=(\d+)/i ) # potentially new arm starting
{
$FileFormat= 3;
my $new_chromosome= $4;
$StartCoord= $5;
# VCRU disable this next line from the original code, don't modify any names!
# $new_chromosome=~s/^chr(omosome)?//i;
debugmsg ( "Match at E format=$FileFormat" );
if( $new_chromosome ne $chromosome )
{
debugmsg ( "New chromosome \"$new_chromosome\" starting line $nlines" );
$nchr++;
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
$chromosome= $new_chromosome;
print OUTDATF "# subset=$SubsetName chromosome=$chromosome\n";
$offset= tell(OUTDATF);
$ResIndexHash{$SubsetName.':'.$chromosome}= $offset; # record offset where new subset:arm data starts
@offsets= ("-200000\t$offset");
print OUTDATF "-200000\t0\n"; # insert one fictive zero read
$offset= tell(OUTDATF);
print OUTDATF "0\t0\n"; # insert one more fictive zero read
push(@offsets,"0\t$offset");
if ( $ntoprint )
{
print "\t\t$SubsetName:$chromosome\n";
$ntoprint--;
unless ( $ntoprint ) { print "\t\tRemainder not shown...\n"; }
}
$counter= 0; $old_signal= 0; $oldcoord= 0; $lastRecordedCoord= 0;
}
elsif( $StartCoord>$oldcoord+1 ) # hole, fill with zeros
{
$oldcoord++;
#print " hole (zeros) from $oldcoord to $StartCoord-1\n" if $debug;
print OUTDATF $oldcoord."\t0\n" unless $old_signal==0;
$old_signal= 0;
next if $signal==0; # no need to duplicate zeros..
}
elsif( $StartCoord<$oldcoord )
{ print "WARNING: backward ref in $file: $str"; }
next; # because it is not a signal, should not be printed in this data loop
}
elsif( $str=~m/^[#]?.*(chr(om(osome)?)?|arm)=(\w+)/ ) # potentially new arm starting
{
$FileFormat= 1;
my $new_chromosome= $4;
# VCRU disable this next line from the original code, don't modify any names!
# $new_chromosome=~s/^chr(omosome)?//i;
debugmsg ( "Match at F format=$FileFormat" );
if( $new_chromosome ne $chromosome )
{
debugmsg ( "New chromosome \"$new_chromosome\" starting line $nlines" );
$nchr++;
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
$chromosome= $new_chromosome;
print OUTDATF "# subset=$SubsetName chromosome=$chromosome\n";
$offset= tell(OUTDATF);
$ResIndexHash{$SubsetName.':'.$chromosome}= $offset; # record offset where new subset:arm data starts
@offsets= ("-200000\t$offset");
print OUTDATF "-200000\t0\n"; # insert one fictive zero read
$offset= tell(OUTDATF);
print OUTDATF "0\t0\n"; # insert one more fictive zero read
push(@offsets,"0\t$offset");
if ( $ntoprint )
{
print "\t\t$SubsetName:$chromosome\n";
$ntoprint--;
unless ( $ntoprint ) { print "\t\tRemainder not shown...\n"; }
}
$counter= 0; $old_signal= 0; $oldcoord= 0; $lastRecordedCoord= 0;
}
next; # because it is not a signal, should not be printed in this data loop
}
elsif( $str=~m/^[#]/ ) # other unspecified comments
{
debugmsg ( "Skipping comment line $nlines: \"$str\"" );
next;
}
elsif( $str=~m/^(\d+)[ \t]+(\d+)\s*\n/ ) { # [coord signal] format
$FileFormat= 1;
my($coord,$signal)= ($1,$2);
$signal= modifySignal($signal);
if( $signal==$old_signal )
{
$oldcoord= $coord;
next;
}
$max_signal= $signal if $max_signal<$signal;
if( $counter++>$step || $coord-$lastRecordedCoord>$coordstep )
{ push(@offsets,"$coord\t$offset"); $counter= 0; $lastRecordedCoord= $coord; }
$str= $coord."\t".$signal."\n";
$oldcoord= $coord;
$old_signal= $signal;
}
# following is a GEO preferred format
# VCRU added allowed decimal point in fourth column and in first column
# VCRU sample bed format
#track type=wiggle_0 name="soapunpaired" description="B493QAL EST fine coverage of soapunpaired"
#C10000184 1 10 0.0
#C10000184 11 20 0.2
elsif( $str=~m/^([\w\.]+)[ \t]+(\d+)[ \t]+(\d+)[ \t]+[\-]?([\d\.]+)\s*\n/ ) # [chr coord tocoord signal] format, all positions and skipped zeros
{
$FileFormat= 4;
my($new_chromosome,$coord,$tocoord,$signal)= ($1,$2,$3,$4);
my $samesignal_l= $tocoord-$coord;
# VCRU disable this next line from the original code, don't modify any names!
# $new_chromosome=~s/^chr(omosome)?//i;
if( $debug && defined $do_only_chr )
{ next unless $new_chromosome eq $do_only_chr; }
$signal= modifySignal($signal);
if( $new_chromosome ne $chromosome )
{
debugmsg ( "New chromosome \"$new_chromosome\" starting line $nlines" );
$nchr++;
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
$chromosome= $new_chromosome;
print OUTDATF "# subset=$SubsetName chromosome=$chromosome\n";
$offset= tell(OUTDATF);
$ResIndexHash{$SubsetName.':'.$chromosome}= $offset; # record offset where new subset:arm data starts
@offsets= ("-200000\t$offset");
print OUTDATF "-200000\t0\n"; # insert one fictive zero read
$offset= tell(OUTDATF);
print OUTDATF "0\t0\n"; # insert one more fictive zero read
push(@offsets,"0\t$offset");
if ( $ntoprint )
{
print "\t\t$SubsetName:$chromosome\n";
$ntoprint--;
unless ( $ntoprint ) { print "\t\tRemainder not shown...\n"; }
}
$counter= 0; $old_signal= 0; $oldcoord= 0; $lastRecordedCoord= 0;
}
if( $coord>$oldcoord+1 ) # hole, fill with zeros
{
$oldcoord++;
debugmsg ( "hole (zeros) from $oldcoord to $coord-1" );
print OUTDATF $oldcoord."\t0\n" unless $old_signal==0;
$old_signal= 0;
next if $signal==0; # no need to duplicate zeros..
}
elsif( $signal==$old_signal ) {
$oldcoord= $coord;
next;
}
$max_signal= $signal if $max_signal<$signal;
if( $counter++>$step || $coord-$lastRecordedCoord>$coordstep )
{ push(@offsets,"$coord\t$offset"); $counter= 0; $lastRecordedCoord= $coord; }
$str= $coord."\t".$signal."\n";
$oldcoord= $coord+$samesignal_l-1;
$old_signal= $signal;
}
elsif( $str=~m/^(\w+)[ \t]+(\d+)[ \t]+(\d+)\s*\n/ ) # [chr coord signal] format, all positions but skipped zeros
{
$FileFormat= 2;
my($new_chromosome,$coord,$signal)= ($1,$2,$3);
# VCRU disable this next line from the original code, don't modify any names!
# $new_chromosome=~s/^chr(omosome)?//i;
if( $debug && defined $do_only_chr )
{ next unless $new_chromosome eq $do_only_chr; }
$signal= modifySignal($signal);
if( $new_chromosome ne $chromosome )
{
debugmsg ( "New chromosome \"$new_chromosome\" starting line $nlines" );
$nchr++;
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
$chromosome= $new_chromosome;
print OUTDATF "# subset=$SubsetName chromosome=$chromosome\n";
$offset= tell(OUTDATF);
$ResIndexHash{$SubsetName.':'.$chromosome}= $offset; # record offset where new subset:arm data starts
@offsets= ("-200000\t$offset");
print OUTDATF "-200000\t0\n"; # insert one fictive zero read
$offset= tell(OUTDATF);
print OUTDATF "0\t0\n"; # insert one more fictive zero read
push(@offsets,"0\t$offset");
if ( $ntoprint )
{
print "\t\t$SubsetName:$chromosome\n";
$ntoprint--;
unless ( $ntoprint ) { print "\t\tRemainder not shown...\n"; }
}
$counter= 0; $old_signal= 0; $oldcoord= 0; $lastRecordedCoord= 0;
}
if( $coord>$oldcoord+1 ) # hole, fill with zeros
{
$oldcoord++;
#print " hole (zeros) from $oldcoord to $coord-1\n" if $debug;
print OUTDATF $oldcoord."\t0\n" unless $old_signal==0;
$old_signal= 0;
next if $signal==0; # no need to duplicate zeros..
}
elsif( $signal==$old_signal )
{
$oldcoord= $coord;
next;
}
$max_signal= $signal if $max_signal<$signal;
if( $counter++>$step || $coord-$lastRecordedCoord>$coordstep ) {
push(@offsets,"$coord\t$offset"); $counter= 0; $lastRecordedCoord= $coord; }
$str= $coord."\t".$signal."\n";
$oldcoord= $coord;
$old_signal= $signal;
}
elsif( $str=~m/^(\d+)\s*\n/ ) # [signal] format, all positions and skipped zeros
{
$FileFormat= 3;
my($coord,$signal)= ($StartCoord++,$1);
$signal= modifySignal($signal);
if( $signal==$old_signal )
{
$oldcoord= $coord;
next;
}
$max_signal= $signal if $max_signal<$signal;
if( $counter++>$step || $coord-$lastRecordedCoord>$coordstep )
{ push(@offsets,"$coord\t$offset"); $counter= 0; $lastRecordedCoord= $coord; }
$str= $coord."\t".$signal."\n";
$oldcoord= $coord;
$old_signal= $signal;
}
else # skip other data - unknown format
{
print ( "Skipping unknown data line $nlines: \"$str\"\n" );
next;
}
print OUTDATF $str;
}
# don't forget to dump offsets data on file end..
shmiggleDumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
close(INF);
unless ( $quiet ) { print "Processed ".commify($nlines)." lines, ".commify($nchr)." chromosomes in file \"$file\"\n"; }
return;
} # sub shmiggleIndexCoverageFile
#*************************************************************************
sub modifySignal { my $signal = shift;
#*************************************************************************
if ( $apply_log )
{
if( exists $LogTabs{$signal} )
{ $signal = $LogTabs{$signal}; }
else
{
# log base 2, and then apply magnification (default 1.0)
my $newval;
if ( $signal >= 1 ) # can't take log of zero, and below 1 is negative
{ $newval = int(log($signal)*$log_magnifier/$log2); }
else
{ $newval = 0; }
$LogTabs{$signal} = $newval;
$signal = $newval;
}
}
else
{ $signal = int($signal * $log_magnifier); }
if ( ( $crop ) and ( $signal > $crop ) ) { $signal = $crop; }
return($signal);
} # sub modifySignal
#***********************************************************
sub dumpOffsets { my ( $start, $key, @offsetlines ) = @_;
#***********************************************************
print $COV "# offsets for $key\n";
my $offset = tell($COV);
my $prevoffset = $offset;
$bdb_hash{ $key . ':offsets' } = $offset
; # record offset where offsets VALUES for subset:arm data start (skip header)
my $oldbigstep = 0;
foreach my $str (@offsetlines)
{
print $COV $str . "\n";
my ( $start, $floffset ) = split( /[ \t]+/, $str );
# following wasn't working properly..
my $newbigstep = int( $start / 1000000.0 );
if ( $newbigstep > $oldbigstep )
{
$bdb_hash{ $key . ':offsets:' . $newbigstep } =
$prevoffset; # one before is the right start
$oldbigstep = $newbigstep;
}
$prevoffset = $offset;
$offset = tell($COV);
}
return;
} # sub dumpOffsets
#*************************************************************************
sub shmiggleDumpOffsets { my ( $key, @offsetlines ) = @_;
#*************************************************************************
print OUTDATF "# offsets for $key\n";
my $offset= tell(OUTDATF);
my $prevoffset= $offset;
$ResIndexHash{$key.':offsets'}= $offset; # record offset where offsets VALUES for subset:arm data start (skip header)
my $oldbigstep= 0;
foreach my $str ( @offsetlines )
{
print OUTDATF $str . "\n";
my($coord,$floffset)= split(/[ \t]+/,$str);
# following wasn't working properly..
my $newbigstep= int($coord/1000000.0);
if( $newbigstep>$oldbigstep )
{
$ResIndexHash{$key.':offsets:'.$newbigstep}= $prevoffset; # one before is the right start
$oldbigstep= $newbigstep;
}
$prevoffset= $offset;
$offset= tell(OUTDATF);
}
return;
} # sub shmiggleDumpOffsets
#***********************************************************
sub get_zcat { my $fullfile = shift;
#***********************************************************
if( $fullfile=~/\.gz$/i )
{
my $zcat= `which zcat`;
if( $? != 0 ) { $zcat=`which gzcat`; }
chomp($zcat);
return($zcat);
}
elsif( $fullfile=~/\.bz2$/i ) { return('bzcat'); }
return('/bin/cat');
} # sub get_zcat
# eof