-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdepthreader.c
More file actions
141 lines (121 loc) · 3.91 KB
/
Copy pathdepthreader.c
File metadata and controls
141 lines (121 loc) · 3.91 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
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include <errno.h>
#include <string.h>
#include <math.h>
/*
Extract summary statistics from samtools depth output
Compile with
gcc -O3 -std=c99 -o depthreader depthreader.c -lz -lm
*/
/* Size of the block of memory to use for reading. */
#define LENGTH 0x1000
#define CUTOFF 10000
void clearBuffer(unsigned char *, int);
/*
Wipe buffer by filling with null bytes
*/
void clearBuffer(unsigned char * buffer, int j) {
for (int i=0; i <= j; i++) {
buffer[i] = '\0';
}
}
int main (int argc, const char** argv)
{
gzFile * file;
long int nLines=0;
int nTabs=0;
long int sumCoverage=0;
long int sumSqCoverage=0;
int maxCoverage=0;
int j=0;
unsigned char field[LENGTH];
long int histogram[CUTOFF+1]; // use this to calculate median
for (size_t i=0; i < (CUTOFF+1); ++i) {
histogram[i] = 0;
}
if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
return 1;
}
file = gzopen (argv[1], "r");
if (! file) {
fprintf (stderr, "gzopen of '%s' failed: %s.\n", argv[1],
strerror (errno));
exit (EXIT_FAILURE);
}
clearBuffer((unsigned char *)field, LENGTH-1);
while (1) {
int err;
int bytes_read;
int fieldValue;
unsigned char buffer[LENGTH];
bytes_read = gzread (file, buffer, LENGTH - 1);
buffer[bytes_read] = '\0';
// examine what's in the buffer
for (int i=0; i < bytes_read; ++i) {
if (nTabs == 2) { // Reached second tab, start recording field contents
field[j] = buffer[i];
j++;
}
if (buffer[i]=='\n') {
nLines += 1;
fieldValue = atoi((char *) field);
// Keep track of max read coverage value seen
if (fieldValue > maxCoverage) {
maxCoverage = fieldValue;
}
// Place value in histogram for median
if (fieldValue >= CUTOFF) {
histogram[CUTOFF] += 1; // collapse all huge values into last bin
}
else {
histogram[fieldValue] += 1;
}
sumCoverage += fieldValue;
sumSqCoverage += fieldValue*fieldValue;
//reset field buffer and tab counter ready for next line
nTabs = 0;
clearBuffer((unsigned char *) field, j);
j = 0;
}
else if (buffer[i]=='\t') {
nTabs += 1;
}
}
if (bytes_read < LENGTH - 1) {
if (gzeof (file)) {
break;
}
else {
const char * error_string;
error_string = gzerror (file, & err);
if (err) {
fprintf (stderr, "Error: %s.\n", error_string);
exit (EXIT_FAILURE);
}
}
}
}
gzclose (file);
// Now find median
double m50 = nLines / 2.0;
long int runningsum = 0;
int median = 0;
for (size_t i=0; i < (CUTOFF+1); ++i) {
runningsum += histogram[i];
if (runningsum > m50) {
median = (int) i; // should be close enough, will be off by 0.5 in the unlikely event the previous bin has exactly m50 reads running total
break;
}
}
printf ("Read total of %ld lines\n", nLines);
printf ("Sum coverage was %ld\n", sumCoverage);
printf ("SumSq coverage was %ld\n", sumSqCoverage);
printf ("Max coverage was %d\n", maxCoverage);
printf (" mean = %f\n", (double)sumCoverage/(double)nLines);
printf (" s.d. = %f\n", sqrt(((double)sumSqCoverage - ((double)sumCoverage*(double)sumCoverage/(double)nLines)) / ((double)nLines - 1)));
printf (" median = %d\n", median);
return 0;
}