This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R programming: search by making key terms

Hi guys,

I have a dataframe(mydf) like this below:

mydf

   chr     start       end REF ALT          TYPE
1   chr11   163082   163082   T   C           snp
2   chr21   999282   999282   G   A           snp
3   chr16  7447514  7447514   T   C           snp
4   chr10 10310574 10310574   C   A           snp
5    chrX 13961560 13961560   C   G           snp
6    chr3  7590814  7590814   C   T           snp
7    chr3  7590811  7590811   G   T           snp

I have made a key from this like this:

key.genom <- paste(paste("chr",mydf[,"start"],sep=""),mydf[,"end"],mydf[,"REF"],mydf[,"ALT"],sep=":")

which gives me keys like these:

"chr163082:163082:T:C"     "chr999282:999282:G:A"

now I want to search the key.genom items in the table (lookupdf) also matching the nucleotides in the columns and get the values for those nucleotides. I want to match the nucleotides in the column names and get the result as shown in the result below. Could you guys please help me . Thank you.

lookupdf

 chr     start       end         A     C      G     T      N  =  - 
  [1,] "chr11" "163082"   "163082"   NA    "1"    NA    "17"   NA NA NA
  [2,] "chr21" "999282"   "999282"   "3"   NA     "24"  NA     NA NA NA
  [3,] "chr16" "7447514"  "7447514"  NA    "5"    NA    "91"   NA NA NA
  [4,] "chr10" "1010574" "1010574" "2"   "22"   NA    NA     NA NA NA

result:

chr11:163082:163082 T(17) C(1)
chr21:999282:999282 G(24) A(3)
r

1 answer

The simplest way to do will be:

result =merge(lookupdf, mydf, by=c("chr", "start","end"))
for(i in 1:nrow(result)){
      final = rbind(final, paste(paste(result$chr[i], result$start[i], result$end[i],sep=":")," ", result$REF[i],"(",result[,(as.character(result$REF[i]))][i],")"," ", result$ALT[i], "(",result[,(as.character(result$ALT[i]))][i],")",sep=""))
}

Thank you so much, it works!

Log in to answer this question.