Source code for pyseg.protocols.protocol_import_starfile

# **************************************************************************
# *
# * Authors:     Estrella Fernandez Gimenez (me.fernandez@cnb.csic.es)
# *
# *  BCU, Centro Nacional de Biotecnologia, CSIC
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307  USA
# *
# *  All comments concerning this program package may be sent to the
# *  e-mail address 'scipion@cnb.csic.es'
# *
# **************************************************************************

from os.path import abspath, basename, dirname

from pyworkflow.protocol.params import PathParam, FloatParam
from pyworkflow.utils.path import createAbsLink, copyFile
from pwem.emlib.image import ImageHandler
from pwem.objects import Transform
from pwem.protocols import EMProtocol

from tomo.protocols import ProtTomoBase
from tomo.objects import SubTomogram

from ..convert import readStarfileRow, readStarfileHeader


[docs]class ProtPySegImportSubtomos(EMProtocol, ProtTomoBase): """ This protocol imports subtomograms from a STAR file generated by PySeg""" _label = 'import star file PySeg' # -------------------------- DEFINE param functions ---------------------- def _defineParams(self, form): form.addSection(label='Input') form.addParam('starFile', PathParam, label="Star file:", help='Select star file generated by Relion/PySeg for linking metadata to the subtomograms that ' 'will be imported to Scipion.') form.addParam('samplingRate', FloatParam, label="Sampling rate: ") # --------------------------- STEPS functions ------------------------------ def _insertAllSteps(self): self._insertFunctionStep('importSubtomogramsStep') self._insertFunctionStep('createOutputStep') # --------------------------- STEPS functions -----------------------------
[docs] def importSubtomogramsStep(self): samplingRate = self.samplingRate.get() starf = self.starFile.get() path = dirname(starf) + '/' subtomo = SubTomogram() subtomo.setSamplingRate(samplingRate) imgh = ImageHandler() self.subtomoSet = self._createSetOfSubTomograms() self.subtomoSet.setSamplingRate(samplingRate) starf_extra = self._getExtraPath('pyseg.star') copyFile(starf, starf_extra) self.fhTable = open(starf_extra, 'r') headerDict, line = readStarfileHeader(self.fhTable) if line == ' \n' or line == '\n': # if there is an empty line between column names and real data pass else: readStarfileRow(line, subtomo, path, headerDict) self._writeSubtomogram(subtomo, imgh, samplingRate) for line in self.fhTable: if line == ' \n' or line == '\n': # if there is an empty line at the end of the file break else: readStarfileRow(line, subtomo, path, headerDict) self._writeSubtomogram(subtomo, imgh, samplingRate) self.fhTable.close()
[docs] def createOutputStep(self): self._defineOutputs(outputSubTomograms=self.subtomoSet)
# --------------------------- INFO functions ----------------------------------- def _summary(self): summary = [] if not hasattr(self, 'outputSubTomograms'): summary.append("Output subtomograms not ready yet.") else: summary.append("Subtomograms imported from: %s\nSampling rate: %0.2f" % (self.starFile.get(), self.samplingRate.get())) return summary def _methods(self): methods = [] if not hasattr(self, 'outputSubTomograms'): methods.append("Output subtomograms not ready yet.") else: methods.append(" %s subtomograms imported from %s with a sampling rate *%0.2f*" % (self.subtomoSet.getSize(), self.starFile.get(), self.samplingRate.get())) return methods # --------------------------- UTILS functions ----------------------------------- def _writeSubtomogram(self, subtomo, imgh, samplingRate): fileName = subtomo.getFileName() 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() origin.setShifts(x / -2. * samplingRate, y / -2. * samplingRate, zDim / -2. * samplingRate) subtomo.setOrigin(origin) newFileName = abspath(self._getVolumeFileName(fileName)) if fileName.endswith(':mrc'): fileName = fileName[:-4] createAbsLink(fileName, newFileName) if n == 1: self._addSubtomogram(subtomo, newFileName) else: for index in range(1, n + 1): self._addSubtomogram(subtomo, newFileName, index=index) def _getVolumeFileName(self, fileName, extension=None): if extension is not None: baseFileName = "import_" + str(basename(fileName)).split(".")[0] + ".%s" % extension else: baseFileName = "import_" + str(basename(fileName)).split(":")[0] return self._getExtraPath(baseFileName) def _addSubtomogram(self, subtomo, newFileName, index=None): subtomo.cleanObjId() if index is None: subtomo.setFileName(newFileName) else: subtomo.setLocation(index, newFileName) self.subtomoSet.append(subtomo)