adegenet adegenet 2.1.11 infile related

I am unable to convert a *.str file into a genind object using the read.structure() command. If I use original "nancycats.str", it works, whereas for any other file it does not. If I try to fool by pasting my file into nancycats.str, it goes till conversion but doesn't convert. But when I revert to original "nancycats.str", it works well.

Modifications tried:

Shortcut for path:

obj1 <- read.structure(system.file("files/Adadig_ImpNgb.str",package="adegenet"))
Error in if (!toupper(.readExt(file)) %in% c("STR", "STRU")) stop("File extension .stru expected") :
argument is of length zero
In addition: Warning messages:
1: package ‘adegenet’ was built under R version 4.4.2
2: package ‘ade4’ was built under R version 4.4.2

Providing a full path:

obj1 <- read.structure("C:/Users/PHendre/OneDrive - CIFOR-ICRAF/Documents/R/win-library/4.0/adegenet/files/Adadig_ImpNgb.str", package="adegenet")
Error in read.structure("C:/Users/PHendre/OneDrive - CIFOR-ICRAF/Documents/R/win-library/4.0/adegenet/files/Adadig_ImpNgb.str", :
unused argument (package = "adegenet")

Same "nancycats.str" relabelled as "nancycatsdupl.str" without changing anything

obj1 <- read.structure(system.file("files/nancycatsdupl.str",package="adegenet"))
Error in if (!toupper(.readExt(file)) %in% c("STR", "STRU")) stop("File extension .stru expected") :
argument is of length zero

Pasting the infile into "nancycats.str" following error:

obj1 <- read.structure(system.file("files/nancycats.str",package="adegenet"))

How many genotypes are there?
320

How many markers are there?
509

Which column contains labels for genotypes ('0' if absent)?
1

Which column contains the population factor ('0' if absent)?
0

Which other optional columns should be read (press 'return' when done)?
1:

Which row contains the marker names ('0' if absent)?
0

Are genotypes coded by a single row (y/n)?
n

Converting data from a STRUCTURE .stru file to a genind object...

Error in txt[(lastline - n + 1):lastline] :
only 0's may be mixed with negative subscripts

The original "nancycats.str" (removing populaiton factor line)

obj1 <- read.structure(system.file("files/nancycats.str",package="adegenet"))

How many genotypes are there?
237

How many markers are there?
9

Which column contains labels for genotypes ('0' if absent)?
1

Which column contains the population factor ('0' if absent)?
0

Which other optional columns should be read (press 'return' when done)?
1:

Which row contains the marker names ('0' if absent)?
0

Are genotypes coded by a single row (y/n)?
n

Converting data from a STRUCTURE .stru file to a genind object...

I'm not familiar with adegenet, but I think here you have a simple problem of pointing to the right file.

First, do you understand this instruction?

system.file("files/nancycats.str",package="adegenet")

This is saying that, when installing the package {adegenet}, it came with some files, including this nancycats.str. During installation they were placed in a special directory, which you can access using system.file(xx, package = "adegenet"), i.e. you're asking R to look for xx in the special directory that was created during installation of the package {adegenet}.

So in your case, unless you somehow went there yourself, Adadig_ImpNgb.str has no reason to be inside this special directory.

In this command:

obj1 <- read.structure(system.file("files/Adadig_ImpNgb.str",package="adegenet"))
Error in if (!toupper(.readExt(file)) %in% c("STR", "STRU")) stop("File extension .stru expected") :
argument is of length zero

I believe the error message is misleading, it should simply say "file not found at this path" (in practice, because the file was not found, the extension could not be identified, hence this error message).

So the right solution was to provide a full path.

obj1 <- read.structure("C:/Users/PHendre/OneDrive - CIFOR-ICRAF/Documents/R/win-library/4.0/adegenet/files/Adadig_ImpNgb.str", package="adegenet")
Error in read.structure("C:/Users/PHendre/OneDrive - CIFOR-ICRAF/Documents/R/win-library/4.0/adegenet/files/Adadig_ImpNgb.str", :
unused argument (package = "adegenet")

Here you need to read the error message: you kept the argument package="adegenet", but this was an argument for system.file(), to tell it which special directory to look in. The function read.structure() does not know what to do with that argument, and fails.

The correct code would then be:

file_path <- "C:/Users/PHendre/OneDrive - CIFOR-ICRAF/Documents/R/win-library/4.0/adegenet/files/Adadig_ImpNgb.str"
# double check this path is correct:
file.exists(file_path)
# now read it
obj1 <- read.structure(file_path)

Thanks Alexis. This has worked wonderfully without any problem!

I still do not understand why pasting the infile into the system folder suddenly stopped working. I have been doing that for years, and it always worked without any issues except this time. Even now the path provided was still the path for the system file folder, which worked if a path was provided, but if directly called through the system file command, it didn't work. Strange!

I have to change the SOP for this analysis now.

But many thanks and much gratitude to you!

Prasad Hendrfe

Great that it worked!

Not sure about the reason, if you want to investigate, you can print the path explicitly:

system.file("files/nancycats.str",package="adegenet")  |> normalizePath()

should give you the current path to the package directory, and you can try to use list.files() to see the contents of this directory from R's point of view.

Also a note, I think putting your own file in the package directory is probably bad practice, e.g. if you install a different version of R the 4.0 part of the path may change (this could be what happened here), and in general on different computers there may be a different place where packages are saved (as described in ?.libPaths ).

There could be a few different "best practice" approaches depending on your exact situation. For example, when I had a big reference file that I needed to use in different projects, I created a small package that I store on Github, I can then install this package on any new computer and use the files that comes with it.

Of course it depends on your situation, if this is a file specific to a project, it would be better to keep it in the project directory.

Another option if you need this file in multiple projects and don't want to look for it; slightly less best practice, but much easier, is to have some directory on your computer, say Documents/.specialFiles, and in your .RPofile, add something like options(ADE_FILE_PATH="~/Documents/.specialFiles/Adadig_ImpNgb.str"). Then when you need the file you can just call getOption("ADE_FILE_PATH") and find it. It still wouldn't transfer well between computers and people, but at least it would not depend on the installation directories (which you do not control: if the authors of R or adegenet decide to change something, that can break your workflow without intervention on your side).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.