-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (79 loc) · 1.94 KB
/
index.js
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
var parser = require("biojs-io-parser");
var MatrixReader = function(text) {
if (this.constructor != MatrixReader) return new MatrixReader(text);
this.matrix = {};
this.parsingOrder = [];
if (text != undefined) {
this.parse(text);
}
return this;
};
parser.mixin(MatrixReader);
module.exports = MatrixReader;
MatrixReader.prototype.parse = function(text) {
text.split("\n").forEach(function(el) {
this.parseLine(el);
}.bind(this));
this.buildMatrix();
return this.matrix;
};
MatrixReader.read = function(url, cb) {
return (new MatrixReader()).read(url, cb);
};
MatrixReader.parse = function(text) {
return (new MatrixReader()).parse(text);
};
MatrixReader.prototype.parseLine = function(line) {
var c = line.charAt(0);
if (c === '#') {
return;
} else {
this.parsingOrder.push(c);
var intStr = line.substring(1);
var ints = intStr.split(/\s+/)
.filter(function(e) {
return e.length > 0;
})
.map(function(e) {
return parseInt(e);
});
var m = {};
for (var i = 0; i < ints.length; i++) {
m[this.parsingOrder[i]] = ints[i];
}
this.matrix[c] = m;
}
};
MatrixReader.prototype.export = function() {
return MatrixReader.export(this.matrix);
};
MatrixReader.export = function(matrix) {
var lines = [];
var max = 1;
// use the matrix attribute if an object is given
if("matrix" in matrix){
matrix = matrix.matrix;
}
for (var key in matrix) {
var line = key;
var keys = Object.keys(matrix[key]);
for (var i = 0; i < max; i++) {
line += "\t" + matrix[key][keys[i]];
}
lines.push(line);
max++;
}
return lines.join("\n");
};
/**
* faster access time
*/
MatrixReader.prototype.buildMatrix = function() {
for (var i = this.parsingOrder.length - 1; i >= 0; i--) {
var curC = this.parsingOrder[i];
var map = this.matrix[curC];
for (var key in map) {
this.matrix[key][curC] = map[key];
}
}
};