I have a dataframe with putative alignments of thousands of probe sequences. I would like to pull out the adjacent nucleotide from the genome for each alignment. My dataframe includes seq.name
for the target chromosome and I've added an adjacent_pos
variable with the location on the target chromosome that I want. Using read.fasta
from the phylotools package gives me a genome in the format of a dataframe with seq.name
and seq.text
for each chromosome and unplaced scaffold.
Working on a test data set with a single chromosome, I successfully used something like this:
adj <- cbind(alignments$adjacent_pos, alignments$adjacent_pos)
alignments$adjacent_base <- str_sub(genome$seq.text, adj)
Now back to the whole genome, I can't wrap my head around how to direct str_sub
to the correct seq.name
for each seq.position
. I'm sure there's an elegant solution, but I haven't been able to find it!
alignments
andgenome
objects? themerge()
function from the data.table package will surely help you. $\endgroup$