I want to convert a genetic matrix (rows are samples, columns are alleles) into a STRUCTURE file (output .str) within R. Is there a preferred way to achieve this? I've found genind_to_structure in the graph4lg R package, but I wanted to see if there are other approaches.

An example of what I mean by a "genetic matrix":

# Build a matrix of alleles
temp <- lapply(1:30, function(i) sample(1:9, 4, replace = TRUE))
temp <- sapply(temp, paste, collapse="")
temp <- matrix(temp, nrow = 10, dimnames = list(paste("ind",1:10), paste("loc",1:3)))

# Convert matrix to genind format (for adegenet package)
test <- df2genind(temp, ploidy = 4, sep = "")

# How to convert to STRUCTURE?

If anyone has input, please chime in! Thanks

  • 1
    $\begingroup$ Structure is a bit old now. There is fastStructure (2014) rajanil.github.io/fastStructure which is Python. Happy to do this if there's a Pythonic Bio.popgen equivalent geared for fastStructure $\endgroup$
    – M__
    Commented Jun 15, 2022 at 16:35
  • 1
    $\begingroup$ I’m not totally sure what you’re asking, since the current method seems to be working? Is there a reason why you need another approach? $\endgroup$
    – user438383
    Commented Jun 17, 2022 at 7:25

1 Answer 1


I ultimately found this function, which I used for the purpose of converting a genind to a STRUCTURE file. The graph4lg genind_2_structure command would regularly error on me (present working directory couldn't be passed as the output argument).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.