-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_stats.c
executable file
·195 lines (163 loc) · 4.59 KB
/
sample_stats.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double nucdiv(int, int, char **);
double tajd(int, int, double) ;
double hfay(int, int, char **);
double thetah(int, int, char **);
int maxsites = 1000 ;
main(argc,argv)
int argc;
char *argv[];
{
int nsam, j ,nsites, i, howmany ;
char **list, **cmatrix(), allele,na, line[1001], slashline[1001] ;
FILE *pf, *fopen(), *pfin ;
double *posit ;
int segsites, count , nadv, probflag ;
double pi , h, th ,prob ;
char dum[20], astr[100] ;
int nsegsub, segsub( int nsam, int segsites, char **list ) ;
/* read in first two lines of output (parameters and seed) */
pfin = stdin ;
fgets( line, 1000, pfin);
sscanf(line," %s %d %d", dum, &nsam, &howmany);
fgets( line, 1000, pfin);
if( argc > 1 ) {
nadv = atoi( argv[1] ) ;
}
list = cmatrix(nsam,maxsites+1);
posit = (double *)malloc( maxsites*sizeof( double ) ) ;
count=0;
probflag = 0 ;
while( howmany-count++ ) {
/* read in a sample */
do {
if( fgets( line, 1000, pfin) == NULL ){
exit(0);
}
if( line[0] == '/' ) strcpy(slashline,line+2);
}while ( (line[0] != 's') && (line[0] != 'p' ) ) ;
if( line[0] == 'p'){
sscanf( line, " prob: %lf", &prob );
probflag = 1 ;
if( fgets( line, 1000, pfin) == NULL ){
exit(0);
}
}
sscanf( line, " segsites: %d", &segsites );
if( segsites >= maxsites){
maxsites = segsites + 10 ;
posit = (double *)realloc( posit, maxsites*sizeof( double) ) ;
biggerlist(nsam,maxsites, list) ;
}
if( segsites > 0) {
fscanf(pfin," %s", astr);
for( i=0; i<segsites ; i++) fscanf(pfin," %lf",posit+i) ;
for( i=0; i<nsam;i++) fscanf(pfin," %s", list[i] );
}
/* analyse sample ( do stuff with segsites and list) */
if( argc > 1 ) nsegsub = segsub( nadv, segsites, list) ;
pi = nucdiv(nsam, segsites, list) ;
h = hfay(nsam, segsites, list) ;
th = thetah(nsam, segsites, list) ;
if( argc > 1 )
printf("pi: %lf ss: %d D: %lf H: %lf thetah: %lf segsub: %d \n", pi,segsites, tajd(nsam,segsites,pi) , h , th, nsegsub ) ;
else if( probflag == 1 )
printf("pi:\t%lf\tss:\t%d\tD:\t%lf\tthetaH:\t%lf\tH:\t%lf\tprob:\t%g%s",
pi,segsites, tajd(nsam,segsites,pi) , th , h, prob , slashline ) ;
else
printf("pi:\t%lf\tss:\t%d\tD:\t%lf\tthetaH:\t%lf\tH:\t%lf%s", pi,segsites, tajd(nsam,segsites,pi) , th , h,slashline ) ;
}
exit (EXIT_SUCCESS);
}
/* allocates space for gametes (character strings) */
char **
cmatrix(nsam,len)
int nsam, len;
{
int i;
char **m;
if( ! ( m = (char **) malloc( (unsigned)( nsam*sizeof( char* )) ) ) )
perror("alloc error in cmatrix") ;
for( i=0; i<nsam; i++) {
if( ! ( m[i] = (char *) malloc( (unsigned) (len*sizeof( char )) )))
perror("alloc error in cmatric. 2");
}
return( m );
}
int
biggerlist(nsam, nmax, list )
int nsam ;
unsigned nmax ;
char ** list ;
{
int i;
maxsites = nmax ;
for( i=0; i<nsam; i++){
list[i] = (char *)realloc( list[i],maxsites*sizeof(char) ) ;
if( list[i] == NULL ) perror( "realloc error. bigger");
}
}
double
nucdiv( int nsam, int segsites, char **list)
{
int s, frequency( char, int, int, char**);
double pi, p1, nd, nnm1 ;
pi = 0.0 ;
nd = nsam;
nnm1 = nd/(nd-1.0) ;
for( s = 0; s <segsites; s++){
p1 = frequency('1', s,nsam,list)/nd ;
pi += 2.0*p1*(1.0 -p1)*nnm1 ;
}
return( pi ) ;
}
/* thetah - pi */
double
hfay( int nsam, int segsites, char **list)
{
int s, frequency( char, int, int, char**);
double pi, p1, nd, nnm1 ;
pi = 0.0 ;
nd = nsam;
nnm1 = nd/(nd-1.0) ;
for( s = 0; s <segsites; s++){
p1 = frequency('1', s,nsam,list)/nd ;
pi += 2.0*p1*(2.*p1 - 1.0 )*nnm1 ;
}
return( -pi ) ;
}
/* Fay's theta_H */
double
thetah( int nsam, int segsites, char **list)
{
int s, frequency( char, int, int, char**);
double pi, p1, nd, nnm1 ;
pi = 0.0 ;
nd = nsam;
nnm1 = nd/(nd-1.0) ;
for( s = 0; s <segsites; s++){
p1 = frequency('1', s,nsam,list) ;
pi += p1*p1 ;
}
return( pi*2.0/( nd*(nd-1.0) ) ) ;
}
int
frequency( char allele,int site,int nsam, char **list)
{
int i, count=0;
for( i=0; i<nsam; i++) count += ( list[i][site] == allele ? 1: 0 ) ;
return( count);
}
int
segsub( int nsub, int segsites, char **list )
{
int i, count = 0 , c1 ;
int frequency( char, int, int, char**) ;
for(i=0; i < segsites ; i++){
c1 = frequency('1',i,nsub, list);
if( ( c1 > 0 ) && ( c1 <nsub ) ) count++;
}
return( count ) ;
}