forked from richarddurbin/pbwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbwtPaint.c
331 lines (296 loc) · 12.5 KB
/
pbwtPaint.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* File: pbwtPaint.c
* Author: Richard Durbin ([email protected]) and Daniel Lawson ([email protected])
* Copyright (C) Genome Research Limited, 2013-
*-------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------
* Description: tools for chromosome painting as in ChromoPainter, FineStructure etc.
* Exported functions:
* HISTORY:
* Last edited: Jul 14 8:33:00 2019 (djl)
* Created: Tue Apr 1 11:34:41 2014 (rd)
*-------------------------------------------------------------------
*/
//#define LEGACY_MATRIX
#include "zlib.h"
#include "pbwt.h"
//#include "dynamiclist.h"
typedef struct { int j ; int start ; int end ; } MatchSegment ;
/* matches are semi-open [start,end) so length is end-start */
static Array *maxMatch = 0 ; /* arrays of MatchSegment */
static void reportMatch (int i, int j, int start, int end)
{ MatchSegment *ms = arrayp (maxMatch[i], arrayMax(maxMatch[i]), MatchSegment) ;
ms->j = j ; ms->start = start ; ms->end = end ;
}
static inline int isprevind(int i,int *map_indhap){
if(i==0) return(0);
return(map_indhap[i]==map_indhap[i-1]);
}
static inline void printAll(int ii,int Ninds,
double *t_counts,double *t_counts2,double *t_counts3,double *t_totlengths,double nregions,
gzFile fc,gzFile fc2,gzFile fc3,gzFile fl,gzFile fr){
int jj ; for (jj = 0 ; jj < Ninds ; ++jj) {
if(t_counts[jj]){
gzprintf (fc, "%i %i %.4f\n", ii+1,jj+1,t_counts[jj]) ;
gzprintf (fl, "%i %i %.4f\n", ii+1,jj+1,t_totlengths[jj]) ;
gzprintf (fc2,"%i %i %.4f\n", ii+1,jj+1,t_counts2[jj]) ;
gzprintf (fc3,"%i %i %.4f\n", ii+1,jj+1,t_counts3[jj]) ;
}
}
gzprintf (fr,"%i %.2f\n",ii+1, nregions) ;
}
void paintAncestryMatrix (PBWT *p, char* fileRoot,int chunksperregion,int ploidy,int outputlocal)
{
int Ninds=p->M/ploidy;
double **totlengths = 0 ;
double **counts = 0 ;
double **counts2 = 0 ; /* store sums of squares of counts in 100 block bins */
double **counts3 = 0 ; /* store sums of counts in 100 block bins */
double *nregions = 0; /* store number of regions found */
/* local ancestry things: */
double **localsum = 0; /* store the total local ancestry weight for the current individual for each snp (if needed)*/
FILE *flp;
int i, j, k ;
int *map_indhap = mycalloc (p->M,int);
for (i = 0 ; i < p->M ; ++i) map_indhap[i]=i/ploidy;
double *totCounts = mycalloc (Ninds, double) ;
nregions = mycalloc (Ninds,double);
counts = myalloc (Ninds, double*) ; counts2 = myalloc (Ninds, double*) ; counts3 = myalloc (Ninds, double*) ; totlengths= myalloc (Ninds, double*);
for (i = 0 ; i < Ninds ; ++i)
{
counts[i] = mycalloc (Ninds, double) ;
counts2[i] = mycalloc (Ninds, double) ;
counts3[i] = mycalloc (Ninds, double) ;
totlengths[i] = mycalloc (Ninds, double) ;
}
memset (nregions, 0, sizeof(double)*Ninds) ;
maxMatch = myalloc (p->M, Array) ;
for (i = 0 ; i < p->M ; ++i) maxMatch[i] = arrayCreate (1024, MatchSegment) ;
matchMaximalWithin (p, reportMatch) ; /* store maximal matches in maxMatch */
double *partCounts = myalloc (Ninds, double) ;
/* now weight per site based on distance from ends */
if(outputlocal) {
flp = fopenTag (fileRoot, "localancestry.out", "w") ;
localsum = myalloc (Ninds, double*) ;
for (i = 0 ; i < Ninds ; ++i) localsum[i] = mycalloc (p->N, double) ;
fprintf (flp,"pos");
for (i = 0 ; i < Ninds ; ++i) fprintf (flp," IND%i",i+1);
fprintf (flp,"\n");
}
for (i = 0 ; i < p->M ; ++i) // i is the SAMPLE NUMBER
{
if(outputlocal) { // initialise the painting probability
for(j=0; j<Ninds;++j) {
for(k=0; k<p->N;++k) localsum[j][k]=0;
}
}
// printf("Processing individual %i (haplotype %i)\n",i/ploidy,i);
MatchSegment *m1 = arrp(maxMatch[i],0,MatchSegment), *m ;
int n1 = 1 ; /* so don't have an empty chunk to start with! */
MatchSegment *mStop = arrp(maxMatch[i], arrayMax(maxMatch[i])-1, MatchSegment) ;
memset (partCounts, 0, sizeof(double)*Ninds) ;
for (k = 1 ; k < p->N ; k++) // k is the SITE NUMBER
{ double sum = 0 ;
while (m1->end <= k && m1 < mStop)
{ if ((n1 % chunksperregion)==0)
{ int jj ; for (jj = 0 ; jj < Ninds ; ++jj) {
if(map_indhap[i]==jj) continue; // skip match to self
counts2[map_indhap[i]][jj] += partCounts[jj]*partCounts[jj] ;
counts3[map_indhap[i]][jj] += partCounts[jj] ;
}
memset (partCounts, 0, sizeof(double)*Ninds) ;
nregions[map_indhap[i]]+=1.0;
}
++m1 ; ++n1 ;
}
for (m = m1 ; m->start < k && m <= mStop ; ++m) if(map_indhap[i]!=map_indhap[m->j]) // skip match to self
sum += (k - m->start) * (m->end - k) ;
if (sum)
for (m = m1 ; m->start < k && m <= mStop ; ++m) {
if(map_indhap[i]==map_indhap[m->j]) continue; // skip match to self
if(outputlocal) localsum[map_indhap[m->j]][k] += (k - m->start) * (m->end - k) / sum;
totlengths[map_indhap[i]][map_indhap[m->j]] += (k - m->start) * (m->end - k) / sum;
double thiscount=(k - m->start) * (m->end - k) / sum/(m->end - m->start);
counts[map_indhap[i]][map_indhap[m->j]] += thiscount;
partCounts[map_indhap[m->j]] += thiscount;
}
}
if(outputlocal) {
fprintf (flp,"HAP %i IND%i\n",i+1,map_indhap[i]+1);
for (k = p->N-1 ; k >=0 ; --k) {
fprintf(flp,"%i",arrayp(p->sites,k,Site)->x);
for(j=0; j<Ninds;++j) fprintf (flp," %0.3f",localsum[j][k]);
fprintf (flp,"\n");
}
}
}
if(outputlocal) fclose(flp);
free(map_indhap);
free (partCounts) ;
// Normalise the chunklengths
for (i = 0 ; i < Ninds ; ++i) {
double indsum=0;
for (j = 0 ; j < Ninds ; ++j) {
indsum+=totlengths[i][j];
}
for (j = 0 ; j < Ninds ; ++j) {
totlengths[i][j]=totlengths[i][j]/indsum * p->N * ploidy;
}
}
/* report results */
FILE *fc = fopenTag (fileRoot, "chunkcounts.out", "w") ;
FILE *fl = fopenTag (fileRoot, "chunklengths.out", "w") ;
FILE *fc2 = fopenTag (fileRoot, "regionsquaredchunkcounts.out", "w") ;
FILE *fc3 = fopenTag (fileRoot, "regionchunkcounts.out", "w") ;
fprintf (fc,"RECIPIENT") ;
fprintf (fl,"RECIPIENT") ;
fprintf (fc2,"RECIPIENT nregions") ;
fprintf (fc3,"RECIPIENT nregions") ;
for (i = 0 ; i < Ninds ; ++i) {
fprintf (fc," IND%i",i+1) ;
fprintf (fl," IND%i",i+1) ;
fprintf (fc2," IND%i",i+1) ;
fprintf (fc3," IND%i",i+1) ;
}
fputc ('\n', fc) ;
fputc ('\n', fl) ;
fputc ('\n', fc2) ;
fputc ('\n', fc3) ;
for (i = 0 ; i < Ninds ; ++i) {
fprintf (fc3,"IND%i %.2f",i+1, nregions[i]) ;
fprintf (fc2,"IND%i %.2f",i+1, nregions[i]) ;
fprintf (fl,"IND%i",i+1) ;
fprintf (fc,"IND%i",i+1) ;
for (j = 0 ; j < Ninds ; ++j)
{
fprintf (fc, " %.4f", counts[i][j]) ;
fprintf (fl, " %.4f", totlengths[i][j]) ;
fprintf (fc2," %.4f", counts2[i][j]) ;
fprintf (fc3," %.4f", counts3[i][j]) ;
totCounts[i] += counts[i][j] ;
}
fputc ('\n', fc) ;
fputc ('\n', fl) ;
fputc ('\n', fc2) ;
fputc ('\n', fc3) ;
if (isCheck && (i%2) && p->samples)
fprintf (logFile, "%s %8.4g %8.4g\n",
sampleName (sample(p,i-1)), totCounts[i-1], totCounts[i]) ;
}
fclose (fc) ; fclose (fl) ; fclose (fc2) ;fclose (fc3) ;
timeUpdate(logFile);
/* clean up */
for (i = 0 ; i < Ninds ; ++i) { free (counts[i]) ; free (counts2[i]) ; free (counts3[i]) ; free (totlengths[i]) ; }
free (counts) ; free (counts2) ; free (counts3) ; free (totCounts) ; free (nregions); free(totlengths);
}
void paintAncestryMatrixSparse (PBWT *p, char* fileRoot,int chunksperregion,int ploidy,int cutoff)
{
int i, j, k ;
int Ninds=p->M/ploidy;
int *map_indhap = mycalloc (p->M,int);
for (i = 0 ; i < p->M ; ++i) map_indhap[i]=i/ploidy;
double *nregions = 0; /* store number of regions found */
nregions = mycalloc (Ninds,double);
// double *totCounts = mycalloc (Ninds, double) ;
gzFile fr = gzopenTag (fileRoot, "nregions.s.out.gz", "w") ;
gzFile fc = gzopenTag (fileRoot, "chunkcounts.s.out.gz", "w") ;
gzFile fl = gzopenTag (fileRoot, "chunklengths.s.out.gz", "w") ;
gzFile fc2 = gzopenTag (fileRoot, "regionsquaredchunkcounts.s.out.gz", "w") ;
gzFile fc3 = gzopenTag (fileRoot, "regionchunkcounts.s.out.gz", "w") ;
/// The dynamic structure storing the sparse matrices
int *t_obs = myalloc (Ninds,int);
double *t_counts = myalloc (Ninds,double);
double *t_counts2 = myalloc (Ninds,double);
double *t_counts3 = myalloc (Ninds,double);
double *t_totlengths = myalloc (Ninds,double);
maxMatch = myalloc (p->M, Array) ;
for (i = 0 ; i < p->M ; ++i) maxMatch[i] = arrayCreate (1024, MatchSegment) ;
matchMaximalWithin (p, reportMatch) ; /* store maximal matches in maxMatch */
double *partCounts = myalloc (Ninds, double) ;
double indsum=0; //sum of the chunklengths to this individual over all individuals (number of snps with matches)
/* now weight per site based on distance from ends */
/// i =1 .. M is each haplotype
for (i = 0 ; i < p->M ; ++i)
{
// printf("Processing Individual %i in haplotype %i\n",i/ploidy,i);
MatchSegment *m1 = arrp(maxMatch[i],0,MatchSegment); // m1 is a SEGMENT that matches at SNP i
MatchSegment *m ;// m is another segment
int n1 = 1 ; // number of chunks found so far. 1 so don't have an empty chunk to start with!
MatchSegment *mStop = arrp(maxMatch[i], arrayMax(maxMatch[i])-1, MatchSegment) ;//
// Clear records if we have a new individual
if(!isprevind(i,map_indhap)){
if(i>0) printAll(map_indhap[i-1],Ninds,t_counts,t_counts2,t_counts3,t_totlengths,nregions[map_indhap[i-1]],fc,fc2,fc3,fl,fr);
memset (t_obs, 0, sizeof(int)*Ninds) ;
memset (partCounts, 0, sizeof(double)*Ninds) ;
memset (t_counts, 0, sizeof(double)*Ninds) ;
memset (t_counts2, 0, sizeof(double)*Ninds) ;
memset (t_counts3, 0, sizeof(double)*Ninds) ;
memset (t_totlengths, 0, sizeof(double)*Ninds) ;
}
for (k = 1 ; k < p->N ; k++)
{ double sum = 0 ;
while (m1->end <= k && m1 < mStop)
{
if ((n1 % chunksperregion)==0) {
int jj ; for (jj = 0 ; jj < Ninds ; ++jj) {
if(map_indhap[i]==jj) continue; // skip match to self
if(partCounts[jj]){
t_counts2[jj] += partCounts[jj]*partCounts[jj] ;
t_counts3[jj] += partCounts[jj] ;
}
}
memset (partCounts, 0, sizeof(double)*Ninds) ;
nregions[map_indhap[i]]+=1.0;
}
++m1 ; ++n1 ;
}
// for every individual who has a sufficiently long match
for (m = m1 ; m->start < k && m <= mStop ; ++m)
if((map_indhap[i]!=map_indhap[m->j])&&(m->end-m->start>cutoff)) // skip match to self
sum += (k - m->start) * (m->end - k) ;
if (sum){
for (m = m1 ; m->start < k && m <= mStop ; ++m) {
if((map_indhap[i]==map_indhap[m->j])||(m->end-m->start<=cutoff)) continue;// skip match to self
double thislengths = (k - m->start) * (m->end - k) / sum;
double thiscount=(k - m->start) * (m->end - k) / sum/(m->end - m->start);
t_obs[map_indhap[m->j]] = 1;
t_totlengths[map_indhap[m->j]] += thislengths;
t_counts[map_indhap[m->j]] += thiscount;
partCounts[map_indhap[m->j]] += thiscount;
// dynamicListSet(usedinds,map_indhap[m->j],1);
}
indsum+=1.0; // update the total number of matches individuals
}
} // end loop over the N loci
if(i%ploidy==1){ // normalise the chunklengths to account for sites with no matches
int jj; for (jj = 0 ; jj < Ninds ; ++jj) {
if(t_totlengths[jj]){
t_totlengths[jj]=t_totlengths[jj]/indsum * p->N * ploidy;
}
}
indsum=0;
}
}// end loop over haplotypes
printAll(map_indhap[p->M-1],Ninds,
t_counts,t_counts2,t_counts3,t_totlengths,nregions[map_indhap[p->M-1]],
fc,fc2,fc3,fl,fr);
/* clean up */
free (t_obs) ;
free (t_counts) ;
free (t_counts2) ;
free (t_counts3) ;
free (t_totlengths) ;
free (partCounts) ;
free (nregions) ;
free(map_indhap);
gzclose (fc) ; gzclose (fl) ; gzclose (fc2) ;gzclose (fc3) ; gzclose(fr);
}
/* end of file */