biocore / empress

A fast and scalable phylogenetic tree viewer for microbiome data analysis
BSD 3-Clause "New" or "Revised" License
48 stars 31 forks source link

Add convenience method to empress.js that gets the 0-based index of a feature metadata column #546

Open fedarko opened 3 years ago

fedarko commented 3 years ago

We repeat this paradigm a lot in empress.js (five times just using this._featureMetadataColumns, by my count, and I'm pretty sure most of these are my fault :P):

var fmIdx = _.indexOf(this._featureMetadataColumns, cat);
if (fmIdx < 0) {
    throw 'Feature metadata column "' + cat + '" not present in data.';
}

It'd be nice to just make a convenience function for this:

// retrieves index in this._featureMetadataColumns and automatically does validation
var fmIdx = this.getFMIdx(cat);

For what it's worth, the BIOM table JS also does something similar with sample metadata. We could essentially just copy over this function and change it to reference feature instead of sample metadata. (I guess it'd be extra nice to make both these functions call another utility function that actually uses _.indexOf(), but at a certain point abstracting things like this stops being useful...)

https://github.com/biocore/empress/blob/582c0544d5d75db2adb4328376a5b7748d1ef174/empress/support_files/js/biom-table.js#L159-L167