我有一个列表:
ATOM 1 N LYS A 1 37.995 20.521 9.231 1.00 31.96
ATOM 2 CA LYS A 1 38.067 19.342 10.141 1.00 30.87
原子3 C LYS A 1 36.817 18.477 10.027 1.00 28.29
...
原子10 N GLU A 2 36.835 17.337 10.707 1.00 24.26
ATOM 11 CA GLU A 2 35.709 16.412 10.692 1.00 19.83
ATOM 12 C GLU A 2 35.496 15.870 12.098 1.00 18.02
...
我逐行阅读这个列表,然后解析它。解析后我希望结果如下:
[ { resName: 'LYS',
resSeqNum: 1,
atoms: { atomName: ' N ', x: 37.995, y: 20.521, z: 9.231 }, { atomName: ' CA ', x: 38.067, y: 19.342, z: 10.141 }, { atomName: ' C ', x: 36.817, y: 18.477, z: 10.027 }, ... },
[ { resName: 'GLU',
resSeqNum: 2,
atoms: { atomName: ' N ', x: 36.835, y: 17.337, z: 10.707 }, { atomName: ' CA ', x: 35.709, y: 16.412, z: 10.692 }, { atomName: ' C ', x: 35.496, y: 15.870, z: 12.098 } } ] ]
我尝试了几种方法,但结果并不像我想要的那样。这是我的代码:
var someData = ["ATOM 1 N LYS A 1 37.995 20.521 9.231 1.00 31.96 N",
"ATOM 2 CA LYS A 1 38.067 19.342 10.141 1.00 30.87 C",
"ATOM 3 C LYS A 1 36.817 18.477 10.027 1.00 28.29 C" ] ;
var protein = [];
var parse;
function parser(line){
parse = {
resName: line.substring(17, 20),
resSeqNum: parseInt(line.substring(22, 26)),
atoms: {
atomName: line.substring(12, 16),
x: parseFloat(line.substring(30, 38)),
y: parseFloat(line.substring(38, 46)),
z: parseFloat(line.substring(46, 54))
}
}
return parse;
}
for(var i=0; i<someData.length; i++){
protein.push(parser(someData[i]));
}
console.log(protein);
我提供了一些测试我的代码的数据。这段代码的结果如下:
[ { resName: 'LYS',
resSeqNum: 1,
atoms: { atomName: ' N ', x: 37.995, y: 20.521, z: 9.231 } },
{ resName: 'LYS',
resSeqNum: 1,
atoms: { atomName: ' CA ', x: 38.067, y: 19.342, z: 10.141 } },
{ resName: 'LYS',
resSeqNum: 1,
atoms: { atomName: ' C ', x: 36.817, y: 18.477, z: 10.027 } } ]
你可以给我任何建议吗?
答案 0 :(得分:0)
您需要为原子创建array
。现在,每一行都有自己的对象。 &#34; res name&#34;没有分组。以下是分两步完成的方法:
var someData = ["ATOM 1 N LYS A 1 37.995 20.521 9.231 1.00 31.96 N",
"ATOM 2 CA LYS A 1 38.067 19.342 10.141 1.00 30.87 C",
"ATOM 3 C LYS A 1 36.817 18.477 10.027 1.00 28.29 C",
"ATOM 10 N GLU A 2 36.835 17.337 10.707 1.00 24.26",
"ATOM 11 CA GLU A 2 35.709 16.412 10.692 1.00 19.83",
"ATOM 12 C GLU A 2 35.496 15.870 12.098 1.00 18.02"] ;
// Parsing logic
var getRes = line => line.substring(17, 20);
var getResSeqNum = line => parseInt(line.substring(22, 26));
var getAtomName = line => line.substring(12, 16);
var getX = line => parseFloat(line.substring(30, 38));
var getY = line => parseFloat(line.substring(38, 46));
var getZ = line => parseFloat(line.substring(46, 54));
var parsedBySeq = someData.reduce(function(result, line) {
var resKey = getRes(line);
var resSeq = getResSeqNum(line);
var newAtom = {
atomName: getAtomName(line),
x: getX(line),
y: getY(line),
z: getZ(line),
};
if (!result[resKey]) { // First time we've seen a res with name 'resKey'
result[resKey] = {
resName: resKey,
resSeqNum: resSeq,
atoms: [newAtom]
};
} else { // Previously encountered atoms had the same name, add this one to it
result[resKey].atoms.push(newAtom);
}
return result;
}, {});
var parsedArr = Object.keys(parsedBySeq).map(k => parsedBySeq[k]);
console.log(parsedArr);
&#13;
.as-console-wrapper { min-height: 100%; }
&#13;
编辑:根据要求,在旧学校&#34; for loop:
var someData = ["ATOM 1 N LYS A 1 37.995 20.521 9.231 1.00 31.96 N",
"ATOM 2 CA LYS A 1 38.067 19.342 10.141 1.00 30.87 C",
"ATOM 3 C LYS A 1 36.817 18.477 10.027 1.00 28.29 C",
"ATOM 10 N GLU A 2 36.835 17.337 10.707 1.00 24.26",
"ATOM 11 CA GLU A 2 35.709 16.412 10.692 1.00 19.83",
"ATOM 12 C GLU A 2 35.496 15.870 12.098 1.00 18.02"] ;
// Parsing logic
var getRes = line => line.substring(17, 20);
var getResSeqNum = line => parseInt(line.substring(22, 26));
var getAtomName = line => line.substring(12, 16);
var getX = line => parseFloat(line.substring(30, 38));
var getY = line => parseFloat(line.substring(38, 46));
var getZ = line => parseFloat(line.substring(46, 54));
var parsedBySeq = {}, // This map is used to store unique keys
i, line, l, resKey, resSeq, newAtom;
for (i = 0, l = someData.length; i < l; i+= 1) {
line = someData[i];
resKey = getRes(line);
resSeq = getResSeqNum(line);
newAtom = {
atomName: getAtomName(line),
x: getX(line),
y: getY(line),
z: getZ(line),
};
if (!parsedBySeq[resKey]) { // First time we've seen a res with name 'resKey'
parsedBySeq[resKey] = {
resName: resKey,
resSeqNum: resSeq,
atoms: [newAtom]
};
} else { // Previously encountered atoms had the same name, add this one to it
parsedBySeq[resKey].atoms.push(newAtom);
}
}
var parsedArr = Object.keys(parsedBySeq).map(k => parsedBySeq[k]);
console.log(parsedArr);
&#13;
.as-console-wrapper { min-height: 100%; }
&#13;