-
Notifications
You must be signed in to change notification settings - Fork 16
/
chain_io.c
183 lines (172 loc) · 6.28 KB
/
chain_io.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
#include <S.h>
#include "ucsc/common.h"
#include "ucsc/hash.h"
#include "rtracklayer.h"
#define LINEBUF_SIZE 20001
/* hash these chain blocks by target and query name */
typedef struct _ChainBlock {
char *name;
IntPairAE *ranges; /* to become an IRanges */
IntAE *offset; /* starts in the other sequence */
/* rle of spaces and scores */
IntAE *length, *score;
CharAE *rev; /* use CharAE until we have a bitset */
CharAEAE *space;
} ChainBlock;
#define HEADER_SIZE 11
#define DATA_SIZE 3
/* returns an array of ChainBlock pointers */
ChainBlock **read_chain_file(FILE *stream, const char *exclude, int *nblocks) {
/* fgets() a line
if first or after blank, parse header
get names, use hash table to get existing block, ow create one
add score, get offsets for starts
ow parse a record for width, tstart, qstart
*/
char linebuf[LINEBUF_SIZE];
char *header[HEADER_SIZE];
char *data[DATA_SIZE];
int tstart = 0, qstart = 0;
Rboolean new_block = TRUE, excluded = FALSE, trc = FALSE, qrc = FALSE;
ChainBlock *block = NULL, **result = NULL;
struct hash *hash = hashNew(6);
struct hashEl *hash_elements;
int line = 0, i = 0, header_line = 0;
while (fgets(linebuf, LINEBUF_SIZE, stream) != NULL) {
line++;
if (strlen(linebuf) == LINEBUF_SIZE - 1) {
error("line %d is too long", line);
}
if (linebuf[0] == '#') {
continue;
}
if (excluded) {
eraseWhiteSpace(linebuf);
if (!strlen(linebuf)) {
excluded = FALSE;
new_block = TRUE;
}
} else if (new_block) { /* have a header */
void *value;
int matches = chopByChar(linebuf, ' ', header, HEADER_SIZE);
if (matches < HEADER_SIZE)
error("expected %d elements in header, got %d, on line %d", HEADER_SIZE,
matches, line);
new_block = FALSE;
if (exclude && (strstr(header[2], exclude) || strstr(header[7], exclude)))
{
//Rprintf("excluding: [%s -> %s]\n", header[2], header[7]);
excluded = TRUE;
continue;
}
value = hashFindVal(hash, header[2]);
if (!value) { /* new block */
int name_size = strlen(header[2])+1;
block = Salloc(1, ChainBlock);
hashAdd(hash, header[2], block);
block->name = Salloc(name_size, char);
memcpy(block->name, header[2], name_size);
block->ranges = new_IntPairAE(0, 0);
block->offset = new_IntAE(0, 0, 0);
block->length = new_IntAE(0, 0, 0);
block->score = new_IntAE(0, 0, 0);
block->rev = new_CharAE(0);
block->space = new_CharAEAE(0, 0);
} else block = value;
IntAE_insert_at(block->score, IntAE_get_nelt(block->score),
atoi(header[1]));
CharAEAE_append_string(block->space, header[7]);
header_line = line;
trc = strcmp("+", header[4]);
qrc = strcmp("+", header[9]);
CharAE_insert_at(block->rev, CharAE_get_nelt(block->rev),
trc != qrc);
tstart = atoi(header[5]) + 1; /* 0-based -> 1-based */
if (trc)
tstart = atoi(header[3]) - tstart + 2; /* start one too high */
qstart = atoi(header[10]) + 1;
if (qrc)
qstart = atoi(header[8]) - qstart + 2;
} else {
int matches = chopByChar(linebuf, '\t', data, DATA_SIZE), width;
if (matches != 1 && matches != 3)
error("expecting 1 or 3 elements on line %d, got %d", line, matches);
width = atoi(data[0]);
tstart -= (trc ? width : 0);
qstart -= (qrc ? width : 0);
IntPairAE_insert_at(block->ranges, IntPairAE_get_nelt(block->ranges),
tstart, width);
IntAE_insert_at(block->offset, IntAE_get_nelt(block->offset),
tstart - qstart);
if (matches == 3) { /* normal line */
int dt = atoi(data[1]), dq = atoi(data[2]);
int tchange, qchange;
if (trc) /* width already subtracted above */
tchange = -dt;
else tchange = width + dt;
tstart += tchange;
if (qrc)
qchange = -dq;
else qchange = width + dq;
qstart += qchange;
} else {
new_block = TRUE;
IntAE_insert_at(block->length, IntAE_get_nelt(block->length),
line-header_line);
//Rprintf("end of %s block, line: %d\n", block->name, line);
if (fgets(linebuf, LINEBUF_SIZE, stream) == NULL) /* skip empty line */
error("incomplete block");
line++;
}
}
}
result = Salloc(hashNumEntries(hash), ChainBlock *);
hash_elements = hashElListHash(hash);
for (struct hashEl *h = hash_elements; h; h = h->next, i++) {
result[i] = h->val;
}
*nblocks = i;
hashElFreeList(&hash_elements);
hashFree(&hash);
return result;
}
/* parse the chain file format from UCSC for representing alignments */
SEXP readChain(SEXP r_path, SEXP r_exclude) {
const char *path, *exclude;
FILE *stream;
SEXP ans, ans_names, ans_listData, chain_class, chainBlock_class;
ChainBlock **chains;
int i, nblocks;
path = translateChar(STRING_ELT(r_path, 0));
if ((stream = fopen(path, "r")) == NULL)
error("cannot open file '%s'", path);
exclude = r_exclude == R_NilValue ? NULL : CHAR(STRING_ELT(r_exclude, 0));
chains = read_chain_file(stream, exclude, &nblocks);
PROTECT(chain_class = MAKE_CLASS("Chain"));
PROTECT(chainBlock_class = MAKE_CLASS("ChainBlock"));
PROTECT(ans = NEW_OBJECT(chain_class));
ans_listData = allocVector(VECSXP, nblocks);
SET_SLOT(ans, install("listData"), ans_listData);
ans_names = allocVector(STRSXP, nblocks);
SET_NAMES(ans_listData, ans_names);
for (i = 0; i < nblocks; i++) {
SEXP block;
block = NEW_OBJECT(chainBlock_class);
SET_VECTOR_ELT(ans_listData, i, block);
SET_SLOT(block, install("ranges"),
new_IRanges_from_IntPairAE("IRanges", chains[i]->ranges));
SET_SLOT(block, install("offset"),
new_INTEGER_from_IntAE(chains[i]->offset));
SET_SLOT(block, install("length"),
new_INTEGER_from_IntAE(chains[i]->length));
SET_SLOT(block, install("score"),
new_INTEGER_from_IntAE(chains[i]->score));
SET_SLOT(block, install("space"),
new_CHARACTER_from_CharAEAE(chains[i]->space));
SET_SLOT(block, install("reversed"),
new_LOGICAL_from_CharAE(chains[i]->rev));
SET_STRING_ELT(ans_names, i, mkChar(chains[i]->name));
}
UNPROTECT(3);
return ans;
}