sequence.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { AminoAlphabet, NuclecicAlphabet, getProteinOneLetterCode, getRnaOneLetterCode, getDnaOneLetterCode } from './constants';
  7. import { Column } from 'mol-data/db'
  8. // TODO add mapping support to other sequence spaces, e.g. uniprot
  9. // TODO sequence alignment (take NGL code as starting point)
  10. type Sequence = Sequence.Protein | Sequence.DNA | Sequence.RNA | Sequence.Generic
  11. namespace Sequence {
  12. export const enum Kind {
  13. Protein = 'protein',
  14. RNA = 'RNA',
  15. DNA = 'DNA',
  16. Generic = 'generic'
  17. }
  18. export interface Base<K extends Kind, Alphabet extends string> {
  19. readonly kind: K,
  20. readonly offset: number,
  21. readonly sequence: ArrayLike<Alphabet>
  22. }
  23. export interface Protein extends Base<Kind.Protein, AminoAlphabet> { }
  24. export interface RNA extends Base<Kind.RNA, NuclecicAlphabet> { }
  25. export interface DNA extends Base<Kind.DNA, NuclecicAlphabet> { }
  26. export interface Generic extends Base<Kind.Generic, 'X'> { }
  27. export function create(kind: Kind, sequence: string, offset: number = 0): Sequence {
  28. return { kind: kind as any, sequence: sequence as any, offset };
  29. }
  30. export function getSequenceString(seq: Sequence) {
  31. return seq.sequence as string;
  32. }
  33. function determineKind(names: Column<string>) {
  34. for (let i = 0, _i = Math.min(names.rowCount, 10); i < _i; i++) {
  35. const name = names.value(i) || '';
  36. if (getProteinOneLetterCode(name) !== 'X') return { kind: Kind.Protein, code: getProteinOneLetterCode };
  37. if (getRnaOneLetterCode(name) !== 'X') return { kind: Kind.RNA, code: getRnaOneLetterCode };
  38. if (getDnaOneLetterCode(name) !== 'X') return { kind: Kind.DNA, code: getDnaOneLetterCode };
  39. }
  40. return { kind: Kind.Generic, code: (v: string) => 'X' };
  41. }
  42. export function ofResidueNames(residueName: Column<string>, seqId: Column<number>): Sequence {
  43. if (seqId.rowCount === 0) throw new Error('cannot be empty');
  44. const { kind, code } = determineKind(residueName);
  45. return new Impl(kind, residueName, seqId, code) as Sequence;
  46. }
  47. class Impl implements Base<any, any> {
  48. private _offset = 0;
  49. private _seq: string | undefined = void 0;
  50. get offset() {
  51. if (this._seq !== void 0) return this._offset;
  52. this.create();
  53. return this._offset;
  54. }
  55. get sequence(): any {
  56. if (this._seq !== void 0) return this._seq;
  57. this.create();
  58. return this._seq;
  59. }
  60. private create() {
  61. let maxSeqId = 0, minSeqId = Number.MAX_SAFE_INTEGER;
  62. for (let i = 0, _i = this.seqId.rowCount; i < _i; i++) {
  63. const id = this.seqId.value(i);
  64. if (maxSeqId < id) maxSeqId = id;
  65. if (id < minSeqId) minSeqId = id;
  66. }
  67. const count = maxSeqId - minSeqId + 1;
  68. const sequenceArray = new Array(maxSeqId + 1);
  69. for (let i = 0; i < count; i++) {
  70. sequenceArray[i] = 'X';
  71. }
  72. for (let i = 0, _i = this.seqId.rowCount; i < _i; i++) {
  73. sequenceArray[this.seqId.value(i) - minSeqId] = this.code(this.residueName.value(i) || '');
  74. }
  75. this._seq = sequenceArray.join('');
  76. this._offset = minSeqId - 1;
  77. }
  78. constructor(public kind: Kind, private residueName: Column<string>, private seqId: Column<number>, private code: (name: string) => string) {
  79. }
  80. }
  81. }
  82. export { Sequence }