OP wants comma-separated output. You may want to amend your solution accordingly.
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!
• 4,773 views
•
link
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.
• 0 views
•
link
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,
• 0 views
•
link
sed
sed '/^>/s/;/\t/g;/^[^>]/d;s/^>//' in.fasta
• 0 views
•
link
OP wants comma separated output so
$ sed '/^>/s/;/,/g;/^[^>]/d;s/^>//' < in.fasta > out.header
• 0 views
•
link
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","",""
• 0 views
•
link
Log in to answer this question.