Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 170 additions & 0 deletions src/eterna/eternaScript/FoldingAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import EPars from 'eterna/EPars';
import Folder, {SuboptEnsembleResult} from 'eterna/folding/Folder';
import DotPlot from 'eterna/rnatypes/DotPlot';
import SecStruct from 'eterna/rnatypes/SecStruct';
import Sequence from 'eterna/rnatypes/Sequence';
import {ExternalInterfaceCtx} from 'eterna/util/ExternalInterface';
import {Assert} from 'flashbang';

/**
* An EternaScript API exposing all functions that handle folding an arbitrary
* RNA sequence, and aren't dependent on what's the RNA in the puzzle.
*
* It adds itself to an existing script API (`ExternalInterfaceCtx`) from
* `this.registerToScriptInterface`.
*
* Note: The API in this class is still affected by the selected folder
* and the pseudoknot mode of the puzzle - just not the sequence.
*/
export default class FoldingAPI {
private readonly _getFolder: () => Folder | null;
private readonly _getIsPseudoknot: () => boolean;

private get _folder(): Folder | null {
return this._getFolder();
}

private get _isPseudoknot(): boolean {
return this._getIsPseudoknot();
}

constructor(params: { getFolder: () => Folder, getIsPseudoknot: () => boolean }) {
this._getIsPseudoknot = params.getIsPseudoknot;
this._getFolder = params.getFolder;
}

public registerToScriptInterface(scriptInterface: ExternalInterfaceCtx) {
scriptInterface.addCallback('current_folder', (): string | null => (
this._folder ? this._folder.name : null
));

scriptInterface.addCallback('fold',
(seq: string, constraint: string | null = null): string | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const folded: SecStruct | null = this._folder.foldSequence(
seqArr, null, constraint, this._isPseudoknot
);
Assert.assertIsDefined(folded);
return folded.getParenthesis(null, this._isPseudoknot);
});

scriptInterface.addCallback('fold_with_binding_site',
(seq: string, site: number[], bonus: number): string | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const folded: SecStruct | null = this._folder.foldSequenceWithBindingSite(
seqArr, null, site, Math.floor(bonus * 100), 2.5
);
if (folded === null) {
return null;
}
return folded.getParenthesis();
});

scriptInterface.addCallback('energy_of_structure', (seq: string, secstruct: string): number | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const structArr: SecStruct = SecStruct.fromParens(secstruct);
const freeEnergy = this._isPseudoknot ? this._folder.scoreStructures(seqArr, structArr, true)
: this._folder.scoreStructures(seqArr, structArr);
return 0.01 * freeEnergy;
});

// AMW: still give number[] back because external scripts may rely on it
scriptInterface.addCallback('pairing_probabilities',
(seq: string, secstruct: string | null = null): number[] | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
let folded: SecStruct | null;
if (secstruct) {
folded = SecStruct.fromParens(secstruct);
} else {
folded = this._folder.foldSequence(seqArr, null, null);
if (folded === null) {
return null;
}
}
const pp: DotPlot | null = this._folder.getDotPlot(seqArr, folded);
Assert.assertIsDefined(pp);
return pp.data;
});

scriptInterface.addCallback('subopt_single_sequence',
(
seq: string, kcalDelta: number,
pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): SuboptEnsembleResult | null => {
if (this._folder === null) {
return null;
}
// now get subopt stuff
const seqArr: Sequence = Sequence.fromSequenceString(seq);
return this._folder.getSuboptEnsembleNoBindingSite(seqArr,
kcalDelta, pseudoknotted, temp);
});

scriptInterface.addCallback('subopt_oligos',
(
seq: string, oligoStrings: string[], kcalDelta: number,
pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): SuboptEnsembleResult | null => {
if (this._folder === null) {
return null;
}
// make the sequence string from the oligos
let newSequence: string = seq;
for (let oligoIndex = 0; oligoIndex < oligoStrings.length; oligoIndex++) {
const oligoSequence: string = oligoStrings[oligoIndex];
newSequence = `${newSequence}&${oligoSequence}`;
}

// now get subopt stuff
const seqArr: Sequence = Sequence.fromSequenceString(newSequence);
return this._folder.getSuboptEnsembleWithOligos(seqArr,
oligoStrings, kcalDelta, pseudoknotted, temp);
});

scriptInterface.addCallback('cofold',
(
seq: string, oligo: string, malus: number = 0.0, constraint: string | null = null
): string | null => {
if (this._folder === null) {
return null;
}
const len: number = seq.length;
const cseq = `${seq}&${oligo}`;
const seqArr: Sequence = Sequence.fromSequenceString(cseq);
const folded: SecStruct | null = this._folder.cofoldSequence(
seqArr, null, Math.floor(malus * 100), constraint
);
if (folded === null) {
return null;
}
return `${folded.slice(0, len).getParenthesis()
}&${folded.slice(len).getParenthesis()}`;
});

scriptInterface.addCallback('get_defect',
(
seq: string, secstruct: string, pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): number | null => {
if (this._folder === null) {
return null;
}
return this._folder.getDefect(
Sequence.fromSequenceString(seq),
SecStruct.fromParens(secstruct),
temp, pseudoknotted
);
});
}
}
13 changes: 13 additions & 0 deletions src/eterna/eternaScript/SelectFolderAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {ExternalInterfaceCtx} from 'eterna/util/ExternalInterface';

/**
* Adds the ability to modify the folder (via its name) to the EternaScript API.
*/
export default function addSelectFolderAPIToInterface({selectFolder, scriptInterface}: {
selectFolder: (folderName: string) => boolean,
scriptInterface: ExternalInterfaceCtx,
}) {
scriptInterface.addCallback(
'select_folder', (folderName: string): boolean => selectFolder(folderName)
);
}
151 changes: 12 additions & 139 deletions src/eterna/mode/PoseEdit/PoseEditMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import Fonts from 'eterna/util/Fonts';
import EternaSettingsDialog, {EternaViewOptionsMode} from 'eterna/ui/EternaSettingsDialog';
import FolderManager from 'eterna/folding/FolderManager';
import Folder, {MultiFoldResult, CacheKey, SuboptEnsembleResult} from 'eterna/folding/Folder';
import Folder, {MultiFoldResult, CacheKey} from 'eterna/folding/Folder';
import {PaletteTargetType, GetPaletteTargetBaseType} from 'eterna/ui/toolbar/NucleotidePalette';
import PoseField from 'eterna/pose2D/PoseField';
import Pose2D, {
Expand Down Expand Up @@ -56,7 +56,6 @@ import {AchievementData} from 'eterna/achievements/AchievementManager';
import {RankScrollData} from 'eterna/rank/RankScroll';
import FolderSwitcher from 'eterna/ui/FolderSwitcher';
import MarkerSwitcher from 'eterna/ui/MarkerSwitcher';
import DotPlot from 'eterna/rnatypes/DotPlot';
import {Oligo, OligoMode} from 'eterna/rnatypes/Oligo';
import SecStruct from 'eterna/rnatypes/SecStruct';
import Sequence from 'eterna/rnatypes/Sequence';
Expand All @@ -79,6 +78,8 @@ import {FederatedPointerEvent} from '@pixi/events';
import NuPACK from 'eterna/folding/NuPACK';
import PasteStructureDialog from 'eterna/ui/PasteStructureDialog';
import ConfirmTargetDialog from 'eterna/ui/ConfirmTargetDialog';
import FoldingContextScriptAPI from 'eterna/eternaScript/FoldingAPI';
import addSelectFolderAPIToInterface from 'eterna/eternaScript/SelectFolderAPI';
import GameMode from '../GameMode';
import SubmittingDialog from './SubmittingDialog';
import SubmitPoseDialog from './SubmitPoseDialog';
Expand Down Expand Up @@ -1269,145 +1270,17 @@ export default class PoseEditMode extends GameMode {
this._puzzle.getBarcodeHairpin(Sequence.fromSequenceString(seq)).sequenceString()
));

this._scriptInterface.addCallback('current_folder', (): string | null => (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should go commit by commit, since the first commit doesn't modify this code, only moves it (and adds code around it)

this._folder ? this._folder.name : null
));

this._scriptInterface.addCallback('fold',
(seq: string, constraint: string | null = null): string | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const pseudoknots = this._targetConditions && this._targetConditions[0]
&& this._targetConditions[0]['type'] === 'pseudoknot';
const folded: SecStruct | null = this._folder.foldSequence(seqArr, null, constraint, pseudoknots);
Assert.assertIsDefined(folded);
return folded.getParenthesis(null, pseudoknots);
});

this._scriptInterface.addCallback('fold_with_binding_site',
(seq: string, site: number[], bonus: number): string | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const folded: SecStruct | null = this._folder.foldSequenceWithBindingSite(
seqArr, null, site, Math.floor(bonus * 100), 2.5
);
if (folded === null) {
return null;
}
return folded.getParenthesis();
});

this._scriptInterface.addCallback('energy_of_structure', (seq: string, secstruct: string): number | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
const structArr: SecStruct = SecStruct.fromParens(secstruct);
const freeEnergy = (this._targetConditions && this._targetConditions[0]
&& this._targetConditions[0]['type'] === 'pseudoknot')
? this._folder.scoreStructures(seqArr, structArr, true)
: this._folder.scoreStructures(seqArr, structArr);
return 0.01 * freeEnergy;
});

// AMW: still give number[] back because external scripts may rely on it
this._scriptInterface.addCallback('pairing_probabilities',
(seq: string, secstruct: string | null = null): number[] | null => {
if (this._folder === null) {
return null;
}
const seqArr: Sequence = Sequence.fromSequenceString(seq);
let folded: SecStruct | null;
if (secstruct) {
folded = SecStruct.fromParens(secstruct);
} else {
folded = this._folder.foldSequence(seqArr, null, null);
if (folded === null) {
return null;
}
}
const pp: DotPlot | null = this._folder.getDotPlot(seqArr, folded);
Assert.assertIsDefined(pp);
return pp.data;
});

this._scriptInterface.addCallback('subopt_single_sequence',
(
seq: string, kcalDelta: number,
pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): SuboptEnsembleResult | null => {
if (this._folder === null) {
return null;
}
// now get subopt stuff
const seqArr: Sequence = Sequence.fromSequenceString(seq);
return this._folder.getSuboptEnsembleNoBindingSite(seqArr,
kcalDelta, pseudoknotted, temp);
});

this._scriptInterface.addCallback('subopt_oligos',
(
seq: string, oligoStrings: string[], kcalDelta: number,
pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): SuboptEnsembleResult | null => {
if (this._folder === null) {
return null;
}
// make the sequence string from the oligos
let newSequence: string = seq;
for (let oligoIndex = 0; oligoIndex < oligoStrings.length; oligoIndex++) {
const oligoSequence: string = oligoStrings[oligoIndex];
newSequence = `${newSequence}&${oligoSequence}`;
}

// now get subopt stuff
const seqArr: Sequence = Sequence.fromSequenceString(newSequence);
return this._folder.getSuboptEnsembleWithOligos(seqArr,
oligoStrings, kcalDelta, pseudoknotted, temp);
});

this._scriptInterface.addCallback('cofold',
(
seq: string, oligo: string, malus: number = 0.0, constraint: string | null = null
): string | null => {
if (this._folder === null) {
return null;
}
const len: number = seq.length;
const cseq = `${seq}&${oligo}`;
const seqArr: Sequence = Sequence.fromSequenceString(cseq);
const folded: SecStruct | null = this._folder.cofoldSequence(
seqArr, null, Math.floor(malus * 100), constraint
);
if (folded === null) {
return null;
}
return `${folded.slice(0, len).getParenthesis()
}&${folded.slice(len).getParenthesis()}`;
});

this._scriptInterface.addCallback('get_defect',
(
seq: string, secstruct: string, pseudoknotted: boolean, temp: number = EPars.DEFAULT_TEMPERATURE
): number | null => {
if (this._folder === null) {
return null;
}
return this._folder.getDefect(
Sequence.fromSequenceString(seq),
SecStruct.fromParens(secstruct),
temp, pseudoknotted
);
});
new FoldingContextScriptAPI({
getFolder: () => this._folder,
getIsPseudoknot: () => Boolean(this._targetConditions && this._targetConditions[0]
&& this._targetConditions[0]['type'] === 'pseudoknot')
}).registerToScriptInterface(this._scriptInterface);

if (this._puzzle.puzzleType === PuzzleType.EXPERIMENTAL) {
this._scriptInterface.addCallback(
'select_folder', (folderName: string): boolean => this.selectFolder(folderName)
);
addSelectFolderAPIToInterface({
selectFolder: this.selectFolder,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably broken due to this shenanigans

scriptInterface: this._scriptInterface
});

this._scriptInterface.addCallback('load_parameters_from_buffer', (_str: string): boolean => {
log.info('TODO: load_parameters_from_buffer');
Expand Down