File: index_cov_files.pl

package info (click to toggle)
libbio-graphics-perl 2.40-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,884 kB
  • sloc: perl: 21,801; makefile: 14
file content (384 lines) | stat: -rw-r--r-- 14,255 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl
#
# index_cov_files.pl
#
# [ to see proper formatting set tab==2 ]
#
# 2009-2010 Victor Strelets, FlyBase.org 

##$testing= 300000;

#$debug= 1; $do_only_chr= '2L';  
#$debug= 1; $do_only_chr= '2L'; $do_only_subset= 'BS40_all_unique'; 
#$debug= 1; $do_only_subset= 'Female_Heads'; $do_only_chr= '2';

$apply_log= 0;
$log2= log(2);
%LogTabs= ( 0, 0, 1, 0 ); # for our purposes, these start values are right	
$log_magnifier= 1.0;

	if( @ARGV && $ARGV[0]=~/^\-?log/i ) { $apply_log= 1; print "applying log\n"; }
	
	print "\nIndexing COV files for use with the fb_shmiggle glyph\n";

	eval {use lib("/LS/common/system-local/perl/lib"); };
	use BerkeleyDB;

	my $filesmask= '*.cov*';
	$filemask= shift(@ARGV) if @ARGV;
	$filemask=~s/\./\\\./g;
	$filemask=~s/[\*]/\\*/g;
	indexfeatdir('./',$filesmask) ; 

	exit();

#*************************************************************************
#
#*************************************************************************

sub indexfeatdir 
{
	my($dir,$mask)= @_;

	local(*D); opendir(D, $dir) || warn "can't open $dir";
	my @files= grep( /\.(cov|wig)/i, readdir(D));
	closedir(D);
	
	my $datfilename= 'data.cat';
	system("rm $datfilename") if -e $datfilename;
	open(OUTDATF,'>'.$datfilename) || die "Cannot open $datfilename!";
	my $bdbfilename= 'index.bdbhash';
	unlink($bdbfilename) if( -e $bdbfilename );
	%ResIndexHash= (); # !!GLOBAL
	tie %ResIndexHash, "BerkeleyDB::Hash", -Filename => $bdbfilename, -Flags => DB_CREATE;

	$max_signal = 0;
	@SubsetNames= ();
	foreach my $file (sort @files) { 
		indexCoverageFile($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 ) { print "\t$kkey => ".$ResIndexHash{$kkey}."\n"; }
	if( $max_signal>10000 ) { print "WARNING: max_signal=$max_signal - TOO HIGH!! Re-run with '-log' option\n"; } 
  untie %ResIndexHash;
	chmod(0666,$bdbfilename); # ! sometimes very important
	close(OUTDATF);

	return;
}
 
#*************************************************************************
#
#*************************************************************************

sub indexCoverageFile 
{
  my($file)= @_;
	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";
	print "\t$file\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;
	while( (my $str= <INF>) ) {
		$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"; }

		# 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= ""; 
			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= ""; 
			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; 
			$new_chromosome=~s/^chr(omosome)?//i;
			if( $new_chromosome ne $chromosome ) {
				dumpOffsets($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"); 
				print "\t\t$SubsetName:$chromosome\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;
			$new_chromosome=~s/^chr(omosome)?//i;
			if( $new_chromosome ne $chromosome ) {
				dumpOffsets($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"); 
				print "\t\t$SubsetName:$chromosome\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; 
			$new_chromosome=~s/^chr(omosome)?//i;
			if( $new_chromosome ne $chromosome ) {
				dumpOffsets($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"); 
				print "\t\t$SubsetName:$chromosome\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/^[#]/ ) { next; } # other unspecified comments

		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
		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;
			$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 ) {
				dumpOffsets($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"); 
				print "\t\t$SubsetName:$chromosome\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+$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);
			$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 ) {
				dumpOffsets($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"); 
				print "\t\t$SubsetName:$chromosome\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 { next; } # skip other data - unknown format
		print OUTDATF $str;
		}
	# don't forget to dump offsets data on file end..
	dumpOffsets($SubsetName.':'.$chromosome,@offsets) unless $chromosome eq ""; # previous subset:arm
	close(INF);
  return;
}

#*************************************************************************
#
#*************************************************************************

sub modifySignal
{
	my $signal= shift;
	return($signal) unless $apply_log;
	if( exists $LogTabs{$signal} ) { $signal= $LogTabs{$signal}; }
	else {
		my $newval= int(log($signal)*$log_magnifier/$log2); # make it larger (magnification)
		$LogTabs{$signal}= $newval;
		$signal= $newval;
		}
	return($signal);
}

#*************************************************************************
#
#*************************************************************************

sub dumpOffsets
{
	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 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'); 
}

#***********************************************************
#
#***********************************************************