Example #1
0
    def run_rosetta(self, movie, args):
        """
        Main method that executes the rosetta simulation

        @param movie: simulation object
        @type movie: L{Movie}

        @param args: list of simulation arguments
        @type args: list

        @note: This method needs to be refactored very badly
        """
        self._movie = movie
        assert args >= 1
        #we have set it up such that the first element in arg[0] is number of simulations
        self.numSim = args[0][0]
        #set the program path, database path and write the paths.txt in here
        #we have set it up such that the third argument in args[0] always have
        # the name of the protein we are running rosetta simulation for
        #also we say that an error has occurred if we cannot write the resfile.
        #not sure if this should be the case
        self.errcode = self.set_options_errQ( args[0][2])
        if self.errcode: # used to be a local var 'r'
            return
        #get the starting pdb structure for rosetta simulation
        self.sim_input_file = self.sim_input_filename(args[0][2])
        if self.sim_input_file is None:
            return
        #this marks the beginning of the simulation. Although technically we are yet
        # to call QProcess, it seems like a good place to set the waitcursor to True
        self.set_waitcursor(True)
        progressBar = self.win.statusBar().progressBar
        # Disable some QActions (menu items/toolbar buttons) while the sim is running.
        self.win.disable_QActions_for_sim(True)

        try:
            self.simProcess = None
            #sets up the argument list for running rosetta including the ones
            #that were provided in the pop up dialog
            backRubArgs = []
            if len(args) == 3:
                backRubArgs = args[2]
            self.setup_sim_args(args[0][1], backRubArgs)
            progressBar.setRange(0, 0)
            progressBar.reset()
            progressBar.show()
            env.history.statusbar_msg("Running Rosetta on " + self.sim_input_file[0:len(self.sim_input_file) - 4])
            #this is used to name all the files related to this simulation
            #we make sure that the pdb id is there in the filename so that it is
            #easy to identify for which protein chunk we are running the simulation
            rosettaFullBaseFileName = self.tmp_file_prefix
            rosettaFullBaseFileInfo = QFileInfo(rosettaFullBaseFileName)
            rosettaWorkingDir = rosettaFullBaseFileInfo.dir().absolutePath()
            rosettaBaseFileName = rosettaFullBaseFileInfo.fileName()

            rosettaProcess = Process()
            rosettaProcess.setProcessName("rosetta")
            rosettaProcess.redirect_stdout_to_file("%s-rosetta-stdout.txt" %
                rosettaFullBaseFileName)
            rosettaProcess.redirect_stderr_to_file("%s-rosetta-stderr.txt" %
                rosettaFullBaseFileName)
            rosettaStdOut = rosettaFullBaseFileName + "-rosetta-stdout.txt"
            #rosetta files are all put in RosettaDesignFiles under Nanorex
            rosettaProcess.setWorkingDirectory(rosettaWorkingDir)
            environmentVariables = rosettaProcess.environment()
            rosettaProcess.setEnvironment(environmentVariables)
            msg = greenmsg("Starting Rosetta sequence design")
            env.history.message(self.cmdname + ": " + msg)
            env.history.message("%s: Rosetta files at %s%s%s.*" %
                (self.cmdname, rosettaWorkingDir, os.sep,
                 rosettaFullBaseFileInfo.completeBaseName()))

            abortHandler = AbortHandler(self.win.statusBar(), "rosetta")
            #main rosetta simulation call
            errorCode = rosettaProcess.run(self.program, self._arguments, False, abortHandler)
            abortHandler = None
            if (errorCode != 0):
                if errorCode == -2: # User pressed Abort button in progress dialog.
                    msg = redmsg("Aborted.")
                    env.history.message(self.cmdname + ": " + msg)
                    env.history.statusbar_msg("")
                    if self.simProcess:
                        self.simProcess.kill()
                else:
                    #the stdout will tell the user for what other reason,
                    #the simulation may fail
                    msg = redmsg("Rosetta sequence design failed. For details check" + rosettaStdOut)
                    env.history.message(self.cmdname + ": " + msg)
                    self.errcode = 2;
                    env.history.statusbar_msg("")
            else:
                #Error code is not zero but there's in reality error in stdout
                #check if that be the case
                env.history.statusbar_msg("")
                errorInStdOut = self.checkErrorInStdOut(rosettaStdOut)
                if errorInStdOut:
                    msg = redmsg("Rosetta sequence design failed, Rosetta returned %d" % errorCode)
                    env.history.message(self.cmdname + "," + self.cmd_type + ": " + msg)
                    env.history.statusbar_msg("")
                else:
                    #bug in rosetta: often for some reason or the other rosetta
                    #run does not produce an o/p file. One instance is that if
                    # you already have an output file for this starting structure
                    #already in the directory rosetta refuses to optimize the
                    #structue again even if your residue file has changed
                    #since we remove all related output files before any run on
                    #the same protein, this is not a possible source of error
                    #in our case but there can be other similar problems
                    #Hence we always check the desired output file actually exists
                    #in the RosettaDesignFiles directory before we actually declare
                    #that it has been a successful run
                    if self.cmd_type == "ROSETTA_FIXED_BACKBONE_SEQUENCE_DESIGN":
                        outputFile = self.outfile + '_0001.pdb'
                        outPath = os.path.join(os.path.dirname(self.tmp_file_prefix), outputFile)
                        if os.path.exists(outPath):
                            #if there's the o/p pdb file, then rosetta design "really"
                            #succeeded
                            msg = greenmsg("Rosetta sequence design succeeded")
                            env.history.message(self.cmdname + "> " + self.cmd_type + ": " + msg)
                            #find out best score from all the generated outputs
                            #may be we will do it some day, but for now we only output
                            #the chunk with the lowest energy (Score)
                            score, bestSimOutFileName = getScoreFromOutputFile(self.tmp_file_prefix, self.outfile, self.numSim)
                            chosenOutPath = os.path.join(os.path.dirname(self.tmp_file_prefix), bestSimOutFileName)
                            insertpdb(self.assy, str(chosenOutPath), None)
                            #set the secondary structure of the rosetta output protein
                            #to that of the inpput protein
                            outProtein = self._set_secondary_structure_of_rosetta_output_protein(bestSimOutFileName)
                            #update the protein combo box in build protein mode with
                            #newly created protein chunk
                            self._updateProteinComboBoxInBuildProteinMode(outProtein)
                            env.history.statusbar_msg("")
                            fastaFile = self.outfile + "_design.fasta"
                            fastaFilePath = os.path.join(os.path.dirname(self.tmp_file_prefix), fastaFile)
                            #process th fasta file to find the sequence of the protein
                            #with lowest score
                            proteinSeqList = processFastaFile(fastaFilePath, bestSimOutFileName, self.sim_input_file[0:len(self.sim_input_file)-4])
                            #show a pop up dialog to show the best score and most
                            #optimized sequence
                            if score is not None and proteinSeqList is not []:
                                self.showResults(score, proteinSeqList)
                        else:
                            #even when there's nothing in stderr or errocode is zero,
                            #rosetta may not output anything.
                            msg1 = redmsg("Rosetta sequence design failed. ")
                            msg2 = redmsg(" %s file was never created by Rosetta." % outputFile)
                            msg = msg1 + msg2
                            env.history.message(self.cmdname + ": " + msg)
                            env.history.statusbar_msg("")

                    if self.cmd_type == "BACKRUB_PROTEIN_SEQUENCE_DESIGN":
                        #its important to set thi pref key to False so that if the
                        #subsequent rosetta run is with fixed backbone then the
                        #resfile is correctly written
                        from utilities.prefs_constants import rosetta_backrub_enabled_prefs_key
                        env.prefs[rosetta_backrub_enabled_prefs_key] = False
                        #Urmi 20080807: first copy the backrub_low.pdb to a new pdb
                        #file with the pdb info also added there
                        outProteinName, outPath = createUniquePDBOutput(self.tmp_file_prefix, self.sim_input_file[0:len(self.sim_input_file)-4], self.win)
                        if outProteinName is None:
                            msg1 = redmsg("Rosetta sequence design with backrub motion has failed. ")
                            msg2 = redmsg(" backrub_low.pdb was never created by Rosetta.")
                            msg = msg1 + msg2
                            env.history.message(self.cmdname + "," + self.cmd_type + ": " + msg)
                            env.history.statusbar_msg("")
                        else:
                            env.history.statusbar_msg("")
                            msg = greenmsg("Rosetta sequence design with backrub motion allowed, succeeded")
                            env.history.message(self.cmdname + "> " + self.cmd_type + ": " + msg)
                            insertpdb(self.assy, str(outPath), None)
                            outProtein = self._set_secondary_structure_of_rosetta_output_protein(outProteinName + ".pdb")
                            self._updateProteinComboBoxInBuildProteinMode(outProtein)
                            inProteinName = self.sim_input_file[0:len(self.sim_input_file)-4]
                            proteinSeqList = getProteinNameAndSeq(inProteinName, outProteinName, self.win)
                            score = getScoreFromBackrubOutFile(outPath)
                            if score is not None and proteinSeqList is not []:
                                self.showResults(score, proteinSeqList)

                    if self.cmd_type == "ROSETTA_SCORE":
                        msg = greenmsg("Rosetta scoring has succeeded")
                        env.history.message(self.cmdname + "> " + self.cmd_type + ": " + msg)
                        showRosettaScore(self.tmp_file_prefix, self.scorefile, self.win)
        except:
            print_compact_traceback("bug in simulator-calling code: ")
            self.errcode = -11111
        self.set_waitcursor(False)
        self.win.disable_QActions_for_sim(False)
        env.history.statusbar_msg("")
        if not self.errcode:
            return # success
        return # caller should look at self.errcode
    def run_using_old_movie_obj_to_hold_sim_params(self, movie, args):
        self._movie = movie 
        assert args >= 1
        #for now args has number of simulations
        self.numSim = args[0][0]
        #set the program path, database path and write the paths.txt in here
        self.errcode = self.set_options_errQ( )
        if self.errcode: # used to be a local var 'r'
            return
        self.sim_input_file = self.sim_input_filename(self.part)
        if self.sim_input_file is None:
            return
           
        self.set_waitcursor(True)
        progressBar = self.win.statusBar().progressBar
        
        # Disable some QActions (menu items/toolbar buttons) while the sim is running.
        self.win.disable_QActions_for_sim(True)

        try: #bruce 050325 added this try/except wrapper, to always restore cursor
            self.simProcess = None #bruce 051231
            self.setup_sim_args(args)
            progressBar.setRange(0, 0)
            progressBar.reset()
            progressBar.show()
            env.history.statusbar_msg("Running Rosetta")
            
            rosettaFullBaseFileName = self.tmp_file_prefix 
            rosettaFullBaseFileInfo = QFileInfo(rosettaFullBaseFileName)
            rosettaWorkingDir = rosettaFullBaseFileInfo.dir().absolutePath()
            rosettaBaseFileName = rosettaFullBaseFileInfo.fileName()
            
            rosettaProcess = Process()
            rosettaProcess.setProcessName("rosetta")
            rosettaProcess.redirect_stdout_to_file("%s-rosetta-stdout.txt" %
                rosettaFullBaseFileName)
            rosettaProcess.redirect_stderr_to_file("%s-rosetta-stderr.txt" %
                rosettaFullBaseFileName)
            rosettaStdOut = rosettaFullBaseFileName + "-rosetta-stdout.txt"
            rosettaProcess.setWorkingDirectory(rosettaWorkingDir)
            environmentVariables = rosettaProcess.environment()
            rosettaProcess.setEnvironment(environmentVariables)
            msg = greenmsg("Starting Rosetta sequence design")
            env.history.message(self.cmdname + ": " + msg)
            env.history.message("%s: Rosetta files at %s%s%s.*" %
                (self.cmdname, rosettaWorkingDir, os.sep,
                 rosettaFullBaseFileInfo.completeBaseName()))
            
            abortHandler = AbortHandler(self.win.statusBar(), "rosetta")
            errorCode = rosettaProcess.run(self.program, self._arguments, False, abortHandler)
            
            abortHandler = None
            if (errorCode != 0):
                if errorCode == -2: # User pressed Abort button in progress dialog.
                    msg = redmsg("Aborted.")
                    env.history.message(self.cmdname + ": " + msg)
                    env.history.statusbar_msg("")
                    if self.simProcess: #bruce 051231 added condition (since won't be there when use_dylib)
                        self.simProcess.kill()
                else: 
                    msg = redmsg("Rosetta sequence design failed. For details check" + rosettaStdOut)
                    env.history.message(self.cmdname + ": " + msg)
                    self.errcode = 2;
                    env.history.statusbar_msg("")
            else:
                #run has been successful
                #open pdb file
                env.history.statusbar_msg("")
                errorInStdOut = self.checkErrorInStdOut(rosettaStdOut)
                if errorInStdOut:
                    msg = redmsg("Rosetta sequence design failed, Rosetta returned %d" % errorCode)
                    env.history.message(self.cmdname + ": " + msg)
                    env.history.statusbar_msg("")
                else:    
                    #env.history.message(self.cmdname + ": " + msg)
                    outputFile = self.outfile + '_0001.pdb'
                    outPath = os.path.join(os.path.dirname(self.tmp_file_prefix), outputFile)
                    if os.path.exists(outPath):
                        msg = greenmsg("Rosetta sequence design succeeded")
                        env.history.message(self.cmdname + ": " + msg)
                        #find out best score from all the generated outputs
                        score, bestSimOutFileName = getScoreFromOutputFile(self.tmp_file_prefix, self.outfile, self.numSim)
                        chosenOutPath = os.path.join(os.path.dirname(self.tmp_file_prefix), bestSimOutFileName)
                        insertpdb(self.assy, str(chosenOutPath), None)
                        env.history.statusbar_msg("")
                        fastaFile = self.outfile + "_design.fasta" 
                        fastaFilePath = os.path.join(os.path.dirname(self.tmp_file_prefix), fastaFile)
                        proteinSeqList = processFastaFile(fastaFilePath, bestSimOutFileName, self.sim_input_file[0:len(self.sim_input_file)-4])
                        #score = getScoreFromOutputFile(outPath)
                        if score is not None and proteinSeqList is not []:
                            self.showResults(score, proteinSeqList)
                        
                    else:
                        msg1 = redmsg("Rosetta sequence design failed. ")
                        msg2 = redmsg(" %s file was never created by Rosetta." % outputFile)
                        msg = msg1 + msg2
                        env.history.message(self.cmdname + ": " + msg)
                        env.history.statusbar_msg("")
        
        except:
            print_compact_traceback("bug in simulator-calling code: ")
            self.errcode = -11111
        self.set_waitcursor(False)
        self.win.disable_QActions_for_sim(False)
        env.history.statusbar_msg("")
        if not self.errcode:
            return # success
        
        return # caller should look at self.errcode