All files / components/xBars OverviewBar.js

90% Statements 27/30
33.33% Branches 1/3
85.71% Functions 12/14
89.66% Lines 26/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                        314x 314x             314x           10x                         8x 8x 8x 8x 8x 12x             10x 10x             8x 12x 12x   1059x   9x   9x 9x             9x                       12x 12x                     5x             5x                                                                   5x 6x                                
/**
* Copyright 2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, { Component, PureComponent } from 'react';
import PropTypes from 'prop-types';
import shallowCompare from 'react-addons-shallow-compare';
 
import {
  partialRight,
  pick,
} from 'lodash-es';
 
import { createSelector } from 'reselect';
 
import XBar from './xBar';
import msaConnect from '../../store/connect'
import shallowSelect from '../../utils/shallowSelect';
import autobind from '../../utils/autobind';
import MSAStats from '../../utils/statSeqs';
 
function createBar({columnHeights, tileWidth, height, fillColor,
  barStyle, barAttributes}) {
  class Bar extends PureComponent {
    render() {
      const { index, ...otherProps} = this.props;
      otherProps.style = {
        height: Math.round(columnHeights[index] * height),
        width: tileWidth,
        display: "inline-block",
        textAlign: "center",
        backgroundColor: fillColor,
      }
      return (
        <div {...otherProps}>
        </div>
      );
    }
  }
  return Bar;
}
 
/**
 * Creates a small overview box of the sequences for a general overview.
 */
class HTMLOverviewBarComponent extends PureComponent {
 
  static barAttributes = [
    "tileWidth", "height", "fillColor", "barStyle", "barAttributes"
  ];
 
  constructor(props) {
    super(props);
    this.cache = function(){};
    this.initializeColumnHeights();
    autobind(this, 'createBar');
    this.bar = shallowSelect(
      s => pick(s, this.constructor.barAttributes),
      this.columnHeights,
      this.createBar,
    );
  }
 
  createBar(props, columnHeights) {
    this.cache = function(){};
    return createBar({...props, columnHeights});
  }
 
  /**
   * Reduces the `props` object to column height by a `props.method`
   */
  initializeColumnHeights() {
    this.columnHeights = createSelector(
      p => p.sequences,
      p => p.method,
      (sequences, method) => {
      const stats = MSAStats(sequences.map(e => e.sequence));
      let result;
      switch (method) {
        case "conservation":
          result = stats.scale(stats.conservation());
          break;
        case "information-content":
          result = stats.scale(stats.ic());
          break;
        default:
          console.error(method + "is an invalid aggregation method for <OverviewBar />");
      }
      return result;
    }).bind(this);
  }
 
  render() {
    const {cacheElements,
      height,
      method,
      fillColor,
      dispatch,
      barStyle,
      barAttributes,
      ...otherProps} = this.props;
    return (
      <XBar
        tileComponent={this.bar(this.props)}
        cacheElements={cacheElements}
        componentCache={this.cache}
        {...otherProps}
      />
    );
  }
}
 
HTMLOverviewBarComponent.defaultProps = {
  height: 50,
  fillColor: "#999999",
  method: "conservation",
  cacheElements: 10,
}
 
HTMLOverviewBarComponent.propTypes = {
  /**
   * Method to use for the OverviewBar:
   *  - `information-content`: Information entropy after Shannon of a column (scaled)
   *  - `conservation`: Conservation of a column (scaled)
   */
  method: PropTypes.oneOf(['information-content', 'conservation']),
 
  /**
   * Height of the OverviewBar (in pixels), e.g. `100`
   */
  height: PropTypes.number,
 
  /**
   * Fill color of the OverviewBar, e.g. `#999999`
   */
  fillColor: PropTypes.string,
 
  /**
   * Inline styles to apply to the OverviewBar component
   */
  style: PropTypes.object,
 
  /**
   * Inline styles to apply to each bar.
   */
  barStyle: PropTypes.object,
 
  /**
   * Attributes to apply to each bar.
   */
  barAttributes: PropTypes.object,
};
 
const mapStateToProps = state => {
  return {
    sequences: state.sequences.raw,
    maxLength: state.sequences.maxLength,
    width: state.props.width,
    tileWidth: state.props.tileWidth,
    nrXTiles: state.sequenceStats.nrXTiles,
  }
}
 
export default msaConnect(
  mapStateToProps,
)(HTMLOverviewBarComponent);
// for testing
export {
  HTMLOverviewBarComponent as OverviewBar,
}