read.cross Error in file(file, "rt"): cannot open the connection

119 views
Skip to first unread message

Lara B

unread,
Nov 19, 2021, 2:25:47 PM11/19/21
to R/qtl discussion

Hi all,

I am having some difficulty with the read.cross() function in r/qtl. The main error I receive is: Error in file(file, "rt"): cannot open the connection. Additionally I sometimes get the error: cannot open file 'D:/Users/larab/Chilling-F2/populations.1': Permission denied. Anybody have this problem before and/or know how to overcome?

I am not sure what I am doing wrong. I’ve tried several different code combinations. I am able to read my files in with read.csv() and read.table() so I am confused about what does not work with read.cross(). I output the genotype from stacks v2.6 populations rqtl F2 format.  

Attempts:

setwd("D:/Users/larab/Chilling-F2")

#setwd("D:/Users/larab/Chilling-F2/populations.1")

 

mydata <- read.cross("csv", "",

                        dir="D:/Users/larab/Chilling-F2/populations.1",

                        genfile="populations.F2.rqtl.csv",

                        phefile="necrosis.csv"

                        )

#Error in file(file, "rt") : cannot open the connection

#In addition: Warning message:

#  In file(file, "rt") :

#  cannot open file 'D:/Users/larab/Chilling-F2/populations.1': Permission denied

 

phenotypes <- read.table("necrosis.csv")

genotypes <- read.table("populations.F2.rqtl.csv")

setwd("D:/Users/larab/Chilling-F2/populations.1")

mydata <- read.cross("csv", "",

                     dir="/",

                     genfile= genotypes,

                     phefile= phenotypes

)

#same error as above

 

mydata <- read.cross("csv", "",

                        dir="/populations.1",

                        genfile="populations.F2.rqtl.csv",

                        phefile="necrosis.csv"

                        )

#Error in file(file, "rt") : cannot open the connection

#In addition: Warning message:

#  In file(file, "rt") :

#  cannot open file '/populations.1': No such file or directory

 

 

setwd("D:/Users/larab/Chilling-F2/")

mydata <- read.cross(format="csv",

                        dir="populations.1",

                        genfile="populations.F2.rqtl.csv",

                        phefile="necrosis.csv"

                        )

#same error as above

 

setwd("D:/Users/larab/Chilling-F2")

mydata <- read.cross(format="csv", " ",

                     dir="populations.1",

                     genfile="populations.F2.rqtl.csv",

                     phefile="necrosis.csv"

)

#same error as above

 

setwd("D:/Users/larab/Chilling-F2")

mydata <- read.cross(format="csv",

                     dir="./populations.1",

                     genfile="populations.F2.rqtl.csv",

                     phefile="necrosis.csv"

)

#same error as above

 

 

mydata <- read.cross(format="csv", " ",

                     dir="",

                     genfile="D:\\Users\\larab\\Chilling-F2\\populations.F2.rqtl.csv",

                     phefile="D:\\Users\\larab\\Chilling-F2\\necrosis.csv"

)

Karl Broman

unread,
Nov 19, 2021, 3:12:17 PM11/19/21
to R/qtl discussion
read.cross with format = "csv" is expecting a single file.

What it does is paste the 'dir' argument onto the beginning of the 'file' argument and then try to read that file with read.csv().
If you have separate files for the genotype and phenotype data, you should use the "csvs" format.

In the call read.cross("csv", "", dir="D:/Users/larab/Chilling-F2/populations.1", genfile="populations.F2.rqtl.csv", phefile="necrosis.csv")
It's going to interpret "" as the argument 'file', will paste that onto the end of 'dir', and then will try to open that as a file.
It'll say "permission denied" and "cannot open the connection", because that's a directory rather than a file.

In the next try, read.cross("csv", "",   dir="/", genfile= genotypes, phefile= phenotypes)
it will be trying to open '/' as a file, and will give a similar error.

The next one read.cross("csv", "", dir="/populations.1", genfile="populations.F2.rqtl.csv", phefile="necrosis.csv")
is doing the same thing but looking for '/populations.1', where the initial '/' stands for the root directory on the drive, and so you get a somewhat different error "No such file or directory", that is that it can't be found.

The next one is maybe closer read.cross(format="csv", dir="populations.1", genfile="populations.F2.rqtl.csv", phefile="necrosis.csv")
but note here that phefile is ignored, and it's just looking for the file "populations.1/populations.F2.rqtl.csv" in the current working directory.

karl

Lara B

unread,
Nov 20, 2021, 10:26:46 AM11/20/21
to R/qtl discussion
Thank you!!!
There were a few components I needed to fix
- changing format from csv to csvs
- adding fileEncoding = 'UTF-8-BOM' so that it recognized the first cells as "id" instead of "id"
- changing the genotype file to remove the first 5 lines of the stacks output
- specifying the genotypes with lower case letters

mydata <- read.cross(format="csvs", fileEncoding = 'UTF-8-BOM', dir="populations.1", genfile="populations.F2.rqtl.csv", phefile="necrosis.csv", genotypes=c("a", "b", "h") )

Reply all
Reply to author
Forward
0 new messages