forked from thegenemyers/DAZZ_DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBstats.c
346 lines (295 loc) · 9 KB
/
DBstats.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*******************************************************************************************
*
* Display statistics about the contents of a .db and a histogram of its read lengths.
*
* Author: Gene Myers
* Date : July 2013
* Mod : April 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "DB.h"
static char *Usage = " [-nu] [-b<int(1000)>] [-m<track>]+ <name:db|dam>";
int main(int argc, char *argv[])
{ HITS_DB _db, *db = &_db;
int dam;
int64 ototal;
int oreads;
int nbin, *hist;
int64 *bsum;
int NONE;
int TRIM;
int BIN;
int MMAX, MTOP;
char **MASK;
{ int i, j, k;
int flags[128];
char *eptr;
ARG_INIT("DBstats")
BIN = 1000;
MTOP = 0;
MMAX = 10;
MASK = (char **) Malloc(MMAX*sizeof(char *),"Allocating mask track array");
if (MASK == NULL)
exit (1);
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("nu")
break;
case 'b':
ARG_POSITIVE(BIN,"Bin size")
break;
case 'm':
if (MTOP >= MMAX)
{ MMAX = 1.2*MTOP + 10;
MASK = (char **) Realloc(MASK,MMAX*sizeof(char *),"Reallocating mask track array");
if (MASK == NULL)
exit (1);
}
MASK[MTOP++] = argv[i]+2;
break;
}
else
argv[j++] = argv[i];
argc = j;
NONE = flags['n'];
TRIM = 1-flags['u'];
if (argc != 2)
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
exit (1);
}
}
{ int i, status, kind;
// Open .db or .dam
status = Open_DB(argv[1],db);
if (status < 0)
exit (1);
dam = status;
// Check tracks and load tracks for untrimmed DB
for (i = 0; i < MTOP; i++)
{ status = Check_Track(db,MASK[i],&kind);
if (status == -2)
fprintf(stderr,"%s: Warning: -m%s option given but no track found.\n",Prog_Name,MASK[i]);
else if (status == -1)
fprintf(stderr,"%s: Warning: %s track not sync'd with db.\n",Prog_Name,MASK[i]);
else if (kind != MASK_TRACK)
fprintf(stderr,"%s: Warning: %s track is not a mask track.\n",Prog_Name,MASK[i]);
else if (status == 0)
Load_Track(db,MASK[i]);
else if (status == 1 && !TRIM)
fprintf(stderr,"%s: Warning: %s track is for a trimmed db but -u is set.\n",
Prog_Name,MASK[i]);
}
oreads = db->nreads;
ototal = db->totlen;
if (TRIM)
{ Trim_DB(db);
// Load tracks for trimmed DB
for (i = 0; i < MTOP; i++)
{ status = Check_Track(db,MASK[i],&kind);
if (status < 0)
continue;
else if (status == 1)
Load_Track(db,MASK[i]);
}
}
}
{ int i;
int64 totlen;
int nreads, maxlen;
HITS_READ *reads;
nreads = db->nreads;
totlen = db->totlen;
maxlen = db->maxlen;
reads = db->reads;
nbin = maxlen/BIN + 1;
hist = (int *) Malloc(sizeof(int)*nbin,"Allocating histograms");
bsum = (int64 *) Malloc(sizeof(int64)*nbin,"Allocating histograms");
if (hist == NULL || bsum == NULL)
exit (1);
for (i = 0; i < nbin; i++)
{ hist[i] = 0;
bsum[i] = 0;
}
for (i = 0; i < nreads; i++)
{ int rlen = reads[i].rlen;
hist[rlen/BIN] += 1;
bsum[rlen/BIN] += rlen;
}
if (dam)
printf("\nStatistics for all contigs");
else if (db->all || !TRIM)
printf("\nStatistics for all wells");
else
printf("\nStatistics for all reads");
if (TRIM && db->cutoff > 0)
{ printf(" of length ");
Print_Number(db->cutoff,0,stdout);
printf(" bases or more\n\n");
}
else if (dam)
printf(" in the map index\n\n");
else
printf(" in the data set\n\n");
Print_Number((int64) nreads,15,stdout);
if (dam)
printf(" contigs");
else
printf(" reads ");
if (TRIM)
{ printf(" out of ");
Print_Number((int64 ) oreads,15,stdout);
if (oreads <= 0)
printf(" (100.0%%)");
else
printf(" (%5.1f%%)",(100.*nreads)/oreads);
}
printf("\n");
Print_Number(totlen,15,stdout);
printf(" base pairs");
if (TRIM)
{ printf(" out of ");
Print_Number(ototal,15,stdout);
if (ototal <= 0)
printf(" (100.0%%)");
else
printf(" (%5.1f%%)",(100.*totlen)/ototal);
}
printf("\n\n");
if (nreads > 0)
{ int64 ave, dev;
ave = totlen/nreads;
Print_Number(ave,15,stdout);
if (dam)
printf(" average contig length\n");
else
{ printf(" average read length\n");
dev = 0;
for (i = 0; i < nreads; i++)
{ int rlen = reads[i].rlen;
dev += (rlen-ave)*(rlen-ave);
}
dev = (int64) sqrt((1.*dev)/nreads);
Print_Number(dev,15,stdout);
printf(" standard deviation\n");
}
}
if (totlen <= 0)
{ free(hist);
free(bsum);
Close_DB(db);
exit (0);
}
printf("\n Base composition: %.3f(A) %.3f(C) %.3f(G) %.3f(T)\n",
db->freq[0],db->freq[1],db->freq[2],db->freq[3]);
if (!NONE)
{ int64 btot;
int cum, skip, avg;
printf("\n Distribution of Read Lengths (Bin size = ");
Print_Number((int64) BIN,0,stdout);
printf(")\n\n Bin: Count %% Reads %% Bases Average\n");
if (dam)
skip = 0;
else
skip = -1;
cum = 0;
btot = 0;
for (i = nbin-1; i >= 0; i--)
{ cum += hist[i];
btot += bsum[i];
if (hist[i] != skip)
{ Print_Number((int64) (i*BIN),11,stdout);
printf(":");
Print_Number((int64) hist[i],11,stdout);
if (cum > 0)
avg = btot/cum;
else
avg = 0;
printf(" %5.1f %5.1f %9d\n",(100.*cum)/nreads,(100.*btot)/totlen,avg);
}
if (cum == nreads) break;
}
}
}
{ int64 totlen;
int numint, maxlen;
HITS_TRACK *track;
for (track = db->tracks; track != NULL; track = track->next)
{ char *data = track->data;
int64 *anno = (int64 *) track->anno;
int *idata, *edata;
int64 ave, dev, btot;
int k, rlen, cum;
totlen = 0;
numint = 0;
maxlen = 0;
for (k = 0; k < db->nreads; k++)
{ edata = (int *) (data + anno[k+1]);
for (idata = (int *) (data + anno[k]); idata < edata; idata += 2)
{ rlen = idata[1] - *idata;
numint += 1;
totlen += rlen;
if (rlen > maxlen)
maxlen = rlen;
}
}
printf("\n\nStatistics for %s-track\n",track->name);
printf("\n There are ");
Print_Number(numint,0,stdout);
printf(" intervals totaling ");
Print_Number(totlen,0,stdout);
printf(" bases (%.1f%% of all data)\n",(100.*totlen)/db->totlen);
if (numint <= 0) continue;
nbin = maxlen/BIN + 1;
for (k = 0; k < nbin; k++)
{ hist[k] = 0;
bsum[k] = 0;
}
ave = totlen/numint;
dev = 0;
for (k = 0; k < db->nreads; k++)
{ edata = (int *) (data + anno[k+1]);
for (idata = (int *) (data + anno[k]); idata < edata; idata += 2)
{ rlen = idata[1] - *idata;
dev += (rlen-ave)*(rlen-ave);
hist[rlen/BIN] += 1;
bsum[rlen/BIN] += rlen;
}
}
dev = (int64) sqrt((1.*dev)/numint);
printf("\n");
Print_Number(ave,15,stdout);
printf(" average interval length\n");
Print_Number(dev,15,stdout);
printf(" standard deviation\n");
printf("\n Distribution of %s intervals (Bin size = ",track->name);
Print_Number((int64) BIN,0,stdout);
printf(")\n\n Bin: Count %% Intervals %% Bases Average\n");
cum = 0;
btot = 0;
for (k = nbin-1; k >= 0; k--)
{ cum += hist[k];
btot += bsum[k];
if (hist[k] > 0)
{ Print_Number((int64) (k*BIN),11,stdout);
printf(":");
Print_Number((int64) hist[k],11,stdout);
printf(" %5.1f %5.1f %9lld\n",(100.*cum)/numint,
(100.*btot)/totlen,btot/cum);
if (cum == numint) break;
}
}
printf("\n");
}
}
free(hist);
free(bsum);
Close_DB(db);
exit (0);
}