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
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)
def test_invalid_correlation_value(self, data_folder): with pytest.raises(ValueError): mminte.read_correlation_file(join(data_folder, 'correlation_value.txt'))
def test_bad_correlation_file(self, data_folder): with pytest.raises(IOError): mminte.read_correlation_file(join(data_folder, 'BAD.txt'))
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)
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)
def getHTML(self, params): """ Run Widget 6 and generate HTML output for Results tab. """ # Validate input parameters. cherrypy.log('Widget 6 input parameters: {0}'.format(params)) if not exists(params['analysis_folder']): try: makedirs(params['analysis_folder']) except Exception as e: cherrypy.log( 'Widget 6: 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) growth_rates_file = join(params['analysis_folder'], params['growth_rates_file']) if not exists(growth_rates_file): cherrypy.log( 'Widget 6: growth rates file "{0}" was not found'.format( growth_rates_file)) return 'Sorry, growth rates file "{0}" was not found. Make sure the path to the file is correct.' \ .format(growth_rates_file) similarity_file = join(params['analysis_folder'], params['similarity_file']) if not exists(similarity_file): cherrypy.log( 'Widget 6: 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']) # Generate data for plot of interaction network. try: cherrypy.log( 'Widget 6: Started generating data for plot of interaction network' ) growth_rates = read_growth_rates_file(growth_rates_file) similarity = read_similarity_file(similarity_file) 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 6: Finished generating data for plot of interaction network' ) except Exception as e: cherrypy.log( 'Widget 6: 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:{0}/widget6_out'.format( cherrypy.server.socket_port), new=1) return text
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
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)