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
|
#include <stdio.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <zlib.h>
int count_sequences(char *filename) {
char command[512];
long sequences = 0;
FILE *grep_output;
sprintf(command, "grep -c ^\\> %s", filename);
grep_output = popen(command, "r");
if(grep_output == NULL) {
fprintf(stderr, "Could not execute %s\n", command);
exit(EXIT_FAILURE);
}
fscanf(grep_output, "%ld", &sequences);
pclose(grep_output);
return sequences;
}
void normalize_matrix(double *matrix, long height, long width) {
long x = 0;
long y = 0;
for(x = 0; x < height; x++) {
double row_sum = 0;
for(y = 0; y < (width); y++)
row_sum = row_sum + matrix[width * x + y];
for(y = 0; y < (width); y++)
matrix[width * x + y] = matrix[width * x + y] / row_sum;
}
}
double *load_count_matrix(char *filename, long width, int kmer) {
double *count_matrix = malloc((width)*sizeof(double));
char count_command[512];
long x = 0;
char *line = NULL;
size_t len = 0;
if(count_matrix == NULL) {
fprintf(stderr, "could not allocate enough memory for the count matrix\n");
exit(EXIT_FAILURE);
}
// create out count matrix
sprintf(count_command, "count-kmers -r %d -1 -u %s", kmer, filename);
FILE *count_output = popen(count_command, "r");
if(count_output == NULL) {
fprintf(stderr, "could not execute \"%s\"", count_command);
exit(EXIT_FAILURE);
}
// set first element to zero.
count_matrix[0] = 0;
// get our first line
getline(&line, &len, count_output);
count_matrix[1] = atoi(line);
// iterate over the rest of the lines
for(x = 2; x < width; x++) {
getline(&line, &len, count_output);
count_matrix[x] = atoi(line);
}
free(line);
pclose(count_output);
return count_matrix;
}
double *load_sensing_matrix(char *filename, long height, long width) {
long x = 0;
long y = 0;
gzFile sensing_matrix_fh = NULL;
double *sensing_matrix = malloc(height * width * sizeof(double));
if(sensing_matrix == NULL) {
fprintf(stderr, "Could not allocate memory for the sensing matrix\n");
}
sensing_matrix_fh = gzopen(filename, "r");
if(sensing_matrix_fh == NULL) {
fprintf(stderr, "could not open %s", filename);
exit(EXIT_FAILURE);
}
// read our sensing matrix in
for(x = 0; x < height; x++) {
for (y = 1; y < width; y++) {
char buffer[14];
gzgets(sensing_matrix_fh, buffer, 14);
sensing_matrix[width*x + y] = atof(buffer);
}
}
gzclose(sensing_matrix_fh);
return sensing_matrix;
}
char **load_headers(char *filename, long sequences) {
char command[512];
char *line= NULL;
long x = 0;
FILE *grep_output;
size_t len = 0;
sprintf(command, "grep ^\\> %s", filename);
grep_output = popen(command, "r");
if(grep_output == NULL) {
fprintf(stderr, "Could not execute %s\n", command);
exit(EXIT_FAILURE);
}
char **headers = malloc(sequences * sizeof(char *));
if(headers == NULL) {
fprintf(stderr, "could not allocated enough memory\n");
exit(EXIT_FAILURE);
}
for(x = 0; x < sequences; x++) {
char *header = malloc(256 * sizeof(char));
if(header == NULL) {
fprintf(stderr, "could not allocated enough memory\n");
exit(EXIT_FAILURE);
}
getline(&line, &len, grep_output);
sscanf(line + 1, "%s", header);
headers[x] = header;
}
free(line);
pclose(grep_output);
return headers;
}
|