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 pathjoin_cluster_and_samples_files.pl
155 lines (147 loc) · 3.58 KB
/
join_cluster_and_samples_files.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
#!/usr/bin/perl
use strict;
use Pod::Usage;
use Getopt::Long;
# initialize user-definable parameters to default values
my $file_list_clusters='';
my $file_list_samples='';
my $out_file_clusters = '';
my $out_file_samples = '';
# set user defined parameters
GetOptions('file_list_clusters=s' => \$file_list_clusters,
'file_list_samples=s' => \$file_list_samples,
'out_file_clusters=s' => \$out_file_clusters,
'out_file_samples=s' => \$out_file_samples
) or pod2usage(1);
#print "######### MERGING CLUSTER FILES ##########\n\n";
my @files;
open(FILES,$file_list_clusters) || die("cannot open file ".$file_list_clusters);
while(my $line=<FILES>){
$line =~ s/\n//g;
push @files, $line;
}
close(FILES);
my $k=0;
my $header;
my %AAT=();
my %ABT=();
my %BBT=();
my %AAR=();
my %ABR=();
my %BBR=();
my %AAn=();
my %ABn=();
my %BBn=();
foreach my $f(@files){
open(FHI,$f) || die("cannot open file ".$f);
$header = <FHI>;
while(my $line=<FHI>){
chomp($line);
my @elts = split(/\t/,$line);
if($k==0){
$AAT{$elts[0]} = 0;
$ABT{$elts[0]} = 0;
$BBT{$elts[0]} = 0;
$AAR{$elts[0]} = 0;
$ABR{$elts[0]} = 0;
$BBR{$elts[0]} = 0;
$AAn{$elts[0]} = 0;
$ABn{$elts[0]} = 0;
$BBn{$elts[0]} = 0;
}
if($elts[1] ne 'NA'){
$AAT{$elts[0]} = $AAT{$elts[0]} + $elts[1];
$AAn{$elts[0]} = $AAn{$elts[0]} + 1;
}
if($elts[2] ne 'NA'){
$ABT{$elts[0]} = $ABT{$elts[0]} + $elts[2];
$ABn{$elts[0]} = $ABn{$elts[0]} + 1;
}
if($elts[3] ne 'NA'){
$BBT{$elts[0]} = $BBT{$elts[0]} + $elts[3];
$BBn{$elts[0]} = $BBn{$elts[0]} + 1;
}
if($elts[4] ne 'NA'){
$AAR{$elts[0]} = $AAR{$elts[0]} + $elts[4];
}
if($elts[5] ne 'NA'){
$ABR{$elts[0]} = $ABR{$elts[0]} + $elts[5];
}
if($elts[6] ne 'NA'){
$BBR{$elts[0]} = $BBR{$elts[0]} + $elts[6];
}
}
$k++;
#print $k."/".scalar(@files)." processed\n";
close(FHI);
}
#print "Writing results\n";
open(CLUST,">".$out_file_clusters) || die("cannot open file ".$out_file_clusters);
print CLUST $header;
foreach my $snp (keys(%AAT)){
print CLUST $snp."\t";
if($AAn{$snp} <= 0){
print CLUST "NA\t";
}
else{
print CLUST $AAT{$snp}/$AAn{$snp}."\t";
}
if($ABn{$snp} <= 0){
print CLUST "NA\t";
}
else{
print CLUST $ABT{$snp}/$ABn{$snp}."\t";
}
if($BBn{$snp} <= 0){
print CLUST "NA\t";
}
else{
print CLUST $BBT{$snp}/$BBn{$snp}."\t";
}
if($AAn{$snp} <= 0){
print CLUST "NA\t";
}
else{
print CLUST $AAR{$snp}/$AAn{$snp}."\t";
}
if($ABn{$snp} <= 0){
print CLUST "NA\t";
}
else{
print CLUST $ABR{$snp}/$ABn{$snp}."\t";
}
if($BBn{$snp} <= 0){
print CLUST "NA\n";
}
else{
print CLUST $BBR{$snp}/$BBn{$snp}."\n";
}
}
close(FHO);
if($file_list_samples ne ''){
print "######### MERGING SAMPLE FILES ##########\n\n";
@files=();
open(FILES,$file_list_samples) || die("cannot open file ".$file_list_samples);
while(my $line=<FILES>){
$line =~ s/\n//g;
push @files, $line;
}
close(FILES);
my $index = 1;
open(FHI,$files[0])|| die("cannot open file ".$files[0]);
$header = <FHI>;
close(FHI);
open(SAMPLES,">".$out_file_samples) || die("cannot open file ".$out_file_samples);
print SAMPLES $header;
foreach my $f(@files){
open(FHI,$f) || die("cannot open file ".$f);
my $line = <FHI>;
while($line=<FHI>){
my @elts = split(/\t/,$line);
print SAMPLES $elts[0]."\t".$elts[1]."\t".$index."\n";
}
$index++;
close(FHI);
}
close(SAMPLES);
}