1

I'm trying to rerun code that I've successfully run previously but I keep getting the error message

Error in file(con, "r") : invalid 'description' argument

The loop I'm using is the following

# Null All Transcripts

setwd("/mnt/data/BMOHAMED/Total_RNAseq/salmon/MDM2/SCU_male/SCU_all/")

path = "/mnt/data/BMOHAMED/Total_RNAseq/salmon/MDM2/SCU_male/SCU/SCU_all/"

out.file <- ""

file.names <- dir(path, pattern ="Null_CDS_allTx.fasta")

table <- NULL

# calculate the codon frequency 

for(i in 1:length(file.names)){
  print(i)  
  file <- read.fasta(file.names[i],as.string=FALSE)
  name <- file.names[i]
  name <- gsub(".fasta","",name)
  print(name)
  UCO <- lapply(file, function(x) uco(x, frame = 0, index = "freq", as.data.frame = FALSE, NA.rscu = NA))
  UCO1 <- do.call(rbind,UCO)
  UCO1 <- as.data.frame(UCO1)
  colnames(UCO1) <- aa$AA
  head(UCO1) 
}

Any ideas why this has gone wrong? I've tried looking at online answers but none of them suit what I'm trying to solve. Thanks in advance!

Danby
  • 118
  • 7
  • 1
    Where exactly do you get that error? We cannot reproduce it since we don't have your files, so it could be any of those. BTW: you store the data into `UC01` and then prompt over-write it on the next pass through your loop. This is likely a candidate for replacing your `for`-loop attempt with an `lapply` solution; for that, see https://stackoverflow.com/a/24376207/3358227. – r2evans Sep 22 '21 at 17:20
  • 1
    I ran traceback() and got 3: file(con, "r") 2: readLines(file) 1: read.fasta(file.names[i], as.string = FALSE) – Danby Sep 22 '21 at 17:35
  • Okay, what does `file.names[i]` return? – r2evans Sep 22 '21 at 17:37
  • file.name[i] returns: [1] NA – Danby Sep 22 '21 at 17:50
  • 2
    That's your problem. My guess is that `dir(...)` is returning *nothing*, and `[1]` is converting it to `NA`. Do you mean `dir(..., recursive=TRUE)`? – r2evans Sep 22 '21 at 17:52
  • 1
    Ahhh ... another thing: the use of `1:length(.)` is deceptively *bad practice*. The intuitive thing is that it will return the indices along the vector, so if the vector is length 3 then the `for` loop will run with `1`, `2`, and then `3`. Unfortunately, `1:length(.)` when the vector is *empty* is effectively `1:0` *which is not empty*, it is `c(1, 0)`. Instead, use `for (i in seq_along(file.names))`, and `seq_along(empty_vector)` does not iterate at all (which is the intent). You still need to solve why `dir(.)` is not finding anything, but you should fix the `for` part of your code. – r2evans Sep 22 '21 at 17:55
  • 1
    Found the error ... it was in the path i defined before the loop, should be the same as the working directory i set before it ... Thanks a million! – Danby Sep 22 '21 at 18:02

0 Answers0