This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Exacting all the headers in a fasta file using R

Hi, I want to extract all the headers from my fasta file. Here is my example:

>Eukaryota;Alveolata;Dinoflagellata;Dinophyceae;Peridiniales;Kryptoperidiniaceae;Unruhdinium;Unruhdinium_kevei;
ATGCTTGTCTCAAAGATTAAGCCA......

All I want is extracting the line starting with the ">", and separate each name (which before the ";") into different columns, and put them into a CSV file.

I know really know how to do, and I really need some help!

r sequence

4 answers

grep "^>" <filename> | sed 's/;/,/g' > <newfilename>

Command line answer. Grep will search for ">" and sed will substitute ";" with a tab creating new columns. the last ">" will output your results to the new file name you indicated.

OP wants comma-separated output. You may want to amend your solution accordingly.

Yikes, amended to be CSV

It will leave the initial > in. If that is not wanted then it can be removed by an extension of solution above.

$ grep "^>" <filename> | sed -e 's/>//' -e 's/;/,/g' > <new_file>

Not a solution in R but you can simply do

$ grep "^>" your_file.fa | awk -F ">|;" '{for(i=2;i<NF;i++){printf "%s,", $i}; printf "\n"}'
Eukaryota,Alveolata,Dinoflagellata,Dinophyceae,Peridiniales,Kryptoperidiniaceae,Unruhdinium,Unruhdinium_kevei,

sed

 sed '/^>/s/;/\t/g;/^[^>]/d;s/^>//' in.fasta

OP wants comma separated output so

$ sed '/^>/s/;/,/g;/^[^>]/d;s/^>//' < in.fasta > out.header

Good bash solutions, that could be wrapped inside R for example as below:

library(data.table)
x <- fread("grep ... myFilename.fasta")

Or do all within R:

#example input fasta
x <- read.table(text = "
>seq0;x1;y1
FQTWEEFSRAAEKLYLADPMKVRVVLKYRHVDGNLCIKVTDDLVCLVYRTDQAQDVKKIEKF
>seq1;x22
KYRTWEEFTRAAEKLYQADPMKVRVVLKYRHCDGNLCIKVTDDVVCLLYRTDQAQDVKKIEKFHSQLMRLME
LKVTDNKECLKFKTDQAQEAKKMEKLNNIFFTLM
>seq2
EEYQTWEEFARAAEKLYLTDPMKVRVVLKYRHCDGNLCMKVTDDAVCLQYKTDQAQDVKKVEKLHGK
", sep = ";", fill = TRUE, header = FALSE)

# keep only header rows
x <- x[ grep("^>", x$V1),  ]

# remove ">"
x$V1 <- gsub(">", "", x$V1, fixed = TRUE)

# output
write.csv(x, "myFile.csv")

myFile.csv

"","V1","V2","V3"
"1","seq0","x1","y1"
"3","seq1","x22",""
"6","seq2","",""

Log in to answer this question.