def checkExistingFullTomoData(path, name):
    pathq = appiondata.ApPath(path=path)
    runq = appiondata.ApFullTomogramRunData(pathq)
    tomoq = appiondata.ApFullTomogramData(name=name, reconrun=runq)
    results = tomoq.query()
    if not results:
        return None
    filepath = os.path.join(path, name + ".rec")
    if not os.path.isfile(filepath):
        return None
    else:
        return results[0]
    def upgradeAppionDB(self):
        if self.appion_dbupgrade.tableExists(
                'ApFullTomogramRunData') and self.appion_dbupgrade.tableExists(
                    'ApFullTomogramData'):
            if (not self.appion_dbupgrade.columnExists('ApFullTomogramRunData',
                                                       'SEQ|excluded')):
                apDisplay.printWarning(
                    'No excluded image column available, not need for column move'
                )
                return

            if (not self.appion_dbupgrade.columnExists('ApFullTomogramData',
                                                       'SEQ|excluded')):
                self.appion_dbupgrade.addColumn('ApFullTomogramData',
                                                'SEQ|excluded',
                                                self.appion_dbupgrade.str)
            q = appiondata.ApFullTomogramData()
            results = q.query()
            for qdata in results:
                rundata = qdata['reconrun']
                try:
                    runid = rundata.dbid
                except:
                    apDisplay.printWarning(
                        'No associated ApFullTomgramRunData. Skip')
                    continue
                tid = qdata.dbid
                query = "Select `SEQ|excluded` from %s.`ApFullTomogramRunData` where `DEF_id`=%d;" % (
                    self.appion_dbupgrade.dbname, runid)
                results = self.appion_dbupgrade.returnCustomSQL(query)
                excludetext = results[0][0]
                try:
                    existing_excludetext = results[0][0]
                except IndexError:
                    apDisplay.printWarning(
                        'No excluded image info available, not need for column move'
                    )
                    continue

                # only do the schema upgrade if ApFullTomogramData has no value in SEQ|excluded'
                existing_excludetext = None
                query = "Select `SEQ|excluded` from %s.`ApFullTomogramData` where `DEF_id`=%d;" % (
                    self.appion_dbupgrade.dbname, runid)
                results = self.appion_dbupgrade.returnCustomSQL(query)
                try:
                    existing_excludetext = results[0][0]
                except IndexError:
                    pass
                if (excludetext and not existing_excludetext):
                    query = "Update ApFullTomogramData set `SEQ|excluded`='%s' WHERE ApFullTomogramData.`DEF_id`=%d;" % (
                        excludetext, tid)
                    self.appion_dbupgrade.executeCustomSQL(query)
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 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 insertFullTomogram(sessiondata,
                       tiltdata,
                       aligner,
                       fullrundata,
                       name,
                       description,
                       projectimagedata,
                       thickness,
                       reconparamdata=None,
                       bin=1,
                       imageidlist=[]):
    tomoq = appiondata.ApFullTomogramData()
    tomoq['session'] = sessiondata
    tomoq['tiltseries'] = tiltdata
    tomoq['aligner'] = aligner
    tomoq['reconrun'] = fullrundata
    tomoq['name'] = name
    tomoq['description'] = description
    tomoq['zprojection'] = projectimagedata
    tomoq['thickness'] = thickness
    tomoq['bin'] = bin
    tomoq['excluded'] = imageidlist
    tomoq['reconparam'] = reconparamdata
    return publish(tomoq)