@datagrok/sequence-translator 1.4.8 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -1
- package/CHANGELOG.md +24 -0
- package/dist/package-test.js +1 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/files/polytool-rules/rules_example.json +35 -38
- package/package.json +9 -9
- package/src/apps/common/model/oligo-toolkit-package.ts +13 -7
- package/src/package.ts +8 -7
- package/src/polytool/const.ts +1 -0
- package/src/polytool/{pt-conversion.ts → conversion/pt-chain.ts} +118 -285
- package/src/polytool/conversion/pt-conversion.ts +26 -0
- package/src/polytool/conversion/pt-misc.ts +193 -0
- package/src/polytool/conversion/pt-rules.ts +231 -0
- package/src/polytool/conversion/rule-manager.ts +205 -0
- package/src/polytool/pt-convert-editor.ts +1 -1
- package/src/polytool/pt-dialog.ts +133 -6
- package/src/polytool/{pt-enumeration-helm-dialog.ts → pt-enumerate-seq-dialog.ts} +243 -114
- package/src/polytool/pt-enumeration-helm.ts +2 -2
- package/src/polytool/pt-placeholders-breadth-input.ts +80 -39
- package/src/polytool/pt-placeholders-input.ts +96 -35
- package/src/polytool/pt-unrule-dialog.ts +1 -1
- package/src/polytool/pt-unrule.ts +2 -2
- package/src/polytool/types.ts +5 -1
- package/src/tests/polytool-chain-from-notation-tests.ts +9 -18
- package/src/tests/polytool-chain-parse-notation-tests.ts +3 -2
- package/src/tests/polytool-convert-tests.ts +5 -3
- package/src/tests/polytool-unrule-tests.ts +1 -1
- package/src/tests/toAtomicLevel-tests.ts +5 -6
- package/src/utils/cell-renderer-cyclized.ts +37 -0
- package/src/utils/context-menu.ts +1 -1
- package/src/utils/cyclized.ts +23 -26
- package/src/polytool/pt-rules.ts +0 -93
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import * as grok from 'datagrok-api/grok';
|
|
2
|
+
|
|
3
|
+
import wu from 'wu';
|
|
4
|
+
import {PolymerTypes} from '@datagrok-libraries/bio/src/helm/consts';
|
|
5
|
+
import {getMonomerLibHelper} from '@datagrok-libraries/bio/src/monomer-works/monomer-utils';
|
|
6
|
+
import {IMonomerLib, IMonomerLibBase, Monomer, MonomerLibData, RGroup} from '@datagrok-libraries/bio/src/types';
|
|
7
|
+
import {RDModule, RDMol, RDReaction, MolList, RDReactionResult} from '@datagrok-libraries/chem-meta/src/rdkit-api';
|
|
8
|
+
import {HELM_REQUIRED_FIELD as REQ,
|
|
9
|
+
HELM_OPTIONAL_FIELDS as OPT, HELM_RGROUP_FIELDS} from '@datagrok-libraries/bio/src/utils/const';
|
|
10
|
+
import {getRdKitModule} from '@datagrok-libraries/bio/src/chem/rdkit-module';
|
|
11
|
+
import {Rules, RuleReaction, getMonomerPairs} from './pt-rules';
|
|
12
|
+
import {InvalidReactionError, MonomerNotFoundError} from '../types';
|
|
13
|
+
import {errInfo} from '@datagrok-libraries/bio/src/utils/err-info';
|
|
14
|
+
|
|
15
|
+
/** Gets 0-based in-index (simple polymer) of out-index (continuous) {@link idx} */
|
|
16
|
+
export function getInnerIdx(outIdx: number, monomers: string[][]): [number, number] {
|
|
17
|
+
// let prevSpCount = 0;
|
|
18
|
+
// for (let spI = 0; spI < monomers.length && idx >= (prevSpCount + monomers[spI].length); ++spI)
|
|
19
|
+
// prevSpCount += monomers[spI].length;
|
|
20
|
+
// return idx - prevSpCount;
|
|
21
|
+
let inIdx = outIdx;
|
|
22
|
+
let spIdx: number;
|
|
23
|
+
for (spIdx = 0; spIdx < monomers.length && inIdx >= monomers[spIdx].length; ++spIdx)
|
|
24
|
+
inIdx -= monomers[spIdx].length;
|
|
25
|
+
return [inIdx, spIdx];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Gets 0-based out-index of 0-based in-index {@link inIdx} monomer of simple polymer {@link spIdx} */
|
|
29
|
+
export function getOuterIdx(inIdx: number, spIdx: number, monomers: string[][]): number {
|
|
30
|
+
let outIdx = 0;
|
|
31
|
+
for (let i = 0; i < spIdx; ++i)
|
|
32
|
+
outIdx += monomers[i].length;
|
|
33
|
+
return outIdx + inIdx;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getMonomersMolBlocks(monomer1: Monomer, monomer2: Monomer): [string, string] {
|
|
37
|
+
const mb1 = monomer1.molfile;
|
|
38
|
+
let mb2 = monomer2.molfile;
|
|
39
|
+
const addGroups = monomer1.rgroups.length;
|
|
40
|
+
|
|
41
|
+
//mol v2000 monomer
|
|
42
|
+
const rgpIdx = mb2.indexOf('M RGP');
|
|
43
|
+
if (rgpIdx !== -1) {
|
|
44
|
+
const groupsCountStr = mb2.substring(rgpIdx + 6, rgpIdx + 9);
|
|
45
|
+
const groupsCount = Number(groupsCountStr);
|
|
46
|
+
|
|
47
|
+
for (let i = 0; i < groupsCount; i++) {
|
|
48
|
+
const start = rgpIdx + 9 + 4 + i * 8;
|
|
49
|
+
const end = rgpIdx + 9 + 8 + i * 8;
|
|
50
|
+
const rGroupSpecifier = mb2.substring(start, end);
|
|
51
|
+
const groupPosition = Number(rGroupSpecifier) + addGroups;
|
|
52
|
+
const digits = Math.floor(Math.log10(groupPosition) + 1);
|
|
53
|
+
const newSpecifier = ' '.repeat(4 - digits) + String(groupPosition);
|
|
54
|
+
mb2 = mb2.substring(0, start) + newSpecifier + mb2.substring(end, mb2.length);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//TODO: same for v3000 monomer
|
|
59
|
+
|
|
60
|
+
return [mb1, mb2];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getSyntheticMolBlock(rdkit: RDModule, reaction: string,
|
|
64
|
+
mb1: string, mb2: string, monomerName: string): string {
|
|
65
|
+
let rxn: RDReaction | null = null;
|
|
66
|
+
let mols: MolList | null = null;
|
|
67
|
+
let mol1: RDMol | null = null;
|
|
68
|
+
let mol2: RDMol | null = null;
|
|
69
|
+
let rctns: RDReactionResult | null = null;
|
|
70
|
+
let molP: RDMol | null = null;
|
|
71
|
+
let molBlock = '';
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
rxn = rdkit.get_rxn(reaction);
|
|
75
|
+
if (!rxn) throw new InvalidReactionError(reaction);
|
|
76
|
+
mols = new rdkit.MolList();
|
|
77
|
+
mol1 = rdkit.get_mol(mb1!);
|
|
78
|
+
mol2 = rdkit.get_mol(mb2!);
|
|
79
|
+
mols.append(mol1!);
|
|
80
|
+
mols.append(mol2!);
|
|
81
|
+
|
|
82
|
+
rctns = rxn.run_reactants(mols, 1);
|
|
83
|
+
//const size = rctns.size();
|
|
84
|
+
const element = rctns.get(0);
|
|
85
|
+
|
|
86
|
+
molP = element.next();
|
|
87
|
+
molBlock = molP?.get_molblock();//molP?.get_v3Kmolblock();//
|
|
88
|
+
} catch (err: any) {
|
|
89
|
+
const [errMsg, _errStack] = errInfo(err);
|
|
90
|
+
grok.shell.error(`Can not assemble monomer '${monomerName}': ${errMsg}.`);
|
|
91
|
+
throw err;
|
|
92
|
+
} finally {
|
|
93
|
+
rxn?.delete();
|
|
94
|
+
mols?.delete();
|
|
95
|
+
mol1?.delete();
|
|
96
|
+
mol2?.delete();
|
|
97
|
+
rctns?.delete();
|
|
98
|
+
molP?.delete();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return molBlock;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function getNewGroups(monomer1: Monomer, monomer2: Monomer): RGroup[] {
|
|
105
|
+
const groups = new Array<RGroup>(monomer1?.rgroups.length! + monomer2?.rgroups.length!);
|
|
106
|
+
const length1 = monomer1?.rgroups.length!;
|
|
107
|
+
const length2 = monomer2?.rgroups.length!;
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < length1; i++)
|
|
110
|
+
groups[i] = monomer1?.rgroups[i]!;
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < length2; i++) {
|
|
113
|
+
const rGroupSpecifier = monomer2?.rgroups[i]!.label.replace('R', '');
|
|
114
|
+
const groupPosition = Number(rGroupSpecifier) + length1;
|
|
115
|
+
const group: RGroup = {
|
|
116
|
+
//@ts-ignore
|
|
117
|
+
[HELM_RGROUP_FIELDS.CAP_GROUP_SMILES_UPPERCASE]: monomer2?.rgroups[i].capGroupSMILES
|
|
118
|
+
.replace(rGroupSpecifier, String(groupPosition)),
|
|
119
|
+
[HELM_RGROUP_FIELDS.ALTERNATE_ID]: monomer2?.rgroups[i].alternateId
|
|
120
|
+
.replace(rGroupSpecifier, String(groupPosition)),
|
|
121
|
+
[HELM_RGROUP_FIELDS.CAP_GROUP_NAME]: monomer2?.rgroups[i].capGroupName,
|
|
122
|
+
[HELM_RGROUP_FIELDS.LABEL]: monomer2?.rgroups[i].label.replace(rGroupSpecifier, String(groupPosition)),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
groups[i + length1] = group;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return groups;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function getNewMonomers(rdkit: RDModule, mLib: IMonomerLib, rule: RuleReaction): [string[], Monomer[]] {
|
|
132
|
+
const reacSmarts = rule.reaction;
|
|
133
|
+
const monomerName = rule.name;
|
|
134
|
+
|
|
135
|
+
const [firstMonomers, secondMonomers] = getMonomerPairs(rule);
|
|
136
|
+
|
|
137
|
+
const monomerNames = new Array<string>(firstMonomers.length);
|
|
138
|
+
const resMonomers = new Array<Monomer>(firstMonomers.length);
|
|
139
|
+
|
|
140
|
+
for (let i = 0; i < firstMonomers.length; i ++) {
|
|
141
|
+
const monomer1 = mLib.getMonomer('PEPTIDE', firstMonomers[i]);
|
|
142
|
+
if (!monomer1) throw new MonomerNotFoundError('PEPTIDE', firstMonomers[i]);
|
|
143
|
+
const monomer2 = mLib.getMonomer('PEPTIDE', secondMonomers[i]);
|
|
144
|
+
if (!monomer2) throw new MonomerNotFoundError('PEPTIDE', secondMonomers[i]);
|
|
145
|
+
|
|
146
|
+
const [mb1, mb2] = getMonomersMolBlocks(monomer1!, monomer2!);
|
|
147
|
+
const molBlock = getSyntheticMolBlock(rdkit, reacSmarts, mb1, mb2, monomerName);
|
|
148
|
+
const groups: RGroup[] = getNewGroups(monomer1!, monomer2!);
|
|
149
|
+
|
|
150
|
+
const resMonomer: Monomer = {
|
|
151
|
+
[REQ.SYMBOL]: monomerName,
|
|
152
|
+
[REQ.NAME]: monomerName,
|
|
153
|
+
[REQ.MOLFILE]: molBlock,
|
|
154
|
+
[REQ.AUTHOR]: '',
|
|
155
|
+
[REQ.ID]: 0,
|
|
156
|
+
[REQ.RGROUPS]: groups,
|
|
157
|
+
[REQ.SMILES]: '',
|
|
158
|
+
[REQ.POLYMER_TYPE]: 'PEPTIDE',
|
|
159
|
+
[REQ.MONOMER_TYPE]: 'Backbone',
|
|
160
|
+
[REQ.CREATE_DATE]: null,
|
|
161
|
+
// // @ts-ignore
|
|
162
|
+
// lib: {source: 'Reaction'},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
resMonomer[OPT.META] = Object.assign(resMonomer[OPT.META] ?? {},
|
|
166
|
+
{'colors': {'default': {line: '#2083D5', text: '#2083D5', background: '#F2F2F5'}}});
|
|
167
|
+
|
|
168
|
+
monomerNames[i] = monomerName;
|
|
169
|
+
resMonomers[i] = resMonomer;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return [monomerNames, resMonomers];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function getOverriddenLibrary(rules: Rules): Promise<IMonomerLibBase> {
|
|
176
|
+
const monomerLibHelper = await getMonomerLibHelper();
|
|
177
|
+
const systemMonomerLib = monomerLibHelper.getMonomerLib();
|
|
178
|
+
|
|
179
|
+
const rdkit = await getRdKitModule();
|
|
180
|
+
const argLib: { [symbol: string]: Monomer } = {};
|
|
181
|
+
|
|
182
|
+
for (let i = 0; i < rules.reactionRules.length; i++) {
|
|
183
|
+
const [names, monomers] = getNewMonomers(rdkit, systemMonomerLib, rules.reactionRules[i]);
|
|
184
|
+
for (let j = 0; j < names.length; j ++)
|
|
185
|
+
argLib[names[j]] = monomers[j];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const overrideMonomerLibData: MonomerLibData = {[PolymerTypes.PEPTIDE]: argLib};
|
|
189
|
+
const overriddenMonomerLib = systemMonomerLib.override(overrideMonomerLibData,
|
|
190
|
+
'ST-PT-reactions.' + wu.repeat(1).map(() => Math.floor((Math.random() * 36))
|
|
191
|
+
.toString(36)).take(4).toArray().join(''));
|
|
192
|
+
return overriddenMonomerLib;
|
|
193
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import * as DG from 'datagrok-api/dg';
|
|
2
|
+
import * as grok from 'datagrok-api/grok';
|
|
3
|
+
import {ActiveFiles} from '@datagrok-libraries/utils/src/settings/active-files-base';
|
|
4
|
+
import {RulesManager} from './rule-manager';
|
|
5
|
+
|
|
6
|
+
export const RULES_PATH = 'System:AppData/SequenceTranslator/polytool-rules/';
|
|
7
|
+
export const RULES_STORAGE_NAME = 'Polytool';
|
|
8
|
+
export const RULES_TYPE_LINK = 'link';
|
|
9
|
+
export const RULES_TYPE_REACTION = 'reaction';
|
|
10
|
+
export const RULES_TYPE_HOMODIMER = 'fragmentDuplication';
|
|
11
|
+
export const RULES_TYPE_HETERODIMER = 'differentFragments';
|
|
12
|
+
|
|
13
|
+
const NAME_CODE = 'code';
|
|
14
|
+
const NAME_FIRST_MONOMERS = 'firstMonomers';
|
|
15
|
+
const NAME_SECOND_MONOMERS = 'secondMonomers';
|
|
16
|
+
const NAME_REACTION_NAME = 'name';
|
|
17
|
+
const NAME_FIRST_LINK = 'firstLinkingGroup';
|
|
18
|
+
const NAME_SECOND_LINK = 'secondLinkingGroup';
|
|
19
|
+
|
|
20
|
+
export class RuleInputs extends ActiveFiles {
|
|
21
|
+
constructor(
|
|
22
|
+
path: string, userStorageName: string, ext: string,
|
|
23
|
+
options?: { onValueChanged: (value: string[]) => void }
|
|
24
|
+
) {
|
|
25
|
+
super(path, userStorageName, ext, options);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
override createInput(available: string, isChecked: boolean): DG.InputBase<boolean> {
|
|
29
|
+
const res = super.createInput(available, isChecked);
|
|
30
|
+
|
|
31
|
+
const editIcon = ui.icons.edit(async () => {
|
|
32
|
+
const rulesManager = await RulesManager.getInstance(available);
|
|
33
|
+
//await rulesManager.show();
|
|
34
|
+
grok.shell.addView(await rulesManager.getView());
|
|
35
|
+
}, 'Edit rules');
|
|
36
|
+
|
|
37
|
+
res.addOptions(editIcon);
|
|
38
|
+
|
|
39
|
+
return res;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type RuleLink = {
|
|
44
|
+
code: number,
|
|
45
|
+
firstMonomers: string[],
|
|
46
|
+
secondMonomers: string[],
|
|
47
|
+
firstLinkingGroup: number,
|
|
48
|
+
secondLinkingGroup: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type RuleReaction = {
|
|
52
|
+
code: number,
|
|
53
|
+
firstMonomers: string[],
|
|
54
|
+
secondMonomers: string[],
|
|
55
|
+
reaction: string,
|
|
56
|
+
name: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class Rules {
|
|
60
|
+
homodimerCode: string | null;
|
|
61
|
+
heterodimerCode: string | null;
|
|
62
|
+
linkRules: RuleLink[];
|
|
63
|
+
reactionRules: RuleReaction[];
|
|
64
|
+
|
|
65
|
+
constructor(homodimerCode: string | null, heterodimerCode: string | null,
|
|
66
|
+
linkRules: RuleLink[], reactionRules: RuleReaction[]) {
|
|
67
|
+
this.homodimerCode = homodimerCode;
|
|
68
|
+
this.heterodimerCode = heterodimerCode;
|
|
69
|
+
this.linkRules = linkRules;
|
|
70
|
+
this.reactionRules = reactionRules;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
set homodimer(code: string) {
|
|
74
|
+
this.homodimerCode = code;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
set heterodimer(code: string) {
|
|
78
|
+
this.heterodimerCode = code;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addLinkRules(rules: RuleLink[]): void {
|
|
82
|
+
for (let j = 0; j < rules.length; j++)
|
|
83
|
+
this.linkRules.push(rules[j]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
addSynthesisRules(rules: RuleReaction[]): void {
|
|
87
|
+
for (let j = 0; j < rules.length; j++)
|
|
88
|
+
this.reactionRules.push(rules[j]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
getLinkRulesDf(): DG.DataFrame {
|
|
92
|
+
const length = this.linkRules.length;
|
|
93
|
+
const codeCol = DG.Column.int(NAME_CODE, length);
|
|
94
|
+
const firstMonomerCol = DG.Column.string(NAME_FIRST_MONOMERS, length);
|
|
95
|
+
const secondMonomerCol = DG.Column.string(NAME_SECOND_MONOMERS, length);
|
|
96
|
+
const firstLinkingGroup = DG.Column.int(NAME_FIRST_LINK, length);
|
|
97
|
+
const secondLinkingGroup = DG.Column.int(NAME_SECOND_LINK, length);
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < length; i++) {
|
|
100
|
+
codeCol.set(i, this.linkRules[i].code);
|
|
101
|
+
firstMonomerCol.set(i, this.linkRules[i].firstMonomers.toString());
|
|
102
|
+
secondMonomerCol.set(i, this.linkRules[i].secondMonomers.toString());
|
|
103
|
+
firstLinkingGroup.set(i, this.linkRules[i].firstLinkingGroup);
|
|
104
|
+
secondLinkingGroup.set(i, this.linkRules[i].secondLinkingGroup);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return DG.DataFrame.fromColumns([
|
|
108
|
+
codeCol, firstMonomerCol, secondMonomerCol, firstLinkingGroup, secondLinkingGroup
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getSynthesisRulesDf(): DG.DataFrame {
|
|
113
|
+
const length = this.reactionRules.length;
|
|
114
|
+
const codeCol = DG.Column.int(NAME_CODE, length);
|
|
115
|
+
const firstMonomerCol = DG.Column.string(NAME_FIRST_MONOMERS, length);
|
|
116
|
+
const secondMonomerCol = DG.Column.string(NAME_SECOND_MONOMERS, length);
|
|
117
|
+
const name = DG.Column.string(NAME_REACTION_NAME, length);
|
|
118
|
+
const firstReactant = DG.Column.string('firstReactant', length);
|
|
119
|
+
const secondReactant = DG.Column.string('secondReactant', length);
|
|
120
|
+
const product = DG.Column.string('product', length);
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < length; i++) {
|
|
123
|
+
codeCol.set(i, this.reactionRules[i].code);
|
|
124
|
+
firstMonomerCol.set(i, this.reactionRules[i].firstMonomers.toString());
|
|
125
|
+
secondMonomerCol.set(i, this.reactionRules[i].secondMonomers.toString());
|
|
126
|
+
name.set(i, this.reactionRules[i].name);
|
|
127
|
+
|
|
128
|
+
const reaction = this.reactionRules[i].reaction.split('>>');
|
|
129
|
+
const reactants = reaction[0].split('.');
|
|
130
|
+
|
|
131
|
+
firstReactant.set(i, reactants[0]);
|
|
132
|
+
secondReactant.set(i, reactants[1]);
|
|
133
|
+
product.set(i, reaction[1]);
|
|
134
|
+
}
|
|
135
|
+
firstReactant.semType = DG.SEMTYPE.MOLECULE;
|
|
136
|
+
secondReactant.semType = DG.SEMTYPE.MOLECULE;
|
|
137
|
+
product.semType = DG.SEMTYPE.MOLECULE;
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
return DG.DataFrame.fromColumns([
|
|
141
|
+
name, firstReactant, secondReactant, product, codeCol, firstMonomerCol, secondMonomerCol
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
setLinkRules(df: DG.DataFrame) : void {
|
|
146
|
+
const length = df.rowCount;
|
|
147
|
+
const rules: RuleLink [] = new Array<RuleLink>(length);
|
|
148
|
+
const codeCol = df.columns.byName(NAME_CODE);
|
|
149
|
+
const firstMonomerCol = df.columns.byName(NAME_FIRST_MONOMERS);
|
|
150
|
+
const secondMonomerCol = df.columns.byName(NAME_SECOND_MONOMERS);
|
|
151
|
+
const firstLink = df.columns.byName(NAME_FIRST_LINK);
|
|
152
|
+
const secondLink = df.columns.byName(NAME_SECOND_LINK);
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
for (let i = 0; i < length; i++) {
|
|
156
|
+
const rule = {
|
|
157
|
+
code: codeCol.get(i),
|
|
158
|
+
firstMonomers: firstMonomerCol.get(i).split(','),
|
|
159
|
+
secondMonomers: secondMonomerCol.get(i).split(','),
|
|
160
|
+
firstLinkingGroup: firstLink.get(i),
|
|
161
|
+
secondLinkingGroup: secondLink.get(i)
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
rules[i] = rule;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.linkRules = rules;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
setSynthesisRules(df: DG.DataFrame) : void {
|
|
171
|
+
const length = df.rowCount;
|
|
172
|
+
const rules: RuleReaction [] = new Array<RuleReaction>(length);
|
|
173
|
+
const codeCol = df.columns.byName(NAME_CODE);
|
|
174
|
+
const firstMonomerCol = df.columns.byName(NAME_FIRST_MONOMERS);
|
|
175
|
+
const secondMonomerCol = df.columns.byName(NAME_SECOND_MONOMERS);
|
|
176
|
+
const name = df.columns.byName(NAME_REACTION_NAME);
|
|
177
|
+
const firstReactant = df.columns.byName('firstReactant');
|
|
178
|
+
const secondReactant = df.columns.byName('secondReactant');
|
|
179
|
+
const product = df.columns.byName('product');
|
|
180
|
+
|
|
181
|
+
for (let i = 0; i < length; i++) {
|
|
182
|
+
const smartsReaction = `${firstReactant.get(i)}.${secondReactant.get(i)}>>${product.get(i)}`;
|
|
183
|
+
|
|
184
|
+
const rule = {
|
|
185
|
+
code: codeCol.get(i),
|
|
186
|
+
firstMonomers: firstMonomerCol.get(i).split(','),
|
|
187
|
+
secondMonomers: secondMonomerCol.get(i).split(','),
|
|
188
|
+
reaction: smartsReaction,
|
|
189
|
+
name: name.get(i)
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
rules[i] = rule;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
this.reactionRules = rules;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function getRules(ruleFiles: string[]): Promise<Rules> {
|
|
200
|
+
const fileSource = new DG.FileSource(RULES_PATH);
|
|
201
|
+
const rules: Rules = new Rules(null, null, [], []);
|
|
202
|
+
|
|
203
|
+
for (let i = 0; i < ruleFiles.length; i++) {
|
|
204
|
+
const rulesRaw = await fileSource.readAsText(ruleFiles[i].replace(RULES_PATH, ''));
|
|
205
|
+
const ruleSingle : Rules = JSON.parse(rulesRaw);
|
|
206
|
+
|
|
207
|
+
rules.homodimer = ruleSingle.homodimerCode!;
|
|
208
|
+
rules.heterodimer = ruleSingle.heterodimerCode!;
|
|
209
|
+
rules.addLinkRules(ruleSingle.linkRules);
|
|
210
|
+
rules.addSynthesisRules(ruleSingle.reactionRules);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return rules;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function getMonomerPairs(rule: RuleLink | RuleReaction) : [string[], string[]] {
|
|
217
|
+
const allPairsNum = rule.firstMonomers.length*rule.secondMonomers.length;
|
|
218
|
+
const firstMonomers = new Array<string>(allPairsNum);
|
|
219
|
+
const secondMonomers = new Array<string>(allPairsNum);
|
|
220
|
+
|
|
221
|
+
let counter = 0;
|
|
222
|
+
for (let i = 0; i < rule.firstMonomers.length; i++) {
|
|
223
|
+
for (let j = 0; j < rule.secondMonomers.length; j++) {
|
|
224
|
+
firstMonomers[counter] = rule.firstMonomers[i];
|
|
225
|
+
secondMonomers[counter] = rule.secondMonomers[j];
|
|
226
|
+
counter++;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return [firstMonomers, secondMonomers];
|
|
231
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import * as DG from 'datagrok-api/dg';
|
|
2
|
+
import * as grok from 'datagrok-api/grok';
|
|
3
|
+
import {getMonomerPairs, getRules, Rules} from './pt-rules';
|
|
4
|
+
import {_package, applyNotationProviderForCyclized} from '../../package';
|
|
5
|
+
import {getHelmHelper} from '@datagrok-libraries/bio/src/helm/helm-helper';
|
|
6
|
+
import {doPolyToolConvert} from './pt-conversion';
|
|
7
|
+
import {NOTATION} from '@datagrok-libraries/bio/src/utils/macromolecule/consts';
|
|
8
|
+
|
|
9
|
+
export class RulesManager {
|
|
10
|
+
rules: Rules;
|
|
11
|
+
linkRuleDataFrame: DG.DataFrame;
|
|
12
|
+
synthRuleDataFrame: DG.DataFrame;
|
|
13
|
+
fileName: string;
|
|
14
|
+
v: DG.View | null = null;
|
|
15
|
+
|
|
16
|
+
homoDimerInput: DG.InputBase;
|
|
17
|
+
heteroDimerInput: DG.InputBase;
|
|
18
|
+
|
|
19
|
+
currentTab = '';
|
|
20
|
+
|
|
21
|
+
private static instance: RulesManager;
|
|
22
|
+
|
|
23
|
+
protected constructor(rules: Rules, fileName: string) {
|
|
24
|
+
this.rules = rules;
|
|
25
|
+
this.linkRuleDataFrame = this.rules.getLinkRulesDf();
|
|
26
|
+
this.synthRuleDataFrame = this.rules.getSynthesisRulesDf();
|
|
27
|
+
this.fileName = fileName;
|
|
28
|
+
|
|
29
|
+
const homoValue = this.rules.homodimerCode ? this.rules.homodimerCode : '';
|
|
30
|
+
const heteroValue = this.rules.heterodimerCode ? this.rules.heterodimerCode : '';
|
|
31
|
+
this.homoDimerInput = ui.input.string('Homo dimer', {value: homoValue, onValueChanged: () => {}, nullable: false});
|
|
32
|
+
this.heteroDimerInput = ui.input.string(
|
|
33
|
+
'Hetero dimer', {value: heteroValue, onValueChanged: () => {}, nullable: false}
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async getView(): Promise<DG.ViewBase> {
|
|
38
|
+
if (!this.v) {
|
|
39
|
+
this.v = DG.View.create();
|
|
40
|
+
this.v.append(await this.getForm());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static async getInstance(name: string): Promise<RulesManager> {
|
|
47
|
+
if (!this.instance) {
|
|
48
|
+
const rules = await getRules([name]);
|
|
49
|
+
this.instance = new RulesManager(rules, name);
|
|
50
|
+
}
|
|
51
|
+
return this.instance;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
save(): void {
|
|
55
|
+
this.rules.homodimerCode = this.homoDimerInput.value;
|
|
56
|
+
this.rules.heterodimerCode = this.heteroDimerInput.value;
|
|
57
|
+
this.rules.setLinkRules(this.linkRuleDataFrame);
|
|
58
|
+
this.rules.setSynthesisRules(this.synthRuleDataFrame);
|
|
59
|
+
|
|
60
|
+
const saveable = {
|
|
61
|
+
homodimerCode: this.rules.homodimerCode,
|
|
62
|
+
heterodimerCode: this.rules.heterodimerCode,
|
|
63
|
+
linkRules: this.rules.linkRules,
|
|
64
|
+
reactionRules: this.rules.reactionRules,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const rrrr = JSON.stringify(saveable, undefined, 2);
|
|
68
|
+
_package.files.writeAsText(`polytool-rules/${this.fileName}`, rrrr);
|
|
69
|
+
grok.shell.info(`Polytool rules at ${this.fileName} was updated`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private createGridDiv(name: string, grid: DG.Grid) {
|
|
73
|
+
const header = ui.h1(name, 'polytool-grid-header');
|
|
74
|
+
grid.root.prepend(header);
|
|
75
|
+
grid.root.style.height = '100%';
|
|
76
|
+
return grid.root;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
async getLinkExamplesGrid() {
|
|
80
|
+
const seqs: string[] = [];
|
|
81
|
+
|
|
82
|
+
for (let i = 0; i < this.rules.linkRules.length; i++) {
|
|
83
|
+
const code = this.rules.linkRules[i].code;
|
|
84
|
+
const [firstMonomers, secondMonomers] = getMonomerPairs(this.rules.linkRules[i]);
|
|
85
|
+
for (let j = 0; j < firstMonomers.length; j++) {
|
|
86
|
+
const seq = `${firstMonomers[j]}(${code})-A-A-A-A-${secondMonomers[j]}(${code})-A`;
|
|
87
|
+
seqs.push(seq);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const helmHelper = await getHelmHelper();
|
|
91
|
+
|
|
92
|
+
const helms = doPolyToolConvert(seqs, this.rules, helmHelper);
|
|
93
|
+
|
|
94
|
+
const initCol = DG.Column.fromStrings('monomers', seqs);
|
|
95
|
+
const helmCol = DG.Column.fromStrings('helm', helms);
|
|
96
|
+
|
|
97
|
+
initCol.semType = DG.SEMTYPE.MACROMOLECULE;
|
|
98
|
+
applyNotationProviderForCyclized(initCol, '-');
|
|
99
|
+
|
|
100
|
+
helmCol.semType = DG.SEMTYPE.MACROMOLECULE;
|
|
101
|
+
helmCol.meta.units = NOTATION.HELM;
|
|
102
|
+
helmCol.setTag(DG.TAGS.CELL_RENDERER, 'helm');
|
|
103
|
+
return DG.DataFrame.fromColumns([
|
|
104
|
+
initCol, helmCol
|
|
105
|
+
]).plot.grid();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async getReactionExamplesGrid() {
|
|
109
|
+
const seqs: string[] = [];
|
|
110
|
+
|
|
111
|
+
for (let i = 0; i < this.rules.reactionRules.length; i++) {
|
|
112
|
+
const code = this.rules.reactionRules[i].code;
|
|
113
|
+
const [firstMonomers, secondMonomers] = getMonomerPairs(this.rules.reactionRules[i]);
|
|
114
|
+
for (let j = 0; j < firstMonomers.length; j++) {
|
|
115
|
+
const seq = `${firstMonomers[j]}(${code})-A-A-A-A-${secondMonomers[j]}(${code})-A`;
|
|
116
|
+
seqs.push(seq);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const helmHelper = await getHelmHelper();
|
|
120
|
+
|
|
121
|
+
const helms = doPolyToolConvert(seqs, this.rules, helmHelper);
|
|
122
|
+
|
|
123
|
+
const initCol = DG.Column.fromStrings('monomers', seqs);
|
|
124
|
+
const helmCol = DG.Column.fromStrings('helm', helms);
|
|
125
|
+
|
|
126
|
+
initCol.semType = DG.SEMTYPE.MACROMOLECULE;
|
|
127
|
+
applyNotationProviderForCyclized(initCol, '-');
|
|
128
|
+
|
|
129
|
+
helmCol.semType = DG.SEMTYPE.MACROMOLECULE;
|
|
130
|
+
helmCol.meta.units = NOTATION.HELM;
|
|
131
|
+
helmCol.setTag(DG.TAGS.CELL_RENDERER, 'helm');
|
|
132
|
+
return DG.DataFrame.fromColumns([
|
|
133
|
+
initCol, helmCol
|
|
134
|
+
]).plot.grid();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async getForm() {
|
|
138
|
+
inputsTabControl: DG.TabControl;
|
|
139
|
+
|
|
140
|
+
const dimerInputsDiv = ui.divV([
|
|
141
|
+
this.homoDimerInput,
|
|
142
|
+
this.heteroDimerInput,
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
const linkExamples = await this.getLinkExamplesGrid();
|
|
146
|
+
const reactionExamples = await this.getReactionExamplesGrid();
|
|
147
|
+
|
|
148
|
+
const inputsTabControl = ui.tabControl({
|
|
149
|
+
'Links': linkExamples,
|
|
150
|
+
'Reactions': reactionExamples,
|
|
151
|
+
'Dimers': dimerInputsDiv,
|
|
152
|
+
}, false);
|
|
153
|
+
|
|
154
|
+
inputsTabControl.root.style.height = '90%';
|
|
155
|
+
inputsTabControl.root.style.width = '100%';
|
|
156
|
+
inputsTabControl.root.classList.add('rules-manager-form-tab-control');
|
|
157
|
+
inputsTabControl.header.style.marginBottom = '10px';
|
|
158
|
+
|
|
159
|
+
const linksGridDiv = this.createGridDiv('Link rules', this.linkRuleDataFrame.plot.grid({showAddNewRowIcon: true}));
|
|
160
|
+
const reactionsGridDiv =
|
|
161
|
+
this.createGridDiv('Reaction rules', this.synthRuleDataFrame.plot.grid({showAddNewRowIcon: true}));
|
|
162
|
+
|
|
163
|
+
linksGridDiv.style.width = '100%';
|
|
164
|
+
reactionsGridDiv.style.width = '100%';
|
|
165
|
+
|
|
166
|
+
const divs = [linksGridDiv, reactionsGridDiv, ui.div()];
|
|
167
|
+
divs[0].style.removeProperty('display');
|
|
168
|
+
divs[1].style.display = 'none';
|
|
169
|
+
divs[2].style.display = 'none';
|
|
170
|
+
|
|
171
|
+
inputsTabControl.onTabChanged.subscribe(() => {
|
|
172
|
+
this.currentTab = inputsTabControl.currentPane.name;
|
|
173
|
+
|
|
174
|
+
const idx = inputsTabControl.panes.findIndex((p) => p.name == this.currentTab);
|
|
175
|
+
|
|
176
|
+
for (let i = 0; i < divs.length; i++) {
|
|
177
|
+
if (i == idx)
|
|
178
|
+
divs[i].style.removeProperty('display');
|
|
179
|
+
else
|
|
180
|
+
divs[i].style.display = 'none';
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const saveButton = ui.bigButton('Save changes', () => {
|
|
185
|
+
this.save();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
const panel = ui.divV([
|
|
190
|
+
inputsTabControl.root,
|
|
191
|
+
saveButton
|
|
192
|
+
]);
|
|
193
|
+
|
|
194
|
+
panel.style.height = '100%';
|
|
195
|
+
panel.style.alignItems = 'center';
|
|
196
|
+
|
|
197
|
+
const form = ui.splitH([
|
|
198
|
+
panel,
|
|
199
|
+
ui.divV(divs, {style: {width: '100%'}})
|
|
200
|
+
], {style: {width: '100%'}}, true);
|
|
201
|
+
form.style.height = '100%';
|
|
202
|
+
return form;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
@@ -4,7 +4,7 @@ import * as DG from 'datagrok-api/dg';
|
|
|
4
4
|
import {_package} from '../package';
|
|
5
5
|
import {defaultErrorHandler} from '../utils/err-info';
|
|
6
6
|
import {PT_UI_DIALOG_CONVERSION, PT_UI_RULES_USED} from './const';
|
|
7
|
-
import {RuleInputs, RULES_PATH, RULES_STORAGE_NAME} from './pt-rules';
|
|
7
|
+
import {RuleInputs, RULES_PATH, RULES_STORAGE_NAME} from './conversion/pt-rules';
|
|
8
8
|
|
|
9
9
|
/** Inputs of polyToolConvert2 package function */
|
|
10
10
|
export enum P {
|