[R] Problems w/ creating object

Spencer Brackett @pbr@ckett20 @end|ng |rom @@|ntjo@ephh@@com
Mon Apr 22 20:49:26 CEST 2019


Hello R users,

I am trying to create an object out of some data a colleague sent my way,
so to duplicate the following code...

library(data.table)
anno = as.data.frame(fread(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/450K/mapper.txt", sep ="\t",
header = T))
meth = read.table(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/27K/GBM.txt", sep  ="\t",
header = T, row.names = 1)
meth = as.matrix(meth)
""" the loop just formats the methylation column names to match format"""
colnames(meth) = sapply(colnames(meth), function(i){
  c1 = strsplit(i,split = '.', fixed = T)[[1]]
  c1[4] = paste(strsplit(c1[4],split = "",fixed = T)[[1]][1:2],collapse =
"")
  paste(c1,collapse = ".")
})
exp = read.table(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/RNAseq/GBM.txt", sep = "\t",
header = T, row.names = 1)
exp = as.matrix(exp)
c = intersect(colnames(exp),colnames(meth))
exp = exp[,c]
meth = meth[,c]
m = apply(meth, 1, function(i){
  log2(i/(1-i))
})
m = t(as.matrix(m))
an = anno[anno$probe %in% rownames(m),]
an = an[an$gene %in% rownames(exp),]
an = an[an$location %in% c("TSS200","TSS1500"),]

p = apply(an,1,function(i){
  tryCatch(summary(lm(exp[as.character(i[2]),] ~
m[as.character(i[1]),]))$coefficient[2,4], error= function(e)NA)
})
t = apply(an,1,function(i){
  tryCatch(summary(lm(exp[as.character(i[2]),] ~
m[as.character(i[1]),]))$coefficient[2,3], error= function(e)NA)
})
an1 =cbind(an,p)
an1 = cbind(an1,t)
an1$q = p.adjust(as.numeric(an1$p))
summary(lm(exp["MAOB",] ~ m["cg00121904",]$coefficient[2,c(3:4)]
###############################################

m2 = m
ll = list()
for(i in colnames(m2)){
  str = strsplit(i, split = ".", fixed = T)[[1]]
  if(str[4] == "11"){

  }else{
    ll = c(ll,i)
  }
}
ll = unlist(ll)
m2 = m2[,ll]
colnames(m2) = sapply(colnames(m2), function(i){
  str = strsplit(i,split = ".", fixed = T)[[1]]
  p = paste(str[c(1:3)], collapse = "-")
})


clin = as.data.frame(fread(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/survival/FireHose/GBM/GBM.clin.merged.txt",
sep = "\t", header = F))
clin = t(clin)
colnames(clin) = clin[1,]
rownames(clin) = toupper(clin[,"patient.bcr_patient_barcode"])
clin = clin[2:length(clin[,1]),]
#"patient.stage_event.pathologic_stage"
clin1 =
clin[,c("patient.age_at_initial_pathologic_diagnosis","patient.days_to_death","patient.days_to_last_followup","patient.vital_status")]
clin1 = cbind(clin1,rep("bla",length(clin1[,1])))
clin2 = as.matrix(clin1)
colnames(clin2)[length(colnames(clin2))] = "time"

for(i in rownames(clin2)){

  if(clin2[i,"patient.vital_status"] %in% c("alive")){
    clin2[i,"patient.vital_status"] =0
  }else if(clin2[i,"patient.vital_status"] %in% c("dead")){
    clin2[i,"patient.vital_status"] =1
  }else{
    clin2[i,"patient.vital_status"] = "NA"
  }

  if(is.na(clin2[i,"patient.days_to_last_followup"])){
    clin2[i,"time"] = clin2[i,"patient.days_to_death"]
  }else{
    clin2[i,"time"] = clin2[i,"patient.days_to_last_followup"]
  }
}

clin2 = clin2[!is.na(clin2[,"time"]),]
clin2 = clin2[!is.na(clin2[,"patient.vital_status"]),]

library(survival)
p = intersect(colnames(m2), rownames(clin2))
surv =
Surv(as.numeric(clin2[p,"time"]),as.numeric(clin2[p,"patient.vital_status"]))

an_m = anno[anno$probe %in% rownames(m2),]
an_m = an[an$gene %in% rownames(exp),]

sur_z = apply(an_m, 1, function(i){
  tryCatch(summary(coxph(surv ~
as.numeric(m2[as.character(i[1]),p])+as.numeric(clin2[p,"patient.age_at_initial_pathologic_diagnosis"])))$coefficients[1,c("z")],
error = function(e) NA)
})

sur_p = apply(an_m, 1, function(i){
  tryCatch(summary(coxph(surv ~
as.numeric(m2[as.character(i[1]),p])+as.numeric(clin2[p,"patient.age_at_initial_pathologic_diagnosis"])))$coefficients[1,c("Pr(>|z|)")],
error = function(e) NA)
})

qsur = p.adjust(as.numeric(sur_p))
sur = cbind(sur_z,sur_p)
sur = cbind(sur,qsur)


The file is a text file
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/450K/mapper.txt", which is
then proceeded by another txt. file
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/27K/GBM.txt, which I wish to
load subsequently. However, when i tried copying the procedure above I
received the following error message..

library(data.table)
> anno = as.data.frame(fread(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/450K/mapper.txt", sep ="\t",
header = T))
Error in fread(file =
"/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/450K/mapper.txt",  :
  File '/rsrch1/bcb/kchen_group/v_mohanty/data/TCGA/450K/mapper.txt' does
not exist or is non-readable.
getwd()=='C:/Users/Spencer/Documents'

The file does exit so in what context is it 'unreadable' and how might I
solve this situation?

Best,

Spencer

	[[alternative HTML version deleted]]



More information about the R-help mailing list