@apollo-annotation/shared 0.1.18 → 0.1.20

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 (157) hide show
  1. package/dist/Changes/AddAssemblyAndFeaturesFromFileChange.d.ts +33 -0
  2. package/dist/Changes/AddAssemblyAndFeaturesFromFileChange.js +83 -0
  3. package/dist/Changes/AddAssemblyAndFeaturesFromFileChange.js.map +1 -0
  4. package/dist/Changes/AddAssemblyFromExternalChange.d.ts +34 -0
  5. package/dist/Changes/AddAssemblyFromExternalChange.js +97 -0
  6. package/dist/Changes/AddAssemblyFromExternalChange.js.map +1 -0
  7. package/dist/Changes/AddAssemblyFromFileChange.d.ts +43 -0
  8. package/dist/Changes/AddAssemblyFromFileChange.js +140 -0
  9. package/dist/Changes/AddAssemblyFromFileChange.js.map +1 -0
  10. package/dist/Changes/AddFeatureChange.d.ts +35 -0
  11. package/dist/Changes/AddFeatureChange.js +169 -0
  12. package/dist/Changes/AddFeatureChange.js.map +1 -0
  13. package/dist/Changes/AddFeaturesFromFileChange.d.ts +32 -0
  14. package/dist/Changes/AddFeaturesFromFileChange.js +76 -0
  15. package/dist/Changes/AddFeaturesFromFileChange.js.map +1 -0
  16. package/dist/Changes/AddRefSeqAliasesChange.d.ts +20 -0
  17. package/dist/Changes/AddRefSeqAliasesChange.js +61 -0
  18. package/dist/Changes/AddRefSeqAliasesChange.js.map +1 -0
  19. package/dist/Changes/DeleteAssemblyChange.d.ts +18 -0
  20. package/dist/Changes/DeleteAssemblyChange.js +72 -0
  21. package/dist/Changes/DeleteAssemblyChange.js.map +1 -0
  22. package/dist/Changes/DeleteFeatureChange.d.ts +42 -0
  23. package/dist/Changes/DeleteFeatureChange.js +143 -0
  24. package/dist/Changes/DeleteFeatureChange.js.map +1 -0
  25. package/dist/Changes/DeleteUserChange.d.ts +25 -0
  26. package/dist/Changes/DeleteUserChange.js +53 -0
  27. package/dist/Changes/DeleteUserChange.js.map +1 -0
  28. package/dist/Changes/FeatureAttributeChange.d.ts +31 -0
  29. package/dist/Changes/FeatureAttributeChange.js +112 -0
  30. package/dist/Changes/FeatureAttributeChange.js.map +1 -0
  31. package/dist/Changes/FromFileBaseChange.d.ts +9 -0
  32. package/dist/Changes/FromFileBaseChange.js +187 -0
  33. package/dist/Changes/FromFileBaseChange.js.map +1 -0
  34. package/dist/Changes/ImportJBrowseConfigChange.d.ts +43 -0
  35. package/dist/Changes/ImportJBrowseConfigChange.js +67 -0
  36. package/dist/Changes/ImportJBrowseConfigChange.js.map +1 -0
  37. package/dist/Changes/LocationEndChange.d.ts +32 -0
  38. package/dist/Changes/LocationEndChange.js +129 -0
  39. package/dist/Changes/LocationEndChange.js.map +1 -0
  40. package/dist/Changes/LocationStartChange.d.ts +32 -0
  41. package/dist/Changes/LocationStartChange.js +129 -0
  42. package/dist/Changes/LocationStartChange.js.map +1 -0
  43. package/dist/Changes/StrandChange.d.ts +31 -0
  44. package/dist/Changes/StrandChange.js +114 -0
  45. package/dist/Changes/StrandChange.js.map +1 -0
  46. package/dist/Changes/TypeChange.d.ts +31 -0
  47. package/dist/Changes/TypeChange.js +114 -0
  48. package/dist/Changes/TypeChange.js.map +1 -0
  49. package/dist/Changes/UserChange.d.ts +25 -0
  50. package/dist/Changes/UserChange.js +51 -0
  51. package/dist/Changes/UserChange.js.map +1 -0
  52. package/dist/Changes/index.d.ts +50 -0
  53. package/dist/Changes/index.js +55 -0
  54. package/dist/Changes/index.js.map +1 -0
  55. package/dist/Checks/CDSCheck.d.ts +8 -0
  56. package/dist/Checks/CDSCheck.js +197 -0
  57. package/dist/Checks/CDSCheck.js.map +1 -0
  58. package/dist/Checks/index.d.ts +1 -0
  59. package/dist/Checks/index.js +5 -0
  60. package/dist/Checks/index.js.map +1 -0
  61. package/dist/Common/index.d.ts +1 -0
  62. package/dist/Common/index.js +5 -0
  63. package/dist/Common/index.js.map +1 -0
  64. package/dist/Common/jwtPayload.d.ts +12 -0
  65. package/dist/Common/jwtPayload.js +16 -0
  66. package/dist/Common/jwtPayload.js.map +1 -0
  67. package/dist/GFF3/gff3ToAnnotationFeature.d.ts +3 -0
  68. package/dist/GFF3/gff3ToAnnotationFeature.js +211 -0
  69. package/dist/GFF3/gff3ToAnnotationFeature.js.map +1 -0
  70. package/dist/GFF3/gff3ToAnnotationFeature.test.d.ts +1 -0
  71. package/dist/GFF3/gff3ToAnnotationFeature.test.js +174 -0
  72. package/dist/GFF3/gff3ToAnnotationFeature.test.js.map +1 -0
  73. package/dist/GFF3/gffReservedKeys.d.ts +5 -0
  74. package/dist/GFF3/gffReservedKeys.js +34 -0
  75. package/dist/GFF3/gffReservedKeys.js.map +1 -0
  76. package/dist/GFF3/index.d.ts +2 -0
  77. package/dist/GFF3/index.js +6 -0
  78. package/dist/GFF3/index.js.map +1 -0
  79. package/dist/Messages.d.ts +28 -0
  80. package/dist/Messages.js +3 -0
  81. package/dist/Messages.js.map +1 -0
  82. package/dist/Operations/GetAssembliesOperation.d.ts +13 -0
  83. package/dist/Operations/GetAssembliesOperation.js +20 -0
  84. package/dist/Operations/GetAssembliesOperation.js.map +1 -0
  85. package/dist/Operations/GetFeaturesOperation.d.ts +26 -0
  86. package/dist/Operations/GetFeaturesOperation.js +42 -0
  87. package/dist/Operations/GetFeaturesOperation.js.map +1 -0
  88. package/dist/Operations/index.d.ts +8 -0
  89. package/dist/Operations/index.js +10 -0
  90. package/dist/Operations/index.js.map +1 -0
  91. package/dist/Validations/CoreValidation.d.ts +17 -0
  92. package/dist/Validations/CoreValidation.js +36 -0
  93. package/dist/Validations/CoreValidation.js.map +1 -0
  94. package/dist/Validations/ParentChildValidation.d.ts +17 -0
  95. package/dist/Validations/ParentChildValidation.js +61 -0
  96. package/dist/Validations/ParentChildValidation.js.map +1 -0
  97. package/dist/Validations/Validation.d.ts +27 -0
  98. package/dist/Validations/Validation.js +27 -0
  99. package/dist/Validations/Validation.js.map +1 -0
  100. package/dist/Validations/ValidationSet.d.ts +24 -0
  101. package/dist/Validations/ValidationSet.js +86 -0
  102. package/dist/Validations/ValidationSet.js.map +1 -0
  103. package/dist/Validations/index.d.ts +4 -0
  104. package/dist/Validations/index.js +8 -0
  105. package/dist/Validations/index.js.map +1 -0
  106. package/dist/Validations/soSequenceTypes.d.ts +2 -0
  107. package/dist/Validations/soSequenceTypes.js +1868 -0
  108. package/dist/Validations/soSequenceTypes.js.map +1 -0
  109. package/dist/index.d.ts +8 -0
  110. package/dist/index.js +12 -0
  111. package/dist/index.js.map +1 -0
  112. package/dist/tsconfig.tsbuildinfo +1 -0
  113. package/dist/util.d.ts +4 -0
  114. package/dist/util.js +102 -0
  115. package/dist/util.js.map +1 -0
  116. package/package.json +17 -8
  117. package/src/Changes/AddAssemblyAndFeaturesFromFileChange.ts +13 -9
  118. package/src/Changes/AddAssemblyFromFileChange.ts +129 -43
  119. package/src/Changes/AddFeatureChange.ts +2 -3
  120. package/src/Changes/AddFeaturesFromFileChange.ts +8 -3
  121. package/src/Changes/AddRefSeqAliasesChange.ts +88 -0
  122. package/src/Changes/DeleteAssemblyChange.ts +1 -1
  123. package/src/Changes/DeleteFeatureChange.ts +1 -1
  124. package/src/Changes/FeatureAttributeChange.ts +1 -1
  125. package/src/Changes/FromFileBaseChange.ts +221 -0
  126. package/src/Changes/ImportJBrowseConfigChange.ts +116 -0
  127. package/src/Changes/LocationEndChange.ts +25 -18
  128. package/src/Changes/LocationStartChange.ts +25 -18
  129. package/src/Changes/TypeChange.ts +1 -1
  130. package/src/Changes/index.ts +6 -6
  131. package/src/Checks/CDSCheck.ts +125 -173
  132. package/src/Common/jwtPayload.ts +1 -1
  133. package/src/GFF3/gff3ToAnnotationFeature.test.ts +209 -0
  134. package/src/GFF3/gff3ToAnnotationFeature.ts +257 -0
  135. package/src/GFF3/gffReservedKeys.ts +61 -0
  136. package/src/GFF3/index.ts +2 -0
  137. package/src/Operations/GetFeaturesOperation.ts +2 -2
  138. package/src/Validations/ParentChildValidation.ts +5 -5
  139. package/src/index.ts +1 -0
  140. package/src/util.ts +16 -12
  141. package/test_data/braker.gff +13 -0
  142. package/test_data/example01.gff3 +26 -0
  143. package/test_data/example01.json +236 -0
  144. package/test_data/example02.json +225 -0
  145. package/test_data/example04.json +225 -0
  146. package/test_data/example05.gff3 +30 -0
  147. package/test_data/example06.gff3 +30 -0
  148. package/test_data/example07.gff3 +30 -0
  149. package/test_data/gene_mrna.gff3 +4 -0
  150. package/test_data/gene_representations.gff3 +1781 -0
  151. package/test_data/one_cds.gff3 +9 -0
  152. package/test_data/one_cds.json +67 -0
  153. package/test_data/two_cds.gff3 +9 -0
  154. package/test_data/two_cds.json +67 -0
  155. package/tsconfig.json +4 -4
  156. package/src/Changes/DiscontinuousLocationEndChange.ts +0 -182
  157. package/src/Changes/DiscontinuousLocationStartChange.ts +0 -182
@@ -0,0 +1,9 @@
1
+ ##gff-version 3
2
+ ##sequence-region chr1 1000 9000
3
+ #example01
4
+ chr1 . gene 1000 9000 . + . ID=gene10001;Name=EDEN;testid=t003
5
+ chr1 . mRNA 1050 9000 . + . ID=mRNA10001;Parent=gene10001;Name=EDEN.1;testid=t004,t001,t004
6
+ chr1 . exon 1050 1500 . + . ID=exon10001;Parent=mRNA10001;testid=t007
7
+ chr1 . exon 5000 5500 . + . ID=exon10004;Parent=mRNA10001;testid=t010
8
+ chr1 . CDS 1201 1500 . + 0 ID=cds10001;Parent=mRNA10001;Name=edenprotein.1;testid=t012,t013,t014
9
+ chr1 . CDS 5000 5000 . + 0 ID=cds10001;Parent=mRNA10001;Name=edenprotein.1;testid=t015,t014
@@ -0,0 +1,67 @@
1
+ {
2
+ "_id": "66d70e4ccc30b55b65e5f619",
3
+ "refSeq": "chr1",
4
+ "type": "gene",
5
+ "min": 999,
6
+ "max": 9000,
7
+ "strand": 1,
8
+ "children": {
9
+ "66d70e4ccc30b55b65e5f618": {
10
+ "_id": "66d70e4ccc30b55b65e5f618",
11
+ "refSeq": "chr1",
12
+ "type": "mRNA",
13
+ "min": 1049,
14
+ "max": 9000,
15
+ "strand": 1,
16
+ "children": {
17
+ "66d70e4ccc30b55b65e5f615": {
18
+ "_id": "66d70e4ccc30b55b65e5f615",
19
+ "refSeq": "chr1",
20
+ "type": "exon",
21
+ "min": 1049,
22
+ "max": 1500,
23
+ "strand": 1,
24
+ "attributes": {
25
+ "gff_id": ["exon10001"],
26
+ "testid": ["t007"]
27
+ }
28
+ },
29
+ "66d70e4ccc30b55b65e5f616": {
30
+ "_id": "66d70e4ccc30b55b65e5f616",
31
+ "refSeq": "chr1",
32
+ "type": "exon",
33
+ "min": 4999,
34
+ "max": 5500,
35
+ "strand": 1,
36
+ "attributes": {
37
+ "gff_id": ["exon10004"],
38
+ "testid": ["t010"]
39
+ }
40
+ },
41
+ "66d70e4ccc30b55b65e5f617": {
42
+ "_id": "66d70e4ccc30b55b65e5f617",
43
+ "refSeq": "chr1",
44
+ "type": "CDS",
45
+ "min": 1200,
46
+ "max": 5000,
47
+ "strand": 1,
48
+ "attributes": {
49
+ "gff_id": ["cds10001"],
50
+ "gff_name": ["edenprotein.1"],
51
+ "testid": ["t012", "t013", "t014", "t015"]
52
+ }
53
+ }
54
+ },
55
+ "attributes": {
56
+ "gff_id": ["mRNA10001"],
57
+ "gff_name": ["EDEN.1"],
58
+ "testid": ["t004", "t001", "t004"]
59
+ }
60
+ }
61
+ },
62
+ "attributes": {
63
+ "gff_id": ["gene10001"],
64
+ "gff_name": ["EDEN"],
65
+ "testid": ["t003"]
66
+ }
67
+ }
@@ -0,0 +1,9 @@
1
+ ##gff-version 3
2
+ ##sequence-region chr1 1000 9000
3
+ #example01
4
+ chr1 . gene 1000 9000 . + . ID=gene10001;Name=EDEN;testid=t003
5
+ chr1 . mRNA 1050 9000 . + . ID=mRNA10001;Parent=gene10001;Name=EDEN.1;testid=t004,t001,t004
6
+ chr1 . exon 1050 1500 . + . ID=exon10001;Parent=mRNA10001;testid=t007
7
+ chr1 . exon 5000 5500 . + . ID=exon10004;Parent=mRNA10001;testid=t010
8
+ chr1 . CDS 1201 1500 . + 0 ID=cds10001;Parent=mRNA10001;Name=edenprotein.1;testid=t012,t013,t014
9
+ chr1 . CDS 5000 5000 . + 0 ID=cds10001;Parent=mRNA10001;Name=edenprotein.1;testid=t014
@@ -0,0 +1,67 @@
1
+ {
2
+ "_id": "66d70f3b9c7a7460925687a3",
3
+ "refSeq": "chr1",
4
+ "type": "gene",
5
+ "min": 999,
6
+ "max": 9000,
7
+ "strand": 1,
8
+ "children": {
9
+ "66d70f3b9c7a7460925687a2": {
10
+ "_id": "66d70f3b9c7a7460925687a2",
11
+ "refSeq": "chr1",
12
+ "type": "mRNA",
13
+ "min": 1049,
14
+ "max": 9000,
15
+ "strand": 1,
16
+ "children": {
17
+ "66d70f3b9c7a74609256879f": {
18
+ "_id": "66d70f3b9c7a74609256879f",
19
+ "refSeq": "chr1",
20
+ "type": "exon",
21
+ "min": 1049,
22
+ "max": 1500,
23
+ "strand": 1,
24
+ "attributes": {
25
+ "gff_id": ["exon10001"],
26
+ "testid": ["t007"]
27
+ }
28
+ },
29
+ "66d70f3b9c7a7460925687a0": {
30
+ "_id": "66d70f3b9c7a7460925687a0",
31
+ "refSeq": "chr1",
32
+ "type": "exon",
33
+ "min": 4999,
34
+ "max": 5500,
35
+ "strand": 1,
36
+ "attributes": {
37
+ "gff_id": ["exon10004"],
38
+ "testid": ["t010"]
39
+ }
40
+ },
41
+ "66d70f3b9c7a7460925687a1": {
42
+ "_id": "66d70f3b9c7a7460925687a1",
43
+ "refSeq": "chr1",
44
+ "type": "CDS",
45
+ "min": 1200,
46
+ "max": 5000,
47
+ "strand": 1,
48
+ "attributes": {
49
+ "gff_id": ["cds10001"],
50
+ "gff_name": ["edenprotein.1"],
51
+ "testid": ["t012", "t013", "t014"]
52
+ }
53
+ }
54
+ },
55
+ "attributes": {
56
+ "gff_id": ["mRNA10001"],
57
+ "gff_name": ["EDEN.1"],
58
+ "testid": ["t004", "t001", "t004"]
59
+ }
60
+ }
61
+ },
62
+ "attributes": {
63
+ "gff_id": ["gene10001"],
64
+ "gff_name": ["EDEN"],
65
+ "testid": ["t003"]
66
+ }
67
+ }
package/tsconfig.json CHANGED
@@ -12,8 +12,8 @@
12
12
  },
13
13
  "include": ["./src"],
14
14
  "references": [
15
- { "path": "../apollo-mst"},
16
- { "path": "../apollo-schemas"},
17
- { "path": "../apollo-common"},
18
- ]
15
+ { "path": "../apollo-mst" },
16
+ { "path": "../apollo-schemas" },
17
+ { "path": "../apollo-common" },
18
+ ],
19
19
  }
@@ -1,182 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
- /* eslint-disable @typescript-eslint/restrict-template-expressions */
3
- /* eslint-disable @typescript-eslint/require-await */
4
- import {
5
- ChangeOptions,
6
- ClientDataStore,
7
- FeatureChange,
8
- LocalGFF3DataStore,
9
- SerializedFeatureChange,
10
- ServerDataStore,
11
- } from '@apollo-annotation/common'
12
-
13
- interface SerializedDiscontinuousLocationEndChangeBase
14
- extends SerializedFeatureChange {
15
- typeName: 'DiscontinuousLocationEndChange'
16
- }
17
-
18
- interface DiscontinuousLocationEndChangeDetails {
19
- featureId: string
20
- oldEnd: number
21
- newEnd: number
22
- index: number
23
- }
24
-
25
- interface SerializedDiscontinuousLocationEndChangeSingle
26
- extends SerializedDiscontinuousLocationEndChangeBase,
27
- DiscontinuousLocationEndChangeDetails {}
28
-
29
- interface SerializedDiscontinuousLocationEndChangeMultiple
30
- extends SerializedDiscontinuousLocationEndChangeBase {
31
- changes: DiscontinuousLocationEndChangeDetails[]
32
- }
33
-
34
- type SerializedDiscontinuousLocationEndChange =
35
- | SerializedDiscontinuousLocationEndChangeSingle
36
- | SerializedDiscontinuousLocationEndChangeMultiple
37
-
38
- export class DiscontinuousLocationEndChange extends FeatureChange {
39
- typeName = 'DiscontinuousLocationEndChange' as const
40
- changes: DiscontinuousLocationEndChangeDetails[]
41
-
42
- constructor(
43
- json: SerializedDiscontinuousLocationEndChange,
44
- options?: ChangeOptions,
45
- ) {
46
- super(json, options)
47
- this.changes = 'changes' in json ? json.changes : [json]
48
- }
49
-
50
- toJSON(): SerializedDiscontinuousLocationEndChange {
51
- const { assembly, changedIds, changes, typeName } = this
52
- if (changes.length === 1) {
53
- const [{ featureId, index, newEnd, oldEnd }] = changes
54
- return {
55
- typeName,
56
- changedIds,
57
- assembly,
58
- featureId,
59
- oldEnd,
60
- newEnd,
61
- index,
62
- }
63
- }
64
- return { typeName, changedIds, assembly, changes }
65
- }
66
-
67
- async executeOnServer(backend: ServerDataStore) {
68
- const { featureModel, session } = backend
69
- const { changes, logger } = this
70
- for (const change of changes) {
71
- const { featureId, index, newEnd, oldEnd: expectedOldEnd } = change
72
- const topLevelFeature = await featureModel
73
- .findOne({ allIds: featureId })
74
- .session(session)
75
- .exec()
76
-
77
- if (!topLevelFeature) {
78
- const errMsg = `ERROR: The following featureId was not found in database ='${featureId}'`
79
- logger.error(errMsg)
80
- throw new Error(errMsg)
81
- }
82
-
83
- const feature = this.getFeatureFromId(topLevelFeature, featureId)
84
- if (!feature) {
85
- const errMsg = 'ERROR when searching feature by featureId'
86
- logger.error(errMsg)
87
- throw new Error(errMsg)
88
- }
89
- logger.debug?.(`*** Found feature: ${JSON.stringify(feature)}`)
90
- if (
91
- !feature.discontinuousLocations ||
92
- feature.discontinuousLocations.length === 0
93
- ) {
94
- const errMsg =
95
- 'Must use "LocationEndChange" to change a feature end that does not have discontinuous locations'
96
- logger.error(errMsg)
97
- throw new Error(errMsg)
98
- }
99
- const oldEnd = feature.discontinuousLocations[index].end
100
- if (oldEnd !== expectedOldEnd) {
101
- const errMsg = `Location's current end value ${oldEnd} doesn't match with expected value ${expectedOldEnd}`
102
- logger.error(errMsg)
103
- throw new Error(errMsg)
104
- }
105
- const { start } = feature.discontinuousLocations[index]
106
- if (newEnd <= start) {
107
- const errMsg = `location end (${newEnd}) can't be smaller than location start (${start})`
108
- logger.error(errMsg)
109
- throw new Error(errMsg)
110
- }
111
- const nextLocation = feature.discontinuousLocations[index + 1]
112
- if (nextLocation && newEnd >= nextLocation.start) {
113
- const errMsg = `Location end (${newEnd}) can't be larger than the next location's start (${nextLocation.start})`
114
- logger.error(errMsg)
115
- throw new Error(errMsg)
116
- }
117
- feature.discontinuousLocations[index].end = newEnd
118
- if (index === feature.discontinuousLocations.length - 1) {
119
- feature.end = newEnd
120
- }
121
-
122
- try {
123
- if (topLevelFeature._id.equals(feature._id)) {
124
- topLevelFeature.markModified('discontinuousLocations')
125
- } else {
126
- topLevelFeature.markModified('children')
127
- }
128
- await topLevelFeature.save()
129
- } catch (error) {
130
- logger.debug?.(`*** FAILED: ${error}`)
131
- throw error
132
- }
133
- }
134
- }
135
-
136
- async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
137
- throw new Error('executeOnLocalGFF3 not implemented')
138
- }
139
-
140
- async executeOnClient(dataStore: ClientDataStore) {
141
- if (!dataStore) {
142
- throw new Error('No data store')
143
- }
144
- for (const [idx, changedId] of this.changedIds.entries()) {
145
- const feature = dataStore.getFeature(changedId)
146
- if (!feature) {
147
- throw new Error(`Could not find feature with identifier "${changedId}"`)
148
- }
149
- const { index, newEnd } = this.changes[idx]
150
- feature.setCDSDiscontinuousLocationEnd(newEnd, index)
151
- }
152
- }
153
-
154
- getInverse() {
155
- const { assembly, changedIds, changes, logger, typeName } = this
156
- const inverseChangedIds = [...changedIds].reverse()
157
- const inverseChanges = [...changes].reverse().map((change) => ({
158
- featureId: change.featureId,
159
- oldEnd: change.newEnd,
160
- newEnd: change.oldEnd,
161
- index: change.index,
162
- }))
163
- return new DiscontinuousLocationEndChange(
164
- {
165
- changedIds: inverseChangedIds,
166
- typeName,
167
- changes: inverseChanges,
168
- assembly,
169
- },
170
- { logger },
171
- )
172
- }
173
- }
174
-
175
- export function isDiscontinuousLocationEndChange(
176
- change: unknown,
177
- ): change is DiscontinuousLocationEndChange {
178
- return (
179
- (change as DiscontinuousLocationEndChange).typeName ===
180
- 'DiscontinuousLocationEndChange'
181
- )
182
- }
@@ -1,182 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-condition */
2
- /* eslint-disable @typescript-eslint/restrict-template-expressions */
3
- /* eslint-disable @typescript-eslint/require-await */
4
- import {
5
- ChangeOptions,
6
- ClientDataStore,
7
- FeatureChange,
8
- LocalGFF3DataStore,
9
- SerializedFeatureChange,
10
- ServerDataStore,
11
- } from '@apollo-annotation/common'
12
-
13
- interface SerializedDiscontinuousLocationStartChangeBase
14
- extends SerializedFeatureChange {
15
- typeName: 'DiscontinuousLocationStartChange'
16
- }
17
-
18
- interface DiscontinuousLocationStartChangeDetails {
19
- featureId: string
20
- oldStart: number
21
- newStart: number
22
- index: number
23
- }
24
-
25
- interface SerializedDiscontinuousLocationStartChangeSingle
26
- extends SerializedDiscontinuousLocationStartChangeBase,
27
- DiscontinuousLocationStartChangeDetails {}
28
-
29
- interface SerializedDiscontinuousLocationStartChangeMultiple
30
- extends SerializedDiscontinuousLocationStartChangeBase {
31
- changes: DiscontinuousLocationStartChangeDetails[]
32
- }
33
-
34
- type SerializedDiscontinuousLocationStartChange =
35
- | SerializedDiscontinuousLocationStartChangeSingle
36
- | SerializedDiscontinuousLocationStartChangeMultiple
37
-
38
- export class DiscontinuousLocationStartChange extends FeatureChange {
39
- typeName = 'DiscontinuousLocationStartChange' as const
40
- changes: DiscontinuousLocationStartChangeDetails[]
41
-
42
- constructor(
43
- json: SerializedDiscontinuousLocationStartChange,
44
- options?: ChangeOptions,
45
- ) {
46
- super(json, options)
47
- this.changes = 'changes' in json ? json.changes : [json]
48
- }
49
-
50
- toJSON(): SerializedDiscontinuousLocationStartChange {
51
- const { assembly, changedIds, changes, typeName } = this
52
- if (changes.length === 1) {
53
- const [{ featureId, index, newStart, oldStart }] = changes
54
- return {
55
- typeName,
56
- changedIds,
57
- assembly,
58
- featureId,
59
- oldStart,
60
- newStart,
61
- index,
62
- }
63
- }
64
- return { typeName, changedIds, assembly, changes }
65
- }
66
-
67
- async executeOnServer(backend: ServerDataStore) {
68
- const { featureModel, session } = backend
69
- const { changes, logger } = this
70
- for (const change of changes) {
71
- const { featureId, index, newStart, oldStart: expectedOldStart } = change
72
- const topLevelFeature = await featureModel
73
- .findOne({ allIds: featureId })
74
- .session(session)
75
- .exec()
76
-
77
- if (!topLevelFeature) {
78
- const errMsg = `ERROR: The following featureId was not found in database ='${featureId}'`
79
- logger.error(errMsg)
80
- throw new Error(errMsg)
81
- }
82
-
83
- const feature = this.getFeatureFromId(topLevelFeature, featureId)
84
- if (!feature) {
85
- const errMsg = 'ERROR when searching feature by featureId'
86
- logger.error(errMsg)
87
- throw new Error(errMsg)
88
- }
89
- logger.debug?.(`*** Found feature: ${JSON.stringify(feature)}`)
90
- if (
91
- !feature.discontinuousLocations ||
92
- feature.discontinuousLocations.length === 0
93
- ) {
94
- const errMsg =
95
- 'Must use "LocationStartChange" to change a feature start that does not have discontinuous locations'
96
- logger.error(errMsg)
97
- throw new Error(errMsg)
98
- }
99
- const oldStart = feature.discontinuousLocations[index].start
100
- if (oldStart !== expectedOldStart) {
101
- const errMsg = `Location's current start value ${oldStart} doesn't match with expected value ${expectedOldStart}`
102
- logger.error(errMsg)
103
- throw new Error(errMsg)
104
- }
105
- const { end } = feature.discontinuousLocations[index]
106
- if (newStart >= end) {
107
- const errMsg = `location start (${newStart}) can't be larger than location end (${end})`
108
- logger.error(errMsg)
109
- throw new Error(errMsg)
110
- }
111
- const previousLocation = feature.discontinuousLocations[index - 1]
112
- if (previousLocation && newStart <= previousLocation.end) {
113
- const errMsg = `Location start (${newStart}) can't be larger than the previous location's end (${previousLocation.end})`
114
- logger.error(errMsg)
115
- throw new Error(errMsg)
116
- }
117
- feature.discontinuousLocations[index].start = newStart
118
- if (index === 0) {
119
- feature.start = newStart
120
- }
121
-
122
- try {
123
- if (topLevelFeature._id.equals(feature._id)) {
124
- topLevelFeature.markModified('discontinuousLocations')
125
- } else {
126
- topLevelFeature.markModified('children')
127
- }
128
- await topLevelFeature.save()
129
- } catch (error) {
130
- logger.debug?.(`*** FAILED: ${error}`)
131
- throw error
132
- }
133
- }
134
- }
135
-
136
- async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
137
- throw new Error('executeOnLocalGFF3 not implemented')
138
- }
139
-
140
- async executeOnClient(dataStore: ClientDataStore) {
141
- if (!dataStore) {
142
- throw new Error('No data store')
143
- }
144
- for (const [idx, changedId] of this.changedIds.entries()) {
145
- const feature = dataStore.getFeature(changedId)
146
- if (!feature) {
147
- throw new Error(`Could not find feature with identifier "${changedId}"`)
148
- }
149
- const { index, newStart } = this.changes[idx]
150
- feature.setCDSDiscontinuousLocationStart(newStart, index)
151
- }
152
- }
153
-
154
- getInverse() {
155
- const { assembly, changedIds, changes, logger, typeName } = this
156
- const inverseChangedIds = [...changedIds].reverse()
157
- const inverseChanges = [...changes].reverse().map((change) => ({
158
- featureId: change.featureId,
159
- oldStart: change.newStart,
160
- newStart: change.oldStart,
161
- index: change.index,
162
- }))
163
- return new DiscontinuousLocationStartChange(
164
- {
165
- changedIds: inverseChangedIds,
166
- typeName,
167
- changes: inverseChanges,
168
- assembly,
169
- },
170
- { logger },
171
- )
172
- }
173
- }
174
-
175
- export function isDiscontinuousLocationStartChange(
176
- change: unknown,
177
- ): change is DiscontinuousLocationStartChange {
178
- return (
179
- (change as DiscontinuousLocationStartChange).typeName ===
180
- 'DiscontinuousLocationStartChange'
181
- )
182
- }