mirror of
https://github.com/Novartis/cellxgene-gateway.git
synced 2026-10-10 16:10:55 +08:00
#14 added custom method to create data dirs
This commit is contained in:
@@ -23,7 +23,7 @@ def recurse_dir(path):
|
|||||||
x[:-4] if x.endwith('.csv') else x),
|
x[:-4] if x.endwith('.csv') else x),
|
||||||
"path": os.path.join(full_path, x).replace(env.cellxgene_data, ""),
|
"path": os.path.join(full_path, x).replace(env.cellxgene_data, ""),
|
||||||
} for x in os.listdir(full_path) if x.endswith('.csv') and os.path.isfile(os.path.join(full_path, x))]
|
} for x in os.listdir(full_path) if x.endswith('.csv') and os.path.isfile(os.path.join(full_path, x))]
|
||||||
return [{"name":'new', "path":full_path.replace(env.cellxgene_data, "")}] + entries
|
return [{"name":'new', "class":'new', "path":full_path.replace(env.cellxgene_data, "")}] + entries
|
||||||
|
|
||||||
def make_entry(el):
|
def make_entry(el):
|
||||||
full_path = os.path.join(path, el)
|
full_path = os.path.join(path, el)
|
||||||
@@ -56,12 +56,14 @@ def render_entries(entries):
|
|||||||
|
|
||||||
def get_url(entry):
|
def get_url(entry):
|
||||||
return f"view/{ entry['path'].lstrip('/') }"
|
return f"view/{ entry['path'].lstrip('/') }"
|
||||||
|
def get_class(entry):
|
||||||
|
return f" class='{entry['class']}'" if 'class' in entry else ''
|
||||||
|
|
||||||
def render_annotations(entry):
|
def render_annotations(entry):
|
||||||
if len(entry['annotations']) > 0:
|
if len(entry['annotations']) > 0:
|
||||||
return ' | annotations: ' + ", ".join([f"<a href='{get_url(a)}'>{a['name']}</a>" for a in entry['annotations']])
|
return ' | annotations: ' + ", ".join([f"<a href='{get_url(a)}'{get_class(a)}>{a['name']}</a>" for a in entry['annotations']])
|
||||||
else:
|
else:
|
||||||
return '';
|
return ''
|
||||||
|
|
||||||
def render_entry(entry):
|
def render_entry(entry):
|
||||||
if entry["type"] == "file":
|
if entry["type"] == "file":
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import json
|
|||||||
from flask import (
|
from flask import (
|
||||||
Flask,
|
Flask,
|
||||||
redirect,
|
redirect,
|
||||||
|
make_response,
|
||||||
render_template,
|
render_template,
|
||||||
request,
|
request,
|
||||||
send_from_directory,
|
send_from_directory,
|
||||||
@@ -163,11 +164,16 @@ if env.enable_upload:
|
|||||||
def filecrawl():
|
def filecrawl():
|
||||||
entries = recurse_dir(env.cellxgene_data)
|
entries = recurse_dir(env.cellxgene_data)
|
||||||
rendered_html = render_entries(entries)
|
rendered_html = render_entries(entries)
|
||||||
return render_template(
|
resp = make_response(render_template(
|
||||||
"filecrawl.html",
|
"filecrawl.html",
|
||||||
extra_scripts=get_extra_scripts(),
|
extra_scripts=get_extra_scripts(),
|
||||||
rendered_html=rendered_html,
|
rendered_html=rendered_html,
|
||||||
)
|
))
|
||||||
|
resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
||||||
|
resp.headers["Pragma"] = "no-cache"
|
||||||
|
resp.headers["Expires"] = "0"
|
||||||
|
resp.headers['Cache-Control'] = 'public, max-age=0'
|
||||||
|
return resp
|
||||||
|
|
||||||
@app.route("/filecrawl/<path:path>")
|
@app.route("/filecrawl/<path:path>")
|
||||||
def do_filecrawl(path):
|
def do_filecrawl(path):
|
||||||
|
|||||||
@@ -28,12 +28,13 @@ def get_key(path):
|
|||||||
if trimmed.endswith('.h5ad') and data_file_exists(trimmed):
|
if trimmed.endswith('.h5ad') and data_file_exists(trimmed):
|
||||||
# 1) somedir/dataset.h5ad: a dataset
|
# 1) somedir/dataset.h5ad: a dataset
|
||||||
return CacheKey(trimmed, trimmed, None)
|
return CacheKey(trimmed, trimmed, None)
|
||||||
elif trimmed.endswith('.csv') and data_file_exists(trimmed):
|
elif trimmed.endswith('.csv'):
|
||||||
|
|
||||||
# 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotaitons file.
|
# 2) somedir/dataset_annotations/saldaal1-T5HMVBNV.csv : an actual annotations file.
|
||||||
annotations_dir = os.path.split(trimmed)[0]
|
annotations_dir = os.path.split(trimmed)[0]
|
||||||
dataset = make_h5ad(annotations_dir)
|
dataset = make_h5ad(annotations_dir)
|
||||||
if data_file_exists(dataset):
|
if data_file_exists(dataset):
|
||||||
|
data_dir_ensure(annotations_dir)
|
||||||
return CacheKey(trimmed, dataset, trimmed)
|
return CacheKey(trimmed, dataset, trimmed)
|
||||||
elif trimmed.endswith('_annotations') and data_dir_exists(trimmed):
|
elif trimmed.endswith('_annotations') and data_dir_exists(trimmed):
|
||||||
# 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not.
|
# 3) somedir/dataset_annotations: an annotation directory. The corresponding h5ad must exist, but the directory may not.
|
||||||
@@ -74,6 +75,10 @@ def data_dir_exists(dataset):
|
|||||||
file_path = os.path.join(env.cellxgene_data, dataset)
|
file_path = os.path.join(env.cellxgene_data, dataset)
|
||||||
validate_is_dir(file_path)
|
validate_is_dir(file_path)
|
||||||
return True
|
return True
|
||||||
|
def data_dir_ensure(dataset):
|
||||||
|
file_path = os.path.join(env.cellxgene_data, dataset)
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
os.makedirs(file_path)
|
||||||
|
|
||||||
def get_file_path(key):
|
def get_file_path(key):
|
||||||
dataset = key.dataset
|
dataset = key.dataset
|
||||||
@@ -82,12 +87,9 @@ def get_file_path(key):
|
|||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
def get_annotation_file_path(key):
|
def get_annotation_file_path(key):
|
||||||
print(f"getting annotaiton_file_path for {key.annotation_file}")
|
|
||||||
if key.annotation_file is None:
|
if key.annotation_file is None:
|
||||||
return None
|
return None
|
||||||
if key.annotation_file == '':
|
if key.annotation_file == '':
|
||||||
return ''
|
return ''
|
||||||
file_path = os.path.join(env.cellxgene_data, key.annotation_file)
|
file_path = os.path.join(env.cellxgene_data, key.annotation_file)
|
||||||
print(f"getting annotaiton_file_path for {key}, file_path {file_path}")
|
|
||||||
validate_is_file(file_path)
|
|
||||||
return file_path
|
return file_path
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// neandertal javascript
|
||||||
|
const new_annotation_callback = (() =>{
|
||||||
|
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
const pickn = (c, n) => n==0?'':c.substr(Math.random()*c.length,1) + pickn(c,n-1);
|
||||||
|
const suffix = `_${pickn(chars,8)}.csv`;
|
||||||
|
return (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const el = $(e.target);
|
||||||
|
const href = el.attr('href');
|
||||||
|
const base = prompt(`Name your annotations collection\nnote: the suffix "${suffix}" will be appended`);
|
||||||
|
if (base !== null && base.length > 0) {
|
||||||
|
if (/^[0-9a-zA-Z_]+$/.test(base)) {
|
||||||
|
window.location = `${href}/${base}${suffix}`;
|
||||||
|
} else {
|
||||||
|
alert("Error: name must match ^[0-9a-zA-Z_]+$\nthat is, only numbers, letters and underscore are allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
@@ -16,7 +16,8 @@
|
|||||||
<link rel="icon" type="image/png" href="{{ url_for('static', filename='nibr.ico') }}">
|
<link rel="icon" type="image/png" href="{{ url_for('static', filename='nibr.ico') }}">
|
||||||
{% for script in extra_scripts %}
|
{% for script in extra_scripts %}
|
||||||
<script src="{{ script }}"></script>
|
<script src="{{ script }}"></script>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<script src="{{ url_for('static', filename='js/annotation.js') }}"></script>
|
||||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -41,5 +42,10 @@
|
|||||||
<li><a href="/">homepage</a></li>
|
<li><a href="/">homepage</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</p>
|
</p>
|
||||||
|
<script>
|
||||||
|
$(() => {
|
||||||
|
$("a.new").click(new_annotation_callback);
|
||||||
|
})
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user