UNPKG

sequaljs

Version:

JavaScript/TypeScript library for parsing and manipulating ProForma peptide sequence notation

144 lines (143 loc) 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseBlock = void 0; const utils_1 = require("./utils"); /** * Base class for biochemical building blocks with position and mass properties. * * This abstract base class provides core functionality for blocks such as * amino acids, modifications, and other biochemical components. */ class BaseBlock { /** * Initializes a BaseBlock object. * * @param value - The identifier of the block. * @param position - The position of the block within a chain. * @param branch - Indicates whether this block is a branch of another block. * @param mass - The mass of the block in Daltons. */ constructor(value, position, branch = false, mass) { this._value = value; this._position = position !== null && position !== void 0 ? position : null; this._branch = branch; this._mass = mass !== null && mass !== void 0 ? mass : null; this._extra = undefined; } /** * Gets the identifier of the block. */ get value() { return this._value; } /** * Sets the identifier of the block. * @param value - The new identifier. */ set value(value) { this._value = value; } /** * Gets the position of the block. */ get position() { var _a; return (_a = this._position) !== null && _a !== void 0 ? _a : null; } /** * Sets the position of the block. * @param position - The new position. */ set position(position) { this._position = position; } /** * Checks if the block is a branch. */ get branch() { return this._branch; } /** * Gets the mass of the block. */ get mass() { var _a; return (_a = this._mass) !== null && _a !== void 0 ? _a : null; } /** * Sets the mass of the block. * @param mass - The new mass. */ set mass(mass) { this._mass = mass; } /** * Gets extra information associated with the block. */ get extra() { return this._extra; } /** * Sets extra information for the block. * @param value - The extra information. */ set extra(value) { this._extra = value; } /** * Converts the block to a dictionary representation. * @returns A dictionary containing the block's attributes. */ toDict() { return { value: this._value, position: this._position, branch: this._branch, mass: this._mass, extra: this._extra, }; } /** * Checks if two blocks are equal. * @param other - The other block to compare with. * @returns True if the blocks are equal, false otherwise. */ equals(other) { if (!(other instanceof BaseBlock)) { return false; } return (this._value === other.value && this._position === other.position && this._branch === other.branch); } /** * Generates a hash for the block. * @returns The hash code. */ hashCode() { const valueHash = (0, utils_1.calculateHashCode)(this._value); const branchHash = this._branch ? 1 : 0; if (this._position === undefined || this._position === null) { return (valueHash !== null && valueHash !== void 0 ? valueHash : 0) ^ branchHash; } else { const positionHash = (0, utils_1.calculateHashCode)(this._position.toString()); return (valueHash !== null && valueHash !== void 0 ? valueHash : 0) ^ (positionHash !== null && positionHash !== void 0 ? positionHash : 0) ^ branchHash; } } /** * Returns a string representation of the block. * @returns The string representation. */ toString() { return this._value; } /** * Returns a detailed string representation of the block. * @returns The detailed string representation. */ toRepr() { return `${this.constructor.name}(value='${this._value}', position=${this._position})`; } } exports.BaseBlock = BaseBlock;