Beispiel #1
0
    def resolution(self, embeddingData, image1, image2, apix):

        np.random.seed(3)

        self.apix = float(apix)

        #****************************
        #**** do two embeddings *****
        #****************************
        if embeddingData is not None:

            #split the localizations randomly in 2 half sets
            numLocalizations = embeddingData.shape[0]
            self.dimension = embeddingData.shape[1]
            sizeHalfSet = int(numLocalizations / 2)
            permutedSequence = np.random.permutation(
                np.arange(numLocalizations))
            self.embeddingsHalf1 = embeddingData[
                permutedSequence[0:sizeHalfSet], :]
            self.embeddingsHalf2 = embeddingData[
                permutedSequence[sizeHalfSet:], :]
            self.embedding = embeddingData
            self.make_half_maps()

        #****************************
        #***** use two images *******
        #****************************
        elif image1 is not None:

            self.halfMap1 = image1
            self.halfMap2 = image2

        self.hannWindow = FDRutil.makeHannWindow(self.halfMap1)
        maskData = self.hannWindow

        self.fullMap = self.halfMap1 + self.halfMap2
        self.frequencyMap = FSCutil.calculate_frequency_map(self.halfMap1)

        tmpResVec, FSC, _, _, qVals_FDR, resolution_FDR, _ = FSCutil.FSC(
            self.halfMap1, self.halfMap2, maskData, self.apix, 0.143, 1, False,
            True, None, True)

        self.resolution = resolution_FDR
        self.FSCdata = FSC
        #self.calcTTest();
        self.qVals = qVals_FDR
        self.resVec = tmpResVec
        self.filterMap()
        self.writeFSC()
def loopOverMap(iSeq, queue, paddedMask, paddedHalfMap1, paddedHalfMap2,
                boxSize, sizeMap, stepSize, halfBoxSize, hannWindow, apix,
                cutoff, numAsymUnits, permutedCorCoeffs):

    # ********************************************
    # ******* iterate over the map and calc ******
    # ************ local resolutions *************
    # ********************************************

    locRes = np.zeros((len(range(boxSize, boxSize + sizeMap[0], stepSize)),
                       len(range(boxSize, boxSize + sizeMap[1], stepSize)),
                       len(range(boxSize, boxSize + sizeMap[2], stepSize))))

    for i in iSeq:

        iInd = int((i - boxSize) / stepSize)
        jInd = 0

        for j in range(boxSize, boxSize + sizeMap[1], stepSize):

            kInd = 0

            for k in range(boxSize, boxSize + sizeMap[2], stepSize):

                if paddedMask[i, j, k] > 0.99:

                    window_halfmap1 = paddedHalfMap1[i - halfBoxSize:i -
                                                     halfBoxSize + boxSize,
                                                     j - halfBoxSize:j -
                                                     halfBoxSize + boxSize,
                                                     k - halfBoxSize:k -
                                                     halfBoxSize + boxSize]
                    window_halfmap2 = paddedHalfMap2[i - halfBoxSize:i -
                                                     halfBoxSize + boxSize,
                                                     j - halfBoxSize:j -
                                                     halfBoxSize + boxSize,
                                                     k - halfBoxSize:k -
                                                     halfBoxSize + boxSize]

                    # apply hann window
                    window_halfmap1 = window_halfmap1 * hannWindow
                    window_halfmap2 = window_halfmap2 * hannWindow

                    _, _, _, _, _, tmpRes, _ = FSCutil.FSC(
                        window_halfmap1, window_halfmap2, None, apix, cutoff,
                        numAsymUnits, True, False, permutedCorCoeffs, False)

                    locRes[iInd, jInd, kInd] = tmpRes

                else:
                    locRes[iInd, jInd, kInd] = 0.0

                kInd = kInd + 1

            jInd = jInd + 1

    #push back the local resolution map to the list
    queue.put(locRes)
Beispiel #3
0
    def localResolution(self, embeddingData, image1, image2, apix, stepSize,
                        boxSize, lowRes):

        np.random.seed(3)

        self.apix = float(apix)
        #****************************
        #**** do two embeddings *****
        #****************************
        if embeddingData is not None:

            #split the localizations randomly in 2 half sets
            numLocalizations = embeddingData.shape[0]
            self.dimension = embeddingData.shape[1]
            sizeHalfSet = int(numLocalizations / 2)
            permutedSequence = np.random.permutation(
                np.arange(numLocalizations))
            self.embeddingsHalf1 = embeddingData[
                permutedSequence[0:sizeHalfSet], :]
            self.embeddingsHalf2 = embeddingData[
                permutedSequence[sizeHalfSet:], :]
            self.embedding = embeddingData
            self.make_half_maps()

        #****************************
        #***** use two images *******
        #****************************
        elif image1 is not None:

            self.halfMap1 = image1
            self.halfMap2 = image2

        self.halfMap1 = self.halfMap1 + np.random.randn(
            self.halfMap1.shape[0], self.halfMap1.shape[1]) * 0.01
        self.halfMap2 = self.halfMap2 + np.random.randn(
            self.halfMap2.shape[0], self.halfMap1.shape[1]) * 0.01

        #maskData = FSCutil.makeCircularMask(self.halfMap1, (np.min(self.halfMap1.shape) / 2.0) - 4.0);  # circular mask
        maskData = np.ones(self.halfMap1.shape)

        self.fullMap = self.halfMap1 + self.halfMap2
        self.frequencyMap = FSCutil.calculate_frequency_map(self.halfMap1)

        #estimate the local resolutions
        self.localResolutions = localResolutions2D.localResolutions2D(
            self.halfMap1, self.halfMap2, boxSize, stepSize, 0.5, self.apix, 1,
            maskData, maskData, lowRes)

        #do local filtering
        self.filteredMap, _, _, _ = mapUtil.localFiltration(
            self.fullMap, self.localResolutions, self.apix, False, None, None,
            None)
        self.filteredMap[self.filteredMap < 0.0] = 0.0
Beispiel #4
0
	def runLocalFSC(self):

		#show message box before starting
		msg = QMessageBox();
		msg.setIcon(QMessageBox.Information);
		msg.setText("Start the job with OK!")
		msg.setInformativeText("GUI will be locked until the job is finished. See terminal printouts for progress ...");
		msg.setWindowTitle("Start job");
		msg.setStandardButtons( QMessageBox.Cancel| QMessageBox.Ok);
		result = msg.exec_();

		if result == QMessageBox.Cancel:
			return;


		start = time.time();

		print('***************************************************');
		print('******* Significance analysis of FSC curves *******');
		print('***************************************************');


		#read the half maps
		try:
			half_map1 = mrcfile.open(self.fileLine_halfMap1.text(), mode='r');
			half_map2 = mrcfile.open(self.fileLine_halfMap2.text(), mode='r');
		except:
			msg = QMessageBox();
			msg.setIcon(QMessageBox.Information);
			msg.setText("Cannot read file ...");
			msg.setWindowTitle("Error");
			msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
			retval = msg.exec_();
			return;

		halfMap1Data = np.copy(half_map1.data);
		halfMap2Data = np.copy(half_map2.data);
		sizeMap = halfMap1Data.shape;

		# set output filename and working directory
		path = self.fileLine_output.text();
		if path == '':
			path = os.path.dirname(self.fileLine_halfMap1.text());
		os.chdir(path);
		splitFilename = os.path.splitext(os.path.basename(self.fileLine_halfMap1.text()));
		outputFilename_LocRes = splitFilename[0] + "_localResolutions.mrc";


		# make the mask
		try:
			mask = mrcfile.open(self.fileLine_mask.text(), mode='r+');
		except:
			mask = None;

		maskData = FSCutil.makeCircularMask(halfMap1Data, (np.min(halfMap1Data.shape) / 2.0) - 4.0);  # circular mask

		if mask is not None:
			print("Using user provided mask ...");
			maskPermutationData = np.copy(mask.data);
		else:
			maskPermutationData = maskData;

		#**************************************
		#********* get pixel size *************
		#**************************************
		apixMap = float(half_map1.voxel_size.x);

		try:
			apix = float(self.apix.text());
		except:
			apix = None;

		if apix is not None:
			print('Pixel size set to {:.3f} Angstroem. (Pixel size encoded in map: {:.3f})'.format(apix, apixMap));
		else:
			print(
				'Pixel size was read as {:.3f} Angstroem. If this is incorrect, please specify with -p pixelSize'.format(
					apixMap));
			apix = apixMap;

		#******************************************
		#************* get step Size **************
		#******************************************

		try:
			stepSize = int(self.stepSize.text());
		except:
			print("Invalid input for stepSize. Needs to be a integer >= 0 ...")

		#******************************************
		#************* get step Size **************
		#******************************************

		try:
			stepSize = int(self.stepSize.text());
		except:
			print("Invalid input for stepSize. Needs to be a integer >= 0 ...")

		#********************************************
		#************* get window size **************
		#********************************************

		try:
			windowSize = int(self.w.text());
		except:
			print("Invalid input for windowSize. Needs to be a integer >= 0 ...");

		# ********************************************
		# ************* get window size **************
		# ********************************************

		try:
			lowRes = int(self.lowRes.text());
		except:
			lowRes = None;

		# *******************************************
		# ********* calc local Resolutions **********
		# *******************************************

		FSCcutoff = 0.5;

		localResMap = localResolutions.localResolutions(halfMap1Data, halfMap2Data, windowSize, stepSize, FSCcutoff, apix,
															1,
															maskData, maskPermutationData);

		# set lowest resolution if wished
		if lowRes is not None:
			lowRes = lowRes;
			localResMap[localResMap > lowRes] = lowRes;

		# write the local resolution map
		localResMapMRC = mrcfile.new(outputFilename_LocRes, overwrite=True);
		localResMap = np.float32(localResMap);
		localResMapMRC.set_data(localResMap);
		localResMapMRC.voxel_size = apix;
		localResMapMRC.close();

		output = "Saved local resolutions map to: " + outputFilename_LocRes;
		print(output);

		end = time.time();
		totalRuntime = end - start;

		print("****** Summary ******");
		print("Runtime: %.2f" % totalRuntime);

		self.showMessageBox();
Beispiel #5
0
	def runFSC(self):


		#show message box before starting
		msg = QMessageBox();
		msg.setIcon(QMessageBox.Information);
		msg.setText("Start the job with OK!")
		msg.setInformativeText("GUI will be locked until the job is finished. See terminal printouts for progress ...");
		msg.setWindowTitle("Start job");
		msg.setStandardButtons( QMessageBox.Cancel| QMessageBox.Ok);
		result = msg.exec_();

		if result == QMessageBox.Cancel:
			return;


		start = time.time();

		print('***************************************************');
		print('******* Significance analysis of FSC curves *******');
		print('***************************************************');

		#read the half maps
		try:
			half_map1 = mrcfile.open(self.fileLine_halfMap1.text(), mode='r');
			half_map2 = mrcfile.open(self.fileLine_halfMap2.text(), mode='r');
		except:
			msg = QMessageBox();
			msg.setIcon(QMessageBox.Information);
			msg.setText("Cannot read file ...");
			msg.setWindowTitle("Error");
			msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
			retval = msg.exec_();
			return;

		halfMap1Data = np.copy(half_map1.data);
		halfMap2Data = np.copy(half_map2.data);
		sizeMap = halfMap1Data.shape;

		# set working directory and output filename
		path = self.fileLine_output.text();
		if path == '':
			path = os.path.dirname(self.fileLine_halfMap1.text());
		os.chdir(path);
		splitFilename = os.path.splitext(os.path.basename(self.fileLine_halfMap1.text()));
		outputFilename_PostProcessed =  splitFilename[0] + "_filtered.mrc";


		# make the mask
		maskData = FSCutil.makeCircularMask(halfMap1Data, (np.min(halfMap1Data.shape) / 2.0) - 4.0);  # circular mask
		maskBFactor = FSCutil.makeCircularMask(halfMap1Data, (
					np.min(halfMap1Data.shape) / 4.0) - 4.0);  # smaller circular mask for B-factor estimation


		#**************************************
		#********* get pixel size *************
		#**************************************
		apixMap = float(half_map1.voxel_size.x);

		try:
			apix = float(self.apix.text());
		except:
			apix = None;

		if apix is not None:
			print('Pixel size set to {:.3f} Angstroem. (Pixel size encoded in map: {:.3f})'.format(apix, apixMap));
		else:
			print(
				'Pixel size was read as {:.3f} Angstroem. If this is incorrect, please specify with -p pixelSize'.format(
					apixMap));
			apix = apixMap;

		#******************************************
		#*********** get num Asym Units ***********
		#******************************************

		try:
			numAsymUnits = int(self.numAsUnit.text());
		except:
			numAsymUnits = None;

		if numAsymUnits is not None:
			print('Using user provided number of asymmetric units, given as {:d}'.format(numAsymUnits));
		else:
			symmetry = self.symmetry.text();
			numAsymUnits = FSCutil.getNumAsymUnits(symmetry);
			print('Using provided ' + symmetry + ' symmetry. Number of asymmetric units: {:d}'.format(numAsymUnits));

		#**********************************************
		#*************** get resolutions **************
		#**********************************************

		#read the mask
		#mask = mrcfile.open('/Users/mbeckers/Documents/LabBook/2019/June2019/mapModel_FSC/betaGal_refinement/mask.mrc', mode='r');
		#maskData = mask.data;

		#run the FSC
		res, FSC, percentCutoffs, pValues, qValsFDR, resolution, _ = FSCutil.FSC(halfMap1Data, halfMap2Data,
																				 maskData, apix, 0.143,
																				 numAsymUnits, False, True, None,
																				 False);

		# write the FSC
		FSCutil.writeFSC(res, FSC, qValsFDR, pValues, resolution);

		processedMap = FDRutil.sharpenMap(0.5 * (halfMap1Data + halfMap2Data), 0, apix, resolution);

		# write the post-processed map
		postProcMRC = mrcfile.new(outputFilename_PostProcessed, overwrite=True);
		postProc = np.float32(processedMap);
		postProcMRC.set_data(postProc);
		postProcMRC.voxel_size = apix;
		postProcMRC.close();

		output = "Saved filtered map to: " + outputFilename_PostProcessed;
		print(output);

		end = time.time();
		totalRuntime = end - start;

		print("****** Summary ******");
		print("Runtime: %.2f" % totalRuntime);

		self.showMessageBox(resolution);
def localResolutions(halfMap1, halfMap2, boxSize, stepSize, cutoff, apix,
                     numAsymUnits, mask, maskPermutation):

    # ********************************************
    # ****** calculate local resolutions by ******
    # ********** local FSC-thresholding **********
    # ********************************************

    print("Starting calculations of local resolutions ...")

    sizeMap = halfMap1.shape
    locRes = np.zeros((len(range(boxSize, boxSize + sizeMap[0], stepSize)),
                       len(range(boxSize, boxSize + sizeMap[1], stepSize)),
                       len(range(boxSize, boxSize + sizeMap[2], stepSize))))

    # pad the volumes
    paddedHalfMap1 = np.zeros(
        (sizeMap[0] + 2 * boxSize, sizeMap[1] + 2 * boxSize,
         sizeMap[2] + 2 * boxSize))
    paddedHalfMap2 = np.zeros(
        (sizeMap[0] + 2 * boxSize, sizeMap[1] + 2 * boxSize,
         sizeMap[2] + 2 * boxSize))
    paddedMask = np.zeros((sizeMap[0] + 2 * boxSize, sizeMap[1] + 2 * boxSize,
                           sizeMap[2] + 2 * boxSize))
    paddedMaskPermutation = np.zeros(
        (sizeMap[0] + 2 * boxSize, sizeMap[1] + 2 * boxSize,
         sizeMap[2] + 2 * boxSize))

    paddedHalfMap1[boxSize:boxSize + sizeMap[0], boxSize:boxSize + sizeMap[1],
                   boxSize:boxSize + sizeMap[2]] = halfMap1
    paddedHalfMap2[boxSize:boxSize + sizeMap[0], boxSize:boxSize + sizeMap[1],
                   boxSize:boxSize + sizeMap[2]] = halfMap2
    paddedMask[boxSize:boxSize + sizeMap[0], boxSize:boxSize + sizeMap[1],
               boxSize:boxSize + sizeMap[2]] = mask
    paddedMaskPermutation[boxSize:boxSize + sizeMap[0],
                          boxSize:boxSize + sizeMap[1],
                          boxSize:boxSize + sizeMap[2]] = maskPermutation

    halfBoxSize = int(boxSize / 2.0)

    # make Hann window
    hannWindow = FDRutil.makeHannWindow(np.zeros((boxSize, boxSize, boxSize)))

    numCalculations = len(range(
        boxSize, boxSize + sizeMap[0], stepSize)) * len(
            range(boxSize, boxSize + sizeMap[1], stepSize)) * len(
                range(boxSize, boxSize + sizeMap[0], stepSize))
    print("Total number of calculations: " + repr(numCalculations))

    # ****************************************************
    # ********* get initial permuted CorCoeffs ***********
    # ****************************************************

    print("Do initial permuations ...")
    for i in range(10):

        xInd = np.random.randint(boxSize, sizeMap[0] + boxSize)
        yInd = np.random.randint(boxSize, sizeMap[1] + boxSize)
        zInd = np.random.randint(boxSize, sizeMap[2] + boxSize)

        #xInd = np.random.randint(sizeMap[0]/2 - sizeMap[0]/8 + boxSize, sizeMap[0]/2 + sizeMap[0]/8 + boxSize);
        #yInd = np.random.randint(sizeMap[1]/2 - sizeMap[1]/8 + boxSize, sizeMap[1]/2 + sizeMap[1]/8 + boxSize);
        #zInd = np.random.randint(sizeMap[2]/2 - sizeMap[2]/8 + boxSize, sizeMap[2]/2 + sizeMap[2]/8 + boxSize);

        #generate new locations until one is found in the mask
        while ((paddedMaskPermutation[xInd, yInd, zInd] < 0.5)):

            xInd = np.random.randint(boxSize, sizeMap[0] + boxSize)
            yInd = np.random.randint(boxSize, sizeMap[1] + boxSize)
            zInd = np.random.randint(boxSize, sizeMap[2] + boxSize)

            #xInd = np.random.randint(sizeMap[0] / 2 - sizeMap[0] / 8 + boxSize,
            #						 sizeMap[0] / 2 + sizeMap[0] / 8 + boxSize);
            #yInd = np.random.randint(sizeMap[1] / 2 - sizeMap[1] / 8 + boxSize,
            #						 sizeMap[1] / 2 + sizeMap[1] / 8 + boxSize);
            #zInd = np.random.randint(sizeMap[2] / 2 - sizeMap[2] / 8 + boxSize,
            #						 sizeMap[2] / 2 + sizeMap[2] / 8 + boxSize);

        #get windowed parts
        windowHalfmap1 = paddedHalfMap1[xInd - halfBoxSize:xInd - halfBoxSize +
                                        boxSize, yInd - halfBoxSize:yInd -
                                        halfBoxSize + boxSize,
                                        zInd - halfBoxSize:zInd - halfBoxSize +
                                        boxSize]
        windowHalfmap2 = paddedHalfMap2[xInd - halfBoxSize:xInd - halfBoxSize +
                                        boxSize, yInd - halfBoxSize:yInd -
                                        halfBoxSize + boxSize,
                                        zInd - halfBoxSize:zInd - halfBoxSize +
                                        boxSize]

        # apply hann window
        windowHalfmap1 = windowHalfmap1 * hannWindow
        windowHalfmap2 = windowHalfmap2 * hannWindow

        res, _, _, _, _, _, tmpPermutedCorCoeffs = FSCutil.FSC(
            windowHalfmap1, windowHalfmap2, None, apix, cutoff, numAsymUnits,
            True, False, None, False)

        if i == 0:
            # initialize the array of correlation coefficients
            permutedCorCoeffs = tmpPermutedCorCoeffs
        else:
            # append the correlation coefficients
            for resInd in range(len(tmpPermutedCorCoeffs)):
                permutedCorCoeffs[resInd] = np.append(
                    permutedCorCoeffs[resInd], tmpPermutedCorCoeffs[resInd])

    # ****************************************************
    # ********* calculate the local resolutions **********
    # ****************************************************

    print("Do local FSC calculations ...")

    # generate partial function to loop over the whole map
    partialLoopOverMap = functools.partial(loopOverMap,
                                           paddedMask=paddedMask,
                                           paddedHalfMap1=paddedHalfMap1,
                                           paddedHalfMap2=paddedHalfMap2,
                                           boxSize=boxSize,
                                           sizeMap=sizeMap,
                                           stepSize=stepSize,
                                           halfBoxSize=halfBoxSize,
                                           hannWindow=hannWindow,
                                           apix=apix,
                                           cutoff=cutoff,
                                           numAsymUnits=numAsymUnits,
                                           permutedCorCoeffs=permutedCorCoeffs)

    #parallelized local resolutions
    numCores = min(multiprocessing.cpu_count(), 4)
    print(
        "Using {:d} cores. This might take a few minutes ...".format(numCores))
    iIterable = range(boxSize, boxSize + sizeMap[0], stepSize)

    #initialize parallel processes
    lenInt = int(math.ceil(len(iIterable) / float(numCores)))
    queue = multiprocessing.Queue()

    #start process for each core and run in parallel
    for i in range(numCores):

        #split the iterable
        startInd = (i * lenInt)
        endInd = (i + 1) * lenInt
        if i == (numCores - 1):
            seq = range(iIterable[startInd],
                        iIterable[len(iIterable) - 1] + stepSize, stepSize)
        else:
            seq = range(iIterable[startInd], iIterable[endInd], stepSize)

        #start the respective process
        proc = multiprocessing.Process(target=partialLoopOverMap,
                                       args=(
                                           seq,
                                           queue,
                                       ))
        proc.start()

    #addition of indiviual local resolution maps to produce the final one
    for i in range(numCores):
        locRes = locRes + queue.get()

    # *************************************
    # ********** do interpolation *********
    # *************************************

    #locRes[locRes==0] = 2.2;

    print("Interpolating local Resolutions ...")
    x = np.linspace(1, 10, locRes.shape[0])
    y = np.linspace(1, 10, locRes.shape[1])
    z = np.linspace(1, 10, locRes.shape[2])

    myInterpolatingFunction = RegularGridInterpolator((x, y, z),
                                                      locRes,
                                                      method='linear')

    xNew = np.linspace(1, 10, sizeMap[0])
    yNew = np.linspace(1, 10, sizeMap[1])
    zNew = np.linspace(1, 10, sizeMap[2])

    xInd, yInd, zInd = np.meshgrid(xNew,
                                   yNew,
                                   zNew,
                                   indexing='ij',
                                   sparse=True)

    localRes = myInterpolatingFunction((xInd, yInd, zInd))

    localRes[mask <= 0.01] = 0.0

    return localRes
Beispiel #7
0
	def runFSC(self):

		#show message box before starting
		msg = QMessageBox();
		msg.setIcon(QMessageBox.Information);
		msg.setText("Start the job with OK!")
		msg.setInformativeText("GUI will be locked until the job is finished. See terminal printouts for progress ...");
		msg.setWindowTitle("Start job");
		msg.setStandardButtons( QMessageBox.Cancel| QMessageBox.Ok);
		result = msg.exec_();

		if result == QMessageBox.Cancel:
			return;


		start = time.time();

		print('***************************************************');
		print('**********  Sharpening of cryo-EM maps  ***********');
		print('***************************************************');

		#read the half maps
		try:
			half_map1 = mrcfile.open(self.fileLine_halfMap1.text(), mode='r');
			half_map2 = mrcfile.open(self.fileLine_halfMap2.text(), mode='r');
		except:
			msg = QMessageBox();
			msg.setIcon(QMessageBox.Information);
			msg.setText("Cannot read file ...");
			msg.setWindowTitle("Error");
			msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
			retval = msg.exec_();
			return;

		halfMap1Data = np.copy(half_map1.data);
		halfMap2Data = np.copy(half_map2.data);
		sizeMap = halfMap1Data.shape;

		# set working directory and output filename
		path = self.fileLine_output.text();
		if path == '':
			path = os.path.dirname(self.fileLine_halfMap1.text());
		os.chdir(path);
		splitFilename = os.path.splitext(os.path.basename(self.fileLine_halfMap1.text()));
		outputFilename_PostProcessed =  "postProcessed.mrc";
		outputFilename_PostProcessed_half1 = "postProcessed_half1.mrc";
		outputFilename_PostProcessed_half2 = "postProcessed_half2.mrc";

		# make the mask
		maskData = FSCutil.makeCircularMask(halfMap1Data, (np.min(halfMap1Data.shape) / 2.0) - 4.0);  # circular mask
		maskBFactor = FSCutil.makeCircularMask(halfMap1Data, (
					np.min(halfMap1Data.shape) / 4.0) - 4.0);  # smaller circular mask for B-factor estimation


		#**************************************
		#********* get pixel size *************
		#**************************************
		apixMap = float(half_map1.voxel_size.x);

		try:
			apix = float(self.apix.text());
		except:
			apix = None;

		if apix is not None:
			print('Pixel size set to {:.3f} Angstroem. (Pixel size encoded in map: {:.3f})'.format(apix, apixMap));
		else:
			print(
				'Pixel size was read as {:.3f} Angstroem. If this is incorrect, please specify with -p pixelSize'.format(
					apixMap));
			apix = apixMap;

		#**********************************************
		#***************** get bfactor ****************
		#**********************************************
		try:
			bFactorInput = float(self.bFactor.text());
		except:
			bFactorInput = None;

		#**********************************************
		#***************** get bfactor ****************
		#**********************************************
		try:
			resolution = float(self.resolution.text());
		except:
			msg = QMessageBox();
			msg.setIcon(QMessageBox.Information);
			msg.setText("No resolution specified ...");
			msg.setWindowTitle("Error");
			msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
			retval = msg.exec_();
			return;


		if (resolution > 8.0) and (bFactorInput is None):
			msg = QMessageBox();
			msg.setIcon(QMessageBox.Information);
			msg.setText("Automated B-factor estimation is unstable for low-resolution maps. Please specify a B-factor!");
			msg.setWindowTitle("Error");
			msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel);
			retval = msg.exec_();
			return;


		if bFactorInput is not None:
			bFactor = bFactorInput;
			bFactor_half1 = bFactorInput;
			bFactor_half2 = bFactorInput;
			print('Using a user-specified B-factor of {:.2f} for map sharpening'.format(-bFactor));
		else:
			# estimate b-factor and sharpen the map
			bFactor = FSCutil.estimateBfactor(0.5 * (halfMap1Data + halfMap2Data), resolution, apix, maskBFactor);
			print('Using a B-factor of {:.2f} for map sharpening.'.format(-bFactor));

			#bFactor_half1 = FSCutil.estimateBfactor(halfMap1Data, resolution, apix, maskBFactor);
			#bFactor_half2 = FSCutil.estimateBfactor(halfMap2Data, resolution, apix, maskBFactor);

			#print("B-factor of halfmap 1: {:.2f}".format(bFactor_half1));
			#print("B-factor of halfmap 2: {:.2f}".format(bFactor_half2));

		processedMap = FDRutil.sharpenMap(0.5 * (halfMap1Data + halfMap2Data), -bFactor, apix, resolution);
		#processed_halfMap1 = FDRutil.sharpenMap(halfMap1Data, -bFactor_half1, apix, resolution);
		#processed_halfMap2 = FDRutil.sharpenMap(halfMap2Data, -bFactor_half2, apix, resolution);

		# write the post-processed map
		postProcMRC = mrcfile.new(outputFilename_PostProcessed, overwrite=True);
		postProc = np.float32(processedMap);
		postProcMRC.set_data(postProc);
		postProcMRC.voxel_size = apix;
		postProcMRC.close();

		"""
		# write the post-processed halfmaps
		postProcMRC = mrcfile.new(outputFilename_PostProcessed_half1, overwrite=True);
		postProc = np.float32(processed_halfMap1);
		postProcMRC.set_data(postProc);
		postProcMRC.voxel_size = apix;
		postProcMRC.close();

		postProcMRC = mrcfile.new(outputFilename_PostProcessed_half2, overwrite=True);
		postProc = np.float32(processed_halfMap2);
		postProcMRC.set_data(postProc);
		postProcMRC.voxel_size = apix;
		postProcMRC.close();
		"""

		output = "Saved sharpened and filtered map to: " + outputFilename_PostProcessed;
		print(output);

		end = time.time();
		totalRuntime = end - start;

		print("****** Summary ******");
		print("Runtime: %.2f" % totalRuntime);

		self.showMessageBox(resolution, bFactor);
def main():
    start = time.time()

    print('***************************************************')
    print('******* Significance analysis of FSC curves *******')
    print('***************************************************')

    # get command line input
    args = cmdl_parser.parse_args()

    #read the half maps
    halfMap1 = mrcfile.open(args.halfmap1, mode='r')
    halfMap2 = mrcfile.open(args.halfmap2, mode='r')

    halfMap1Data = np.copy(halfMap1.data)
    halfMap2Data = np.copy(halfMap2.data)

    #get size of map
    sizeMap = halfMap2Data.shape

    #set pixel size
    apix = float(halfMap1.voxel_size.x)
    if args.apix is not None:
        print(
            'Pixel size set to {:.3f} Angstroem. (Pixel size encoded in map: {:.3f})'
            .format(args.apix, apix))
        apix = args.apix
    else:
        print(
            'Pixel size was read as {:.3f} Angstroem. If this is incorrect, please specify with -p pixelSize'
            .format(apix))
        args.apix = apix

    # set output filename
    splitFilename = os.path.splitext(os.path.basename(args.halfmap1))
    print(splitFilename[0])
    outputFilename_LocRes = splitFilename[0] + "_localResolutions.mrc"
    outputFilename_PostProcessed = "postProcessed.mrc"
    outputFilename_PostProcessed_half1 = "postProcessed_half1.mrc"
    outputFilename_PostProcessed_half2 = "postProcessed_half2.mrc"
    outputFilename_averagedHalfmaps = splitFilename[0] + "_avg.mrc"

    #handle window size for local FSC
    if args.window_size is not None:
        wn = args.window_size
        wn = int(wn)
    else:
        wn = 20
        #default is 20 pixels

    #handle step size for local FSC
    if args.stepSize is None:
        stepSize = float(sizeMap[0] * sizeMap[1] * sizeMap[2]) / 300000.0
        stepSize = max(int(math.ceil(stepSize**(1.0 / 3.0))), 1)
    else:
        stepSize = int(args.stepSize)

    if not args.localResolutions:
        if args.numAsymUnits is not None:

            numAsymUnits = args.numAsymUnits
            print(
                'Using user provided number of asymmetric units, given as {:d}'
                .format(numAsymUnits))
        else:
            if args.symmetry is not None:

                numAsymUnits = FSCutil.getNumAsymUnits(args.symmetry)
                print('Using provided ' + args.symmetry +
                      ' symmetry. Number of asymmetric units: {:d}'.format(
                          numAsymUnits))
            else:
                numAsymUnits = 1
                print('Using C1 symmetry. Number of asymmetric units: {:d}'.
                      format(numAsymUnits))
    else:
        #if local resolutions are calculated, no symmetry correction needed
        print(
            "Using a step size of {:d} voxel. If you prefer another one, please specify with -step."
            .format(stepSize))
        print(
            'Calculating local resolutions. No symmetry correction necessary.')
        numAsymUnits = 1.0

    #make the mask
    print("Using a circular mask ...")
    maskData = FSCutil.makeCircularMask(
        halfMap1Data, (np.min(halfMap1Data.shape) / 2.0) - 4.0)
    #circular mask
    maskBFactor = FSCutil.makeCircularMask(
        halfMap1Data, (np.min(halfMap1Data.shape) / 4.0) - 4.0)
    #smaller circular mask for B-factor estimation

    #*******************************************
    #********** no local Resolutions ***********
    #*******************************************

    if not args.localResolutions:
        res, FSC, percentCutoffs, pValues, qValsFDR, resolution, _ = FSCutil.FSC(
            halfMap1Data, halfMap2Data, maskData, apix, 0.143, numAsymUnits,
            False, True, None, False)
        # write the FSC
        FSCutil.writeFSC(res, FSC, qValsFDR, pValues, resolution)

        if resolution < 8.0:

            #estimate b-factor and sharpen the map
            bFactor = FSCutil.estimateBfactor(
                0.5 * (halfMap1Data + halfMap2Data), resolution, apix,
                maskBFactor)
            #bFactor_half1 = FSCutil.estimateBfactor(halfMap1Data, resolution, apix, maskBFactor);
            #bFactor_half2 = FSCutil.estimateBfactor(halfMap2Data, resolution, apix, maskBFactor);

            if args.bFactor is not None:
                bFactor = args.bFactor
                print(
                    'Using a user-specified B-factor of {:.2f} for map sharpening'
                    .format(-bFactor))
            else:
                print('Using a B-factor of {:.2f} for map sharpening.'.format(
                    -bFactor))

            processedMap = FDRutil.sharpenMap(
                0.5 * (halfMap1Data + halfMap2Data), -bFactor, apix,
                resolution)

            #processed_halfMap1 = FDRutil.sharpenMap(halfMap1Data, -bFactor_half1, apix, resolution);
            #processed_halfMap2 = FDRutil.sharpenMap(halfMap2Data, -bFactor_half2, apix, resolution);

            #write the post-processed maps
            postProcMRC = mrcfile.new(outputFilename_PostProcessed,
                                      overwrite=True)
            postProc = np.float32(processedMap)
            postProcMRC.set_data(postProc)
            postProcMRC.voxel_size = apix
            postProcMRC.close()
            """
			#write the post-processed halfmaps
			postProcMRC = mrcfile.new(outputFilename_PostProcessed_half1, overwrite=True);
			postProc= np.float32(processed_halfMap1);
			postProcMRC.set_data(postProc);
			postProcMRC.voxel_size = apix;
			postProcMRC.close();

			postProcMRC = mrcfile.new(outputFilename_PostProcessed_half2, overwrite=True);
			postProc= np.float32(processed_halfMap2);
			postProcMRC.set_data(postProc);
			postProcMRC.voxel_size = apix;
			postProcMRC.close();
			"""

            output = "Saved sharpened and filtered map to: " + outputFilename_PostProcessed
            print(output)

    #*******************************************
    #********* calc local Resolutions **********
    #*******************************************
    else:

        FSCcutoff = 0.5

        #set mask for locations of permutations
        if args.mask is not None:
            maskPermuation = mrcfile.open(args.mask, mode='r')
            maskPermutationData = np.copy(maskPermuation.data)
        else:
            maskPermutationData = maskData

        localResMap = localResolutions.localResolutions(
            halfMap1Data, halfMap2Data, wn, stepSize, FSCcutoff, apix,
            numAsymUnits, maskData, maskPermutationData)

        # set lowest resolution if wished
        if args.lowRes is not None:
            lowRes = args.lowRes
            localResMap[localResMap > lowRes] = lowRes

        #write the local resolution map
        localResMapMRC = mrcfile.new(outputFilename_LocRes, overwrite=True)
        localResMap = np.float32(localResMap)
        localResMapMRC.set_data(localResMap)
        localResMapMRC.voxel_size = apix
        localResMapMRC.close()

        output = "Saved local resolutions map to: " + outputFilename_LocRes
        print(output)

    end = time.time()
    totalRuntime = end - start

    print("****** Summary ******")
    print("Runtime: %.2f" % totalRuntime)