dsh-molbio-tools 0.27.0 → 0.28.0
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.
- package/CHANGELOG.md +212 -0
- package/README.md +39 -14
- package/benchmark/README.md +7 -7
- package/benchmark/_probe-v28.mjs +60 -0
- package/benchmark/harness.mjs +24 -5
- package/benchmark/profile.mjs +14 -5
- package/benchmark/tasks/10-v28.json +300 -0
- package/benchmark/verifications.mjs +49 -0
- package/build/browser-api.mjs +1 -1
- package/build/client-entry.mjs +153 -38
- package/build/panel-core.mjs +132 -11
- package/cordis.patch.yml +5 -3
- package/docs/capability-gap-survey.md +1 -1
- package/docs/client-panel.md +13 -13
- package/docs/history.md +48 -0
- package/docs/maintainer.md +9 -6
- package/docs/roadmap.md +27 -5
- package/docs/v28-plan.md +227 -0
- package/docs/workflow.md +6 -6
- package/index.mjs +636 -2
- package/lib/client.js +292 -52
- package/logo.mjs +5 -1
- package/package.json +7 -3
- package/preset/molbio-lab/agent.cordis.yml +13 -7
- package/preset/molbio-lab/preset.yml +1 -1
- package/protein-annotate.mjs +525 -0
- package/sequence-ops.mjs +203 -0
- package/sequencing.mjs +517 -0
- package/test/client-mount.mjs +6 -17
- package/test/client.mjs +106 -3
- package/test/contract.mjs +48 -23
- package/test/panel-render.mjs +114 -57
- package/test/smoke.mjs +191 -2
- package/test/svgpng.mjs +2 -0
package/sequence-ops.mjs
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-molbio-tools/sequence-ops.mjs
|
|
3
|
+
*
|
|
4
|
+
* Format interchange and sequence surgery for annotated DNA:
|
|
5
|
+
* - features → GFF3 / BED / CSV, with the three coordinate conventions kept
|
|
6
|
+
* apart instead of silently converted;
|
|
7
|
+
* - rotate / insert / delete / replace / split / join on a sequence.
|
|
8
|
+
*
|
|
9
|
+
* The coordinate systems are the whole point of the first half. GenBank and
|
|
10
|
+
* GFF3 are 1-based and INCLUSIVE; BED is 0-based and HALF-OPEN. An off-by-one
|
|
11
|
+
* here does not throw — it silently exports the wrong interval — so each format
|
|
12
|
+
* is built from its own explicit conversion and the tool reports which one it
|
|
13
|
+
* used. CDS phase is NOT computed: the transcript frame is not modelled, so the
|
|
14
|
+
* column is '.' (unknown) rather than a plausible-looking guess.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { MolbioInputError, normalizeSequence } from './lib.mjs';
|
|
18
|
+
|
|
19
|
+
const FORMATS = ['gff3', 'bed', 'csv'];
|
|
20
|
+
const OPERATIONS = ['rotate', 'insert', 'delete', 'replace', 'split', 'join'];
|
|
21
|
+
|
|
22
|
+
function sanitizeName(value, fallback) {
|
|
23
|
+
const text = String(value ?? '').trim();
|
|
24
|
+
return text === '' ? fallback : text.replace(/[\s]+/g, '_');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Percent-encode the characters GFF3 reserves inside an attribute value. */
|
|
28
|
+
function gffValue(value) {
|
|
29
|
+
return String(value).replace(/[%&=;,\t\n\r]/g, (ch) => '%' + ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** RFC 4180 quoting for one CSV cell. */
|
|
33
|
+
function csvCell(value) {
|
|
34
|
+
const text = String(value ?? '');
|
|
35
|
+
return /[",\n\r]/.test(text) ? '"' + text.replaceAll('"', '""') + '"' : text;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateFeatures(features, length) {
|
|
39
|
+
const rows = [];
|
|
40
|
+
for (const [index, feature] of features.entries()) {
|
|
41
|
+
const start = feature.start;
|
|
42
|
+
const end = feature.end;
|
|
43
|
+
if (!Number.isInteger(start) || !Number.isInteger(end) || start < 1 || end < 1 || start > end) {
|
|
44
|
+
throw new MolbioInputError(`feature ${index + 1} has an invalid span ${JSON.stringify(start)}..${JSON.stringify(end)} (1-based inclusive, start <= end)`);
|
|
45
|
+
}
|
|
46
|
+
if (end > length) throw new MolbioInputError(`feature ${index + 1} ends at ${end}, past the sequence length ${length}`);
|
|
47
|
+
// Parsers use 1 / -1; GFF3 and BED spell the strand '+' / '-' / '.'.
|
|
48
|
+
const strand = feature.strand === '-' || feature.strand === -1 ? '-' : feature.strand === '+' || feature.strand === 1 ? '+' : '.';
|
|
49
|
+
rows.push({ ...feature, start, end, strand });
|
|
50
|
+
}
|
|
51
|
+
rows.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
52
|
+
return rows;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Convert annotated features to GFF3, BED or CSV.
|
|
57
|
+
*
|
|
58
|
+
* @param {object} input {name, sequence, features, format}
|
|
59
|
+
* @returns {object} {format, coordinate_system, content, count, notes}
|
|
60
|
+
*/
|
|
61
|
+
export function exportFeatures({ name, sequence, features = [], format }) {
|
|
62
|
+
if (!FORMATS.includes(format)) throw new MolbioInputError(`format must be one of ${FORMATS.join(', ')}`);
|
|
63
|
+
if (typeof sequence !== 'string' || sequence.length === 0) throw new MolbioInputError('a sequence is required to export features');
|
|
64
|
+
const rows = validateFeatures(features, sequence.length);
|
|
65
|
+
const seqid = sanitizeName(name, 'sequence');
|
|
66
|
+
const notes = [];
|
|
67
|
+
if (rows.length === 0) notes.push('the input carries no features; the file is still a valid empty document');
|
|
68
|
+
if (rows.some((row) => row.multi_part === true)) notes.push('multi-part features are exported as their outer span; the raw location is preserved in the location attribute/column');
|
|
69
|
+
if (format === 'gff3' || format === 'bed') notes.push('CDS phase is not computed (the transcript frame is not modelled), so the GFF3 phase column is \'.\'');
|
|
70
|
+
|
|
71
|
+
if (format === 'gff3') {
|
|
72
|
+
const lines = ['##gff-version 3', `##sequence-region ${seqid} 1 ${sequence.length}`];
|
|
73
|
+
rows.forEach((row, index) => {
|
|
74
|
+
const attributes = [`ID=${gffValue(sanitizeName(row.type, 'feature'))}_${index + 1}`];
|
|
75
|
+
if (row.label !== undefined && row.label !== '') attributes.push(`Name=${gffValue(row.label)}`);
|
|
76
|
+
if (row.gene !== undefined && row.gene !== '') attributes.push(`gene=${gffValue(row.gene)}`);
|
|
77
|
+
if (row.product !== undefined && row.product !== '') attributes.push(`product=${gffValue(row.product)}`);
|
|
78
|
+
if (row.note !== undefined && row.note !== '') attributes.push(`Note=${gffValue(row.note)}`);
|
|
79
|
+
if (row.location !== undefined && row.location !== '') attributes.push(`location=${gffValue(row.location)}`);
|
|
80
|
+
lines.push([seqid, 'dsh-molbio-tools', sanitizeName(row.type, 'feature'), row.start, row.end, '.', row.strand, '.', attributes.join(';')].join('\t'));
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
format,
|
|
84
|
+
coordinate_system: '1-based inclusive',
|
|
85
|
+
content: lines.join('\n') + '\n',
|
|
86
|
+
count: rows.length,
|
|
87
|
+
notes,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (format === 'bed') {
|
|
92
|
+
const lines = [`track name="${seqid}" description="dsh-molbio-tools feature export"`];
|
|
93
|
+
for (const row of rows) {
|
|
94
|
+
const start0 = row.start - 1;
|
|
95
|
+
const end0 = row.end;
|
|
96
|
+
lines.push([seqid, start0, end0, sanitizeName(row.label ?? row.type, 'feature'), 0, row.strand].join('\t'));
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
format,
|
|
100
|
+
coordinate_system: '0-based half-open',
|
|
101
|
+
content: lines.join('\n') + '\n',
|
|
102
|
+
count: rows.length,
|
|
103
|
+
notes,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const header = ['seqid', 'type', 'start', 'end', 'strand', 'length', 'label', 'gene', 'product', 'location'];
|
|
108
|
+
const lines = [header.join(',')];
|
|
109
|
+
for (const row of rows) {
|
|
110
|
+
lines.push([
|
|
111
|
+
seqid, row.type, row.start, row.end, row.strand, row.end - row.start + 1,
|
|
112
|
+
row.label ?? '', row.gene ?? '', row.product ?? '', row.location ?? '',
|
|
113
|
+
].map(csvCell).join(','));
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
format,
|
|
117
|
+
coordinate_system: '1-based inclusive',
|
|
118
|
+
content: lines.join('\n') + '\n',
|
|
119
|
+
count: rows.length,
|
|
120
|
+
notes,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function requireInteger(value, label, minimum, maximum) {
|
|
125
|
+
if (!Number.isInteger(value) || value < minimum || value > maximum) {
|
|
126
|
+
throw new MolbioInputError(`${label} must be an integer between ${minimum} and ${maximum}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Rotate / insert / delete / replace / split / join a DNA sequence.
|
|
132
|
+
*
|
|
133
|
+
* All positions are 1-based and inclusive, matching the rest of the toolset;
|
|
134
|
+
* `position` for rotate/insert and `at` for split is the 0-based-style cut used
|
|
135
|
+
* throughout the description of each operation. Features are NOT remapped —
|
|
136
|
+
* re-export annotations against the new coordinates if the edit moves them.
|
|
137
|
+
*/
|
|
138
|
+
export function editSequence({ sequence: rawSequence, operation, position, start, end, inserted, replacement, joinWith, overlap = 0 } = {}) {
|
|
139
|
+
const sequence = normalizeSequence(rawSequence, 'sequence');
|
|
140
|
+
if (!OPERATIONS.includes(operation)) throw new MolbioInputError(`operation must be one of ${OPERATIONS.join(', ')}`);
|
|
141
|
+
const notes = [];
|
|
142
|
+
|
|
143
|
+
if (operation === 'rotate') {
|
|
144
|
+
requireInteger(position, 'position', 1, sequence.length);
|
|
145
|
+
const rotated = sequence.slice(position - 1) + sequence.slice(0, position - 1);
|
|
146
|
+
notes.push(`the original base ${position} is now base 1 (rotation preserves the circular sequence)`);
|
|
147
|
+
return { operation, input_length: sequence.length, output_length: rotated.length, sequences: [rotated], notes };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (operation === 'insert') {
|
|
151
|
+
requireInteger(position, 'position', 0, sequence.length);
|
|
152
|
+
const fragment = normalizeSequence(inserted ?? '', 'inserted');
|
|
153
|
+
if (fragment.length === 0) throw new MolbioInputError('insert needs a non-empty inserted sequence');
|
|
154
|
+
const result = sequence.slice(0, position) + fragment + sequence.slice(position);
|
|
155
|
+
notes.push(`inserted ${fragment.length} bp after ${position} base(s)`);
|
|
156
|
+
return { operation, input_length: sequence.length, output_length: result.length, sequences: [result], notes };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (operation === 'delete') {
|
|
160
|
+
requireInteger(start, 'start', 1, sequence.length);
|
|
161
|
+
requireInteger(end, 'end', 1, sequence.length);
|
|
162
|
+
if (start > end) throw new MolbioInputError('delete needs start <= end (1-based inclusive)');
|
|
163
|
+
const result = sequence.slice(0, start - 1) + sequence.slice(end);
|
|
164
|
+
notes.push(`deleted ${end - start + 1} bp at ${start}-${end}`);
|
|
165
|
+
return { operation, input_length: sequence.length, output_length: result.length, sequences: [result.length === 0 ? '' : result], notes };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (operation === 'replace') {
|
|
169
|
+
requireInteger(start, 'start', 1, sequence.length);
|
|
170
|
+
requireInteger(end, 'end', 1, sequence.length);
|
|
171
|
+
if (start > end) throw new MolbioInputError('replace needs start <= end (1-based inclusive)');
|
|
172
|
+
const fragment = normalizeSequence(replacement ?? '', 'replacement');
|
|
173
|
+
const result = sequence.slice(0, start - 1) + fragment + sequence.slice(end);
|
|
174
|
+
notes.push(`replaced ${end - start + 1} bp at ${start}-${end} with ${fragment.length} bp`);
|
|
175
|
+
return { operation, input_length: sequence.length, output_length: result.length, sequences: [result], notes };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (operation === 'split') {
|
|
179
|
+
requireInteger(position, 'position', 1, sequence.length - 1);
|
|
180
|
+
notes.push(`split after base ${position} (1-based; both parts are inclusive)`);
|
|
181
|
+
return {
|
|
182
|
+
operation,
|
|
183
|
+
input_length: sequence.length,
|
|
184
|
+
output_length: position,
|
|
185
|
+
sequences: [sequence.slice(0, position), sequence.slice(position)],
|
|
186
|
+
notes,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// join
|
|
191
|
+
if (typeof joinWith !== 'string' || joinWith.trim() === '') throw new MolbioInputError('join needs join_with (the second sequence)');
|
|
192
|
+
const second = normalizeSequence(joinWith, 'join_with');
|
|
193
|
+
requireInteger(overlap, 'overlap', 0, Math.min(sequence.length, second.length));
|
|
194
|
+
if (overlap > 0 && sequence.slice(sequence.length - overlap) !== second.slice(0, overlap)) {
|
|
195
|
+
throw new MolbioInputError(`join overlap ${overlap} does not match: the first sequence must end with the second sequence\'s first ${overlap} bases`);
|
|
196
|
+
}
|
|
197
|
+
const result = sequence + second.slice(overlap);
|
|
198
|
+
notes.push(overlap > 0 ? `joined with a verified ${overlap} bp exact overlap` : 'joined end to end (no overlap)');
|
|
199
|
+
return { operation, input_length: sequence.length, output_length: result.length, sequences: [result], notes };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export const SEQUENCE_OPS_FORMATS = FORMATS;
|
|
203
|
+
export const SEQUENCE_OPS_OPERATIONS = OPERATIONS;
|