Reading an FASTA file

荒凉一梦 提交于 2021-02-05 09:15:47

问题


I want to convert the following line of a file into JSON, I want to save that into an mongoose schema.

>HWI-ST700660_96:2:1101:1455:2154#5@0/1
GAA…..GAATG

Should be:

{“>HWI-ST700660_96:2:1101:1455:2154#5@0/1”: “GAA…..GAATG”}

I have tried several options, one sample below, but no success, any suggestion?

const parser = require("csv-parse/lib/sync");//import parser
const fs = require("fs");//import file reader
const path = require("path");//for join paths


const sourceData = fs.readFileSync(path.join(__dirname, "Reads.txt"), "utf8");//read the file, locally stored

console.log(sourceData);//print out for checking
const documents = parser(sourceData);//parsing, it works for other situations I have tested, in a column like data

console.log(documents);//printing out

This code give me an output as following:

[ [ '>HWI-ST700660_96:2:1101:1455:2154#5@0/1' ],
  [ 'GAATGGAATGAAATGGATAGGAATGGAATGGAATGGAATGGATTGGAATGGATTAGAATGGATTGGAATGGAATGAAATTAATTTGATTGGAATGGAATG' ],...

Similar question: fasta file reading python


回答1:


Because you are using the default config of the parser, it does simply output arrays of arrays in that configuration. If you want to receive objects you will need to give the parser some options (columns) first. Take a look at the doc.

When using the sync parsing mode (like you are using) you can provide options like this:

const documents = parse(sourceData, {columns: true})

columns:true will infer the column names from the first line of the input csv.



来源:https://stackoverflow.com/questions/60545123/reading-an-fasta-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!