def get_hyphy_debug_info(hyphy_output): """ @param hyphy_output: the string representing the hyphy output @return: a string explaining how the output was interpreted """ ns = Hyphy.get_hyphy_namespace(StringIO(hyphy_output)) out = StringIO() print >> out, 'raw hyphy output:' print >> out, '---------------------------------------' print >> out, hyphy_output print >> out, '---------------------------------------' print >> out, '' print >> out, '' print >> out, 'processed hyphy output lines:' print >> out, '---------------------------------------' for i, line in enumerate(ns.get_processed_lines()): print >> out, i, ':', line print >> out, '---------------------------------------' print >> out, '' print >> out, '' print >> out, 'hyphy namespace object dictionary:' print >> out, '---------------------------------------' print >> out, ns.__dict__ print >> out, '---------------------------------------' return out.getvalue().strip()
def get_response_content(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # read the nexus data nexus = Nexus.Nexus() try: nexus.load(StringIO(fs.nexus)) except Nexus.NexusError as e: raise HandlingError(e) # do things in the working directory with Util.remember_cwd(): os.chdir(Config.data_path) # create the batch file with open(hyphy_bf, 'wt') as fout: print >> fout, hky_hyphy_model # create the nexus file with open(hyphy_nexus, 'wt') as fout: print >> fout, nexus # run hyphy cmd = [Config.hyphy_exe_path, hyphy_bf] p = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE) hyphy_output = p.stdout.read() # read the hyphy output ns = Hyphy.get_hyphy_namespace(StringIO(hyphy_output)) out = StringIO() if fs.outdebug: print >> out, get_hyphy_debug_info(hyphy_output) print >> out, '' print >> out, '' if fs.outmodel: print >> out, 'hyphy model:' print >> out, '---------------------------------------' print >> out, hky_hyphy_model print >> out, '---------------------------------------' print >> out, '' print >> out, '' print >> out, 'tree :', ns.givenTree.get_newick_string() print >> out, '' print >> out, 'log likelihood :', ns.lnL print >> out, '' print >> out, 'kappa :', ns.kappa print >> out, '' print >> out, 'A :', ns.eqFreqA print >> out, 'C :', ns.eqFreqC print >> out, 'G :', ns.eqFreqG print >> out, 'T :', ns.eqFreqT # return the results return out.getvalue()
def get_response_content(fs): # read the nexus data nexus = Nexus.Nexus() try: nexus.load(StringIO(fs.nexus)) except Nexus.NexusError as e: raise HandlingError(e) # read the hyphy variables ns = Hyphy.get_hyphy_namespace(StringIO(fs.hyphy)) # get the mixture weights mixture_weights = [ns.P, 1.0 - ns.P] # get the nucleotide distributions nucleotide_distributions = [] for suffix in ("", "2"): distribution = {} for nt in list("ACGT"): var = "eqFreq" + nt + suffix proportion = getattr(ns, var) distribution[nt] = proportion nucleotide_distributions.append(distribution) # create the normalized nucleotide HKY rate matrix objects rate_matrix_objects = [] for nt_distribution in nucleotide_distributions: rate_matrix_object = RateMatrix.get_unscaled_hky85_rate_matrix(nt_distribution, ns.kappa) rate_matrix_object.normalize() rate_matrix_objects.append(rate_matrix_object) # create the mixture proportions weight_sum = sum(mixture_weights) mixture_proportions = [weight / weight_sum for weight in mixture_weights] # scale each rate matrix object by its branch length ratio for rate_matrix_object, tree_name in zip(rate_matrix_objects, ("givenTree", "otherTree")): nexus_tree = nexus.tree hyphy_tree = getattr(ns, tree_name) try: nexus_human_node = nexus_tree.get_unique_node("Human") except Newick.NewickSearchError as e: raise HandlingError("nexus tree error: %s" % e) try: hyphy_human_node = hyphy_tree.get_unique_node("HUMAN") except Newick.NewickSearchError as e: raise HandlingError("hyphy tree error: %s" % e) sf = hyphy_human_node.blen / nexus_human_node.blen rate_matrix_object.rescale(sf) # create the mixture model mixture_model = SubModel.MixtureModel(mixture_proportions, rate_matrix_objects) # return the results return do_analysis(mixture_model, nexus.alignment, nexus.tree) + "\n"
def get_response(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # read the nexus data nexus = Nexus.Nexus() try: nexus.load(StringIO(fs.nexus)) except Nexus.NexusError as e: raise HandlingError(e) # move to the data directory original_directory = os.getcwd() os.chdir(Config.data_path) # create the batch file category_suffixes = [ str(category + 1) for category in range(fs.ncategories) ] hky_hyphy_model = get_hyphy_model_string(hyphy_nexus, fs.ncategories) with open(hyphy_bf, 'wt') as fout: print >> fout, hky_hyphy_model # create the nexus file with open(hyphy_nexus, 'wt') as fout: print >> fout, nexus # run hyphy p = subprocess.Popen([Config.hyphy_exe_path, hyphy_bf], close_fds=True, stdout=subprocess.PIPE) hyphy_output = p.stdout.read() # move back to the original directory os.chdir(original_directory) # read the hyphy output ns = Hyphy.get_hyphy_namespace(StringIO(hyphy_output)) out = StringIO() if fs.outdebug: print >> out, get_hyphy_debug_info(hyphy_output) print >> out, '' print >> out, '' if fs.outmodel: print >> out, 'hyphy model:' print >> out, '---------------------------------------' print >> out, hky_hyphy_model print >> out, '---------------------------------------' print >> out, '' print >> out, '' if True: print >> out, 'reformatted hyphy output:' print >> out, '---------------------------------------' # show the log likelihood print >> out, 'log likelihood :', ns.lnL print >> out, '' # show the kappa value print >> out, 'kappa :', ns.kappa print >> out, '' category_blocks = [] for suffix in category_suffixes: block = StringIO() print >> block, 'mixing proportion :', getattr( ns, 'catFreq' + suffix) print >> block, 'tree :', getattr(ns, 'tree' + suffix).get_newick_string() for nt in list('ACGT'): print >> block, nt, ':', getattr(ns, 'eqFreq' + nt + suffix) category_blocks.append(block.getvalue().strip()) print >> out, '\n\n'.join(category_blocks) print >> out, '---------------------------------------' print >> out, '' print >> out, '' if fs.outcheck: # get the raw matrices matrices = [] for suffix in category_suffixes: nt_dict = {} for nt in list('ACGT'): nt_dict[nt] = getattr(ns, 'eqFreq' + nt + suffix) total = float(sum(nt_dict.values())) nt_dict = dict((k, v / total) for k, v in nt_dict.items()) matrix = RateMatrix.get_unscaled_hky85_rate_matrix( nt_dict, ns.kappa) matrices.append(matrix) raw_matrix_rates = [matrix.get_expected_rate() for matrix in matrices] category_weights = [] for suffix in category_suffixes: category_weights.append(getattr(ns, 'catFreq' + suffix)) total = float(sum(category_weights)) category_distribution = [weight / total for weight in category_weights] mixture_model = SubModel.MixtureModel(category_distribution, matrices) raw_mixture_rate = mixture_model.get_expected_rate() # rescale the mixture model # 0.75 is the expected rate of the initial model r1 = 0.75 scaling_factor = r1 mixture_model.rescale(scaling_factor) recomputed_log_likelihood = PhyLikelihood.get_log_likelihood( nexus.tree, nexus.alignment, mixture_model) print >> out, 'recomputed likelihood and rates:' print >> out, '---------------------------------------' print >> out, 'log likelihood :', recomputed_log_likelihood print >> out, '' print >> out, 'rate :', raw_mixture_rate print >> out, '' for rate, suffix in zip(raw_matrix_rates, category_suffixes): print >> out, 'rate%s : %s' % (suffix, rate) print >> out, '---------------------------------------' print >> out, '' print >> out, '' # return the response return out.getvalue()
def get_response(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # read the nexus data nexus = Nexus.Nexus() try: nexus.load(StringIO(fs.nexus)) except Nexus.NexusError as e: raise HandlingError(e) # move to the data directory original_directory = os.getcwd() os.chdir(Config.data_path) # create the batch file category_suffixes = [str(category+1) for category in range(fs.ncategories)] hky_hyphy_model = get_hyphy_model_string(hyphy_nexus, fs.ncategories) with open(hyphy_bf, 'wt') as fout: print >> fout, hky_hyphy_model # create the nexus file with open(hyphy_nexus, 'wt') as fout: print >> fout, nexus # run hyphy p = subprocess.Popen([Config.hyphy_exe_path, hyphy_bf], close_fds=True, stdout=subprocess.PIPE) hyphy_output = p.stdout.read() # move back to the original directory os.chdir(original_directory) # read the hyphy output ns = Hyphy.get_hyphy_namespace(StringIO(hyphy_output)) out = StringIO() if fs.outdebug: print >> out, get_hyphy_debug_info(hyphy_output) print >> out, '' print >> out, '' if fs.outmodel: print >> out, 'hyphy model:' print >> out, '---------------------------------------' print >> out, hky_hyphy_model print >> out, '---------------------------------------' print >> out, '' print >> out, '' if True: print >> out, 'reformatted hyphy output:' print >> out, '---------------------------------------' # show the log likelihood print >> out, 'log likelihood :', ns.lnL print >> out, '' # show the kappa value print >> out, 'kappa :', ns.kappa print >> out, '' category_blocks = [] for suffix in category_suffixes: block = StringIO() print >> block, 'mixing proportion :', getattr(ns, 'catFreq'+suffix) print >> block, 'tree :', getattr(ns, 'tree'+suffix).get_newick_string() for nt in list('ACGT'): print >> block, nt, ':', getattr(ns, 'eqFreq'+nt+suffix) category_blocks.append(block.getvalue().strip()) print >> out, '\n\n'.join(category_blocks) print >> out, '---------------------------------------' print >> out, '' print >> out, '' if fs.outcheck: # get the raw matrices matrices = [] for suffix in category_suffixes: nt_dict = {} for nt in list('ACGT'): nt_dict[nt] = getattr(ns, 'eqFreq'+nt+suffix) total = float(sum(nt_dict.values())) nt_dict = dict((k, v/total) for k, v in nt_dict.items()) matrix = RateMatrix.get_unscaled_hky85_rate_matrix( nt_dict, ns.kappa) matrices.append(matrix) raw_matrix_rates = [matrix.get_expected_rate() for matrix in matrices] category_weights = [] for suffix in category_suffixes: category_weights.append(getattr(ns, 'catFreq'+suffix)) total = float(sum(category_weights)) category_distribution = [weight / total for weight in category_weights] mixture_model = SubModel.MixtureModel(category_distribution, matrices) raw_mixture_rate = mixture_model.get_expected_rate() # rescale the mixture model # 0.75 is the expected rate of the initial model r1 = 0.75 scaling_factor = r1 mixture_model.rescale(scaling_factor) recomputed_log_likelihood = PhyLikelihood.get_log_likelihood( nexus.tree, nexus.alignment, mixture_model) print >> out, 'recomputed likelihood and rates:' print >> out, '---------------------------------------' print >> out, 'log likelihood :', recomputed_log_likelihood print >> out, '' print >> out, 'rate :', raw_mixture_rate print >> out, '' for rate, suffix in zip(raw_matrix_rates, category_suffixes): print >> out, 'rate%s : %s' % (suffix, rate) print >> out, '---------------------------------------' print >> out, '' print >> out, '' # return the response return out.getvalue()