R programming: search by making key terms
1
0
Entering edit mode
9.5 years ago
MAPK ★ 2.1k

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.6k views
ADD COMMENT
2
Entering edit mode
9.5 years ago
Sam ★ 4.8k

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=""))
}
ADD COMMENT
0
Entering edit mode

Thank you so much, it works!

ADD REPLY

Login before adding your answer.

Traffic: 1253 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6