alignment.js
Version:
A suite of reusable [React](http://reactjs.org/) components for creating a variety of visualizations involving [multiple sequence alignments](https://en.wikipedia.org/wiki/Multiple_sequence_alignment). [View the live demo here](http://alignment.hyphy.org/
301 lines (247 loc) • 14.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _fasta = _interopRequireDefault(require("../helpers/fasta"));
var _computeLabelWidth = _interopRequireDefault(require("../helpers/computeLabelWidth"));
var _BaseAlignment = _interopRequireDefault(require("./BaseAlignment.js"));
var _SiteAxis = _interopRequireDefault(require("./SiteAxis.js"));
var _Placeholder = _interopRequireDefault(require("./Placeholder.js"));
var _SequenceAxis = _interopRequireDefault(require("./SequenceAxis.js"));
var _ScrollBroadcaster = _interopRequireDefault(require("./../helpers/ScrollBroadcaster"));
var _colors = require("./../helpers/colors");
var _nucleotideComposition = require("./../helpers/nucleotideComposition");
var _BaseSiteBarPlot = _interopRequireDefault(require("./BaseSiteBarPlot.js"));
var _SitePlotAxis = _interopRequireDefault(require("./SitePlotAxis.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var $ = require("jquery");
var SiteBarPlotExample = /*#__PURE__*/function (_Component) {
_inherits(SiteBarPlotExample, _Component);
var _super = _createSuper(SiteBarPlotExample);
function SiteBarPlotExample(props) {
var _this;
_classCallCheck(this, SiteBarPlotExample);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "getSiteNucleotideData", function (sequence_data) {
(0, _nucleotideComposition.siteComposition)(sequence_data, _this.setSiteNucleotideDataToState);
});
_defineProperty(_assertThisInitialized(_this), "setSiteNucleotideDataToState", function (siteNucleotideData) {
_this.setState({
siteNucleotideData: siteNucleotideData
});
});
_defineProperty(_assertThisInitialized(_this), "switchNucleotide", function (nucleotide) {
_this.setState({
nucleotideInView: nucleotide
});
});
_this.label_width = 200;
_this.state = {
nucleotideInView: "A",
siteNucleotideData: null
};
_this.initialize(_this.props);
return _this;
}
_createClass(SiteBarPlotExample, [{
key: "componentDidMount",
value: function componentDidMount() {
this.getSiteNucleotideData(this.sequence_data);
}
}, {
key: "componentWillUpdate",
value: function componentWillUpdate(nextProps, prevState) {
//TODO: replace componentWillUpdate as this is deprecated in future versions of react.
this.setScrollingEvents(nextProps);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (this.state.siteNucleotideData == null) {
this.getSiteNucleotideData(this.sequence_data);
}
}
}, {
key: "setScrollingEvents",
value: function setScrollingEvents(props) {
var _this2 = this;
if (props.fasta) {
var width = props.width,
height = props.height,
axis_height = props.axis_height;
this.scroll_broadcaster = new _ScrollBroadcaster["default"]({
width: this.full_pixel_width,
height: this.full_pixel_height,
x_pad: width - this.label_width,
y_pad: height - axis_height,
x_pixel: this.x_pixel,
y_pixel: this.y_pixel,
bidirectional: ["alignmentjs-siteBarPlot-div", "alignmentjs-alignment", "alignmentjs-axis-div", "alignmentjs-labels-div"]
}); // Update this.x_pixel on scroll events so that the alignment and plots will render to the correct scroll location on re-renders.
document.getElementById("alignmentjs-alignment").addEventListener("alignmentjs_wheel_event", function (e) {
_this2.x_pixel = e.detail.x_pixel;
});
}
}
}, {
key: "initialize",
value: function initialize(props) {
if (props.fasta) {
var fasta = props.fasta,
site_size = props.site_size,
width = props.width,
height = props.height;
this.sequence_data = (0, _fasta["default"])(props.fasta);
var sequence_data = this.sequence_data;
var label_padding = this.props.label_padding;
this.label_width = (0, _computeLabelWidth["default"])(sequence_data, label_padding);
this.full_pixel_width = site_size * sequence_data.number_of_sites;
this.full_pixel_height = site_size * sequence_data.number_of_sequences;
var centerOnSite = props.centerOnSite,
centerOnHeader = props.centerOnHeader;
this.x_pixel = site_size * centerOnSite - width / 2 || 0;
this.y_pixel = centerOnHeader ? site_size * this.sequence_data.map(function (record) {
return record.header;
}).indexOf(centerOnHeader) - height / 2 : 0;
}
this.setScrollingEvents(props);
}
}, {
key: "render",
value: function render() {
var full_pixel_width = this.full_pixel_width,
full_pixel_height = this.full_pixel_height,
label_width = this.label_width,
width = full_pixel_width ? Math.min(label_width + full_pixel_width, this.props.width) : this.props.width,
height = full_pixel_height ? Math.min(full_pixel_height + this.props.axis_height, this.props.height) : this.props.height;
var barPlotHeight = 120;
var bar_plot_data = this.state.siteNucleotideData == null ? null : this.state.siteNucleotideData[this.state.nucleotideInView];
return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(SwitchNucleotideButtons, {
switchNucleotide: this.switchNucleotide
}), /*#__PURE__*/_react["default"].createElement("div", {
id: "alignmentjs-main-div",
style: {
width: width,
height: height
}
}, /*#__PURE__*/_react["default"].createElement(_SitePlotAxis["default"], {
label_width: this.label_width,
data: bar_plot_data,
height: barPlotHeight,
max_value: 1,
axis_label: "Nucleotide %",
scroll_broadcaster: this.scroll_broadcaster
}), /*#__PURE__*/_react["default"].createElement(_BaseSiteBarPlot["default"], {
data: bar_plot_data,
siteSize: this.props.site_size,
width: full_pixel_width,
height: barPlotHeight,
fillColor: this.props.site_color(this.state.nucleotideInView),
outlineColor: this.props.text_color(this.state.nucleotideInView),
x_pixel: this.x_pixel,
max_value: 1,
scroll_broadcaster: this.scroll_broadcaster
}), /*#__PURE__*/_react["default"].createElement(_Placeholder["default"], {
width: this.label_width,
height: this.props.axis_height
}), /*#__PURE__*/_react["default"].createElement(_SiteAxis["default"], {
height: this.props.axis_height,
site_size: this.props.site_size,
sequence_data: this.sequence_data,
x_pixel: this.x_pixel,
scroll_broadcaster: this.scroll_broadcaster
}), /*#__PURE__*/_react["default"].createElement(_SequenceAxis["default"], {
width: this.label_width,
height: height - this.props.axis_height,
sequence_data: this.sequence_data,
site_size: this.props.site_size,
y_pixel: this.y_pixel,
scroll_broadcaster: this.scroll_broadcaster
}), /*#__PURE__*/_react["default"].createElement(_BaseAlignment["default"], {
width: width - this.label_width,
height: height - this.props.axis_height,
sequence_data: this.sequence_data,
site_color: this.props.site_color,
text_color: this.props.text_color,
site_size: this.props.site_size,
x_pixel: this.x_pixel,
y_pixel: this.y_pixel,
scroll_broadcaster: this.scroll_broadcaster
})));
}
}]);
return SiteBarPlotExample;
}(_react.Component);
SiteBarPlotExample.defaultProps = {
site_color: _colors.nucleotide_color,
text_color: _colors.nucleotide_text_color,
label_padding: 10,
site_size: 20,
axis_height: 20,
sender: "main"
};
var SwitchNucleotideButtons = /*#__PURE__*/function (_React$Component) {
_inherits(SwitchNucleotideButtons, _React$Component);
var _super2 = _createSuper(SwitchNucleotideButtons);
function SwitchNucleotideButtons(props) {
_classCallCheck(this, SwitchNucleotideButtons);
return _super2.call(this, props);
}
_createClass(SwitchNucleotideButtons, [{
key: "render",
value: function render() {
var _this3 = this;
return /*#__PURE__*/_react["default"].createElement("div", {
className: "btn-group btn-group-toggle",
"data-toggle": "buttons"
}, /*#__PURE__*/_react["default"].createElement("label", {
className: "btn btn-light active",
onClick: function onClick() {
return _this3.props.switchNucleotide("A");
}
}, /*#__PURE__*/_react["default"].createElement("input", {
type: "radio"
}), "A"), /*#__PURE__*/_react["default"].createElement("label", {
className: "btn btn-light",
onClick: function onClick() {
return _this3.props.switchNucleotide("C");
}
}, /*#__PURE__*/_react["default"].createElement("input", {
type: "radio"
}), "C"), /*#__PURE__*/_react["default"].createElement("label", {
className: "btn btn-light",
onClick: function onClick() {
return _this3.props.switchNucleotide("G");
}
}, /*#__PURE__*/_react["default"].createElement("input", {
type: "radio"
}), "G"), /*#__PURE__*/_react["default"].createElement("label", {
className: "btn btn-light",
onClick: function onClick() {
return _this3.props.switchNucleotide("T");
}
}, /*#__PURE__*/_react["default"].createElement("input", {
type: "radio"
}), "T"));
}
}]);
return SwitchNucleotideButtons;
}(_react["default"].Component);
var _default = SiteBarPlotExample;
exports["default"] = _default;