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/
224 lines (190 loc) • 10.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _underscore = _interopRequireDefault(require("underscore"));
var _colors = require("./../helpers/colors");
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); }
var BaseAlignment = /*#__PURE__*/function (_Component) {
_inherits(BaseAlignment, _Component);
var _super = _createSuper(BaseAlignment);
function BaseAlignment(props) {
var _this;
_classCallCheck(this, BaseAlignment);
_this = _super.call(this, props);
_this.canvas_id = props.id + "-alignment";
return _this;
}
_createClass(BaseAlignment, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
document.getElementById(this.canvas_id).addEventListener("alignmentjs_wheel_event", function (e) {
_this2.draw(e.detail.x_pixel, e.detail.y_pixel);
});
if (this.props.sequence_data) {
this.draw(this.props.x_pixel || 0, this.props.y_pixel || 0);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props = this.props,
x_pixel = _this$props.x_pixel,
y_pixel = _this$props.y_pixel,
pixel_specified = x_pixel != undefined || y_pixel != undefined,
data_changed = !_underscore["default"].isEqual(prevProps.sequence_data, this.props.sequence_data),
site_color_changed = this.props.site_color != prevProps.site_color,
text_color_changed = this.props.text_color != prevProps.text_color,
molecule_changed = this.props.molecule != prevProps.molecule,
should_draw = _underscore["default"].some([pixel_specified, data_changed, site_color_changed, text_color_changed, molecule_changed]);
if (should_draw) {
this.draw(x_pixel || 0, y_pixel || 0);
}
}
}, {
key: "draw",
value: function draw(x_pixel, y_pixel) {
if (this.props.disableVerticalScrolling) y_pixel = 0;
var _this$props2 = this.props,
width = _this$props2.width,
height = _this$props2.height,
site_size = _this$props2.site_size,
molecule = _this$props2.molecule,
start_site = Math.floor(x_pixel / site_size),
end_site = Math.ceil((x_pixel + width) / site_size),
start_seq = Math.floor(y_pixel / site_size),
end_seq = Math.ceil((y_pixel + height) / site_size),
site_color = this.props.amino_acid ? _colors.amino_acid_color : this.props.site_color,
text_color = this.props.amino_acid ? _colors.amino_acid_text_color : this.props.text_color;
var individual_sites = _underscore["default"].flatten(this.props.sequence_data.filter(function (row, i) {
var after_start = i >= start_seq;
var before_finish = i <= end_seq;
return after_start && before_finish;
}).map(function (row, i) {
return row.seq.slice(start_site, end_site).split("").map(function (mol, j) {
return {
mol: mol,
j: start_site + j + 1,
i: start_seq + i + 1,
header: row.header
};
});
}));
var context = document.getElementById(this.canvas_id).getContext("2d");
context.font = "14px Courier";
context.textAlign = "center";
context.textBaseline = "middle";
context.setTransform(1, 0, 0, 1, -x_pixel, -y_pixel);
individual_sites.forEach(function (d) {
var x = site_size * (d.j - 1),
y = site_size * (d.i - 1),
mol = molecule(d.mol, d.j, d.header);
context.beginPath();
context.fillStyle = site_color(d.mol, d.j, d.header);
context.rect(x, y, site_size, site_size);
context.fill();
context.fillStyle = text_color(d.mol, d.j, d.header);
context.fillText(mol, x + site_size / 2, y + site_size / 2);
context.closePath();
});
}
}, {
key: "handleClick",
value: function handleClick(e) {
var _this$props3 = this.props,
scroll_broadcaster = _this$props3.scroll_broadcaster,
sender = _this$props3.sender,
site_size = _this$props3.site_size,
_scroll_broadcaster$l = scroll_broadcaster.location(sender),
x_pixel = _scroll_broadcaster$l.x_pixel,
y_pixel = _scroll_broadcaster$l.y_pixel,
x_click = e.clientX - e.target.offsetLeft + x_pixel,
y_click = e.clientY - e.target.offsetTop + y_pixel,
x_site = Math.floor(x_click / site_size),
y_sequence_index = Math.floor(y_click / site_size),
y_sequence = this.props.sequence_data[y_sequence_index];
this.props.onSiteClick(x_site, y_sequence);
}
}, {
key: "handleHover",
value: function handleHover(e) {
var _this$props4 = this.props,
scroll_broadcaster = _this$props4.scroll_broadcaster,
sender = _this$props4.sender,
site_size = _this$props4.site_size,
_scroll_broadcaster$l2 = scroll_broadcaster.location(sender),
x_pixel = _scroll_broadcaster$l2.x_pixel,
y_pixel = _scroll_broadcaster$l2.y_pixel,
x_click = e.clientX - e.target.offsetLeft + x_pixel,
y_click = e.clientY - e.target.offsetTop + y_pixel,
x_site = Math.floor(x_click / site_size),
y_sequence_index = Math.floor(y_click / site_size),
y_sequence = this.props.sequence_data[y_sequence_index];
this.props.onSiteHover(x_site, y_sequence);
}
}, {
key: "handleWheel",
value: function handleWheel(e) {
e.preventDefault();
this.props.scroll_broadcaster.handleWheel(e, this.props.sender);
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var div_id = this.props.id + "-alignment-div";
return /*#__PURE__*/_react["default"].createElement("div", {
id: div_id,
className: "alignmentjs-container",
onClick: function onClick(e) {
return _this3.handleClick(e);
},
onWheel: function onWheel(e) {
return _this3.handleWheel(e);
},
onMouseMove: function onMouseMove(e) {
return _this3.handleHover(e);
}
}, /*#__PURE__*/_react["default"].createElement("canvas", {
width: this.props.width,
height: this.props.height,
id: this.canvas_id
}));
}
}]);
return BaseAlignment;
}(_react.Component);
BaseAlignment.defaultProps = {
site_color: _colors.nucleotide_color,
text_color: _colors.nucleotide_text_color,
molecule: function molecule(mol) {
return mol;
},
site_size: 20,
id: "alignmentjs",
sender: "main",
onSiteClick: function onSiteClick() {
return null;
},
onSiteHover: function onSiteHover() {
return null;
}
};
var _default = BaseAlignment;
exports["default"] = _default;
;