Skip to content

Commit

Permalink
Merge pull request #227 from scipion-em/v320
Browse files Browse the repository at this point in the history
V3.2.0
  • Loading branch information
fonsecareyna82 authored Nov 29, 2023
2 parents b88ba4b + 4c004a6 commit 9109cda
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 99 deletions.
9 changes: 8 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
3.3.0:
- Fixing a bug with odd even in tomogram reconstruction
- Tolerating errors in alignment
- Making tomogram reconstruction error tolerant
- Making tilt series alignment error tolerant
- Adding possible outputs in xray eraser
- Removing visible parameters with the intepolation is not applied
3.2.0:
- add more possible outputs
- fixing ctf after aretomo
Expand Down Expand Up @@ -79,4 +86,4 @@
- fix betterradius param in gold eraser tab of the fid alignment
- add missing pix size in fid align
- fix minpacing in gold picker
- add missing taper arg for newstack
- add missing taper arg for newstack
102 changes: 56 additions & 46 deletions imod/protocols/protocol_applyTransformationMatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ def _defineParams(self, form):

# -------------------------- INSERT steps functions -----------------------
def _insertAllSteps(self):
self._failedTs = []

for ts in self.inputSetOfTiltSeries.get():
self._insertFunctionStep(self.generateTransformFileStep, ts.getObjId())
self._insertFunctionStep(self.computeAlignmentStep, ts.getObjId())
self._insertFunctionStep(self.generateOutputStackStep, ts.getObjId())
self._insertFunctionStep(self.createOutputFailedSet, ts.getObjId())
self._insertFunctionStep(self.closeOutputSetsStep)

# --------------------------- STEPS functions ------------------------------
Expand All @@ -90,6 +93,7 @@ def generateTransformFileStep(self, tsObjId):
os.path.join(extraPrefix,
ts.getFirstItem().parseFileName(extension=".xf")))

@ProtImodBase.tryExceptDecorator
def computeAlignmentStep(self, tsObjId):
ts = self.inputSetOfTiltSeries.get()[tsObjId]
tsId = ts.getTsId()
Expand Down Expand Up @@ -142,59 +146,65 @@ def computeAlignmentStep(self, tsObjId):
Plugin.runImod(self, 'newstack', argsAlignment % paramsAlignment)

def generateOutputStackStep(self, tsObjId):
output = self.getOutputInterpolatedSetOfTiltSeries(self.inputSetOfTiltSeries.get())

ts = self.inputSetOfTiltSeries.get()[tsObjId]
tsId = ts.getTsId()
extraPrefix = self._getExtraPath(tsId)
binning = self.binning.get()

newTs = TiltSeries(tsId=tsId)
newTs.copyInfo(ts)
newTs.setInterpolated(True)
acq = newTs.getAcquisition()
acq.setTiltAxisAngle(0.) # 0 because TS is aligned
newTs.setAcquisition(acq)
output.append(newTs)

if binning > 1:
newTs.setSamplingRate(ts.getSamplingRate() * binning)

ih = ImageHandler()

index = 1
for tiltImage in ts:
if tiltImage.isEnabled():
newTi = TiltImage()
newTi.copyInfo(tiltImage, copyId=False, copyTM=False)
acq = tiltImage.getAcquisition()
acq.setTiltAxisAngle(0.)
newTi.setAcquisition(acq)
newTi.setLocation(index, (os.path.join(extraPrefix, tiltImage.parseFileName())))
if self.applyToOddEven(ts):
locationOdd = index, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_ODD_NAME))
locationEven = index, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_EVEN_NAME))
newTi.setOddEven([ih.locationToXmipp(locationOdd), ih.locationToXmipp(locationEven)])
else:
newTi.setOddEven([])

index += 1
if binning > 1:
newTi.setSamplingRate(tiltImage.getSamplingRate() * binning)
newTs.append(newTi)

ih = ImageHandler()
x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName())
newTs.setDim((x, y, z))

newTs.write(properties=False)
output.update(newTs)
output.write()
self._store()
outputLocation = os.path.join(extraPrefix, ts.getFirstItem().parseFileName())

if os.path.exists(outputLocation):
output = self.getOutputInterpolatedSetOfTiltSeries(self.inputSetOfTiltSeries.get())

binning = self.binning.get()

newTs = TiltSeries(tsId=tsId)
newTs.copyInfo(ts)
newTs.setInterpolated(True)
acq = newTs.getAcquisition()
acq.setTiltAxisAngle(0.) # 0 because TS is aligned
newTs.setAcquisition(acq)
output.append(newTs)

if binning > 1:
newTs.setSamplingRate(ts.getSamplingRate() * binning)

ih = ImageHandler()

index = 1
for tiltImage in ts:
if tiltImage.isEnabled():
newTi = TiltImage()
newTi.copyInfo(tiltImage, copyId=False, copyTM=False)
acq = tiltImage.getAcquisition()
acq.setTiltAxisAngle(0.)
newTi.setAcquisition(acq)
newTi.setLocation(index, outputLocation)
if self.applyToOddEven(ts):
locationOdd = index, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_ODD_NAME))
locationEven = index, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_EVEN_NAME))
newTi.setOddEven([ih.locationToXmipp(locationOdd), ih.locationToXmipp(locationEven)])
else:
newTi.setOddEven([])

index += 1
if binning > 1:
newTi.setSamplingRate(tiltImage.getSamplingRate() * binning)
newTs.append(newTi)

ih = ImageHandler()
x, y, z, _ = ih.getDimensions(newTs.getFirstItem().getFileName())
newTs.setDim((x, y, z))

newTs.write(properties=False)
output.update(newTs)
output.write()
self._store()

def closeOutputSetsStep(self):
self.InterpolatedTiltSeries.setStreamState(Set.STREAM_CLOSED)
self.InterpolatedTiltSeries.write()
for _, output in self.iterOutputAttributes():
output.setStreamState(Set.STREAM_CLOSED)
output.write()
self._store()

# --------------------------- INFO functions ------------------------------
Expand Down
22 changes: 17 additions & 5 deletions imod/protocols/protocol_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def wrapper(self, tsId, *args):
return wrapper

def convertInputStep(self, tsObjId, generateAngleFile=True,
imodInterpolation=True, doSwap=False):
imodInterpolation=True, doSwap=False, oddEven=False):
"""
:param tsObjId: Tilt series identifier
Expand Down Expand Up @@ -140,18 +140,30 @@ def convertInputStep(self, tsObjId, generateAngleFile=True,
# Use IMOD newstack interpolation
if firstItem.hasTransform():
# Generate transformation matrices file
outputTmFileName = os.path.join(tmpPrefix,
firstItem.parseFileName(extension=".xf"))
outputTmFileName = os.path.join(tmpPrefix, firstItem.parseFileName(extension=".xf"))
utils.formatTransformFile(ts, outputTmFileName)

argsAlignment, paramsAlignment = self.getBasicNewstackParams(ts,
def applyNewStack(outputTsFileName, fnIn):

argsAlignment, paramsAlignment = self.getBasicNewstackParams(ts,
outputTsFileName,
inputTsFileName=fnIn,
xfFile=outputTmFileName,
firstItem=firstItem,
doSwap=doSwap)
Plugin.runImod(self, 'newstack', argsAlignment % paramsAlignment)

self.info("Interpolating tilt series %s with imod" % tsId)
Plugin.runImod(self, 'newstack', argsAlignment % paramsAlignment)
applyNewStack(outputTsFileName, None)

if oddEven:
fnOdd = ts.getOddFileName()
fnEven = ts.getEvenFileName()

outputOddTsFileName = os.path.join(tmpPrefix, tsId+EXT_MRCS_TS_EVEN_NAME)
outputEvenTsFileName = os.path.join(tmpPrefix, tsId+EXT_MRCS_TS_ODD_NAME)
applyNewStack(outputOddTsFileName, fnOdd)
applyNewStack(outputEvenTsFileName, fnEven)

else:
self.info("Linking tilt series %s" % tsId)
Expand Down
20 changes: 18 additions & 2 deletions imod/protocols/protocol_ctfCorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _defineParams(self, form):
def _insertAllSteps(self):
self._failedTs = []

for ts in self.inputSetOfTiltSeries.get():
for ts in self.inputSetOfTiltSeries.get().iterItems():
self._insertFunctionStep(self.convertInputStep, ts.getObjId())
self._insertFunctionStep(self.generateDefocusFile, ts.getObjId())
self._insertFunctionStep(self.ctfCorrection, ts.getObjId())
Expand All @@ -150,7 +150,14 @@ def convertInputStep(self, tsObjId, **kwargs):
# Considering swapXY is required to make tilt axis vertical
super().convertInputStep(tsObjId, doSwap=True)

def tsToProcess(self, tsObjId) -> bool:
tsId = self.inputSetOfTiltSeries.get()[tsObjId].getTsId()
ctfTomoSeries = self.getCtfTomoSeriesFromTsId(tsId)
return ctfTomoSeries

def generateDefocusFile(self, tsObjId):
if self.tsToProcess(tsObjId) is None:
return
ts = self.inputSetOfTiltSeries.get()[tsObjId]
tsId = ts.getTsId()

Expand All @@ -160,7 +167,6 @@ def generateDefocusFile(self, tsObjId):
defocusFilePath = self.getDefocusFileName(ts)

"""Generate defocus file"""

ctfTomoSeries = self.getCtfTomoSeriesFromTsId(tsId)
utils.generateDefocusIMODFileFromObject(ctfTomoSeries, defocusFilePath, inputTiltSeries=ts)

Expand All @@ -176,6 +182,8 @@ def getDefocusFileName(self, ts):

@ProtImodBase.tryExceptDecorator
def ctfCorrection(self, tsObjId):
if self.tsToProcess(tsObjId) is None:
return
ts = self.inputSetOfTiltSeries.get()[tsObjId]
tsId = ts.getTsId()
extraPrefix = self._getExtraPath(tsId)
Expand Down Expand Up @@ -228,6 +236,8 @@ def ctfCorrection(self, tsObjId):
Plugin.runImod(self, 'ctfphaseflip', argsCtfPhaseFlip % paramsCtfPhaseFlip)

def createOutputStep(self, tsObjId):
if self.tsToProcess(tsObjId) is None:
return
if tsObjId not in self._failedTs:
inputTs = self.inputSetOfTiltSeries.get()
output = self.getOutputSetOfTiltSeries(inputTs)
Expand Down Expand Up @@ -274,6 +284,11 @@ def createOutputStep(self, tsObjId):
output.write()
self._store()

def createOutputFailedSet(self, tsObjId):
if self.tsToProcess(tsObjId) is None:
return
super().createOutputFailedSet(tsObjId)

def closeOutputSetsStep(self):
for _, output in self.iterOutputAttributes():
output.setStreamState(Set.STREAM_CLOSED)
Expand All @@ -285,6 +300,7 @@ def getCtfTomoSeriesFromTsId(self, tsId):
for ctfTomoSeries in self.inputSetOfCtfTomoSeries.get():
if tsId == ctfTomoSeries.getTsId():
return ctfTomoSeries
return None

# --------------------------- INFO functions ------------------------------
def _warnings(self):
Expand Down
13 changes: 8 additions & 5 deletions imod/protocols/protocol_tomoReconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from tomo.objects import Tomogram

from .. import Plugin
from .protocol_base import ProtImodBase, EXT_MRC_ODD_NAME, EXT_MRC_EVEN_NAME
from .protocol_base import ProtImodBase, EXT_MRC_ODD_NAME, EXT_MRC_EVEN_NAME, EXT_MRCS_TS_EVEN_NAME, EXT_MRCS_TS_ODD_NAME


class ProtImodTomoReconstruction(ProtImodBase):
Expand Down Expand Up @@ -181,7 +181,10 @@ def _insertAllSteps(self):
# --------------------------- STEPS functions -----------------------------
def convertInputStep(self, tsObjId, **kwargs):
# Considering swapXY is required to make tilt axis vertical
super().convertInputStep(tsObjId, doSwap=True)
oddEvenFlag = False
if self.inputSetOfTiltSeries.get().hasOddEven():
oddEvenFlag = True
super().convertInputStep(tsObjId, doSwap=True, oddEven=oddEvenFlag)

@ProtImodBase.tryExceptDecorator
def computeReconstructionStep(self, tsObjId):
Expand Down Expand Up @@ -244,13 +247,13 @@ def getArgs():
oddEvenTmp = [[], []]

if self.applyToOddEven(ts):
oddFn = firstItem.getOdd().split('@')[1]
oddFn = os.path.join(tmpPrefix, tsId+EXT_MRCS_TS_ODD_NAME)
paramsTilt['InputProjections'] = oddFn
oddEvenTmp[0] = os.path.join(tmpPrefix, firstItem.parseFileName(extension="_odd.rec"))
paramsTilt['OutputFile'] = oddEvenTmp[0]

Plugin.runImod(self, 'tilt', argsTilt % paramsTilt)
evenFn = firstItem.getEven().split('@')[1]

evenFn = os.path.join(tmpPrefix, tsId+EXT_MRCS_TS_EVEN_NAME)
paramsTilt['InputProjections'] = evenFn
oddEvenTmp[1] = os.path.join(tmpPrefix, firstItem.parseFileName(extension="_even.rec"))
paramsTilt['OutputFile'] = oddEvenTmp[1]
Expand Down
29 changes: 17 additions & 12 deletions imod/protocols/protocol_tsNormalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,12 @@ def _defineParams(self, form):

# -------------------------- INSERT steps functions -----------------------
def _insertAllSteps(self):
self._failedTs = []

for ts in self.inputSetOfTiltSeries.get():
self._insertFunctionStep(self.convertInputStep, ts.getObjId())
self._insertFunctionStep(self.generateOutputStackStep, ts.getObjId())
self._insertFunctionStep(self.createOutputFailedSet, ts.getObjId())
self._insertFunctionStep(self.closeOutputSetsStep)

# --------------------------- STEPS functions -----------------------------
Expand All @@ -210,10 +213,8 @@ def convertInputStep(self, tsObjId, **kwargs):
super().convertInputStep(tsObjId, imodInterpolation=None,
generateAngleFile=False)

@ProtImodBase.tryExceptDecorator
def generateOutputStackStep(self, tsObjId):
output = self.getOutputSetOfTiltSeries(self.inputSetOfTiltSeries.get(),
self.binning.get())

ts = self.inputSetOfTiltSeries.get()[tsObjId]
tsId = ts.getTsId()

Expand All @@ -231,7 +232,8 @@ def generateOutputStackStep(self, tsObjId):

argsNewstack, paramsNewstack = self.getBasicNewstackParams(ts,
os.path.join(extraPrefix, firstItem.parseFileName()),
inputTsFileName=os.path.join(tmpPrefix, firstItem.parseFileName()),
inputTsFileName=os.path.join(tmpPrefix,
firstItem.parseFileName()),
xfFile=xfFile,
firstItem=firstItem,
binning=binning,
Expand Down Expand Up @@ -269,14 +271,16 @@ def generateOutputStackStep(self, tsObjId):

if self.applyToOddEven(ts):
oddFn = firstItem.getOdd().split('@')[1]
evenFn= firstItem.getEven().split('@')[1]
evenFn = firstItem.getEven().split('@')[1]
paramsNewstack['input'] = oddFn
paramsNewstack['output'] = os.path.join(extraPrefix, tsId+EXT_MRCS_TS_ODD_NAME)
paramsNewstack['output'] = os.path.join(extraPrefix, tsId + EXT_MRCS_TS_ODD_NAME)
Plugin.runImod(self, 'newstack', argsNewstack % paramsNewstack)
paramsNewstack['input'] = evenFn
paramsNewstack['output'] = os.path.join(extraPrefix, tsId+EXT_MRCS_TS_EVEN_NAME)
paramsNewstack['output'] = os.path.join(extraPrefix, tsId + EXT_MRCS_TS_EVEN_NAME)
Plugin.runImod(self, 'newstack', argsNewstack % paramsNewstack)

output = self.getOutputSetOfTiltSeries(self.inputSetOfTiltSeries.get(), self.binning.get())

newTs = tomoObj.TiltSeries(tsId=tsId)
newTs.copyInfo(ts)
output.append(newTs)
Expand All @@ -298,8 +302,8 @@ def generateOutputStackStep(self, tsObjId):

newTi.setAcquisition(tiltImage.getAcquisition())
if self.applyToOddEven(ts):
locationOdd = index + 1, (os.path.join(extraPrefix, tsId+EXT_MRCS_TS_ODD_NAME))
locationEven = index + 1, (os.path.join(extraPrefix, tsId+EXT_MRCS_TS_EVEN_NAME))
locationOdd = index + 1, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_ODD_NAME))
locationEven = index + 1, (os.path.join(extraPrefix, tsId + EXT_MRCS_TS_EVEN_NAME))
newTi.setOddEven([ih.locationToXmipp(locationOdd), ih.locationToXmipp(locationEven)])
else:
newTi.setOddEven([])
Expand All @@ -320,8 +324,9 @@ def generateOutputStackStep(self, tsObjId):
self._store()

def closeOutputSetsStep(self):
self.TiltSeries.setStreamState(Set.STREAM_CLOSED)
self.TiltSeries.write()
for _, output in self.iterOutputAttributes():
output.setStreamState(Set.STREAM_CLOSED)
output.write()
self._store()

# --------------------------- UTILS functions -----------------------------
Expand All @@ -345,7 +350,7 @@ def updateTM(self, newTi):

transform.setMatrix(matrix)
newTi.setTransform(transform)

return newTi

# --------------------------- INFO functions ------------------------------
Expand Down
Loading

0 comments on commit 9109cda

Please sign in to comment.