-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
395 lines (344 loc) · 11.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// rewriting a zillioniem pdb file parser.
// expose a parse function/ Input is file or stream
// return a pdb instance
// API for pdbAtomRecord AKA model to directly manipulate a pdb record OR a set of pdb record (for superimposition).
// Usage :
//var h = p.parse({file : "/data/dev/ardock/scriptPackage/test/4MOW.pdb"})
// .on('end', function (obj){ pdb = obj;});
//console.log(pdb.model(1).resName("AS*")
// .resSeq("220:230", "24*").name("CA", "CB")
// .delete({resSeq : ['221:223', 224], name : '*B'})
// .dump());
var naturals = ['ALA', 'ASP', 'ARG', 'ASN', 'CYS',
'GLU', 'GLN', 'GLY', 'HIS', 'LEU',
'ILE', 'LYS', 'MET', 'PHE', 'PRO',
'SER', 'THR', 'TYR', 'TRY', 'TRP',
'VAL'];
var events = require('events');
var stream = require('stream');
var fs = require('fs'),
byline = require('byline');
var preambule = require('./lib/preambule.js');
var coordinates = require('./lib/coordinates.js');
var seqres = require('./lib/seqres.js');
//var _ = require('underscore');
var re = /^([\S]+)/;
var parseBuffer = function () {
return {
seqres : { tags : seqres.sectionTags, data : [] },
coordinates : {tags : coordinates.sectionTags, data : [] },
preambule : {tags : preambule.sectionTags, data : [] }
};
}
var arrayCoherce = function(value) {
var param = [];
if (typeof(value) === 'string')
param.push(value);
else if (typeof(value) === 'array')
param = value;
else
throw 'parameter must be a list or a string ' + typeof(value);
return param;
};
var PdbObject = function(data) {
this.currentModel = 1;
this.altLocPreserve = false;
this.preambule = null;
this.seqres = null;
this.coordinates = null;
this.currentSelection = [];
// TO DO DEVELOP high level selection parser
this.remove = function(expression) {
return this;
};
this.select = function(expression) {
return this;
};
// Set B-factor of current selection
this.bFactor = function(value, type) { // assign or read bFactor value to/from current selection
this.coordinates.bFactor(this.currentSelection, value, type);
};
// Number of atoms in current selection
this.selecSize = function() {
return this.currentSelection.length;
};
// non-redundant list of chains found in currentSelection
this.listChainID = function() {
var chainList = this.coordinates.listChainID(this.currentSelection);
return chainList
}
// Initialize/ reset selection
this.model = function(string) {
this.currentSelection = this.coordinates.model(string);
return this;
},
// Selector short-cut
this.naturalAminoAcidOnly = function() {
var self = this.resName.apply(this, naturals);
return self;
};
// Basic Selectors
this.chain = function () { // could receive an array as sole argument
var nArgs = [this.currentSelection];
if(Array.isArray(arguments[0])) {
arguments[0].forEach(function(e){
nArgs.push(e);
});
} else {
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
}
this.currentSelection = this.coordinates.chain.apply(this.coordinates, nArgs);
return this;
};
this.name = function (args) {
var nArgs = [this.currentSelection];
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
this.currentSelection = this.coordinates.name.apply(this.coordinates, nArgs);
return this;
};
this.resSeq = function (args) {
var nArgs = [this.currentSelection];
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
this.currentSelection = this.coordinates.resSeq.apply(this.coordinates, nArgs);
return this;
};
this.resName = function (args) {
var nArgs = [this.currentSelection];
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
this.currentSelection = this.coordinates.resName.apply(this.coordinates, nArgs);
return this;
};
// Deleters
this.chainDel = function () {
var nArgs = [this.currentSelection];
if(Array.isArray(arguments[0])) {
arguments[0].forEach(function(e){
nArgs.push(e);
});
} else {
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
}
this.currentSelection = this.coordinates.delChain.apply(this.coordinates, nArgs)
return this;
};
this.nameDel = function () {
var nArgs = [this.currentSelection];
if(Array.isArray(arguments[0])) {
arguments[0].forEach(function(e){
nArgs.push(e);
});
} else {
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
}
this.currentSelection = this.coordinates.delName.apply(this.coordinates, nArgs)
return this;
};
this.resNameDel = function () {
var nArgs = [this.currentSelection];
if(Array.isArray(arguments[0])) {
arguments[0].forEach(function(e){
nArgs.push(e);
});
} else {
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
}
this.currentSelection = this.coordinates.delResName.apply(this.coordinates, nArgs)
return this;
};
this.resSeqDel = function () {
var nArgs = [this.currentSelection];
if(Array.isArray(arguments[0])) {
arguments[0].forEach(function(e){
nArgs.push(e);
});
} else {
for (var i = 0; i < arguments.length ; i++) {
nArgs.push(arguments[i]);
}
}
this.currentSelection = this.coordinates.delResSeq.apply(this.coordinates, nArgs)
return this;
};
// Clone selection into a new pdb object and set currentSelection to total atomRecord
this.pull = function() {
var pdbObject = new PdbObject();
pdbObject.coordinates = coordinates.clone(this.currentSelection);
pdbObject.model().name('*');
return pdbObject;
}
this.pdbnum = function () {
if (this.currentSelection.length === 0) {
console.log("Empty atom selection !");
return null;
}
var numSequence = [];
var warden = null;
this.currentSelection.forEach(function(e, i, array){
if (!warden) {
warden = e.pdbNum();
numSequence.push(warden);
return;
}
if (warden === e.pdbNum())
return;
numSequence.push(e.pdbNum());
warden = e.pdbNum();
});
return numSequence;
}
this.sequence = function () {
if (this.currentSelection.length === 0) {
console.log("Empty atom selection !");
return null;
}
var string = '';
var warden = null;
this.currentSelection.forEach(function(e, i, array){
if (!warden)
warden = e.pdbNum();
else if (warden === e.pdbNum())
return;
string += e.oneLetter();
warden = e.pdbNum();
});
return string;
}
// Display current Selection optional number of field (columns)
this.dump = function (nField) {
if (this.currentSelection.length === 0) {
console.log("Empty atom selection !");
return null;
}
var string = '';
if (!nField) {
this.currentSelection.forEach(function(e, i, array){
string += e.stringify();
});
} else {
var n = parseInt(nField);
if (n < 0) {
throw "irregular field number \"" + n + "\"";
}
this.currentSelection.forEach(function(e, i, array){
string += e.stringify().substring(0,n);
if( !string.endsWith("\n") )
string += '\n';
});
}
return string;
}
// create a stream with the PdbObject into a JSON or not
this.stream = function (b_jsonFormat = false, name = 'pdb') {
var s = new stream.Readable();
if (b_jsonFormat) {
s.push('{ "' + name + '" : "');
s.push(this.model(1).dump().replace(/\n/g, '\\n').replace(/\r/g, '\\r'));
s.push('"}');
} else {
s.push(this.model(1).dump());
}
s.push(null);
return s;
}
this.asArray = function () {
if (this.currentSelection.length === 0) {
console.log("Empty atom selection !");
return null;
}
var array = [coordinates.atomFields];
this.currentSelection.forEach(function(e) {
array.push(e.asArray());
});
return array;
}
this.asFasta = function () {
if (this.currentSelection.length === 0) {
console.log("Empty atom selection !");
return null;
}
var array = [coordinates.atomFields];
this.currentSelection.forEach(function(e) {
array.push(e.asArray());
});
return array;
}
}
// Parsing Routines
var setBuffer = function(line, buffers) {
var m = line.match(re);
if (!m){
console.log("unknown tag " + line);
return null;
}
var tBuf = null;
for (var k in buffers) {
if(buffers[k].tags.indexOf(m[0]) === -1)
continue;
return buffers[k].data;
}
return null;
};
var parse = function(opt) {
if (!opt) throw "No input specified";
//console.dir(opt);
var emitter = new events.EventEmitter();
var stream;
if ('file' in opt)
stream = byline(fs.createReadStream(opt.file, { encoding: 'utf8' }));
else if('rStream' in opt)
stream = byline(opt.rStream);
else
throw "No input specified";
var nBuffer = parseBuffer();
stream.on('readable', function() {
while (null !== (line = stream.read())) {
if(typeof(line) !== 'string' ) line = line.toString();
// console.log(line);
var buffer = setBuffer(line, nBuffer);
if (!buffer) {
//console.log("unknown tag at " + line);
continue;
}
buffer.push(line);
}
})
.on('end',function(){
var pdbObject = new PdbObject();
pdbObject.coordinates = coordinates.parse(pdbObject.altLocPreserve,
nBuffer.coordinates.data);
emitter.emit('end', pdbObject);
});
return emitter;
};
module.exports = {
parse : parse,
fWrite : function (pdbObj, path) {
var emitter = new events.EventEmitter();
fs.writeFile(path, pdbObj.dump(), function(err) {
if(err) {
return console.log(err);
}
emitter.emit('saved')
});
return emitter;
}
}
/* Test -- Case */
process.argv.forEach(function (val, index, array){
if (val === '-f')
var pdbObj = parse({'file' : array[index + 1] });
});