Hi, Thanks for your help! I just downloaded the CMV data from TCGA database and mapped genomic regions to gene symbols using the following code:
mart <- useMart(biomart="ensembl", dataset="hsapiens_gene_ensembl")
genes <- getBM(attributes=c("hgnc_symbol","chromosome_name","start_position","end_position"), mart=mart)
genes <- genes[genes[,1]!="" & genes[,2] %in% c(1:22,"X","Y"),]
colnames(genes) <- c("GeneSymbol","Chr","Start","End")
genes_GR <- makeGRangesFromDataFrame(genes,keep.extra.columns = TRUE)
df = read.csv("BRCA-CNV.csv") # CNV data downloaded from TCGA database.
cnv = makeGRangesFromDataFrame(df, keep.extra.columns = TRUE)
hits <- findOverlaps(genes_GR, cnv, type="within")
df_ann <- cbind(df[subjectHits(hits),],genes[queryHits(hits),])
df_ann <- unique(df_ann)
write.table(df_ann, file="BRCA-CNV-withGeneSymbol.txt", quote = F, sep = "\t", row.names = F)
Then I constructed the CNV matrix using the following code:
cnv <- as.matrix(read.table("BRCA-CNV-withGeneSymbol.txt"))
cnv <- cnv[,c(7:9)]
samples.tumor <- samples[which(substr(samples, 14, 16) %in% c("01A", "01B", "06A"))]
tumor <- matrix(0, nrow = length(genes), ncol = length(samples.tumor))
colnames(tumor) <- samples.tumor
rownames(tumor) <- genes
for(i in 1:nrow(tumor)){
for(j in 1:ncol(tumor)){
gene.tmp <- rownames(tumor)[i]
sample.tmp <- colnames(tumor)[j]
cnv.tmp <- cnv[which(cnv[,3] %in% gene.tmp),]
tumor[i,j] <- mean(as.numeric(cnv.tmp[which(cnv.tmp[,2] %in% sample.tmp),1]), rm.na = T)
}
}
At last, I got the CNV matrix. Is there any error in this process? The final data is like this:
gene1 gene2 gene3 gene4 gene5
TCGA-LD-A9QF-01A-32D-A41E-01 -0.3258360 0.2291041 -0.29244881 0.4456383 0.2990137
TCGA-EW-A1OX-01A-11D-A141-01 5.4529917 -0.6737190 -0.17351193 0.2469372 0.4106143
TCGA-A2-A0YT-01A-11D-A107-01 0.5855575 -1.4167641 -0.09580651 -1.9142909 -0.7032367
TCGA-E2-A14N-01A-31D-A134-01 -0.5166007 -0.4208664 0.64834184 0.1254564 0.3653708
TCGA-BH-A18F-01A-11D-A12A-01 -1.4072964 -0.5098043 0.22809823 0.1885511 -0.1663476
