dsh-molbio-tools 0.28.0 → 0.28.2

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/genbank.mjs CHANGED
@@ -41,15 +41,35 @@ function parseLocation(raw) {
41
41
  const parts = [...cleaned.matchAll(/\d+(?:\.\.\d+)?/g)];
42
42
  const numbers = [...cleaned.matchAll(/\d+/g)].map((match) => Number(match[0]));
43
43
  if (numbers.length === 0) return undefined;
44
+ // Reduce with a loop, never `Math.min(...numbers)`: a 20 Mb join() with a few
45
+ // hundred thousand parts is a real NCBI record shape, and spreading it into
46
+ // the argument list throws `RangeError: Maximum call stack size exceeded`
47
+ // instead of returning a span.
48
+ let start = numbers[0];
49
+ let end = numbers[0];
50
+ for (const number of numbers) {
51
+ if (number < start) start = number;
52
+ if (number > end) end = number;
53
+ }
44
54
  return {
45
- start: Math.min(...numbers),
46
- end: Math.max(...numbers),
55
+ start,
56
+ end,
47
57
  strand: /complement/i.test(raw) ? -1 : 1,
48
58
  raw: raw.trim(),
49
59
  multiPart: parts.length > 1,
50
60
  };
51
61
  }
52
62
 
63
+ /** Does this location still have an unclosed `(` (i.e. it wraps onto the next line)? */
64
+ function isLocationOpen(text) {
65
+ let depth = 0;
66
+ for (const character of text) {
67
+ if (character === '(') depth += 1;
68
+ else if (character === ')') depth = Math.max(0, depth - 1);
69
+ }
70
+ return depth > 0;
71
+ }
72
+
53
73
  /** Parse one qualifier line: /key="value" (or unquoted value). */
54
74
  function parseQualifier(line) {
55
75
  const match = /^\s*\/?([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(?:"([^"]*)"|(\S+))/.exec(line);
@@ -82,6 +102,31 @@ export function parseGenBank(text) {
82
102
  let inOrigin = false;
83
103
  let currentFeature = undefined;
84
104
  let currentQualifier = undefined;
105
+ let currentLocation = '';
106
+ let locationOpen = false;
107
+ let placed = false;
108
+
109
+ /** Publish the current feature once its location can be placed. */
110
+ const placeCurrentFeature = () => {
111
+ if (currentFeature === undefined) return;
112
+ const parsedLocation = parseLocation(currentLocation);
113
+ if (parsedLocation === undefined) return;
114
+ currentFeature.location = parsedLocation;
115
+ if (!placed) {
116
+ features.push(currentFeature);
117
+ placed = true;
118
+ }
119
+ };
120
+
121
+ /** Count a feature that never got a placeable location, then reset per-feature state. */
122
+ const finishFeature = () => {
123
+ if (currentFeature !== undefined && !placed) skippedFeatures += 1;
124
+ currentFeature = undefined;
125
+ currentQualifier = undefined;
126
+ currentLocation = '';
127
+ locationOpen = false;
128
+ placed = false;
129
+ };
85
130
 
86
131
  for (let i = locusIndex; i < lines.length; i++) {
87
132
  const line = lines[i];
@@ -92,29 +137,35 @@ export function parseGenBank(text) {
92
137
  }
93
138
  if (inFeatures) {
94
139
  if (/^ORIGIN/.test(line)) {
140
+ finishFeature();
95
141
  inFeatures = false;
96
142
  inOrigin = true;
97
143
  continue;
98
144
  }
99
145
  if (/^\s{5}\S/.test(line)) {
100
146
  // New feature: 5 spaces, then type and location.
147
+ finishFeature();
101
148
  const match = /^\s{5}(\S+)\s*(.*)$/.exec(line);
102
149
  if (match === null) {
103
150
  skippedFeatures += 1;
104
151
  continue;
105
152
  }
106
- currentFeature = {
107
- type: match[1],
108
- location: parseLocation(match[2] ?? ''),
109
- qualifiers: {},
110
- };
111
- if (currentFeature.location === undefined) {
112
- // No digits in the location: the feature cannot be placed. It is
113
- // dropped, and the caller reports the count.
114
- skippedFeatures += 1;
115
- continue;
116
- }
117
- features.push(currentFeature);
153
+ currentFeature = { type: match[1], location: undefined, qualifiers: {} };
154
+ currentLocation = match[2] ?? '';
155
+ locationOpen = isLocationOpen(currentLocation);
156
+ placeCurrentFeature();
157
+ continue;
158
+ }
159
+ // A location wider than the column wraps onto 21-space continuation lines
160
+ // (`join(1..10,30..40,` + `50..60,70..80)`), which is how NCBI writes any
161
+ // long join(). Such a line belongs to the LOCATION. It used to be read as
162
+ // a qualifier continuation only, so the span silently stopped at the first
163
+ // line and every downstream coordinate (map, batch table, feature counts)
164
+ // sat on the wrong bases.
165
+ if (locationOpen && /^\s{21}[0-9()][0-9,().^<>?\s]*$/.test(line)) {
166
+ currentLocation += line.trim();
167
+ locationOpen = isLocationOpen(currentLocation);
168
+ placeCurrentFeature();
118
169
  continue;
119
170
  }
120
171
  if (/^\s{21}\//.test(line) && currentFeature !== undefined) {
package/host.mjs CHANGED
@@ -27,7 +27,7 @@
27
27
  *
28
28
  * ── WHY NOT JUST ANCHOR ON index.mjs ───────────────────────────────────────
29
29
  *
30
- * `index.mjs` is the TOOL plugin: importing it must register all 65 tools and
30
+ * `index.mjs` is the TOOL plugin: importing it must register all 72 tools and
31
31
  * a prompt section. A host row naming it would put every tool back into every
32
32
  * session, which is the thing the preset split exists to avoid. This anchor is
33
33
  * deliberately inert.