Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PFAM parser for Spidermonkey developer shell
//- JavaScript source code
//- parse_pfam-min.js ~~
//
// This is the minimal version of "parse_pfam.js" requested by Malay Basu.
//
// ~~ (c) SRW, 24 Feb 2014
// ~~ last updated 24 Feb 2014
(function main(argv) {
var filename, pfam_dic;
filename = argv[0];
pfam_dic = {};
read(filename).trim().split('\n').forEach(function (line) {
var pfam, pid, vec;
if (line[0] !== '#') {
vec = line.split('\t');
pid = vec[0];
pfam = vec[5];
if (pfam_dic.hasOwnProperty(pid) === false) {
pfam_dic[pid] = [];
}
pfam_dic[pid].push(pfam);
}
});
print(JSON.stringify(pfam_dic['P04637']));
}(this.scriptArgs || this.arguments || []));
//- vim:set syntax=javascript:
//- JavaScript source code
//- parse_pfam.js ~~
//
// This program implements the same things we covered today in the computing
// class, but it uses JavaScript rather than Python. It targets the developer
// shells like Google V8, Mozilla Spidermonkey, and JavaScriptCore. There are
// a lot of unnecessary things in this code because I was experimenting with
// `readline` functions, also.
//
// To duplicate today's "parse_pfam.py" demo from class, run either of
//
// $ d8 parse_pfam.js -- 9606.tsv
// $ js -f parse_pfam.js -- 9606.tsv
//
// ~~ (c) SRW, 24 Feb 2014
// ~~ last updated 24 Feb 2014
(function (global) {
'use strict';
// Pragmas
/*global */
/*jshint maxparams: 1, quotmark: single, strict: true */
/*jslint indent: 4, maxlen: 80 */
/*properties
apply, arguments, call, forEach, hasOwnProperty, print, prototype,
push, read, readline, scriptArgs, slice, split, stringify, trim
*/
// Declarations
var getArgs, print, read, readline;
// Definitions
getArgs = function () {
// This function needs documentation.
if (global.hasOwnProperty('arguments')) {
return Array.prototype.slice.call(global.arguments);
}
if (global.hasOwnProperty('scriptArgs')) {
return Array.prototype.slice.call(global.scriptArgs);
}
return [];
};
print = function () {
// This function uses lazy-loading to redefine itself when it first runs.
// I have written it this way because `print` isn't always available, but
// it's not always needed, either; it's annoying for the program to throw
// errors because it's trying to anticipate what you're going to need.
if (global.hasOwnProperty('print')) {
print = global.print;
} else {
print = function () {
// This function throws an error every time it is invoked.
throw new Error('No native `print` function available.');
};
}
return print.apply(this, arguments);
};
read = function (filename) {
// This function uses lazy-loading to redefine itself when it first runs.
// I have written it this way because `read` isn't always available, but
// it's not always needed, either; it's annoying for the program to throw
// errors because it's trying to anticipate what you're going to need.
if (global.hasOwnProperty('read')) {
read = global.read;
} else {
read = function (filename) {
// This function throws an error every time it is invoked.
throw new Error('No native `read` function available.');
};
}
return read(filename);
};
readline = function (filename) {
// This function uses lazy-loading to redefine itself when it first runs.
// I have written it this way because `readline` isn't always available,
// but it's not always needed, either; it's annoying for the program to
// throw errors because it's trying to anticipate your every move.
if (global.hasOwnProperty('readline')) {
readline = global.readline;
} else {
readline = function (filename) {
// This function throws an error every time it is invoked.
throw new Error('No native `readline` function available.');
};
}
return readline(filename);
};
// Demonstrations
(function () {
// This is just for fun :-P
var argv, main;
argv = getArgs();
/*
main = function (argv) {
// This function needs documentation.
var line;
while (line = readline()) {
print(line);
}
return;
};
*/
main = function (argv) {
// This follows the "parse_pfam.py" demo from today's class ...
var filename, pfam_dic, rows;
filename = argv[0];
pfam_dic = {};
rows = read(filename).trim().split('\n');
rows.forEach(function (row) {
// This implements the `for` loop used in the Python example.
var pfam, pid, vec;
if (row[0] !== '#') {
vec = row.split('\t');
pid = vec[0];
pfam = vec[5];
if (pfam_dic.hasOwnProperty(pid) === false) {
pfam_dic[pid] = [];
}
pfam_dic[pid].push(pfam);
}
return;
});
//print(JSON.stringify(pfam_dic, undefined, 4));
print(JSON.stringify(pfam_dic['P04637']));
return;
};
main(argv);
return;
}());
// That's all, folks!
return;
}(Function.prototype.call.call(function (that) {
'use strict';
// This strict anonymous closure encapsulates the logic for detecting which
// object in the environment should be treated as _the_ global object. It's
// not as easy as you may think -- strict mode disables the `call` method's
// default behavior of replacing `null` with the global object. Luckily, we
// can work around that by passing a reference to the enclosing scope as an
// argument at the same time and testing to see if strict mode has done its
// deed. This task is not hard in the usual browser context because we know
// that the global object is `window`, but CommonJS implementations such as
// RingoJS confound the issue by modifying the scope chain, running scripts
// in sandboxed contexts, and using identifiers like `global` carelessly ...
/*global global: false */
/*jslint indent: 4, maxlen: 80 */
/*properties global */
if (this === null) {
// Strict mode has captured us, but we already passed a reference :-)
return (typeof global === 'object') ? global : that;
}
// Strict mode isn't supported in this environment, but we need to make sure
// we don't get fooled by Rhino's `global` function.
return (typeof this.global === 'object') ? this.global : this;
}, null, this)));
//- vim:set syntax=javascript:
@wilkinson

This comment has been minimized.

Copy link
Owner Author

commented Feb 24, 2014

The data are available here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.