def createOutput(self): # Check vol and pdb files directory = self._getExtraPath() for filename in sorted(os.listdir(directory)): if filename.endswith(".mrc"): volFileName = os.path.join(directory, filename) vol = Volume() vol.setFileName(volFileName) # fix mrc header ccp4header = Ccp4Header(volFileName, readHeader=True) sampling = ccp4header.computeSampling() origin = Transform() shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) vol.setOrigin(origin) vol.setSamplingRate(sampling) keyword = filename.split(".mrc")[0] kwargs = {keyword: vol} self._defineOutputs(**kwargs) elif filename.endswith(".pdb") or filename.endswith(".cif"): path = os.path.join(directory, filename) pdb = AtomStruct() pdb.setFileName(path) keyword = filename.split(".pdb")[0].replace(".","_") kwargs = {keyword: pdb} self._defineOutputs(**kwargs)
def createOutputStep(self): outVolFn = self._getExtraPath("inputVolumeAligned.mrc") Ts = self.inputVolume.get().getSamplingRate() self.runJob("xmipp_image_header", "-i %s --sampling_rate %f" % (outVolFn, Ts)) outVol = Volume() outVol.setLocation(outVolFn) #set transformation matrix fhInputTranMat = self._getExtraPath('transformation-matrix.txt') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) outVol.setSamplingRate(Ts) outputArgs = {'outputVolume': outVol} self._defineOutputs(**outputArgs) self._defineSourceRelation(self.inputVolume, outVol) #particles.... outParticlesFn = self._getExtraPath('outputParticles.xmd') outputParticles = self._createSetOfParticles() outputParticles.copyInfo(self.inputParticles.get()) outputParticles.setAlignmentProj() readSetOfParticles(outParticlesFn, outputParticles) outputArgs = {'outputParticles': outputParticles} self._defineOutputs(**outputArgs) self._defineSourceRelation(self.inputParticles, outputParticles)
def createOutput(self): """ Copy the PDB structure and register the output object. """ # Check vol and pdb files directory = self._getExtraPath() for filename in sorted(os.listdir(directory)): if not filename.startswith("tmp"): # files starting with "tmp" will not be converted in scipion objects if filename.endswith(".mrc"): volFileName = os.path.join(directory, filename) vol = Volume() vol.setFileName(volFileName) # fix mrc header ccp4header = Ccp4Header(volFileName, readHeader=True) sampling = ccp4header.computeSampling() origin = Transform() shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) vol.setOrigin(origin) vol.setSamplingRate(sampling) keyword = filename.split(".mrc")[0] kwargs = {keyword: vol} self._defineOutputs(**kwargs) if filename.endswith(".pdb") or filename.endswith(".cif"): path = os.path.join(directory, filename) pdb = AtomStruct() pdb.setFileName(path) if filename.endswith(".cif"): keyword = filename.split(".cif")[0].replace(".", "_") else: keyword = filename.split(".pdb")[0].replace(".", "_") kwargs = {keyword: pdb} self._defineOutputs(**kwargs)
def createOutputStep(self, tsObjId): ts = self.inputSetOfTiltSeries.get()[tsObjId] tsId = ts.getTsId() extraPrefix = self._getExtraPath(tsId) outputSetOfTomograms = self.getOutputSetOfTomograms() newTomogram = Tomogram() newTomogram.setLocation(os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".mrc"))) # Set tomogram origin ih = ImageHandler() xDim, yDim, zDim, _ = \ ih.getDimensions(os.path.join(extraPrefix, ts.getFirstItem().parseFileName(extension=".mrc"))) origin = Transform() sr = self.inputSetOfTiltSeries.get().getSamplingRate() origin.setShifts(xDim / -2. * sr, yDim / -2. * sr, zDim / -2 * sr) newTomogram.setOrigin(origin) # Set tomogram acquisition acquisition = TomoAcquisition() acquisition.setAngleMin(ts.getFirstItem().getTiltAngle()) acquisition.setAngleMax(ts[ts.getSize()].getTiltAngle()) acquisition.setStep(self.getAngleStepFromSeries(ts)) newTomogram.setAcquisition(acquisition) outputSetOfTomograms.append(newTomogram) outputSetOfTomograms.update(newTomogram) outputSetOfTomograms.write() self._store()
def _visualize(self, obj, **args): """ Visualize any saved pdb and map if none were saved show the input files. """ _inputVolFlag = False _inputPDBFlag = False directory = self.protocol._getExtraPath() fnCmd = os.path.abspath(self.protocol._getTmpPath("chimera_output.cxc")) f = open(fnCmd, 'w') f.write('cd %s\n' % os.getcwd()) counter = 0 # Find all saved maps and pdbs from protocl. If none # are found show the input files to the protocol for filename in sorted(os.listdir(directory)): if filename.endswith(".mrc"): _inputVolFlag = True counter += 1 volFileName = os.path.join(directory, filename) vol = Volume() vol.setFileName(volFileName) # fix mrc header ccp4header = Ccp4Header(volFileName, readHeader=True) sampling = ccp4header.computeSampling() origin = Transform() shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) f.write("open %s\n" % volFileName) f.write("volume #%d style surface voxelSize %f\n" "volume #%d origin %0.2f,%0.2f,%0.2f\n" % (counter, sampling, counter, shifts[0], shifts[1], shifts[2])) # Set volume to translucent f.write("volume #%d transparency 0.5\n" % counter) for filename in os.listdir(directory): if filename.endswith(".pdb") or filename.endswith(".cif"): _inputPDBFlag = True path = os.path.join(directory, filename) f.write("open %s\n" % path) # If no pdbs or maps found use inputs to protocol if not _inputVolFlag: counter += 1 f.write("open %s \n" % os.path.abspath(self.protocol.inputVolume.get().getFileName())) # Set volume to translucent f.write("volume #%d transparency 0.5\n" % counter) if not _inputPDBFlag: f.write("open %s \n" % os.path.abspath(self.protocol.pdbFileToBeRefined.get().getFileName())) f.close() # run in the background Chimera.runProgram(chimera.getProgram(), fnCmd + "&") return []
def importSubTomogramsStep(self, pattern, samplingRate): """ Copy images matching the filename pattern Register other parameters. """ self.info("Using pattern: '%s'" % pattern) # Create a Volume template object subtomo = SubTomogram() subtomo.setSamplingRate(samplingRate) imgh = ImageHandler() self.subtomoSet = self._createSetOfSubTomograms() self.subtomoSet.setSamplingRate(samplingRate) # if self.importCoordinates.get(): # self.coords = [] # for coord3D in self.importCoordinates.get().iterCoordinates(): # self.coords.append(coord3D.clone()) # self.subtomoSet.setCoordinates3D(self.importCoordinates) self._parseAcquisitionData() for fileName, fileId in self.iterFiles(): x, y, z, n = imgh.getDimensions(fileName) if fileName.endswith('.map'): fileName += ':mrc' if fileName.endswith('.mrc') or fileName.endswith(':mrc'): if z == 1 and n != 1: zDim = n n = 1 else: zDim = z else: zDim = z origin = Transform() origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) subtomo.setOrigin(origin) # read origin from form newFileName = _getUniqueFileName(self.getPattern(), fileName) # newFileName = abspath(self._getVolumeFileName(fileName)) if fileName.endswith(':mrc'): fileName = fileName[:-4] createAbsLink(fileName, self._getExtraPath(newFileName)) if n == 1: self._addSubtomogram(subtomo, self._getExtraPath(fileName), self._getExtraPath(newFileName)) else: for index in range(1, n + 1): self._addSubtomogram(subtomo, self._getExtraPath(fileName), self._getExtraPath(newFileName), index=index)
def createOutput(self): volume = Volume() volume.setSamplingRate(self.sampling.get()) volume.setFileName(self._getVolName()) if self.vol: origin = Transform() origin.setShiftsTuple(self.shifts) volume.setOrigin(origin) self._defineOutputs(outputVolume=volume) if self.inputPdbData == self.IMPORT_OBJ: self._defineSourceRelation(self.pdbObj, volume)
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 generateOutputStackStep(self, tomoObjId): tomo = self.inputSetOfTomograms.get()[tomoObjId] tomoId = os.path.splitext(os.path.basename(tomo.getFileName()))[0] extraPrefix = self._getExtraPath(tomoId) outputProjectedSetOfTiltSeries = self.getOutputProjectedSetOfTiltSeries( ) newTs = tomoObj.TiltSeries(tsId=tomoId) newTs.copyInfo(tomo) newTs.setTsId(tomoId) # Add origin to output tilt-series origin = Transform() outputProjectedSetOfTiltSeries.append(newTs) tiltAngleList = self.getTiltAngleList() for index in range(self.getProjectionRange()): newTi = tomoObj.TiltImage() newTi.setTiltAngle(tiltAngleList[index]) newTi.setTsId(tomoId) newTi.setLocation( index + 1, os.path.join(extraPrefix, os.path.basename(tomo.getFileName()))) newTi.setSamplingRate( self.inputSetOfTomograms.get().getSamplingRate()) newTs.append(newTi) ih = ImageHandler() x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName()) newTs.setDim((x, y, z)) # Set origin to output tilt-series origin.setShifts( x / -2. * self.inputSetOfTomograms.get().getSamplingRate(), y / -2. * self.inputSetOfTomograms.get().getSamplingRate(), 0) newTs.setOrigin(origin) newTs.write(properties=False) outputProjectedSetOfTiltSeries.update(newTs) outputProjectedSetOfTiltSeries.updateDim() outputProjectedSetOfTiltSeries.write() self._store()
def createOutputStep(self): vol1 = self.vol1.get() volume = Volume() volume.setSamplingRate(vol1.getSamplingRate()) if vol1.getFileName().endswith('mrc'): origin = Transform() ccp4header = headers.Ccp4Header(vol1.getFileName(), readHeader=True) shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) volume.setOrigin(origin) volume.setFileName(self._getExtraPath("output_volume.mrc")) filename = volume.getFileName() if filename.endswith('.mrc') or filename.endswith('.map'): volume.setFileName(filename + ':mrc') self._defineOutputs(outputVolume=volume)
def _createCoordinate3D(self): # Tomogram associated to Coordinate3D self.Tomo = Tomogram() self.Tomo.setSamplingRate(1) self.Tomo.setLocation(self.TomogramPath) x, y, z = self.Tomo.getDim() origin = Transform() origin.setShifts(x / -2., y / -2., z / -2.) self.Tomo.setOrigin(origin) # Coordinate3D self.coord = Coordinate3D() self.coord.setBoxSize(32) self.coord.setVolume(self.Tomo) self.coord.setPosition(0, 0, 0, const.BOTTOM_LEFT_CORNER) self.coord.setMatrix(np.eye(4))
def createSetOfParticles(self, setPartSqliteName, partFn, doCtf=False): # create a set of particles self.partSet = SetOfParticles(filename=setPartSqliteName) self.partSet.setAlignment(ALIGN_PROJ) self.partSet.setAcquisition( Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000)) self.partSet.setSamplingRate(samplingRate) self.partSet.setHasCTF(True) aList = [np.array(m) for m in mList] #defocus=15000 + 5000* random.random() for i, a in enumerate(aList): p = Particle() if doCtf: defocusU = defocusList[i] #+500. defocusV = defocusList[i] ctf = CTFModel(defocusU=defocusU, defocusV=defocusV, defocusAngle=defocusAngle[i]) ctf.standardize() p.setCTF(ctf) p.setLocation(i + 1, partFn) p.setTransform(Transform(a)) self.partSet.append(p) self.partSet.write()
def createOutputStep(self): vol = Volume() vol.setLocation(self._getOutputVol()) sampling = self.inputVolumes.get().getSamplingRate() vol.setSamplingRate(sampling) # ccp4header = Ccp4Header(self._getOutputVol(), readHeader=True) t = Transform() x, y, z = ccp4header.getOrigin() # origin output vol # coordinates t.setShifts(x, y, z) vol.setOrigin(t) # self._defineOutputs(outputVolume=vol) self._defineSourceRelation(self.inputVolumes, self.outputVolume)
def createOutputStep(self): Ts = self.inputReference.get().getSamplingRate() vols = [] idx = 1 for vol in self._iterInputVolumes(): outVol = Volume() fnOutVol = self._getExtraPath("vol%02d.mrc" % idx) outVol.setLocation(fnOutVol) outVol.setObjComment(vol.getObjComment()) #set transformation matrix fhInputTranMat = self._getExtraPath( 'transformation-matrix_vol%06d.txt' % idx) transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) vols.append(outVol) # Set the sampling rate in the mrc header self.runJob("xmipp_image_header", "-i %s --sampling_rate %f" % (fnOutVol, Ts)) idx += 1 if len(vols) > 1: volSet = self._createSetOfVolumes() volSet.setSamplingRate(Ts) for vol in vols: volSet.append(vol) outputArgs = {'outputVolumes': volSet} else: vols[0].setSamplingRate(Ts) outputArgs = {'outputVolume': vols[0]} self._defineOutputs(**outputArgs) if len(vols) > 1: for pointer in self.inputVolumes: self._defineSourceRelation(pointer, outputArgs['outputVolumes']) else: for pointer in self.inputVolumes: self._defineSourceRelation(pointer, outputArgs['outputVolume'])
def createOutputStep(self): """ Copy the PDB structure and register the output object. """ # Check vol and pdb files directory = self._getExtraPath() for filename in sorted(os.listdir(directory)): if filename.endswith(".mrc"): volFileName = os.path.join(directory, filename) vol = Volume() vol.setFileName(volFileName) # fix mrc header ccp4header = Ccp4Header(volFileName, readHeader=True) sampling = ccp4header.computeSampling() origin = Transform() shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) vol.setOrigin(origin) vol.setSamplingRate(sampling) keyword = filename.split(".mrc")[0] kwargs = {keyword: vol} self._defineOutputs(**kwargs) if filename.endswith(".pdb") or filename.endswith(".cif"): path = os.path.join(directory, filename) pdb = AtomStruct() pdb.setFileName(path) if filename.endswith(".cif"): keyword = filename.split(".cif")[0].replace(".", "_") else: keyword = filename.split(".pdb")[0].replace(".", "_") kwargs = {keyword: pdb} self._defineOutputs(**kwargs) # upodate config file flag enablebundle # so scipionwrite is disabled config = configparser.ConfigParser() config.read(self._getExtraPath(CHIMERA_CONFIG_FILE)) config.set('chimerax', 'enablebundle', 'False') with open(self._getExtraPath(CHIMERA_CONFIG_FILE), 'w') as configfile: config.write(configfile)
def importTomogramsStep(self, pattern, samplingRate): """ Copy images matching the filename pattern Register other parameters. """ self.info("Using pattern: '%s'" % pattern) # Create a Volume template object tomo = Tomogram() tomo.setSamplingRate(samplingRate) imgh = ImageHandler() tomoSet = self._createSetOfTomograms() tomoSet.setSamplingRate(samplingRate) self._parseAcquisitionData() for fileName, fileId in self.iterFiles(): x, y, z, n = imgh.getDimensions(fileName) if fileName.endswith('.mrc') or fileName.endswith('.map'): fileName += ':mrc' if z == 1 and n != 1: zDim = n n = 1 else: zDim = z else: zDim = z origin = Transform() if self.setOrigCoord.get(): origin.setShiftsTuple(self._getOrigCoord()) else: origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) tomo.setOrigin(origin) # read origin from form newFileName = _getUniqueFileName(self.getPattern(), fileName.split(':')[0]) tsId = removeExt(newFileName) tomo.setTsId(tsId) if fileName.endswith(':mrc'): fileName = fileName[:-4] createAbsLink(fileName, abspath(self._getExtraPath(newFileName))) tomo.setAcquisition(self._extractAcquisitionParameters(fileName)) if n == 1: # One volume per file tomo.cleanObjId() tomo.setFileName(self._getExtraPath(newFileName)) tomoSet.append(tomo) else: # A stack of volumes per file (not common) for index in range(1, n + 1): tomo.cleanObjId() tomo.setLocation(index, self._getExtraPath(newFileName)) tomoSet.append(tomo) self._defineOutputs(outputTomograms=tomoSet)
def rowToAlignment(alignmentRow, samplingRate): """ Return an Transform object representing the Alignment from a given parFile row. :param alignmentRow: input row object :param samplingRate: input pixel size :return Transform object """ angles = np.zeros(3) shifts = np.zeros(3) alignment = Transform() # PSI THETA PHI SHX SHY angles[0] = float(alignmentRow.get('PSI')) angles[1] = float(alignmentRow.get('THETA')) angles[2] = float(alignmentRow.get('PHI')) # shifts are converted from Angstroms to px shifts[0] = float(alignmentRow.get('SHX')) / samplingRate shifts[1] = float(alignmentRow.get('SHY')) / samplingRate M = matrixFromGeometry(shifts, angles) alignment.setMatrix(M) return alignment
def createOutputStep(self): inputVol = self.inputStructure.get() samplingRate = inputVol.getSamplingRate() volume = Volume() volume.setFileName(self._getExtraPath("pseudoatoms_approximation.vol")) volume.setSamplingRate(samplingRate) x, y, z = volume.getDim() xv, yv, zv = inputVol.getOrigin(force=True).getShifts() t = Transform() t.setShifts((x / 2. * samplingRate) - xv, (y / 2. * samplingRate) - yv, (z / 2. * samplingRate) - zv) volume.setOrigin(inputVol.getOrigin()) self._defineOutputs(outputVolume=volume) self._defineSourceRelation(self.inputStructure.get(), volume) pdb = AtomStruct(self._getPath('pseudoatoms.pdb'), pseudoatoms=True) pdb.setVolume(volume) pdb.setOrigin(t) self.createChimeraScript(inputVol, pdb) self._defineOutputs(outputPdb=pdb) self._defineSourceRelation(self.inputStructure, pdb)
def _testInv(matrix): matrix = np.array(matrix) a = Transform(matrix) row1 = md.Row() convert.alignmentToRow(a, row1, alignType=ALIGN_2D) row2 = None row3 = md.Row() convert.alignmentToRow(a, row3, alignType=ALIGN_PROJ) return row1, row2, row3
def rowToAlignment(alignmentRow, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ is2D = alignType == ALIGN_2D inverseTransform = True # alignType == em.ALIGN_PROJ alignment = Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) angles[2] = alignmentRow.get('ANGLE_PSI') shifts[0] = alignmentRow.get('SHIFTX') shifts[1] = alignmentRow.get('SHIFTY') if not is2D: angles[0] = alignmentRow.get('ANGLE_PHI') angles[1] = alignmentRow.get('ANGLE_THE') M = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(M) return alignment
def createOutputStep(self): input = self.input.get() imgSetOut = self._createSetOfParticles() imgSetOut.setSamplingRate(input.getSamplingRate()) imgSetOut.setAlignmentProj() for i, subtomo in enumerate(input.iterItems()): idx = subtomo.getObjId() p = Particle() p.setLocation(ih._convertToLocation((i+1, self._getExtraPath("projections.mrcs")))) p._subtomogramID = String(idx) if type(subtomo) == SubTomogram: if subtomo.hasCoordinate3D(): coord = Coordinate() coord.setX(subtomo.getCoordinate3D().getX(const.BOTTOM_LEFT_CORNER)) coord.setY(subtomo.getCoordinate3D().getY(const.BOTTOM_LEFT_CORNER)) p.setCoordinate(coord) p.setClassId(subtomo.getClassId()) if subtomo.hasTransform(): transform = Transform() transform.setMatrix(subtomo.getTransform().getMatrix()) p.setTransform(transform) imgSetOut.append(p) imgSetOut.setObjComment(self.getSummary(imgSetOut)) self._defineOutputs(outputParticles=imgSetOut) self._defineSourceRelation(self.input, imgSetOut) if self.radAvg.get(): avgFile = self._getExtraPath("average.xmp") imgh = ih() avgImage = imgh.computeAverage(imgSetOut) avgImage.write(avgFile) avg = Particle() avg.setLocation(1, avgFile) avg.copyInfo(imgSetOut) self._defineOutputs(outputAverage=avg) self._defineSourceRelation(self.input, avg)
def createOutputStep(self): outSubtomos = self._createSetOfSubTomograms() outSubtomos.setSamplingRate(self.second_subtomos.getSamplingRate()) outSubtomos.setCoordinates3D(self.second_subtomos.getCoordinates3D()) # if self.second_subtomos.getAcquisition() is not None: # acquisition = TomoAcquisition() # acquisition.setAngleMin(self.second_subtomos.getFirstItem().getAcquisition().getAngleMin()) # acquisition.setAngleMax(self.second_subtomos.getFirstItem().getAcquisition().getAngleMax()) # acquisition.setStep(self.second_subtomos.getFirstItem().getAcquisition().getStep()) # outSubtomos.setAcquisition(acquisition) for ids, inSubtomo in enumerate(self.second_subtomos.iterItems()): subtomogram = SubTomogram() subtomogram.setObjId(self.second_matrices[ids][0]) subtomogram.setLocation(inSubtomo.getLocation()) subtomogram.setCoordinate3D(inSubtomo.getCoordinate3D()) subtomogram.setTransform(Transform(self.second_matrices[ids][1])) subtomogram.setVolName(inSubtomo.getVolName()) outSubtomos.append(subtomogram) self._defineOutputs(outputSetOfSubtomogram=outSubtomos) self._defineSourceRelation(self.firstAverage, outSubtomos) self._defineSourceRelation(self.secondAverage, outSubtomos)
def setParticleTransform(self, particle, row): """ Set the transform values from the row. """ if ((self._alignType == ALIGN_NONE) or not row.hasAnyColumn(self.ALIGNMENT_LABELS)): self.setParticleTransform = self.__setParticleTransformNone else: # Ensure the Transform object exists self._angles = np.zeros(3) self._shifts = np.zeros(3) particle.setTransform(Transform()) if self._alignType == ALIGN_2D: self.setParticleTransform = self.__setParticleTransform2D elif self._alignType == ALIGN_PROJ: self.setParticleTransform = self.__setParticleTransformProj else: raise TypeError("Unexpected alignment type: %s" % self._alignType) # Call again the modified function self.setParticleTransform(particle, row)
def launchTest(self, fileKey, mList, alignType=None, **kwargs): """ Helper function to launch similar alignment tests given the EMX transformation matrix. Params: fileKey: the file where to grab the input stack images. mList: the matrix list of transformations (should be the same length of the stack of images) """ print("\n") print("*" * 80) print("* Launching test: ", fileKey) print("*" * 80) is2D = alignType == ALIGN_2D if fileKey == 'alignShiftRotExp': # relion requires mrcs stacks origFn = self.dataset.getFile(fileKey) stackFn = replaceExt(origFn, ".mrcs") createLink(origFn, stackFn) else: stackFn = self.dataset.getFile(fileKey) partFn1 = self.getOutputPath(fileKey + "_particles1.sqlite") mdFn = self.getOutputPath(fileKey + "_particles.star") partFn2 = self.getOutputPath(fileKey + "_particles2.sqlite") if self.IS_ALIGNMENT: outputFn = self.getOutputPath(fileKey + "_output.mrcs") outputFnRelion = self.getOutputPath(fileKey + "_output") goldFn = self.dataset.getFile(fileKey + '_Gold_output_relion.mrcs') else: outputFn = self.getOutputPath(fileKey + "_output.vol") goldFn = self.dataset.getFile("reconstruction/gold/" + fileKey + '_Gold_rln_output.vol') if PRINT_FILES: print("BINARY DATA: ", stackFn) print("SET1: ", partFn1) print(" MD: ", mdFn) print("SET2: ", partFn2) print("OUTPUT: ", outputFn) print("GOLD: ", goldFn) if alignType == ALIGN_2D or alignType == ALIGN_PROJ: partSet = SetOfParticles(filename=partFn1) else: partSet = SetOfVolumes(filename=partFn1) partSet.setAlignment(alignType) acq = Acquisition(voltage=300, sphericalAberration=2, amplitudeContrast=0.1, magnification=60000) og = OpticsGroups.create(rlnMtfFileName="mtfFile1.star", rlnImageSize=128) partSet.setSamplingRate(1.0) partSet.setAcquisition(acq) og.toImages(partSet) # Populate the SetOfParticles with images # taken from images.mrc file # and setting the previous alignment parameters aList = [np.array(m) for m in mList] for i, a in enumerate(aList): p = Particle() p.setLocation(i + 1, stackFn) p.setTransform(Transform(a)) partSet.append(p) # Write out the .sqlite file and check that are correctly aligned print("Partset", partFn1) partSet.printAll() partSet.write() # Convert to a Xmipp metadata and also check that the images are # aligned correctly if alignType == ALIGN_2D or alignType == ALIGN_PROJ: starWriter = convert.createWriter() starWriter.writeSetOfParticles(partSet, mdFn, alignType=alignType) partSet2 = SetOfParticles(filename=partFn2) else: convert.writeSetOfVolumes(partSet, mdFn, alignType=alignType) partSet2 = SetOfVolumes(filename=partFn2) # Let's create now another SetOfImages reading back the written # Xmipp metadata and check one more time. partSet2.copyInfo(partSet) if alignType == ALIGN_2D or alignType == ALIGN_PROJ: convert.readSetOfParticles(mdFn, partSet2, alignType=alignType) else: convert.readSetOfParticles(mdFn, partSet2, rowToFunc=convert.rowToVolume, alignType=alignType) partSet2.write() if PRINT_MATRIX: for i, img in enumerate(partSet2): m1 = aList[i] m2 = img.getTransform().getMatrix() print("-" * 5) print(img.getFileName(), img.getIndex()) print('m1:\n', m1, convert.geometryFromMatrix(m1, False)) print('m2:\n', m2, convert.geometryFromMatrix(m2, False)) self.assertTrue(np.allclose(m1, m2, rtol=1e-2)) # Launch apply transformation and check result images runRelionProgram(self.CMD % locals()) if SHOW_IMAGES: runRelionProgram('scipion show %(outputFn)s' % locals()) if os.path.exists(goldFn): self.assertTrue( ImageHandler().compareData(goldFn, outputFn, tolerance=0.001), "Different data files:\n>%s\n<%s" % (goldFn, outputFn)) if CLEAN_IMAGES: cleanPath(outputFn)
def _visualize(self, obj, **args): # THe input map or pdb may be a parameter from the protocol # or from the parent protocol. dim = 150. sampling = 1. _inputVol = None directory = self.protocol._getExtraPath() try: try: if self.protocol.inputVolume.get() is not None: _inputVol = self.protocol.inputVolume.get() elif self.protocol.pdbFileToBeRefined.get().getVolume( ) is not None: _inputVol = self.protocol.pdbFileToBeRefined.get( ).getVolume() elif self.protocol.inputVolumes[0] is not None: _inputVol = self.protocol.inputVolumes[0].get() except: output3DMapList = [] for filename in sorted(os.listdir(directory)): if filename.endswith(".mrc"): output3DMapList.append(filename.split('.')[0]) output3DMap = str(output3DMapList[0]) if len(output3DMap) > 0: _inputVol = self.protocol.output3DMap except: # TODO: I do not know if we still need this part # Remark that inputProtocol does not longer exist, it has been replaced by inputProtocolDict # Compare with the previous code, specially the alternative directory for item in list(self.protocol.inputProtocolDict().values()): if item.hasAttribute( 'inputVolume') and item.inputVolume.get() is not None: _inputVol = item.inputVolume.get() break elif item.hasAttribute('pdbFileToBeRefined') and \ item.pdbFileToBeRefined.get().getVolume() is not None: _inputVol = item.pdbFileToBeRefined.get().getVolume() break # directory = item._getExtraPath() if _inputVol is not None: dim = _inputVol.getDim()[0] sampling = _inputVol.getSamplingRate() bildFileName = self.protocol._getTmpPath("axis_output.bild") Chimera.createCoordinateAxisFile(dim, bildFileName=bildFileName, sampling=sampling) fnCmd = self.protocol._getTmpPath("chimera_output.cxc") f = open(fnCmd, 'w') # change to workingDir # If we do not use cd and the project name has an space # the protocol fails even if we pass absolute paths f.write('cd %s\n' % os.getcwd()) f.write("open %s\n" % bildFileName) f.write("cofr 0,0,0\n") # set center of coordinates inputVolFileName = '' counter = 2 if _inputVol is not None: # In case we have PDBs only, _inputVol is None: self.visInputVolume(f, _inputVol, counter) else: counter = 1 if (self.protocol.hasAttribute("inputVolume2") and\ self.protocol.inputVolume2.get() is not None): counter += 1 _inputVol2 = self.protocol.inputVolume2.get() self.visInputVolume(f, _inputVol2, counter) for filename in sorted(os.listdir(directory)): if filename.endswith(".mrc") and filename != inputVolFileName: counter += 1 volFileName = os.path.join(directory, filename) vol = Volume() vol.setFileName(volFileName) # fix mrc header ccp4header = Ccp4Header(volFileName, readHeader=True) sampling = ccp4header.computeSampling() origin = Transform() shifts = ccp4header.getOrigin() origin.setShiftsTuple(shifts) f.write("open %s\n" % volFileName) f.write("volume #%d style surface voxelSize %f\n" "volume #%d origin %0.2f,%0.2f,%0.2f\n" % (counter, sampling, counter, shifts[0], shifts[1], shifts[2])) f.write("volume #%d level %0.3f\n" % (counter, 0.001)) for filename in os.listdir(directory): if filename.endswith(".pdb") or filename.endswith(".cif"): path = os.path.join(directory, filename) # f.write("open %s\n" % os.path.abspath(path)) f.write("open %s\n" % path) f.close() # run in the background Chimera.runProgram(Chimera.getProgram(), fnCmd + "&") return []
def extractunitCell(self, sym, offset=0, cropZ=False): # FlexProtConvertToPseudoAtoms = Domain.importFromPlugin("continuousflex.protocols", # "FlexProtConvertToPseudoAtoms", # doRaise=True) # NMA_MASK_THRE = Domain.importFromPlugin("continuousflex.protocols.pdb.protocol_pseudoatoms_base", # "NMA_MASK_THRE", # doRaise=True) """ extract unit cell from icosahedral phantom using xmipp_i2 symmetry """ # create phantom (3D map) _samplingRate = 1.34 _, outputFile1 = mkstemp(suffix=".mrc") command = "xmipp_phantom_create " args = " -i %s" % self.filename[sym] args += " -o %s" % outputFile1 runJob(None, command, args, env=Plugin.getEnviron()) ccp4header = Ccp4Header(outputFile1, readHeader=True) x, y, z = ccp4header.getDims() t = Transform() if cropZ: _, outputFile2 = mkstemp(suffix=".mrc") args = "-i %s -o %s" % (outputFile1, outputFile2) args += " --corners " args += " %d " % (-x / 2.) args += " %d " % (-y / 2.) args += " %d " % (0.) args += " %d " % (+x / 2.) args += " %d " % (+y / 2.) args += " %d " % (+z / 2.) runJob(None, "xmipp_transform_window", args, env=Plugin.getEnviron()) t.setShifts(0, 0, 0) outputFile = outputFile2 ccp4header = Ccp4Header(outputFile2, readHeader=True) else: t.setShifts(0, 0, 0) outputFile = outputFile1 ccp4header.setSampling(_samplingRate) ccp4header.setOrigin(t.getShifts()) ccp4header.writeHeader() # import volume if cropZ: args = { 'filesPath': outputFile, 'filesPattern': '', 'samplingRate': _samplingRate, 'copyFiles': True, 'setOrigCoord': True, 'x': 90. * _samplingRate, 'y': 90. * _samplingRate, 'z': 0. # x, y, z in Angstroms } else: args = { 'filesPath': outputFile, 'filesPattern': '', 'samplingRate': _samplingRate, 'copyFiles': True, 'setDefaultOrigin': False, } prot = self.newProtocol(ProtImportVolumes, **args) prot.setObjLabel('import volume(%s)' % XMIPP_SYM_NAME[sym]) self.launchProtocol(prot) # execute protocol extract unitCell args = { 'inputVolumes': prot.outputVolume, 'symmetryGroup': sym, 'symmetryOrder': self.symOrder, 'innerRadius': self.innerRadius, 'outerRadius': self.outerRadius, 'expandFactor': .2, 'offset': offset } prot = self.newProtocol(XmippProtExtractUnit, **args) prot.setObjLabel('extract unit cell') self.launchProtocol(prot) # check results ih = ImageHandler() xdim, ydim, zdim, ndim = \ ih.getDimensions(prot.outputVolume.getFileName()) self.assertTrue(abs(xdim - self.box[sym][0]) < 2) self.assertTrue(abs(ydim - self.box[sym][1]) < 2) self.assertTrue(abs(zdim - self.box[sym][2]) < 2) # create pdb fileoutput # args = {'inputStructure': prot.outputVolume, # 'maskMode': NMA_MASK_THRE, # 'maskThreshold': 0.5, # 'pseudoAtomRadius': 1.5 # } # prot = self.newProtocol(FlexProtConvertToPseudoAtoms, **args) # prot.setObjLabel('get pdb') # self.launchProtocol(prot) # check results # filenamePdb = prot._getPath('pseudoatoms.pdb') # self.assertTrue(os.path.isfile(filenamePdb)) # delete temporary files os.remove(self.filename[sym]) os.remove(outputFile)
def _updateItem(self, item, index): item.setLocation(index, self._getFileName('particlesAligned')) item.setTransform(Transform())
def test_forward_backwards(self): """convert transformation matrix to xmipp and back""" mList = [ [ [0.71461016, 0.63371837, -0.29619813, 1.], # a1 [-0.61309201, 0.77128059, 0.17101008, 2.], [0.33682409, 0.059391174, 0.93969262, 3.], [0, 0, 0, 1.] ], [ [0., 0., -1., 0.], # a2 [0., 1., 0., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.] ], [ [0., 1., 0., 0.], # a3 [0., 0., 1., 0.], [1., 0., 0., 0.], [0., 0., 0., 1.] ], [ [0.22612257, 0.82379508, -0.51983678, 0.], # a4 [-0.88564873, 0.39606407, 0.24240388, 0.], [0.40557978, 0.40557978, 0.81915206, 0.], [0., 0., 0., 1.] ], [ [-0.78850311, -0.24329656, -0.56486255, 0.], # a5 [0.22753462, -0.96866286, 0.099600501, 0.], [-0.57139379, -0.049990479, 0.81915206, 0.], [0., 0., 0., 1.] ], [ [1.0, 0.0, 0.0, 0.0], # a6 [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0] ], [ [0., 0., -1., 0.], # a7 [-1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 0., 1.] ] ] aList = [np.array(m) for m in mList] rowa = md.Row() rowb = md.Row() labelList = [ md.RLN_ORIENT_ROT, md.RLN_ORIENT_TILT, md.RLN_ORIENT_PSI, md.RLN_ORIENT_ORIGIN_X, md.RLN_ORIENT_ORIGIN_Y, md.RLN_ORIENT_ORIGIN_Z ] for i, a in enumerate(aList): a = Transform(aList[i]) convert.alignmentToRow(a, rowa, ALIGN_PROJ) b = convert.rowToAlignment(rowa, ALIGN_PROJ) convert.alignmentToRow(b, rowb, ALIGN_PROJ) # same two matrices self.assertTrue( np.allclose(a.getMatrix(), b.getMatrix(), rtol=1e-2)) for label in labelList: auxBtilt = rowb.getValue(label) auxAtilt = rowa.getValue(label) # same two rows self.assertAlmostEqual(auxBtilt, auxAtilt, places=3) b = convert.rowToAlignment(rowa, ALIGN_PROJ) convert.alignmentToRow(b, rowb, ALIGN_PROJ) aMatrix = a.getMatrix() # aMatrix[0,:] *= -1; aMatrix[2,:] *= -1; # same two matrices with flip print("\naMatrix: \n", aMatrix, "\nbMatrix: \n", b.getMatrix()) self.assertTrue(np.allclose(aMatrix, b.getMatrix(), rtol=1e-2))
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'))