|
| 1 | +import React from "react"; |
| 2 | +import { scaleLinear } from "d3-scale"; |
| 3 | +import { max } from "d3-array"; |
| 4 | + |
| 5 | +import BaseAlignment from "./components/BaseAlignment.jsx"; |
| 6 | +import SiteAxis from "./components/SiteAxis.jsx"; |
| 7 | +import SequenceAxis from "./components/SequenceAxis.jsx"; |
| 8 | +import Placeholder from "./components/Placeholder.jsx"; |
| 9 | +import BaseSequenceBarPlot from "./components/BaseSequenceBarPlot.jsx"; |
| 10 | +import fastaParser from "./helpers/fasta"; |
| 11 | +import ScrollBroadcaster from "./helpers/ScrollBroadcaster"; |
| 12 | +import AxisTop from "./components/AxisTop.jsx"; |
| 13 | +import { nucleotide_color, nucleotide_text_color } from "./helpers/colors"; |
| 14 | +import computeLabelWidth from "./helpers/computeLabelWidth"; |
| 15 | +import css_grid_format from "./helpers/format"; |
| 16 | + |
| 17 | +function SequenceBarChart(props) { |
| 18 | + const has_sequence_data = props.fasta || props.sequence_data; |
| 19 | + if (!has_sequence_data || !props.data) return <div />; |
| 20 | + const sequence_data = props.sequence_data || fastaParser(props.fasta), |
| 21 | + { width, bar_width, height, axis_height, site_size, label_padding } = props, |
| 22 | + label_width = computeLabelWidth(sequence_data, label_padding), |
| 23 | + full_pixel_width = sequence_data[0].seq.length * site_size, |
| 24 | + full_pixel_height = sequence_data.length * site_size, |
| 25 | + base_alignment_width = width - bar_width - label_width, |
| 26 | + base_alignment_height = height - axis_height, |
| 27 | + alignment_width = Math.min(full_pixel_width, base_alignment_width), |
| 28 | + alignment_height = Math.min(full_pixel_height, height - axis_height), |
| 29 | + scale = scaleLinear() |
| 30 | + .domain([0, max(props.data)]) |
| 31 | + .range([props.left_bar_padding, bar_width - props.right_bar_padding]), |
| 32 | + container_style = { |
| 33 | + display: "grid", |
| 34 | + gridTemplateColumns: css_grid_format([ |
| 35 | + label_width, |
| 36 | + base_alignment_width, |
| 37 | + bar_width |
| 38 | + ]), |
| 39 | + gridTemplateRows: css_grid_format([axis_height, base_alignment_height]) |
| 40 | + }, |
| 41 | + scroll_broadcaster = new ScrollBroadcaster({ |
| 42 | + width: full_pixel_width, |
| 43 | + height: full_pixel_height, |
| 44 | + x_pad: base_alignment_width, |
| 45 | + y_pad: base_alignment_height, |
| 46 | + bidirectional: [ |
| 47 | + "alignmentjs-alignment", |
| 48 | + "alignmentjs-axis-div", |
| 49 | + "alignmentjs-bar" |
| 50 | + ] |
| 51 | + }); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div id="alignmentjs-main-div" style={container_style}> |
| 55 | + <Placeholder width={label_width} height={axis_height} /> |
| 56 | + <SiteAxis |
| 57 | + width={alignment_width} |
| 58 | + height={axis_height} |
| 59 | + sequence_data={sequence_data} |
| 60 | + scroll_broadcaster={scroll_broadcaster} |
| 61 | + /> |
| 62 | + <AxisTop |
| 63 | + width={bar_width} |
| 64 | + height={axis_height} |
| 65 | + scale={scale} |
| 66 | + label={props.label} |
| 67 | + /> |
| 68 | + <SequenceAxis |
| 69 | + width={label_width} |
| 70 | + height={alignment_height} |
| 71 | + sequence_data={sequence_data} |
| 72 | + site_size={site_size} |
| 73 | + scroll_broadcaster={scroll_broadcaster} |
| 74 | + /> |
| 75 | + <BaseAlignment |
| 76 | + sequence_data={sequence_data} |
| 77 | + width={alignment_width} |
| 78 | + height={alignment_height} |
| 79 | + site_color={props.site_color} |
| 80 | + text_color={props.text_color} |
| 81 | + site_size={props.site_size} |
| 82 | + molecule={props.molecule} |
| 83 | + scroll_broadcaster={scroll_broadcaster} |
| 84 | + /> |
| 85 | + <BaseSequenceBarPlot |
| 86 | + data={props.data} |
| 87 | + width={bar_width} |
| 88 | + height={alignment_height} |
| 89 | + scroll_broadcaster={scroll_broadcaster} |
| 90 | + scale={scale} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +SequenceBarChart.defaultProps = { |
| 97 | + site_color: nucleotide_color, |
| 98 | + text_color: nucleotide_text_color, |
| 99 | + label_padding: 10, |
| 100 | + left_bar_padding: 10, |
| 101 | + right_bar_padding: 20, |
| 102 | + site_size: 20, |
| 103 | + axis_height: 50, |
| 104 | + bar_width: 300, |
| 105 | + width: 960, |
| 106 | + height: 500, |
| 107 | + sender: "main", |
| 108 | + molecule: mol => mol |
| 109 | +}; |
| 110 | + |
| 111 | +export default SequenceBarChart; |
0 commit comments