forked from Molmed/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.pl
executable file
·851 lines (686 loc) · 25.4 KB
/
archive.pl
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
#!/usr/bin/perl -w
use FindBin; # Find the script location
use lib "$FindBin::Bin/lib";# Add the script libdir to libs
use Molmed::Sisyphus::Libpath;
use strict;
use Getopt::Long;
use Pod::Usage;
use Cwd qw(abs_path cwd);
use File::Basename;
use File::Find;
use File::Copy;
use Digest::MD5;
use Molmed::Sisyphus::Common qw(mkpath);
=head1 NAME
archive.pl - Archive a runfolder
=head1 SYNOPSIS
archive.pl -help|-man
archive.pl -runfolder <runfolder> -outdir <outdir> [-debug]
=head1 OPTIONS
=over 4
=item -h|-help
prints out a brief help text.
=item -m|-man
Opens the manpage.
=item -runfolder
The runfolder to pack.
=item -outdir
The directory to create the archive copy in.
=item -verifyOnly
Assume the archiving has already been done and only do the verification.
=item -swestore
Start archive2swestore.pl when done
=item -debug
Print debugging information
=back
=head1 DESCRIPTION
archive.pl is a script for creating a copy of runfolder suitable for archival.
All files except those in a compressed format like png and jpeg are compressed
with gzip and the checksums of both original and compressed files are written
to special files. The files are copied to the target and verified.
Two archives are created, one a tar-archive of all files except the fastq-files,
and one with the project folders suitable for delivery to clients.
This results in the following structure:
121120_SN866_0192_BD1H31ACXX/
121120_SN866_0192_BD1H31ACXX/121120_SN866_0192_BD1H31ACXX.tar
121120_SN866_0192_BD1H31ACXX/Projects/LE-0082/
121120_SN866_0192_BD1H31ACXX/Projects/LE-0082/Report.tar.gz
121120_SN866_0192_BD1H31ACXX/Projects/LE-0082/Sample_123/
121120_SN866_0192_BD1H31ACXX/Projects/LE-0082/Sample_123/123_TTAGGC_L008_R1_001.fastq.gz
This requires that extract_project has already been run with the outbox set to <runfolder>/Projects/!
=head1 FUNCTIONS
=cut
# Parse options
my($help,$man) = (0,0);
my($rfPath,$outDir,$verifyOnly,$swestore) = (undef,undef,0,0);
our($debug) = 0;
GetOptions('help|?'=>\$help,
'man'=>\$man,
'runfolder=s' => \$rfPath,
'outdir=s' => \$outDir,
'verifyOnly' =>\$verifyOnly,
'swestore' =>\$swestore,
'debug' => \$debug,
) or pod2usage(-verbose => 0);
pod2usage(-verbose => 1) if ($help);
pod2usage(-verbose => 2) if ($man);
unless(defined $rfPath && -e $rfPath){
print STDERR "Runfolder not specified or does not exist\n";
pod2usage(-verbose => 1);
exit;
}
unless(defined $outDir){
print STDERR "Output directory must be specified\n";
pod2usage(-verbose => 1);
exit;
}
# Create a new sisyphus object for common functions
my $sisyphus = Molmed::Sisyphus::Common->new(PATH=>$rfPath, DEBUG=>$debug);
$rfPath = $sisyphus->PATH;
$outDir =~ s:/+$::;
# Add the year and month to the outdir if not already included
unless($outDir =~ m/201\d-[0123]\d$/){
if($sisyphus->RUNFOLDER =~ m/(1\d)([01]\d)[0123]\d_/){
$outDir .= "/20$1-$2";
}
}
$outDir = "$outDir/" . $sisyphus->RUNFOLDER;
# Make outDir absolute, if it is not already
$outDir = abs_path($outDir) unless( $outDir =~ m(^/) );
my $runfolder = $sisyphus->RUNFOLDER;
my %checksums = (ORIGINAL=>{}, COMPRESSED=>{});
unless($verifyOnly){
# First create the output runfolder
if(-e $outDir){
warn "$outDir already exists\n";
my $i=1;
while(-e "$outDir.old.$i"){
$i++;
}
warn "Renaming it to $outDir.old.$i\n";
rename($outDir, "$outDir.old.$i");
}
mkpath($outDir,2770) or die "Failed to create output runfolder '$outDir': $!\n";
# check that all projects have been processed
my $sampleSheet = $sisyphus->readSampleSheet();
foreach my $proj (keys %{$sampleSheet}){
unless(-e "$rfPath/Statistics/Project_$proj/extractProject.complete" || -e "$rfPath/Statistics/Project_$proj/extractProject.complete.gz"){
die "$proj has not been processed, will not archive!"
}
}
# First make one big tar-file of everything except fastq-files and the Projects-dir.
archiveRunfolder($sisyphus, \%checksums, $outDir);
# Then make one tar-file per project containing reports and plots
my %projMd5fh;
foreach my $proj (keys %{$sampleSheet}){
my $sums = { ORIGINAL=>{}, COMPRESSED=>{} };
archiveProjectReports($proj, $sisyphus, $sums, $outDir);
foreach my $src (qw(ORIGINAL COMPRESSED)){
my $md5fh;
if($src eq 'ORIGINAL'){
open($md5fh, '>', "$outDir/Projects/$proj/$runfolder.original.md5") or die "Failed to open '$outDir/$runfolder/Projects/$proj/$runfolder.original.md5': $!";
}else{
open($md5fh, '>', "$outDir/Projects/$proj/$runfolder.compressed.md5") or die "Failed to open '$outDir/$runfolder/Projects/$proj/$runfolder.compressed.md5': $!";
}
$projMd5fh{$proj}->{$src} = $md5fh;
foreach my $key (keys %{$sums->{$src}}){
$checksums{$src}->{$key} = $sums->{$src}->{$key};
(my $file = $key) =~ s:^$rfPath/Projects/::;
print $md5fh "$sums->{$src}->{$key} $file\n";
}
}
}
# Archive the MiSeq_Runfolder.tar.gz if it exist.
if(-e "MiSeq_Runfolder.tar.gz") {
$sisyphus->copy("MiSeq_Runfolder.tar.gz", $outDir, {VERIFY=>1,LINK=>1,RELATIVE=>0});
$sisyphus->copy("MD5/checksums.miseqrunfolder.md5",$outDir, {VERIFY=>1,LINK=>1,RELATIVE=>0});
# Rename the checksum file that goes into swestore
move("$outDir/checksums.miseqrunfolder.md5","$outDir/MiSeq_Runfolder.original.md5") or warn "Failed to rename the MiSeq_Runfolder checksum file";
}
# Then copy/link the fastq-files. If the file exists in both the project directory
# and the Unaligned/Excluded directory, archive only the copy in the project directory
# Write the skipped files to a list
my $skipped = archiveFastq($sisyphus, \%checksums, $outDir, [keys %{$sampleSheet}], \%projMd5fh);
open(my $skFh, '>', "$outDir/$runfolder.skipped.txt") or die "Failed to open '$outDir/$runfolder.skipped.txt': $!";
print $skFh "# Files skipped because they are identical to an already included file\n";
foreach my $key (keys %{$skipped}){
(my $file1 = $key) =~ s/^$rfPath/$runfolder/;
(my $file2 = $skipped->{$key}) =~ s/^$rfPath/$runfolder/;
print $skFh "$file1\t$file2\n";
}
close($skFh);
open(my $orgFh, '>', "$outDir/$runfolder.original.md5") or die "Failed to open '$outDir/$runfolder.original.md5': $!";
foreach my $key (keys %{$checksums{ORIGINAL}}){
(my $file = $key) =~ s/^$rfPath/$runfolder/;
print $orgFh "$checksums{ORIGINAL}->{$key} $file\n";
}
close($orgFh);
open(my $compFh, '>', "$outDir/$runfolder.compressed.md5") or die "Failed to open '$outDir/$runfolder.compressed.md5': $!";
foreach my $key (keys %{$checksums{COMPRESSED}}){
(my $file = $key) =~ s/^$rfPath/$runfolder/;
print $compFh "$checksums{COMPRESSED}->{$key} $file\n";
}
close($compFh);
print STDERR "Archiving done, starting verification\n";
} # end unless $verifyOnly
# First verify that all files that should be archived are included in
# the (compressed) checksums file
# Begin by reading the file we just wrote
my %compressedMd5;
open(my $compFh, '<', "$outDir/$runfolder.compressed.md5") or die "Failed to read '$outDir/$runfolder.compressed.md5': $!";
while(<$compFh>){
chomp;
my($md5,$path)=split / /, $_;
$compressedMd5{$path} = $md5;
}
# Let a sub check the original dirtree against the list of files
verifyFileList($sisyphus, \%compressedMd5, $outDir);
# Now verify that we can read all (exept fastq) files from the tar-files created
my %archiveMd5;
if(verifyTarFiles($outDir, \%compressedMd5, \%archiveMd5)){
print STDERR "Tar files passed OK\n";
}else{
print STDERR "Tar files FAILED\n";
exit 1;
}
# If we have come this far, make a new MD5-file for tar- and fastq-files
print STDERR "Writing archive MD5-sums\n";
open(my $archFh, '>', "$outDir/$runfolder.archive.md5") or die "Failed to open '$outDir/$runfolder.archive.md5': $!";
foreach my $key (keys %archiveMd5){
my $file = $key;
$file =~ s:$outDir:$runfolder:;
print $archFh "$archiveMd5{$key} $file\n";
}
close($archFh);
print STDERR "$runfolder archive verified\n";
# Now kick off a script to upload to SweStore and verify the upload
# But first we have to unpack ourself if we were started from
# the Sisyphus dir in the runfolder
find(\&_gunzipScriptBin, $FindBin::Bin);
if(-e "$rfPath/sisyphus.yml.gz"){
system("gunzip $rfPath/sisyphus.yml.gz")==0 or die "Failed to gunzip '$rfPath/sisyphus.yml.gz': $!";
}
if($swestore){
my $debugFlag = '';
$debugFlag = '-debug', if($debug);
print STDERR "Starting archive2swestore.pl with\n";
print STDERR qq($FindBin::Bin/archive2swestore.pl -runfolder $outDir -config $rfPath/sisyphus.yml $debugFlag\n);
exec ($FindBin::Bin."/archive2swestore.pl",
'-runfolder', $outDir,
'-config', "$rfPath/sisyphus.yml",
$debugFlag);
# Remaining options should be read from sisyphus.yml
}
# Sub for gunzip of Sisyphus directory
sub _gunzipScriptBin{
if(m/\.pl\.gz/){
system('gunzip', $_)==0 or die;
s/\.gz$//;
system('chmod','+x', $_)==0 or die;
}elsif(m/\.pm\.gz/){
system('gunzip', $_)==0 or die;
}
}
=pod
=head2 verifyTarFiles()
Title : verifyTarFiles()
Usage : verifyTarFiles($outDir, \%checksums)
Function: Read all tar-files from an archive directory and verify against the checksums
Example :
Returns : nothing
Args : The archive directory to start in
A hashref with the expected files. Path as key, checksum as value
=cut
sub verifyTarFiles{
my $archDir = shift;
my $checksums = shift;
my $archiveMd5 = shift; # Used to store checksums for the archive files
my $ok = 1;
# Make a reverse copy of the checksum hash
my %fileSums;
foreach my $file (keys %{$checksums}){
push @{$fileSums{$checksums->{$file}}}, $file;
}
# Assume basename of dir is runfolder name
my $rfName = basename($archDir);
# There should be a tar file for the runfolder
my $rfTar = "$archDir/$rfName.tar";
unless(-e $rfTar){
die "$rfTar does not exist";
}
print STDERR "Verifying $rfTar ...\n";
if(verifyTar($rfTar,undef,\%fileSums, $archiveMd5, $rfName)){
print STDERR "$rfTar\tOK\n";
}else{
print STDERR "$rfTar\tFAILED\n";
$ok = 0;
}
opendir(my $projDir, "$archDir/Projects/");
foreach my $proj ( grep {!/^\.{1,2}$/} readdir($projDir) ){
print STDERR "Verifying $archDir/Projects/$proj/$rfName.tar ...\n";
if(verifyTar("$archDir/Projects/$proj/$rfName.tar", "$rfName/Projects", \%fileSums, $archiveMd5, $rfName)){
print STDERR "$archDir/Projects/$proj/$rfName.tar\tOK\n";
}else{
print STDERR "$archDir/Projects/$proj/$rfName.tar\tFAILED\n";
$ok = 0;
}
}
# The remaining files in %fileSums should be single files which we will check
# after upload to SweStore, so just check that they exist
# Make a reverse lookup on the checksums
my @missing;
foreach my $sum (keys %fileSums){
unless(grep /^SEEN$/, @{$fileSums{$sum}}){
foreach my $file (@{$fileSums{$sum}}){
next if($file eq 'SEEN');
if(-e "$archDir/../$file"){
# Save the checksum for single files
$archiveMd5->{$file} = $checksums->{$file};
}else{
push @missing, $file;
}
}
}
}
if(@missing){
print STDERR "The following files are missing from the archive\n";
print STDERR join "\n", @missing;
print STDERR "\n";
$ok = 0;
}else{
print STDERR "All files were included\n";
}
return $ok;
}
sub verifyTar{
my $tarFile = shift;
my $dirName = shift;
my $fileSums = shift;
my $archiveMd5 = shift;
my $rfName = shift;
my $ok = 1;
open(my $tarFh, '-|', qq(tar xf $tarFile --to-command 'echo -n "\$TAR_FILENAME\t"; md5sum')) or die $!;#'$FindBin::Bin/verifyTarHelper.sh'") or die $!;
while(<$tarFh>){
chomp;
s/ -$//;
my($f,$sum) = split /\t/, $_;
# print STDERR "$f -- $sum\n";
$f = "$dirName/$f" if(defined $dirName);
if(exists $fileSums->{$sum} && grep /^$f$/, @{$fileSums->{$sum}}){
push @{$fileSums->{$sum}}, 'SEEN';
}else{
print STDERR "Checksum for $f does not exist\n";
$ok = 0;
}
}
if($ok){
open(my $fh, $tarFile) or die "Failed to open $tarFile: $!";
my $sum = Digest::MD5->new->addfile($fh)->hexdigest;
$archiveMd5->{$tarFile} = $sum;
close($fh);
}
return $ok;
}
=pod
=head2 verifyFileList()
Title : verifyFileList()
Usage : verifyFileList($sisyphus, \%checksums)
Function: Recurse the runfolder and verify that all files are included in the checksums hash
Example :
Returns : nothing
Args : A Sisyphus::Common object for the runfolder to archive
A hashref with the expected files. Path as key, checksum as value
=cut
sub verifyFileList{
my $sisyphus = shift;
my $checksums = shift;
# Make a copy of all filenames
my %files;
@files{keys %{$checksums}} = ();
# Make a reverse lookup on the checksums
my %revSums;
@revSums{values %{$checksums}} = ();
my $rfPath = $sisyphus->PATH;
my $dirMask = dirname($rfPath);
# This should delete all entries in %files that are present in the runfolder
# So files should be empty when sub returns
my @missed = verifyRecurseRunfolder($rfPath,$dirMask,\%files);
if(keys %files > 0){
print STDERR "Found files in checksum file that are missing from runfolder\n";
print STDERR join("\n", keys %files), "\n";
exit 1;
}
my @skipped;
foreach my $file (@missed){
# Skip the sisyphus md5-sum files
next if($file=~ m:MD5/[^/]+\.md5$:);
# If the checksum exists in the list we got, then this file
# is included with another name
# Sisyphus should have the checksum in cache, so we hopefully
# do not have the overhead of calculating it here
my $md5 = $sisyphus->getMd5($file);
next if(exists $revSums{$md5});
push @skipped, $file;
}
if(@skipped){
print STDERR "The following files are skipped from the archive\n";
print join "\n", @skipped;
print "\n---END SKIPPED---\n\n";
}
}
=pod
=head2 verifyRecurseRunfolder()
Title : verifyRecurseRunfolder()
Usage : verifyRecurseRunfolder($dirPath, $dirMask, \%files)
Function: Recurse the runfolder and verify remove any seen files from the %files hash
Example :
Returns : a list of files that are not present in the %files hash
Args : A directory to start in
A directory mask to remove from the path for finding the hash key
A hashref with the expected files. Path as key
=cut
sub verifyRecurseRunfolder{
my $dir = shift;
my $dirMask = shift;
my $files = shift;
my @missed;
opendir(my $dh, $dir) or die;
foreach my $file ( grep {!/^\.{1,2}$/} readdir($dh) ){
if(-d "$dir/$file"){
push @missed, verifyRecurseRunfolder("$dir/$file", $dirMask, $files);
}else{
my $path = "$dir/$file";
$path =~ s:^$dirMask/*::;
if(exists $files->{$path}){
delete $files->{$path};
}else{
push @missed, $path;
}
}
}
return @missed;
}
=pod
=head2 archiveRunfolder()
Title : archiveRunfolder
Usage : archiveRunfolder($sisyphus, \%checksums, $outDir)
Function: Recursively archive the contentes of runfolder $inDir to $outDir
But skip the fastq.gz files
Example :
Returns : nothing
Args : A Sisyphus::Common object for the runfolder to archive
A hashref for saving checksums of the archived files
The absolute path of the output directory
=cut
sub archiveRunfolder{
my $sisyphus = shift;
my $checksums = shift;
my $outDir = shift;
my $rfPath = $sisyphus->PATH;
my $rfName = $sisyphus->RUNFOLDER;
my $dirMask = dirname($rfPath);
my $tarFile = "$outDir/$rfName.tar";
$tarFile =~ s:/+:/:g; # Remove multiple slashes
open(my $tarPipe, qq(| tar cf "$tarFile" --no-recursion -C "$dirMask" --files-from -) )
or die qq(Failed to open tarpipe 'tar cf "$tarFile" --no-recursion -C "$dirMask" --files-from -'\n\t$!\n);
compressMiSeqRunFolder($rfPath, $checksums, $sisyphus, $dirMask);
recurseRunfolder($rfPath, $tarPipe, $checksums, $sisyphus, $dirMask);
}
=pod
=head2 recurseRunfolder()
Title : recurseRunfolder
Usage : recurseRunfolder($dir, $tarFh, \%checksums, $sisyphus, $dirMask)
Function: Recurse a runfolder and write all files to be included to the open
tar pipe/filehandle.
Example :
Returns : nothing
Args : The path to recurse from
An open filehandle for writing filenames to tar
A hashref for saving checksums of the archived files
A Sisyphus::Common object for the runfolder to archive
The part of the path to remove from the file's path
=cut
sub recurseRunfolder {
my $dir = shift;
my $tarPipe = shift;
my $checksums = shift;
my $sisyphus = shift;
my $dirMask = shift;
if(basename($dir) eq 'MD5'){
print STDERR "Skipping MD5 dir '$dir'\n" if($debug);
return;
}
if(basename($dir) eq 'Projects'){
print STDERR "Skipping Projects dir '$dir'\n" if($debug);
return;
}
if(basename($dir) eq 'MiSeq_Runfolder'){
print STDERR "Skipping MiSeq_Runfolder dir '$dir'\n" if($debug);
return;
}
opendir(my $dh, $dir) or die;
foreach my $file ( grep {!/^\.{1,2}$/} readdir($dh) ){
# Skip fastq files and delivery logs
next if($file =~ m/\.fastq(\.gz)?$/ ||
$file =~ m/\.delivery\.log$/);
if(-d "$dir/$file"){
# Recurse into sub directories
recurseRunfolder("$dir/$file", $tarPipe, $checksums, $sisyphus, $dirMask);
}else{
# Skip archiving logs from sbatch, as this might be written to
# by self
if( !($dir =~ m:slurmscripts/log: && $file =~ /^Arch-.*\.log/i) && !($file =~/MiSeq_Runfolder\.tar\.gz/)){
addFile($dir, $file, $tarPipe, $checksums, $sisyphus, $dirMask);
}
}
}
closedir($dh);
}
=pod
=head2 addFile()
Title : addFile
Usage : addFile($dir, $file, $tarFh, \%checksums, $sisyphus, $dirMask)
Function: Make sure the file is compressed and get the checksum, then
write the file path to be included to the open tar pipe/filehandle.
Example :
Returns : nothing
Args : The directory path of the file
The filename
An open filehandle for writing filenames to tar
A hashref for saving checksums of the archived files
A Sisyphus::Common object for the runfolder to archive
The part of the path to remove from the file's path
=cut
sub addFile{
my $inDir = shift;
my $file = shift;
my $tarPipe = shift;
my $checksums = shift;
my $sisyphus = shift;
my $dirMask = shift;
# Compress files if necessary, skip compressing the Sisyphus code, as well as reports and checksums and README in the project dir
if($file =~ m/\.(png|jpg|jpeg|zip)$/i ||
$file =~ m/(summary)?report.(htm|xm|xs)l/i ||
"$inDir/$file"=~m:Projects/.*/(checksums|README|.*\.md5): ||
$inDir =~ m/\/Sisyphus/){
$checksums->{ORIGINAL}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
$checksums->{COMPRESSED}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
}elsif($file =~ m/\.(gz|bz2)$/i){
$checksums->{COMPRESSED}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
(my $original = $file) =~ s/\.(gz|bz2)$//i;
my $md5 = $sisyphus->getMd5("$inDir/$original", -skipMissing=>1);
if(defined $md5){
$checksums->{ORIGINAL}->{"$inDir/$original"} = $md5;
}else{
$checksums->{ORIGINAL}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
}
}elsif(! -l "$inDir/$file"){ # Do not checksum symlinks
$checksums->{ORIGINAL}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
$file = $sisyphus->gzip("$inDir/$file"); # Gzip returns abs path
$file =~ s:^$inDir/::; # Make $file relative again
$checksums->{COMPRESSED}->{"$inDir/$file"} = $sisyphus->getMd5("$inDir/$file");
}
my $path = "$inDir/$file";
$path =~ s:^$dirMask/*::;
print $tarPipe "$path\n" || die "Failed to write $path to tarPipe";
}
=pod
=head2 archiveProjectReports()
Title : archiveProjectReports
Usage : archiveProjectReports($proj,$sisyphus,\%checksums, $outDir)
Function: Archive the contentes of the report for $proj
Example :
Returns : A hashref with md5sums of the included files
Args : The name of the project to include
A Sisyphus::Common object for the runfolder to archive
A hashref for saving checksums of the archived files
The absolute path of the output directory
=cut
sub archiveProjectReports{
my $proj = shift;
my $sisyphus = shift;
my $checksums = shift;
my $outDir = shift;
my $rfPath = $sisyphus->PATH;
my $rfName = $sisyphus->RUNFOLDER;
my $dirMask = "$rfPath/Projects/";
my $tarFile = "$outDir/Projects/$proj/$rfName.tar";
$tarFile =~ s:/+:/:g; # Remove multiple slashes
unless(-e dirname($tarFile)){
mkpath(dirname($tarFile), 2770);
}
open(my $tarPipe, qq(| tar cf "$tarFile" --no-recursion -C "$dirMask" --files-from -) )
or die qq(Failed to open tarpipe 'tar cf "$tarFile" --no-recursion -C "$dirMask" --files-from -'\n\t$!\n);
recurseProjectReports("$rfPath/Projects/$proj", $tarPipe, $checksums, $sisyphus, $dirMask);
}
sub recurseProjectReports{
my $dir = shift;
my $tarPipe = shift;
my $checksums = shift;
my $sisyphus = shift;
my $dirMask = shift;
opendir(my $dh, $dir) or die "Failed to open $dir: $!";
foreach my $file ( grep {!/^\.{1,2}$/} readdir($dh) ){
# Skip fastq files
next if($file =~ m/\.fastq(\.gz)?$/);
if(-d "$dir/$file"){
# Recurse into sub directories
recurseProjectReports("$dir/$file", $tarPipe, $checksums, $sisyphus, $dirMask);
}else{
addFile($dir, $file, $tarPipe, $checksums, $sisyphus, $dirMask);
}
}
closedir($dh);
}
=pod
=head2 archiveFastq()
Title : archiveFastq
Usage : archiveFastq($sisyphus,\%checksums, $outDir, $projects, $projMd5fh)
Function: Add the fastq files to the archive. Only include one copy of each file,
regardless of location. Priority is given the files located in project
directories.
Example :
Returns : A hashref with md5sums of the included files
Args : A Sisyphus::Common object for the runfolder to archive
A hashref for saving checksums of the archived files
The absolute path of the output directory
An arrayref of the project directories
A hashref with open filehandles for each projects checksum files, project as key
=cut
sub archiveFastq{
my $sisyphus = shift;
my $checksums = shift;
my $outDir = shift;
my $projects = shift;
my $projMd5fh = shift;
my $rfPath = $sisyphus->PATH;
my $rfName = $sisyphus->RUNFOLDER;
my $skipped = {};
# Start with the fastq-files in the Projects folder
# And keep track of which we have already seen
my %seen;
foreach my $dir (@{$projects}, "Unaligned", "Excluded"){
my $sums = {COMPRESSED=>{}, ORIGINAL=>{}};
if($dir eq "Unaligned"||$dir eq "Excluded"){
next unless(-e "$rfPath/$dir");
recurseFastq($sisyphus, "$rfPath/$dir", "$outDir/$dir", $sums, \%seen, $skipped);
}else{
next unless(-e "$rfPath/Projects/$dir");
recurseFastq($sisyphus, "$rfPath/Projects/$dir", "$outDir/Projects/$dir", $sums, \%seen, $skipped);
}
foreach my $src (qw(ORIGINAL COMPRESSED)){
foreach my $key (keys %{$sums->{$src}}){
unless($dir eq "Unaligned" || $dir eq "Excluded"){
(my $file = $key) =~ s:^$rfPath/Projects/::;
print {$projMd5fh->{$dir}->{$src}} "$sums->{$src}->{$key} $file\n";
}
$checksums->{$src}->{$key} = $sums->{$src}->{$key};
}
}
}
return($skipped);
}
sub recurseFastq{
my $sisyphus = shift;
my $indir = shift;
my $outdir = shift;
my $checksums = shift;
my $seen = shift;
my $skipped = shift;
my $rfName = $sisyphus->RUNFOLDER;
my $rfPath = $sisyphus->PATH;
opendir(my $dh, $indir) or die;
foreach my $file ( grep {!/^\.{1,2}$/} readdir($dh) ){
if(-d "$indir/$file"){
# Recurse into sub directories
recurseFastq($sisyphus, "$indir/$file", "$outdir/$file", $checksums, $seen, $skipped);
}elsif($file =~ m/\.fastq(\.gz)?$/){
my $srcMd5 = $sisyphus->getMd5("$indir/$file");
if(exists $seen->{"$file-$srcMd5"} && $seen->{"$file-$srcMd5"}){
$skipped->{"$indir/$file"} = $seen->{"$file-$srcMd5"};
}else{
my ($target,$md5) = $sisyphus->copy("$indir/$file", $outdir, {VERIFY=>1,LINK=>1,RELATIVE=>0});
$seen->{"$file-$srcMd5"} = "$indir/$file";
if($file=~m/\.gz$/){
$checksums->{COMPRESSED}->{"$indir/$file"} = $md5;
}
(my $orgFile = $file) =~ s/\.gz$//;
my $orgSum = $sisyphus->getMd5("$indir/$orgFile", -skipMissing=>1);
if(defined $orgSum){
$checksums->{ORIGINAL}->{"$indir/$orgFile"} = $orgSum;
}
}
}
}
closedir($dh);
}
=pod
=head2 compressMiSeqRunFolder()
Title : compressMiSeqRunFolder
Usage : compressMiSeqRunFolder()($dir, $tarPipe, \%checksums, $sisyphus, $dirMask)
Function: Compress the MiSeq_Runfolder if it exists.
Example :
Return : nothing
Args : The directory path of the file
An open filehandle for writing filenames to tar
A hashref for saving checksums of the archived files
An open filehandle for writing filenames to tar
A Sisyphus::Common object for the runfolder to archive
The part of the path to remove from the file's path
=cut
sub compressMiSeqRunFolder{
my $inDir = shift;
my $checksums = shift;
my $sisyphus = shift;
my $dirMask = shift;
my $file = 'MiSeq_Runfolder';
my $md5Sum = 'MD5/checksums.miseqrunfolder.md5';
if(-e "$inDir/$file.tar.gz") {
$checksums->{COMPRESSED}->{"$inDir/$file.tar.gz"} = $sisyphus->getMd5("$inDir/$file.tar.gz");
print "Compressed MiSeq_Runfolder already exists!\n";
return;
}
elsif( -d "$inDir/$file"){
die "MiSeq_Runfolder exists but is not compressed!\n";
}
}