Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankbrgowda committed Aug 20, 2024
1 parent 30b9fb3 commit 3044ad6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
2 changes: 0 additions & 2 deletions packages/apollo-common/src/Check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AnnotationFeature,
AnnotationFeatureSnapshot,
CheckResultSnapshot,
} from '@apollo-annotation/mst'
Expand All @@ -11,6 +10,5 @@ export abstract class Check {
abstract checkFeature(
featureSnapshot: AnnotationFeatureSnapshot,
getSequence: (start: number, end: number) => Promise<string>,
feature?: AnnotationFeature,
): Promise<CheckResultSnapshot[]>
}
6 changes: 2 additions & 4 deletions packages/apollo-mst/src/AnnotationFeatureModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export const AnnotationFeatureModel = types
min: number
max: number
phase: 0 | 1 | 2
strand: 1 | -1 | undefined
}[][] {
if (self.type !== 'mRNA') {
throw new Error(
Expand All @@ -135,10 +134,9 @@ export const AnnotationFeatureModel = types
min: number
max: number
phase: 0 | 1 | 2
strand: 1 | -1 | undefined
}[][] = []
for (const cds of cdsChildren) {
const { _id, max: cdsMax, min: cdsMin, strand } = cds
const { _id, max: cdsMax, min: cdsMin } = cds
const locs: {
min: number
max: number
Expand Down Expand Up @@ -168,7 +166,7 @@ export const AnnotationFeatureModel = types
| 0
| 1
| 2
return { ...loc, phase, _id, strand }
return { ...loc, phase, _id }
})
cdsLocations.push(phasedLocs)
}
Expand Down
88 changes: 55 additions & 33 deletions packages/apollo-shared/src/Checks/CDSCheck.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Check } from '@apollo-annotation/common'
import {
AnnotationFeature,
AnnotationFeatureSnapshot,
CheckResultSnapshot,
} from '@apollo-annotation/mst'
import { intersection2 } from '@jbrowse/core/util'
import ObjectID from 'bson-objectid'

enum STOP_CODONS {
Expand Down Expand Up @@ -35,7 +35,6 @@ interface CDSLocation {
min: number
max: number
phase: 0 | 1 | 2
strand: 1 | -1 | undefined
}

function reverseComplement(dna: string): string {
Expand All @@ -55,7 +54,7 @@ function reverseComplement(dna: string): string {
}

function checkCDS(
parent: AnnotationFeature,
parent: AnnotationFeatureSnapshot,
cdsSequence: string,
cdsMin: number,
cdsMax: number,
Expand Down Expand Up @@ -137,36 +136,73 @@ function getOriginalCodonLocation(
throw new Error(`Could not find original codon location for index ${index}`)
}

function getCdsLocations(mRNA: AnnotationFeatureSnapshot): CDSLocation[][] {
const { children, strand } = mRNA
if (!children) {
return []
}
const cdsChildren = Object.values(children).filter(
(child) => child.type === 'CDS',
)
if (cdsChildren.length === 0) {
return []
}
const cdsLocations: CDSLocation[][] = []
for (const cds of cdsChildren) {
const { _id, max: cdsMax, min: cdsMin } = cds
const locs: {
min: number
max: number
}[] = []
for (const child of Object.values(children)) {
if (child.type !== 'exon') {
continue
}
const [start, end] = intersection2(cdsMin, cdsMax, child.min, child.max)
if (start !== undefined && end !== undefined) {
locs.push({ min: start, max: end })
}
}
locs.sort(({ min: a }, { min: b }) => a - b)
if (strand === -1) {
locs.reverse()
}
let nextPhase: 0 | 1 | 2 = 0
const phasedLocs = locs.map((loc) => {
const phase = nextPhase
nextPhase = ((3 - ((loc.max - loc.min - phase + 3) % 3)) % 3) as 0 | 1 | 2
return { ...loc, phase, _id }
})
cdsLocations.push(phasedLocs)
}
return cdsLocations
}

export class CDSCheck extends Check {
name = 'CDSCheck'
version = 1
default = true

async checkFeature(
_featureSnapshot: AnnotationFeatureSnapshot,
featureSnapshot: AnnotationFeatureSnapshot,
getSequence: (start: number, end: number) => Promise<string>,
feature?: AnnotationFeature,
): Promise<CheckResultSnapshot[]> {
if (!feature) {
return []
}

const mRNAs: AnnotationFeature[] = []
if (feature.type === 'gene' && feature.children) {
for (const [, child] of feature.children) {
const mRNAs: AnnotationFeatureSnapshot[] = []
if (featureSnapshot.type === 'gene' && featureSnapshot.children) {
for (const child of Object.values(featureSnapshot.children)) {
if (child.type === 'mRNA') {
mRNAs.push(child)
}
}
}

if (feature.type === 'mRNA') {
mRNAs.push(feature)
if (featureSnapshot.type === 'mRNA') {
mRNAs.push(featureSnapshot)
}

const checkResults: CheckResultSnapshot[] = []
for (const mRNA of mRNAs) {
const { cdsLocations } = mRNA
const cdsLocations = getCdsLocations(mRNA)

if (cdsLocations.length === 0) {
// move to next mRNA
Expand All @@ -178,32 +214,18 @@ export class CDSCheck extends Check {
const codons: string[] = []
const cdsMin = cds.at(0)?.min
const cdsMax = cds.at(-1)?.max
const firstLocStrand = cds.at(0)?.strand
let isValidStrand = true

if (
cdsMin === undefined ||
cdsMax === undefined ||
firstLocStrand === undefined
) {

if (cdsMin === undefined || cdsMax === undefined) {
// move to next CDS
continue
}

for (const loc of cds) {
if (loc.strand !== firstLocStrand) {
isValidStrand = false
break
}
cdsSequence = cdsSequence + (await getSequence(loc.min, loc.max))
cdsIds.push(loc._id)
}

if (!isValidStrand) {
continue
}

if (firstLocStrand === -1) {
if (featureSnapshot.strand === -1) {
cdsSequence = reverseComplement(cdsSequence)
}

Expand All @@ -213,7 +235,7 @@ export class CDSCheck extends Check {

checkResults.push(
...checkCDS(
feature,
featureSnapshot,
cdsSequence,
cdsMin,
cdsMax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export async function checkFeatures(
getSnapshot(feature),
(start: number, stop: number) =>
Promise.resolve(ref.getSequence(start, stop)),
feature,
)
checkResults.push(...result)
}
Expand Down

0 comments on commit 3044ad6

Please sign in to comment.