mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-22 05:58:12 +08:00
Move to new REST v0.2 communication between front and back-end. This is a first cut implementation which is functional, but will need follow-up enhancements for performance, error checking, etc. Protocol spec is in docs directory. * Add filtering via indexing * Using new filter specs Indexing working * Added filtering by annotation value * factor out common methods * Documentation * create enum for axis (obs/var) * Better description for filter's return * Add boolean to enumerated types * Augmented enum for scanpy axis * Create schema for annotations Based on datatype within scanpy/anndata + tests * remove obsolete schema parse script * Update rest api to remove old routes and add schema route * Separate development requirements * Warning for unsupported datatypes * include -r requirements.txt in dev * Merged downcast warnings * Fixed bug where names were NaNs Needed to include the index too when creating the series * Add config endpoint * Generate app features from CLI selections * Move features to driver * Add tests for schema * Clearer version wording * python3 version of super * version from engine to package level * move features to driver * Revise layout function to match the new spec * GET for layout/obs * PUT Layout (#211) * PUT Layout * Csweaver/annotations (#212) * Update scanpy engine to support the rest v0.2 annotation requests * GET endpoint for obs annotations + tests * Documentation * Test annotations in scanpy engine * Description for annotation-keys param * annotation->annotations * clarified return for annotations * Use URL query list for annotations fields * parse_filter parses v0.2 GET filters (#215) * parse_filter parses v0.2 GET filters * Don't allow index filters from query params * Better variable conversion * Parse filter improvements - uses default dict - renamed filter -> query_filter * Cleanup Tasks (#216) * Add test_api back into travis build * Do custom JSON encoding the correct way * Run cellxgene server in test setup * Cleanup new tests too * Option to bind to all interfaces (#225) app.run("0.0.0.0") instead of app.run("127.0.0.1") binds to all interfaces. Note: There are comments on the internet that says that the flask server is not up to the task of production serving. I don't think that such scalability concerns apply here, but I was able to get cellxgene working with twistd relatively easily, and we could switch to that if there are scalability concerns. Test plan: browsed to <ip>:5005/api/v0.2/config on a different host. * Add filtering via indexing * Using new filter specs Indexing working * Added filtering by annotation value * factor out common methods * Documentation * create enum for axis (obs/var) * Better description for filter's return * Add boolean to enumerated types * Augmented enum for scanpy axis * Create schema for annotations Based on datatype within scanpy/anndata + tests * remove obsolete schema parse script * Update rest api to remove old routes and add schema route * Separate development requirements * Warning for unsupported datatypes * include -r requirements.txt in dev * Merged downcast warnings * Fixed bug where names were NaNs Needed to include the index too when creating the series * Add config endpoint * Generate app features from CLI selections * Move features to driver * Add tests for schema * Clearer version wording * python3 version of super * version from engine to package level * move features to driver * Revise layout function to match the new spec * GET for layout/obs * PUT Layout (#211) * PUT Layout * Csweaver/annotations (#212) * Update scanpy engine to support the rest v0.2 annotation requests * GET endpoint for obs annotations + tests * Documentation * Test annotations in scanpy engine * Description for annotation-keys param * annotation->annotations * clarified return for annotations * Use URL query list for annotations fields * parse_filter parses v0.2 GET filters (#215) * parse_filter parses v0.2 GET filters * Don't allow index filters from query params * Better variable conversion * Parse filter improvements - uses default dict - renamed filter -> query_filter * Cleanup Tasks (#216) * Add test_api back into travis build * Do custom JSON encoding the correct way * Run cellxgene server in test setup * Cleanup new tests too * Option to bind to all interfaces (#225) app.run("0.0.0.0") instead of app.run("127.0.0.1") binds to all interfaces. Note: There are comments on the internet that says that the flask server is not up to the task of production serving. I don't think that such scalability concerns apply here, but I was able to get cellxgene working with twistd relatively easily, and we could switch to that if there are scalability concerns. Test plan: browsed to <ip>:5005/api/v0.2/config on a different host. * Fix merge errors - import warnings was improperly deleted - scanpy engine tests were totally wrong * Fix merge error with driver * PUT /annotations (#235) * Add query param for annotation name * fix descriptions, eliminate else clause * first cut at initial data load on rest 0.2 api * Annotation var (#248) * Fix bug strings are always objects in pandas * Add axis to annotation method * Add /annotation/var to REST api * Csweaver/expressiondata (#242) * Refactor expression method for REST v2 * Add message to QueryStringError * Fix range filters * Add GET route for /data * /data PUT route * rename expression to data_frame * clarification of error * Improve accept type handling * support all schema types for 0.2 REST API * remove REST 0.1 code; connect var annotations loading * config reducer; use config to set data set title; remove obsolete templating code for data set title * REST 0.2 expression conversion support * partial port of expression to REST 0.2 * diffexp (#273) * Add diffexp method to scanpy and test * Minor tweaks to diffexp Get a minimal working version to unblock FE development * Fixing things git deleted * cleanup print statements * Add index test * additional, partial REST 0.2 bring up of diffexp * Ignore unstructured annotations for data (#275) This is a temp hack, need to figure out how to include data.uns if there is only one gene * diffexp REST 0.2 port finish * ignore unstructured annotaitons on all routes except layout * correctly use varDataCache; maintain state during world rebuild * correct varDataCache use * temporarily disable all memoization * refinements to expression data caching * clear cell sets upon regraph/reset * update version of REST to 0.2 * Travis build fixes - comment out cache import - fix duplicate test name * Remove dependency from travis * clarify semantics of config variables * move generic action helpers into util
85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
import argparse
|
|
import os
|
|
import warnings
|
|
|
|
from flask import Flask
|
|
from flask_caching import Cache
|
|
from flask_compress import Compress
|
|
from flask_cors import CORS
|
|
from flask_restful_swagger_2 import get_swagger_blueprint
|
|
|
|
from .rest_api.rest import get_api_resources
|
|
from .util.utils import Float32JSONEncoder
|
|
from .web import webapp
|
|
|
|
REACTIVE_LIMIT = 1_000_000
|
|
|
|
app = Flask(__name__, static_folder="web/static")
|
|
app.json_encoder = Float32JSONEncoder
|
|
cache = Cache(app, config={"CACHE_TYPE": "simple", "CACHE_DEFAULT_TIMEOUT": 860000})
|
|
Compress(app)
|
|
CORS(app)
|
|
|
|
# Config
|
|
SECRET_KEY = os.environ.get("CXG_SECRET_KEY", default="SparkleAndShine")
|
|
|
|
app.config.update(
|
|
SECRET_KEY=SECRET_KEY,
|
|
)
|
|
|
|
# Application Data
|
|
data = None
|
|
|
|
|
|
# A list of swagger document objects
|
|
docs = []
|
|
resources = get_api_resources()
|
|
docs.append(resources.get_swagger_doc())
|
|
|
|
app.register_blueprint(webapp.bp)
|
|
app.register_blueprint(resources.blueprint)
|
|
app.register_blueprint(
|
|
get_swagger_blueprint(docs, "/api/swagger", produces=["application/json"], title="cellxgene rest api",
|
|
description="An API connecting ExpressionMatrix2 clustering algorithm to cellxgene"))
|
|
|
|
app.add_url_rule("/", endpoint="index")
|
|
|
|
|
|
def run_scanpy(args):
|
|
title = args.title
|
|
if not title:
|
|
title = os.path.basename(os.path.normpath(args.data_directory))
|
|
api_base = f"http://127.0.0.1:{args.port}/api/"
|
|
app.config.update(
|
|
DATASET_TITLE=title,
|
|
CXG_API_BASE=api_base
|
|
)
|
|
|
|
from .scanpy_engine.scanpy_engine import ScanpyEngine
|
|
app.data = ScanpyEngine(args.data_directory, layout_method=args.layout, diffexp_method=args.diffexp)
|
|
if args.bind_all:
|
|
host = "0.0.0.0"
|
|
else:
|
|
host = "127.0.0.1"
|
|
app.run(host=host, debug=True, port=args.port)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Cellxgene is a tool for exploring single cell expression.")
|
|
parser.add_argument("--title", "-t", help="Title to display -- if this is omitted the title will be the name "
|
|
"of the directory from the data_directory arg")
|
|
parser.add_argument("--port", help="Port to run server on.", type=int, default=5005)
|
|
parser.add_argument(
|
|
"--bind-all",
|
|
help="Bind to all interfaces (this makes the server accessible beyond this computer)",
|
|
action="store_true")
|
|
subparsers = parser.add_subparsers(dest="cellxgene_command")
|
|
try:
|
|
from .scanpy_engine.scanpy_engine import ScanpyEngine
|
|
except ImportError:
|
|
warnings.warn("Scanpy engine not available", ImportWarning)
|
|
else:
|
|
ScanpyEngine.add_to_parser(subparsers, run_scanpy)
|
|
args = parser.parse_args()
|
|
args.func(args)
|