Switch to unified view

a b/web/assets/js/api-client.js
1
/**
2
 * DNAnalyzer API Client
3
 * 
4
 * This module provides functions to interact with the DNAnalyzer API endpoints.
5
 * It enables the website to access the core DNAnalyzer features through a unified interface.
6
 * 
7
 * @version 1.0.0
8
 */
9
10
const DNAnalyzerAPI = {
11
    // Base URL for API requests - configurable for different environments
12
    baseUrl: 'http://localhost:8080/api/v1',
13
14
    /**
15
     * Set the base URL for the API
16
     * @param {string} url - The base URL for the API
17
     */
18
    setBaseUrl(url) {
19
        this.baseUrl = url;
20
    },
21
22
    /**
23
     * Check if the API is online
24
     * @returns {Promise<Object>} - API status information
25
     */
26
    async checkStatus() {
27
        try {
28
            const response = await fetch(`${this.baseUrl}/status`);
29
            if (!response.ok) {
30
                throw new Error('API status check failed');
31
            }
32
            return await response.json();
33
        } catch (error) {
34
            console.error('API status check error:', error);
35
            throw error;
36
        }
37
    },
38
39
    /**
40
     * Analyze a DNA file with various options
41
     * @param {File} dnaFile - The DNA file to analyze
42
     * @param {Object} options - Analysis options
43
     * @returns {Promise<Object>} - Analysis results
44
     */
45
    async analyzeDNA(dnaFile, options = {}) {
46
        try {
47
            const formData = new FormData();
48
            formData.append('dnaFile', dnaFile);
49
            
50
            // Add options to form data
51
            if (options.amino) formData.append('amino', options.amino);
52
            if (options.minCount) formData.append('minCount', options.minCount);
53
            if (options.maxCount) formData.append('maxCount', options.maxCount);
54
            if (options.reverse) formData.append('reverse', options.reverse);
55
            if (options.rcomplement) formData.append('rcomplement', options.rcomplement);
56
            if (options.codons) formData.append('codons', options.codons);
57
            if (options.coverage) formData.append('coverage', options.coverage);
58
            if (options.longest) formData.append('longest', options.longest);
59
            if (options.format) formData.append('format', options.format);
60
            
61
            const response = await fetch(`${this.baseUrl}/analyze`, {
62
                method: 'POST',
63
                body: formData
64
            });
65
            
66
            if (!response.ok) {
67
                const errorData = await response.json();
68
                throw new Error(errorData.message || 'DNA analysis failed');
69
            }
70
            
71
            // Handle different response formats
72
            const contentType = response.headers.get('content-type');
73
            if (contentType && contentType.includes('application/json')) {
74
                return await response.json();
75
            } else {
76
                return await response.text();
77
            }
78
        } catch (error) {
79
            console.error('DNA analysis error:', error);
80
            throw error;
81
        }
82
    },
83
84
    /**
85
     * Analyze base pair composition of a DNA sequence
86
     * @param {string} sequence - The DNA sequence
87
     * @returns {Promise<Object>} - Base pair analysis results
88
     */
89
    async analyzeBasePairs(sequence) {
90
        try {
91
            const response = await fetch(`${this.baseUrl}/base-pairs`, {
92
                method: 'POST',
93
                headers: {
94
                    'Content-Type': 'application/json'
95
                },
96
                body: JSON.stringify({ sequence })
97
            });
98
            
99
            if (!response.ok) {
100
                const errorData = await response.json();
101
                throw new Error(errorData.message || 'Base pair analysis failed');
102
            }
103
            
104
            return await response.json();
105
        } catch (error) {
106
            console.error('Base pair analysis error:', error);
107
            throw error;
108
        }
109
    },
110
111
    /**
112
     * Find proteins in a DNA sequence
113
     * @param {string} sequence - The DNA sequence
114
     * @param {string} aminoAcid - The amino acid to start proteins with
115
     * @returns {Promise<Object>} - Protein analysis results
116
     */
117
    async findProteins(sequence, aminoAcid = 'M') {
118
        try {
119
            const response = await fetch(`${this.baseUrl}/find-proteins`, {
120
                method: 'POST',
121
                headers: {
122
                    'Content-Type': 'application/json'
123
                },
124
                body: JSON.stringify({
125
                    sequence,
126
                    aminoAcid
127
                })
128
            });
129
            
130
            if (!response.ok) {
131
                const errorData = await response.json();
132
                throw new Error(errorData.message || 'Protein finding failed');
133
            }
134
            
135
            return await response.json();
136
        } catch (error) {
137
            console.error('Protein finding error:', error);
138
            throw error;
139
        }
140
    },
141
142
    /**
143
     * Analyze reading frames in a DNA sequence
144
     * @param {string} sequence - The DNA sequence
145
     * @param {number} minLength - Minimum length for potential genes
146
     * @returns {Promise<Object>} - Reading frame analysis results
147
     */
148
    async analyzeReadingFrames(sequence, minLength = 300) {
149
        try {
150
            const response = await fetch(`${this.baseUrl}/reading-frames`, {
151
                method: 'POST',
152
                headers: {
153
                    'Content-Type': 'application/json'
154
                },
155
                body: JSON.stringify({
156
                    sequence,
157
                    minLength
158
                })
159
            });
160
            
161
            if (!response.ok) {
162
                const errorData = await response.json();
163
                throw new Error(errorData.message || 'Reading frame analysis failed');
164
            }
165
            
166
            return await response.json();
167
        } catch (error) {
168
            console.error('Reading frame analysis error:', error);
169
            throw error;
170
        }
171
    },
172
173
    /**
174
     * Analyze genetic testing data (23andMe, AncestryDNA, etc.)
175
     * @param {File} geneticFile - The genetic data file
176
     * @param {boolean} snpAnalysis - Whether to include detailed SNP analysis
177
     * @returns {Promise<Object>} - Genetic analysis results
178
     */
179
    async analyzeGeneticData(geneticFile, snpAnalysis = false) {
180
        try {
181
            const formData = new FormData();
182
            formData.append('geneticFile', geneticFile);
183
            formData.append('snpAnalysis', snpAnalysis);
184
            
185
            const response = await fetch(`${this.baseUrl}/analyze-genetic`, {
186
                method: 'POST',
187
                body: formData
188
            });
189
            
190
            if (!response.ok) {
191
                const errorData = await response.json();
192
                throw new Error(errorData.message || 'Genetic data analysis failed');
193
            }
194
            
195
            return await response.json();
196
        } catch (error) {
197
            console.error('Genetic data analysis error:', error);
198
            throw error;
199
        }
200
    },
201
202
    /**
203
     * Manipulate a DNA sequence (reverse, complement)
204
     * @param {string} sequence - The DNA sequence
205
     * @param {boolean} reverse - Whether to reverse the sequence
206
     * @param {boolean} complement - Whether to get the complement
207
     * @returns {Promise<Object>} - Manipulated sequence results
208
     */
209
    async manipulateDNA(sequence, reverse = false, complement = false) {
210
        try {
211
            const response = await fetch(`${this.baseUrl}/manipulate`, {
212
                method: 'POST',
213
                headers: {
214
                    'Content-Type': 'application/json'
215
                },
216
                body: JSON.stringify({
217
                    sequence,
218
                    reverse,
219
                    complement
220
                })
221
            });
222
            
223
            if (!response.ok) {
224
                const errorData = await response.json();
225
                throw new Error(errorData.message || 'DNA manipulation failed');
226
            }
227
            
228
            return await response.json();
229
        } catch (error) {
230
            console.error('DNA manipulation error:', error);
231
            throw error;
232
        }
233
    },
234
235
    /**
236
     * Parse a DNA file (FASTA, FASTQ)
237
     * @param {File} file - The file to parse
238
     * @returns {Promise<Object>} - Parsed DNA sequence
239
     */
240
    async parseFile(file) {
241
        try {
242
            const formData = new FormData();
243
            formData.append('file', file);
244
            
245
            const response = await fetch(`${this.baseUrl}/parse`, {
246
                method: 'POST',
247
                body: formData
248
            });
249
            
250
            if (!response.ok) {
251
                const errorData = await response.json();
252
                throw new Error(errorData.message || 'File parsing failed');
253
            }
254
            
255
            return await response.json();
256
        } catch (error) {
257
            console.error('File parsing error:', error);
258
            throw error;
259
        }
260
    }
261
};
262
263
// Export the API client for use in other modules
264
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
265
    module.exports = DNAnalyzerAPI;
266
} else {
267
    window.DNAnalyzerAPI = DNAnalyzerAPI;
268
}