Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .container--outer
  .description
    h1 DNA Mutation Demo
    .instructions Drag and drop DNA to see how changes in the DNA sequence affect the resulting mRNA and amino acids.
  #app
            
          
!
            
              $dna-base: #bdc3c7;
$rna-base: #7f8c8d;
$a: #1abc9c;
$t: #2980b9;
$u: #f1c40f;
$g: #9b59b6;
$c: #e74c3c;
$time: 250ms;

.container--outer {
  align-items: center;
  display: flex;
  justify-content: space-around;
  padding: 1.5em;
}

.container--inner:not(:last-child) {
  margin-right: 6em;
}

.instructions {
  max-width: 500px;
}

.diagram {
  display: flex;
  justify-content: center;
  
  h2 {
    margin-bottom: 2em;
  }
  
  ul {
    list-style-type: none;
    padding: 0;
  }
  
  li {
    margin: 1em;
    padding: 0;
  }
}

.nucleotide--draggable {
  cursor: grab;
  
  &:active {
    cursor: grabbing;
  }
}

.dna, .rna {
  position: relative;
  
  &:before {
    background: $dna-base;
    border-top: 2px solid darken($dna-base, 15%);
    border-left: 2px solid darken($dna-base, 15%);
    content: ' ';
    height: 100%;
    left: .5em;
    position: absolute;
    transform: rotate(-25deg) skew(-25deg);
    transition: all $time;
    z-index: 0;
    width: 1em;
  }  
  
  li {
    line-height: 2em;
    margin: 0 1em;
    padding: .5em 0;
    position: relative;
    text-align: center;
    z-index: 200;
    
    div {
      height: 2em;
      width: 2em;
      z-index: 100;
    }
  }
  
}

.a { background: $a; }
.t { background: $t; }
.u { background: $u; }
.c { background: $c; }
.g { background: $g; }

.rna {
  &:before {
    background: $rna-base;
    border-top: 2px solid darken($rna-base, 15%);
    border-left: 2px solid darken($rna-base, 15%);
  }
}

.amino-acid {
  background: radial-gradient(circle at 2em 2em, pink, darken(pink, 50%));
  border-radius: 8em;
  height: 8em;
  line-height: 8em;
  margin-bottom: .5em;
  position: relative;
  text-align: center;
  width: 8em;
  
  &:before {
    border: 1px solid black;
    content: '';
    left: -2em;
    height: 0px;
    position: absolute;
    top: 50%;
    width: 1em;
  }
  
  &:after {
    border-bottom: 2px solid black;
    border-right: 2px solid black;
    border-top: 2px solid black;
    content: '';
    height: 8em;
    left: -3em;
    position: absolute;
    width: 1em;
  }
}

.amino-acid-list {
  margin-top: 3.5rem; 
}

.active-strand {
  &:before {
    background: darken($dna-base, 25%);
  }
}

.placeholder {
  height: 2em;
  position: relative;
  
  &:before {
    background: rgb(255,240,120);
    content: '';
    height: 2em;
    left: 0;
    position: absolute;
    top: .5em;
    width: 2em;
  }
}
            
          
!
            
              class DNAUtils {
  static _transcriptionLookup = {
    'a': 'u',
    't': 'a',
    'c': 'g',
    'g': 'c',
  };

  static _nucleotideToNumberLookup = {
    'a': 60,
    'c': 120,
    'u': 180,
    'g': 240,
  };

  static _codonToAminoAcidLookup = {
    'uuu': 'Phenylalanine',
    'uuc': 'Phenylalanine',
    'uua': 'Leucine',
    'uug': 'Leucine',
    'cuu': 'Leucine',
    'cuc': 'Leucine',
    'cua': 'Leucine',
    'cug': 'Leucine',
    'auu': 'Isolecine',
    'auc': 'Isolecine',
    'aua': 'Isolecine',
    'aug': 'Methionine',
    'guu': 'Valine',
    'guc': 'Valine',
    'gua': 'Valine',
    'gug': 'Valine',
    'ucu': 'Serine',
    'ucc': 'Serine',
    'uca': 'Serine',
    'ucg': 'Serine',
    'ccu': 'Proline',
    'ccc': 'Proline',
    'cca': 'Proline',
    'ccg': 'Proline',
    'acu': 'Threonine',
    'acc': 'Threonine',
    'aca': 'Threonine',
    'acg': 'Threonine',
    'gcu': 'Alanine',
    'gcc': 'Alanine',
    'gca': 'Alanine',
    'gcg': 'Alanine',
    'uau': 'Tyrosine',
    'uac': 'Tyrosine',
    'uaa': 'Stop',
    'uag': 'Stop',
    'cau': 'Histidine',
    'cac': 'Histidine',
    'caa': 'Glutamine',
    'cag': 'Glutamine',
    'aau': 'Asparagine',
    'aac': 'Asparagine',
    'aaa': 'Lysine',
    'aag': 'Lysine',
    'gau': 'Aspartic acid',
    'gac': 'Aspartic acid',
    'gaa': 'Glutamic acid',
    'gag': 'Glutamic acid',
    'ugu': 'Cysteine',
    'ugc': 'Cysteine',
    'uga': 'Stop',
    'ugg': 'Tryptophan',
    'cgu': 'Arginine',
    'cgc': 'Arginine',
    'cga': 'Arginine',
    'cgg': 'Arginine',
    'agu': 'Serine',
    'agc': 'Serine',
    'aga': 'Arginine',
    'agg': 'Arginine',
    'ggu': 'Glycine',
    'ggc': 'Glycine',
    'gga': 'Glycine',
    'ggg': 'Glycine',
  }

  static codonToAminoAcid(codon) {
    return this._codonToAminoAcidLookup[codon.join('')];
  }

  static codonToRGB(codon) {
    return codon.map(nucleotide => this._nucleotideToNumberLookup[nucleotide]);
  }

  static chunkDnaIntoCodons(dna) {
    // would use lodash chunk in real life
    return dna.reduce((a, b, c) => {
        if (c % 3 == 0  && c !== 0) {
          a.push([]);
        };
        a[a.length - 1].push(b);
        return a;
      }, [[]])
      .filter(function(codon) { 
        return codon.length === 3;
      });
  }
  
  static transcribeSingleNucleotide(nucleotide) {
    return this._transcriptionLookup[nucleotide];
  }
  
  static transcribeAllNucleotides(nucleotides) {
    return nucleotides.map(nucleotide => this.transcribeSingleNucleotide(nucleotide));
  }
}

class StyleUtil {
  static darkenRgb(rgb, value) {
    if (!Array.isArray(rgb) || !Number.isInteger(value)) return;
    
    return rgb.map(color => color -= value);
  }
  
  static rgbArrayToCssString(rgb) {
    return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
  }
  
  static circleGradient(fromColor, toColor, size) {
    return `radial-gradient(circle at ${size} ${size}, ${fromColor}, ${toColor})`
  }
}

class MutationSimulator extends React.Component {
	constructor(props) {
		super(props);
    const { dna } = props;
    
		this.state = {
      dna,
      rna: DNAUtils.transcribeAllNucleotides(dna),
    };
	}

	render() {
    const rna = DNAUtils.transcribeAllNucleotides(this.state.dna);
    const aminoAcids = DNAUtils.chunkDnaIntoCodons(rna)
      .map((codon, i) => {
        return (
          <li key={`aminoAcid${i}`}>
            <AminoAcid codon={codon}/>
          </li>
        );
      });

    return (
      <div className="diagram">
        <div className="container--inner dna-container">
          <h2>DNA</h2>
          <Strand
            callback={this.updateNucleotides.bind(this)}
            canDrag={true}
            className='dna'
            nucleotides={this.state.dna} />
        </div>
        <div className="container--inner rna-container">
          <h2>mRNA</h2>
          <Strand
            canDrag={false}
            className="rna"
            nucleotides={this.state.rna} />
        </div>
        <div className="container--inner amino-acid-container">
          <h2>Amino Acids</h2>
          <ul className="amino-acid-list">
            {aminoAcids}
          </ul>
        </div>
      </div>
  	);
 	}

  updateNucleotides(nucleotides) {
    this.setState({
      dna: nucleotides,
      rna: DNAUtils.transcribeAllNucleotides(nucleotides)
    });
  }
}

class Strand extends React.Component {
  constructor(props) {
    super(props);
    
    this.dragged = null;
    this.placeholder = document.createElement('li');
    this.placeholder.classList.add('placeholder');
    this.state = { activeDragging: false };
  }

  _dragEnd(evt) {
    this.setState({ activeDragging: false });
    this.dragged.style.display = 'block';
    
    try {
      this.dragged.parentNode.removeChild(this.placeholder);
    } catch(e) {
      // do nothing if dropped outside target area
    }
    
    const data = this.props.nucleotides;
    const from = Number(this.dragged.dataset.id);
    let to = Number(this.over.dataset.id);
    
    if (from < to) to--;
    data.splice(to, 0, data.splice(from, 1)[0]);
    this.props.callback(data);
  }

  _dragOver(evt) {
    if (evt.target.classList.contains('dna-nucleotide')) {
      evt.preventDefault();
      try {
        this.dragged.style.display = 'none';
      } catch(e) {
        // do nothing if dragged outside target area
      }
      if (evt.target.className === 'placeholder') {
        return;
      }
      this.over = evt.target;
      evt.target.parentNode.insertBefore(this.placeholder, evt.target);
    }
  }

  _dragStart(evt) {
    this.setState({ activeDragging: true });
    this.dragged = evt.currentTarget;
    evt.dataTransfer.effectAllowed = 'move';
    evt.dataTransfer.setData('text/html', this.dragged);
  }

  render() {
    let activeClass = '';
    let dragAttributes = {};
    let nucleotides;

    if (this.props.canDrag) {
      dragAttributes = {
        draggable: true,
        onDragEnd: this._dragEnd.bind(this),
        onDragStart: this._dragStart.bind(this)
      };
    }

    if (this.state.activeDragging) {
      activeClass = 'active-strand';
    }

    nucleotides = this.props.nucleotides.map((nucleotide, i) => {
      return (
        <Nucleotide
          baseClassName={this.props.className}
          canDrag={this.props.canDrag}
          dragAttributes={dragAttributes}
          index={i}
          key={`${this.props.className}-nucleotide-${i}`}
          nucleotide={nucleotide}
        />
      );
    });

    return (
      <ul
        className={`${this.props.className} ${activeClass}`}
        onDragOver={this._dragOver.bind(this)}>
        {nucleotides}
      </ul>
    );
  }
}

Strand.propTypes = {
  callback: React.PropTypes.func,
  canDrag: React.PropTypes.bool,
  className: React.PropTypes.string.isRequired,
  nucleotides: React.PropTypes.array.isRequired
};
Strand.defaultProps = {
  canDrag: false
}

class Nucleotide extends React.Component {
  render() {
    const { baseClassName, canDrag, dragAttributes, index, nucleotide } = this.props;
    return (
      <li
        className={`${baseClassName}-nucleotide`}
        data-id={index}
        key={`${baseClassName}${nucleotide}${index}`}
        {...dragAttributes}>
        <div className={`nucleotide ${canDrag ? 'nucleotide--draggable' : ''} ${nucleotide}`}>                     {nucleotide}
        </div>
      </li> 
    );
  }
}

Nucleotide.propTypes = {
  canDrag: React.PropTypes.bool,
  baseClassName: React.PropTypes.string,
  dragAttributes: React.PropTypes.object,
  index: React.PropTypes.number,
  nucleotide: React.PropTypes.string,
}

class AminoAcid extends React.Component {
  getBackgroundColor(codon) {
    const codonRgb = DNAUtils.codonToRGB(codon);
    const darkenedRgb = StyleUtil.darkenRgb(codonRgb, 60);
    const colorString = StyleUtil.rgbArrayToCssString(codonRgb);
    const darkenedColorString = StyleUtil.rgbArrayToCssString(darkenedRgb);

    return StyleUtil.circleGradient(colorString, darkenedColorString, '2em');
  }

  render() {
    const styles = { background: this.getBackgroundColor(this.props.codon) };
    const aminoAcidName = DNAUtils.codonToAminoAcid(this.props.codon) || 'Other';

    return (
      <div className="amino-acid" style={styles}>
        {aminoAcidName}
      </div>
    );
  }
}

AminoAcid.propTypes = {
  codon: React.PropTypes.array.isRequired
}

ReactDOM.render(
	<MutationSimulator dna={['a', 'c', 'g', 't', 'a',
            'c', 't', 'g', 'g', 'c', 'c']}/>,
  document.getElementById('app')
);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console