-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvcfparse.pl
126 lines (107 loc) · 2.2 KB
/
vcfparse.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
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
# vcfparse.pl
# program to extract the first 8 columns from a vcf for the IC.pl program
# and optionally gzip the output
# Created W.Rayner 2016-2017 wrayner@well.ox.ac.uk
#
# version 1.0 created
# version 1.1 added option for gzip on output and command line options
#
#
#
#
my $folder = '.';
my $gzip = 0;
my $stem;
GetOptions
(
"d|directory=s" => \$folder, # top level directory for the imputed data files
"g|gzip" => \$gzip,
"o|output=s" => \$stem # Set path to an output directory, will be created if it doesn't exist
);
print "Options:\n";
print "Input Folder: $folder\n";
print "Output Folder: $stem\n";
if (-e $stem)
{
print "Output folder $stem already exists\n";
}
else
{
mkdir $stem;
print "Creating Output folder $stem\n";
}
#find vcfs
my @filelisting = find_all_files($folder);
my $total = @filelisting;
print "Found $total files in $folder\n";
foreach my $file (@filelisting)
{
my $toprocess;
if ($file =~ /(.*\.vcf)\.gz$/)
{
$toprocess = $1;
}
elsif ($file =~ /(.*\.vcf)$/)
{
$toprocess = $file;
}
else
{
}
if ($toprocess)
{
my $cmd;
my($filename, $dirs, $suffix) = fileparse($toprocess);
my $outfile = $stem.'/'.$filename.'.cut';
if (-e $outfile)
{
print "ERROR: $outfile Exists, skipping\n";
}
else
{
print "Processing $file\n";
if (!$gzip)
{
$cmd = "zgrep -v '##' $file \| cut -f 1-8 \> $outfile";
}
else
{
$outfile = $outfile.'.gz';
$cmd = "zgrep -v '##' $file \| cut -f 1-8 \| gzip \> $outfile"; #gzip output
}
system $cmd;
print "Output in $outfile\n";
}
}
}
sub find_all_files
{
my $path = $_[0];
my @filelist;
# Open the directory.
if (opendir (DIR, $path)) #or die "Unable to open $path: $!";
{
# Read files
my @files = grep { !/^\.{1,2}$/ } readdir (DIR);
# Close the directory.
closedir (DIR);
@files = map { $path . '/' . $_ } @files;
foreach my $file (@files)
{
if (-d $file) # If it is a directory
{
push @filelist, find_all_files($file);
}
else # If it isn't a directory
{
push @filelist, $file;
}
}
}
return @filelist;
}