Skip to content

Commit

Permalink
cds discontinuous start/end location change
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankbrgowda committed Sep 9, 2023
1 parent 3ec4ee2 commit e86c7ee
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 130 deletions.
18 changes: 18 additions & 0 deletions packages/apollo-mst/src/AnnotationFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ export const AnnotationFeature = types
self.start = start
}
},
setCDSDiscontinuousLocationStart(start: number, index: number) {
const dl = self.discontinuousLocations
if (dl && dl.length > 0 && dl[index].start !== start) {
dl[index].start = start
if (index === 0) {
self.start = start
}
}
},
setEnd(end: number) {
if (end < self.start) {
throw new Error(`End "${end}" is less than start "${self.start}"`)
Expand All @@ -121,6 +130,15 @@ export const AnnotationFeature = types
self.end = end
}
},
setCDSDiscontinuousLocationEnd(end: number, index: number) {
const dl = self.discontinuousLocations
if (dl && dl.length > 0 && dl[index].end !== end) {
dl[index].end = end
if (index === 0) {
self.end = end
}
}
},
setStrand(strand?: 1 | -1) {
self.strand = strand
},
Expand Down
158 changes: 158 additions & 0 deletions packages/apollo-shared/src/Changes/DiscontinuousLocationChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import {
ChangeOptions,
ClientDataStore,
FeatureChange,
LocalGFF3DataStore,
SerializedFeatureChange,
ServerDataStore,
} from 'apollo-common'

interface SerializedDiscontinuousLocationChangeBase
extends SerializedFeatureChange {
typeName: 'DiscontinuousLocationChange'
}

interface DiscontinuousLocationChangeDetails {
featureId: string
start?: {
newStart: number
index: number // discontinuous location index
}
end?: {
newEnd: number
index: number // discontinuous location index
}
}

interface SerializedDiscontinuousLocationChangeSingle
extends SerializedDiscontinuousLocationChangeBase,
DiscontinuousLocationChangeDetails {}

interface SerializedDiscontinuousLocationChangeMultiple
extends SerializedDiscontinuousLocationChangeBase {
changes: DiscontinuousLocationChangeDetails[]
}

type SerializedDiscontinuousLocationChange =
| SerializedDiscontinuousLocationChangeSingle
| SerializedDiscontinuousLocationChangeMultiple

export class DiscontinuousLocationChange extends FeatureChange {
typeName = 'DiscontinuousLocationChange' as const
changes: DiscontinuousLocationChangeDetails[]

constructor(
json: SerializedDiscontinuousLocationChange,
options?: ChangeOptions,
) {
super(json, options)
this.changes = 'changes' in json ? json.changes : [json]
}

toJSON(): SerializedDiscontinuousLocationChange {
const { assembly, changedIds, changes, typeName } = this
if (changes.length === 1) {
const [{ end, featureId, start }] = changes
return { typeName, changedIds, assembly, featureId, start, end }
}
return { typeName, changedIds, assembly, changes }
}

async executeOnServer(backend: ServerDataStore) {
const { featureModel, session } = backend
const { changes, logger } = this
for (const change of changes) {
const { end, featureId, start } = change
const topLevelFeature = await featureModel
.findOne({ allIds: featureId })
.session(session)
.exec()

if (!topLevelFeature) {
const errMsg = `ERROR: The following featureId was not found in database ='${featureId}'`
logger.error(errMsg)
throw new Error(errMsg)
}

const cdsFeature = this.getFeatureFromId(topLevelFeature, featureId)
if (!cdsFeature?.discontinuousLocations) {
const errMsg = 'ERROR when searching feature by featureId'
logger.error(errMsg)
throw new Error(errMsg)
}

let locChanged
if (start) {
cdsFeature.discontinuousLocations[start.index].start = start.newStart
if (start.index === 0) {
cdsFeature.start = start.newStart
}
locChanged = true
}

if (end) {
cdsFeature.discontinuousLocations[end.index].end = end.newEnd
if (end.index === 0) {
cdsFeature.end = end.newEnd
}
locChanged = true
}

if (locChanged) {
try {
// Mark as modified. Without this save() -method is not updating data in database
topLevelFeature.markModified('children')
await topLevelFeature.save()
} catch (error) {
logger.debug?.(`*** FAILED: ${error}`)
throw error
}
}
}
}

async executeOnLocalGFF3(_backend: LocalGFF3DataStore) {
throw new Error('executeOnLocalGFF3 not implemented')
}

async executeOnClient(dataStore: ClientDataStore) {
if (!dataStore) {
throw new Error('No data store')
}
for (const [idx, changedId] of this.changedIds.entries()) {
const feature = dataStore.getFeature(changedId)
if (!feature) {
throw new Error(`Could not find feature with identifier "${changedId}"`)
}
const change = this.changes[idx]
const { end, start } = change

if (start) {
feature.setCDSDiscontinuousLocationStart(start.newStart, start.index)
}

if (end) {
feature.setCDSDiscontinuousLocationEnd(end.newEnd, end.index)
}
}
}

getInverse() {
const { assembly, changedIds, changes, logger, typeName } = this
const inverseChangedIds = [...changedIds].reverse()
const inverseChanges = [...changes].reverse().map((c) => ({
featureId: c.featureId,
start: c.start,
end: c.end,
}))
return new DiscontinuousLocationChange(
{
changedIds: inverseChangedIds,
typeName,
changes: inverseChanges,
assembly,
},
{ logger },
)
}
}
3 changes: 3 additions & 0 deletions packages/apollo-shared/src/Changes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AddFeaturesFromFileChange } from './AddFeaturesFromFileChange'
import { DeleteAssemblyChange } from './DeleteAssemblyChange'
import { DeleteFeatureChange } from './DeleteFeatureChange'
import { DeleteUserChange } from './DeleteUserChange'
import { DiscontinuousLocationChange } from './DiscontinuousLocationChange'
import { FeatureAttributeChange } from './FeatureAttributeChange'
import { LocationEndChange } from './LocationEndChange'
import { LocationStartChange } from './LocationStartChange'
Expand All @@ -24,6 +25,7 @@ export const changes = {
DeleteUserChange,
LocationEndChange,
LocationStartChange,
DiscontinuousLocationChange,
TypeChange,
UserChange,
}
Expand All @@ -39,5 +41,6 @@ export * from './DeleteUserChange'
export * from './FeatureAttributeChange'
export * from './LocationEndChange'
export * from './LocationStartChange'
export * from './DiscontinuousLocationChange'
export * from './TypeChange'
export * from './UserChange'
Loading

0 comments on commit e86c7ee

Please sign in to comment.