|
| 1 | +//------------------ |
| 2 | +// Helper Functions for Chromosome.tsx |
| 3 | +//------------------ |
| 4 | + |
| 5 | +import { |
| 6 | + ChromosomeItem, |
| 7 | + GeneAnnotationItem, |
| 8 | + GeneItem, |
| 9 | + GeneRange, |
| 10 | +} from './types' |
| 11 | + |
| 12 | +/** |
| 13 | + * formats a GeneItem into GeneAnnotationItem |
| 14 | + */ |
| 15 | +export const getGeneAnnotation = (gene: GeneItem): GeneAnnotationItem => { |
| 16 | + const genePixelLoc: number = ((gene.start + gene.end) / 2) * 0.000015 |
| 17 | + const geneAnnotation: GeneAnnotationItem = { |
| 18 | + id: gene.id, |
| 19 | + chromosome: gene.chromosome, |
| 20 | + location: genePixelLoc, |
| 21 | + strand: gene.strand, |
| 22 | + } |
| 23 | + return geneAnnotation |
| 24 | +} |
| 25 | +/** |
| 26 | + * Gets the Chromosome svg element. |
| 27 | + */ |
| 28 | +export const getChromosomeSvg = ( |
| 29 | + chrId: string |
| 30 | +): HTMLElement & SVGSVGElement => { |
| 31 | + const svg = document.getElementById(chrId + '_svg') as HTMLElement & |
| 32 | + SVGSVGElement |
| 33 | + return svg |
| 34 | +} |
| 35 | +/** |
| 36 | + * Gets the Chromosome top y-coordinate. |
| 37 | + */ |
| 38 | +export const getChromosomeYCoordinate = (id: string): number => { |
| 39 | + return getChromosomeSvg(id).getBoundingClientRect().top |
| 40 | +} |
| 41 | +/** |
| 42 | + * Gets the Chromosome right x-coordinate. |
| 43 | + */ |
| 44 | +export const getChromosomeXCoordinate = (id: string): number => { |
| 45 | + return getChromosomeSvg(id).getBoundingClientRect().right |
| 46 | +} |
| 47 | +/** |
| 48 | + * Gets the height of the Chromosome. |
| 49 | + */ |
| 50 | +export const getChromosomeHeight = (id: string): number => { |
| 51 | + return getChromosomeSvg(id).getBoundingClientRect().height |
| 52 | +} |
| 53 | +/** |
| 54 | + * Gets the number of base-pairs per pixel. |
| 55 | + */ |
| 56 | +export const getBpPerPixel = (chromosome: ChromosomeItem): number => { |
| 57 | + return chromosome.size / (getChromosomeHeight(chromosome.id) - 1) |
| 58 | +} |
| 59 | +/** |
| 60 | + * Converts a pixel value to the equivalent base-pair range. |
| 61 | + */ |
| 62 | +export const pixelToBp = ( |
| 63 | + range: GeneRange, |
| 64 | + chromosome: ChromosomeItem, |
| 65 | + pixel: number |
| 66 | +): GeneRange => { |
| 67 | + const id = chromosome.id |
| 68 | + if ( |
| 69 | + pixel > getChromosomeYCoordinate(id) && |
| 70 | + pixel < getChromosomeYCoordinate(id) + getChromosomeHeight(id) |
| 71 | + ) { |
| 72 | + const range = { |
| 73 | + start: Math.floor( |
| 74 | + (pixel - 1 - getChromosomeYCoordinate(id)) * getBpPerPixel(chromosome) + |
| 75 | + 1 |
| 76 | + ), |
| 77 | + end: Math.floor( |
| 78 | + (pixel - getChromosomeYCoordinate(id)) * getBpPerPixel(chromosome) |
| 79 | + ), |
| 80 | + } |
| 81 | + if (range.end > chromosome.size) { |
| 82 | + range.end = chromosome.size |
| 83 | + } |
| 84 | + return range |
| 85 | + } else { |
| 86 | + return { |
| 87 | + start: 0, |
| 88 | + end: 0, |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments