I've already tried that. Error: the 'cbind' method for "DNAbin" accepts only matrices
Hello, biostars! I have two fasta files for two different genes and want to create one data matrix. Is there any function in R for that? F.ex. if I have 2 DNAbin objects for that genes. Id numbers are identical in both files. I have the first file:
>sp1
aacc
>sp2
ggtt
the second file:
>sp1
ggaa
>sp2
ttgg
I want:
>sp1
aaccggaa
>sp2
ggttttgg
Python is also OK, but i'm interested in R.
2 answers
Just cbind(A,B) to merge the sequences for DNAbin A and DNAbin B:
A.fa:
>sp1
aacc
>sp2
ggtt
B.fa:
>sp1
ggaa
>sp2
ttgg
In R using DNAbin (as you requested):
library(ape)
A <- read.dna("A.fa", format="fasta")
B <- read.dna("B.fa", format="fasta")
C <- cbind(A,B)
write.dna(C, "C.fa", format="fasta")
C.fa:
>sp1
aaccggaa
>sp2
ggttttgg
See help(DNAbin) for more details about options for cbind(), particularly fill.with.gaps and check.names.
How did you read in the sequences?
read.dna("B.fa", format="fasta") - fail read.FASTA("B.fasta") - fail
Worked for me.
You can do this with an awk
paste A.fa B.fa | awk '{if (NR%2==0) {print $1 $2} else {print $1}}'
Thank you! It works. I have absolutely no experience with awk, so i have one question: the order of IDs in A.fa have to be the same, as in B.fa? Or concatenation goes by comparing IDs in two files?
paste -d '\0' File_A File_B | sed 's/>[A-Z]*//' > File_C.fa will also do the same.
Log in to answer this question.
Could you comment on the rationale behind what you're trying to do?
In few words: concatenated sequence matrix -> alignment -> phylogenetic tree
Is it some kind of homework question. I answered the same question 4-5 days back. See ehre: C: Combining dna sequences files into one
No, it's for my lab work. Your answer is also helpful, thanks.