def getSubTomogramData(subtomorundata, stackpdata):
    pdata = stackpdata['particle']
    tomoq = appiondata.ApTomogramData(subtomorun=subtomorundata, center=pdata)
    results = tomoq.query()
    if results:
        tomo = results[0]
        return tomo
def getLastVolumeIndex(fulltomodata):
    tomoq = appiondata.ApTomogramData(fulltomogram=fulltomodata)
    results = tomoq.query()
    if results:
        return results[0]['number']
    else:
        return 0
	def setRunDir(self):
		"""
		this function only runs if no rundir is defined at the command line
		"""
		subtomorunq = appiondata.ApSubTomogramRunData()
		subtomorundata = subtomorunq.direct_query(self.params['subtomoId'])
		subtomoq = appiondata.ApTomogramData(subtomorun=subtomorundata)
		results = subtomoq.query(results=1)
		if results:
			subtomodir = results[0]['path']['path']
			tiltdirs = subtomodir.split('/tiltseries')
			self.params['rundir'] = os.path.join(tiltdirs[0],"average",self.params['runname'])
def isTomoInDB(md5sum, full=False, recfile=''):
    abspath = os.path.abspath(recfile)
    rundir = os.path.dirname(abspath)
    basename = os.path.basename(abspath)
    rootname = os.path.splitext(basename)
    if not full:
        tomoq = appiondata.ApTomogramData(name=rootname[0])
        tomoq['md5sum'] = md5sum
    else:
        tomoq = appiondata.ApFullTomogramData(name=rootname[0])
    tomod = tomoq.query(results=1)
    if tomod:
        tomodata = tomod[0]
        # old style
        if tomodata['path'] is not None and tomodata['path']['path'] == rundir:
            return True
        # new style
        if tomodata['reconrun']['path']['path'] == rundir:
            return True
    return False
def insertSubTomogram(fulltomodata, rundata, center, offsetz, dimension, path,
                      name, index, pixelsize, description):
    tomoq = appiondata.ApTomogramData(fulltomogram=fulltomodata)
    tomoq['session'] = fulltomodata['session']
    tomoq['tiltseries'] = fulltomodata['tiltseries']
    tomoq['subtomorun'] = rundata
    tomoq['path'] = appiondata.ApPathData(path=os.path.abspath(path))
    tomoq['name'] = name
    tomoq['number'] = index
    tomoq['center'] = center
    # offsetz is in pixel of the full tomogram
    tomoq['offsetz'] = offsetz
    # dimension is that of the original tilt images, i.e., before binning of the full tomogram
    tomoq['dimension'] = dimension
    # pixelsize is of the full_bin * sub_bin tomogram
    tomoq['pixelsize'] = pixelsize
    tomoq['description'] = description
    filepath = os.path.join(path, name + ".rec")
    tomoq['md5sum'] = apFile.md5sumfile(filepath)
    return publish(tomoq)
def getSubvolumeInfo(subtomorundata):
    tomoq = appiondata.ApTomogramData(subtomorun=subtomorundata)
    results = tomoq.query(results=1)
    if results:
        tomo = results[0]
        subbin = subtomorundata['subbin']
        fullq = appiondata.ApFullTomogramData()
        fulltomogram = tomo['fulltomogram']
        if fulltomogram['alignrun'] is None:
            # new data has no alignrun
            fullbin = tomo['fulltomogram']['aligner']['alignrun']['bin']
        else:
            # old data has no aligner
            fullbin = tomo['fulltomogram']['alignrun']['bin']
        totalbin = subbin * fullbin
        shape = (tomo['dimension']['z'] / totalbin,
                 tomo['dimension']['y'] / totalbin,
                 tomo['dimension']['x'] / totalbin)
        pixelsize = tomo['pixelsize']
        return shape, totalbin, pixelsize
    else:
        return None, 1, None
def uploadTomo(params):
    if not params['commit']:
        apDisplay.printWarning("not commiting tomogram to database")
        return
    apDisplay.printMsg("Commiting tomogram to database")
    sessiondata = apDatabase.getSessionDataFromSessionName(
        params['sessionname'])
    tiltdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(
        params['tiltseriesnumber'], sessiondata)
    runname = params['runname']
    name = params['name']
    if params['full']:
        fullbin = params['bin']
    else:
        fullbin = 1
        subbin = params['bin']
    alignrun = insertTomoAlignmentRun(sessiondata, None, None, None, None,
                                      fullbin, runname, params['aligndir'],
                                      'manual alignment from upload')
    # only tilt series in one alignrun for now
    insertTiltsInAlignRun(alignrun, tiltdata, None, True)
    alignerdata = insertAlignerParams(alignrun, params)
    firstimagedata = getFirstImage(tiltdata)
    path = os.path.abspath(params['rundir'])
    description = params['description']
    if params['full']:
        thickness = params['shape'][0] * fullbin
        uploadfile = params['zprojfile']
        projectimagedata = uploadZProjection(runname, firstimagedata,
                                             uploadfile)
        fullrundata = insertFullTomoRun(sessiondata, path, runname, 'upload')
        return insertFullTomogram(sessiondata, tiltdata, alignerdata,
                                  fullrundata, name, description,
                                  projectimagedata, thickness, None, fullbin,
                                  [])
    else:
        projectimagedata = None
        fulltomopath = params['rundir'].replace('/' + params['volume'], '')
        dummyname = 'dummy'
        dummydescription = 'fake full tomogram for subtomogram upload'
        thickness = params['shape'][0] * subbin
        fullrundata = insertFullTomoRun(sessiondata, fulltomopath, runname,
                                        'upload')
        fulltomogram = insertFullTomogram(sessiondata, tiltdata, alignerdata,
                                          fullrundata, dummyname,
                                          dummydescription, projectimagedata,
                                          thickness, None, fullbin, [])
        apix = apDatabase.getPixelSize(firstimagedata)
        tomoq = appiondata.ApTomogramData()
        tomoq['session'] = sessiondata
        tomoq['tiltseries'] = tiltdata
        results = tomoq.query()
        index = len(results) + 1
        pixelsize = 1e-10 * apix * subbin
        runname = params['volume']
        shape = map((lambda x: x * subbin), params['shape'])
        dimension = {'x': shape[2], 'y': shape[1], 'z': shape[0]}
        subtomorundata = insertSubTomoRun(sessiondata, None, None, runname,
                                          params['invert'], subbin)
        return insertSubTomogram(fulltomogram, subtomorundata, None, 0,
                                 dimension, path, name, index, pixelsize,
                                 description)