-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
490 lines (446 loc) · 15.1 KB
/
lib.rs
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
//! # genomicsqlite
//!
//! Genomics Extension for SQLite
//!
//! Installation & programming guide: [https://mlin.github.io/GenomicSQLite/](https://mlin.github.io/GenomicSQLite/)
use json::object::Object;
use rusqlite::{params, Connection, LoadExtensionGuard, OpenFlags, Result};
use std::collections::HashMap;
use std::env;
#[cfg(feature = "bundle_libgenomicsqlite")]
use std::fs::File;
#[cfg(feature = "bundle_libgenomicsqlite")]
use std::io::prelude::*;
use std::path::Path;
use std::sync::Once;
use tempfile::TempDir;
/* Helper functions for bundling libgenomicsqlite.{so,dylib} into the compilation unit */
// extract slice into basename under a temp directory
#[cfg(feature = "bundle_libgenomicsqlite")]
fn extract_libgenomicsqlite_impl(
bytes: &[u8],
basename: &str,
) -> std::io::Result<(String, TempDir)> {
let tmp = tempfile::tempdir()?;
let libpath = match tmp.path().join(basename).to_str() {
Some(p) => p.to_string(),
None => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"tempdir",
))
}
};
{
let mut file = File::create(&libpath)?;
file.write_all(bytes)?;
}
Ok((libpath, tmp))
}
#[cfg(feature = "bundle_libgenomicsqlite")]
fn extract_libgenomicsqlite(bytes: &[u8], filename: &str) -> (String, TempDir) {
match extract_libgenomicsqlite_impl(bytes, filename) {
Ok(ans) => ans,
Err(_) => panic!("genomicsqlite: failed extracting temp libgenomicsqlite; check temp free space & permissions")
}
}
#[cfg(not(feature = "bundle_libgenomicsqlite"))]
fn bundle_libgenomicsqlite() -> Option<(String, TempDir)> {
None
}
#[cfg(all(
feature = "bundle_libgenomicsqlite",
target_os = "linux",
target_arch = "x86_64"
))]
fn bundle_libgenomicsqlite() -> Option<(String, TempDir)> {
Some(extract_libgenomicsqlite(
include_bytes!("../libgenomicsqlite.so"),
&"libgenomicsqlite.so",
))
}
#[cfg(all(
feature = "bundle_libgenomicsqlite",
target_os = "macos",
target_arch = "x86_64"
))]
fn bundle_libgenomicsqlite() -> Option<(String, TempDir)> {
Some(extract_libgenomicsqlite(
include_bytes!("../libgenomicsqlite.dylib"),
"libgenomicsqlite.dylib",
))
}
// helper for simple queries
fn query1str<P>(conn: &Connection, sql: &str, params: P) -> Result<String>
where
P: rusqlite::Params,
{
let ans: Result<String> = conn.query_row(sql, params, |row| row.get(0));
ans
}
static START: Once = Once::new();
/// Open a [rusqlite::Connection] for a compressed database with the [ConnectionMethods] available.
///
/// # Arguments
///
/// * `path` - Database filename
/// * `flags` - SQLite open mode flags, as in [rusqlite::Connection::open_with_flags]
/// * `config` - [GenomicSQLite tuning options](https://mlin.github.io/GenomicSQLite/guide/#tuning-options),
/// as JSON keys & values; may be empty `Object::new()` to use defaults
///
/// # Examples
///
/// ```
/// use genomicsqlite::ConnectionMethods;
/// let conn = genomicsqlite::open(
/// "/tmp/rustdoc_example.genomicsqlite",
/// rusqlite::OpenFlags::SQLITE_OPEN_CREATE | rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE,
/// &json::object::Object::new()
/// );
/// println!("GenomicSQLite {}", conn.unwrap().genomicsqlite_version());
/// ```
pub fn open<P: AsRef<Path>>(path: P, flags: OpenFlags, config: &Object) -> Result<Connection> {
// once: load libgenomicsqlite extension
START.call_once(|| {
let mut _tmpdir;
let libgenomicsqlite = match env::var("LIBGENOMICSQLITE") {
Ok(v) => v,
Err(_) => match bundle_libgenomicsqlite() {
Some((ans, td)) => {
_tmpdir = td;
ans
}
None => "libgenomicsqlite".to_string(),
},
};
let memconn = Connection::open_in_memory().unwrap();
unsafe {
let _guard = LoadExtensionGuard::new(&memconn).unwrap();
match memconn.load_extension(libgenomicsqlite.clone(), None) {
Err(err) => panic!(
"genomicsqlite failed to load_extension(\"{}\"): {}",
libgenomicsqlite, err
),
Ok(()) => (),
}
}
});
// generate config & connection strings
let memconn = Connection::open_in_memory().unwrap();
let config_json_str = config.dump();
let uri: String = query1str(
&memconn,
"SELECT genomicsqlite_uri(?,?)",
params![path.as_ref().to_str().unwrap(), &config_json_str],
)?;
// open GenomicSQLite connection
let conn = Connection::open_with_flags(uri, flags | OpenFlags::SQLITE_OPEN_URI)?;
// generate and execute tuning SQL script
let tuning_sql = query1str(
&conn,
"SELECT genomicsqlite_tuning_sql(?)",
params![&config_json_str],
)?;
conn.execute_batch(&tuning_sql)?;
Ok(conn)
}
/// Genomic reference sequence metadata
#[derive(Clone)]
pub struct RefSeq {
pub rid: i64,
pub name: String,
pub length: i64,
pub assembly: Option<String>,
pub refget_id: Option<String>,
pub meta_json: Object,
}
/// Methods for GenomicSQLite [rusqlite::Connection]s; see [Programming Guide](https://mlin.github.io/GenomicSQLite/guide/)
/// for each method's semantics. The methods can also be invoked on an open
/// [rusqlite::Transaction], via its implicit `Deref<Target=Connection>`.
pub trait ConnectionMethods {
/// Get Genomics Extension version
fn genomicsqlite_version(&self) -> String;
/// Generate SQL command to attach another GenomicSQLite database to an existing connection
fn genomicsqlite_attach_sql<P: AsRef<Path>>(
&self,
path: P,
schema_name: &str,
config: &Object,
) -> Result<String>;
/// Generate SQL command to `VACUUM INTO` the database into a new file
fn genomicsqlite_vacuum_into_sql<P: AsRef<Path>>(
&self,
path: P,
config: &Object,
) -> Result<String>;
/// Generate SQL script to create Genomic Range Index for a table
fn create_genomic_range_index_sql(
&self,
table_name: &str,
chromosome: &str,
begin_pos: &str,
end_pos: &str,
) -> Result<String>;
fn create_genomic_range_index_sql_with_floor(
&self,
table_name: &str,
chromosome: &str,
begin_pos: &str,
end_pos: &str,
floor: i64,
) -> Result<String>;
/// Generate SQL script to add a reference assembly to the stored metadata
fn put_reference_assembly_sql(&self, assembly: &str) -> Result<String>;
fn put_reference_assembly_sql_with_schema(
&self,
assembly: &str,
schema: &str,
) -> Result<String>;
/// Generate SQL script to add one reference sequence to the stored metadata. Set `rid: -1` to
/// let the database generate a value.
fn put_reference_sequence_sql(&self, refseq: &RefSeq) -> Result<String>;
fn put_reference_sequence_sql_with_schema(
&self,
refseq: &RefSeq,
schema: &str,
) -> Result<String>;
/// Read the stored reference sequence metadata into a lookup table keyed by `rid`
fn get_reference_sequences_by_rid(&self) -> Result<HashMap<i64, RefSeq>>;
fn get_reference_sequences_by_rid_with_options(
&self,
assembly: Option<&str>,
schema: Option<&str>,
) -> Result<HashMap<i64, RefSeq>>;
/// Read the stored reference sequence metadata into a lookup table keyed by `name`
fn get_reference_sequences_by_name(&self) -> Result<HashMap<String, RefSeq>>;
fn get_reference_sequences_by_name_with_options(
&self,
assembly: Option<&str>,
schema: Option<&str>,
) -> Result<HashMap<String, RefSeq>>;
}
impl ConnectionMethods for Connection {
fn genomicsqlite_version(&self) -> String {
query1str(self, "SELECT genomicsqlite_version()", []).unwrap()
}
fn genomicsqlite_attach_sql<P: AsRef<Path>>(
&self,
path: P,
schema_name: &str,
config: &Object,
) -> Result<String> {
query1str(
self,
"SELECT genomicsqlite_attach_sql(?,?,?)",
params![path.as_ref().to_str(), schema_name, config.dump()],
)
}
fn genomicsqlite_vacuum_into_sql<P: AsRef<Path>>(
&self,
path: P,
config: &Object,
) -> Result<String> {
query1str(
self,
"SELECT genomicsqlite_vacuum_into_sql(?,?)",
params![path.as_ref().to_str(), config.dump()],
)
}
fn create_genomic_range_index_sql(
&self,
table_name: &str,
chromosome: &str,
begin_pos: &str,
end_pos: &str,
) -> Result<String> {
self.create_genomic_range_index_sql_with_floor(
table_name, chromosome, begin_pos, end_pos, -1,
)
}
fn create_genomic_range_index_sql_with_floor(
&self,
table_name: &str,
chromosome: &str,
begin_pos: &str,
end_pos: &str,
floor: i64,
) -> Result<String> {
query1str(
self,
"SELECT create_genomic_range_index_sql(?,?,?,?,?)",
params![table_name, chromosome, begin_pos, end_pos, floor],
)
}
fn put_reference_assembly_sql(&self, assembly: &str) -> Result<String> {
self.put_reference_assembly_sql_with_schema(assembly, "")
}
fn put_reference_assembly_sql_with_schema(
&self,
assembly: &str,
schema: &str,
) -> Result<String> {
query1str(
self,
"SELECT put_genomic_reference_assembly_sql(?,?)",
params![assembly, schema],
)
}
fn put_reference_sequence_sql(&self, refseq: &RefSeq) -> Result<String> {
self.put_reference_sequence_sql_with_schema(refseq, "")
}
fn put_reference_sequence_sql_with_schema(
&self,
refseq: &RefSeq,
schema: &str,
) -> Result<String> {
query1str(
self,
"SELECT put_genomic_reference_sequence_sql(?,?,?,?,?,?)",
params![
refseq.length,
refseq.assembly,
refseq.refget_id,
refseq.meta_json.dump(),
refseq.rid,
schema
],
)
}
fn get_reference_sequences_by_rid(&self) -> Result<HashMap<i64, RefSeq>> {
self.get_reference_sequences_by_rid_with_options(None, None)
}
fn get_reference_sequences_by_rid_with_options(
&self,
assembly: Option<&str>,
schema: Option<&str>,
) -> Result<HashMap<i64, RefSeq>> {
let sql = format!(
"SELECT
_gri_rid,
gri_refseq_name, gri_refseq_length,
gri_assembly, gri_refget_id,
gri_refseq_meta_json
FROM {}_gri_refseq{}",
schema.map_or("".to_string(), |s| { s.to_string() + "." }),
assembly.map_or("", |_| " WHERE gri_assembly = ?")
);
let mut stmt = self.prepare(&sql)?;
let assembly_param = params![assembly];
let refseqs = stmt.query_map(assembly.map_or(params![], |_| &assembly_param), |row| {
let meta_json_str: String = row.get(5)?;
Ok(RefSeq {
rid: row.get(0)?,
name: row.get(1)?,
length: row.get(2)?,
assembly: row.get(3)?,
refget_id: row.get(4)?,
meta_json: match json::parse(&meta_json_str.as_str()) {
Ok(json::JsonValue::Object(obj)) => Ok(obj),
_ => Err(rusqlite::Error::ModuleError(
"genomicsqlite::get_reference_sequences_by_rid: invalid meta_json"
.to_string(),
)),
}?,
})
})?;
let mut ans = HashMap::new();
for res in refseqs {
let refseq = res?;
ans.insert(refseq.rid, refseq);
}
Ok(ans)
}
fn get_reference_sequences_by_name(&self) -> Result<HashMap<String, RefSeq>> {
self.get_reference_sequences_by_name_with_options(None, None)
}
fn get_reference_sequences_by_name_with_options(
&self,
assembly: Option<&str>,
schema: Option<&str>,
) -> Result<HashMap<String, RefSeq>> {
let mut ans = HashMap::new();
for (_, refseq) in self
.get_reference_sequences_by_rid_with_options(assembly, schema)?
.drain()
{
ans.insert(refseq.name.clone(), refseq);
}
Ok(ans)
}
}
#[cfg(test)]
mod tests {
use super::ConnectionMethods;
use rusqlite::OpenFlags;
#[test]
fn smoke_test() {
let dbfn = format!(
"/tmp/genomicsqlite_rust_smoke_test.{}.db",
uuid::Uuid::new_v4()
);
let mut config = json::object::Object::new();
config.insert("threads", json::JsonValue::from(3));
let mut conn = super::open(
dbfn,
OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE,
&config,
)
.unwrap();
// check that tuning SQL applied successfully
let mut ans: i64 = conn
.query_row("PRAGMA threads", [], |row| row.get(0))
.unwrap();
assert_eq!(ans, 3);
// begin transaction
{
let txn = conn.transaction().unwrap();
// load table & create GRI
txn.execute_batch(
"CREATE TABLE feature(rid INTEGER, beg INTEGER, end INTEGER);
INSERT INTO feature VALUES(3, 12, 34);
INSERT INTO feature VALUES(3, 0, 23);
INSERT INTO feature VALUES(3, 34, 56)",
)
.unwrap();
let gri_sql = txn
.create_genomic_range_index_sql("feature", "rid", "beg", "end")
.unwrap();
txn.execute_batch(&gri_sql).unwrap();
txn.commit().unwrap();
}
// GRI query
ans = conn
.query_row(
"SELECT COUNT(*) FROM genomic_range_rowids('feature',3,34,34)",
[],
|row| row.get(0),
)
.unwrap();
assert_eq!(ans, 2);
// reference genome stuff
conn.execute_batch(
&conn
.put_reference_assembly_sql("GRCh38_no_alt_analysis_set")
.unwrap(),
)
.unwrap();
let refseqs = conn.get_reference_sequences_by_name().unwrap();
let chr3 = refseqs.get("chr3").unwrap();
assert_eq!(chr3.name, "chr3");
assert_eq!(chr3.rid, 3);
assert_eq!(chr3.length, 198295559);
}
#[test]
fn web_test() {
let config = json::object::Object::new();
let conn = super::open(
"https://github.com/mlin/sqlite_zstd_vfs/releases/download/web-test-db-v1/TxDb.Hsapiens.UCSC.hg38.knownGene.vacuum.genomicsqlite",
OpenFlags::SQLITE_OPEN_READ_ONLY,
&config,
).unwrap();
let ans: i64 = conn
.query_row("SELECT COUNT(1) FROM sqlite_master", [], |row| row.get(0))
.unwrap();
assert_eq!(ans, 12);
}
}