Skip to content

Instantly share code, notes, and snippets.

@linnil1
Last active May 18, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnil1/483317c7b4296162f8d8b43a9f40f6f7 to your computer and use it in GitHub Desktop.
Save linnil1/483317c7b4296162f8d8b43a9f40f6f7 to your computer and use it in GitHub Desktop.
str+pdb2upl+lol
<div class="container" id="app">
<div class="input-group-prepend">
<span class="input-group-text">.str file</span>
<input type="file" @change="fileUpload($event, 'strdata')" />
</div>
<div class="input-group">
<textarea v-model="strdata" class="form-control"></textarea>
</div>
<div class="input-group-prepend">
<span class="input-group-text">.pdb file</span>
<input type="file" @change="fileUpload($event, 'pdbdata')" />
</div>
<div class="input-group">
<textarea v-model="pdbdata" class="form-control"></textarea>
</div>
<div class="btn-group m-4" role="group">
<button type="button" class="btn btn-primary" v-on:click="goClick">.str .pdb convert to .upl .lol</button>
</div>
<div class="input-group-prepend">
<span class="input-group-text">.upl file</span>
</div>
<div class="input-group">
<textarea v-model="upldata" class="form-control"></textarea>
</div>
<div class="input-group-prepend">
<span class="input-group-text">.lol file</span>
</div>
<div class="input-group">
<textarea v-model="loldata" class="form-control"></textarea>
</div>
<div class="btn-group m-4" role="group">
<button type="button" class="btn btn-primary" v-on:click="goDownload()"> Download </button>
</div>
</div>
function regexGet(regex, str) {
var m;
var matchs = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
matchs.push(m);
}
return matchs;
}
// https://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
function download(filename, text) {
var element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
new Vue({
el: "#app",
data: {
strdata: ` **testing**
assi sele resid 1 .and. type HT3 end -
sele resid 2 .and. type HN end -
rmin 1.94190 rmax 5.94190 -
rexp @rexp fmax @fmax rswi @rswi sexp @sexp kmin @kmin kmax @kmax SUMR
assi sele resid 1 .and. type HT3 end -
sele resid 2 .and. type HA2 end -
rmin 2.58100 rmax 6.58100 -
rexp @rexp fmax @fmax rswi @rswi sexp @sexp kmin @kmin kmax @kmax SUMR`,
pdbdata: `MODEL 1
ATOM 18 1HE2 GLN 1 3.477 -1.956 1.651 1.00 1.00 TAG
ATOM 19 2HE2 GLN 1 2.439 -2.764 0.524 1.00 1.00 TAG
ATOM 20 N GLY 2 -0.652 2.217 0.763 1.00 1.00 TAG
ATOM 21 CA GLY 2 -1.519 3.387 0.826 1.00 1.00 TAG`,
upldata: "",
loldata: ""
},
methods: {
goClick: function() {
// get dictionary
var dict_num = {};
this.pdbdata.split("\n").forEach(pdb => {
var pdbs = pdb.split(/\s+/);
if (pdbs.length > 4) dict_num[pdbs[4]] = pdbs[3];
});
// parse main
this.upldata = this.loldata = "";
var units = regexGet(/assi[\s\S]*?SUMR/gm, this.strdata);
units.forEach(unit_match => {
var id_data = regexGet(/resid\s*(\d+).*?type (\w+)/gm, unit_match);
var min_max = regexGet(
/rmin\s*([\.\d]+) rmax\s*([\.\d]+)/gm,
unit_match
);
// possible error
if (
!(
min_max.length === 1 &&
min_max[0].length === 3 &&
id_data[0].length === 3 &&
id_data[1].length === 3 &&
id_data.length === 2
)
)
console.log(unit_match);
min_max = [parseFloat(min_max[0][1]), parseFloat(min_max[0][2])];
// transfer to this format
// 109 GLU HG3 110 GLN H 5.90 #peak 13
var resids = id_data.map(m =>
[
(" " + m[1]).slice(-3),
(dict_num[m[1]] + " ").slice(0, 4),
(m[2] + " ").slice(0, 5)
].join(" ")
);
this.upldata +=
resids.join(" ") + " " + min_max[1].toPrecision(3) + "\n";
this.loldata +=
resids.join(" ") + " " + min_max[0].toPrecision(3) + "\n";
});
console.log(123);
},
fileUpload: function(evt, data) {
console.log(data);
var files = evt.target.files || evt.dataTransfer.files;
if (!files.length) return;
var reader = new FileReader();
reader.onload = event => {
// The file's text will be printed here
this[data] = event.target.result;
};
reader.readAsText(files[0]);
},
goDownload: function() {
download("test.upl", this.upldata);
download("test.lol", this.loldata);
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
textarea {
min-height: 20vh;
font-family: monospace;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment