source: js/human_3d_alignment/src/utils/genotypes.js @ 911

Last change on this file since 911 was 911, checked in by Maciej Komosinski, 4 years ago

Added the actual functionality of the app in place of previous draft

File size: 2.8 KB
Line 
1/*global Module*/
2"use strict";
3
4/**
5 * Class to keep genotypes data
6 */
7class Genotypes {
8
9    /**
10     * Default constructor for genotypes object
11     * @param {string} path path to .gen file with genotypes
12     */
13    constructor(obj, path) {
14        this.parent = obj;
15        this.loaded = false;
16        this.id = [];
17        this.name = [];
18        this.genotype = [];
19        this.readGenotypesFromFile(path);
20       
21    }
22
23    /**
24     * Read genotypes from genotypes.gen file.
25     * @param {string} file path to genotypes.gen
26     */
27    readGenotypesFromFile(file) {
28        let rawFile = new XMLHttpRequest();
29        rawFile.open("GET", file, true);
30        rawFile.onload = (e) => {
31            if (rawFile.readyState === 4) {
32                if (rawFile.status === 200) {
33                    this.readGenotypesFromText(rawFile.responseText);
34                } else {
35                    console.error(rawFile.statusText);
36                }
37            }
38        };
39        rawFile.onerror = (e) => {
40            console.error(rawFile.statusText);
41        };
42        rawFile.send(null);
43    }
44
45    /**
46     * Read genotypes from text read from genotypes.gen. TODO mk> This function should be removed; use SDK functionality instead for reliable parsing.
47     * @param {string} text
48     */
49    readGenotypesFromText(text) {
50        let textTable = text.split('org:\n');
51        let i = 1;
52        while (i < textTable.length) {
53            let lines = textTable[i].split('\n');
54            if (lines[0].includes('name:')) {
55                let j = 0;
56                let gen = '';
57                let name = '';
58                let id = '';
59                while (j < lines.length) {
60                    if (lines[j].includes('name:')) {
61                        name += lines[j].split(':')[1];
62                    } else if (lines[j].includes('genotype:')) {
63                        gen += lines[j].replace('genotype:', '');
64                        let k = j + 1;
65                        while (!lines[k].includes('info_timestamp:')) {
66                            if (lines[k] != '\n') {
67                                gen += ' ' + lines[k];
68                            } else {
69                                gen += ' '
70                            }
71                            k++;
72                        }
73                        gen = gen.split('~').join('');
74                    } else if (lines[j].includes('num:')) {
75                        id += lines[j].split(':')[1];
76                    }
77                    j++;
78                }
79                if (!gen.includes('//0')) {
80                    this.name.push(name);
81                    this.genotype.push(gen);
82                    this.id.push(id);
83                }
84            }
85
86            i++;
87        }
88        this.loaded = true;
89        this.parent.start();
90    }
91
92}
93
94
95export default Genotypes;
Note: See TracBrowser for help on using the repository browser.