about summary refs log tree commit diff
path: root/src/indexer/build.rs
blob: 13074f8903f917e5e186a77aed6205d5203d488e (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! Document construction, mirroring `index_genes` / `index_phenotypes`
//! in `~/project/genenetwork3/scripts/index-genenetwork` call for call.
//!
//! The whole point of this module is *not* to improve on the indexer:
//! the TermGenerator configuration (English stemmer, default stemming
//! strategy — never set, `STOP_ALL` with an empty `SimpleStopper`),
//! the order of the `index_text` / `index_text_without_positions`
//! calls, the prefixes, the value slots, the popped-before-storage
//! fields and the identity term are reproduced exactly, because the
//! twin gate compares term sets, wdf, positions, slots and docdata
//! byte for byte.

use xapian_rs::{Document, SimpleStopper, Stem, StopStrategy, TermGenerator};

use super::cache::RdfCaches;
use super::pyjson::{JsonObject, JsonValue};
use super::rows::{GeneRow, PhenotypeRow};
use super::schema::{self, prefix, slot};
use super::sha256::hex_digest;

/// A document ready to write, its identity term and its source hash.
pub struct BuiltDocument {
    pub idterm: String,
    pub document: Document,
    pub source_hash: String,
}

/// The writer's TermGenerator, configured exactly as the indexer.
fn writer_termgen() -> TermGenerator {
    let mut termgen = TermGenerator::default();
    termgen.set_stemmer(Stem::for_language("en"));
    termgen.set_stopper_strategy(StopStrategy::All);
    termgen.set_stopper(SimpleStopper::new());
    termgen
}

/// Canonical encoder for a document's complete source, used for the
/// incremental change hash. Deterministic and delimiter-safe.
#[derive(Default)]
struct Canon {
    buffer: Vec<u8>,
}

impl Canon {
    fn field(&mut self, key: &str, value: &str) {
        self.buffer.extend_from_slice(key.as_bytes());
        self.buffer.push(b'=');
        self.buffer
            .extend_from_slice(value.len().to_string().as_bytes());
        self.buffer.push(b':');
        self.buffer.extend_from_slice(value.as_bytes());
        self.buffer.push(b'\n');
    }

    fn opt_str(&mut self, key: &str, value: &Option<String>) {
        match value {
            Some(text) => self.field(key, text),
            None => {
                self.buffer.extend_from_slice(key.as_bytes());
                self.buffer.extend_from_slice(b"=N\n");
            }
        }
    }

    fn opt_f64(&mut self, key: &str, value: &Option<f64>) {
        match value {
            Some(number) => self.field(key, &format!("{:016x}", number.to_bits())),
            None => {
                self.buffer.extend_from_slice(key.as_bytes());
                self.buffer.extend_from_slice(b"=N\n");
            }
        }
    }

    fn opt_i64(&mut self, key: &str, value: &Option<i64>) {
        match value {
            Some(number) => self.field(key, &number.to_string()),
            None => {
                self.buffer.extend_from_slice(key.as_bytes());
                self.buffer.extend_from_slice(b"=N\n");
            }
        }
    }
}

/// The stable identity term of a gene row, or `None` when name or
/// dataset is absent (the crawl then produces no document).
///
/// This is the cheap identity pre-pass: it computes exactly the
/// `name:dataset` term `build_gene` would write, without building a
/// document, so identity uniqueness can be validated before any write.
pub fn gene_idterm(row: &GeneRow) -> Option<String> {
    let (name, dataset) = (row.name.as_ref()?, row.dataset.as_ref()?);
    Some(schema::identity_term(schema::DOCTYPE_GENE, name, dataset))
}

/// The stable identity term of a phenotype row, or `None` when name or
/// dataset is absent. The integer name is stringified exactly as the
/// document builder does.
pub fn phenotype_idterm(row: &PhenotypeRow) -> Option<String> {
    let (name, dataset) = (row.name.as_ref()?, row.dataset.as_ref()?);
    Some(schema::identity_term(
        schema::DOCTYPE_PHENOTYPE,
        &name.to_string(),
        dataset,
    ))
}

/// Build a gene document. `None` when name or dataset is absent (the
/// indexer then writes no document at all).
pub fn build_gene(row: &GeneRow, caches: &RdfCaches) -> Option<BuiltDocument> {
    let idterm = gene_idterm(row)?;

    let mut doc = Document::default();
    {
        let mut termgen = writer_termgen();
        termgen.set_document(&doc);

        // Values (slots 0,1,2,3,4).
        if let Some(mean) = row.mean {
            doc.set_value(slot::MEAN, mean);
        }
        if let Some(lrs) = row.lrs {
            doc.set_value(slot::PEAK, lrs);
        }
        if let Some(mb) = row.mb {
            doc.set_value(slot::POSITION, mb);
        }
        if let Some(geno_mb) = row.geno_mb {
            doc.set_value(slot::PEAK_MB, geno_mb);
        }
        if let Some(additive) = row.additive {
            doc.set_value(slot::ADDITIVE, additive);
        }

        // Free text WITH positions.
        for text in [&row.description, &row.tissue, &row.dataset] {
            if let Some(text) = text {
                termgen.index_text::<&str>(text, None, None);
                termgen.increase_termpos(None);
            }
        }
        if let Some(text) = &row.probe_target_description {
            termgen.index_text::<&str>(text, None, None);
            termgen.increase_termpos(None);
        }
        // Free text WITHOUT positions, unprefixed.
        for text in [&row.name, &row.symbol, &row.species, &row.group] {
            if let Some(text) = text {
                termgen.index_text_without_positions::<&str>(text, None, None);
            }
        }
        for text in [&row.alias, &row.genbankid, &row.unigeneid] {
            if let Some(text) = text {
                termgen.index_text_without_positions::<&str>(text, None, None);
            }
        }
        // Prefixed: WITH positions for XI/XDS, without for the rest.
        if let Some(text) = &row.species {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::SPECIES));
        }
        if let Some(text) = &row.group {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::GROUP));
        }
        if let Some(text) = &row.tissue {
            termgen.index_text::<&str>(text, Some(0u32), Some(prefix::TISSUE));
        }
        if let Some(text) = &row.dataset {
            termgen.index_text::<&str>(text, Some(0u32), Some(prefix::DATASET));
        }
        if let Some(text) = &row.symbol {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::SYMBOL));
        }
        if let Some(text) = &row.chr {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::CHR));
        }
        if let Some(text) = &row.geno_chr {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::PEAK_CHR));
        }
        // RDF dictionaries (XRF / XWK), keyed on (species, symbol).
        if let (Some(species), Some(symbol)) = (&row.species, &row.symbol) {
            if let Some(text) = caches.rif.get(species, symbol) {
                termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::RIF));
            }
            if let Some(text) = caches.wiki.get(species, symbol) {
                termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::WIKI));
            }
        }
    }

    // Stored data: insertion order minus the popped fields.
    let mut data = JsonObject::new();
    push_opt_str(&mut data, "name", &row.name);
    push_opt_str(&mut data, "symbol", &row.symbol);
    push_opt_str(&mut data, "description", &row.description);
    push_opt_str(&mut data, "chr", &row.chr);
    push_opt_f64(&mut data, "mb", &row.mb);
    push_opt_str(&mut data, "dataset", &row.dataset);
    push_opt_str(&mut data, "dataset_fullname", &row.dataset_fullname);
    push_opt_str(&mut data, "species", &row.species);
    push_opt_str(&mut data, "group", &row.group);
    push_opt_str(&mut data, "tissue", &row.tissue);
    push_opt_f64(&mut data, "mean", &row.mean);
    push_opt_f64(&mut data, "lrs", &row.lrs);
    push_opt_f64(&mut data, "additive", &row.additive);
    push_opt_str(&mut data, "geno_chr", &row.geno_chr);
    push_opt_f64(&mut data, "geno_mb", &row.geno_mb);
    doc.set_data(data.to_string());

    doc.add_boolean_term(schema::type_term(schema::DOCTYPE_GENE));
    doc.add_boolean_term(&idterm);

    // Source hash: full row + the monolithic cache hashes.
    let mut canon = Canon::default();
    canon.opt_str("name", &row.name);
    canon.opt_str("symbol", &row.symbol);
    canon.opt_str("description", &row.description);
    canon.opt_str("chr", &row.chr);
    canon.opt_f64("mb", &row.mb);
    canon.opt_str("alias", &row.alias);
    canon.opt_str("genbankid", &row.genbankid);
    canon.opt_str("unigeneid", &row.unigeneid);
    canon.opt_str("probe_target_description", &row.probe_target_description);
    canon.opt_str("dataset", &row.dataset);
    canon.opt_str("dataset_fullname", &row.dataset_fullname);
    canon.opt_str("species", &row.species);
    canon.opt_str("group", &row.group);
    canon.opt_str("tissue", &row.tissue);
    canon.opt_f64("mean", &row.mean);
    canon.opt_f64("lrs", &row.lrs);
    canon.opt_f64("additive", &row.additive);
    canon.opt_str("geno_chr", &row.geno_chr);
    canon.opt_f64("geno_mb", &row.geno_mb);
    canon.field("rif-cache", &caches.rif.hash);
    canon.field("wiki-cache", &caches.wiki.hash);

    Some(BuiltDocument {
        idterm,
        document: doc,
        source_hash: hex_digest(&canon.buffer),
    })
}

/// Build a phenotype document.
pub fn build_phenotype(row: &PhenotypeRow) -> Option<BuiltDocument> {
    // Python converts the integer name to str for the document (and for
    // the identity term's identifier).
    let name_text = row.name.as_ref()?.to_string();
    let idterm = phenotype_idterm(row)?;

    // The raw comma-separated authors string is indexed before storage.
    let authors_raw = row.authors.clone();
    let authors_list: Option<Vec<String>> = authors_raw
        .as_ref()
        .map(|text| text.split(',').map(|a| a.trim().to_string()).collect());

    let mut doc = Document::default();
    {
        let mut termgen = writer_termgen();
        termgen.set_document(&doc);

        // Values (slots 0,1,3,4,5,6 — no position slot).
        if let Some(mean) = row.mean {
            doc.set_value(slot::MEAN, mean);
        }
        if let Some(lrs) = row.lrs {
            doc.set_value(slot::PEAK, lrs);
        }
        if let Some(geno_mb) = row.geno_mb {
            doc.set_value(slot::PEAK_MB, geno_mb);
        }
        if let Some(additive) = row.additive {
            doc.set_value(slot::ADDITIVE, additive);
        }
        if let Some(year) = row.year {
            doc.set_value(slot::YEAR, year as f64);
        }
        if let Some(nsamples) = row.nsamples {
            doc.set_value(slot::NSAMPLES, nsamples as f64);
        }

        // Free text WITH positions.
        for text in [&row.description, &authors_raw, &row.dataset] {
            if let Some(text) = text {
                termgen.index_text::<&str>(text, None, None);
                termgen.increase_termpos(None);
            }
        }
        for text in [&row.abstract_, &row.title] {
            if let Some(text) = text {
                termgen.index_text::<&str>(text, None, None);
                termgen.increase_termpos(None);
            }
        }
        // Free text WITHOUT positions, unprefixed.
        for text in [&row.species, &row.group, &row.inbredsetcode] {
            if let Some(text) = text {
                termgen.index_text_without_positions::<&str>(text, None, None);
            }
        }
        for text in [&row.abbreviation, &row.lab_code] {
            if let Some(text) = text {
                termgen.index_text_without_positions::<&str>(text, None, None);
            }
        }
        // Prefixed: WITH positions for A/XDS.
        if let Some(text) = &row.species {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::SPECIES));
        }
        if let Some(text) = &row.group {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::GROUP));
        }
        if let Some(text) = &authors_raw {
            termgen.index_text::<&str>(text, Some(0u32), Some(prefix::AUTHOR));
        }
        if let Some(text) = &row.geno_chr {
            termgen.index_text_without_positions::<&str>(text, Some(0u32), Some(prefix::PEAK_CHR));
        }
        if let Some(text) = &row.dataset {
            termgen.index_text::<&str>(text, Some(0u32), Some(prefix::DATASET));
        }
    }

    let mut data = JsonObject::new();
    push_opt_str(&mut data, "species", &row.species);
    push_opt_str(&mut data, "group", &row.group);
    push_opt_str(&mut data, "dataset", &row.dataset);
    push_opt_str(&mut data, "dataset_fullname", &row.dataset_fullname);
    data.push("name", JsonValue::Str(name_text.clone()));
    push_opt_str(&mut data, "description", &row.description);
    if let Some(authors) = &authors_list {
        data.push("authors", JsonValue::StrList(authors.clone()));
    }
    push_opt_i64(&mut data, "year", &row.year);
    push_opt_i64(&mut data, "pubmed_id", &row.pubmed_id);
    push_opt_f64(&mut data, "lrs", &row.lrs);
    push_opt_f64(&mut data, "additive", &row.additive);
    push_opt_str(&mut data, "inbredsetcode", &row.inbredsetcode);
    push_opt_f64(&mut data, "mean", &row.mean);
    push_opt_i64(&mut data, "nsamples", &row.nsamples);
    push_opt_str(&mut data, "geno_chr", &row.geno_chr);
    push_opt_f64(&mut data, "geno_mb", &row.geno_mb);
    doc.set_data(data.to_string());

    doc.add_boolean_term(schema::type_term(schema::DOCTYPE_PHENOTYPE));
    doc.add_boolean_term(&idterm);

    let mut canon = Canon::default();
    canon.opt_str("species", &row.species);
    canon.opt_str("group", &row.group);
    canon.opt_str("dataset", &row.dataset);
    canon.opt_str("dataset_fullname", &row.dataset_fullname);
    canon.opt_i64("name", &row.name);
    canon.opt_str("abbreviation", &row.abbreviation);
    canon.opt_str("description", &row.description);
    canon.opt_str("Lab_code", &row.lab_code);
    canon.opt_str("Abstract", &row.abstract_);
    canon.opt_str("Title", &row.title);
    canon.opt_str("authors", &row.authors);
    canon.opt_i64("year", &row.year);
    canon.opt_i64("pubmed_id", &row.pubmed_id);
    canon.opt_f64("lrs", &row.lrs);
    canon.opt_f64("additive", &row.additive);
    canon.opt_str("inbredsetcode", &row.inbredsetcode);
    canon.opt_f64("mean", &row.mean);
    canon.opt_i64("nsamples", &row.nsamples);
    canon.opt_str("geno_chr", &row.geno_chr);
    canon.opt_f64("geno_mb", &row.geno_mb);

    Some(BuiltDocument {
        idterm,
        document: doc,
        source_hash: hex_digest(&canon.buffer),
    })
}

fn push_opt_str(object: &mut JsonObject, key: &str, value: &Option<String>) {
    if let Some(text) = value {
        object.push(key, JsonValue::Str(text.clone()));
    }
}

fn push_opt_f64(object: &mut JsonObject, key: &str, value: &Option<f64>) {
    if let Some(number) = value {
        object.push(key, JsonValue::Float(*number));
    }
}

fn push_opt_i64(object: &mut JsonObject, key: &str, value: &Option<i64>) {
    if let Some(number) = value {
        object.push(key, JsonValue::Int(*number));
    }
}