I want to pull out the proteins in seq.df (a single column df) that match the indices in the matching map.list (a list of lists).
Example data:
seq.df<- rbind.data.frame("MTHISPAVYGLWAIMSVLLAAFCAY",
"MERSSAIVFPNVGTSVLSATIHLVGVTVLAHLISRRTALRGTST",
"MLFEPFWCLLDLLRWSLDTHYIPAKRPLNGGGRSSNFD")
map.list<- list(a<- list(2,3,4,5,6,7),
b<- list(13,14,30,31,32),
c<- list(5,6,10,11))
Desired output:
THISPA
GTAHL
PFLD
If I run a nested apply over just the first sublist of map.list, I get what I want for the first protein:
prot.list<- apply(seq.df, 1, function (x) lapply(map.list[[1]], function (y) substring(x, y, y)))
returns the expected result for the first sequence (THISPA,)
But I'm unsure how to get this function to iterate over all the sublists in map.list. I tried to wrap this into a for loop, but it does not give me the expected result:
for (i in seq_along(map.list)){
each.map.list<- map.list[[i]]
prot.list<- apply(seq.df, 1, function (x) lapply(each.map.list, function (y) substring(x, y, y)))
}
Output:
SPGL
SAPN
PFLD
I'd much rather add another lapply statement, but I'm not sure how to specify each list in map.list
#this does not work, but something like:
prot.list<- apply(seq.df, 1, function (x) lapply(map.list, function (y) lapply([[y]], function (z) substring(x, z, z)))