This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_samples.pl
281 lines (231 loc) · 6.73 KB
/
split_samples.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
#!/usr/bin/perl
use strict;
use File::Spec;
use Getopt::Long;
use IO::File;
use Pod::Usage;
# initialize user-definable parameters to default values
my $dir='';
my $beadstudio_file='';
my $rsOnly="true";
my $ncols=3;
my $samples_to_process='all';
my $xcol = 0;
my $ycol = 1;
my $genocol=2;
# set user defined parameters
GetOptions('data_file=s' => \$beadstudio_file,
'output_directory=s' => \$dir,
'rs_only=s' => \$rsOnly,
'ncols=s' => \$ncols,
'samples=s' => \$samples_to_process,
'xcol=s' => \$xcol,
'ycol=s' => \$ycol,
'genocol=s' => \$genocol
) or pod2usage(1);
my $extract_dir = $dir."/normalization/";
my $extract_geno_dir = $dir."/genotypes/";
mkdir $extract_dir;
mkdir $extract_geno_dir;
$beadstudio_file=File::Spec->canonpath($beadstudio_file);
$extract_dir=File::Spec->canonpath($extract_dir);
if($beadstudio_file ne '') {
unless((-r $beadstudio_file)) {
print STDERR "Error: Cannot read data file: $beadstudio_file\n";
exit(0);
}
}
else {
print STDERR "Error: No file with data specified!\n";
pod2usage(1);
}
unless((-d $dir)) {
print STDERR "Error: Cannot access directory for storing output: " .
"$dir\n";
exit(0);
}
# Main program - Split multiple sample file into one file per sample
# Read header
open(BEADSTUDIO,"$beadstudio_file");
my $beadstudio_error="Error: BeadStudio data in $beadstudio_file " .
"is not in the correct format";
my $header=<BEADSTUDIO>;
#chomp($header);
my @header=split(/\t/,$header,-1);
if($header[0] ne 'Name' || $header[1] ne 'Chr' || $header[2] ne 'Position') {
print STDERR "$beadstudio_error\n";
print STDERR "The first three column headers should be: " .
"Name, Chr, Position\n";
exit(0);
}
#close(BEADSTUDIO);
my @samples;
my $beadstudio_format='normalization';
my $ndup=0;
for(my $i=3;$i<scalar(@header);$i+=$ncols) {
if($header[($i+$xcol)]!~/\.X/ && $header[($i+$xcol)]!~/\.B Allele Freq/) {
print STDERR "$beadstudio_error\n";
print STDERR "Column " . ($i+$xcol+1) . " header, $header[($i+$xcol)]," .
" should be <sample>.X or <sample>.B Allele Freq\n";
exit(0);
}
if($header[$i]=~/\.B Allele Freq/) {
$beadstudio_format='segmentation';
}
my $sample_name=$header[($i+$xcol)];
$sample_name=~s/\.X//;
$sample_name=~s/\.B Allele Freq//;
my $found = 0;
foreach my $s (@samples){
if($sample_name eq $s){
$found=1;
$ndup++;
}
}
if($found>0){
print "\t\tWARNING : sample name ".$sample_name." is duplicated\n";
$sample_name = $sample_name."duplicated_".$ndup;
}
push(@samples,$sample_name);
}
my $name="sample_names.txt";
my $filename=File::Spec->canonpath("$extract_dir/$name");
my $fh1=IO::File->new(">$filename");
if(!defined($fh1)) {
print STDERR "Error: Cannot open file $filename for writing\n";
exit(0);
}
print $fh1 "Assay\tFilename\tIGV_index\n";
my $igv_index=1;
foreach my $sample (@samples) {
print $fh1 "$sample" .
"\t${sample}_extracted.txt" .
"\t$igv_index" .
"\n";
$igv_index++;
}
close($fh1);
my $start=3;
my $stop=scalar(@header);
my $sampleStart = 0;
my $sampleStop = scalar(@samples)-1;
if($samples_to_process ne 'all'){
my @sp = split(/-/,$samples_to_process);
$start=$sp[0]*$ncols+3;
$stop=$sp[1]*$ncols+3;
$sampleStart=$sp[0];
$sampleStop=$sp[1];
}
print "\t\tProcessing ".($sampleStop-$sampleStart)."/".scalar(@samples)." samples for sample ".$sampleStart." to ".$sampleStop."\n";
my $decimal_error=0;
my $first = 0;
print "\t\tInitializing outputfiles \n";
my @file_handlesXY;
my @file_handlesGeno;
my $i=0;
foreach my $sample (@samples) {
if($i >= $sampleStart & $i < $sampleStop){
my $filename=File::Spec->canonpath("$extract_dir/${sample}_extracted.txt");
my $fh=IO::File->new(">$filename");
if(!defined($fh)) {
print $sample."\n";
print STDERR "Error: Cannot open file $filename for writing\n";
exit(0);
}
my $out_header="Name\tChr\tPosition\tX\tY";
print $fh "$out_header\n";
push(@file_handlesXY,$fh);
$filename = File::Spec->canonpath("$extract_geno_dir/${sample}_extracted.txt");
$fh=IO::File->new(">$filename");
if(!defined($fh)) {
print $sample."\n";
print STDERR "Error: Cannot open file $filename for writing\n";
exit(0);
}
$out_header="Name\tChr\tPosition\tGType";
print $fh "$out_header\n";
push(@file_handlesGeno,$fh);
}
$i++;
}
print "\t\tProcessing data \n";
my $k=0;
my $step = 1;
my $nok=0;
while(my $lin = <BEADSTUDIO>) {
my $snp_ok = 'nok';
my $snp;
my $i = 1;
if($rsOnly eq "true"){
if(substr($lin,0,2) eq "rs"){
$snp_ok='ok';
}
}
else{
$snp_ok='ok';
}
if($snp_ok eq 'ok'){
$nok++;
chomp($lin);
my @line=split(/\t/,$lin);
my $snp = $line[0];
if(scalar(@header) != scalar(@line)) {
print STDERR "$beadstudio_error\n";
print STDERR "Not the same number of columns in all rows\n";
exit(0);
}
my $fh_index = 0;
for(my $i=$start;$i<$stop;$i+=$ncols) {
my $fh=$file_handlesXY[$fh_index];
my $data_line="$line[($i+$xcol)]\t$line[($i+$ycol)]";
if($data_line =~ /,/) {
$decimal_error=1;
$data_line =~ s/,/./g;
}
print $fh "$line[0]\t$line[1]\t$line[2]\t$data_line\n";
$fh=$file_handlesGeno[$fh_index];
my $geno=$line[($i+$genocol)];
print $fh "$line[0]\t$line[1]\t$line[2]\t$geno\n";
$fh_index++;
}
}
$k++;
if($k == $step*10000){
print "\t\t\t".$k." lines processed \n";
$step++;
}
}
close(BEADSTUDIO);
print "\t\t=>".$nok." SNPs retained\n";
foreach my $fh (@file_handlesXY) {
close($fh);
}
foreach my $fh (@file_handlesGeno) {
close($fh);
}
if($decimal_error) {
print "Warning: commas (,) were found in you BeadStudio data columns. " .
"These have been interpreted as decimal points and " .
"have been replaced with points (.) in the generated files. " .
"BAFsegmentation and tQN requires decimal points to be points (.) and not commas (,).\n"
}
# Documentation
=pod
=head1 NAME
split_samples.pl - split multiple samples in a data file into separate files for use with BAFsegmentation or tQN
=head1 SYNOPSIS
split_samples.pl --data_file=<name> [--output_directory=<name>] [--prefix=<name>]
=head1 OPTIONS
=over 8
=item B<--data_file=<name>>
Specify a file with data from BeadStudio.
=item B<--output_directory=<name>>
Specify a directory to store the generated files in. Default is "extracted".
=item B<--prefix=<name>>
Specify a prefix for the generated file with sample names: <prefix>_sample_names.txt. Default is to use no prefix.
=back
=head1 AUTHORS
Markus Ringner
Please report bugs to [email protected]
=cut
# End of documentation