Exemplo n.º 1
0
    def test_growth_rates(self, data_folder, test_folder):
        model_files = ['BT.sbml', 'FP.sbml']
        source_models = mminte.get_all_pairs(
            [join(data_folder, x) for x in model_files])
        pair_models = mminte.create_interaction_models(source_models,
                                                       test_folder)
        assert len(pair_models) == 1
        assert pair_models[0] == '{0}/BTxFP.json'.format(test_folder) or \
               pair_models[0] == '{0}/FPxBT.json'.format(test_folder)

        western = json.load(open(join(data_folder, 'western.json')))
        growth_rates = mminte.calculate_growth_rates(pair_models, western)
        assert growth_rates.at[0, 'A_ID'] == 'BT'
        assert growth_rates.at[0, 'B_ID'] == 'FP'
        assert growth_rates.at[0, 'TYPE'] == 'Parasitism'
        assert growth_rates.at[0, 'TOGETHER'] == pytest.approx(0.49507501)
        assert growth_rates.at[0, 'A_TOGETHER'] == pytest.approx(0.27746256)
        assert growth_rates.at[0, 'B_TOGETHER'] == pytest.approx(0.21761245)
        assert growth_rates.at[0, 'A_ALONE'] == pytest.approx(0.44073842)
        assert growth_rates.at[0, 'B_ALONE'] == pytest.approx(0.16933796)
        assert growth_rates.at[0, 'A_CHANGE'] == pytest.approx(-0.37045977)
        assert growth_rates.at[0, 'B_CHANGE'] == pytest.approx(0.28507777)

        for model in pair_models:
            unlink(model)
Exemplo n.º 2
0
def prepare(single_file_names,
            pair_model_folder,
            optimize=False,
            n_processes=None):
    """ Prepare for simulation by creating two species community models.

    Parameters
    ----------
    single_file_names : list of str
        Paths to single species model files of community members
    pair_model_folder : str
        Path to folder for storing two species community models
    optimize : bool
        When True, confirm single species models optimize successfully before creating community models
    n_processes: int, optional
        Number of processes in job pool

    Returns
    -------
    list of str
        List of paths to two species community models
    """

    # If requested, confirm all of the single species input models produce growth as provided.
    if optimize:
        LOGGER.info('Started checking %d single models for growth',
                    len(single_file_names))
        if n_processes is None:
            n_processes = min(cpu_count(), 4)
        pool = Pool(n_processes)
        result_list = [
            pool.apply_async(check_for_growth, (file_name, ))
            for file_name in single_file_names
        ]
        for result in result_list:
            summary = result.get()
            if not summary['grows']:
                warn(summary['message'])
        LOGGER.info('Finished checking single models for growth')

    # Create folder for output pair models if needed.
    if not exists(pair_model_folder):
        makedirs(pair_model_folder)

    # Create all of the pair models and store in specified folder.
    LOGGER.info('Started creating two species community models')
    pair_file_names = create_interaction_models(
        get_all_pairs(single_file_names),
        pair_model_folder,
        n_processes=n_processes)
    LOGGER.info('Finished creating two species community models')
    return pair_file_names
Exemplo n.º 3
0
    def getHTML(self, params):
        """ Run Widget All and generate HTML output for Results tab. """

        # Validate input parameters.
        cherrypy.log('Widget A input parameters: {0}'.format(params))
        if not exists(params['correlation_file']):
            cherrypy.log('Widget A: correlation file "{0}" was not found'.format(params['correlation_file']))
            return 'Sorry, correlation file "{0}" was not found. Make sure the path to the file is correct.' \
                   .format(params['correlation_file'])
        if not exists(params['representative_otu_file']):
            cherrypy.log('Widget A: representative OTU file "{0}" was not found'
                         .format(params['representative_otu_file']))
            return 'Sorry, representative OTU file "{0}" was not found. Make sure the path to the file is correct.' \
                   .format(params['representative_otu_file'])
        if not exists(params['diet_file']):
            cherrypy.log('Widget A: diet file "{0}" was not found'.format(params['diet_file']))
            return 'Sorry, diet file "{0}" was not found. Make sure the path to the file is correct.' \
                   .format(params['diet_file'])
        if not exists(params['analysis_folder']):
            try:
                makedirs(params['analysis_folder'])
            except Exception as e:
                cherrypy.log('Widget A: Error creating folder "{0}" for analysis files: {1}'
                             .format(params['analysis_folder'], e))
                return 'Sorry, something went wrong creating the folder "{0}" for the analysis files. Make sure ' \
                       'the path to the file is correct.<br><br>Exception: {1}'.format(params['analysis_folder'], e)

        # Widget 1 - Get the unique OTU sequences.
        try:
            cherrypy.log('Widget A1: Started getting unique OTU sequences')
            unique_otus_file = join(params['analysis_folder'], 'unique_otus.fasta')
            get_unique_otu_sequences(read_correlation_file(params['correlation_file']),
                                     params['representative_otu_file'],
                                     unique_otus_file)
            cherrypy.log("Widget A1: Finished getting unique OTU sequences")

        except Exception as e:
            cherrypy.log('Widget A1: Error getting unique OTU sequences: {0}'.format(e))
            return "Sorry, something went wrong. Make sure the paths to your files are correct.<br><br>" \
                   "Exception: {0}.".format(e)

        # Widget 2 - Run blast search to find matching bacterial species.
        try:
            cherrypy.log('Widget A2: Started blast search for matching bacterial species')
            blast_output_file = join(params['analysis_folder'], 'blast.txt')
            genome_ids, similarity = search(unique_otus_file, blast_output_file)
            with open(join(params['analysis_folder'], 'genome_ids.txt'), 'w') as handle:
                handle.write('\n'.join(genome_ids)+'\n')
            write_similarity_file(similarity, join(params['analysis_folder'], 'similarity.csv'))
            cherrypy.log("Widget A2: Finished blast search")

        except Exception as e:
            cherrypy.log("Widget A2: Error running blast search: {0}".format(e))
            return "Sorry, something went wrong. Make sure the paths to your files are correct and " \
                   "that the correct version of blast is installed.<br><br>Exception: {0}".format(e)

        # Widget 3 - Create single species models using ModelSEED.
        model_folder = join(params['analysis_folder'], 'single_models')
        if not exists(model_folder):
            try:
                makedirs(model_folder)
            except Exception as e:
                cherrypy.log('Widget A3: Error creating folder "{0}" for model files: {1}'
                             .format(params['model_folder'], e))
                return 'Sorry something went wrong creating the folder "{0}" for the model files. Make sure ' \
                       'the path to the folder is correct.<br><br>Exception: {1}'.format(params['model_folder'], e)
        try:
            cherrypy.log('Widget A3: Started creating models for {0} genomes'.format(len(genome_ids)))
            single_filenames = create_species_models(genome_ids, model_folder)
            output_filename = join(params['analysis_folder'], 'single_model_filenames.txt')
            with open(output_filename, 'w') as handle:
                handle.write('\n'.join(single_filenames)+'\n')
            cherrypy.log('Widget A3: Finished creating and downloading {0} models'.format(len(single_filenames)))

        except Exception as e:
            cherrypy.log('Widget A3: Error creating models: {0}'.format(e))
            return "Sorry, something went wrong creating metabolic models using ModelSEED.<br><br>" \
                   "Exception: {0}".format(e)

        # Widget 4 - Create two species community models.
        pair_model_folder = join(params['analysis_folder'], 'pair_models')
        if not exists(pair_model_folder):
            try:
                makedirs(pair_model_folder)
            except Exception as e:
                cherrypy.log('Widget A4: We were unable to create folder "{0}" for community model files'
                             .format(pair_model_folder))
                return 'Sorry, something went wrong creating the folder "{0}" for the community model files. ' \
                       'Make sure the path to the folder is correct.<br><br>Exception: {1}' \
                       .format(pair_model_folder, e)
        try:
            pairs = get_all_pairs(single_filenames)
            cherrypy.log('Widget A4: Started creating community models from {0} pairs of single species models'
                         .format(len(pairs)))
            pair_filenames = create_interaction_models(pairs, output_folder=pair_model_folder)
            output_filename = join(params['analysis_folder'], 'pair_model_filenames.txt')
            with open(output_filename, 'w') as handle:
                handle.write('\n'.join(pair_filenames)+'\n')
            cherrypy.log("Widget A4: Finished creating {0} community models".format(len(pair_filenames)))

        except Exception as e:
            cherrypy.log("Widget A4: Error creating community models: {0}".format(e))
            return "Sorry, something went wrong. Make sure the path to your file is correct and " \
                   "that the Python package cobrapy is loaded into your system.<br><br>Exception: {0}".format(e)

        # Widget 5 - Calculate growth rates for the two species models.
        try:
            cherrypy.log("Widget A5: Started calculating growth rates for {0} pair models"
                         .format(len(pair_filenames)))
            medium = read_diet_file(params['diet_file'])
            growth_rates = calculate_growth_rates(pair_filenames, medium)
            write_growth_rates_file(growth_rates, join(params['analysis_folder'], 'growth_rates.csv'))
            cherrypy.log("Widget A5: Finished calculating the growth rates")

        except Exception as e:
            cherrypy.log("Widget A5: Error calculating growth rates: {0}".format(e))
            return "Sorry, something went wrong. Make sure the path to your file is correct.<br><br>" \
                   "Exception: {0}".format(e)

        # Widget 6 - Generate data for plot of interaction network.
        try:
            cherrypy.log('Widget A6: Started generating data for plot of interaction network')
            correlation = read_correlation_file(params['correlation_file'])
            make_d3_source(growth_rates, join(params['analysis_folder'], 'data4plot.json'), similarity, correlation)
            make_d3_source(growth_rates, self.getRoot().data4plot_filename(), similarity, correlation)
            cherrypy.log('Widget A6: Finished generating data for plot of interaction network')

        except Exception as e:
            cherrypy.log('Widget A6: Error generating data for plot of network: {0}'.format(e))
            return 'Sorry, something went wrong. Make sure the locations of your files are correct.<br><br>' \
                   'Exception: {0}'.format(e)

        # Generate the output for the Results tab.
        text = ["The plot with the network of interactions between your organisms is shown below or "
                "on a new tab.<br><br>The shading of the nodes indicates how close the sequence of the "
                "OTU is to the sequence of the genome. The darker the node, the higher the similarity.<br><br>"
                "The length and thickness of the links reflect the association values on the initial "
                "file you provided. The shorter and thicker the link, the higher the association value.<br><br>"
                "The colors of the links reflect the kind of interaction. The red, green and grey "
                "represent negative, positive and no interaction, respectively.<br><br>"
                "<a href='http://d3js.org/'>D3 is awesome</a>! If you mouse over the nodes, you get "
                "the ID of the OTU, and if you click a node and drag it, the network will follow it."]

        if params['browser_tab'] == 'Current':
            text.append(self.getRoot().widget6_out())
        else:
            webbrowser.open('http://localhost:8080/widget6_out', new=1)

        return text
Exemplo n.º 4
0
    def getHTML(self, params):
        """ Run Widget 4 and generate HTML output for Results tab. """

        # Validate input parameters.
        cherrypy.log('Widget 4 input parameters: {0}'.format(params))
        if not exists(params['analysis_folder']):
            try:
                makedirs(params['analysis_folder'])
            except Exception as e:
                cherrypy.log('Widget 4: Error creating folder "{0}" for analysis files: {1}'
                             .format(params['analysis_folder'], e))
                return 'Sorry, something went wrong creating the folder "{0}" for the analysis files. Make sure ' \
                       'the path to the file is correct.<br><br>Exception: {1}'.format(params['analysis_folder'], e)
        community_folder = join(params['analysis_folder'], params['community_folder'])
        if not exists(community_folder):
            try:
                makedirs(community_folder)
            except Exception as e:
                cherrypy.log('Widget 4: We were unable to create folder "{0}" for community model files'
                             .format(community_folder))
                return 'Sorry, something went wrong creating the folder "{0}" for the community model files. ' \
                       'Make sure the path to the folder is correct.<br><br>Exception: {1}' \
                       .format(community_folder, e)
        if params['pair_input_type'] == 'all':
            model_list_file = join(params['analysis_folder'], params['single_models_file'])
            if not exists(model_list_file):
                cherrypy.log('Widget 4: Model list file "{0}" was not found'.format(model_list_file))
                return 'Sorry, model list file "{0}" was not found. Make sure the path to the file is correct.' \
                       .format(model_list_file)
            with open(model_list_file, 'r') as handle:
                source_models = [line.strip() for line in handle]
            pairs = get_all_pairs(source_models)
            cherrypy.log('Widget 4: Generated {0} pairs from {1} source models'.format(len(pairs), len(source_models)))
        elif params['pair_input_type'] == 'specific':
            pair_list_file = join(params['analysis_folder'], params['pair_list_file'])
            if not exists(pair_list_file):
                cherrypy.log('Widget 4: Pair list file "{0}" was not found'.format(pair_list_file))
                return 'Sorry, pair list file "{0}" was not found. Make sure the path to the file is correct.' \
                       .format(pair_list_file)
            with open(pair_list_file, 'r') as handle:
                pair_list = [line.strip().split('\t') for line in handle]
            for index in range(len(pair_list)):
                fields = pair_list[index]
                if len(fields) != 2:
                    return 'Line {0} in "{1}" file must have two columns separated by tab' \
                           .format(index, params['pair_list_file'])
            pairs = [(fields[0], fields[1]) for fields in pair_list]
            cherrypy.log('Widget 4: Found {0} pairs in pair list file'.format(len(pairs)))
        else:
            similarity_file = join(params['analysis_folder'], params['similarity_file'])
            if not exists(similarity_file):
                cherrypy.log('Widget 4: Similarity file "{0}" was not found'.format(similarity_file))
                return 'Sorry, similarity file "{0}" was not found. Make sure the path to the file is correct.' \
                       .format(similarity_file)
            if not exists(params['correlation_file']):
                cherrypy.log('Widget 6: correlation file "{0}" was not found'.format(params['correlation_file']))
                return 'Sorry, correlation file "{0}" was not found. Make sure the path to the file is correct.' \
                    .format(params['correlation_file'])
            similarity = read_similarity_file(similarity_file)
            correlation = read_correlation_file(params['correlation_file'])

            pairs = list()
            for corr in correlation:
                one = similarity.loc[similarity['OTU_ID'] == corr[0]].iloc[0]['GENOME_ID']
                two = similarity.loc[similarity['OTU_ID'] == corr[1]].iloc[0]['GENOME_ID']
                pairs.append((join(params['analysis_folder'], params['model_folder'], '{0}.json'.format(one)),
                              join(params['analysis_folder'], params['model_folder'], '{0}.json'.format(two))))

        # Create two species community models.
        try:
            cherrypy.log('Widget 4: Started creating community models from {0} pairs of single species models'
                         .format(len(pairs)))
            model_filenames = create_interaction_models(pairs, output_folder=community_folder)
            output_filename = join(params['analysis_folder'], params['pair_models_file'])
            with open(output_filename, 'w') as handle:
                handle.write('\n'.join(model_filenames)+'\n')
            cherrypy.log("Widget 4: Finished creating {0} community models".format(len(model_filenames)))

        except Exception as e:
            cherrypy.log("Widget 4: Error creating community models: {0}".format(e))
            return "Sorry something's wrong. Make sure the path to your file is correct and " \
                   "that the Python package cobrapy is loaded into your system.<br><br>Exception: {0}".format(e)

        # Generate the output for the Results tab.
        return 'We created {0} two species community models. In the next widget, we will use them ' \
               'to predict the growth rate of their species in isolation and when in the community ' \
               'using COBRA tools. A list of the community model file names was stored in the ' \
               '"{1}" file.'.format(len(model_filenames), output_filename)
Exemplo n.º 5
0
 def test_bad_extension(self, data_folder, test_folder):
     model_files = ['BT.sbml', 'FP.bad']
     source_models = mminte.get_all_pairs(
         [join(data_folder, x) for x in model_files])
     with pytest.raises(IOError):
         mminte.create_interaction_models(source_models, test_folder)
Exemplo n.º 6
0
 def test_not_enough_source(self, data_folder, test_folder):
     with pytest.raises(ValueError):
         mminte.create_interaction_models([(join(data_folder, 'BT.sbml'))],
                                          test_folder)
Exemplo n.º 7
0
 def test_bad_extension(self, data_folder):
     model_files = ['BT.sbml', 'FP.bad']
     source_models = [join(data_folder, x) for x in model_files]
     with pytest.raises(IOError):
         mminte.create_interaction_models(source_models, output_folder=gettempdir())