-
Notifications
You must be signed in to change notification settings - Fork 15
/
illumina_createConfig.pl
executable file
·184 lines (158 loc) · 5.45 KB
/
illumina_createConfig.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
#!/usr/bin/perl -w
###################################################
### illumina_createConfig.pl
### - Creates a config file based on user input
### Author: R.F.Ernst
###################################################
use strict;
use Cwd qw( abs_path );
use File::Basename qw( dirname );
use Getopt::Long;
use File::Path qw(make_path);
### Variables ###
my $settingsDir = dirname(abs_path($0))."/settings";
### Check usage ###
#interactive() if @ARGV == 0;
### Parse options ###
my $iniFile;
my $iniPath;
my $ini;
my $outputDir;
my @fastqDirs;
my @bamDirs;
my $vcfFile;
my $mail;
my $help;
my $run;
GetOptions ("iniFile|i=s" => \$iniFile,
"iniPath|ip=s" => \$iniPath,
"outputDir|o=s" => \$outputDir,
"fastqDir|f=s" => \@fastqDirs,
"bamDir|b=s" => \@bamDirs,
"vcfFile|v=s" => \$vcfFile,
"mail|m=s" => \$mail,
"help|h" => \$help,
"run" => \$run)
or die usage();
if ($help || ! ( $iniFile || $iniPath ) || ! $outputDir || ! (@fastqDirs || @bamDirs || $vcfFile ) || ! $mail ) { usage() }
### Non interactive mode ###
if ( $iniFile) {
$ini = $settingsDir."/".$iniFile;
} elsif ($iniPath) {
$ini = $iniPath;
}
if ( ! -e $ini ) {
print "ERROR: $ini does not exist.\n";
}
createConfig($ini,$outputDir,\@fastqDirs,\@bamDirs,$vcfFile,$mail,$run);
### Parse and print available ini files ###
sub getIniFiles{
my $iniDir = shift;
my @iniFiles;
my $iniIndex = -1;
opendir (INIDIR, $iniDir) or die "Can't open $iniDir";
while (my $iniFile = readdir(INIDIR)) {
next unless ($iniFile =~ /\.ini$/); #skip non .ini files
push(@iniFiles, $iniFile);
$iniIndex ++;
print "\t$iniIndex: \t $iniFile\n";
}
closedir(INIDIR);
return(@iniFiles);
}
### Create config file ###
sub createConfig {
my $iniFile = $_[0];
my $outputDir = $_[1];
my @fastqDirs = @{$_[2]};
my @bamDirs = @{$_[3]};
my $vcfFile = $_[4];
my $mail = $_[5];
my $run = $_[6];
my $configFile = $outputDir."/settings.config";
if(! -e $outputDir){
make_path($outputDir) or die "Couldn't create directory: $outputDir\n";
}
# Create settings.config file in outputDir
open CONFIG, ">$configFile" or die "cannot open file $configFile \n";
print CONFIG "### SETTINGS ###\n";
print CONFIG "INIFILE\t$iniFile\n";
print CONFIG "OUTPUT_DIR\t$outputDir\n";
print CONFIG "MAIL\t$mail\n";
if(@fastqDirs){
print CONFIG "\n### FASTQ FILES ###\n";
#Find fastq files for each rawDataDir
foreach my $fastqDir (@fastqDirs){
if(! -e $fastqDir) { die "$fastqDir does not exist." }
print CONFIG "# $fastqDir\n";
my $query_one = $fastqDir."/*.fastq.gz "; # look in fastq folder
my $query_two = $fastqDir."/*/*.fastq.gz "; # look one folder deep
my $query_three = $fastqDir."/*/*/*.fastq.gz "; # look two folders deep
my @fastqFiles = glob($query_one.$query_two.$query_three);
foreach my $fastqFile (@fastqFiles){ print CONFIG "FASTQ\t$fastqFile\n" }
}
}
if(@bamDirs){
print CONFIG "\n### BAM FILES###\n";
foreach my $bamDir (@bamDirs){
if(! -e $bamDir) { die "$bamDir does not exist." }
print CONFIG "# $bamDir\n";
my $query_one = $bamDir."/*.bam "; # look in bam folder
my $query_two = $bamDir."/*/*.bam "; # look one folder deep
my @bamFiles = glob($query_one.$query_two);
foreach my $bamFile (@bamFiles){
my $baiFile = $bamFile;
$baiFile =~ s/\.bam/.bai/;
if (! (-e $baiFile || -e "$bamFile.bai")) { die "ERROR: $bamFile.bai or $baiFile does not exist please create an index for $bamFile" }
print CONFIG "BAM\t$bamFile\n"
}
}
}
if($vcfFile){
print CONFIG "\n### VCF FILE###\n";
if(! -e $vcfFile) { die "$vcfFile does not exist." }
print CONFIG "VCF\t$vcfFile\n"
}
close CONFIG;
###Run pipeline if -run is specified
if($run) {
### run pipeline
my $pipeline = dirname(abs_path($0))."/illumina_pipeline.pl";
system "perl $pipeline $configFile";
}
}
### Help information ###
sub usage{
print "Usage: perl illumina_createConfig.pl\n\n";
print "Advanced usage: \n";
print "illumina_createConfig.pl (-i|-iniFile settings.ini OR -ip|-iniPath /path/to/settings.ini) -o|-outputDir /path/to/outputDir (-f|-fastqDir /fastqFolder OR -b|-bamDir /bamFolder OR -v|-vcfFile vcfFile.vcf) -m|-mail example\@mail.nl [-run]\n\n";
print "Available ini files: (use -i)\n";
getIniFiles($settingsDir);
exit;
}
### Interactive mode ###
sub interactive{
print "Using interactive mode \n";
print "Avaible setting files:\n";
my @iniFiles = getIniFiles($settingsDir);
# Settings file
print "Choose setting file [index]: ";
chomp(my $iniIndex = <STDIN>);
if ($iniIndex eq "" || ! $iniFiles[$iniIndex] ) { die "Please provide a correct ini index number." }
my $iniFile = $settingsDir ."/". $iniFiles[$iniIndex];
#Output dir
print "Output dir: ";
chomp($outputDir = <STDIN>); # no tab completion
if ($outputDir eq "") { die "Please provide a correct output directory." }
#Raw data dir -> add while loop to allow for multiple raw data dirs.
print "Raw data project dir: ";
chomp(my $rawDataDir = <STDIN>); # no tab completion
if($rawDataDir eq "") { die "Please provide a correct raw data directory." } #check for existence
push(@fastqDirs, $rawDataDir);
#Output dir
print "Mail address: ";
chomp($mail = <STDIN>);
if ($mail eq "") { die "Please provide a correct mail address." }
#Create config
createConfig($iniFile,$outputDir,\@fastqDirs,$mail,$run);
}