Diff of /data/.Rhistory [000000] .. [a23a6e]

Switch to unified view

a b/data/.Rhistory
1
library(lmQCM)
2
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
3
options(stringsAsFactors = F)
4
dataset = {} # KIRP
5
########################################################################
6
#                      Clinical
7
########################################################################
8
dataset[['clinical']] = read.table('KIRP/clinical/nationwidechildrens.org_clinical_patient_kirp.txt', header = T, sep = '\t')
9
dataset[['clinical']] = dataset[['clinical']][3:dim(dataset[['clinical']])[1],]
10
colnames(dataset[['clinical']])
11
head(dataset[['clinical']])
12
View(dataset)
13
colnames(dataset[['clinical']])
14
dataset[['clinical']]$age_at_diagnosis
15
dataset[['clinical']]$age_at_diagnosis = strtoi(dataset[['clinical']]$age_at_diagnosis)
16
dataset[['clinical']]$last_contact_days_to = strtoi(dataset[['clinical']]$last_contact_days_to)
17
dataset[['clinical']]$death_days_to = strtoi(dataset[['clinical']]$death_days_to)
18
dataset[['clinical']]$tobacco_smoking_history_indicator = strtoi(dataset[['clinical']]$tobacco_smoking_history_indicator)
19
dataset[['clinical']]$tobacco_smoking_history_indicator
20
dataset[['clinical']]$age_at_diagnosis = strtoi(dataset[['clinical']]$age_at_diagnosis)
21
dataset[['clinical']]$last_contact_days_to = strtoi(dataset[['clinical']]$last_contact_days_to)
22
dataset[['clinical']]$death_days_to = strtoi(dataset[['clinical']]$death_days_to)
23
print('use valid \'death_days_to\' to replace \'last_contact_days_to\'')
24
# days_to_last_followup and days_to_death
25
dataset[['clinical']]$survival_days = dataset[['clinical']]$last_contact_days_to
26
dataset[['clinical']]$survival_days[!is.na(dataset[['clinical']]$death_days_to)] = dataset[['clinical']]$death_days_to[!is.na(dataset[['clinical']]$death_days_to)]
27
# extract useful columns
28
dataset[['clinical']] = dataset[['clinical']][, c('bcr_patient_barcode', 'gender','age_at_diagnosis', 'vital_status','survival_days')]
29
dataset[['clinical']] = dataset[['clinical']][complete.cases(dataset[['clinical']]),]
30
print(paste0('[clinical] ', dim(dataset[['clinical']])[1], ' complete rows found in clinical data.'))
31
dataset[['clinical']]
32
dataset[['TMB']] = read.table("KIRP/TMB/KIRP_TMB.csv", header = T, row.names = 1, sep = ",")
33
print(paste0('[TMB] ', dim(dataset[['TMB']])[1], ' complete rows found in clinical data.'))
34
########################################################################
35
#                      CNB
36
########################################################################
37
# Xiaohui Zhan:
38
# https://www.nature.com/articles/ng.3725
39
# a) For high quality CNVs ,the length of segmental region >20kb
40
#
41
# b) The number of probes spanning a CNV (a segmental region) to be
42
#    at least 10 to decrease false positives in calling CNVs.
43
#
44
# c) For a segmental region ,if the segment mean < |0.2|,this segmental
45
#    region should be discard.(Generally ,we using +/-0.2 as threshold
46
#    for a duplication/deletion. Because lots of noise will be introduced
47
#    from the microarray. The thresholds(+/- 0.2) were derived by examining
48
#    the distribution of segment mean values from tumor and normal samples)
49
SNP = read.table("LUAD/CNB/broad.mit.edu_PANCAN_Genome_Wide_SNP_6_whitelisted.seg", sep = "\t", header = T)
50
SNP$LENGTH = SNP$End - SNP$Start
51
SNP.filtered = SNP[(SNP$LENGTH >= 20000) &
52
(SNP$Num_Probes >= 10) &
53
(abs(SNP$Segment_Mean) >= 0.2),]
54
# SNP = read.table("data/UCSC Xena/broad.mit.edu_PANCAN_Genome_Wide_SNP_6_whitelisted.xena", sep = "\t", header = T)
55
SNP.filtered.sum = aggregate(SNP.filtered$LENGTH, by=list(Category=SNP.filtered$Sample), FUN=sum)
56
# 1Mb = 1,000 kb = 1,000,000 pb
57
SNP.filtered.sum$LENGTH_KB = SNP.filtered.sum$x/1000
58
# remove normal group 10A, 11A, ...
59
SNP.filtered.sum = SNP.filtered.sum[as.numeric(substr(SNP.filtered.sum$Category, 14, 15)) == 1, ] # only based on primary cancer (01A)
60
barcode = substr(SNP.filtered.sum$Category, 1, 12)
61
length(unique(barcode)) == length(barcode)
62
# samebarcode = names(table(barcode)[table(barcode)>=2])
63
# same = unlist(lapply(samebarcode, function(x) which(grepl(x, SNP.filtered.sum$Category))))
64
# SNP.filtered.sum$Category[same]
65
#### Get patients information
66
pinfo = read.table("LUAD/CNB/data/UCSC Xena/TCGA_phenotype_denseDataOnlyDownload.tsv", sep = "\t", header = T)
67
pinfo$barcode = substr(pinfo$sample, 1, 12)
68
SNP.filtered.sum$CANCER = pinfo$X_primary_disease[match(barcode, pinfo$barcode)]
69
study.abbr = read.table("KIRP/CNB/data/TCGA study abbreviations.tsv", sep = "\t", header = T)
70
study.abbr$Study.Name = tolower(study.abbr$Study.Name)
71
SNP.f2 = SNP.filtered.sum[SNP.filtered.sum$CANCER %in% study.abbr$Study.Name,]
72
SNP.f2$CANCER_ABBR = study.abbr$Study.Abbreviation[match(SNP.f2$CANCER, study.abbr$Study.Name)]
73
SNP.f3 = SNP.f2[SNP.f2$CANCER_ABBR %in% c("BLCA", "BRCA","CESC","HNSC","KIRC", "KIRP", "LIHC", "LUAD",
74
"LUSC", "OV", "PAAD","STAD"),]
75
table(SNP.f3$CANCER_ABBR)
76
dataset[['CNB']] = SNP.f3[SNP.f3$CANCER_ABBR == "KIRP",]
77
dataset[['CNB']] = data.frame(cbind(dataset[['CNB']]$Category, dataset[['CNB']]$LENGTH_KB))
78
colnames(dataset[['CNB']]) = c("barcode", "LENGTH_KB")
79
dataset[['CNB']]$barcode = unlist(lapply(dataset[['CNB']]$barcode, function(x) substr(x, 1, 12) ))
80
print(paste0('[CNB] ', dim(dataset[['CNB']])[1], ' complete rows found in clinical data.'))