-
Notifications
You must be signed in to change notification settings - Fork 12
Refactor/extract scripting api #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa1e580
Broken commit: Move folding-related scripting API to another module
guyguy2001 78dc724
Fix FoldingContextAPI by using _isPseudoknot
guyguy2001 4ec97e6
Move FoldingContextAPI's script interface to be a normal argument
guyguy2001 7e73c3d
Move the folding eternaScript API to a better file/directory
guyguy2001 c7ce02a
Extract the 'select_folder' api to an external file
guyguy2001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, { | ||
|
|
@@ -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'; | ||
|
|
@@ -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'; | ||
|
|
@@ -1269,145 +1270,17 @@ export default class PoseEditMode extends GameMode { | |
| this._puzzle.getBarcodeHairpin(Sequence.fromSequenceString(seq)).sequenceString() | ||
| )); | ||
|
|
||
| this._scriptInterface.addCallback('current_folder', (): string | null => ( | ||
| 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, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably broken due to |
||
| scriptInterface: this._scriptInterface | ||
| }); | ||
|
|
||
| this._scriptInterface.addCallback('load_parameters_from_buffer', (_str: string): boolean => { | ||
| log.info('TODO: load_parameters_from_buffer'); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)