molstar
Version:
A comprehensive macromolecular library.
160 lines (159 loc) • 6.47 kB
JavaScript
/**
* Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { getProteinOneLetterCode, getRnaOneLetterCode, getDnaOneLetterCode } from './constants';
import { Column } from '../../mol-data/db';
import { assertUnreachable } from '../../mol-util/type-helpers';
var Sequence;
(function (Sequence) {
function getSequenceString(seq) {
var array = seq.code.toArray();
return (array instanceof Array ? array : Array.from(array)).join('');
}
Sequence.getSequenceString = getSequenceString;
function determineKind(names) {
for (var i = 0, _i = Math.min(names.rowCount, 10); i < _i; i++) {
var name_1 = names.value(i) || '';
if (getProteinOneLetterCode(name_1) !== 'X')
return "protein" /* Kind.Protein */;
if (getRnaOneLetterCode(name_1) !== 'X')
return "RNA" /* Kind.RNA */;
if (getDnaOneLetterCode(name_1) !== 'X')
return "DNA" /* Kind.DNA */;
}
return "generic" /* Kind.Generic */;
}
function codeProvider(kind, map) {
var code;
switch (kind) {
case "protein" /* Kind.Protein */:
code = getProteinOneLetterCode;
break;
case "DNA" /* Kind.DNA */:
code = getDnaOneLetterCode;
break;
case "RNA" /* Kind.RNA */:
code = getRnaOneLetterCode;
break;
case "generic" /* Kind.Generic */:
code = function () { return 'X'; };
break;
default: assertUnreachable(kind);
}
if (map && map.size > 0) {
return function (name) {
var ret = code(name);
if (ret !== 'X' || !map.has(name))
return ret;
return code(map.get(name));
};
}
return code;
}
function ofResidueNames(compId, seqId) {
if (seqId.rowCount === 0)
throw new Error('cannot be empty');
var kind = determineKind(compId);
return new ResidueNamesImpl(kind, compId, seqId);
}
Sequence.ofResidueNames = ofResidueNames;
var ResidueNamesImpl = /** @class */ (function () {
function ResidueNamesImpl(kind, compId, seqId) {
this.kind = kind;
this.microHet = new Map();
var codeFromName = codeProvider(kind);
var codes = [];
var compIds = [];
var seqIds = [];
var microHet = new Map();
var idx = 0;
var indexMap = new Map();
for (var i = 0, il = seqId.rowCount; i < il; ++i) {
var seq_id = seqId.value(i);
if (!indexMap.has(seq_id)) {
indexMap.set(seq_id, idx);
var comp_id = compId.value(i);
compIds[idx] = comp_id;
seqIds[idx] = seq_id;
codes[idx] = codeFromName(comp_id);
idx += 1;
}
else {
// micro-heterogeneity
if (!microHet.has(seq_id)) {
microHet.set(seq_id, [compIds[indexMap.get(seq_id)], compId.value(i)]);
}
else {
microHet.get(seq_id).push(compId.value(i));
}
}
}
var labels = [];
for (var i = 0, il = idx; i < il; ++i) {
var mh = microHet.get(seqIds[i]);
if (mh) {
var l = mh.map(function (id) {
var c = codeFromName(id);
return c === 'X' ? id : c;
});
labels[i] = "(".concat(l.join('|'), ")");
}
else {
labels[i] = codes[i] === 'X' ? compIds[i] : codes[i];
}
}
this.length = idx;
this.code = Column.ofStringArray(codes);
this.compId = Column.ofStringArray(compIds);
this.seqId = Column.ofIntArray(seqIds);
this.label = Column.ofStringArray(labels);
this.microHet = microHet;
this.indexMap = indexMap;
}
ResidueNamesImpl.prototype.index = function (seqId) {
return this.indexMap.get(seqId);
};
return ResidueNamesImpl;
}());
function ofSequenceRanges(seqIdBegin, seqIdEnd) {
var kind = "generic" /* Kind.Generic */;
return new SequenceRangesImpl(kind, seqIdBegin, seqIdEnd);
}
Sequence.ofSequenceRanges = ofSequenceRanges;
var SequenceRangesImpl = /** @class */ (function () {
function SequenceRangesImpl(kind, seqIdStart, seqIdEnd) {
this.kind = kind;
this.seqIdStart = seqIdStart;
this.seqIdEnd = seqIdEnd;
this.microHet = new Map();
var maxSeqId = 0, minSeqId = Number.MAX_SAFE_INTEGER;
for (var i = 0, _i = this.seqIdStart.rowCount; i < _i; i++) {
var idStart = this.seqIdStart.value(i);
var idEnd = this.seqIdEnd.value(i);
if (idStart < minSeqId)
minSeqId = idStart;
if (maxSeqId < idEnd)
maxSeqId = idEnd;
}
var count = maxSeqId - minSeqId + 1;
this.code = Column.ofConst('X', count, Column.Schema.str);
this.label = Column.ofConst('', count, Column.Schema.str);
this.seqId = Column.ofLambda({
value: function (row) { return row + minSeqId + 1; },
rowCount: count,
schema: Column.Schema.int
});
this.compId = Column.ofConst('', count, Column.Schema.str);
this.length = count;
this.minSeqId = minSeqId;
}
SequenceRangesImpl.prototype.index = function (seqId) {
return seqId - this.minSeqId;
};
return SequenceRangesImpl;
}());
})(Sequence || (Sequence = {}));
export { Sequence };