-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelperFns.js
127 lines (119 loc) · 2.6 KB
/
helperFns.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require("fs");
const defaultCount = 10;
const defaultMaxLength = 10000;
const defaultMinLength = 1000;
function genHelper(
{
count = defaultCount,
lengths,
maxLength = defaultMaxLength,
minLength = defaultMinLength,
postProcess,
isCDS,
toFile = true,
type,
},
fn
) {
if (lengths && typeof lengths === "string") {
lengths = lengths.split(",").map((n) => Number(n) || 10);
}
let lastLength = 0;
let toRet = [];
for (let i = 0; i < count; i++) {
const name = "pTG_00" + (i + 1);
let length;
if (lengths) {
length = lengths[lastLength];
lastLength++;
if (lastLength === lengths.length) {
lastLength = 0;
}
}
const bps = getBps({ length, maxLength, minLength, isCDS });
toRet.push(fn({ name, bps }));
}
if (postProcess) {
toRet = postProcess(toRet);
}
if (toFile) {
toRet.forEach((seq, i) => {
fs.writeFile(`sequence_${i}.${type}`, seq, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
});
});
} else {
return toRet;
}
}
function getBps({ length, maxLength, minLength, isCDS }) {
const bps = ["a", "g", "c", "t"];
let newBps = isCDS ? "atg" : "";
if (isCDS) {
//resize the lengths to account for the cds
if (length) {
length = length - 6;
} else {
maxLength = maxLength - 6;
}
}
for (
let j = 0;
j <
(length
? length
: Math.floor(Math.random() * (maxLength - minLength) + minLength));
j++
) {
newBps += bps[Math.floor(Math.random() * 4)];
}
if (isCDS) {
newBps += "taa";
}
return newBps;
}
function getSequenceFileForType(opts) {
if (opts.type === "gb") {
return genHelper(opts, ({ name, bps }) => {
return (
"LOCUS " +
name +
" " +
bps.length +
" bp DNA linear 19-NOV-2018\n" +
"ORIGIN \n" +
" 1 " +
bps +
"\n" +
"//\n"
);
});
} else if (opts.type === "csv") {
genHelper(
{
...opts,
postProcess: (data) => {
let toRet = "Name,Sequence\n";
data.forEach((l) => {
toRet += l;
});
return [toRet];
},
},
({ name, bps }) => {
return name + "," + bps + "\n";
}
);
} else if (opts.type === "fasta") {
genHelper(opts, ({ name, bps }) => {
return ">" + name + "||" + bps.length + "|linear\n" + bps;
});
}
}
module.exports = {
getSequenceFileForType,
defaultCount,
defaultMaxLength,
defaultMinLength,
};