def test_run_taxa_tree_sample(self): # pylint: disable=invalid-name """Ensure TaxaTree run_sample produces correct results.""" kwargs = { KrakenResultModule.name(): create_kraken(), KrakenHLLResultModule.name(): create_krakenhll(), Metaphlan2ResultModule.name(): create_metaphlan2(), } self.generic_run_sample_test(kwargs, TaxaTreeDisplayModule)
def tool_name_to_param(tool_name): """Convert tool short names to param names.""" for param_name in [ KrakenResultModule.name(), Metaphlan2ResultModule.name() ]: if tool_name in param_name: return param_name return tool_name
def create_sample(i): """Create unique sample for index i.""" args = { 'name': f'Sample{i}', 'metadata': {'foobar': f'baz{i}'}, CARDAMRResultModule.name(): create_card_amr(), KrakenResultModule.name(): create_kraken(), Metaphlan2ResultModule.name(): create_metaphlan2(), } return Sample(**args).save()
def create_sample(i): """Create unique sample for index i.""" return Sample( **{ 'name': f'Sample{i}', 'metadata': { 'foobar': f'baz{i}' }, KrakenResultModule.name(): create_kraken(), KrakenHLLResultModule.name(): create_krakenhll(), Metaphlan2ResultModule.name(): create_metaphlan2(), }).save()
def trees_from_sample(sample): """Build taxa trees for a given sample.""" metaphlan2 = sample[Metaphlan2ResultModule.name()] metaphlan2 = reduce_taxa_list(metaphlan2['taxa']) kraken = sample[KrakenResultModule.name()] kraken = reduce_taxa_list(kraken['taxa']) krakenhll = sample[KrakenHLLResultModule.name()] krakenhll = reduce_taxa_list(krakenhll['taxa']) return { 'kraken': kraken, 'metaphlan2': metaphlan2, 'krakenhll': krakenhll, }
def run_sample_group(cls, sample_group, samples): """Gather samples and process.""" reducer = sample_similarity_reducer.s(samples) persist_task = persist_result.s(sample_group.analysis_result_uuid) categories_task = categories_from_metadata.s(samples) kraken_task = taxa_tool_tsne.s(samples, KrakenResultModule.name()) krakenhll_task = taxa_tool_tsne.s(samples, KrakenHLLResultModule.name()) metaphlan2_task = taxa_tool_tsne.s(samples, Metaphlan2ResultModule.name()) middle_tasks = [ categories_task, kraken_task, krakenhll_task, metaphlan2_task ] return chord(middle_tasks)(reducer | persist_task)
def make_all_flows(samples): """Determine flows by tool.""" flow_tbl = {} tool_names = [ Metaphlan2ResultModule.name(), KrakenResultModule.name(), KrakenHLLResultModule.name(), ] for tool_name in tool_names: taxa_tbl = make_taxa_table(samples, tool_name) save_tool_name = 'kraken' if 'metaphlan2' in tool_name: save_tool_name = 'metaphlan2' elif 'krakenhll' in tool_name: save_tool_name = 'krakenhll' flow_tbl[save_tool_name] = make_flow(taxa_tbl) return {'by_tool': flow_tbl}
def sample_similarity_reducer(args, samples): """Combine Sample Similarity components.""" categories = args[0] kraken_tool, kraken_labeled = args[1] krakenhll_tool, krakenhll_labeled = args[2] metaphlan_tool, metaphlan_labeled = args[3] data_records = update_data_records(samples, categories, kraken_labeled, krakenhll_labeled, metaphlan_labeled) tools = { KrakenResultModule.name(): kraken_tool, KrakenHLLResultModule.name(): krakenhll_tool, Metaphlan2ResultModule.name(): metaphlan_tool, } result_data = { 'categories': categories, 'tools': tools, 'data_records': data_records, } return result_data
def make_volcanos(categories, samples): """Return the JSON for a VolcanoResult.""" dataframe_keys = { KrakenResultModule.name(): 'taxa', Metaphlan2ResultModule.name(): 'taxa', } out = {'categories': categories, 'tools': {}} for tool_name, dataframe_key in dataframe_keys.items(): out['tools'][tool_name] = {'tool_categories': {}} tool_tbl = out['tools'][tool_name]['tool_categories'] for category_name, category_values in categories.items(): tool_tbl[category_name] = {} for category_value in category_values: category_value = scrub_category_val(category_value) scatter_plot = handle_one_tool_category( category_name, category_value, samples, tool_name, dataframe_key, ) if scatter_plot is not None: tool_tbl[category_name][category_value] = scatter_plot return out
from app import db from app.display_modules.sample_similarity import SampleSimilarityDisplayModule from app.display_modules.sample_similarity.wrangler import SampleSimilarityWrangler from app.samples.sample_models import Sample from app.tool_results.kraken import KrakenResultModule from app.tool_results.kraken.tests.factory import create_kraken from app.tool_results.krakenhll import KrakenHLLResultModule from app.tool_results.krakenhll.tests.factory import create_krakenhll from app.tool_results.metaphlan2 import Metaphlan2ResultModule from app.tool_results.metaphlan2.tests.factory import create_metaphlan2 from tests.base import BaseTestCase from tests.utils import add_sample_group KRAKEN_NAME = KrakenResultModule.name() KRAKENHLL_NAME = KrakenHLLResultModule.name() METAPHLAN2_NAME = Metaphlan2ResultModule.name() class TestSampleSimilarityWrangler(BaseTestCase): """Test suite for Sample Similarity Wrangler.""" def test_run_sample_group(self): """Ensure run_sample_group produces correct results.""" def create_sample(i): """Create unique sample for index i.""" metadata = {'foobar': f'baz{i}'} sample_data = { 'name': f'Sample{i}', 'metadata': metadata, KRAKEN_NAME: create_kraken(),