Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates from DNAnexus on 20240802 #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions bco-tool/app/services/custom_bco_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import Dict
import requests
import json

class CustomBcoDB():
def __init__(self, host_url: str, credentials: dict):
self.__host_url__ = host_url
self.__credentials__ = credentials

def upload(self) -> Dict[str, str]:
try:
with open("app/schemas/biocompute_export.json", "r") as file:
file_content = json.load(file)

headers = {
"Content-type": "application/json; charset=UTF-8",
}

if self.__credentials__ and len(self.__credentials__) > 0:
headers.update(self.__credentials__)

response = requests.post(
url=self.__host_url__,
data=json.dumps({ "contents": file_content }),
headers=headers
)

if response.status_code == 200:
print("File uploaded to Custom BCO DB")
return {
"code": 200,
"message": "File uploaded to Custom BCO DB",
}
else:
print("Failed to update file to Custom BCO DB", response.__dict__)
return {
"code": response.status_code,
"message": "Failed to update file to Custom BCO DB"
}
except Exception as e:
print(f"Error uploading to Custom BCO DB -- {repr(e)}")
return {
"code": 500,
"message": "Internal server error"
}
101 changes: 101 additions & 0 deletions bco-tool/app/static/exportfile_custom_bco_db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
$(document).ready(function() {
bindEvents();

function bindEvents() {
// config to show toast on top center
toastr.options = { "positionClass": "toast-top-center" };

// enable/disable the submit button if url is changed
$("#bco_custom_db_url").on("input", (event) => {
$(".btn-submit-custom-bco-db").prop("disabled", !event.target.value);
});

// handle event when clicking on the "add/remove credential" button
$(document).on('click', '.btn-add-credential', addCredential);
$(document).on('click', '.btn-remove-credential', removeCredential);

// handle event when clicking on the submit button
$('.btn-submit-custom-bco-db').on('click', exportFileToCustomBcoDb);
}

function exportFileToCustomBcoDb(event) {
const btnSubmit = event.target;
const url = $("#bco_custom_db_url").val();

if (!url) {
return toastr.error("Please enter data for all fields to upload custom BCO database");
}

const setSubmitting = (isSubmitting) => {
if (isSubmitting) {
btnSubmit.disabled = true;
btnSubmit.innerHTML = "Submitting...";
} else {
btnSubmit.disabled = false;
btnSubmit.innerHTML = "Submit";
}
};

// submit form data
setSubmitting(true);
$.ajax({
type: "POST",
headers: { "X-CSRFToken": getCookie("csrftoken") },
url: "export_to_custom_bco_db",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
url,
credentials: getCredentials(),
}),
success: (message) => {
toastr.success(message)
setSubmitting(false);
$("#bco_custom_db_export_container").modal("hide");
},
error: (err) => {
toastr.error(err.responseText || err.statusText || "Error when exporting file to custom BCO database");
setSubmitting(false);
},
});
}

function addCredential() {
const credentialContainer = $("#credentials-container");
const credentialRow = $("<div></div>")
.css("display", "flex")
.css("gap", "10px")
.addClass("mb-2")
.html(`
<input type="text" class="form-control rounded-lg" placeholder="key" />
<input type="text" class="form-control rounded-lg" placeholder="value" />
<button
type="button"
class="btn-remove-credential btn btn-outline-danger rounded-lg"
>
Remove
</button>
`);
credentialContainer.append(credentialRow);
}

function removeCredential() {
const button = $(this);
const credentialRow = button.parent();
credentialRow.remove();
}

function getCredentials() {
const credentials = {};

$("#credentials-container > div").each(function() {
const credentialKey = $(this).find("input[placeholder='key']").val();
const credentialValue = $(this).find("input[placeholder='value']").val();

if (credentialKey && credentialValue) {
credentials[credentialKey] = credentialValue;
}
});

return credentials;
}
});
3 changes: 3 additions & 0 deletions bco-tool/app/static/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ $(document).ready(function() {
$("#user_token").val("")
$('#bco_db_export_container').modal('show')
}
else if (export_option == "Custom BCO DB") {
$('#bco_custom_db_export_container').modal('show');
}
else if (export_option=="PFDA BCO File"){
$("#pfda_export_space_selector_div").css("visibility", "hidden")
$("#pfda_export_folder_selector_div").css("visibility", "hidden")
Expand Down
3 changes: 3 additions & 0 deletions bco-tool/app/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@
border-color: #007bff;
background-color: #f8f9fa;
}
.rounded-lg {
border-radius: 0.5rem;
}
/* .lorem{
content: '-';
} */
Expand Down
8 changes: 8 additions & 0 deletions bco-tool/app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>

<!-- toastr -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

<script src="{% static 'scripts.js' %}"></script>
</head>

Expand Down Expand Up @@ -75,6 +80,8 @@
<a class="dropdown-item" data-value="BCO DB" href="#">BCO Repository</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" data-value="PFDA BCO File" href="#">precisionFDA</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" data-value="Custom BCO DB" href="#">Custom BCO database</a>
</div>
</li>
</ul>
Expand Down Expand Up @@ -125,6 +132,7 @@
{% include "exportfile_dna_nexus.html" %}
{% include "exportfile_pfda.html" %}
{% include "exportfile_bco_db.html" %}
{% include "exportfile_custom_bco_db.html" %}
{% include "description_modal.html" %}
{% include "image_zoom_modal.html" %}
</div>
Expand Down
97 changes: 97 additions & 0 deletions bco-tool/app/templates/exportfile_custom_bco_db.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{% load static %}
{% block file_uploader %}
{% csrf_token %}
<div
id="bco_custom_db_export_container"
class="modal"
role="dialog"
aria-labelledby="customBCOModalLabel"
aria-hidden="true"
>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="background-color: navy">
<h4 class="modal-title" id="customBCOModalLabel" style="color: white">
Custom BCO database
</h4>
<button
type="button"
class="close"
data-dismiss="modal"
style="color: white"
aria-label="Close"
>
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="row mb-2">
<div class="col-3 d-flex align-items-center">
<label for="bco_custom_db_url" class="mb-0">BCO DB URL</label>
</div>
<div class="col-9">
<input
type="text"
class="form-control rounded-lg"
id="bco_custom_db_url"
placeholder="url"
/>
</div>
</div>
<div class="mb-2 text-left">
Credentials
</div>
<div id="credentials-container">
<div
class="mb-2"
style="
display: flex;
gap: 10px;
"
>
<input
type="text"
class="form-control rounded-lg"
placeholder="key"
/>
<input
type="text"
class="form-control rounded-lg"
placeholder="value"
/>
<button
type="button"
class="btn-remove-credential btn btn-outline-danger rounded-lg"
>
Remove
</button>
</div>
</div>
<button
type="button"
class="btn-add-credential btn btn-outline-primary btn-block mb-2 rounded-lg"
>
Add
</button>
</div>
<div class="modal-footer">
<button
type="button"
class="btn rounded-lg"
data-dismiss="modal"
>
Close
</button>
<button
type="button"
class="btn-submit-custom-bco-db btn btn-primary rounded-lg"
disabled
>
Submit
</button>
</div>
</div>
</div>
</div>
<script src="{% static 'exportfile_custom_bco_db.js' %}"></script>
{% endblock %}
22 changes: 21 additions & 1 deletion bco-tool/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import OrderedDict

from app.services.bco_db import BcoDB
from app.services.custom_bco_db import CustomBcoDB
from app.services.cwl import CWL
from app.services.dataservices.bco_data_service import \
BcoDataService, ComparisonBcoDataService
Expand All @@ -20,7 +21,7 @@
from django.contrib import messages
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.http import HttpResponse, JsonResponse, QueryDict
from django.http import HttpResponse, HttpResponseNotAllowed, HttpResponseServerError, JsonResponse, QueryDict
from django.shortcuts import render
from django.views.decorators.csrf import csrf_protect
from script import CompileWorkflow
Expand Down Expand Up @@ -1287,6 +1288,25 @@ def export_to_bco_db(request):
except Exception as e:
return HttpResponse(status=400)

def export_to_custom_bco_db(request) -> HttpResponse:
if request.method != "POST":
return HttpResponseNotAllowed(permitted_methods="POST")

try:
# create biocompute_export.json file
create_export_bco()

# get request data
requestData = json.loads(request.body)
url = requestData.get("url")
credentials = requestData.get("credentials")

# call request
response = CustomBcoDB(host_url=url, credentials=credentials).upload()
return HttpResponse(status=response.get("code"), content=response.get("message"))
except Exception as e:
return HttpResponseServerError()

def save_bco_editor(request):
try:
if request.method == "POST":
Expand Down
4 changes: 2 additions & 2 deletions bco-tool/bconexus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["*"]


# Application definition
Expand Down Expand Up @@ -102,7 +102,7 @@
"formatter": 'formatter_1',
"class": "logging.handlers.RotatingFileHandler",
"filename": "./Logs/user.log",
"maxBytes":10485760,
"maxBytes":10485760,
"backupCount":1
},
},
Expand Down
1 change: 1 addition & 0 deletions bco-tool/bconexus/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
path('export_dowload_bco', export_dowload_bco, name='export_dowload_bco'),
path('export_bco_to_dnanexus', export_bco_to_dnanexus, name='export_bco_to_dnanexus'),
path('export_to_bco_db', export_to_bco_db, name='export_to_bco_db'),
path('export_to_custom_bco_db', export_to_custom_bco_db, name='export_to_custom_bco_db'),
path('search', search, name='search'),
path('save_bco_editor',save_bco_editor, name='save_bco_editor'),
path('validate_bco_editor',validate_bco_editor, name='validate_bco_editor'),
Expand Down
1 change: 1 addition & 0 deletions bco-tool/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ xhtml2pdf==0.2.1
xlrd==1.2.0
xtermcolor==1.3
zipp==3.1.0
tzdata==2024.1
1 change: 1 addition & 0 deletions bco-tool/requirements_latest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ urllib3==1.26.10
websocket-client==0.54.0
xdg==5.1.1
zipp==3.8.1
tzdata==2024.1