我收到了以下从 Excel 电子表格中提取的 CSV 文件。只是为了提供一些可能有帮助的背景信息,它讨论了 AGI 编号(将其视为蛋白质标识符),这些蛋白质标识符的未修饰肽序列,然后是对未修饰序列进行修改的修饰肽序列,索引/索引这些修饰,然后是重复肽的组合光谱计数。文本文件名为 MASP.GlycoModReader.txt,信息格式如下:
AGI,UnMd Peptide (M) = x,Mod Peptide (oM) = Ox,Index/Indeces of Modification,counts,Combined
Spectral count for repeated Peptides
AT1G56070.1,NMSVIAHVDHGKSTLTDSLVAAAGIIAQEVAGDVR,NoMSVIAHVDHGKSTLTDSLVAAAGIIAQEVAGDVR,2,17
AT1G56070.1,LYMEARPMEEGLAEAIDDGR,LYoMEARPoMEEGLAEAIDDGR,"3, 9",1
AT1G56070.1,EAMTPLSEFEDKL,EAoMTPLSEFEDKL,3,7
AT1G56070.1,LYMEARPMEEGLAEAIDDGR,LYoMEARPoMEEGLAEAIDDGR,"3, 9",2
AT1G56070.1,EGPLAEENMR,EGPLAEENoMR,9,2
AT1G56070.1,DLQDDFMGGAEIIK,DLQDDFoMGGAEIIK,7,1
以上解压后需要得到的输出文件格式如下:
AT1G56070.1,{"peptides": [{"sequence": "NMSVIAHVDHGKSTLTDSLVAAAGIIAQEVAGDVR", "mod_sequence":
"NoMSVIAHVDHGKSTLTDSLVAAAGIIAQEVAGDVR" , "mod_indeces": 2, "spectral_count": 17}, {"sequence":
"LYMEARPMEEGLAEAIDDGR" , "mod_sequence": "LYoMEARPoMEEGLAEAIDDGR", "mod_indeces": [3, 9],
"spectral_count": 3}, {"sequence": "EAMTPLSEFEDKL" , "mod_sequence": "EAoMTPLSEFEDKL",
"mod_indeces": [3,9], "spectral_count": 7}, {"sequence": "EGPLAEENMR", "mod_sequence":
"EGPLAEENoMR", "mod_indeces": 9, "spectral_count": 2}, {"sequence": "DLQDDFMGGAEIIK",
"mod_sequence": "DLQDDFoMGGAEIIK", "mod_indeces": [7], "spectral_count": 1}]}
我在下面提供了我的解决方案:如果有人用另一种语言有更好的解决方案,或者可以分析我的解决方案并让我知道是否有更有效的方法来解决这个问题,请在下面发表评论。谢谢你。
#!/usr/bin/env node
var fs = require('fs');
var csv = require('csv');
var data ="proteins.csv";
/* Uses csv nodejs module to parse the proteins.csv file.
* Parses the csv file row by row and updates the peptide_arr.
* For new entries creates a peptide object, for similar entries it updates the
* counts in the peptide object with the same AGI#.
* Uses a peptide object to store protein ID AGI#, and the associated data.
* Writes all formatted peptide objects to a txt file - output.txt.
*/
// Tracks current row
var x = 0;
// An array of peptide objects stores the information from the csv file
var peptide_arr = [];
// csv module reads row by row from data
csv()
.from(data)
.to('debug.csv')
.transform(function(row, index) {
// For the first entry push a new peptide object with the AGI# (row[0])
if(x == 0) {
// cur is the current peptide read into row by csv module
Peptide cur = new Peptide( row[0] );
// Add the assoicated data from row (1-5) to cur
cur.data.peptides.push({
"sequence" : row[1];
"mod_sequence" : row[2];
if(row[5]){
"mod_indeces" : "[" + row[3] + ", " + row[4] + "]";
"spectral_count" : row[5];
} else {
"mod_indeces" : row[3];
"spectral_count" : row[4];
}
});
// Add the current peptide to the array
peptide_arr.push(cur);
}
// Move to the next row
x++;
});
// Loop through peptide_arr and append output with each peptide's AGI# and its data
String output = "";
for(var peptide in peptide_arr)
{
output = output + peptide.toString()
}
// Write the output to output.txt
fs.writeFile("output.txt", output);
/* Peptide Object :
* - id:AGI#
* - data: JSON Array associated
*/
function Peptide(id) // this is the actual function that does the ID retrieving and data
// storage
{
this.id = id;
this.data = {
peptides: []
};
}
/* Peptide methods :
* - toJson : Returns the properly formatted string
*/
Peptide.prototype = {
toString: function(){
return this.id + "," + JSON.stringify(this.data, null, " ") + "/n"
}
};
编辑说明:似乎当我运行我发布的这个解决方案时,我遇到了内存泄漏错误;它无限运行,但不会产生任何实质性的可读输出。如果有人愿意协助评估为什么会发生这种情况,那就太好了。