qiime2 / q2-diversity

BSD 3-Clause "New" or "Revised" License
4 stars 45 forks source link

Flipping coordinates for biplot ordination #258

Closed mortonjt closed 5 years ago

mortonjt commented 5 years ago

Addition Description One thing that could become increasing helpful with the introduction of feature-specific stats (i.e. songbird, aldex2, rhapsody, qurro) is the ability to flip ordinations.

For example, if someone wants to analyze their metabolites in a qurro rank plot, they would need to first flip their ordination coordinates (i.e. swap sample coordinates with feature coordinates) so that they can have metabolites as "features".

Another use case is if someone wants to visualize the microbe co-occurrences learned from DEICODE - it'll require the features to be plotted instead of the samples.

Given that this will likely be a recurring pattern, it may make sense to have this "flipping" operation further upstream.

Current Behavior Doesn't exist at the moment.

Proposed Behavior I've written a simple CLI script that can do this at the moment - but if there is interest, I'll be happy to make a contribution to q2-diversity

from skbio import OrdinationResults
import sys

res = OrdinationResults.read(sys.argv[1])
samples = res.samples.copy()
features = res.features.copy()
res.samples = features
res.features = samples
res.write(sys.argv[2])

References https://github.com/biocore/qurro https://github.com/biocore/DEICODE https://msystems.asm.org/content/4/1/e00016-19

CC @fedarko @cameronmartino @ElDeveloper

nbokulich commented 5 years ago

Wouldn't it make more sense for the visualizers to do the flipping as appropriate? Otherwise I worry that this could enable "illicit" actions, e.g., if passing those ordination results to a method that should not operate on flipped samples/loadings.

I see that this makes more sense for feature-specific stats, but keep in mind that rhapsody is currently making use of the PCoAResults % Properties('biplot') type. Should we be allowing samples/loadings to be flipped for all biplot results? Maybe we need to define a new OrdinationResults % Properties('FeaturesBiplot') type that rhapsody and other plugins use when they make microbe/metabolite biplots?

mortonjt commented 5 years ago

Good point @nbokulich . One option along those lines is to have an --p-invert option for each of the visualizers that flips the assignment of points vs arrows or vice versa. There is a similar issue raised here.

I'm not sure how I feel about defining a OrdinationResults % Properties('FeaturesBiplot') - how would a FeaturesBiplot be different just a Biplot? There are definitely use cases for allowing for all biplots to be flipped -- for instance, if the coordinates in DEICODE were flipped (where points now represent microbes), the distances between points would directly map to microbe-microbe co-occurrence strengths.

mortonjt commented 5 years ago

@thermokarst @nbokulich moving this to emperor and eventually qurro, since it is probably a better place to put this