-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathform.js
More file actions
126 lines (108 loc) · 4.43 KB
/
form.js
File metadata and controls
126 lines (108 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
$(document).ready(function() {
function updateProteinSequenceField() {
var sessionType = $('#batch_connect_session_context_session_type').val();
var label = 'Input Sequence';
var helpText = 'Enter the input sequence';
var rows = 5;
if (sessionType === 'AlphaFold 2') {
label = 'Input Sequence (FASTA format)';
helpText = 'Must be in FASTA format for AlphaFold 2';
rows = 5;
} else if (sessionType === 'AlphaFold 3') {
label = 'Input Sequence (JSON format)';
helpText = 'Must be in compatible JSON format (specifications are in the <a href="https://github.com/google-deepmind/alphafold3/blob/main/docs/input.md" target="_blank">AlphaFold 3 documentation)</a>.';
rows = 15;
}
var $formGroup = $('#batch_connect_session_context_protein_sequence').closest('.form-group');
$formGroup.find('label').text(label);
$formGroup.find('.form-text').html(helpText);
$('#batch_connect_session_context_protein_sequence').attr('rows', rows);
}
function updateFormVisibility() {
var sessionType = $('#batch_connect_session_context_session_type').val();
if (sessionType === 'AlphaFold 2') {
$('#batch_connect_session_context_agree_terms').closest('.form-group').hide();
} else {
$('#batch_connect_session_context_agree_terms').closest('.form-group').show();
}
}
function isValidFASTA(sequence) {
const fastaPattern = /^>.*\n([A-Za-z\n]+)$/;
return fastaPattern.test(sequence);
}
function isValidJSON(sequence) {
try {
const json = JSON.parse(sequence);
if (!(typeof json === 'object' && json !== null)) {
displayError("JSON input must be an object");
return false;
}
if (!json.name) {
displayError("JSON input must contain a 'name' field");
return false;
}
if (!Array.isArray(json.modelSeeds)) {
displayError("JSON input must contain a 'modelSeeds' array");
return false;
}
if (!Array.isArray(json.sequences)) {
displayError("JSON input must contain a 'sequences' array");
return false;
}
for (const entity of json.sequences) {
if (!entity.protein && !entity.dnaSequence && !entity.rnaSequence && !entity.ligand && !entity.ion) {
displayError("Each sequence must contain at least one of 'protein', 'dnaSequence', 'rnaSequence', 'ligand', or 'ion'");
return false;
}
if (entity.protein) {
if (!entity.protein.id || !entity.protein.sequence) {
displayError("Each 'protein' must contain 'id' and 'sequence'");
return false;
}
}
}
return true;
} catch (e) {
displayError("Invalid JSON format: " + e.message);
return false;
}
}
const form = $('#new_batch_connect_session_context');
const errorContainer = $('<div class="alert alert-danger" style="display: none;"></div>');
form.prepend(errorContainer);
function displayError(message) {
errorContainer.text(message).show();
}
function validateInput() {
const sequence = $('#batch_connect_session_context_protein_sequence').val().trim();
const sessionType = $('#batch_connect_session_context_session_type').val();
errorContainer.hide();
if (sessionType === 'AlphaFold 3') {
if (!isValidJSON(sequence)) {
displayError("Invalid Input: Enter a valid JSON sequence for AlphaFold 3");
return false;
}
} else if (sessionType === 'AlphaFold 2') {
if (!isValidFASTA(sequence)) {
displayError("Invalid Input: Enter a valid FASTA sequence for AlphaFold 2");
return false;
}
}
return true;
}
form.on('submit', function(event) {
if (!validateInput()) {
event.preventDefault();
$('input[type="submit"]').prop('disabled', false).removeAttr('data-disable-with');
}
});
$('#batch_connect_session_context_protein_sequence').on('input', function() {
$('input[type="submit"]').prop('disabled', false).removeAttr('data-disable-with');
});
$('#batch_connect_session_context_session_type').change(function() {
updateProteinSequenceField();
updateFormVisibility();
});
updateProteinSequenceField();
updateFormVisibility();
});