我上传的文件中有多个DNA序列ID,后面跟着它们的DNA序列。我希望能够解析文件并将信息存储在对象中:
function scanForSequences(event) {
//Get the file from HTML input tag
var file = event.target.files[0];
var output = document.getElementById('table');
var tr = document.createElement('tr');
var td = document.createElement('td');
if(file) {
var sequenceArray = [];
var objArray = [];
var obj = {};
//Create a new file reader
var reader = new FileReader();
//When the file reader loads
reader.onload = function(evt) {
//Add the contents of file to variable contents
var contentsByLine = evt.target.result.split('\n');
//Alert user the file upload has succeeded
alert('File ' + file.name + ' has been uploaded!');
for(var i = 0; i < contentsByLine.length; i++){
if(contentsByLine[i].charAt(i) == '>'){
obj.id = contentsByLine[i];
}else{
sequenceArray.push(contentsByLine[i]);
obj.sequence = sequenceArray;
obj.lead_trim = 0;
obj.trail_trim = 0;
}
objArray.push({obj});
console.log(objArray);
//console.log(sequenceArray[i].length);
}
}
reader.readAsText(file);
} else {
alert('Failed to upload file!');
}
}
问题是只识别第一个序列ID,其余的信息才会进入我的sequenceArray。任何人都可以帮我解决我的错误吗?我很感激!
这是文本文件:
&GT; 9013e1 ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGC CTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGG AAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCC CTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAG TTTAATTACAGACCTGAA
&GT; 9042f1 ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGC CTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGG AAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCC CTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAG TTTAATTACAGACCTGAA
答案 0 :(得分:0)
我不确定我是否完全理解你在寻找什么,但是这个函数应该将文本解析为数组中的对象。希望你可以修改它以获得你需要的东西。通过一些编辑,您应该可以将其用作reader.onload
函数。
我修正了什么:
function parse(fileContents) {
//Add the contents of file to variable contents
var contentsByLine = fileContents.split('\n'),
objArray = [],
obj;
for(var i = 0; i < contentsByLine.length; i++){
if(contentsByLine[i][0] == '>'){
obj = {
id: contentsByLine[i],
sequence: [],
lead_trim: 0,
trail_trim: 0
};
objArray.push({obj});
}else{
obj.sequence.push(contentsByLine[i]);
}
console.log(objArray);
}
}
parse(">9013e1\nACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC\n>9042f1\nACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC");