biovault 0.1.37

A bioinformatics data vault CLI tool
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
577
578
579
580
581
582
583
584
585
586
587
588
589
use crate::cli::commands::messages::init_message_system;
use crate::cli::syft_url::SyftURL;
use crate::config::Config;
use crate::error::{Error, Result};
use crate::messages::{Message, MessageType};
use crate::types::{ProjectYaml, SyftPermissions};
use anyhow::Context;
use chrono::Local;
use dialoguer::{Confirm, Editor};
use serde_json::json;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

pub async fn submit(
    project_path: String,
    destination: String,
    non_interactive: bool,
    force: bool,
) -> Result<()> {
    let config = Config::load()?;

    // Parse destination - could be email or full syft URL
    let (datasite_email, participant_url) = if destination.starts_with("syft://") {
        // Full syft URL provided
        let dest_url = SyftURL::parse(&destination)?;
        let email = dest_url.email.clone();
        let participant = if dest_url.fragment.is_some() {
            Some(destination.clone())
        } else {
            None
        };
        (email, participant)
    } else if destination.contains('@') {
        // Just an email/datasite provided
        (destination.clone(), None)
    } else {
        return Err(Error::from(anyhow::anyhow!(
            "Invalid destination: must be either an email address or a syft:// URL"
        )));
    };

    // Determine project directory
    let project_dir = PathBuf::from(&project_path);
    let project_dir = if project_dir.is_relative() && project_path == "." {
        std::env::current_dir()?
    } else {
        project_dir
    };

    if !project_dir.exists() {
        return Err(Error::from(anyhow::anyhow!(
            "Project directory not found: {}",
            project_dir.display()
        )));
    }

    let project_yaml_path = project_dir.join("project.yaml");
    if !project_yaml_path.exists() {
        return Err(Error::from(anyhow::anyhow!(
            "project.yaml not found in: {}",
            project_dir.display()
        )));
    }

    // Load and validate project
    let mut project = ProjectYaml::from_file(&project_yaml_path)?;

    // Override security-sensitive fields
    project.author = config.email.clone();
    project.datasites = Some(vec![datasite_email.clone()]);

    // Handle participants from destination URL
    project.participants = participant_url.clone().map(|url| vec![url]);

    // Hash workflow.nf
    let workflow_path = project_dir.join("workflow.nf");
    if !workflow_path.exists() {
        return Err(Error::from(anyhow::anyhow!(
            "workflow.nf not found in project directory"
        )));
    }
    let workflow_hash = hash_file(&workflow_path)?;

    // Collect and hash asset files
    let assets_dir = project_dir.join("assets");
    let mut asset_files = Vec::new();
    let mut b3_hashes = HashMap::new();

    // Add workflow hash
    b3_hashes.insert("workflow.nf".to_string(), workflow_hash.clone());

    if assets_dir.exists() && assets_dir.is_dir() {
        for entry in WalkDir::new(&assets_dir)
            .min_depth(1)
            .follow_links(false)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            let path = entry.path();
            // Relative path from project root (for hashes)
            let relative_path = path
                .strip_prefix(&project_dir)
                .unwrap_or(path)
                .to_string_lossy()
                .to_string();
            // Relative path from assets dir (for YAML assets list)
            let assets_rel = path
                .strip_prefix(&assets_dir)
                .unwrap_or(path)
                .to_string_lossy()
                .to_string();

            asset_files.push(assets_rel);
            let file_hash = hash_file(path)?;
            b3_hashes.insert(relative_path, file_hash);
        }
    }

    project.assets = if asset_files.is_empty() {
        None
    } else {
        Some(asset_files)
    };
    project.b3_hashes = Some(b3_hashes);

    // Calculate project hash from workflow.nf and assets only (deterministic)
    let mut hash_content = String::new();
    hash_content.push_str(&project.name);
    hash_content.push_str(&workflow_hash);
    if let Some(ref hashes) = project.b3_hashes {
        let mut sorted_hashes: Vec<_> = hashes.iter().collect();
        sorted_hashes.sort_by_key(|k| k.0);
        for (file, hash) in sorted_hashes {
            hash_content.push_str(file);
            hash_content.push_str(hash);
        }
    }
    let project_hash = blake3::hash(hash_content.as_bytes()).to_hex().to_string();

    // Create submission folder name
    let date_str = Local::now().format("%Y-%m-%d").to_string();
    let short_hash = &project_hash[0..8];
    let submission_folder_name = format!("{}-{}-{}", project.name, date_str, short_hash);

    // Create submission path
    let submissions_path = config.get_shared_submissions_path()?;
    fs::create_dir_all(&submissions_path)?;

    let submission_path = submissions_path.join(&submission_folder_name);

    // Check if already submitted
    if submission_path.exists() {
        // Verify the hash matches using the same deterministic method
        let existing_project_yaml = submission_path.join("project.yaml");
        if existing_project_yaml.exists() {
            let existing_project = ProjectYaml::from_file(&existing_project_yaml)?;

            // Calculate existing project hash using same method
            let mut existing_hash_content = String::new();
            existing_hash_content.push_str(&existing_project.name);

            // Get workflow hash from existing submission
            if let Some(ref existing_hashes) = existing_project.b3_hashes {
                if let Some(existing_workflow_hash) = existing_hashes.get("workflow.nf") {
                    existing_hash_content.push_str(existing_workflow_hash);

                    let mut sorted_hashes: Vec<_> = existing_hashes.iter().collect();
                    sorted_hashes.sort_by_key(|k| k.0);
                    for (file, hash) in sorted_hashes {
                        existing_hash_content.push_str(file);
                        existing_hash_content.push_str(hash);
                    }
                }
            }

            let existing_hash = blake3::hash(existing_hash_content.as_bytes())
                .to_hex()
                .to_string();

            if existing_hash == project_hash {
                if !force {
                    println!("⚠️  This exact project version has already been submitted.");
                    println!("   Location: {}", submission_path.display());
                    println!("   Hash: {}", short_hash);
                    println!("   Use --force to resend the message.");
                    return Ok(());
                } else {
                    println!(
                        "ℹ️  Project already submitted, but --force flag used. Sending message..."
                    );
                    println!("   Location: {}", submission_path.display());
                    println!("   Hash: {}", short_hash);

                    // Skip to message sending part since files already exist
                    // Build the syft URL and send the message
                    let datasite_root = config.get_datasite_path()?;
                    let rel_from_datasite = submission_path
                        .strip_prefix(&datasite_root)
                        .unwrap_or(&submission_path)
                        .to_string_lossy()
                        .to_string();
                    let submission_syft_url =
                        format!("syft://{}/{}", config.email, rel_from_datasite);

                    // Use project data from existing submission
                    let default_body = "I would like to run the following project.".to_string();
                    let mut body = if non_interactive {
                        default_body.clone()
                    } else {
                        let use_custom = Confirm::new()
                            .with_prompt("Write a custom message body?")
                            .default(false)
                            .interact()
                            .unwrap_or(false);

                        if use_custom {
                            match Editor::new().edit(&default_body) {
                                Ok(Some(content)) if !content.trim().is_empty() => content,
                                _ => default_body.clone(),
                            }
                        } else {
                            default_body.clone()
                        }
                    };

                    let sender_local_path = submission_path.to_string_lossy().to_string();
                    let receiver_local_path_template = format!(
                        "$SYFTBOX_DATA_DIR/datasites/{}/shared/biovault/submissions/{}",
                        config.email, submission_folder_name
                    );
                    body.push_str(&format!(
                        "\n\nSubmission location references:\n- syft URL: {}\n- Sender local path: {}\n- Receiver local path (template): {}\n",
                        submission_syft_url, sender_local_path, receiver_local_path_template
                    ));

                    let metadata = json!({
                        "project": existing_project,
                        "project_location": submission_syft_url,
                        "participants": "With your participants: ALL",
                        "date": date_str,
                        "assets": existing_project.assets.clone().unwrap_or_default(),
                        "sender_local_path": sender_local_path,
                        "receiver_local_path_template": receiver_local_path_template,
                    });

                    let (db, sync) = init_message_system(&config)?;

                    let mut msg = Message::new(config.email.clone(), datasite_email.clone(), body);
                    msg.subject = Some(format!("Project Request - {}", existing_project.name));
                    msg.message_type = MessageType::Project {
                        project_name: existing_project.name.clone(),
                        submission_id: submission_folder_name.clone(),
                        files_hash: Some(existing_hash.clone()),
                    };
                    msg.metadata = Some(metadata);

                    db.insert_message(&msg)?;
                    let _ = sync.send_message(&msg.id);

                    println!("✉️  Project message prepared for {}", datasite_email);
                    if let Some(subj) = &msg.subject {
                        println!("  Subject: {}", subj);
                    }
                    println!("  Submission URL: {}", submission_syft_url);

                    return Ok(());
                }
            }
        }

        return Err(Error::from(anyhow::anyhow!(
            "A submission with this name and date already exists but with different content: {}",
            submission_path.display()
        )));
    }

    // Create submission directory
    fs::create_dir_all(&submission_path)?;

    // Copy project files
    copy_project_files(&project_dir, &submission_path)?;

    // Save updated project.yaml
    project.save(&submission_path.join("project.yaml"))?;

    // Create permissions file
    let permissions = SyftPermissions::new_for_datasite(&datasite_email);
    let permissions_path = submission_path.join("syft.pub.yaml");
    permissions.save(&permissions_path)?;

    println!("✓ Project submitted successfully!");
    println!("  Name: {}", project.name);
    println!("  To: {}", datasite_email);
    if let Some(participants) = &project.participants {
        println!("  Participants: {}", participants.join(", "));
    }
    println!("  Location: {}", submission_path.display());
    println!("  Hash: {}", short_hash);

    // Build a syft:// URL for the saved submission so the receiver can locate it
    let datasite_root = config.get_datasite_path()?;
    let rel_from_datasite = submission_path
        .strip_prefix(&datasite_root)
        .unwrap_or(&submission_path)
        .to_string_lossy()
        .to_string();
    let submission_syft_url = format!("syft://{}/{}", config.email, rel_from_datasite);

    // Prepare default message body and allow user to override
    let default_body = "I would like to run the following project.".to_string();
    let mut body = if non_interactive {
        // In non-interactive mode, use default message
        default_body.clone()
    } else {
        let use_custom = Confirm::new()
            .with_prompt("Write a custom message body?")
            .default(false)
            .interact()
            .unwrap_or(false);

        if use_custom {
            match Editor::new().edit(&default_body) {
                Ok(Some(content)) if !content.trim().is_empty() => content,
                _ => default_body.clone(),
            }
        } else {
            default_body.clone()
        }
    };

    // Add handy paths for the recipient to copy/paste
    let sender_local_path = submission_path.to_string_lossy().to_string();
    let receiver_local_path_template = format!(
        "$SYFTBOX_DATA_DIR/datasites/{}/shared/biovault/submissions/{}",
        config.email, submission_folder_name
    );
    body.push_str(&format!(
        "\n\nSubmission location references:\n- syft URL: {}\n- Sender local path: {}\n- Receiver local path (template): {}\n",
        submission_syft_url, sender_local_path, receiver_local_path_template
    ));

    // Construct metadata for the message
    let metadata = json!({
        "project": project,
        "project_location": submission_syft_url,
        // Receiver guidance about which of their participants to use
        "participants": "With your participants: ALL",
        // Date component used in the submission folder name
        "date": date_str,
        // Explicit list of asset files (if any)
        "assets": project.assets.clone().unwrap_or_default(),
        // Helpful paths for receiver tooling
        "sender_local_path": sender_local_path,
        "receiver_local_path_template": receiver_local_path_template,
    });

    // Initialize messaging system and send a project message
    let (db, sync) = init_message_system(&config)?;

    let mut msg = Message::new(config.email.clone(), datasite_email.clone(), body);
    msg.subject = Some(format!("Project Request - {}", project.name));
    msg.message_type = MessageType::Project {
        project_name: project.name.clone(),
        submission_id: submission_folder_name.clone(),
        files_hash: Some(project_hash.clone()),
    };
    msg.metadata = Some(metadata);

    db.insert_message(&msg)?;
    // Try to send immediately; if offline, it will remain queued locally
    let _ = sync.send_message(&msg.id);

    println!("✉️  Project message prepared for {}", datasite_email);
    if let Some(subj) = &msg.subject {
        println!("  Subject: {}", subj);
    }
    println!("  Submission URL: {}", submission_syft_url);

    Ok(())
}

fn hash_file(path: &Path) -> Result<String> {
    let content =
        fs::read(path).with_context(|| format!("Failed to read file: {}", path.display()))?;
    Ok(blake3::hash(&content).to_hex().to_string())
}

fn copy_project_files(src: &Path, dest: &Path) -> Result<()> {
    // Copy workflow.nf
    let src_workflow = src.join("workflow.nf");
    let dest_workflow = dest.join("workflow.nf");
    fs::copy(&src_workflow, &dest_workflow)
        .with_context(|| "Failed to copy workflow.nf".to_string())?;

    // Copy assets directory if it exists
    let src_assets = src.join("assets");
    if src_assets.exists() && src_assets.is_dir() {
        let dest_assets = dest.join("assets");
        fs::create_dir_all(&dest_assets)?;

        for entry in WalkDir::new(&src_assets)
            .min_depth(1)
            .follow_links(false)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let src_path = entry.path();
            let relative_path = src_path.strip_prefix(&src_assets).unwrap();
            let dest_path = dest_assets.join(relative_path);

            if entry.file_type().is_dir() {
                fs::create_dir_all(&dest_path)?;
            } else {
                if let Some(parent) = dest_path.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::copy(src_path, &dest_path)
                    .with_context(|| format!("Failed to copy file: {}", src_path.display()))?;
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn hash_file_computes_blake3() {
        let tmp = TempDir::new().unwrap();
        let f = tmp.path().join("a.txt");
        fs::write(&f, b"content").unwrap();
        let h = hash_file(&f).unwrap();
        assert_eq!(h.len(), 64);
    }

    #[test]
    fn copy_project_files_copies_workflow_and_assets() {
        let tmp = TempDir::new().unwrap();
        let src = tmp.path().join("src");
        let dest = tmp.path().join("dest");
        fs::create_dir_all(&dest).unwrap();
        fs::create_dir_all(src.join("assets/nested")).unwrap();
        fs::write(src.join("workflow.nf"), b"wf").unwrap();
        fs::write(src.join("assets/nested/file.bin"), b"x").unwrap();

        copy_project_files(&src, &dest).unwrap();

        assert!(dest.join("workflow.nf").exists());
        assert!(dest.join("assets/nested/file.bin").exists());

        // No assets dir case should still succeed
        let src2 = tmp.path().join("src2");
        fs::create_dir_all(&src2).unwrap();
        fs::write(src2.join("workflow.nf"), b"wf").unwrap();
        let dest2 = tmp.path().join("dest2");
        fs::create_dir_all(&dest2).unwrap();
        copy_project_files(&src2, &dest2).unwrap();
    }

    #[test]
    fn test_hash_file_with_different_content() {
        let tmp = TempDir::new().unwrap();

        let file1 = tmp.path().join("file1.txt");
        fs::write(&file1, b"content1").unwrap();
        let hash1 = hash_file(&file1).unwrap();

        let file2 = tmp.path().join("file2.txt");
        fs::write(&file2, b"content2").unwrap();
        let hash2 = hash_file(&file2).unwrap();

        // Different content should produce different hashes
        assert_ne!(hash1, hash2);

        // Same content should produce same hash
        let file3 = tmp.path().join("file3.txt");
        fs::write(&file3, b"content1").unwrap();
        let hash3 = hash_file(&file3).unwrap();
        assert_eq!(hash1, hash3);
    }

    #[test]
    fn test_hash_file_empty_file() {
        let tmp = TempDir::new().unwrap();
        let empty_file = tmp.path().join("empty.txt");
        fs::write(&empty_file, b"").unwrap();
        let hash = hash_file(&empty_file).unwrap();
        // Blake3 hash of empty string is a known value
        assert_eq!(hash.len(), 64);
    }

    #[test]
    fn test_copy_project_files_missing_workflow() {
        let tmp = TempDir::new().unwrap();
        let src = tmp.path().join("src");
        let dest = tmp.path().join("dest");
        fs::create_dir_all(&src).unwrap();
        fs::create_dir_all(&dest).unwrap();
        // No workflow.nf file

        let result = copy_project_files(&src, &dest);
        assert!(result.is_err());
    }

    #[test]
    fn test_copy_project_files_with_symlinks() {
        let tmp = TempDir::new().unwrap();
        let src = tmp.path().join("src");
        let dest = tmp.path().join("dest");

        fs::create_dir_all(&src).unwrap();
        fs::create_dir_all(&dest).unwrap();
        fs::create_dir_all(src.join("assets")).unwrap();

        fs::write(src.join("workflow.nf"), b"workflow").unwrap();
        fs::write(src.join("assets/real_file.txt"), b"real").unwrap();

        // Create a symlink (this test will only work on Unix-like systems)
        #[cfg(unix)]
        {
            use std::os::unix::fs::symlink;
            let _ = symlink(
                src.join("assets/real_file.txt"),
                src.join("assets/link_file.txt"),
            );
        }

        let result = copy_project_files(&src, &dest);
        assert!(result.is_ok());
        assert!(dest.join("workflow.nf").exists());
    }

    #[test]
    fn test_copy_project_files_deeply_nested() {
        let tmp = TempDir::new().unwrap();
        let src = tmp.path().join("src");
        let dest = tmp.path().join("dest");

        fs::create_dir_all(&dest).unwrap();
        fs::create_dir_all(src.join("assets/a/b/c/d")).unwrap();
        fs::write(src.join("workflow.nf"), b"wf").unwrap();
        fs::write(src.join("assets/a/b/c/d/deep.txt"), b"deep").unwrap();

        copy_project_files(&src, &dest).unwrap();

        assert!(dest.join("workflow.nf").exists());
        assert!(dest.join("assets/a/b/c/d/deep.txt").exists());

        let content = fs::read(dest.join("assets/a/b/c/d/deep.txt")).unwrap();
        assert_eq!(content, b"deep");
    }

    #[test]
    fn test_copy_project_files_preserves_content() {
        let tmp = TempDir::new().unwrap();
        let src = tmp.path().join("src");
        let dest = tmp.path().join("dest");

        fs::create_dir_all(&dest).unwrap();
        fs::create_dir_all(src.join("assets")).unwrap();

        let workflow_content = b"nextflow.enable.dsl=2\nworkflow { }";
        let asset_content = b"important data";

        fs::write(src.join("workflow.nf"), workflow_content).unwrap();
        fs::write(src.join("assets/data.csv"), asset_content).unwrap();

        copy_project_files(&src, &dest).unwrap();

        let copied_workflow = fs::read(dest.join("workflow.nf")).unwrap();
        let copied_asset = fs::read(dest.join("assets/data.csv")).unwrap();

        assert_eq!(copied_workflow, workflow_content);
        assert_eq!(copied_asset, asset_content);
    }

    #[test]
    fn test_hash_file_nonexistent() {
        let result = hash_file(Path::new("/nonexistent/file.txt"));
        assert!(result.is_err());
    }
}