gecco 0.5.3

Gene Cluster prediction with Conditional Random Fields
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! The `gecco update-interpro` subcommand — rebuild interpro.json from upstream sources.
//!
//! Downloads the Gene Ontology (OBO format) and InterPro XML database,
//! then builds the interpro.json metadata file. Replaces the Python
//! `setup.py update_interpro` command that required the `pronto` library.

use std::collections::{HashMap, HashSet, VecDeque};
use std::io::{BufRead, BufReader, Read};
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::Args;
use log::info;

const GO_OBO_URL: &str = "http://purl.obolibrary.org/obo/go.obo";
const INTERPRO_XML_URL: &str =
    "https://ftp.ebi.ac.uk/pub/databases/interpro/current_release/interpro.xml.gz";

/// Molecular function root term.
const GO_MOLECULAR_FUNCTION: &str = "GO:0003674";

// ---------------------------------------------------------------------------
// CLI args
// ---------------------------------------------------------------------------

#[derive(Args)]
pub struct UpdateInterProArgs {
    /// Output directory for interpro.json (default: ./gecco_data).
    #[arg(short, long)]
    pub output_dir: Option<PathBuf>,

    /// Force re-download even if files exist.
    #[arg(short, long)]
    pub force: bool,
}

impl UpdateInterProArgs {
    pub fn execute(&self) -> Result<()> {
        let output_dir = self
            .output_dir
            .clone()
            .unwrap_or_else(|| PathBuf::from("gecco_data"));
        std::fs::create_dir_all(&output_dir)?;

        let interpro_path = output_dir.join("interpro.json");
        if interpro_path.exists() && !self.force {
            info!(
                "{:?} already exists, skipping (use --force to re-download)",
                interpro_path
            );
            return Ok(());
        }

        // 1. Download and parse Gene Ontology
        info!("Downloading Gene Ontology from {}", GO_OBO_URL);
        let go_data = ureq_get(GO_OBO_URL)?;
        info!("Parsing OBO file ({} bytes)", go_data.len());
        let go = parse_obo(&go_data)?;
        info!("Loaded {} GO terms", go.terms.len());

        // Find top-level molecular functions (direct children of GO:0003674)
        let top_functions = go.children_of(GO_MOLECULAR_FUNCTION);
        info!(
            "Found {} top-level molecular function terms",
            top_functions.len()
        );

        // 2. Download and parse InterPro XML
        info!("Downloading InterPro XML from {}", INTERPRO_XML_URL);
        info!("(this file is ~1.5 GB compressed, download may take a while)");
        let xml_gz_data = ureq_get_large(INTERPRO_XML_URL)?;
        info!(
            "Downloaded {} MB, decompressing and parsing...",
            xml_gz_data.len() / (1024 * 1024)
        );

        let decoder = flate2::read::GzDecoder::new(xml_gz_data.as_slice());
        let entries = parse_interpro_xml(decoder, &go, &top_functions)?;
        info!("Parsed {} InterPro entries", entries.len());

        // 3. Write JSON
        let json = serde_json::to_string_pretty(&entries)?;
        std::fs::write(&interpro_path, &json)?;
        info!("Wrote interpro.json to {:?}", interpro_path);

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// OBO parser — minimal parser for Gene Ontology .obo files
// ---------------------------------------------------------------------------

/// A parsed GO term.
#[derive(Debug, Clone)]
struct OboTerm {
    id: String,
    name: String,
    namespace: String,
    is_a: Vec<String>, // parent term IDs
}

/// The parsed Gene Ontology graph.
struct GeneOntology {
    terms: HashMap<String, OboTerm>,
    /// Reverse index: parent_id → set of direct child IDs.
    children: HashMap<String, HashSet<String>>,
}

impl GeneOntology {
    /// Get direct children of a term.
    fn children_of(&self, term_id: &str) -> HashSet<String> {
        self.children.get(term_id).cloned().unwrap_or_default()
    }

    /// Get all ancestor term IDs (transitive closure of is_a), including self.
    fn superclasses(&self, term_id: &str) -> HashSet<String> {
        let mut result = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(term_id.to_string());
        while let Some(current) = queue.pop_front() {
            if !result.insert(current.clone()) {
                continue;
            }
            if let Some(term) = self.terms.get(&current) {
                for parent in &term.is_a {
                    queue.push_back(parent.clone());
                }
            }
        }
        result
    }
}

/// Parse an OBO file into a GeneOntology.
fn parse_obo(data: &[u8]) -> Result<GeneOntology> {
    let reader = BufReader::new(data);
    let mut terms = HashMap::new();
    let mut children: HashMap<String, HashSet<String>> = HashMap::new();

    let mut in_term = false;
    let mut id = String::new();
    let mut name = String::new();
    let mut namespace = String::new();
    let mut is_a: Vec<String> = Vec::new();
    let mut is_obsolete = false;

    for line in reader.lines() {
        let line = line?;
        let line = line.trim_end();

        if line == "[Term]" {
            // Save previous term if any
            if in_term && !id.is_empty() && !is_obsolete {
                for parent in &is_a {
                    children
                        .entry(parent.clone())
                        .or_default()
                        .insert(id.clone());
                }
                terms.insert(
                    id.clone(),
                    OboTerm {
                        id: id.clone(),
                        name: name.clone(),
                        namespace: namespace.clone(),
                        is_a: is_a.clone(),
                    },
                );
            }
            in_term = true;
            id.clear();
            name.clear();
            namespace.clear();
            is_a.clear();
            is_obsolete = false;
            continue;
        }

        if line.starts_with('[') {
            // End of [Term] block (e.g. [Typedef])
            if in_term && !id.is_empty() && !is_obsolete {
                for parent in &is_a {
                    children
                        .entry(parent.clone())
                        .or_default()
                        .insert(id.clone());
                }
                terms.insert(
                    id.clone(),
                    OboTerm {
                        id: id.clone(),
                        name: name.clone(),
                        namespace: namespace.clone(),
                        is_a: is_a.clone(),
                    },
                );
            }
            in_term = false;
            continue;
        }

        if !in_term {
            continue;
        }

        if let Some(val) = line.strip_prefix("id: ") {
            id = val.to_string();
        } else if let Some(val) = line.strip_prefix("name: ") {
            name = val.to_string();
        } else if let Some(val) = line.strip_prefix("namespace: ") {
            namespace = val.to_string();
        } else if let Some(val) = line.strip_prefix("is_a: ") {
            // "is_a: GO:0008150 ! biological_process" — take just the ID
            if let Some(parent_id) = val.split_whitespace().next() {
                is_a.push(parent_id.to_string());
            }
        } else if line == "is_obsolete: true" {
            is_obsolete = true;
        }
    }

    // Don't forget the last term
    if in_term && !id.is_empty() && !is_obsolete {
        for parent in &is_a {
            children
                .entry(parent.clone())
                .or_default()
                .insert(id.clone());
        }
        terms.insert(
            id.clone(),
            OboTerm {
                id,
                name,
                namespace,
                is_a,
            },
        );
    }

    Ok(GeneOntology { terms, children })
}

// ---------------------------------------------------------------------------
// InterPro XML parser — streaming parser for interpro.xml
// ---------------------------------------------------------------------------

/// Output entry matching Python GECCO's interpro.json schema.
#[derive(Debug, serde::Serialize)]
struct InterProJsonEntry {
    accession: String,
    databases: Vec<String>,
    go_functions: Vec<GoRef>,
    go_terms: Vec<GoTermRef>,
    members: Vec<String>,
    name: String,
    #[serde(rename = "type")]
    entry_type: String,
}

#[derive(Debug, serde::Serialize)]
struct GoRef {
    accession: String,
    name: String,
}

#[derive(Debug, serde::Serialize)]
struct GoTermRef {
    accession: String,
    name: String,
    namespace: String,
}

/// Parse the InterPro XML and build JSON entries.
fn parse_interpro_xml<R: Read>(
    reader: R,
    go: &GeneOntology,
    top_functions: &HashSet<String>,
) -> Result<Vec<InterProJsonEntry>> {
    use quick_xml::events::Event;
    use quick_xml::Reader;

    let buf_reader = BufReader::new(reader);
    let mut xml_reader = Reader::from_reader(buf_reader);
    xml_reader.config_mut().trim_text(true);

    let mut entries = Vec::new();
    let mut buf = Vec::new();

    // State for current <interpro> element
    let mut in_interpro = false;
    let mut accession = String::new();
    let mut name = String::new();
    let mut entry_type = String::new();
    let mut members: HashSet<String> = HashSet::new();
    let mut databases: HashSet<String> = HashSet::new();
    let mut go_term_ids: HashSet<String> = HashSet::new();

    // Nested element tracking
    let mut in_name = false;
    let mut in_member_list = false;
    let mut in_class_list = false;
    let mut depth: u32 = 0;

    loop {
        match xml_reader.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Ok(Event::Start(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"interpro" => {
                        in_interpro = true;
                        depth = 0;
                        accession.clear();
                        name.clear();
                        entry_type.clear();
                        members.clear();
                        databases.clear();
                        go_term_ids.clear();
                        in_name = false;
                        in_member_list = false;
                        in_class_list = false;

                        for attr in e.attributes().flatten() {
                            match attr.key.as_ref() {
                                b"id" => {
                                    accession = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                b"type" => {
                                    entry_type =
                                        String::from_utf8_lossy(&attr.value).to_lowercase();
                                }
                                _ => {}
                            }
                        }
                    }
                    b"name" if in_interpro && depth == 0 => {
                        in_name = true;
                    }
                    b"member_list" if in_interpro => {
                        in_member_list = true;
                    }
                    b"db_xref" if in_interpro && in_member_list => {
                        let mut dbkey = String::new();
                        let mut db = String::new();
                        for attr in e.attributes().flatten() {
                            match attr.key.as_ref() {
                                b"dbkey" => {
                                    dbkey = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                b"db" => {
                                    db = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                _ => {}
                            }
                        }
                        if !dbkey.is_empty() {
                            members.insert(dbkey);
                        }
                        if !db.is_empty() {
                            databases.insert(db);
                        }
                    }
                    b"class_list" if in_interpro => {
                        in_class_list = true;
                    }
                    b"classification" if in_interpro && in_class_list => {
                        let mut class_type = String::new();
                        let mut class_id = String::new();
                        for attr in e.attributes().flatten() {
                            match attr.key.as_ref() {
                                b"class_type" => {
                                    class_type = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                b"id" => {
                                    class_id = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                _ => {}
                            }
                        }
                        if class_type == "GO" && !class_id.is_empty() {
                            go_term_ids.insert(class_id);
                        }
                    }
                    _ => {
                        if in_interpro {
                            depth += 1;
                        }
                    }
                }
            }
            Ok(Event::End(ref e)) => {
                let local = e.local_name();
                match local.as_ref() {
                    b"interpro" => {
                        if in_interpro {
                            // Build GO terms and functions
                            let mut go_terms: Vec<GoTermRef> = Vec::new();
                            let mut go_function_set: HashSet<String> = HashSet::new();

                            for go_id in &go_term_ids {
                                if let Some(term) = go.terms.get(go_id) {
                                    go_terms.push(GoTermRef {
                                        accession: term.id.clone(),
                                        name: term.name.clone(),
                                        namespace: term.namespace.clone(),
                                    });

                                    // Find top-level function ancestors
                                    let ancestors = go.superclasses(go_id);
                                    for ancestor_id in ancestors.intersection(top_functions) {
                                        go_function_set.insert(ancestor_id.clone());
                                    }
                                }
                            }

                            go_terms.sort_by(|a, b| a.accession.cmp(&b.accession));

                            let mut go_functions: Vec<GoRef> = go_function_set
                                .iter()
                                .filter_map(|fid| {
                                    go.terms.get(fid).map(|t| GoRef {
                                        accession: t.id.clone(),
                                        name: t.name.clone(),
                                    })
                                })
                                .collect();
                            go_functions.sort_by(|a, b| a.accession.cmp(&b.accession));

                            let mut members_sorted: Vec<String> = members.iter().cloned().collect();
                            members_sorted.sort();

                            let mut databases_sorted: Vec<String> =
                                databases.iter().cloned().collect();
                            databases_sorted.sort();

                            entries.push(InterProJsonEntry {
                                accession: std::mem::take(&mut accession),
                                databases: databases_sorted,
                                go_functions,
                                go_terms,
                                members: members_sorted,
                                name: name.clone(),
                                entry_type: entry_type.clone(),
                            });

                            in_interpro = false;
                        }
                    }
                    b"name" => {
                        in_name = false;
                    }
                    b"member_list" => {
                        in_member_list = false;
                    }
                    b"class_list" => {
                        in_class_list = false;
                    }
                    _ => {
                        if in_interpro && depth > 0 {
                            depth -= 1;
                        }
                    }
                }
            }
            Ok(Event::Empty(ref e)) => {
                let local = e.local_name();
                // Handle self-closing tags like <db_xref .../> and <classification .../>
                match local.as_ref() {
                    b"db_xref" if in_interpro && in_member_list => {
                        let mut dbkey = String::new();
                        let mut db = String::new();
                        for attr in e.attributes().flatten() {
                            match attr.key.as_ref() {
                                b"dbkey" => {
                                    dbkey = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                b"db" => {
                                    db = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                _ => {}
                            }
                        }
                        if !dbkey.is_empty() {
                            members.insert(dbkey);
                        }
                        if !db.is_empty() {
                            databases.insert(db);
                        }
                    }
                    b"classification" if in_interpro && in_class_list => {
                        let mut class_type = String::new();
                        let mut class_id = String::new();
                        for attr in e.attributes().flatten() {
                            match attr.key.as_ref() {
                                b"class_type" => {
                                    class_type = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                b"id" => {
                                    class_id = String::from_utf8_lossy(&attr.value).to_string();
                                }
                                _ => {}
                            }
                        }
                        if class_type == "GO" && !class_id.is_empty() {
                            go_term_ids.insert(class_id);
                        }
                    }
                    _ => {}
                }
            }
            Ok(Event::Text(ref e)) => {
                if in_name && in_interpro {
                    name = e.xml_content().unwrap_or_default().to_string();
                    in_name = false;
                }
            }
            Err(e) => {
                return Err(anyhow::anyhow!(
                    "XML parse error at position {}: {}",
                    xml_reader.error_position(),
                    e
                ));
            }
            _ => {}
        }
        buf.clear();
    }

    // Sort by accession like Python does
    entries.sort_by(|a, b| a.accession.cmp(&b.accession));

    Ok(entries)
}

// ---------------------------------------------------------------------------
// HTTP helpers
// ---------------------------------------------------------------------------

fn ureq_get(url: &str) -> Result<Vec<u8>> {
    let agent = ureq::Agent::new_with_config(
        ureq::config::Config::builder()
            .timeout_global(Some(std::time::Duration::from_secs(300)))
            .build(),
    );
    let body = agent
        .get(url)
        .call()
        .with_context(|| format!("downloading {}", url))?
        .into_body()
        .with_config()
        .limit(200 * 1024 * 1024)
        .read_to_vec()
        .with_context(|| format!("reading response from {}", url))?;
    Ok(body)
}

/// Download a large file (up to 2 GB) — needed for interpro.xml.gz.
fn ureq_get_large(url: &str) -> Result<Vec<u8>> {
    let agent = ureq::Agent::new_with_config(
        ureq::config::Config::builder()
            .timeout_global(Some(std::time::Duration::from_secs(1800)))
            .build(),
    );
    let body = agent
        .get(url)
        .call()
        .with_context(|| format!("downloading {}", url))?
        .into_body()
        .with_config()
        .limit(2 * 1024 * 1024 * 1024) // 2 GB
        .read_to_vec()
        .with_context(|| format!("reading response from {}", url))?;
    Ok(body)
}