Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (89 loc) · 3.18 KB
/
Copy pathserver.js
File metadata and controls
107 lines (89 loc) · 3.18 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
import express from 'express';
import fileUpload from 'express-fileupload';
import { parse } from 'csv-parse/sync';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
app.use(fileUpload());
app.use(express.static('public'));
// Process TSV/CSV data
function processData(fileContent, delimiter) {
return parse(fileContent, {
columns: true,
skip_empty_lines: true,
delimiter: delimiter
});
}
// Process phenotype and SNP data
function processPhenoAndSnpData(phenoData, alleleData) {
// Rename strain to Accession_ID in allele data
const processedAlleleData = alleleData.map(row => ({
...row,
Accession_ID: row.strain
}));
// Merge phenotype and allele data
const mergedData = phenoData.map(pheno => {
const allele = processedAlleleData.find(a => a.Accession_ID === pheno.Accession_ID);
return {
...pheno,
alt: allele ? allele.alt : null
};
});
// Determine allele type (major/minor)
const altValues = mergedData.map(row => row.alt).filter(Boolean);
const mostCommonAllele = mode(altValues);
return mergedData.map(row => ({
...row,
allele: row.alt === mostCommonAllele ? 'minor' : 'major'
}));
}
// Helper function to calculate mode
function mode(arr) {
return arr.sort((a,b) =>
arr.filter(v => v === a).length - arr.filter(v => v === b).length
).pop();
}
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'public', 'index.html'));
});
app.post('/upload', async (req, res) => {
try {
if (!req.files || !req.files.pheno_file || !req.files.snp_files) {
return res.status(400).send('Please upload all required files');
}
const phenoFile = req.files.pheno_file;
const snpFiles = Array.isArray(req.files.snp_files)
? req.files.snp_files
: [req.files.snp_files];
// Process phenotype data
const phenoData = processData(phenoFile.data.toString(), '\t');
// Process each SNP file and prepare plot data
const processedData = [];
for (const snpFile of snpFiles) {
const alleleData = processData(snpFile.data.toString(), ',');
const mergedData = processPhenoAndSnpData(phenoData, alleleData);
// Split data by allele type
const majorData = mergedData.filter(row => row.allele === 'major');
const minorData = mergedData.filter(row => row.allele === 'minor');
processedData.push({
fileName: snpFile.name,
majorData,
minorData
});
}
// Send processed data back to client for plotting
res.json({
success: true,
data: processedData
});
} catch (error) {
console.error('Error processing files:', error);
res.status(500).send('An error occurred during file processing');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});