Skip to content

Commit

Permalink
Merge pull request #52 from daisybio/refactorKonstantin
Browse files Browse the repository at this point in the history
Shareable links and custom save time for analyses
  • Loading branch information
Eeeeelias authored Sep 12, 2024
2 parents ccd8c97 + ab454e0 commit 8771770
Show file tree
Hide file tree
Showing 18 changed files with 1,363 additions and 1,288 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,6 @@ cython_debug/
# IntelliJ like IDEs (e.g. PyCharm)
.idea

tmp*
tmp*
container/nease_events

42 changes: 37 additions & 5 deletions container/domain/Process/nease_output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pickle
import traceback
from io import StringIO
Expand All @@ -8,6 +9,7 @@

from matplotlib import pyplot as plt

from domain.models import NeaseSaveLocationMapping
from domain.nease import nease
from domain.nease.process import webify_table
from django.conf import settings
Expand All @@ -16,12 +18,14 @@
images_path = os.path.join(settings.MEDIA_ROOT, 'images/')
data_path = os.path.join(settings.MEDIA_ROOT, 'nease_tables/')
nease_path = 'nease_events/'
# The subdirectories contain files saved for one week, one month, and six months. To be very lenient, we calculated every month with 31 days.
days_to_folder = {"0": nease_path+"zero_days/", "7": nease_path+"seven_days/", "31": nease_path+"thirtyone_days/", "186": nease_path+"onehundredeightysix_days/"}
default_path = days_to_folder["7"]

for path in [images_path, data_path, nease_path]:
for path in [images_path, data_path] + list(days_to_folder.values()):
if not os.path.exists(path):
os.makedirs(path)


# web_tables_options is a dictionary that contains the options for webifying the tables
web_tables_options = {
'domains': {'link_col': ['Gene name', 'Gene stable ID', 'Exon stable ID', 'Pfam ID'],
Expand Down Expand Up @@ -50,7 +54,7 @@
}


def run_nease(data, organism, params):
def run_nease(data, organism, params, file_name='', custom_name=''):
run_id = str(uuid.uuid4())
image_path = images_path + run_id

Expand Down Expand Up @@ -97,7 +101,8 @@ def run_nease(data, organism, params):
value.drop(columns=['Unnamed: 0'], inplace=True)

# save events to pickle
events.save(nease_path + run_id)
events.save(default_path + run_id)
NeaseSaveLocationMapping(run_id=run_id, saved_for_days=7, file_name=file_name, custom_name=custom_name).save()
return events, info_tables, run_id


Expand Down Expand Up @@ -126,7 +131,13 @@ def read_extra_spaces(file_obj):


def get_nease_events(run_id):
events = nease.load(nease_path + run_id + '.pkl')
days = NeaseSaveLocationMapping.get_saved_for_days(run_id)
if days not in days_to_folder:
file_path = default_path
else:
file_path = days_to_folder[str(days)]
print(f"Loading events from {file_path + run_id + '.pkl'}")
events = nease.load(file_path + run_id + '.pkl')
info_tables = {}
try:
domains = webify_table(pd.read_csv(f"{data_path}{run_id}_domains.csv"), web_tables_options['domains'])
Expand Down Expand Up @@ -243,6 +254,27 @@ def create_plot(terms, pvalues, cut_off, filename):
plt.clf()
plt.close()

def change_save_timing(run_id, days):
mapping = NeaseSaveLocationMapping.objects.get(run_id=run_id)
current_days_folder = mapping.get_number_of_saved_for_days()
if days not in days_to_folder:
new_file_path = default_path
else:
new_file_path = days_to_folder[str(days)]
if current_days_folder not in days_to_folder:
old_file_path = default_path
else:
old_file_path = days_to_folder[str(current_days_folder)]
# move the file
os.rename(old_file_path + run_id + '.pkl', new_file_path + run_id + '.pkl')
# update the database
mapping.saved_for_days = int(days)
mapping.save()

return json.dumps(
{"logmessage": "Changing the save timing from " + str(current_days_folder) + " to " + str(days) + " was successful.",
"days_left": mapping.days_left()}
)

def match_name_with_format(filename):
name_matches = {'deltapsi': 'MAIJQ',
Expand Down
38 changes: 38 additions & 0 deletions container/domain/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 2.2.28 on 2024-09-09 12:32

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Domain',
fields=[
('pfam_id', models.CharField(db_index=True, max_length=10, primary_key=True, serialize=False)),
('symbol', models.CharField(max_length=20)),
('description', models.CharField(max_length=150)),
],
),
migrations.CreateModel(
name='Gene',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ensembl_id', models.CharField(max_length=20)),
('gene_symbol', models.CharField(db_index=True, max_length=20)),
],
),
migrations.CreateModel(
name='NeaseSaveLocationMapping',
fields=[
('run_id', models.CharField(db_index=True, max_length=36, primary_key=True, serialize=False)),
('saved_for_days', models.IntegerField()),
('date_of_creation', models.DateTimeField(auto_now_add=True)),
],
),
]
18 changes: 18 additions & 0 deletions container/domain/migrations/0002_neasesavelocationmapping_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.28 on 2024-09-09 13:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('domain', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='neasesavelocationmapping',
name='name',
field=models.CharField(default='', max_length=255),
),
]
27 changes: 27 additions & 0 deletions container/domain/migrations/0003_auto_20240910_1357.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.2.28 on 2024-09-10 11:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('domain', '0002_neasesavelocationmapping_name'),
]

operations = [
migrations.RemoveField(
model_name='neasesavelocationmapping',
name='name',
),
migrations.AddField(
model_name='neasesavelocationmapping',
name='custom_name',
field=models.CharField(default='', max_length=255),
),
migrations.AddField(
model_name='neasesavelocationmapping',
name='file_name',
field=models.CharField(default='', max_length=255),
),
]
Empty file.
26 changes: 26 additions & 0 deletions container/domain/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone


class Gene(models.Model):
Expand All @@ -16,3 +17,28 @@ class Domain(models.Model):

#def __str__(self):
# return f"Pfam: {self.pfam_id} - Symbol: {self.symbol}"

class NeaseSaveLocationMapping(models.Model):
run_id = models.CharField(max_length=36, primary_key=True, db_index=True)
saved_for_days = models.IntegerField()
date_of_creation = models.DateTimeField(auto_now_add=True)
file_name = models.CharField(max_length=255, default='')
custom_name = models.CharField(max_length=255, default='')

# Method to query the database for the run_id and return the saved_for_days
@staticmethod
def get_saved_for_days(run_id):
return str(NeaseSaveLocationMapping.objects.get(run_id=run_id).saved_for_days)

def get_number_of_saved_for_days(self):
return str(self.saved_for_days)

# Calculate how many days are left until deletion, negative values are set to 0
def days_left(self):
return max(0, self.saved_for_days - (timezone.now() - self.date_of_creation).days)

# Get the custom name and return None if it is empty
def get_custom_name(self):
if self.custom_name == '':
return None
return self.custom_name
2 changes: 1 addition & 1 deletion container/domain/static/domain/initStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
const newValue =
value == null
? null
: { value, expiresAt: getExpiration(expiresInDays), name }
: { value, expiresAt: getExpiration(expiresInDays), name , createdAt: Date.now()}
setExpiration(newValue?.expiresAt)
storage.set(prefixedKey, newValue)
},
Expand Down
9 changes: 4 additions & 5 deletions container/domain/static/domain/loadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ function removeExpiredData(key) {
}

// Function to create an HTML template
function createHtmlTemplate(data, expiresInDays = 7) {
function createHtmlTemplate(data) {
// subtract 7 days from the expiry date
const createdDate = new Date(data.expiresAt) - (expiresInDays * 24 * 60 * 60 * 1000);
const createdDate = new Date(data.createdAt);
// format to readable date
const formattedDate = new Date(createdDate).toLocaleString();

return `
<div class="row">
<div class="col-md-12 my-1">
<div class="card previous-card" onclick="prevAnalysis('${data.value}', '${data.name}')">
<div class="card previous-card" onclick="prevAnalysis('${data.value}')">
<div class="card-body">
<h6 class="card-title" style="display: inline; font-weight: bold">${data.name}</h6>
<p class="card-text mx-1" style="display: inline; color: gray">●</p>
Expand All @@ -49,8 +49,7 @@ function appendTemplateToDiv(template, divId) {
}
}

function prevAnalysis(id, name) {
function prevAnalysis(id) {
document.getElementById('previous_analyses_input').value = id;
document.getElementById('previous_analyses_name').value = name;
document.getElementById('submit').click();
}
Loading

0 comments on commit 8771770

Please sign in to comment.