def getLabels(data): labels = db.Labels(data['user'], data['hub'], data['track'], data['ref']) labelsDf = labels.getInBounds(data['ref'], data['start'], data['end']) if len(labelsDf.index) < 1: return [] labelsDf = labelsDf[labelColumns] labelsDf.columns = jbrowseLabelColumns return labelsDf.to_dict('records')
def stats(): chroms = labels = 0 for key in db.Labels.db_key_tuples(): labelsDf = db.Labels(*key).get() if labelsDf.empty: continue chroms = chroms + 1 labels = labels + len(labelsDf.index) return chroms, labels
def removeLabel(data): toRemove = pd.Series({ 'chrom': data['ref'], 'chromStart': data['start'], 'chromEnd': data['end'] }) txn = db.getTxn() labels = db.Labels(data['user'], data['hub'], data['track'], data['ref']) removed, after = labels.remove(toRemove, txn=txn) db.Prediction('changes').increment(txn=txn) Models.updateAllModelLabels(data, after) txn.commit() return removed.to_dict()
def moreHubInfo(request): userid = request.authenticated_userid query = request.matchdict hubName = query['hub'] myKeys = db.Labels.keysWhichMatch(userid, hubName) num_labels = 0 labels = {} for key in myKeys: num_labels += db.Labels(*key).get().shape[0] labels[(key[2], key[3])] = db.Labels(*key).get().to_html() thisHubInfo = db.HubInfo(userid, hubName).get() tracks = [] return { 'user': userid, 'hubName': hubName, 'hubInfo': thisHubInfo, 'numLabels': num_labels, 'tracks': tracks, 'labels': labels }
def updateLabel(data): label = data['label'] updateLabel = pd.Series({ 'chrom': data['ref'], 'chromStart': data['start'], 'chromEnd': data['end'], 'annotation': label }) txn = db.getTxn() labelDb = db.Labels(data['user'], data['hub'], data['track'], data['ref']) item, labels = labelDb.add(updateLabel, txn=txn) db.Prediction('changes').increment(txn=txn) Models.updateAllModelLabels(data, labels) txn.commit() return item.to_dict()
def publicHubs(request): userid = request.authenticated_userid everyKey = db.HubInfo.keysWhichMatch(db.HubInfo) hubNames = list(map(lambda tuple: tuple[1], everyKey)) hubInfos = {} for key in everyKey: currentHub = db.HubInfo(key[0], key[1]).get() if 'isPublic' in currentHub.keys() and currentHub['isPublic']: currentHub['labels'] = 0 for labelKey in db.Labels.keysWhichMatch(key[0], key[1]): currentHub['labels'] += db.Labels(*labelKey).get().shape[0] currentHub['owner'] = key[0] hubInfos['{hubName}'.format(hubName=key[1])] = currentHub return {"user": userid, "HubNames": hubNames, "hubInfos": hubInfos}
def putModel(data): modelData = pd.read_json(data['modelData']) modelData.columns = modelColumns modelInfo = data['modelInfo'] problem = modelInfo['problem'] penalty = data['penalty'] user = modelInfo['user'] hub = modelInfo['hub'] track = modelInfo['track'] txn = db.getTxn() db.Model(user, hub, track, problem['chrom'], problem['chromStart'], penalty).put(modelData, txn=txn) labels = db.Labels(user, hub, track, problem['chrom']).get() errorSum = calculateModelLabelError(modelData, labels, problem, penalty) db.ModelSummaries(user, hub, track, problem['chrom'], problem['chromStart']).add(errorSum, txn=txn) txn.commit() return modelInfo
def saveLabelGroup(group, user, hub, track, genome, coverageUrl): group = group.sort_values('chromStart', ignore_index=True) group['annotation'] = group.apply(fixNoPeaks, axis=1) chrom = group['chrom'].loc[0] txn = db.getTxn() db.Labels(user, hub, track['track'], chrom).put(group, txn=txn) chromProblems = Tracks.getProblemsForChrom(genome, chrom, txn) withLabels = chromProblems.apply(checkIfProblemHasLabels, axis=1, args=(group,)) doPregen = chromProblems[withLabels] submitPregenWithData(doPregen, user, hub, track, coverageUrl) txn.commit()
def myHubs(request): userid = request.authenticated_userid everyKey = db.HubInfo.keysWhichMatch(db.HubInfo) hubInfos = Hubs.getHubInfos(everyKey, userid) labels = {} usersdict = {} permissions = {} for hubName in hubInfos: currHubInfo = hubInfos[hubName] owner = currHubInfo['owner'] usersdict[hubName] = currHubInfo[ 'users'] if 'users' in currHubInfo.keys() else [] everyLabelKey = db.Labels.keysWhichMatch(owner, hubName) num_labels = 0 for key in everyLabelKey: if key[0] == userid or userid in hubInfos[key[1]]['users']: num_labels += db.Labels(*key).get().shape[0] labels[hubName] = num_labels # get each co users permissions for couser in usersdict[hubName]: permissions[(hubName, couser)] = db.Permissions(owner, hubName, couser).get() return { "user": userid, "hubInfos": hubInfos, "usersdict": usersdict, "permissions": permissions, "labels": labels }
def addLabel(data): label = 'unknown' for i in range(100): print("#################################################") print("THIS IS THE DATA", data) for i in range(10): print("#################################################") # Duplicated because calls from updateLabel are causing freezing newLabel = pd.Series({ 'chrom': data['ref'], 'chromStart': data['start'], 'chromEnd': data['end'], 'annotation': label }) txn = db.getTxn() item, labels = db.Labels(data['user'], data['hub'], data['track'], data['ref']).add(newLabel, txn=txn) db.Prediction('changes').increment(txn=txn) Models.updateAllModelLabels(data, labels) txn.commit() return data
def generateLOPARTModel(data, problem): user = data['user'] hub = data['hub'] track = data['track'] chrom = data['ref'] blockStart = data['start'] blockEnd = data['end'] scale = data['scale'] hubInfo = db.HubInfo(user, hub).get() trackUrl = hubInfo['tracks'][data['track']]['url'] datapoints = data['width'] start = max(data['visible']['start'], problem['chromStart']) end = min(data['visible']['end'], problem['chromEnd']) scaledBins = int(scale * (end - start)) sumData = bw.bigWigSummary(trackUrl, chrom, start, end, scaledBins) if len(sumData) < 1: return [] dbLabels = db.Labels(user, hub, track, chrom) labels = dbLabels.getInBounds(chrom, start, end) denom = end - start if len(labels.index) < 1: labelsToUse = pd.DataFrame({'start': [1], 'end': [2], 'change': [-1]}) else: lopartLabels = labels[(labels['annotation'] != 'unknown') & (labels['annotation'] != 'peak')] if len(lopartLabels.index) < 1: labelsToUse = pd.DataFrame({ 'start': [1], 'end': [2], 'change': [-1] }) else: labelsToUse = labels.apply(convertLabelsToLopart, axis=1, args=(start, end, denom, scaledBins)) lopartOut = LOPART.runSlimLOPART(sumData, labelsToUse, getLOPARTPenalty(data)) if lopartOut.empty: return [] lopartPeaks = lopartToPeaks(lopartOut) if lopartPeaks.empty: return [] blockBinStart = round((blockStart - start) * scale) blockBinEnd = round((blockEnd - start) * scale) isInBounds = lopartPeaks.apply(checkBlockInBounds, axis=1, args=(blockBinStart, blockBinEnd)) lopartInBlock = lopartPeaks[isInBounds].copy() if lopartInBlock.empty: return [] lopartInBlock['ref'] = chrom lopartInBlock['type'] = 'lopart' return convertLopartOutToJbrowse(lopartInBlock, blockBinStart, blockBinEnd, datapoints)