Example #1
0
    def getHTML(self, params):
        """ Run Widget 1 and generate HTML output for Results tab. """

        # Validate input parameters.
        cherrypy.log('Widget 1 input parameters: {0}'.format(params))
        if not exists(params['correlation_file']):
            cherrypy.log(
                'Widget 1: 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 1: 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['analysis_folder']):
            try:
                makedirs(params['analysis_folder'])
            except Exception as e:
                cherrypy.log(
                    'Widget 1: 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)

        # Get the unique OTU sequences.
        try:
            cherrypy.log('Widget 1: Started getting unique OTU sequences')
            get_unique_otu_sequences(
                read_correlation_file(params['correlation_file']),
                params['representative_otu_file'],
                join(params['analysis_folder'], params['unique_otu_file']))
            cherrypy.log("Widget 1: Finished getting unique OTU sequences")
        except Exception as e:
            cherrypy.log(
                'Widget 1: 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)

        # Generate the output for the Results tab.
        text = [
            'Here are the OTUs that will be used in the rest of the analysis. You can find '
            'the full sequences in the "{0}" file.<br>'.format(
                params['unique_otu_file'])
        ]
        with open(join(params['analysis_folder'], params['unique_otu_file']),
                  'r') as handle:
            for line in handle:
                if line.startswith('>'):
                    text.append('<br>{0}'.format(line))

        return text
Example #2
0
    def test_representative(self, data_folder, test_folder):
        correlations = mminte.read_correlation_file(join(data_folder, 'correlation.txt'))
        sequence_filename = join(data_folder, 'all_otus.fasta')
        unique_filename = join(test_folder, 'unique_otus.fasta')
        num_sequences = mminte.get_unique_otu_sequences(correlations, sequence_filename, unique_filename)
        assert num_sequences == 18

        blast_filename = join(test_folder, 'blast.txt')
        genome_ids, similarity = mminte.search(unique_filename, blast_filename)
        assert len(genome_ids) == 18
        assert len(similarity) == 18
        assert '484018.6' in genome_ids

        unlink(unique_filename)
        unlink(blast_filename)
Example #3
0
 def test_bad_all_sequence_file(self, data_folder, test_folder):
     correlations = mminte.read_correlation_file(join(data_folder, 'correlation.txt'))
     sequence_filename = join(data_folder, 'BAD.fasta')
     unique_filename = join(test_folder, 'unique_otus.fasta')
     with pytest.raises(IOError):
         mminte.get_unique_otu_sequences(correlations, sequence_filename, unique_filename)
Example #4
0
 def test_missing_otu(self, data_folder, test_folder):
     correlations = mminte.read_correlation_file(join(data_folder, 'correlation.txt'))
     sequence_filename = join(data_folder, 'missing.fasta')
     unique_filename = join(test_folder, 'unique_otus.fasta')
     with pytest.raises(ValueError):
         mminte.get_unique_otu_sequences(correlations, sequence_filename, unique_filename)
Example #5
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