@genobank/biofs 3.20.8 → 3.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.loadObjects = loadObjects;
40
+ exports.validateObject = validateObject;
41
+ exports.buildOnrampBody = buildOnrampBody;
42
+ exports.onrampHttpsSubmitCommand = onrampHttpsSubmitCommand;
43
+ exports.onrampHttpsStatusCommand = onrampHttpsStatusCommand;
44
+ /**
45
+ * biofs onramp-https submit --owner <biowallet> --serial <biosample serial> --case-id <case id> --originlab <lab or source tag> --manifest <manifest.json> [--wait]
46
+ * biofs onramp-https status <job> [--wait]
47
+ *
48
+ * CLIENT verb. biofs-node streams each public HTTPS object into the BioRouter vault under the owner biowallet (a custodial biowallet is claimable) and registers
49
+ * its biocid in biocid_registry and bioroutes.inventory. No biodata passes through this machine and none is staged on the node disk. Every object in the manifest
50
+ * carries the SHA-256 and byte size its source published; the node registers an object only when the streamed bytes reproduce both, otherwise it deletes the copy.
51
+ * The node refuses non-https sources, credentials in a URL, redirects and hosts that resolve to private addresses. The caller must be a node operator or the owner.
52
+ *
53
+ * Manifest: {"objects": [{"url": "https://...", "sha256": "<64 hex>", "bytes": 123, "dataset": "optional name"}]}
54
+ * (A source catalog works too: pass --base-url and --key-prefix, and each object with "key", "sha256", "bytes" becomes base-url/key.)
55
+ */
56
+ const fs = __importStar(require("fs"));
57
+ const axios_1 = __importDefault(require("axios"));
58
+ const chalk_1 = __importDefault(require("chalk"));
59
+ const credentials_1 = require("../../lib/auth/credentials");
60
+ const logger_1 = require("../../lib/utils/logger");
61
+ const BIOFS_NODE_BASE = process.env.BIOFS_NODE_URL
62
+ || `${process.env.GENOBANK_API_URL || 'https://genobank.app'}/api_biofs_node`;
63
+ function loadObjects(manifestPath, baseUrl, keyPrefix) {
64
+ const doc = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
65
+ const list = Array.isArray(doc) ? doc : (doc.objects || []);
66
+ const out = [];
67
+ for (const o of list) {
68
+ let url = o.url || '';
69
+ if (!url && o.key && baseUrl) {
70
+ if (keyPrefix && !String(o.key).startsWith(keyPrefix))
71
+ continue;
72
+ url = `${baseUrl.replace(/\/+$/, '')}/${String(o.key).split('/').map(encodeURIComponent).join('/')}`;
73
+ }
74
+ if (!url)
75
+ continue;
76
+ out.push(validateObject({ url, sha256: String(o.sha256 || ''), bytes: Number(o.bytes ?? o.size), dataset: o.dataset }));
77
+ }
78
+ if (out.length < 1 || out.length > 50)
79
+ throw new Error(`the manifest selects ${out.length} objects; 1 to 50 are accepted`);
80
+ return out;
81
+ }
82
+ function validateObject(o) {
83
+ let u;
84
+ try {
85
+ u = new URL(o.url);
86
+ }
87
+ catch {
88
+ throw new Error(`not a url: ${o.url.slice(0, 80)}`);
89
+ }
90
+ if (u.protocol !== 'https:')
91
+ throw new Error('only https sources are accepted');
92
+ if (u.username || u.password)
93
+ throw new Error('a url with credentials is never accepted');
94
+ if (!/^[0-9a-f]{64}$/i.test(o.sha256))
95
+ throw new Error(`${u.pathname}: the published sha256 is required`);
96
+ if (!Number.isInteger(o.bytes) || o.bytes <= 0)
97
+ throw new Error(`${u.pathname}: the byte size is required`);
98
+ return { url: u.toString(), sha256: o.sha256.toLowerCase(), bytes: o.bytes, dataset: o.dataset };
99
+ }
100
+ function buildOnrampBody(o, wallet, signature) {
101
+ if (!/^0x[0-9a-fA-F]{40}$/.test(o.owner))
102
+ throw new Error('--owner must be an EIP-55 biowallet address');
103
+ if (!o.serial || o.serial.toUpperCase() === 'UNKNOWN' || !/^[A-Za-z0-9._-]{1,64}$/.test(o.serial))
104
+ throw new Error('--serial must be the origin biosample serial (never UNKNOWN)');
105
+ if (!/^[A-Za-z0-9_-]{1,64}$/.test(o.caseId))
106
+ throw new Error('--case-id must be a case identifier of letters, digits, _ or - (never a person name)');
107
+ if (!/^[a-z0-9_-]{2,40}$/.test(o.originlab))
108
+ throw new Error('--originlab must be 2 to 40 lowercase letters, digits, _ or -');
109
+ return { wallet, signature, owner: o.owner, serial: o.serial, case_id: o.caseId, originlab: o.originlab, objects: loadObjects(o.manifest, o.baseUrl, o.keyPrefix) };
110
+ }
111
+ async function auth() {
112
+ const c = await (0, credentials_1.getCredentials)();
113
+ if (!c)
114
+ throw new Error('Not authenticated. Run "biofs login" first (an operator profile for a custodial biowallet).');
115
+ return { wallet: c.wallet_address, signature: c.user_signature, headers: { 'X-Biofs-Wallet': c.wallet_address, 'X-Biofs-Signature': c.user_signature } };
116
+ }
117
+ async function fetchStatus(id, headers) {
118
+ const r = await axios_1.default.get(`${BIOFS_NODE_BASE}/https_onramp_status`, { params: { onramp_job_id: id }, headers, timeout: 60000, validateStatus: (s) => s < 500 });
119
+ if (r.status >= 400)
120
+ throw new Error(`status ${r.status}: ${r.data?.error || 'unknown'}`);
121
+ return r.data;
122
+ }
123
+ function summary(row) {
124
+ console.log(`${chalk_1.default.cyan('Job:')} ${row.onramp_job_id} ${row.status}`);
125
+ if (row.current)
126
+ console.log(`${chalk_1.default.cyan('Now:')} ${row.current.dataset} ${(row.current.bytes_done / 1e9).toFixed(2)} of ${(row.current.bytes_total / 1e9).toFixed(2)} GB at ${row.current.mb_per_s} MB/s`);
127
+ console.log(`${chalk_1.default.cyan('Objects:')} ${row.n_files || 0} of ${(row.objects || []).length || row.n_listed || '?'} verified and registered`);
128
+ for (const f of row.files || [])
129
+ console.log(` ${f.biocid}`);
130
+ if (row.error)
131
+ console.log(`${chalk_1.default.red('Error:')} ${row.error}`);
132
+ }
133
+ async function waitFor(id, headers, json) {
134
+ for (;;) {
135
+ const row = await fetchStatus(id, headers);
136
+ if (row.status === 'done' || row.status === 'failed') {
137
+ if (json)
138
+ console.log(JSON.stringify(row, null, 2));
139
+ else
140
+ summary(row);
141
+ if (row.status === 'failed')
142
+ process.exit(1);
143
+ return;
144
+ }
145
+ await new Promise((res) => setTimeout(res, 30000));
146
+ }
147
+ }
148
+ async function onrampHttpsSubmitCommand(options) {
149
+ try {
150
+ const a = await auth();
151
+ const body = buildOnrampBody(options, a.wallet, a.signature);
152
+ const r = await axios_1.default.post(`${BIOFS_NODE_BASE}/submit_https_onramp`, body, { headers: a.headers, timeout: 60000, validateStatus: (s) => s < 500 });
153
+ if (r.status >= 400)
154
+ throw new Error(`${r.status}: ${r.data?.error || 'unknown'}`);
155
+ const id = r.data.onramp_job_id;
156
+ if (!options.wait) {
157
+ if (options.json)
158
+ console.log(JSON.stringify(r.data, null, 2));
159
+ else {
160
+ console.log(chalk_1.default.green(`onramp_job_id=${id} status=${r.data.status} objects=${r.data.n_objects}`));
161
+ console.log(chalk_1.default.gray('Monitor: ') + chalk_1.default.cyan(`biofs onramp-https status ${id} --wait`));
162
+ }
163
+ return;
164
+ }
165
+ await waitFor(id, a.headers, options.json);
166
+ }
167
+ catch (e) {
168
+ logger_1.Logger.error(`onramp-https submit failed: ${e.message}`);
169
+ process.exit(1);
170
+ }
171
+ }
172
+ async function onrampHttpsStatusCommand(id, options) {
173
+ try {
174
+ const a = await auth();
175
+ if (options.wait)
176
+ return await waitFor(id, a.headers, options.json);
177
+ const row = await fetchStatus(id, a.headers);
178
+ if (options.json)
179
+ console.log(JSON.stringify(row, null, 2));
180
+ else
181
+ summary(row);
182
+ }
183
+ catch (e) {
184
+ logger_1.Logger.error(`onramp-https status failed: ${e.message}`);
185
+ process.exit(1);
186
+ }
187
+ }
188
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/onramp-https/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,kCAeC;AAED,wCAQC;AAED,0CAMC;AAkCD,4DAcC;AAED,4DAOC;AArHD;;;;;;;;;;;GAWG;AACH,uCAAyB;AACzB,kDAA0B;AAC1B,kDAA0B;AAC1B,4DAA4D;AAC5D,mDAAgD;AAEhD,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc;OAC7C,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,sBAAsB,iBAAiB,CAAC;AAQhF,SAAgB,WAAW,CAAC,YAAoB,EAAE,OAAgB,EAAE,SAAkB;IACpF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9D,MAAM,IAAI,GAAU,KAAK,CAAC,OAAO,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AAC,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QACpE,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;QAC7C,IAAI,OAAO,CAAC,IAAI;YAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;;YAAM,OAAO,CAAC,GAAG,CAAC,CAAC;IACjF,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAAC,eAAM,CAAC,KAAK,CAAC,+BAA+B,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;QAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAAC,CAAC;AACjG,CAAC"}
@@ -0,0 +1,18 @@
1
+ export interface RegisterDerivedGcsOptions {
2
+ type: string;
3
+ parent: string[];
4
+ caseId?: string;
5
+ filename?: string;
6
+ category?: string;
7
+ json?: boolean;
8
+ }
9
+ export declare function buildRegisterDerivedGcsBody(gcsUri: string, o: RegisterDerivedGcsOptions): Record<string, unknown>;
10
+ export declare function registerDerivedGcsCommand(gcsUri: string, options: RegisterDerivedGcsOptions): Promise<void>;
11
+ export declare function validateReadArgs(biocid: string, out?: string): void;
12
+ export declare function sha256Hex(buf: Buffer): string;
13
+ /** Read a small derived biofile of your own back through biofs-node (owner match, consent not revoked, 5 MB cap, SHA-256 checked by the node);
14
+ * the bytes are checked here against the node's X-Sha256 header before anything is written. Primary biodata is never served this way. */
15
+ export declare function readDerivedCommand(biocid: string, options: {
16
+ out?: string;
17
+ }): Promise<void>;
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/commands/register-derived/index.ts"],"names":[],"mappings":"AAmBA,MAAM,WAAW,yBAAyB;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAID,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,EAAE,yBAAyB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAWjH;AAED,wBAAsB,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBjH;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAGnE;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;0IAC0I;AAC1I,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA0BjG"}
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.buildRegisterDerivedGcsBody = buildRegisterDerivedGcsBody;
40
+ exports.registerDerivedGcsCommand = registerDerivedGcsCommand;
41
+ exports.validateReadArgs = validateReadArgs;
42
+ exports.sha256Hex = sha256Hex;
43
+ exports.readDerivedCommand = readDerivedCommand;
44
+ /**
45
+ * biofs register-derived gcs <gs-uri> --type <biodata type> --parent <biocid...> [--case-id <id>] [--filename <name>] [--category dna|rna|clinical]
46
+ *
47
+ * CLIENT verb. Gives a biocid, with lineage, to a job output that already sits in the signer's own GCS prefix
48
+ * (gs://genobank-parabricks-output/biowallet/<owner>/... or gs://genobank-vault/<owner>/...). biofs-node hashes the object on
49
+ * the server and registers it IN PLACE in biorouter.biocid_registry and bioroutes.inventory; no byte moves and none reaches this
50
+ * client. Every parent biocid must be owned by the signer and not revoked or erased. The node refuses an object outside the
51
+ * signer's own prefix. Route: POST /api_biofs_node/register_derived_gcs.
52
+ */
53
+ const axios_1 = __importDefault(require("axios"));
54
+ const crypto = __importStar(require("crypto"));
55
+ const fs = __importStar(require("fs"));
56
+ const chalk_1 = __importDefault(require("chalk"));
57
+ const credentials_1 = require("../../lib/auth/credentials");
58
+ const logger_1 = require("../../lib/utils/logger");
59
+ const BIOFS_NODE_BASE = process.env.BIOFS_NODE_URL
60
+ || `${process.env.GENOBANK_API_URL || 'https://genobank.app'}/api_biofs_node`;
61
+ const BIOCID_RE = /^biocid(:\/\/|:)[A-Za-z0-9_-]+\/0x[0-9a-fA-F]{40}\/[A-Za-z0-9_-]+\/\S+$/i;
62
+ function buildRegisterDerivedGcsBody(gcsUri, o) {
63
+ if (!/^gs:\/\/[a-z0-9._-]+\/[^\s]+$/.test(gcsUri))
64
+ throw new Error('the argument must be a gs:// object URI inside your own biowallet prefix');
65
+ if (!/^[a-z0-9][a-z0-9_-]{1,40}$/.test(o.type || ''))
66
+ throw new Error('--type must be lowercase letters, digits, - or _ (for example vcf, annotation, report)');
67
+ const parents = (o.parent || []).map((p) => p.trim()).filter(Boolean);
68
+ if (parents.length < 1 || parents.length > 8)
69
+ throw new Error('--parent: 1 to 8 parent biocids are required (a derived biofile always carries its lineage)');
70
+ for (const p of parents)
71
+ if (!BIOCID_RE.test(p))
72
+ throw new Error(`--parent "${p}" is not a biocid`);
73
+ const filename = o.filename || gcsUri.split('/').pop() || '';
74
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,120}$/.test(filename))
75
+ throw new Error('--filename has unsupported characters');
76
+ if (o.caseId && !/^[A-Za-z0-9_-]{1,64}$/.test(o.caseId))
77
+ throw new Error('--case-id must be a case identifier (never a person name)');
78
+ if (o.category && !/^(dna|rna|clinical|imaging|protein)$/.test(o.category))
79
+ throw new Error('--category must be dna, rna, clinical, imaging or protein');
80
+ return { gcs_uri: gcsUri, filename, biodata_type: o.type, parent_biocids: parents, case_id: o.caseId || '', data_category: o.category || 'dna' };
81
+ }
82
+ async function registerDerivedGcsCommand(gcsUri, options) {
83
+ try {
84
+ const credentials = await (0, credentials_1.getCredentials)();
85
+ if (!credentials)
86
+ throw new Error('Not authenticated. Run "biofs login" first.');
87
+ const body = buildRegisterDerivedGcsBody(gcsUri, options);
88
+ const r = await axios_1.default.post(`${BIOFS_NODE_BASE}/register_derived_gcs`, body, {
89
+ headers: { 'X-Biofs-Wallet': credentials.wallet_address, 'X-Biofs-Signature': credentials.user_signature },
90
+ timeout: 660000,
91
+ validateStatus: (s) => s < 500,
92
+ });
93
+ if (r.status >= 400)
94
+ throw new Error(`${r.status}: ${r.data?.error || 'unknown'}`);
95
+ if (options.json) {
96
+ console.log(JSON.stringify(r.data, null, 2));
97
+ return;
98
+ }
99
+ console.log(chalk_1.default.green(`registered ${r.data.biocid}`));
100
+ console.log(` sha256 ${r.data.sha256} size ${r.data.size} consent ${r.data.consent_status}`);
101
+ }
102
+ catch (e) {
103
+ logger_1.Logger.error(`register-derived gcs failed: ${e.message}`);
104
+ process.exit(1);
105
+ }
106
+ }
107
+ function validateReadArgs(biocid, out) {
108
+ if (!BIOCID_RE.test(biocid || ''))
109
+ throw new Error('the argument must be a biocid (biocid://<bioagent>/<owner wallet>/<biodata type>/<dataset>)');
110
+ if (out && !/^[^\0]+$/.test(out))
111
+ throw new Error('--out must be a file path');
112
+ }
113
+ function sha256Hex(buf) {
114
+ return crypto.createHash('sha256').update(buf).digest('hex');
115
+ }
116
+ /** Read a small derived biofile of your own back through biofs-node (owner match, consent not revoked, 5 MB cap, SHA-256 checked by the node);
117
+ * the bytes are checked here against the node's X-Sha256 header before anything is written. Primary biodata is never served this way. */
118
+ async function readDerivedCommand(biocid, options) {
119
+ try {
120
+ validateReadArgs(biocid, options.out);
121
+ const credentials = await (0, credentials_1.getCredentials)();
122
+ if (!credentials)
123
+ throw new Error('Not authenticated. Run "biofs login" first.');
124
+ const r = await axios_1.default.get(`${BIOFS_NODE_BASE}/derived_read`, {
125
+ params: { biocid },
126
+ headers: { 'X-Biofs-Wallet': credentials.wallet_address, 'X-Biofs-Signature': credentials.user_signature },
127
+ timeout: 120000, responseType: 'arraybuffer', validateStatus: (s) => s < 500,
128
+ });
129
+ if (r.status >= 400)
130
+ throw new Error(`${r.status}: ${Buffer.from(r.data).toString('utf8').slice(0, 200)}`);
131
+ const body = Buffer.from(r.data);
132
+ const got = sha256Hex(body);
133
+ const want = String(r.headers['x-sha256'] || '');
134
+ if (!want)
135
+ throw new Error('the node sent no X-Sha256: refusing to trust unverified bytes');
136
+ if (got !== want)
137
+ throw new Error(`SHA-256 mismatch: node says ${want}, received ${got}`);
138
+ if (options.out) {
139
+ fs.writeFileSync(options.out, body, { mode: 0o600 });
140
+ console.log(`${chalk_1.default.green('saved')} ${options.out} sha256 ${got} ${body.length} bytes`);
141
+ }
142
+ else {
143
+ process.stdout.write(body.toString('utf8'));
144
+ }
145
+ }
146
+ catch (e) {
147
+ logger_1.Logger.error(`register-derived read failed: ${e.message}`);
148
+ process.exit(1);
149
+ }
150
+ }
151
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/register-derived/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,kEAWC;AAED,8DAkBC;AAED,4CAGC;AAED,8BAEC;AAID,gDA0BC;AApGD;;;;;;;;GAQG;AACH,kDAA0B;AAC1B,+CAAiC;AACjC,uCAAyB;AACzB,kDAA0B;AAC1B,4DAA4D;AAC5D,mDAAgD;AAEhD,MAAM,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc;OAC7C,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,sBAAsB,iBAAiB,CAAC;AAWhF,MAAM,SAAS,GAAG,0EAA0E,CAAC;AAE7F,SAAgB,2BAA2B,CAAC,MAAc,EAAE,CAA4B;IACtF,IAAI,CAAC,+BAA+B,CAAC,IAAI,CAAC,MAAM,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAC;IAC/I,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,wFAAwF,CAAC,CAAC;IAChK,MAAM,OAAO,GAAG,CAAC,CAAC,CAAC,MAAM,IAAI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398284) in both staged BAMs and writes the alt fractions to the run log, so the run
14
14
  * itself documents which BAM carries the tumor signal.
15
15
  *
16
+ * Large pairs: a normal BAM above --stream-threshold-gb (default 60), or --stream-normal, is NOT copied: it is read in place through a read-only gcsfuse mount (the executor disk
17
+ * cannot hold a 143 GB whole-genome normal next to the tumor, and genomic bytes are read through gcsfuse by rule). --interval-file (gs:// BED) restricts calling to the territory the
18
+ * tumor assay covers, which is what makes a whole-genome normal against an exome tumor practical. The reference build is detected from the tumor header (hg38 when chr1 is 248,956,422).
19
+ *
16
20
  * AUDIT: gs://<OUT>/biowallet/<WALLET_LC>/somatic-mutect/<JOB_ID>/
17
21
  */
18
22
  export interface SomaticMutectExecOptions {
@@ -20,6 +24,9 @@ export interface SomaticMutectExecOptions {
20
24
  tumorBam: string;
21
25
  normalBam: string;
22
26
  refFasta?: string;
27
+ intervalFile?: string;
28
+ streamNormal?: boolean;
29
+ streamThresholdGb?: string;
23
30
  lowMemory?: boolean;
24
31
  hotspot?: string;
25
32
  jobId?: string;
@@ -28,5 +35,15 @@ export interface SomaticMutectExecOptions {
28
35
  outBucket?: string;
29
36
  refBucket?: string;
30
37
  }
38
+ export declare function splitGs(uri: string): {
39
+ bucket: string;
40
+ object: string;
41
+ dir: string;
42
+ file: string;
43
+ };
44
+ export declare function detectBuild(chr1Length: number | null): 'hg38' | 'b37' | 'unknown';
45
+ export declare function shouldStream(opts: {
46
+ streamNormal?: boolean;
47
+ }, sizeBytes: number, thresholdGb: number): boolean;
31
48
  export declare function somaticMutectExecCommand(opts: SomaticMutectExecOptions): Promise<void>;
32
49
  //# sourceMappingURL=exec.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"exec.d.ts","sourceRoot":"","sources":["../../../src/commands/somatic-mutect/exec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAOH,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,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AKlG;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,KAAK,GAAG,SAAS,CAIjF;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE;IAAE,YAAY,CAAC,EAAE,OAAO,CAAA;CAAE,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAE9G;AAED,wBAAsB,wBAAwB,CAAC,IAAI,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwK5F"}
@@ -14,6 +14,10 @@
14
14
  * b37 12:25398284) in both staged BAMs and writes the alt fractions to the run log, so the run
15
15
  * itself documents which BAM carries the tumor signal.
16
16
  *
17
+ * Large pairs: a normal BAM above --stream-threshold-gb (default 60), or --stream-normal, is NOT copied: it is read in place through a read-only gcsfuse mount (the executor disk
18
+ * cannot hold a 143 GB whole-genome normal next to the tumor, and genomic bytes are read through gcsfuse by rule). --interval-file (gs:// BED) restricts calling to the territory the
19
+ * tumor assay covers, which is what makes a whole-genome normal against an exome tumor practical. The reference build is detected from the tumor header (hg38 when chr1 is 248,956,422).
20
+ *
17
21
  * AUDIT: gs://<OUT>/biowallet/<WALLET_LC>/somatic-mutect/<JOB_ID>/
18
22
  */
19
23
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
@@ -50,6 +54,9 @@ var __importStar = (this && this.__importStar) || (function () {
50
54
  };
51
55
  })();
52
56
  Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.splitGs = splitGs;
58
+ exports.detectBuild = detectBuild;
59
+ exports.shouldStream = shouldStream;
53
60
  exports.somaticMutectExecCommand = somaticMutectExecCommand;
54
61
  const child_process_1 = require("child_process");
55
62
  const fs = __importStar(require("fs"));
@@ -89,6 +96,23 @@ function capture(cmd, args) {
89
96
  const r = (0, child_process_1.spawnSync)(cmd, args, { encoding: 'utf8', maxBuffer: 256 * 1024 * 1024 });
90
97
  return r.status === 0 ? (r.stdout || '').trim() : '';
91
98
  }
99
+ function splitGs(uri) {
100
+ const m = /^gs:\/\/([^/]+)\/(.+)$/.exec(uri || '');
101
+ if (!m)
102
+ throw new Error(`not a gs:// object URI: ${uri}`);
103
+ const object = m[2];
104
+ return { bucket: m[1], object, dir: object.includes('/') ? object.slice(0, object.lastIndexOf('/')) : '', file: object.slice(object.lastIndexOf('/') + 1) };
105
+ }
106
+ function detectBuild(chr1Length) {
107
+ if (chr1Length === 248956422)
108
+ return 'hg38';
109
+ if (chr1Length === 249250621)
110
+ return 'b37';
111
+ return 'unknown';
112
+ }
113
+ function shouldStream(opts, sizeBytes, thresholdGb) {
114
+ return !!opts.streamNormal || sizeBytes > thresholdGb * 1024 ** 3;
115
+ }
92
116
  async function somaticMutectExecCommand(opts) {
93
117
  const caseId = opts.sample;
94
118
  const outBucket = opts.outBucket || 'genobank-parabricks-output';
@@ -151,12 +175,40 @@ async function somaticMutectExecCommand(opts) {
151
175
  return local;
152
176
  };
153
177
  const tumorLocal = stage(opts.tumorBam, 'tumor');
154
- const normalLocal = stage(opts.normalBam, 'normal');
178
+ // the normal: copy when it fits, stream through a read-only gcsfuse mount when it is large (or --stream-normal)
179
+ const normalSize = Number((capture('sh', ['-c', `gcloud storage ls -l ${opts.normalBam} 2>/dev/null | head -1 | awk '{print $1}'`]) || '0'));
180
+ const streamIt = shouldStream(opts, normalSize, Number(opts.streamThresholdGb || 60));
181
+ let normalDocker = '/w/normal.bam';
182
+ let normalVolume = [];
183
+ if (streamIt) {
184
+ const g = splitGs(opts.normalBam);
185
+ const mnt = `/mnt/gcsfuse-raw/${g.bucket}`;
186
+ const mounted = (0, child_process_1.spawnSync)('mountpoint', ['-q', mnt]).status === 0;
187
+ if (!mounted) {
188
+ run('sudo', ['mkdir', '-p', mnt], 'create the gcsfuse mount point');
189
+ run('sudo', ['gcsfuse', '--implicit-dirs', '-o', 'ro,allow_other', '--type-cache-max-size-mb=32', '--kernel-list-cache-ttl-secs=60', g.bucket, mnt], `mount gs://${g.bucket} read-only (gcsfuse)`);
190
+ }
191
+ const hostDir = path.join(mnt, g.dir);
192
+ if (!fs.existsSync(path.join(hostDir, g.file)) || !(fs.existsSync(path.join(hostDir, `${g.file}.bai`)) || fs.existsSync(path.join(hostDir, g.file.replace(/\.bam$/, '.bai'))))) {
193
+ logLine(`[somatic-mutect] the normal BAM and its index must both be in ${hostDir}; cannot stream`);
194
+ uploadAudit();
195
+ process.exit(1);
196
+ }
197
+ normalVolume = ['-v', `${hostDir}:/n:ro`];
198
+ normalDocker = `/n/${g.file}`;
199
+ logLine(`[somatic-mutect] normal BAM (${(normalSize / 1e9).toFixed(1)} GB) is read in place through ${mnt}; not copied`);
200
+ }
201
+ else {
202
+ stage(opts.normalBam, 'normal');
203
+ }
155
204
  // sample names come from each BAM's own @RG SM tag (Parabricks requires exact SM matches)
156
- const smOf = (base) => capture('docker', ['run', '--rm', '-v', `${work}:/w:ro`, '--entrypoint', 'bash', IMG_SAMTOOLS, '-c',
157
- `samtools view -H /w/${base}.bam | grep -m1 '^@RG' | tr '\\t' '\\n' | grep '^SM:' | cut -c4- | head -1`]);
205
+ const smOf = (base) => capture('docker', ['run', '--rm', '-v', `${work}:/w:ro`, ...(base === 'normal' ? normalVolume : []), '--entrypoint', 'bash', IMG_SAMTOOLS, '-c',
206
+ `samtools view -H ${base === 'normal' ? normalDocker : '/w/' + base + '.bam'} | grep -m1 '^@RG' | tr '\\t' '\\n' | grep '^SM:' | cut -c4- | head -1`]);
158
207
  const tumorSM = smOf('tumor');
159
208
  const normalSM = smOf('normal');
209
+ const chr1 = capture('docker', ['run', '--rm', '-v', `${work}:/w:ro`, '--entrypoint', 'bash', IMG_SAMTOOLS, '-c', "samtools view -H /w/tumor.bam | grep -m1 -P '^@SQ\\tSN:(chr)?1\\t' | tr '\\t' '\\n' | grep '^LN:' | cut -c4-"]);
210
+ const build = detectBuild(chr1 ? Number(chr1) : null);
211
+ logLine(`[somatic-mutect] reference build detected from the tumor header: ${build}`);
160
212
  if (!tumorSM || !normalSM) {
161
213
  logLine(`[somatic-mutect] missing @RG SM tag (tumor='${tumorSM}' normal='${normalSM}'); reheader the BAMs first`);
162
214
  uploadAudit();
@@ -170,7 +222,9 @@ async function somaticMutectExecCommand(opts) {
170
222
  logLine(`[somatic-mutect] tumor SM=${tumorSM} normal SM=${normalSM}`);
171
223
  // 1. reference: exact assembly the BAMs were aligned to. Explicit --ref-fasta wins; else b37
172
224
  // candidates (hs37d5 with decoys first — the standard Signatera/1000G alignment reference).
173
- const refCandidates = opts.refFasta ? [opts.refFasta] : [
225
+ const refCandidates = opts.refFasta ? [opts.refFasta] : build === 'hg38' ? [
226
+ `gs://${refBucket}/reference/hg38/Homo_sapiens_assembly38.fasta`,
227
+ ] : [
174
228
  `gs://${refBucket}/reference/hs37d5/Homo_sapiens_assembly19_1000genomes_decoy.fasta`,
175
229
  `gs://${refBucket}/reference/hg19/Homo_sapiens_assembly19.fasta`,
176
230
  ];
@@ -192,11 +246,22 @@ async function somaticMutectExecCommand(opts) {
192
246
  run('gcloud', ['storage', 'cp', `${refGs}.fai`, path.join(work, `${localRef}.fai`)], 'stage reference .fai');
193
247
  (0, child_process_1.spawnSync)('sh', ['-c', `gcloud storage cp ${refGs.replace(/\.(fa|fasta)$/, '.dict')} ${work}/ 2>/dev/null || gcloud storage cp ${refGs}.dict ${work}/ 2>/dev/null || true`], { stdio: 'ignore' });
194
248
  logLine(`[somatic-mutect] ref=${refGs}`);
249
+ let intervalLocal = '';
250
+ if (opts.intervalFile) {
251
+ if (!opts.intervalFile.startsWith('gs://')) {
252
+ logLine('[somatic-mutect] --interval-file must be a gs:// BED');
253
+ uploadAudit();
254
+ process.exit(1);
255
+ }
256
+ intervalLocal = 'callable.bed';
257
+ run('gcloud', ['storage', 'cp', opts.intervalFile, path.join(work, intervalLocal)], 'stage interval BED');
258
+ logLine(`[somatic-mutect] intervals: ${capture('sh', ['-c', `wc -l < ${work}/${intervalLocal}`])} regions`);
259
+ }
195
260
  // 1b. identity sanity check (non-blocking): pileup the hotspot in both BAMs and log alt fractions,
196
261
  // so the run log itself documents which BAM carries the tumor signal.
197
262
  for (const base of ['tumor', 'normal']) {
198
- const pile = capture('docker', ['run', '--rm', '-v', `${work}:/w:ro`, '--entrypoint', 'bash', IMG_SAMTOOLS, '-c',
199
- `samtools mpileup -r ${hotspot}-${hotspot.split(':')[1]} -f /w/${localRef} /w/${base}.bam 2>/dev/null | head -1`]);
263
+ const pile = capture('docker', ['run', '--rm', '-v', `${work}:/w:ro`, ...(base === 'normal' ? normalVolume : []), '--entrypoint', 'bash', IMG_SAMTOOLS, '-c',
264
+ `samtools mpileup -r ${hotspot}-${hotspot.split(':')[1]} -f /w/${localRef} ${base === 'normal' ? normalDocker : '/w/' + base + '.bam'} 2>/dev/null | head -1`]);
200
265
  logLine(`[somatic-mutect] hotspot ${hotspot} ${base}: ${pile ? pile.split('\t').slice(3, 5).join(' ') : 'no coverage/parse'}`);
201
266
  }
202
267
  // 2. pbrun mutectcaller (Mutect2 tumor/normal, GPU) inside the Clara container.
@@ -204,11 +269,13 @@ async function somaticMutectExecCommand(opts) {
204
269
  const mutectArgs = ['pbrun', 'mutectcaller',
205
270
  '--ref', `/w/${localRef}`,
206
271
  '--in-tumor-bam', '/w/tumor.bam', '--tumor-name', tumorSM,
207
- '--in-normal-bam', '/w/normal.bam', '--normal-name', normalSM,
272
+ '--in-normal-bam', normalDocker, '--normal-name', normalSM,
208
273
  '--out-vcf', `/w/${vcfGz}`, '--num-gpus', '1'];
274
+ if (intervalLocal)
275
+ mutectArgs.push('--interval-file', `/w/${intervalLocal}`);
209
276
  if (lowMemory)
210
277
  mutectArgs.push('--mutect-low-memory');
211
- run('docker', ['run', '--rm', '--gpus', 'all', '-v', `${work}:/w:rw`, IMG_PARABRICKS, ...mutectArgs], 'pbrun mutectcaller (Mutect2 tumor/normal somatic calling, GPU, local NVMe)');
278
+ run('docker', ['run', '--rm', '--gpus', 'all', '-v', `${work}:/w:rw`, ...normalVolume, IMG_PARABRICKS, ...mutectArgs], 'pbrun mutectcaller (Mutect2 tumor/normal somatic calling, GPU, local NVMe)');
212
279
  const producedVcf = fs.existsSync(path.join(work, vcfGz)) ? vcfGz
213
280
  : (fs.existsSync(path.join(work, vcfGz.replace(/\.gz$/, ''))) ? vcfGz.replace(/\.gz$/, '') : '');
214
281
  if (!producedVcf) {
@@ -234,7 +301,7 @@ async function somaticMutectExecCommand(opts) {
234
301
  const manifest = {
235
302
  schema: 'genobank.somamu.manifest/v1', pipeline: 'somatic-mutect2',
236
303
  jobId, caseId, creator: walletLc, status: valid ? 'OK' : 'EMPTY',
237
- inputs: { tumorBam: opts.tumorBam, normalBam: opts.normalBam, tumorSM, normalSM, reference: refGs, lowMemory },
304
+ inputs: { tumorBam: opts.tumorBam, normalBam: opts.normalBam, tumorSM, normalSM, reference: refGs, build, intervalFile: opts.intervalFile || null, normalStreamed: streamIt, lowMemory },
238
305
  tools: { parabricks_mutectcaller: IMG_PARABRICKS, htslib: IMG_HTSLIB, samtools: IMG_SAMTOOLS },
239
306
  outputs: { somatic_vcf: `${BIOWALLET_GCS}/${finalVcf}` },
240
307
  summary: { total_variants: Number(total), pass_variants: Number(passN) },