def setupInitialModel(self):
        modeldata = apModel.getModelFromId(self.params["modelid"])
        modelfile = os.path.join(modeldata["path"]["path"], modeldata["name"])
        modelsym = modeldata["symmetry"]["symmetry"]

        apDisplay.printMsg("Current symmetry: %s" % (modeldata["symmetry"]["description"]))

        # if icosahedral recon, rotate volume to 3DEM standard orientation
        if modelsym.startswith("Icos (5 3 2)"):
            tempfile = "temp.mrc"
            emancmd = "proc3d %s %s icos5fTo2f" % (modelfile, tempfile)
            apEMAN.executeEmanCmd(emancmd, verbose=True)
            modelfile = tempfile

        if modelsym.startswith("Icos (5 3 2)") or modelsym.startswith("Icos (2 5 3)"):
            tempfile = "temp.mrc"
            emancmd = "proc3d %s %s rotspin=0,0,1,90" % (modelfile, tempfile)
            apEMAN.executeEmanCmd(emancmd, verbose=True)
            modelfile = tempfile

        outmodelfile = os.path.join(self.params["rundir"], "initmodel.hed")
        apFile.removeStack(outmodelfile, warn=False)
        emancmd = "proc3d %s %s clip=%d,%d,%d " % (modelfile, outmodelfile, self.boxsize, self.boxsize, self.boxsize)

        # rescale initial model if necessary
        scale = modeldata["pixelsize"] / self.apix
        if abs(scale - 1.0) > 1e-3:
            emancmd += "scale=%.4f " % (scale)

        apEMAN.executeEmanCmd(emancmd, verbose=True)
        apImagicFile.setImagic4DHeader(outmodelfile, machineonly=True)

        return outmodelfile
	def setupInitialModel(self):
		modeldata = apModel.getModelFromId(self.params['modelid'])
		modelfile = os.path.join(modeldata['path']['path'], modeldata['name'])
		modelsym = modeldata['symmetry']['symmetry']

		apDisplay.printMsg("Current symmetry: %s"%(modeldata['symmetry']['description']))

		# if icosahedral recon, rotate volume to 3DEM standard orientation
		if modelsym.startswith('Icos (5 3 2)'):
			tempfile = "temp.mrc"
			emancmd = 'proc3d %s %s icos5fTo2f' % (modelfile, tempfile)
			apEMAN.executeEmanCmd(emancmd, verbose=True)
			modelfile = tempfile

		if modelsym.startswith('Icos (5 3 2)') or modelsym.startswith('Icos (2 5 3)'):
			tempfile = "temp.mrc"
			emancmd = 'proc3d %s %s rotspin=0,0,1,90' % (modelfile, tempfile)
			apEMAN.executeEmanCmd(emancmd, verbose=True)
			modelfile = tempfile

		outmodelfile = os.path.join(self.params['rundir'], "initmodel.hed")
		apFile.removeStack(outmodelfile, warn=False)
		emancmd = "proc3d %s %s clip=%d,%d,%d " % (modelfile, outmodelfile, self.boxsize, self.boxsize, self.boxsize)

		# rescale initial model if necessary
		scale = modeldata['pixelsize']/self.apix
		if abs(scale - 1.0) > 1e-3:
 			emancmd += "scale=%.4f "%(scale)

		apEMAN.executeEmanCmd(emancmd, verbose=True)
		apImagicFile.setImagic4DHeader(outmodelfile, machineonly=True)

		return outmodelfile
    def start(self):
        self.iflag = 1
        self.setIBLOW()

        ### get stack info
        self.stackdata = apStack.getOnlyStackData(self.params['stackid'])
        self.refinestackfile = os.path.join(self.stackdata['path']['path'],
                                            self.stackdata['name'])
        apImagicFile.setImagic4DHeader(self.refinestackfile)
        ### copy stack or start job file
        if self.params['cluster'] is False:
            #create alias to stack data
            pass
        if self.params['cluster'] is True:
            self.setupMultiNode()

        if self.params['reconstackid'] is not None:
            self.reconstackdata = apStack.getOnlyStackData(
                self.params['stackid'])
            self.reconstackfile = os.path.join(self.stackdata['path']['path'],
                                               self.stackdata['name'])
        else:
            self.reconstackfile = self.refinestackfile

        ### create initial model file
        self.currentvol = os.path.basename(self.setupInitialModel())

        ### create parameter file
        self.currentparam = os.path.basename(self.setupParticleParams())
        apDisplay.printColor(
            "Initial files:\n Stack: %s\n Volume: %s\n Params: %s\n" %
            (os.path.basename(
                self.refinestackfile), self.currentvol, self.currentparam),
            "violet")

        ## run frealign for number for refinement cycles
        for i in range(self.params['numiter']):
            iternum = i + 1
            if self.params['cluster'] is True:
                self.multiNodeRun(iternum)
            else:
                self.singleNodeRun(iternum)
                time.sleep(2)

            ### calculate FSC
            #emancmd = 'proc3d %s %s fsc=fsc.eotest.%d' % (evenvol, oddvol, self.params['iter'])
            #apEMAN.executeEmanCmd(emancmd, verbose=True)

        if self.params['cluster'] is True:
            self.prepareForCluster()

        print "Done!"
    def start(self):
        self.iflag = 1
        self.setIBLOW()

        ### get stack info
        self.stackdata = apStack.getOnlyStackData(self.params["stackid"])
        self.refinestackfile = os.path.join(self.stackdata["path"]["path"], self.stackdata["name"])
        apImagicFile.setImagic4DHeader(self.refinestackfile)
        ### copy stack or start job file
        if self.params["cluster"] is False:
            # create alias to stack data
            pass
        if self.params["cluster"] is True:
            self.setupMultiNode()

        if self.params["reconstackid"] is not None:
            self.reconstackdata = apStack.getOnlyStackData(self.params["stackid"])
            self.reconstackfile = os.path.join(self.stackdata["path"]["path"], self.stackdata["name"])
        else:
            self.reconstackfile = self.refinestackfile

        ### create initial model file
        self.currentvol = os.path.basename(self.setupInitialModel())

        ### create parameter file
        self.currentparam = os.path.basename(self.setupParticleParams())
        apDisplay.printColor(
            "Initial files:\n Stack: %s\n Volume: %s\n Params: %s\n"
            % (os.path.basename(self.refinestackfile), self.currentvol, self.currentparam),
            "violet",
        )

        ## run frealign for number for refinement cycles
        for i in range(self.params["numiter"]):
            iternum = i + 1
            if self.params["cluster"] is True:
                self.multiNodeRun(iternum)
            else:
                self.singleNodeRun(iternum)
                time.sleep(2)

            ### calculate FSC
            # emancmd = 'proc3d %s %s fsc=fsc.eotest.%d' % (evenvol, oddvol, self.params['iter'])
            # apEMAN.executeEmanCmd(emancmd, verbose=True)

        if self.params["cluster"] is True:
            self.prepareForCluster()

        print "Done!"
        def start(self):
                t0 = time.time()

                self.checkAnalysisRun()

                # get stack parameters
                if self.params['alignid'] is not None:
                        self.alignstackdata = appiondata.ApAlignStackData.direct_query(self.params['alignid'])
                        stackpixelsize = self.alignstackdata['pixelsize']
                        stack_box_size = self.alignstackdata['boxsize']
                        self.params['boxsize'] = stack_box_size / int(self.params['bin'])
                        self.params['apix'] = stackpixelsize * int(self.params['bin'])
                        orig_path = self.alignstackdata['path']['path']
                        orig_file = self.alignstackdata['imagicfile']
                        linkingfile = orig_path+"/"+orig_file
                        linkingfile = linkingfile.replace(".hed", "")
                else:
                        apDisplay.printError("stack not in the database")

                # link stack file to working directory
                if not os.path.isfile(linkingfile+".hed"):
                        apDisplay.printError("stackfile does not exist: "+linkingfile+".img")
                else:
                        if not os.path.isfile(os.path.join(str(self.params['rundir']), "start.img")):
                                apDisplay.printMsg("copying aligned stack into working directory for operations with IMAGIC")
#                               shutil.copyfile(linkingfile+".img", str(self.params['rundir'])+"/start.img")
#                               shutil.copyfile(linkingfile+".hed", str(self.params['rundir'])+"/start.hed")
                                lnkcmd1 = "ln -s "+linkingfile+".img "+os.path.join(self.params['rundir'], "start.img")
                                lnkcmd2 = "ln -s "+linkingfile+".hed "+os.path.join(self.params['rundir'], "start.hed")
                                proc = subprocess.Popen(lnkcmd1, shell=True)
                                proc.wait()
                                proc = subprocess.Popen(lnkcmd2, shell=True)
                                proc.wait()
                                ### header bug
                                apImagicFile.setImagic4DHeader(os.path.join(self.params['rundir'], "start.hed"))
                        else:
                                apDisplay.printColor("aligned stack already exists in working directory", "green")

                ### NEED TO CONVERT FILTERING PARAMETERS TO IMAGIC FORMAT BETWEEN 0-1
                if self.params['lpfilt'] is not None:
                        self.params['lpfilt_imagic'] = 2 * float(self.params['apix']) / int(self.params['lpfilt'])
                else:
                        self.params['lpfilt_imagic'] = False
                if float(self.params['lpfilt_imagic']) > 1:
                        self.params['lpfilt_imagic'] = 1        # imagic cannot perform job when lowpass > 1
                if self.params['hpfilt'] is not None:
                        self.params['hpfilt_imagic'] = 2 * float(self.params['apix']) / int(self.params['hpfilt'])
                else:
                        self.params['hpfilt_imagic'] = False

                print self.params
                print "... aligned stack pixel size: "+str(self.params['apix'])
                print "... aligned stack box size: "+str(self.params['boxsize'])
                apDisplay.printColor("Running IMAGIC .batch file: See imagicMultivariateStatisticalAnalysis.log for details", "cyan")

                ### create imagic batch file
                filename = self.createImagicBatchFile()
                ### execute batch file that was created
                aligntime = time.time()
                proc = subprocess.Popen('chmod 775 '+filename, shell=True)
                proc.wait()
                apParam.runCmd(filename, package="IMAGIC")
                logfile = open(os.path.join(self.params['rundir'], "imagicMultivariateStatisticalAnalysis.log"))
                apIMAGIC.checkLogFileForErrors(os.path.join(self.params['rundir'], "imagicMultivariateStatisticalAnalysis.log"))
                if not os.path.isfile(os.path.join(self.params['rundir'], "eigenimages.hed")):
                        apDisplay.printError("IMAGIC did not run and did not create eigenimages")
                aligntime = time.time() - aligntime

                ### remove copied stack
#               while os.path.isfile(os.path.join(self.params['rundir'], "start.img")):
#               apFile.removeStack(os.path.join(self.params['rundir'], "start.img"))

                ### normalize eigenimages
                eigenimages = os.path.join(self.params['rundir'], "eigenimages.img")
                emancmd = "proc2d "+str(eigenimages)+" "+str(eigenimages)+" inplace"
                apParam.runCmd(emancmd, package="EMAN")

                ### upload alignment
                imagicstack = os.path.join(self.params['rundir'], "start.hed")
                inserttime = time.time()
                if self.params['commit'] is True:
                        self.insertAnalysis(imagicstack, runtime=aligntime, insert=True)
                else:
                        apDisplay.printWarning("not committing results to DB")
                inserttime = time.time() - inserttime

                apDisplay.printMsg("Alignment time: "+apDisplay.timeString(aligntime))
                apDisplay.printMsg("Database Insertion time: "+apDisplay.timeString(inserttime))