Skip to content

Commit

Permalink
Implement annotation based filters and allow frontent to specify vmax
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisDavie committed Feb 27, 2018
1 parent 3c37a49 commit efb22a6
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 69 deletions.
4 changes: 4 additions & 0 deletions scope-client/src/proto/s.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ message CellColorByFeaturesRequest {
bool hasCpmTranform=5;
repeated float threshold=6;
bool scaleThresholded=7;
repeated Annotation annotation=8;
float vmax=9;
}

message CellColorByFeaturesReply {
repeated string color=1;
float vmax=2;
}

message CellAUCValuesByFeaturesRequest {
Expand Down Expand Up @@ -75,6 +78,7 @@ message FeatureReply {
message CoordinatesRequest {
string loomFilePath=1;
int32 coordinatesID=2;
repeated Annotation annotation=3;
}

message CoordinatesReply {
Expand Down
66 changes: 49 additions & 17 deletions scope-server/scopeserver/modules/gserver/GServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_nb_cells(self, loom_file_path):
loom = self.get_loom_connection(loom_file_path)
return loom.shape[1]

def get_gene_expression(self, loom_file_path, gene_symbol, log_transform=True, cpm_normalise=False):
def get_gene_expression(self, loom_file_path, gene_symbol, log_transform=True, cpm_normalise=False, annotation=''):
loom = self.get_loom_connection(loom_file_path)
print("Debug: getting expression of " + gene_symbol + "...")
gene_expr = loom[loom.ra.Gene == gene_symbol, :][0]
Expand All @@ -71,13 +71,28 @@ def get_gene_expression(self, loom_file_path, gene_symbol, log_transform=True, c
print("Debug: CPM normalising gene expression...")
gene_expr = gene_expr / loom.ca.nUMI
gene_expr = gene_expr
if len(annotation) > 0:
cellIndices = set()
for n, annoName in enumerate(annotation):
for annotationValue in annotation[n]:
[cellIndices.add(x) for x in np.where(loom.ca[annoName] == annotationValue)]
cellIndices = sorted(list(cellIndices))
gene_expr = gene_expr[cellIndices]
return gene_expr

def get_auc_values(self, loom_file_path, regulon):
def get_auc_values(self, loom_file_path, regulon, annotation=''):
loom = self.get_loom_connection(loom_file_path)
print("Debug: getting AUC values for {0} ...".format(regulon))
if regulon in loom.ca.RegulonsAUC.dtype.names:
return loom.ca.RegulonsAUC[regulon]
vals = loom.ca.RegulonsAUC[regulon]
if len(annotation) > 0:
cellIndices = set()
for n, annoName in enumerate(annotation):
for annotationValue in annotation[n]:
[cellIndices.add(x) for x in np.where(loom.ca[annoName] == annotationValue)]
cellIndices = sorted(list(cellIndices))
vals = vals[cellIndices]
return vals
return []

def get_clusterIDs(self, loom_file_path, clusterID):
Expand Down Expand Up @@ -129,7 +144,7 @@ def get_features(self, loom_file_path, query):
return {'feature': res,
'featureType': resF}

def get_coordinates(self, loom_file_path, coordinatesID=-1):
def get_coordinates(self, loom_file_path, coordinatesID=-1, annotation=''):
loom = self.get_loom_connection(loom_file_path)
dims = 0
if coordinatesID == -1:
Expand All @@ -142,15 +157,23 @@ def get_coordinates(self, loom_file_path, coordinatesID=-1):
if 'tSNE'.casefold() in ca.casefold():
if dims == 0:
x = loom.ca[ca]
dims +=1
dims += 1
continue
if dims == 1:
y = loom.ca[ca]
dims +=1
dims += 1
continue
else:
x = loom.ca.Embeddings_X[str(coordinatesID)]
y = loom.ca.Embeddings_Y[str(coordinatesID)]
if len(annotation) > 0:
cellIndices = set()
for n, annoName in enumerate(annotation):
for annotationValue in annotation[n]:
[cellIndices.add(x) for x in np.where(loom.ca[annoName] == annotationValue)]
cellIndices = sorted(list(cellIndices))
x = x[cellIndices]
y = y[cellIndices]
return {"x": x,
"y": y}

Expand Down Expand Up @@ -194,11 +217,6 @@ def getVmax(self, vals):
return vmax

def getCellColorByFeatures(self, request, context):
# request content
# - lfp = .loom file path
# - e = entries
# - f = features
# - lte = log transform expression
start_time = time.time()
loomFilePath = self.get_loom_filepath(request.loomFilePath)
if not os.path.isfile(loomFilePath):
Expand All @@ -209,16 +227,28 @@ def getCellColorByFeatures(self, request, context):
if request.featureType[n] == 'gene':
if feature != '':
vals = self.get_gene_expression(
loom_file_path=loomFilePath, gene_symbol=feature, log_transform=request.hasLogTranform, cpm_normalise=request.hasCpmTranform)
vmax = self.getVmax(vals)
loom_file_path=loomFilePath,
gene_symbol=feature,
log_transform=request.hasLogTranform,
cpm_normalise=request.hasCpmTranform,
annotation=request.annotation)
if request.vmax != 0.0:
vmax = request.vmax
else:
vmax = self.getVmax(vals)
vals = np.round((vals / vmax) * 255)
features.append([x if x <= 255 else 255 for x in vals])
else:
features.append(np.zeros(n_cells))
elif request.featureType[n] == 'regulon':
if feature != '':
vals = self.get_auc_values(loom_file_path=loomFilePath, regulon=feature)
vmax = self.getVmax(vals)
vals = self.get_auc_values(loom_file_path=loomFilePath,
regulon=feature,
annotation=request.annotation)
if request.vmax != 0.0:
vmax = request.vmax
else:
vmax = self.getVmax(vals)
if request.scaleThresholded:
vals = ([auc if auc >= request.threshold[n] else 0 for auc in vals])
vals = np.round((vals / vmax) * 255)
Expand All @@ -234,7 +264,7 @@ def getCellColorByFeatures(self, request, context):
hex_vec = []

print("Debug: %s seconds elapsed ---" % (time.time() - start_time))
return s_pb2.CellColorByFeaturesReply(color=hex_vec)
return s_pb2.CellColorByFeaturesReply(color=hex_vec, vmax=vmax)

def getCellAUCValuesByFeatures(self, request, context):
loomFilePath = self.get_loom_filepath(request.loomFilePath)
Expand Down Expand Up @@ -278,7 +308,9 @@ def getFeatures(self, request, context):

def getCoordinates(self, request, context):
# request content
c = self.get_coordinates(self.get_loom_filepath(request.loomFilePath), coordinatesID=request.coordinatesID)
c = self.get_coordinates(self.get_loom_filepath(request.loomFilePath),
coordinatesID=request.coordinatesID,
annotation=request.annotation)
return s_pb2.CoordinatesReply(x=c["x"], y=c["y"])

def getMyLooms(self, request, context):
Expand Down
Loading

0 comments on commit efb22a6

Please sign in to comment.