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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | 5x 24x 12x 12x 12x 12x 12x 12x 12x 12x 5x 1080x 1080x 5x 5x 12x 1080x 5x 12x 12x 12x 12x 5x 5x 1092x 5460x 1092x 1092x 5x 5x 5x 5x 5x 25x 72x 48x 72x 5x 5x 5x 12x 12x 12x 12x 12x 1080x 304680x 284392x 890x 284392x 1295x 284392x 284392x 890x 284392x 12x 920x 1295x 12x 12x 5x 5x 12x 12x 12x 12x 12x 920x 1295x 1295x 1295x 12x 12x 5x 12x 12x 12x 12x 12x 12x 12x 920x 920x 920x 12x 5x 5x 5x 12x 12x 12x 920x 12x 5x 12x 12x 5x 5x 5x 12x 12x 12x 12x 1080x 304680x 920x 304680x 304680x 12x 920x 12x | import { each, extend, map, mapValues, max, reduce, reject, } from 'lodash-es' const stat = function(seqs, opts) { // if someone forgets new if (!this || this.constructor !== stat) { return new stat(seqs); } Iif (seqs === undefined || typeof seqs === "string") { throw new TypeError("you need to give the seq stat an array"); } //if(seqs.length == 0){ //throw new TypeError("you need to give the seq stat a real array"); //} this.resetSeqs(seqs); this.alphabetSize = 4; this._useBackground = false; this.useGaps = false; this.ignoredChars = ["-", "*"]; extend(this, opts); }; stat.prototype.addSeq = function addSeq(seq) { this.seqs.push(seq); this._reset(); }; stat.prototype.removeSeq = function addSeq(seq) { // check for int or string if (typeof seq === 'number') { this.seqs.splice(seq, 1); } else { // identify matches (we could have multiple) each(this.seqs, function(s, i) { if (seq === s) { this.seqs.splice(i, 1); } }.bind(this)); } this._reset(); }; stat.prototype.addSeqs = function addSeqs(seqs) { seqs.forEach(function(seq) { this.addSeq(seq); }.bind(this)); }; stat.prototype.resetSeqs = function reset(seqs) { this.seqs = []; // support sequence models Iif (! seqs instanceof Array || "at" in seqs) { this.mseqs = seqs; var mSeqsPluck = function() { var seqArr = this.mseqs.pluck("seq"); this.resetSeqs(seqArr); }; seqs.on("add change reset ", mSeqsPluck, this); mSeqsPluck.call(this); } else { this.addSeqs(seqs); this._reset(); } }; var calcValues = ["consensus", "frequency", "maxLength", "ic", "gaps"]; stat.prototype._reset = function _reset() { for (var i = 0; i < calcValues.length; i++) { this["_" + calcValues[i]] = undefined; } this._identity = undefined; this._background = undefined; }; // ----------------------------------------------------------------------------- // BEGIN: setter/getter // ----------------------------------------------------------------------------- stat.prototype.setBackground = function setBackground(b) { this._useBackground = b; this._reset(); }; stat.prototype.useBackground = function useBackground() { this.setBackground(true); }; stat.prototype.setDNA = function setNucleotide() { this.alphabetSize = 4; }; stat.prototype.setProtein = function setDNA() { this.alphabetSize = 20; }; // ----------------------------------------------------------------------------- // BEGIN: auto wrappers // ----------------------------------------------------------------------------- // neat auto-wrappers calcValues.forEach(function(key) { stat.prototype[key] = function() { if (this["_" + key] === undefined) { this["_" + key] = this[key + "Calc"](); } return this["_" + key]; }; }); stat.prototype.identity = function identitiy(seq) { // do not cache if its called with a special compare seq var ident; if (this._identity === undefined || seq) { ident = this.identityCalc(seq); this._identity = undefined; } return this._identity || ident; }; // set your own background with obj.bg stat.prototype.background = function background() { if (this.bg !== undefined) { return this.bg; } if (this._background === undefined) { this.backgroundCalc(); } return this._background; }; // ----------------------------------------------------------------------------- // BEGIN: calc tools // ----------------------------------------------------------------------------- // calculates the relative frequency of a base at a given position // this is needed e.g. for the entropy calculation // seqs: array of sequences (strings) // opts: // all: boolean (use to show the frequencies for all letters [including the ignored ones] // (default false) // @returns array of all positions with a dictionary of all bases with their relative frequency stat.prototype.frequencyCalc = function frequencyCalc(opts) { var occs, totalPerPos; occs = new Array(this.maxLength()); totalPerPos = new Array(this.seqs.length); var ignoredChars = this.ignoredChars; Iif(opts !== undefined && opts.all){ ignoredChars = []; } // count the occurrences of the chars at a position each(this.seqs, function(el) { each(el, function(c, pos) { if (ignoredChars.indexOf(c) >= 0) return; if (occs[pos] === undefined) { occs[pos] = {}; } if (occs[pos][c] === undefined) { occs[pos][c] = 0; } occs[pos][c] ++; if (totalPerPos[pos] === undefined) { totalPerPos[pos] = 0; } totalPerPos[pos] ++; }); }); // normalize to 1 each(occs, function(el, pos) { return each(el, function(val, c) { return (occs[pos][c] = val / totalPerPos[pos]); }); }); this._frequency = occs; return occs; }; // seqs: array of sequences (strings) stat.prototype.backgroundCalc = function backgroundCalc() { var occ = {}; var total = 0; // count the occurences of the chars of a position each(this.seqs, function(el) { each(el, function(c) { if (occ[c] === undefined) { occ[c] = 0; } occ[c] ++; return total++; }); }); // normalize to 1 occ = mapValues(occ, function(val) { return val / total; }); this._background = occ; return occ; }; // information content after Shannon // * gaps are excluded stat.prototype.icCalc = function icCalc() { var f = this.frequency(); Iif (this._useBackground) { var b = this.background(); } var ignoredChars = this.ignoredChars; var useBackground = this._useBackground; var ic = map(f, function(el) { return reduce(el, function(memo, val, c) { Iif (ignoredChars.indexOf(c) >= 0) return memo; Iif (useBackground) { val = val / b[c]; } return memo - val * (Math.log(val) / Math.log(2)); }, 0); }); this._ic = ic; return ic; }; // sequence conservation after Schneider and Stephens (1990) // @cite Schneider, T.D. and Stephens, R.M. 1990. Sequence logos: A new way to // display consensus sequences. Nucleic Acids Res. 18: 6097–6100. stat.prototype.conservation = function conservation(alphabetSize) { var ic = this.ic(); var gaps = this.gaps(); var self = this; alphabetSize = alphabetSize || this.alphabetSize; var icMax = Math.log(alphabetSize) / Math.log(2); var i = 0; var conserv = map(ic, function(el) { var ret = (icMax - el); Iif(self.useGaps){ ret = ret * (1 - gaps[i++]); } return ret; }); return conserv; }; // sequence conservation after Schneider and Stephens (1990) // conservation for each amino acid // * gaps are excluded stat.prototype.conservResidue = function conservation(input) { var alphabetSize = input ? input.alphabetSize : undefined; var ic; var ignoredChars = this.ignoredChars; if (input !== undefined && input.scaled) { ic = this.scale(this.conservation(alphabetSize)); } else { ic = this.conservation(alphabetSize); } var f = this.frequency(); var keys; var conserv = map(f, function(el, i) { keys = reject(keys(el), function(c) { return ignoredChars.indexOf(c) >= 0; }); var obj = {}; each(keys, function(key) { obj[key] = el[key] * ic[i]; }); return obj; }); return conserv; }; // type 2 sequence logo method // scales relative to background stat.prototype.conservResidue2 = function conservation(alphabetSize) { var f = this.frequency(); var ic = this.conservation(alphabetSize); var b = this.background(); var conserv = map(f, function(el, i) { return map(el, function(val) { var sum = reduce(f[i], function(memo, e) { return memo + e / b[i]; }, 0); return ((val / b[i]) / sum) * ic[i]; }, 0); }); return conserv; }; // scale information content or conservation to 1 stat.prototype.scale = function conservation(ic, alphabetSize) { alphabetSize = alphabetSize || this.alphabetSize; var icMax = Math.log(alphabetSize) / Math.log(2); var conserv = map(ic, function(el) { return el / icMax; }); return conserv; }; stat.prototype.maxLengthCalc = function() { Iif(this.seqs.length === 0){ return 0; } return max(this.seqs, function(seq) { return seq.length; }).length; }; // seqs: array of sequences (strings) // @returns consenus sequence stat.prototype.consensusCalc = function consensusCal() { var occs = new Array(this.maxLength()); // count the occurrences of the chars of a position each(this.seqs, function(el) { each(el, function(c, pos) { if (occs[pos] === undefined) { occs[pos] = {}; } if (occs[pos][c] === undefined) { occs[pos][c] = 0; } occs[pos][c] ++; }); }); // now pick the char with most occurrences this._consensus = reduce(occs, function(memo, occ) { var keys; keys = Object.keys(occ); return memo += max(keys, function(key) { return occ[key]; }); }, ""); return this._consensus; }; // seqs: array of sequences (strings) // consensus: calculated consensus seq // calculates for each sequence // * matches with the consensus seq // * identity = matchedChars / totalChars (excluding gaps) // @returns: array of length of the seqs with the identity to the consensus (double) stat.prototype.identityCalc = function identitiyCalc(compareSeq) { var consensus = compareSeq || this.consensus(); this._identity = this.seqs.map(function(seq) { var matches = 0; var total = 0; for (var i = 0; i < seq.length; i++) { if (seq[i] !== "-" && consensus[i] !== "-") { total++; if (seq[i] === consensus[i]) { matches++; } } } return matches / total; }); return this._identity; }; // percentage of gaps per column stat.prototype.gapsCalc = function gapsCount() { var mLength = this.maxLength(); Iif(mLength <= 1 || typeof mLength === "undefined" ){ return []; } var occs = new Array(this.maxLength()); // count the occurrences of the chars of a position each(this.seqs, function(el) { each(el, function(c, pos) { if (occs[pos] === undefined) { occs[pos] = { g: 0, t: 0 }; } c = c === "-" ? "g" : "t"; occs[pos][c] ++; }); }); // now pick the char with most occurrences this._gaps = map(occs, function(el) { return el.g / (el.g + el.t); }); return this._gaps; }; export default stat; |