def alignParticlesStep(self): fhInputTranMat = self._getExtraPath('transformation-matrix.txt') outParticlesFn = self._getExtraPath('outputParticles.xmd') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = Transform() transform.setMatrix(transformationMat) resultMat = Transform() outputParts = md.MetaData() mdToAlign = md.MetaData(self.imgsInputFn) for row in md.iterRows(mdToAlign): inMat = rowToAlignment(row, ALIGN_PROJ) partTransformMat = inMat.getMatrix() partTransformMatrix = np.matrix(partTransformMat) newTransformMatrix = np.matmul(transformationMat, partTransformMatrix) resultMat.setMatrix(newTransformMatrix) rowOut = md.Row() rowOut.copyFromRow(row) alignmentToRow(resultMat, rowOut, ALIGN_PROJ) rowOut.addToMd(outputParts) outputParts.write(outParticlesFn) cleanPath(self.imgsInputFn)
def _appendRctImages(self, particles): blockMd = "class%06d_images@%s" % (particles.getObjId(), self.rctClassesFn) classMd = emlib.MetaData() partPairs = self.inputParticlesTiltPair.get() uImages = partPairs.getUntilted() tImages = partPairs.getTilted() sangles = partPairs.getCoordsPair().getAngles() micPairs = partPairs.getCoordsPair().getMicsPair() uMics = micPairs.getUntilted() tMics = micPairs.getTilted() scaleFactor = uImages.getSamplingRate() / particles.getSamplingRate() for img in particles: imgId = img.getObjId() uImg = uImages[imgId] tImg = tImages[imgId] if uImg is None or tImg is None: print(">>> Warning, for id %d, tilted or untilted particle " "was not found. Ignored." % imgId) else: objId = classMd.addObject() pairRow = emlib.metadata.Row() pairRow.setValue(emlib.MDL_IMAGE, getImageLocation(uImg)) uCoord = uImg.getCoordinate() micId = uCoord.getMicId() uMic = uMics[micId] angles = sangles[micId] pairRow.setValue(emlib.MDL_MICROGRAPH, uMic.getFileName()) pairRow.setValue(emlib.MDL_XCOOR, uCoord.getX()) pairRow.setValue(emlib.MDL_YCOOR, uCoord.getY()) pairRow.setValue(emlib.MDL_ENABLED, 1) pairRow.setValue(emlib.MDL_ITEM_ID, int(imgId)) pairRow.setValue(emlib.MDL_REF, 1) alignment = img.getTransform() # Scale alignment by scaleFactor alignment.scale(scaleFactor) alignmentToRow(alignment, pairRow, alignType=ALIGN_2D) pairRow.setValue(emlib.MDL_IMAGE_TILTED, getImageLocation(tImg)) tMic = tMics[micId] pairRow.setValue(emlib.MDL_MICROGRAPH_TILTED, tMic.getFileName()) (angleY, angleY2, angleTilt) = angles.getAngles() pairRow.setValue(emlib.MDL_ANGLE_Y, float(angleY)) pairRow.setValue(emlib.MDL_ANGLE_Y2, float(angleY2)) pairRow.setValue(emlib.MDL_ANGLE_TILT, float(angleTilt)) pairRow.writeToMd(classMd, objId) classMd.write(blockMd, emlib.MD_APPEND)
def applyAlignmentStep(self, inputFn): inputSt = self.inputSubtomograms.get() # Window subtomograms twice their size windowedStk = self._getExtraPath('windowed_subtomograms.stk') self.runJob( 'xmipp_transform_window', '-i %s -o %s --size %d --save_metadata_stack' % (inputFn, windowedStk, 2 * inputSt.getFirstItem().getDim()[0]), numberOfMpi=1) # Add input transform matrix to md generated by xmipp_transform_window mdWindow = md.MetaData(self._getExtraPath('windowed_subtomograms.xmd')) mdWindowTransform = md.MetaData() idList = list(inputSt.getIdSet()) for row in md.iterRows(mdWindow): rowOut = md.Row() rowOut.copyFromRow(row) id = row.getValue(MDL_IMAGE) id = id.split('@')[0] id = id.strip('0') alignmentToRow(inputSt[(idList[int(id) - 1])].getTransform(), rowOut, pwem.ALIGN_3D) rowOut.addToMd(mdWindowTransform) mdWindowTransform.write( self._getExtraPath("window_with_original_geometry.xmd")) # Align subtomograms self.runJob( 'xmipp_transform_geometry', '-i %s -o %s --apply_transform' % (self._getExtraPath("window_with_original_geometry.xmd"), self._getExtraPath('aligned_subtomograms.stk'))) # Window subtomograms to their original size alignStk = self._getExtraPath('aligned_subtomograms.stk') outputStk = self._getPath('output_subtomograms.stk') self.runJob('xmipp_transform_window', '-i %s -o %s --size %d ' % (alignStk, outputStk, self.inputSubtomograms.get().getFirstItem().getDim()[0]), numberOfMpi=1) return [outputStk]
def applyAlignStep(self): """Align subtomograms to be in the same orientation as the reference""" inputSet = self.inputSubtomos.get() outputFn = self._getExtraPath('input_subtomos.xmd') if inputSet.getFirstItem().getFileName().endswith('.mrc') or \ inputSet.getFirstItem().getFileName().endswith('.map'): S = self._createSetOfSubTomograms() S.setSamplingRate(inputSet.getSamplingRate()) for subtomo in inputSet: s = subtomo.clone() s.setFileName(subtomo.getFileName() + ':mrc') S.append(s) writeSetOfVolumes(S, outputFn, alignType=ALIGN_3D) else: writeSetOfVolumes(inputSet, outputFn, alignType=ALIGN_3D) # Window subtomograms twice their size windowedStk = self._getExtraPath('windowed_subtomograms.stk') self.runJob( 'xmipp_transform_window', '-i %s -o %s --size %d --save_metadata_stack' % (outputFn, windowedStk, 2 * inputSet.getFirstItem().getDim()[0]), numberOfMpi=1) # Add input transform matrix to md generated by xmipp_transform_window mdWindow = md.MetaData(self._getExtraPath('windowed_subtomograms.xmd')) mdWindowTransform = md.MetaData() idList = list(inputSet.getIdSet()) for row in md.iterRows(mdWindow): rowOut = md.Row() rowOut.copyFromRow(row) id = row.getValue(MDL_IMAGE) id = id.split('@')[0] id = id.strip('0') alignmentToRow(inputSet[(idList[int(id) - 1])].getTransform(), rowOut, ALIGN_3D) rowOut.addToMd(mdWindowTransform) mdWindowTransform.write( self._getExtraPath("window_with_original_geometry.xmd")) # Align subtomograms self.runJob( 'xmipp_transform_geometry', '-i %s -o %s --apply_transform --dont_wrap' % (self._getExtraPath("window_with_original_geometry.xmd"), self._getExtraPath('aligned_subtomograms.stk'))) # Window subtomograms to their original size alignStk = self._getExtraPath('aligned_subtomograms.stk') outputStk = self._getExtraPath('output_subtomograms.stk') self.runJob('xmipp_transform_window', '-i %s -o %s --size %d ' % (alignStk, outputStk, inputSet.getFirstItem().getDim()[0]), numberOfMpi=1) self.alignedSet = self._createSetOfSubTomograms() self.alignedSet.copyInfo(inputSet) inputMd = self._getExtraPath('output_subtomograms.stk') self.alignedSet.copyItems(inputSet, updateItemCallback=self._updateItemAlign, itemDataIterator=md.iterRows( inputMd, sortByLabel=md.MDL_ITEM_ID))
def realignStep(self): inputMdName = self._getExtraPath('inputClasses.xmd') writeSetOfClasses2D(self.inputClasses.get(), inputMdName, writeParticles=True) centeredStackName = self._getExtraPath('centeredStack.stk') self._params = {'input': inputMdName, 'output': centeredStackName} args = ('-i %(input)s -o %(output)s --save_metadata_transform') self.runJob("xmipp_transform_center_image", args % self._params, numberOfMpi=1) centeredMdName = centeredStackName.replace('stk', 'xmd') centeredMd = md.MetaData(centeredMdName) centeredStack = md.MetaData(centeredStackName) listName = [] listTransform=[] for rowStk in md.iterRows(centeredStack): listName.append(rowStk.getValue(md.MDL_IMAGE)) for rowMd in md.iterRows(centeredMd): listTransform.append(rowToAlignment(rowMd, ALIGN_2D)) mdNewClasses = md.MetaData() for i, row in enumerate(md.iterRows(inputMdName)): newRow = md.Row() newRow.setValue(md.MDL_IMAGE, listName[i]) refNum = row.getValue(md.MDL_REF) newRow.setValue(md.MDL_REF, refNum) classCount = row.getValue(md.MDL_CLASS_COUNT) newRow.setValue(md.MDL_CLASS_COUNT, classCount) newRow.addToMd(mdNewClasses) mdNewClasses.write('classes@' + self._getExtraPath('final_classes.xmd'), MD_APPEND) mdImages = md.MetaData() i=0 mdBlocks = md.getBlocksInMetaDataFile(inputMdName) resultMat = Transform() for block in mdBlocks: if block.startswith('class00'): newMat = listTransform[i] newMatrix = newMat.getMatrix() mdClass = md.MetaData(block + "@" + inputMdName) mdNewClass = md.MetaData() i+=1 for rowIn in md.iterRows(mdClass): #To create the transformation matrix (and its parameters) # for the realigned particles if rowIn.getValue(md.MDL_ANGLE_PSI)!=0: flag_psi=True if rowIn.getValue(md.MDL_ANGLE_ROT)!=0: flag_psi=False inMat = rowToAlignment(rowIn, ALIGN_2D) inMatrix = inMat.getMatrix() resultMatrix = np.dot(newMatrix,inMatrix) resultMat.setMatrix(resultMatrix) rowOut=md.Row() rowOut.copyFromRow(rowIn) alignmentToRow(resultMat, rowOut, ALIGN_2D) if flag_psi==False: newAngle = rowOut.getValue(md.MDL_ANGLE_PSI) rowOut.setValue(md.MDL_ANGLE_PSI, 0.) rowOut.setValue(md.MDL_ANGLE_ROT, newAngle) #To create the new coordinates for the realigned particles inPoint = np.array([[0.],[0.],[0.],[1.]]) invResultMat = np.linalg.inv(resultMatrix) centerPoint = np.dot(invResultMat,inPoint) rowOut.setValue(md.MDL_XCOOR, rowOut.getValue( md.MDL_XCOOR)+int(centerPoint[0])) rowOut.setValue(md.MDL_YCOOR, rowOut.getValue( md.MDL_YCOOR)+int(centerPoint[1])) rowOut.addToMd(mdNewClass) mdNewClass.write(block + "@" + self._getExtraPath( 'final_classes.xmd'), MD_APPEND) mdImages.unionAll(mdNewClass) mdImages.write(self._getExtraPath('final_images.xmd'))
def runMapBack(self, classId): for tomo in self.inputTomograms.get().iterItems(): if self.selection == 0: TsSubtomo = self.inputClasses.get().getSamplingRate() else: TsSubtomo = self.inputRef.get().getSamplingRate() TsTomo = tomo.getSamplingRate() scaleFactor = TsSubtomo / TsTomo mdGeometry = lib.MetaData() if self.selection == 0: inputSet = self.inputClasses.get().getFirstItem() ref = self._getExtraPath("reference%d.mrc" % classId) else: inputSet = self.inputSubtomos.get() ref = self.inputRef.get().getFileName() for subtomo in inputSet.iterItems(): fn = subtomo.getFileName() if fn.endswith('.mrc'): fn += ':mrc' if subtomo.getCoordinate3D().getVolId() == tomo.getObjId() \ or basename(subtomo.getVolName()) == tomo.getBaseName().partition('import_')[0]: nRow = md.Row() nRow.setValue(lib.MDL_ITEM_ID, int(subtomo.getObjId())) coord = subtomo.getCoordinate3D() coord.setVolume(tomo) nRow.setValue( lib.MDL_XCOOR, int( coord.getX(const.BOTTOM_LEFT_CORNER) * scaleFactor)) nRow.setValue( lib.MDL_YCOOR, int( coord.getY(const.BOTTOM_LEFT_CORNER) * scaleFactor)) nRow.setValue( lib.MDL_ZCOOR, int( coord.getZ(const.BOTTOM_LEFT_CORNER) * scaleFactor)) # Compute inverse matrix A = subtomo.getTransform().getMatrix() subtomo.getTransform().setMatrix(np.linalg.inv(A)) # Convert transform matrix to Euler Angles (rot, tilt, psi) alignmentToRow(subtomo.getTransform(), nRow, ALIGN_3D) nRow.addToMd(mdGeometry) fnGeometry = self._getExtraPath("geometry%d.xmd" % classId) mdGeometry.write(fnGeometry) if TsSubtomo != TsTomo: factor = TsSubtomo / TsTomo args = "-i %s -o %s --scale %d" % (ref, ref, factor) self.runJob('xmipp_transform_geometry', args) if self.paintingType.get() == 0: painting = 'copy' elif self.paintingType.get() == 1: painting = 'avg %d' % self.threshold.get() elif self.paintingType.get() == 2: painting = 'highlight %d' % self.constant.get() elif self.paintingType.get() == 3: painting = 'copy_binary %f' % self.threshold.get() tomogram = self._getExtraPath("tomogram_%d.mrc" % tomo.getObjId()) args = " -i %s -o %s --geom %s --ref %s --method %s" % ( tomogram, tomogram, self._getExtraPath("geometry%d.xmd" % classId), ref, painting) self.runJob("xmipp_tomo_map_back", args)