Tsumego

Fills the sequence of a single-state puzzle according to simple heuristics.
var Lib = new Library();

var applet = document.getElementById('maingame');
if (applet == null) return "maingame element missing";

var targets = applet.get_targets();
if (targets.length > 1) {
alert("This script is designed for single state puzzles only.");
return "not applicable";
}
var structure = targets[0]['secstruct'];

var rna = new RNA(structure);
var sequence = new Array;

var i,idx;

// fill with '-'
rna.map(function(e) {
idx = e.getIndices();
for( i=0; i < idx.length; i++ ) sequence[idx[i]] = '-';
});

// fill loops with A
rna.map(function(e) {
if( e.getBaseType() == RNAElement.Stack ) return;
idx = e.getIndices();
for( i=0; i < idx.length; i++ ) sequence[idx[i]] = 'A';
});

// fill stacks with AU (random orientation, except at the ends)
rna.map(function(e) {
if( e.getBaseType() == RNAElement.Loop ) return;
idx = e.getIndices();
len = idx.length / 2;
for( i=0; i < len; i++ ) {
sequence[idx[i]] = i==0 ? "A" : i==len-1 ? "U" : Lib.randomSequenceWithBases(1, "AU");
sequence[idx[idx.length-1-i]] =
String.fromCharCode( sequence[idx[i]].charCodeAt()
^ "A".charCodeAt()
^ "U".charCodeAt() );
}
});

// reorient pairs at bulges, and strengthen them when necessary
rna.map(function(e) {
if( e.getBaseType() != RNAElement.Loop
|| e.getType() != RNAElement.Bulge ) return;
idx = e.getIndices();
pm = rna.getPairmap(rna.structure);
pa1 = idx[0]-1;
pa2 = pm[pa1];
ca1 = idx[idx.length-1]+1;
ca2 = pm[ca1];

if( e.getParent().getIndices().length < 6 ) {
if( pa1<pa2 ) {
sequence[pa1] = idx.length==1 ? "G" : "C";
sequence[pa2] = idx.length==1 ? "C" : "G";
} else {
sequence[ca1] = "C";
sequence[ca2] = "G";
}
} else {
sequence[pa1<pa2?pa1:ca1] = "U";
sequence[pa1<pa2?pa2:ca2] = "A";
}

Discussion


Help Eterna invent medicine. Make a donation today.

TwitterFacebookInstagramYouTubeTwitchGitHub