def rowToCoordinate(coordRow): """ Create a Coordinate from a row of a meta """ # Check that all required labels are present in the row if coordRow.containsAll(COOR_DICT): coord = Coordinate() rowToObject(coordRow, coord, COOR_DICT, extraLabels=COOR_EXTRA_LABELS) # Gautomatch starts _rlnClassNumber at 0, but relion at 1 # so let's increment its value if coord.hasAttribute('_rlnClassNumber'): coord._rlnClassNumber.increment() micName = None if coordRow.hasLabel(md.RLN_MICROGRAPH_ID): micId = int(coordRow.getValue(md.RLN_MICROGRAPH_ID)) coord.setMicId(micId) # If RLN_MICROGRAPH_NAME is not present, use the id as a name micName = micId if coordRow.hasLabel(md.RLN_MICROGRAPH_NAME): micName = coordRow.getValue(md.RLN_MICROGRAPH_NAME) coord.setMicName(micName) else: coord = None return coord
def get2DCoordList(start=1, end=3): """ Mocks a 2d coordinates dict""" newCoordDict = {} for index in range(start, end + 1): coord = Coordinate() newCoordDict[index] = coord return newCoordDict
def _checkNewOutput(self): if getattr(self, 'finished', False): return self.finished = self.streamClosed and self.checkedMics == self.processedMics streamMode = Set.STREAM_CLOSED if self.finished else Set.STREAM_OPEN newFiles = getFiles(self._getTmpPath()) if newFiles or self.finished: # when finished to close the output set outSet = self._loadOutputSet(SetOfCoordinates, 'coordinates.sqlite') for fnTmp in newFiles: coords = np.loadtxt(fnTmp) moveFile(fnTmp, self._getExtraPath()) if coords.size == 2: # special case with only one coordinate coords = [coords] for coord in coords: newCoord = Coordinate() micrographs = self.getMainInput().getMicrographs() newCoord.setMicrograph(micrographs[self.getMicId(fnTmp)]) newCoord.setPosition(coord[0], coord[1]) outSet.append(newCoord) firstTime = not self.hasAttribute(self.outputName) self._updateOutputSet(self.outputName, outSet, streamMode) if firstTime: self.defineRelations(outSet) outSet.close() if self.finished: # Unlock createOutputStep if finished all jobs outputStep = self._getFirstJoinStep() if outputStep and outputStep.isWaiting(): outputStep.setStatus(STATUS_NEW)
def _createSetOfParts(self, nMics=10, nOptics=2, partsPerMic=10): micSet = self._createSetOfMics(nMics, nOptics) outputSqlite = self.getOutputPath('particles.sqlite') cleanPath(outputSqlite) print(">>> Writing to particles db: %s" % outputSqlite) outputParts = SetOfParticles(filename=outputSqlite) outputParts.setSamplingRate(1.234) outputParts.setAcquisition(micSet.getAcquisition()) part = SetOfParticles.ITEM_TYPE() coord = Coordinate() for mic in micSet: for i in range(1, partsPerMic + 1): part.setLocation(i, mic.getFileName().replace('mrc', 'mrcs')) coord.setPosition(x=np.random.randint(0, 1000), y=np.random.randint(0, 1000)) coord.setMicrograph(mic) part.setObjId(None) part.setCoordinate(coord) part.setAcquisition(mic.getAcquisition()) outputParts.append(part) outputParts.write() return outputParts
def basiccoordsFactory(self, iteration): # newCoord = Coordinate(x=i, y=i) # To avoid dict get newCoord = Coordinate() newCoord.setX(iteration) newCoord.setY(iteration) return newCoord
def rowToCoordinate(coordRow): """ Create a Coordinate from a row of a metadata. """ # Check that all required labels are present in the row if _containsAll(coordRow, COOR_DICT): coord = Coordinate() rowToObject(coordRow, coord, COOR_DICT) # , extraLabels=COOR_EXTRA_LABELS) # TODO: switch back on extra labels when # sqlite mapper can tolerate dots in label names else: coord = None return coord
def _fillParticles(self, outputParticles, outputCoords, inputParticles): """ Create the SetOfParticles and SetOfCoordinates""" myParticles = md.MetaData(self._getExtraPath('final_images.xmd')) outputParticles.enableAppend() outputCoords.enableAppend() #Calculating the scale that relates the coordinates with the actual # position in the mic scale = inputParticles.getSamplingRate() / \ self.inputMics.get().getSamplingRate() #Dictionary with the name and id of the inpt mics micDictname = {} micDictId = {} for mic in self.inputMics.get(): micKey = mic.getMicName() micDictname[micKey] = mic.clone() micKey2 = mic.getObjId() micDictId[micKey2] = mic.clone() for row in md.iterRows(myParticles): #To create the new particle p = rowToParticle(row) #To create the new coordinate newCoord = Coordinate() coord = p.getCoordinate() if coord.getMicName() is not None: micKey = coord.getMicName() micDict = micDictname else: micKey = coord.getMicId() micDict = micDictId mic = micDict.get(micKey, None) if mic is None: print("Skipping particle, key %s not found" % micKey) else: newCoord.copyObjId(p) x, y = coord.getPosition() newCoord.setPosition(x * scale, y * scale) newCoord.setMicrograph(mic) outputCoords.append(newCoord) p.setCoordinate(newCoord) #Storing the new particle outputParticles.append(p) boxSize = inputParticles.getXDim() * scale outputCoords.setBoxSize(boxSize)
def readSetOfCoordinates(coordinatesCsvFn, micSet, coordSet, scale): """ Read coordinates produced by Topaz. Coordinates are expected in a single csv file, with the following columns: first: image_name (mic id) second: x_coord third: y_coord forth: score """ csv = CsvCoordinateList(coordinatesCsvFn, score=True) lastMicId = None coord = Coordinate() coord._topazScore = Float() micDict = {} # loop to generate a dictionary --> micBaseName : Micrograph for mic in micSet: micNew = mic.clone() micDict[mic.getObjId()] = micNew #loop the Topaz outputfile for row in csv: micId = int(row[0]) if micId != lastMicId: mic = micDict[micId] if mic is None: print("Missing id: ", micId) else: coord.setMicrograph(mic) lastMicId = micId coord.setPosition(int(round(float(row[1]) * scale)), int(round(float(row[2]) * scale))) coord._topazScore.set(float(row[3])) coord.setObjId(None) coordSet.append(coord) csv.close()
def readCoordinates(mic, fileName, coordsSet): if exists(fileName): md = emlib.MetaData() md.readPlain(fileName, 'xcoor ycoor') for objId in md: x = md.getValue(emlib.MDL_XCOOR, objId) y = md.getValue(emlib.MDL_YCOOR, objId) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordsSet.append(coord)
def getParticleList(start=1, end=3): """ Mocks a Particle dict""" newPartDict = {} for index in range(start, end + 1): part = Particle() coord = Coordinate() coord.setMicId(index) coord.setMicName(getMicNameFromId(index)) part.setCoordinate(coord) newPartDict[index] = part return newPartDict
def readCoordinates(mic, fn, coordsSet): """ Parse coords file and populate coordsSet. :param mic: input micrograph object :param fn: input file to parse :param coordsSet: output set of coords """ if exists(fn): with open(fn, 'r') as f: for line in f: values = line.strip().split() # plt coords are in Imagic style x = float(values[1]) y = float(mic.getYDim() - float(values[0])) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordsSet.append(coord)
def readCoordsFromMics(self, workingDir, micList, coordSet): coordSet.setBoxSize(self.radius.get() * 2) for mic in micList: micFn = mic.getFileName() micDir = self._getMicDir(micFn) coordFile = os.path.join(micDir, self._getMicPosFn(micFn)) if os.path.exists(coordFile): coordMd = md.MetaData() coordMd.readPlain(coordFile, 'xcoor ycoor') for objId in coordMd: x = coordMd.getValue(md.MDL_XCOOR, objId) y = coordMd.getValue(md.MDL_YCOOR, objId) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordSet.append(coord) else: print("Coordinate file '%s' not found. " % coordFile)
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 coordsFactory(iteration): # newCoord = Coordinate(x=i, y=i) # To avoid dict get newCoord = Coordinate() newCoord.setX(iteration) newCoord.setY(iteration) newCoord.extra1 = Integer(1) newCoord.extra2 = Integer(1) newCoord.extra3 = Integer(1) newCoord.extra4 = Integer(1) newCoord.extra5 = Integer(1) newCoord.extra6 = Integer(1) newCoord.extra7 = Integer(1) newCoord.extra8 = Integer(1) newCoord.extra9 = Integer(1) newCoord.extra10 = Integer(1) newCoord.extra11 = Integer(1) newCoord.extra12 = Integer(1) newCoord.extra13 = Integer(1) newCoord.extra14 = Integer(1) newCoord.extra15 = Integer(1) newCoord.extra16 = Integer(1) newCoord.extra17 = Integer(1) newCoord.extra18 = Integer(1) newCoord.extra19 = Integer(1) newCoord.extra20 = Integer(1) return newCoord
def particlesFactory(index): newParticle = Particle(location=(1, self.particlesStk)) newParticle.setCoordinate(Coordinate(x=1, y=2)) newParticle.setAcquisition(Acquisition()) return newParticle