Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy feature from evidence track #473

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable unicorn/no-useless-undefined */
/* eslint-disable @typescript-eslint/no-misused-promises */
import React, { useEffect, useMemo, useState } from 'react'

Check warning on line 4 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L4

Added line #L4 was not covered by tests

import {

Check warning on line 6 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L6

Added line #L6 was not covered by tests
Box,
Button,
Checkbox,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
FormControlLabel,
MenuItem,
Select,
SelectChangeEvent,
Typography,
} from '@mui/material'

import { Dialog } from './Dialog'

Check warning on line 21 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L21

Added line #L21 was not covered by tests
import { ApolloSessionModel } from '../session'
import { AnnotationFeatureSnapshot } from '@apollo-annotation/mst'
import { getRoot } from 'mobx-state-tree'

Check warning on line 24 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L24

Added line #L24 was not covered by tests
import { ApolloRootModel } from '../types'
import { ApolloInternetAccountModel } from '../ApolloInternetAccount/model'
import { AddFeatureChange } from '@apollo-annotation/shared'

Check warning on line 27 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L27

Added line #L27 was not covered by tests
import { Assembly } from '@jbrowse/core/assemblyManager/assembly'
import { AbstractSessionModel } from '@jbrowse/core/util'

interface CreateApolloAnnotationProps {
session: AbstractSessionModel
handleClose(): void
annotationFeature: AnnotationFeatureSnapshot
assembly: Assembly
}

// TODO: Integrate SO
const isGeneOrTranscript = (annotationFeature: AnnotationFeatureSnapshot) => {
return (

Check warning on line 40 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L39-L40

Added lines #L39 - L40 were not covered by tests
annotationFeature.type === 'gene' ||
annotationFeature.type === 'mRNA' ||
annotationFeature.type === 'transcript'

Check warning on line 43 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L42-L43

Added lines #L42 - L43 were not covered by tests
)
}

// TODO: Integrate SO
const isTranscript = (annotationFeature: AnnotationFeatureSnapshot) => {
return (

Check warning on line 49 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L48-L49

Added lines #L48 - L49 were not covered by tests
annotationFeature.type === 'mRNA' || annotationFeature.type === 'transcript'
)
}

export function CreateApolloAnnotation({

Check warning on line 54 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L54

Added line #L54 was not covered by tests
annotationFeature,
assembly,
handleClose,
session,
}: CreateApolloAnnotationProps) {
const apolloSessionModel = session as unknown as ApolloSessionModel
const { internetAccounts } = getRoot<ApolloRootModel>(session)
const apolloInternetAccounts = internetAccounts.filter(
(ia) => ia.type === 'ApolloInternetAccount',

Check warning on line 63 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L59-L63

Added lines #L59 - L63 were not covered by tests
) as ApolloInternetAccountModel[]
if (apolloInternetAccounts.length === 0) {
throw new Error('No Apollo internet account found')

Check warning on line 66 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L66

Added line #L66 was not covered by tests
}
const [apolloInternetAccount] = apolloInternetAccounts
const childIds = useMemo(

Check warning on line 69 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L68-L69

Added lines #L68 - L69 were not covered by tests
() => Object.keys(annotationFeature.children ?? {}),
[annotationFeature],
)

const [parentFeatureChecked, setParentFeatureChecked] = useState(true)
const [checkedChildrens, setCheckedChildrens] = useState<string[]>(childIds)
const [errorMessage, setErrorMessage] = useState('')
const [destinationFeatures, setDestinationFeatures] = useState<

Check warning on line 77 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L74-L77

Added lines #L74 - L77 were not covered by tests
AnnotationFeatureSnapshot[]
>([])
const [selectedDestinationFeature, setSelectedDestinationFeature] =
useState<AnnotationFeatureSnapshot>()

Check warning on line 81 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L81

Added line #L81 was not covered by tests

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of fetching features from the server, this should look at the already-fetched features stored in session.apolloDataStore.

const getFeatures = async (min: number, max: number) => {
const { baseURL } = apolloInternetAccount
const uri = new URL('features/getFeatures', baseURL)
const searchParams = new URLSearchParams({

Check warning on line 87 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L83-L87

Added lines #L83 - L87 were not covered by tests
refSeq: annotationFeature.refSeq,
start: min.toString(),
end: max.toString(),
})
uri.search = searchParams.toString()
const fetch = apolloInternetAccount.getFetcher({

Check warning on line 93 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L92-L93

Added lines #L92 - L93 were not covered by tests
locationType: 'UriLocation',
uri: uri.toString(),
})

const response = await fetch(uri.toString(), { method: 'GET' })

Check warning on line 98 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L98

Added line #L98 was not covered by tests

if (response.ok) {
const [features] = (await response.json()) as [

Check warning on line 101 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L101

Added line #L101 was not covered by tests
AnnotationFeatureSnapshot[],
]
return features

Check warning on line 104 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L104

Added line #L104 was not covered by tests
}
return []

Check warning on line 106 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L106

Added line #L106 was not covered by tests
}

if (childIds.every((childId) => checkedChildrens.includes(childId))) {
setParentFeatureChecked(true)
} else {
setSelectedDestinationFeature(undefined)
setParentFeatureChecked(false)

Check warning on line 113 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L110-L113

Added lines #L110 - L113 were not covered by tests

if (annotationFeature.children) {
const checkedAnnotationFeatureChildren = Object.values(

Check warning on line 116 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L116

Added line #L116 was not covered by tests
annotationFeature.children,
)
.filter((child) => isTranscript(child))
.filter((child) => checkedChildrens.includes(child._id))
const mins = checkedAnnotationFeatureChildren.map((f) => f.min)
const maxes = checkedAnnotationFeatureChildren.map((f) => f.max)
const min = Math.min(...mins)
const max = Math.max(...maxes)

Check warning on line 124 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L119-L124

Added lines #L119 - L124 were not covered by tests

getFeatures(min, max)
.then((features) => {
setDestinationFeatures(features)

Check warning on line 128 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L126-L128

Added lines #L126 - L128 were not covered by tests
})
.catch(() => {
setErrorMessage('Failed to fetch destination features')

Check warning on line 131 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L130-L131

Added lines #L130 - L131 were not covered by tests
})
}
}
}, [checkedChildrens])

const handleParentFeatureCheck = (

Check warning on line 137 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L137

Added line #L137 was not covered by tests
event: React.ChangeEvent<HTMLInputElement>,
) => {
const isChecked = event.target.checked
setParentFeatureChecked(isChecked)

Check warning on line 141 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L139-L141

Added lines #L139 - L141 were not covered by tests
setCheckedChildrens(isChecked ? childIds : [])
}

const handleChildFeatureCheck = (

Check warning on line 145 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L145

Added line #L145 was not covered by tests
event: React.ChangeEvent<HTMLInputElement>,
child: AnnotationFeatureSnapshot,
) => {
setCheckedChildrens((prevChecked) =>

Check warning on line 149 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L148-L149

Added lines #L148 - L149 were not covered by tests
event.target.checked
? [...prevChecked, child._id]
: prevChecked.filter((childId) => childId !== child._id),

Check warning on line 152 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L151-L152

Added lines #L151 - L152 were not covered by tests
)
}

const handleDestinationFeatureChange = (e: SelectChangeEvent) => {
const selectedFeature = destinationFeatures.find(
(f) => f._id === e.target.value,

Check warning on line 158 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L156-L158

Added lines #L156 - L158 were not covered by tests
)
setSelectedDestinationFeature(selectedFeature)

Check warning on line 160 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L160

Added line #L160 was not covered by tests
}

const handleCreateApolloAnnotation = async () => {

Check warning on line 163 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L163

Added line #L163 was not covered by tests
if (parentFeatureChecked) {
const change = new AddFeatureChange({

Check warning on line 165 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L165

Added line #L165 was not covered by tests
changedIds: [annotationFeature._id],
typeName: 'AddFeatureChange',
assembly: assembly.name,
addedFeature: annotationFeature,
})
await apolloSessionModel.apolloDataStore.changeManager.submit(change)
session.notify('Annotation added successfully', 'success')
handleClose()
} else {

Check warning on line 174 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L171-L174

Added lines #L171 - L174 were not covered by tests
if (!annotationFeature.children) {
return

Check warning on line 176 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L176

Added line #L176 was not covered by tests
}
if (!selectedDestinationFeature) {
return

Check warning on line 179 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L179

Added line #L179 was not covered by tests
}

for (const childId of checkedChildrens) {
const child = annotationFeature.children[childId]
const change = new AddFeatureChange({

Check warning on line 184 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L182-L184

Added lines #L182 - L184 were not covered by tests
parentFeatureId: selectedDestinationFeature._id,
changedIds: [selectedDestinationFeature._id],
typeName: 'AddFeatureChange',
assembly: assembly.name,
addedFeature: child,
})
await apolloSessionModel.apolloDataStore.changeManager.submit(change)
session.notify('Annotation added successfully', 'success')
handleClose()

Check warning on line 193 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L191-L193

Added lines #L191 - L193 were not covered by tests
}
}
}

return (

Check warning on line 198 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L198

Added line #L198 was not covered by tests
<Dialog
open
title="Create Apollo Annotation"
handleClose={handleClose}
fullWidth={true}
maxWidth="sm"
>
<DialogTitle fontSize={15}>
Select the feature to be copied to apollo track
</DialogTitle>
<DialogContent>
<Box sx={{ ml: 3 }}>
{isGeneOrTranscript(annotationFeature) && (
<FormControlLabel

Check warning on line 212 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L212

Added line #L212 was not covered by tests
control={
<Checkbox
size="small"
checked={parentFeatureChecked}
onChange={handleParentFeatureCheck}
/>
}
label={`${annotationFeature.type}:${annotationFeature.min}..${annotationFeature.max}`}
/>
)}
{annotationFeature.children && (
<Box sx={{ display: 'flex', flexDirection: 'column', ml: 3 }}>

Check warning on line 224 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L224

Added line #L224 was not covered by tests
{Object.values(annotationFeature.children)
.filter((child) => isTranscript(child))

Check warning on line 226 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L226

Added line #L226 was not covered by tests
.map((child) => (
<FormControlLabel

Check warning on line 228 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L228

Added line #L228 was not covered by tests
key={child._id}
control={
<Checkbox
size="small"
checked={checkedChildrens.includes(child._id)}
onChange={(e) => {
handleChildFeatureCheck(e, child)

Check warning on line 235 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L234-L235

Added lines #L234 - L235 were not covered by tests
}}
/>
}
label={`${child.type}:${child.min}..${child.max}`}
/>
))}
</Box>
)}
</Box>
{!parentFeatureChecked &&
checkedChildrens.length > 0 &&
destinationFeatures.length > 0 && (
<Box sx={{ ml: 3 }}>

Check warning on line 248 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L246-L248

Added lines #L246 - L248 were not covered by tests
<Typography variant="caption" fontSize={12}>
Select the destination feature to copy the selected features
</Typography>

<Box sx={{ mt: 1 }}>
<Select
labelId="label"
style={{ width: '100%' }}
value={selectedDestinationFeature?._id ?? ''}
onChange={handleDestinationFeatureChange}
>
{destinationFeatures.map((f) => (
<MenuItem key={f._id} value={f._id}>

Check warning on line 261 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L261

Added line #L261 was not covered by tests
{`${f.type}:${f.min}..${f.max}`}
</MenuItem>
))}
</Select>
</Box>
</Box>
)}
</DialogContent>
<DialogActions>
<Button
variant="contained"
type="submit"
disabled={
checkedChildrens.length === 0 ||
(!parentFeatureChecked &&
checkedChildrens.length > 0 &&
!selectedDestinationFeature)

Check warning on line 278 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L276-L278

Added lines #L276 - L278 were not covered by tests
}
onClick={handleCreateApolloAnnotation}
>
Create
</Button>
<Button variant="outlined" type="submit" onClick={handleClose}>
Cancel
</Button>
</DialogActions>
{errorMessage ? (
<DialogContent>

Check warning on line 289 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L289

Added line #L289 was not covered by tests
<DialogContentText color="error">{errorMessage}</DialogContentText>
</DialogContent>
) : null}

Check warning on line 292 in packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx

View check run for this annotation

Codecov / codecov/patch

packages/jbrowse-plugin-apollo/src/components/CreateApolloAnnotation.tsx#L292

Added line #L292 was not covered by tests
</Dialog>
)
}
Loading