-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructureoutput2sa
More file actions
executable file
·171 lines (132 loc) · 3.05 KB
/
Copy pathstructureoutput2sa
File metadata and controls
executable file
·171 lines (132 loc) · 3.05 KB
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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use fralib;
use File::Basename;
use POSIX qw(ceil floor);
use File::Path;
use Pod::Usage;
=head1 NAME
structureoutput2sa
=head1 SYNOPSIS
structureoutput2sa [options] structure-output-file
-h help
structure-output-file structure output file containing membership coefficients
example: structureoutput2sa pscalare_f
Extracts the cluster membership coefficients of each sample from a structure output file.
=head1 DESCRIPTION
=cut
#option variables
my $help;
my $structureOutputFile;
my $saFile;
my $dataFound = 0;
my $K;
my $confidenceIntervalsPresent = 0;
my $confidence;
#initialize options
Getopt::Long::Configure ('bundling');
if(!GetOptions('h'=>\$help)
|| scalar(@ARGV)!=1)
{
if ($help)
{
pod2usage(-verbose => 2);
}
else
{
pod2usage(1);
}
}
$structureOutputFile = $ARGV[0];
open(STRUCTURE_OUTPUT, $structureOutputFile) || die "Cannot open $structureOutputFile";
undef($/);
$_ = <STRUCTURE_OUTPUT>;
if (/ANCESTDIST=1/)
{
$confidenceIntervalsPresent = 1;
if (/ANCESTPINT=([\d]+.[\d]+)/)
{
$confidence = (100*$1);
}
}
seek(STRUCTURE_OUTPUT, 0, 0);
$/="\n";
while(<STRUCTURE_OUTPUT>)
{
s/\r?\n?$//;
if (/(\d+) populations assumed/)
{
$K = $1;
}
elsif (/Label\s+\(%Miss\)\s+Pop:\s+Inferred clusters/)
{
$dataFound = 1;
if (!defined($saFile))
{
my ($name, $dir, $ext) = fileparse($structureOutputFile, '_f');
$saFile = "$name.sa";
}
open (SA, ">$saFile") || die "Cannot open $saFile";
print SA "sample-id";
for my $clusterNo (1 .. $K)
{
if ($confidenceIntervalsPresent)
{
print SA "\tcluster-$clusterNo\tcluster-$clusterNo-L$confidence\tcluster-$clusterNo-H$confidence";
}
else
{
print SA "\tcluster-$clusterNo";
}
}
print SA "\n";
while(<STRUCTURE_OUTPUT>)
{
s/\r?\n?$//;
if (!/^$/)
{
if ($confidenceIntervalsPresent)
{
my @fields = split(/\s+/, trim($_));
if ($#fields-4 != 2*$K)
{
die "Number of assumed populations wrongly detected: $K vs " . ($#fields-4);
}
print SA $fields[1];
for my $col (5 .. $#fields-$K)
{
$fields[$col+$K]=~/\(([01]\.\d+),([01]\.\d+)\)/;
print SA "\t$fields[$col]\t$1\t$2";
}
print SA "\n";
}
else
{
my @fields = split(/\s+/, trim($_));
if ($#fields-4 != $K)
{
die "Number of assumed populations wrongly detected: $K vs " . ($#fields-4);
}
print SA $fields[1];
for my $col (5 .. $#fields)
{
print SA "\t$fields[$col]";
}
print SA "\n";
}
}
else
{
last;
}
}
close(SA);
}
}
if (!$dataFound)
{
warn "Can't find individual cluster membership coefficients in $structureOutputFile";
}
close(STRUCTURE_OUTPUT);