-
Notifications
You must be signed in to change notification settings - Fork 2
/
termbuilder.cpp
206 lines (179 loc) · 4.45 KB
/
termbuilder.cpp
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
#include<string.h>
#include<stdlib.h>
#include<stddef.h>
#include<syslog.h>
#include<stdio.h>
#include<map>
using std::map;
#include"preprocessor.h"
#include"processor.h"
/*local allocator*/
static struct term_t *pool;
static int pool_size;
struct term_t *new_term()
{
if (pool_size==0)
{
pool_size=100000;
pool=(struct term_t*)malloc(sizeof(*pool)*pool_size);
}
pool_size--;
new (pool) term_t();
return pool++;
}
/*tree management*/
static map<const char*, struct term_t*, ltstr> tree;
static map<const char*, ptrdiff_t, ltstr> stringset;
void add_term(struct term_t *term)
{
if (term==NULL) { return; }
if (term==NULL || tree.count(term->name)>0) { return; }
tree[term->id]=term;
}
int link_terms()
{
/*preprocess isa_for cache first; the inverses of these _should_ be subsets of the respective isa's*/
for (auto &i : tree)
{
struct term_t *t=i.second;
for (auto &j : t->isa_for)
{
if (tree.count(j)==0) { syslog(LOG_ERR,"term %s has nonexistent child '%s'",t->id,j); return 0; }
tree[j]->isa.insert(i.first);
}
}
/*now link term_t* parents and children by names*/
for (auto &i : tree)
{
struct term_t *t=i.second,*p;
for (auto &j : t->isa)
{
if (tree.count(j)==0) { syslog(LOG_ERR,"term %s has nonexistent parent '%s'",t->id,j); return 0; }
p=tree[j];
t->parents.insert(p);
p->children.insert(t);
}
stringset[t->id]=0;
stringset[t->name]=0;
}
return 1;
}
int coagulate_terms(char **buf, size_t *buf_len)
{
int strlens=2*sizeof(int),linklens,termlens,renderlens;
char *renderbuf;
/*string index*/
for (auto &i : stringset)
{
if (strlens%sizeof(size_t)) { strlens+=sizeof(size_t)-strlens%sizeof(size_t); }
i.second=strlens+str_serialize_padsize();
strlens+=str_serialize_len(i.first);
}
strlens=align(strlens,4096);
/*term linkage arrays*/
linklens=strlens;
for (auto &i : tree)
{
struct term_t *t=i.second;
t->i_parents=linklens;
linklens+=t->parents.size()*sizeof(ptrdiff_t);
t->i_children=linklens;
linklens+=t->children.size()*sizeof(ptrdiff_t);
}
linklens=align(linklens,4096);
/*term indexing*/
termlens=linklens;
int arr_index=0;
for (auto &i : tree)
{
struct term_t *t=i.second;
t->i_this=termlens;
termlens+=sizeof(struct int_term_t);
t->arr_index=arr_index++;
}
termlens=align(termlens,4096);
/*get buffer size for the json pre-render*/
renderlens=termlens;
for (auto &i : tree)
{
struct term_t *t=i.second;
renderlens += 200 + strlen(t->id) + strlen(t->name); //200 should encompass all the below rendered text
}
/*build the actual buffer*/
*buf_len=renderlens;
*buf=(char*)malloc(*buf_len);
memset(*buf,0,*buf_len);
((int*)(*buf))[0]=linklens;
((int*)(*buf))[1]=tree.size();
/*build the string section*/
for (auto &i : stringset)
{
str_serialize((*buf)+i.second,i.first);
}
/*build the term linkage tables*/
for (auto &i : tree)
{
ptrdiff_t *arr;
struct term_t *t=i.second;
arr=(ptrdiff_t*)((*buf)+t->i_parents);
for (auto &j : t->parents)
{
*(arr++)=j->i_this;
}
arr=(ptrdiff_t*)((*buf)+t->i_children);
for (auto &j : t->children)
{
*(arr++)=j->i_this;
}
}
/*build the actual term list*/
struct int_term_t *it=(struct int_term_t*)((*buf)+linklens);
renderbuf=*buf+termlens;
static const char *intplaceholder=" "; // 8 chars
static const char *floatplaceholder=" "; // 24 chars
for (auto &i : tree)
{
struct term_t *t=i.second;
it->parents.idx=t->i_parents;
it->nparents=t->parents.size();
it->children.idx=t->i_children;
it->nchildren=t->children.size();
it->id.idx=stringset[t->id];
it->name.idx=stringset[t->name];
it->flags.obsolete=t->obsolete;
if (!strcmp(t->name_space,"biological_process"))
{
it->flags.type=1;
}
if (!strcmp(t->name_space,"cellular_component"))
{
it->flags.type=2;
}
if (!strcmp(t->name_space,"molecular_function"))
{
it->flags.type=3;
}
if (it->flags.type!=0 && it->flags.obsolete==0 && t->parents.size()==0)
{
fprintf(stderr,"root term %s is a %d\n",t->id,(int)it->flags.type);
}
int renderlen;
it->prerender.idx=renderbuf-*buf;
renderlen=sprintf(renderbuf,
"{\"matched\": %s" // +16
",\"pval\":%s" // +32
",\"score\": %s" // +72
",\"total\": %s" // +112
",\"term_id\":\"%s\",\"term_name\":\"%s\",\"gene_ids\":[",
intplaceholder,
floatplaceholder,
floatplaceholder,
intplaceholder,
t->id,t->name
);
it->prerender_len=renderlen;
renderbuf+=align(renderlen,64);
it++;
}
return 1;
}