def runAgent(self): GrabScreen.findWindow("RotMGExalt") while True: while self.gameState.mode[0] == "Nexus": print("hello sir, i reside in the nexus.") screenEnemies = GetData.getEnemiesScreen( self.gameState.frame[0]) AgentTest.Aim(screenEnemies, self.gameState.gameWindow[0], self.gameState.frame[0]) Nexus.doNexus() while self.gameState.mode[0] == "Realm": start_time = time.time() print("ahh good day fine sir, you may find me in the realm.") screenEnemies = GetData.debugEnemiesScreen1( self.gameState.frame[0]) AgentTest.Aim1(screenEnemies, self.gameState.gameWindow[0], self.gameState.frame[0]) mapEnemies = GetData.getEnemiesMap(self.gameState.frame[0]) screenBags = GetData.findLootBags(self.gameState.frame[0]) self.gameState.playerPos[0] = GetData.getPlayerPos( self.gameState.frame[0])[0] #Hey future alex, this is a note from past alex...i know magic right? #This is where you left off, check for screen enemies > 1 and use getData.GetSafeMovement to update closestEnemyPos for movement. if len(screenEnemies) >= 1: self.gameState.closestEnemyPos[ 0] = GetData.getSafeMovement(self.gameState.frame[0]) self.gameState.retreatDistance[0] = -10 self.gameState.moveTowardDistance[0] = 0 self.gameState.movementLength[0] = 1000 print("avoid move") elif len(screenBags) > 0 and random.randint(0, 10) > 4: self.gameState.closestEnemyPos[0] = screenBags[0] self.gameState.retreatDistance[0] = 0 self.gameState.moveTowardDistance[0] = 20 else: self.gameState.closestEnemyPos[ 0] = AgentTest.findClosestEnemy( mapEnemies, [self.gameState.playerPos[0]]) self.gameState.retreatDistance[0] = 15 self.gameState.moveTowardDistance[0] = 16 self.gameState.movementLength[0] = 1000 self.gameState.playerPos[0] = self.gameState.playerPos[0] print("FPS: ", 1.0 / (time.time() - start_time)) while self.gameState.mode[0] == "Transition": print("To be honest...im not sure where i am, im blind") while self.gameState.mode[0] == "Loot": print("pardon me sir, im counting my cheddar") pyautogui.keyUp("w") pyautogui.keyUp("s") pyautogui.keyUp("a") pyautogui.keyUp("d") Looting.doLootingNoImage(self.gameState.frame[0], self.gameState.gameWindow[0])
def run_in_tmp(fs, nexus): """ @param fs: fieldstorage-like user options @param nexus: the nexus object that defines the tree and alignment @return: from_paml """ # create the control object config = Paml.PamlConfig() config.set_hky() # create the baseml.ctl control file ctl_string = config.to_ctl_string() with open(Paml.baseml_ctl, 'wt') as fout: print >> fout, ctl_string # create the nexus object that defines the tree and alignment nexus = Nexus.get_sample_nexus_object() # create the baseml.newick tree file with open(Paml.baseml_newick, 'wt') as fout: print >> fout, nexus.tree.get_newick_string() # create the baseml.phylip alignment file s_phylip = Phylip.get_alignment_string_non_interleaved(nexus.alignment) with open(Paml.baseml_phylip, 'wt') as fout: print >> fout, s_phylip # run PAML args = [Config.baseml_exe_path, Paml.baseml_ctl] from_paml, to_paml = popen2.popen4(args) return from_paml
def run_hky(tree, alignment): """ @param tree: a tree object @param alignment: an alignment object @return: messages from the program but not the results """ # create the baseml.ctl control file config = PamlConfig() config.set_hky() config.to_ctl_string() with open(baseml_ctl, 'wt') as fout: print >> fout, config.to_ctl_string() # create the nexus object that defines the tree and alignment nexus = Nexus.get_sample_nexus_object() # create the baseml.newick tree file with open(baseml_newick, 'wt') as fout: print >> fout, nexus.tree.get_newick_string() # create the baseml.phylip alignment file phylip_string = Phylip.get_alignment_string_non_interleaved( nexus.alignment) with open(baseml_phylip, 'wt') as fout: print >> fout, phylip_string # change the current directory to the data directory with Util.remember_cwd(): os.chdir(Config.data_path) # run PAML exe_path = Config.baseml_exe_path ctl_path = baseml_ctl #cmd = '%s %s > /dev/null' % (exe_path, ctl_path) #os.system(cmd) from_paml, to_paml = popen2.popen4([exe_path, ctl_path]) #p = subprocess.Popen([cmd, arg], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) # change back to the old directory return from_paml.read()
def run_hky(tree, alignment): """ @param tree: a tree object @param alignment: an alignment object @return: messages from the program but not the results """ # create the baseml.ctl control file config = PamlConfig() config.set_hky() config.to_ctl_string() with open(baseml_ctl, "wt") as fout: print >> fout, config.to_ctl_string() # create the nexus object that defines the tree and alignment nexus = Nexus.get_sample_nexus_object() # create the baseml.newick tree file with open(baseml_newick, "wt") as fout: print >> fout, nexus.tree.get_newick_string() # create the baseml.phylip alignment file phylip_string = Phylip.get_alignment_string_non_interleaved(nexus.alignment) with open(baseml_phylip, "wt") as fout: print >> fout, phylip_string # change the current directory to the data directory with Util.remember_cwd(): os.chdir(Config.data_path) # run PAML exe_path = Config.baseml_exe_path ctl_path = baseml_ctl # cmd = '%s %s > /dev/null' % (exe_path, ctl_path) # os.system(cmd) from_paml, to_paml = popen2.popen4([exe_path, ctl_path]) # p = subprocess.Popen([cmd, arg], stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True) # change back to the old directory return from_paml.read()
def get_form(): """ @return: the body of a form """ # define the default nexus string tree = get_sample_tree() mixture_model = get_sample_mixture_model() ncols = 200 seed = 314159 alignment = PhyLikelihood.simulate_alignment(tree, mixture_model, ncols, seed) nexus = Nexus.Nexus() nexus.tree = tree nexus.alignment = alignment nexus_string = str(nexus) # define the form objects form_objects = [ Form.MultiLine('nexus', 'nexus data', nexus_string), Form.Integer('ncategories', 'use this many categories', 3, low=1, high=5), Form.CheckGroup('options', 'output options', [ Form.CheckItem('outdebug', 'show debug info'), Form.CheckItem('outmodel', 'show the model'), Form.CheckItem('outcheck', 'show the likelihood and rates', True) ]) ] return form_objects
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) # get the mixture weights mixture_weights = [fs.weight_a, fs.weight_b] # get the kappa values kappa_values = [fs.kappa_a, fs.kappa_b] # get the nucleotide distributions nucleotide_distributions = [] for nt_string in (fs.frequency_a, fs.frequency_b): distribution = SnippetUtil.get_distribution(nt_string, 'nucleotide', list('ACGT')) nucleotide_distributions.append(distribution) # create the nucleotide HKY rate matrix objects rate_matrix_objects = [] for nt_distribution, kappa in zip(nucleotide_distributions, kappa_values): rate_matrix_object = RateMatrix.get_unscaled_hky85_rate_matrix( nt_distribution, kappa) 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] # create the mixture model mixture_model = SubModel.MixtureModel(mixture_proportions, rate_matrix_objects) # normalize the mixture model mixture_model.normalize() # return the results return do_analysis(mixture_model, nexus.alignment, nexus.tree) + '\n'
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) # run paml in a temp directory with Util.remember_cwd(): os.chdir(Config.data_path) from_paml = run_in_tmp(fs, nexus) # get the paml output paml_debug_output = from_paml.read() # get the paml output file with open(Paml.baseml_out) as fin: d = Paml.parse_hky_output(fin) out = StringIO() if fs.outdebug: print >> out, 'baseml stdout:' print >> out, '-----------------------------------------' print >> out, paml_debug_output.strip() print >> out, '-----------------------------------------' print >> out, '' print >> out, '' if fs.outmodel: print >> out, 'baseml control file:' print >> out, '-----------------------------------------' print >> out, ctl_string.strip() print >> out, '-----------------------------------------' print >> out, '' print >> out, '' print >> out, 'log likelihood :', d['lnL'] print >> out, '' print >> out, 'kappa :', d['kappa'] print >> out, '' for nt in list('ACGT'): print >> out, ('%s : ' % nt) + str(d[nt]) # return the results return out.getvalue()
def get_response(fs): """ @param fs: a FieldStorage object containing the cgi arguments @return: a (response_headers, response_text) pair """ # parse the tree try: tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() except Newick.NewickSyntaxError as e: raise HandlingError(str(e)) # get the normalized model mixture_model = deserialize_mixture_model(fs.model) # sample the alignment, possibly using a specified seed try: alignment = PhyLikelihood.simulate_alignment(tree, mixture_model, fs.ncols, fs.seed) except PhyLikelihood.SimulationError as e: raise HandlingError(e) # get the output string output_string = '' if fs.fastaformat: # the output is the alignment arr = [] for node in tree.gen_tips(): arr.append(alignment.get_fasta_sequence(node.name)) alignment_string = '\n'.join(arr) output_string = alignment_string elif fs.nexusformat: # the output is the alignment and the tree nexus = Nexus.Nexus() nexus.tree = tree nexus.alignment = alignment output_string = str(nexus) # print the results response_headers = [('Content-Type', 'text/plain')] return response_headers, output_string
def _readTreeFile(self): gm = ["TreeFileLite._readTreeFile()"] # Read in the trees try: f = file(self.fName, "U") except IOError: gm.append("Can't find tree file '%s'" % self.fName) raise Glitch, gm fLines = f.readlines() f.close() # We cant use doFastNextTok savedDoFastNextTok = var.nexus_doFastNextTok var.nexus_doFastNextTok = False # If it is not a nexus file, it must be a phylip file, so we # are done. lNum = 0 aLine = fLines[0].strip() if not aLine.startswith("#"): self.tLines = fLines return # So assume it is nexus. Get the 'header', which might be # useful. Its everything up to the first tree line. headerLines = [] lNum = 0 aLine = fLines[0] sLine = aLine.lstrip() lowLine = string.lower(sLine) while 1: if lowLine.startswith("tree"): break headerLines.append(aLine) lNum += 1 try: aLine = fLines[lNum] sLine = aLine.lstrip() lowLine = string.lower(sLine) except IndexError: headerLines = [] # something went wrong ... break if headerLines: self.header = ''.join(headerLines) # Get the translate command, if it exists translateLines = [] lNum = 0 aLine = fLines[0].strip() lowLine = string.lower(aLine) #print "a aLine: %s" % aLine try: while not lowLine.startswith("translate"): lNum += 1 aLine = fLines[lNum].strip() lowLine = string.lower(aLine) if lowLine.startswith('tree'): # then we have gone too far lNum = 0 aLine = fLines[0].strip() lowLine = string.lower(aLine) break except IndexError: # no translate line, so go back to the beginning lNum = 0 aLine = fLines[0].strip() lowLine = string.lower(aLine) #print "b lowLine: %s" % lowLine # If we got a translate line, then parse the translate command. assert lowLine if lowLine.startswith("translate"): lNum += 1 aLine = fLines[lNum].strip() while not aLine.endswith(";"): translateLines.append(aLine) lNum += 1 aLine = fLines[lNum].strip() translateLines.append(aLine) translateFlob = cStringIO.StringIO(' '.join(translateLines)) nx = Nexus.Nexus() self.translationHash = nx.readTranslateCommand(translateFlob) #print self.translationHash var.nexus_doFastNextTok = savedDoFastNextTok while not aLine.startswith("tree ") and not aLine.startswith("TREE "): lNum += 1 aLine = fLines[lNum].strip() # Get the tree lines. self.tLines = [] while 1: if aLine.startswith("tree ") or aLine.startswith("TREE "): tempLine = aLine # accommodate trees with line breaks. while aLine.find(";") < 0: lNum += 1 aLine = fLines[lNum].strip() tempLine += aLine self.tLines.append(tempLine[5:]) lNum += 1 aLine = fLines[lNum].strip() if aLine.startswith("end;") or aLine.startswith("End;") or aLine.startswith("ENDBLOCK;") or aLine.startswith('END'): break
def main(): nexus = Nexus.get_sample_nexus_object() run_hky(nexus.tree, nexus.alignment) with open(baseml_out) as fin: d = parse_hky_output(fin) print d
def nexus(self): print("Do Nexus Stuff...") Nexus.doNexus() print("Nexus return")
def _readP4Files(self): gm = ["PosteriorSamples._readP4Files()"] # Read in the trees fName = "mcmc_trees_%i.nex" % self.runNum if self.directory != '.': fName = os.path.join(self.directory, fName) try: f = file(fName) except IOError: gm.append("Can't find tree file '%s'" % fName) raise Glitch, gm fLines = f.readlines() f.close() # Get the translate command savedDoFastNextTok = var.nexus_doFastNextTok var.nexus_doFastNextTok = False lNum = 0 aLine = fLines[0].strip() translateLines = [] while not aLine.startswith("translate"): lNum += 1 aLine = fLines[lNum].strip() lNum += 1 aLine = fLines[lNum].strip() while not aLine.startswith(";"): translateLines.append(aLine) lNum += 1 aLine = fLines[lNum].strip() translateLines.append(aLine) translateFlob = cStringIO.StringIO(' '.join(translateLines)) nx = Nexus.Nexus() self.translationHash = nx.readTranslateCommand(translateFlob) #print self.translationHash var.nexus_doFastNextTok = savedDoFastNextTok # Get the models definition, if it exists. Move to the first tree line. while not aLine.startswith("tree t_"): if aLine.startswith("[&&p4 models"): self.modelLine = aLine lNum += 1 aLine = fLines[lNum].strip() # Get the tree lines. self.tLines = [] while not aLine.startswith("end;"): self.tLines.append(aLine[5:]) lNum += 1 aLine = fLines[lNum].strip() if self.tree.model.nFreePrams: # Read in the prams fName = "mcmc_prams_%i" % self.runNum if self.directory != '.': fName = os.path.join(self.directory, fName) try: f = file(fName) except IOError: gm.append("Can't find prams file '%s'" % fName) raise Glitch, gm fLines = f.readlines() f.close() lNum = 0 aLine = fLines[0].strip() while aLine[0] not in string.digits: lNum += 1 aLine = fLines[lNum].strip() self.pLines = [] while aLine: self.pLines.append(aLine) lNum += 1 try: aLine = fLines[lNum].strip() except IndexError: break # Read in the pramsProfile self.nPrams = None self.pramsProfile = None fName = "mcmc_pramsProfile.py" if self.directory != '.': fName = os.path.join(self.directory, fName) try: loc = {} execfile(fName, {}, loc) #loc =locals() no workee. #print "loc = %s" % loc self.nPrams = loc['nPrams'] self.pramsProfile = loc['pramsProfile'] except IOError: print "The file '%s' cannot be found." % fName
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 """ # parse the tree try: tree = Newick.parse(fs.tree, Newick.NewickTree) tree.assert_valid() except Newick.NewickSyntaxError as e: raise HandlingError(str(e)) # get the mixture weights mixture_weights = [fs.weight_a, fs.weight_b] # get the kappa values kappa_values = [fs.kappa_a, fs.kappa_b] # get the nucleotide distributions frequency_strings = (fs.frequency_a, fs.frequency_b) nucleotide_distributions = [] for nt_string in frequency_strings: d = SnippetUtil.get_distribution(nt_string, 'nucleotide', list('ACGT')) nucleotide_distributions.append(d) # create the nucleotide HKY rate matrix objects rate_matrix_objects = [] for nt_distribution, kappa in zip(nucleotide_distributions, kappa_values): rate_matrix_object = RateMatrix.get_unscaled_hky85_rate_matrix( nt_distribution, kappa) 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] # create the mixture model mixture_model = SubModel.MixtureModel(mixture_proportions, rate_matrix_objects) # normalize the mixture model mixture_model.normalize() # simulate the alignment try: alignment = PhyLikelihood.simulate_alignment(tree, mixture_model, fs.ncols) except PhyLikelihood.SimulationError as e: raise HandlingError(e) # get the output string output_string = '' if fs.fasta: # the output is the alignment arr = [] for node in tree.gen_tips(): arr.append(alignment.get_fasta_sequence(node.name)) alignment_string = '\n'.join(arr) output_string = alignment_string elif fs.nex: # the output is the alignment and the tree nexus = Nexus.Nexus() nexus.tree = tree nexus.alignment = alignment for i in range(2): arr = [] arr.append('weight: %s' % mixture_weights[i]) arr.append('kappa: %s' % kappa_values[i]) nexus.add_comment('category %d: %s' % (i + 1, ', '.join(arr))) output_string = str(nexus) # define the filename if fs.fasta: filename_extension = 'fasta' elif fs.nex: filename_extension = 'nex' filename = 'sample.' + fs.fmt #TODO use the correct filename extension in the output return output_string
def _readMrBayesFiles(self): gm = ["PosteriorSamples._readMrBayesFiles()"] # Read in the trees fName = "%s.run%i.t" % (self.mbBaseName, self.runNum) if self.directory != '.': fName = os.path.join(self.directory, fName) try: f = file(fName) except IOError: gm.append("Can't find tree file '%s'" % fName) raise Glitch, gm fLines = f.readlines() f.close() # Get the translate command savedDoFastNextTok = var.nexus_doFastNextTok var.nexus_doFastNextTok = False lNum = 0 aLine = fLines[0].strip() translateLines = [] while not aLine.startswith("translate"): lNum += 1 aLine = fLines[lNum].strip() lNum += 1 aLine = fLines[lNum].strip() while not aLine.endswith(";"): translateLines.append(aLine) lNum += 1 aLine = fLines[lNum].strip() translateLines.append(aLine) translateFlob = cStringIO.StringIO(' '.join(translateLines)) nx = Nexus.Nexus() self.translationHash = nx.readTranslateCommand(translateFlob) #print self.translationHash var.nexus_doFastNextTok = savedDoFastNextTok # Get the models definition, if it exists. Move to the first tree line. # MrBayes3.2 uses 'gen', 3.1.2 uses 'rep'. while not aLine.startswith("tree gen.") and not aLine.startswith( "tree rep."): lNum += 1 aLine = fLines[lNum].strip() # Get the tree lines. self.tLines = [] while not aLine.startswith("end;"): self.tLines.append(aLine[5:]) lNum += 1 aLine = fLines[lNum].strip() # Read in the prams fName = "%s.run%i.p" % (self.mbBaseName, self.runNum) if self.directory != '.': fName = os.path.join(self.directory, fName) try: f = file(fName) except IOError: gm.append("Can't find prams file '%s'" % fName) raise Glitch, gm fLines = f.readlines() f.close() # Get the header line, starts with Gen lNum = 1 aLine = fLines[lNum].strip() self.pramsHeader = aLine.split() if self.verbose >= 2: print "pramsHeader: %s" % self.pramsHeader assert self.pramsHeader[0] == 'Gen' # Collect pram lines lNum += 1 aLine = fLines[lNum].strip() self.pLines = [] while aLine: self.pLines.append(aLine) lNum += 1 try: aLine = fLines[lNum].strip() except IndexError: break #print self.pLines self.nPrams = len(self.pramsHeader) if self.verbose >= 2: print "pram line length is %i" % self.nPrams