Skip to content

Commit caf38e6

Browse files
committed
Fixed unneccary gene anootation fetches - moved
fetch to index.tsx
1 parent d647495 commit caf38e6

5 files changed

Lines changed: 82 additions & 163 deletions

File tree

Eplant/views/ChromosomeViewer/Viewer/Chromosome.tsx

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// -------
22
// IMPORTS
33
// -------
4-
import React, { FC, useEffect, useLayoutEffect, useState } from 'react'
4+
import React, { FC, useEffect, useState } from 'react'
55

6-
import { useCollections, useGeneticElements } from '@eplant/state'
6+
import { useCollections } from '@eplant/state'
77
import { Unstable_Popup as Popup } from '@mui/base/Unstable_Popup'
88
import ArrowLeft from '@mui/icons-material/ArrowLeft'
99
import Box from '@mui/material/Box'
@@ -17,28 +17,32 @@ import {
1717
GeneAnnotationItem,
1818
GeneRange,
1919
} from '../types'
20-
21-
import GeneAnnotation from './GeneAnnotation'
22-
import GeneList from './GeneList'
2320
import {
2421
getChromosomeSvg,
2522
getChromosomeXCoordinate,
26-
getGeneAnnotation,
2723
pixelToBp,
28-
} from './utilities'
24+
} from '../utilities'
25+
26+
import GeneAnnotation from './GeneAnnotation'
27+
import GeneList from './GeneList'
2928

3029
//----------
3130
// TYPES
3231
//----------
3332
interface ChromosomeProps {
34-
scale: number
3533
chromosome: ChromosomeItem
34+
annotations: GeneAnnotationItem[]
35+
scale: number
3636
}
3737

3838
//----------
3939
// COMPONENT
4040
//----------
41-
const Chromosome: FC<ChromosomeProps> = ({ scale, chromosome }) => {
41+
const Chromosome: FC<ChromosomeProps> = ({
42+
chromosome,
43+
annotations,
44+
scale,
45+
}) => {
4246
// State
4347
const [isHovered, setIsHovered] = useState<boolean>(false)
4448
const [anchorOrigin, setAnchorOrigin] = useState<number[]>([])
@@ -47,13 +51,7 @@ const Chromosome: FC<ChromosomeProps> = ({ scale, chromosome }) => {
4751
start: 0,
4852
end: 0,
4953
})
50-
// Gene Annotation drawing
51-
const [geneAnnotationArray, setGeneAnnotationArray] = useState<
52-
GeneAnnotationItem[]
53-
>([])
5454

55-
// Global State
56-
const [geneticElements] = useGeneticElements()
5755
const [collections] = useCollections()
5856
const theme = useTheme()
5957

@@ -73,45 +71,6 @@ const Chromosome: FC<ChromosomeProps> = ({ scale, chromosome }) => {
7371
// Gene List popover variables
7472
const openPopup = Boolean(anchorEl)
7573

76-
// Fire before paint, converts geneticElements into geneAnnotationArray
77-
useLayoutEffect(() => {
78-
// TODO: move this to top level to prevent uneccessary api calls
79-
const poplar = false
80-
const species = poplar ? 'Populus_trichocarpa' : 'Arabidopsis_thaliana'
81-
let newGeneAnnotationArray: GeneAnnotationItem[] = []
82-
geneticElements.map((gene) => {
83-
// for each item in geneticElements, fetch it's gene information to add to it's geneAnnotation
84-
fetch(
85-
`https://bar.utoronto.ca/eplant${
86-
poplar ? '_poplar' : ''
87-
}/cgi-bin/querygene.cgi?species=${species}&term=${gene.id}`
88-
)
89-
.then((response) => response.json())
90-
.then((geneItem) => {
91-
if (geneItem.chromosome === chromosome.id) {
92-
newGeneAnnotationArray = geneAnnotationArray
93-
const geneAnnotation: GeneAnnotationItem =
94-
getGeneAnnotation(geneItem)
95-
96-
// Make sure new geneAnnotation is not already in geneAnnotationArray
97-
const isDuplicate = newGeneAnnotationArray.some((gene) => {
98-
if (gene.id === geneAnnotation.id) {
99-
return true
100-
}
101-
return false
102-
})
103-
if (!isDuplicate) {
104-
newGeneAnnotationArray.push(geneAnnotation)
105-
setGeneAnnotationArray(newGeneAnnotationArray)
106-
}
107-
}
108-
})
109-
.catch((err) => {
110-
console.log(err)
111-
})
112-
})
113-
}, [])
114-
11574
// Execute on first render, after paint
11675
useEffect(() => {
11776
const svg: HTMLElement & SVGSVGElement = getChromosomeSvg(chromosome.id)
@@ -288,7 +247,7 @@ const Chromosome: FC<ChromosomeProps> = ({ scale, chromosome }) => {
288247
</g>
289248
{/* GENES ANNOTATION TAGS */}
290249
<g id={`${chromosome.id}_geneAnnotationTags`}>
291-
{geneAnnotationArray.map((gene, i) => {
250+
{annotations.map((gene, i) => {
292251
// Flatten collections into array of gene IDs - important for not drawing removed gene annotations
293252
const flatGeneCollection = collections.flatMap(
294253
(collection) => collection.genes

Eplant/views/ChromosomeViewer/Viewer/GeneList.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import React, { FC, useEffect, useState } from 'react'
66
import List from '@mui/material/List'
77
import ListItem from '@mui/material/ListItem'
88
import ListItemButton from '@mui/material/ListItemButton'
9-
import ListItemIcon from '@mui/material/ListItemIcon'
109
import ListItemText from '@mui/material/ListItemText'
1110
import useTheme from '@mui/material/styles/useTheme'
1211

13-
import { GeneIcon } from '../icons'
1412
import { GeneItem } from '../types'
1513

1614
import GeneInfoPopup from './GeneInfoPopup'
@@ -47,12 +45,8 @@ const GeneList: FC<GeneListProps> = ({ id, start, end, anchorOrigin }) => {
4745
}/cgi-bin/querygenesbyposition.cgi?chromosome=${id}&start=${start}&end=${end}`
4846
)
4947
.then((response) => response.json())
50-
.then((json) => {
51-
setGeneList(json)
52-
})
53-
.catch((err) => {
54-
console.log(err)
55-
})
48+
.then((json) => setGeneList(json))
49+
.catch((err) => console.log(err))
5650
}, [])
5751
// --------------
5852
// EVENT HANDLERS
@@ -104,7 +98,7 @@ const GeneList: FC<GeneListProps> = ({ id, start, end, anchorOrigin }) => {
10498
className='geneAliases'
10599
style={{ color: theme.palette.secondary.main }}
106100
>
107-
{`/${gene.aliases[0]}`}
101+
{`/${gene?.aliases[0]}`}
108102
</span>
109103
)}
110104
</ListItemText>

Eplant/views/ChromosomeViewer/Viewer/Viewer.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import { FC } from 'react'
77
import Box from '@mui/material/Box'
88
import Typography from '@mui/material/Typography'
99

10-
import { ChromosomeItem } from '../types'
10+
import { ChromosomeItem, GeneAnnotationItem } from '../types'
1111

1212
import Chromosome from './Chromosome'
1313

1414
// TYPES
1515
interface ViewerProps {
1616
chromosomes: ChromosomeItem[]
17+
annotations: GeneAnnotationItem[]
1718
scale: number
1819
}
1920
//----------
2021
// COMPONENT
2122
//----------
22-
const Viewer: FC<ViewerProps> = ({ chromosomes, scale }) => {
23+
const Viewer: FC<ViewerProps> = ({ chromosomes, annotations, scale }) => {
2324
return (
2425
<div
2526
style={{
@@ -33,13 +34,23 @@ const Viewer: FC<ViewerProps> = ({ chromosomes, scale }) => {
3334
}}
3435
>
3536
{chromosomes.map((chromosome, i) => {
37+
const chromosomeAnnotations: GeneAnnotationItem[] = []
38+
annotations.map((gene, j) => {
39+
if (gene.chromosome === chromosome.id) {
40+
chromosomeAnnotations.push(gene)
41+
}
42+
})
3643
// Render a Chromosome component for each chromosome
3744
return (
3845
<Box key={i}>
3946
<Typography fontSize='30px' noWrap sx={{ marginLeft: '-20px' }}>
4047
{chromosome.name}
4148
</Typography>
42-
<Chromosome scale={scale} chromosome={chromosome} />
49+
<Chromosome
50+
chromosome={chromosome}
51+
annotations={chromosomeAnnotations}
52+
scale={scale}
53+
/>
4354
<Typography sx={{ fontSize: 8 }} noWrap>
4455
{(chromosome.size * 0.000001).toLocaleString()}Mb
4556
</Typography>

Eplant/views/ChromosomeViewer/Viewer/utilities.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)