@apollo-annotation/shared 0.3.6 → 0.3.8

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.
Files changed (94) hide show
  1. package/dist/Changes/AddAssemblyAliasesChange.d.ts +16 -0
  2. package/dist/Changes/AddAssemblyAliasesChange.js +50 -0
  3. package/dist/Changes/AddAssemblyAliasesChange.js.map +1 -0
  4. package/dist/Changes/AddAssemblyAndFeaturesFromFileChange.js +1 -1
  5. package/dist/Changes/AddAssemblyAndFeaturesFromFileChange.js.map +1 -1
  6. package/dist/Changes/AddAssemblyFromFileChange.js +1 -1
  7. package/dist/Changes/AddAssemblyFromFileChange.js.map +1 -1
  8. package/dist/Changes/AddFeatureChange.d.ts +1 -0
  9. package/dist/Changes/AddFeatureChange.js +5 -25
  10. package/dist/Changes/AddFeatureChange.js.map +1 -1
  11. package/dist/Changes/AddRefSeqAliasesChange.js +2 -4
  12. package/dist/Changes/AddRefSeqAliasesChange.js.map +1 -1
  13. package/dist/Changes/DeleteFeatureChange.d.ts +9 -8
  14. package/dist/Changes/DeleteFeatureChange.js +37 -32
  15. package/dist/Changes/DeleteFeatureChange.js.map +1 -1
  16. package/dist/Changes/FeatureAttributeChange.d.ts +2 -1
  17. package/dist/Changes/FeatureAttributeChange.js +14 -6
  18. package/dist/Changes/FeatureAttributeChange.js.map +1 -1
  19. package/dist/Changes/ImportJBrowseConfigChange.d.ts +3 -0
  20. package/dist/Changes/ImportJBrowseConfigChange.js +17 -13
  21. package/dist/Changes/ImportJBrowseConfigChange.js.map +1 -1
  22. package/dist/Changes/LocationEndChange.js +40 -37
  23. package/dist/Changes/LocationEndChange.js.map +1 -1
  24. package/dist/Changes/LocationStartChange.js +40 -37
  25. package/dist/Changes/LocationStartChange.js.map +1 -1
  26. package/dist/Changes/MergeExonsChange.d.ts +29 -0
  27. package/dist/Changes/MergeExonsChange.js +114 -0
  28. package/dist/Changes/MergeExonsChange.js.map +1 -0
  29. package/dist/Changes/MergeTranscriptsChange.d.ts +34 -0
  30. package/dist/Changes/MergeTranscriptsChange.js +245 -0
  31. package/dist/Changes/MergeTranscriptsChange.js.map +1 -0
  32. package/dist/Changes/SplitExonChange.d.ts +33 -0
  33. package/dist/Changes/SplitExonChange.js +142 -0
  34. package/dist/Changes/SplitExonChange.js.map +1 -0
  35. package/dist/Changes/UndoMergeExonsChange.d.ts +27 -0
  36. package/dist/Changes/UndoMergeExonsChange.js +104 -0
  37. package/dist/Changes/UndoMergeExonsChange.js.map +1 -0
  38. package/dist/Changes/UndoMergeTranscriptsChange.d.ts +27 -0
  39. package/dist/Changes/UndoMergeTranscriptsChange.js +104 -0
  40. package/dist/Changes/UndoMergeTranscriptsChange.js.map +1 -0
  41. package/dist/Changes/UndoSplitExonChange.d.ts +32 -0
  42. package/dist/Changes/UndoSplitExonChange.js +111 -0
  43. package/dist/Changes/UndoSplitExonChange.js.map +1 -0
  44. package/dist/Changes/index.d.ts +21 -0
  45. package/dist/Changes/index.js +21 -0
  46. package/dist/Changes/index.js.map +1 -1
  47. package/dist/Checks/CDSCheck.d.ts +1 -1
  48. package/dist/Checks/CDSCheck.js +8 -9
  49. package/dist/Checks/CDSCheck.js.map +1 -1
  50. package/dist/Checks/TranscriptCheck.d.ts +9 -0
  51. package/dist/Checks/TranscriptCheck.js +109 -0
  52. package/dist/Checks/TranscriptCheck.js.map +1 -0
  53. package/dist/Checks/index.d.ts +1 -0
  54. package/dist/Checks/index.js +1 -0
  55. package/dist/Checks/index.js.map +1 -1
  56. package/dist/GFF3/annotationFeatureToGFF3.js +7 -1
  57. package/dist/GFF3/annotationFeatureToGFF3.js.map +1 -1
  58. package/dist/GFF3/gff3ToAnnotationFeature.test.js +1 -1
  59. package/dist/GFF3/gff3ToAnnotationFeature.test.js.map +1 -1
  60. package/dist/Validations/ParentChildValidation.js +3 -1
  61. package/dist/Validations/ParentChildValidation.js.map +1 -1
  62. package/dist/tsconfig.tsbuildinfo +1 -1
  63. package/dist/util.d.ts +5 -0
  64. package/dist/util.js +46 -0
  65. package/dist/util.js.map +1 -1
  66. package/package.json +7 -7
  67. package/src/Changes/AddAssemblyAliasesChange.ts +69 -0
  68. package/src/Changes/AddAssemblyAndFeaturesFromFileChange.ts +2 -1
  69. package/src/Changes/AddAssemblyFromFileChange.ts +1 -1
  70. package/src/Changes/AddFeatureChange.ts +5 -29
  71. package/src/Changes/AddRefSeqAliasesChange.ts +2 -4
  72. package/src/Changes/DeleteFeatureChange.ts +42 -36
  73. package/src/Changes/FeatureAttributeChange.ts +16 -7
  74. package/src/Changes/ImportJBrowseConfigChange.ts +33 -18
  75. package/src/Changes/LocationEndChange.ts +41 -50
  76. package/src/Changes/LocationStartChange.ts +41 -50
  77. package/src/Changes/MergeExonsChange.ts +201 -0
  78. package/src/Changes/MergeTranscriptsChange.ts +394 -0
  79. package/src/Changes/SplitExonChange.ts +249 -0
  80. package/src/Changes/UndoMergeExonsChange.ts +149 -0
  81. package/src/Changes/UndoMergeTranscriptsChange.ts +153 -0
  82. package/src/Changes/UndoSplitExonChange.ts +174 -0
  83. package/src/Changes/index.ts +21 -0
  84. package/src/Checks/CDSCheck.ts +10 -10
  85. package/src/Checks/TranscriptCheck.ts +139 -0
  86. package/src/Checks/index.ts +1 -0
  87. package/src/GFF3/annotationFeatureToGFF3.ts +7 -1
  88. package/src/GFF3/gff3ToAnnotationFeature.test.ts +1 -1
  89. package/src/Validations/ParentChildValidation.ts +3 -2
  90. package/src/util.ts +53 -0
  91. package/test_data/example01.json +20 -20
  92. package/test_data/example02.json +38 -38
  93. package/test_data/example04.json +57 -57
  94. package/test_data/gene_representations.gff3 +1054 -276
@@ -60,19 +60,32 @@ export class LocationStartChange extends FeatureChange {
60
60
  async executeOnServer(backend: ServerDataStore) {
61
61
  const { featureModel, session } = backend
62
62
  const { changes, logger } = this
63
- const featuresForChanges: {
64
- feature: Feature
65
- topLevelFeature: FeatureDocument
66
- }[] = []
67
63
  // Let's first check that all features are found and those old values match with expected ones. We do this just to be sure that all changes can be done.
64
+ const topLevelFeatures: FeatureDocument[] = []
68
65
  for (const change of changes) {
69
- const { featureId, oldStart } = change
70
-
71
- // Search correct feature
72
- const topLevelFeature = await featureModel
73
- .findOne({ allIds: featureId })
74
- .session(session)
75
- .exec()
66
+ const { featureId, oldStart, newStart } = change
67
+
68
+ // See if we already have top-level feature for this feature
69
+ let topLevelFeature: FeatureDocument | undefined | null
70
+ let feature: Feature | undefined | null
71
+ for (const tlv of topLevelFeatures) {
72
+ const childFeature = this.getFeatureFromId(tlv, featureId)
73
+ if (childFeature) {
74
+ topLevelFeature = tlv
75
+ feature = childFeature
76
+ break
77
+ }
78
+ }
79
+ if (!topLevelFeature) {
80
+ // Don't already have top-level feature, so let's query it
81
+ topLevelFeature = await featureModel
82
+ .findOne({ allIds: featureId })
83
+ .session(session)
84
+ .exec()
85
+ if (topLevelFeature) {
86
+ topLevelFeatures.push(topLevelFeature)
87
+ }
88
+ }
76
89
 
77
90
  if (!topLevelFeature) {
78
91
  const errMsg = `*** ERROR: The following featureId was not found in database ='${featureId}'`
@@ -84,57 +97,34 @@ export class LocationStartChange extends FeatureChange {
84
97
  `*** TOP level feature found: ${JSON.stringify(topLevelFeature)}`,
85
98
  )
86
99
 
87
- const foundFeature = this.getFeatureFromId(topLevelFeature, featureId)
88
- if (!foundFeature) {
100
+ if (!feature) {
101
+ feature = this.getFeatureFromId(topLevelFeature, featureId)
102
+ }
103
+ if (!feature) {
89
104
  const errMsg = 'ERROR when searching feature by featureId'
90
105
  logger.error(errMsg)
91
106
  throw new Error(errMsg)
92
107
  }
93
- logger.debug?.(`*** Found feature: ${JSON.stringify(foundFeature)}`)
94
- if (foundFeature.min === oldStart) {
95
- featuresForChanges.push({ feature: foundFeature, topLevelFeature })
96
- } else {
97
- if (foundFeature.children) {
98
- for (const [, childFeature] of foundFeature.children) {
99
- if (childFeature.min === oldStart) {
100
- logger.debug?.(
101
- `*** UPDATE CHILD FEATURE ID= ${featureId}, CHILD: ${JSON.stringify(
102
- childFeature,
103
- )}`,
104
- )
105
- featuresForChanges.push({
106
- feature: childFeature,
107
- topLevelFeature,
108
- })
109
- break
110
- }
111
- }
112
- }
108
+ logger.debug?.(`*** Found feature: ${JSON.stringify(feature)}`)
109
+ if (feature.min !== oldStart) {
110
+ const errMsg = 'Expected previous max does not match'
111
+ logger.error(errMsg)
112
+ throw new Error(errMsg)
113
113
  }
114
- }
115
-
116
- // Let's update objects.
117
- for (const [idx, change] of changes.entries()) {
118
- const { newStart } = change
119
- const { feature, topLevelFeature } = featuresForChanges[idx]
120
114
  feature.min = newStart
121
115
  if (topLevelFeature._id.equals(feature._id)) {
122
116
  topLevelFeature.markModified('start') // Mark as modified. Without this save() -method is not updating data in database
123
117
  } else {
124
118
  topLevelFeature.markModified('children') // Mark as modified. Without this save() -method is not updating data in database
125
119
  }
126
-
120
+ }
121
+ for (const tlv of topLevelFeatures) {
127
122
  try {
128
- await topLevelFeature.save()
123
+ await tlv.save()
129
124
  } catch (error) {
130
125
  logger.debug?.(`*** FAILED: ${error}`)
131
126
  throw error
132
127
  }
133
- logger.debug?.(
134
- `*** Object updated in Mongo. New object: ${JSON.stringify(
135
- topLevelFeature,
136
- )}`,
137
- )
138
128
  }
139
129
  }
140
130
 
@@ -146,12 +136,13 @@ export class LocationStartChange extends FeatureChange {
146
136
  if (!dataStore) {
147
137
  throw new Error('No data store')
148
138
  }
149
- for (const [idx, changedId] of this.changedIds.entries()) {
150
- const feature = dataStore.getFeature(changedId)
139
+ for (const change of this.changes) {
140
+ const { featureId, newStart } = change
141
+ const feature = dataStore.getFeature(featureId)
151
142
  if (!feature) {
152
- throw new Error(`Could not find feature with identifier "${changedId}"`)
143
+ throw new Error(`Could not find feature with identifier "${featureId}"`)
153
144
  }
154
- feature.setMin(this.changes[idx].newStart)
145
+ feature.setMin(newStart)
155
146
  }
156
147
  }
157
148
 
@@ -0,0 +1,201 @@
1
+ /* eslint-disable unicorn/prefer-structured-clone */
2
+ /* eslint-disable @typescript-eslint/require-await */
3
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
4
+
5
+ import {
6
+ type ChangeOptions,
7
+ type ClientDataStore,
8
+ FeatureChange,
9
+ type LocalGFF3DataStore,
10
+ type SerializedFeatureChange,
11
+ type ServerDataStore,
12
+ } from '@apollo-annotation/common'
13
+ import { type AnnotationFeatureSnapshot } from '@apollo-annotation/mst'
14
+
15
+ import { attributesToRecords, stringifyAttributes } from '../util'
16
+
17
+ import { findAndDeleteChildFeature } from './DeleteFeatureChange'
18
+ import { UndoMergeExonsChange } from './UndoMergeExonsChange'
19
+
20
+ interface SerializedMergeExonsChangeBase extends SerializedFeatureChange {
21
+ typeName: 'MergeExonsChange'
22
+ }
23
+
24
+ export interface MergeExonsChangeDetails {
25
+ firstExon: AnnotationFeatureSnapshot
26
+ secondExon: AnnotationFeatureSnapshot
27
+ parentFeatureId: string
28
+ }
29
+
30
+ interface SerializedMergeExonsChangeSingle
31
+ extends SerializedMergeExonsChangeBase,
32
+ MergeExonsChangeDetails {}
33
+
34
+ interface SerializedMergeExonsChangeMultiple
35
+ extends SerializedMergeExonsChangeBase {
36
+ changes: MergeExonsChangeDetails[]
37
+ }
38
+
39
+ export type SerializedMergeExonsChange =
40
+ | SerializedMergeExonsChangeSingle
41
+ | SerializedMergeExonsChangeMultiple
42
+
43
+ export class MergeExonsChange extends FeatureChange {
44
+ typeName = 'MergeExonsChange' as const
45
+ changes: MergeExonsChangeDetails[]
46
+
47
+ constructor(json: SerializedMergeExonsChange, options?: ChangeOptions) {
48
+ super(json, options)
49
+ this.changes = 'changes' in json ? json.changes : [json]
50
+ }
51
+ // eslint-disable-next-line @typescript-eslint/class-literal-property-style
52
+ get notification() {
53
+ return 'Exons successfully merged'
54
+ }
55
+
56
+ toJSON(): SerializedMergeExonsChange {
57
+ const { assembly, changedIds, changes, typeName } = this
58
+ if (changes.length === 1) {
59
+ const [{ firstExon, secondExon, parentFeatureId }] = changes
60
+
61
+ return {
62
+ typeName,
63
+ changedIds,
64
+ assembly,
65
+ firstExon,
66
+ secondExon,
67
+ parentFeatureId,
68
+ }
69
+ }
70
+ return { typeName, changedIds, assembly, changes }
71
+ }
72
+
73
+ async executeOnServer(backend: ServerDataStore) {
74
+ const { featureModel, session } = backend
75
+ const { changes, logger } = this
76
+ for (const change of changes) {
77
+ const { firstExon, secondExon } = change
78
+ const topLevelFeature = await featureModel
79
+ .findOne({ allIds: firstExon._id })
80
+ .session(session)
81
+ .exec()
82
+ if (!topLevelFeature) {
83
+ const errMsg = `*** ERROR: The following featureId was not found in database ='${firstExon._id}'`
84
+ logger.error(errMsg)
85
+ throw new Error(errMsg)
86
+ }
87
+ const mergedExon = this.getFeatureFromId(topLevelFeature, firstExon._id)
88
+ if (!mergedExon) {
89
+ const errMsg = 'ERROR when searching feature by featureId'
90
+ logger.error(errMsg)
91
+ throw new Error(errMsg)
92
+ }
93
+ mergedExon.min = Math.min(firstExon.min, secondExon.min)
94
+ mergedExon.max = Math.max(firstExon.max, secondExon.max)
95
+
96
+ const mergedAttributes: Record<string, string[]> = mergedExon.attributes
97
+ ? JSON.parse(JSON.stringify(mergedExon.attributes))
98
+ : {}
99
+ mergedAttributes.merged_with = [
100
+ stringifyAttributes(attributesToRecords(secondExon.attributes)),
101
+ ]
102
+ mergedExon.attributes = mergedAttributes
103
+
104
+ const deletedIds = findAndDeleteChildFeature(
105
+ topLevelFeature,
106
+ secondExon._id,
107
+ this,
108
+ )
109
+ deletedIds.push(secondExon._id)
110
+ topLevelFeature.allIds = topLevelFeature.allIds.filter(
111
+ (id) => !deletedIds.includes(id),
112
+ )
113
+ await topLevelFeature.save()
114
+ }
115
+ }
116
+
117
+ async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
118
+ throw new Error('executeOnLocalGFF3 not implemented')
119
+ }
120
+
121
+ async executeOnClient(dataStore: ClientDataStore) {
122
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
123
+ if (!dataStore) {
124
+ throw new Error('No data store')
125
+ }
126
+
127
+ for (const change of this.changes) {
128
+ const { firstExon, secondExon } = change
129
+ const mergedExon = dataStore.getFeature(firstExon._id)
130
+ if (!mergedExon) {
131
+ throw new Error(
132
+ `Could not find feature with identifier "${firstExon._id}"`,
133
+ )
134
+ }
135
+ mergedExon.setMin(Math.min(firstExon.min, secondExon.min))
136
+ mergedExon.setMax(Math.max(firstExon.max, secondExon.max))
137
+
138
+ const mrg = mergedExon.attributes.get('merged_with')?.slice() ?? []
139
+ const mergedWith = stringifyAttributes(
140
+ attributesToRecords(secondExon.attributes),
141
+ )
142
+ if (!mrg.includes(mergedWith)) {
143
+ mrg.push(mergedWith)
144
+ }
145
+ mergedExon.setAttribute('merged_with', mrg)
146
+
147
+ mergedExon.parent?.deleteChild(secondExon._id)
148
+ }
149
+ }
150
+
151
+ getInverse() {
152
+ const { assembly, changedIds, changes, logger } = this
153
+ const inverseChangedIds = [...changedIds].reverse()
154
+ const inverseChanges = [...changes].reverse().map((mergeExonChange) => ({
155
+ exonsToRestore: [mergeExonChange.firstExon, mergeExonChange.secondExon],
156
+ parentFeatureId: mergeExonChange.parentFeatureId,
157
+ }))
158
+ logger.debug?.(`INVERSE CHANGE '${JSON.stringify(inverseChanges)}'`)
159
+ return new UndoMergeExonsChange(
160
+ {
161
+ changedIds: inverseChangedIds,
162
+ typeName: 'UndoMergeExonsChange',
163
+ changes: inverseChanges,
164
+ assembly,
165
+ },
166
+ { logger },
167
+ )
168
+ }
169
+
170
+ // mergeAttributes(
171
+ // firstExon: AnnotationFeatureSnapshot,
172
+ // secondExon: AnnotationFeatureSnapshot,
173
+ // ): Record<string, string[]> {
174
+ // let mergedAttrs: Record<string, string[]> = {}
175
+ // if (firstExon.attributes) {
176
+ // // eslint-disable-next-line unicorn/prefer-structured-clone
177
+ // mergedAttrs = JSON.parse(JSON.stringify(firstExon.attributes))
178
+ // }
179
+
180
+ // if (secondExon.attributes) {
181
+ // // eslint-disable-next-line unicorn/prefer-structured-clone
182
+ // const attrs: Record<string, string[]> = JSON.parse(
183
+ // JSON.stringify(secondExon.attributes),
184
+ // )
185
+ // for (const key of Object.keys(attrs)) {
186
+ // if (key === '_id' || key === 'gff_id') {
187
+ // continue
188
+ // }
189
+ // if (!Object.keys(mergedAttrs).includes(key)) {
190
+ // mergedAttrs[key] = []
191
+ // }
192
+ // attrs[key].map((x) => {
193
+ // if (!mergedAttrs[key].includes(x)) {
194
+ // mergedAttrs[key].push(x)
195
+ // }
196
+ // })
197
+ // }
198
+ // }
199
+ // return mergedAttrs
200
+ // }
201
+ }