Define clusters in Tab 3 first...
';
return;
}
clusterInputs.forEach((row, i) => {
const clusterName = row.querySelector('input').value || `Cluster ${i}`;
const formGroup = document.createElement('div');
formGroup.className = 'scrna-form-group';
formGroup.innerHTML = `
`;
markerInputsDiv.appendChild(formGroup);
});
}
function updateMarkerTable() {
const markerTextareas = markerInputsDiv.querySelectorAll('textarea');
disp.markerTable.innerHTML = "";
if (markerTextareas.length === 0) {
disp.markerTable.innerHTML = '
| No clusters defined. |
';
return;
}
markerTextareas.forEach(ta => {
const clusterName = ta.dataset.clusterName;
const genes = ta.value || "N/A";
const tr = document.createElement('tr');
tr.innerHTML = `
${clusterName} | ${genes} | `;
disp.markerTable.appendChild(tr);
});
}
function updateUMAP() {
const clusterInputs = clusterInputsDiv.querySelectorAll('.scrna-cluster-input-row input');
if (clusterInputs.length === 0) {
chartPlaceholder.style.display = 'flex';
if (umapChart) umapChart.destroy();
umapChart = null;
disp.clusterCount.textContent = "0";
return;
}
chartPlaceholder.style.display = 'none';
disp.clusterCount.textContent = clusterInputs.length;
const datasets = [];
const numClusters = clusterInputs.length;
// Simple grid layout for cluster centers
const gridCols = Math.ceil(Math.sqrt(numClusters));
for(let i = 0; i < numClusters; i++) {
const clusterName = clusterInputs[i].value || `Cluster ${i}`;
const color = UMAP_COLORS[i % UMAP_COLORS.length];
// Position centers in a grid
const gridX = i % gridCols;
const gridY = Math.floor(i / gridCols);
const cx = 10 + gridX * 20;
const cy = 10 + gridY * 20;
const points = generateClusterData(Math.floor(Math.random() * 500) + 200, cx, cy, 5);
datasets.push({
label: clusterName,
data: points,
backgroundColor: color,
pointRadius: 2,
pointHoverRadius: 4
});
}
if (umapChart) {
umapChart.destroy();
}
umapChart = new Chart(chartCanvas, {
type: 'scatter',
data: { datasets: datasets },
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { display: false },
y: { display: false }
},
plugins: {
legend: { position: 'bottom', labels: { usePointStyle: true } },
tooltip: {
callbacks: {
label: (context) => ` ${context.dataset.label}`
}
}
}
}
});
}
function addClusterRow(name = '') {
const row = document.createElement('div');
row.className = 'scrna-cluster-input-row';
const inputId = `scrna-cluster-${clusterCounter++}`;
row.innerHTML = `
`;
row.querySelector('.scrna-btn-sm-del').addEventListener('click', () => {
row.remove();
});
clusterInputsDiv.appendChild(row);
}
function loadSampleData() {
inputs.title.value = "scRNA-seq Analysis of Healthy Mouse Spleen";
inputs.pi.value = "Dr. J. P. Allison, University of Texas";
inputs.abstract.value = "This experiment profiles the cellular landscape of a C57BL/6 mouse spleen to establish a baseline reference map of immune cell populations. We identify major T-cell, B-cell, and Myeloid populations.";
inputs.cells.value = 10245;
inputs.genes.value = 1850;
inputs.mito.value = 4.2;
// Clear existing dynamic fields
clusterInputsDiv.innerHTML = "";
markerInputsDiv.innerHTML = "";
// Add clusters
addClusterRow("T-Cells (Cluster 0)");
addClusterRow("B-Cells (Cluster 1, 3)");
addClusterRow("Monocytes (Cluster 2)");
// Update markers (which now exist)
updateMarkerInputs();
document.getElementById('scrna-marker-0').value = "CD3E, CD4, CD8A, IL2R";
document.getElementById('scrna-marker-1').value = "CD19, CD79A, MS4A1";
document.getElementById('scrna-marker-2').value = "CD14, LYZ, S100A9";
inputs.conclusion.value = "The analysis successfully resolved all major expected splenic populations. T-Cells (CD3+) and B-Cells (CD19+) were the most abundant. Monocytes (CD14+) represented a smaller, distinct population. This dataset serves as a robust control for future disease models.";
updateDashboard();
alert("Sample Data Loaded. Check the Dashboard.");
openTab(0);
}
// --- Navigation ---
function openTab(index) {
tabPanes.forEach(p => p.classList.remove('scrna-active'));
tabLinks.forEach(l => l.classList.remove('scrna-active'));
tabPanes[index].classList.add('scrna-active');
tabLinks[index].classList.add('scrna-active');
currentTab = index;
if (currentTab === 0) updateDashboard(); // Always refresh dashboard on view
prevBtn.style.display = (currentTab === 0) ? 'none' : 'inline-block';
nextBtn.style.display = (currentTab === tabLinks.length - 1) ? 'none' : 'inline-block';
}
// --- PDF Export ---
async function generatePDF() {
const element = document.getElementById('scrna-pdf-export-area');
const originalBtnText = downloadPdfBtn.textContent;
downloadPdfBtn.textContent = "Generating Report...";
downloadPdfBtn.disabled = true;
updateDashboard();
try {
const canvas = await html2canvas(element, {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff'
});
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF('p', 'mm', 'a4');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 10;
const printWidth = pdfWidth - (margin * 2);
const imgHeight = (canvas.height * printWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', margin, margin, printWidth, imgHeight);
const safeName = (inputs.title.value || "scRNA_Report").replace(/[^a-z0-9]/gi, '_');
pdf.save(`${safeName}.pdf`);
} catch (error) {
console.error("PDF Error", error);
alert("Error creating PDF.");
} finally {
downloadPdfBtn.textContent = originalBtnText;
downloadPdfBtn.disabled = false;
}
}
// --- Event Listeners ---
tabLinks.forEach((link, index) => {
link.addEventListener('click', () => openTab(index));
});
prevBtn.addEventListener('click', () => openTab(currentTab - 1));
nextBtn.addEventListener('click', () => openTab(currentTab + 1));
addClusterBtn.addEventListener('click', () => addClusterRow());
updatePlotBtn.addEventListener('click', () => {
updateMarkerInputs();
updateDashboard();
alert("Dashboard and Marker fields updated!");
});
loadSampleBtn.addEventListener('click', loadSampleData);
resetBtn.addEventListener('click', () => {
if(confirm("Clear all data?")) {
Object.values(inputs).forEach(i => i.value = "");
clusterInputsDiv.innerHTML = "";
markerInputsDiv.innerHTML = '
Define clusters in Tab 3 first...
';
updateDashboard();
}
});
downloadPdfBtn.addEventListener('click', generatePDF);
// Init
updateDashboard();
});