<p>I ultimately found <a href="https://github.com/lvclark/R_genetics_conv/blob/master/genind2structure.R" rel="nofollow noreferrer">this function</a>, which I used for the purpose of converting a genind to a STRUCTURE file. The graph4lg <a href="https://rdrr.io/cran/graph4lg/man/genind_to_structure.html" rel="nofollow noreferrer">genind_2_structure</a> command would regularly error on me (present working directory couldn't be passed as the <code>output</code> argument).</p>

CC BY-SA 4.0
2022-07-06T19:28:52.927Z
2022-07-06T19:28:52.927Z
akoontz11
<p>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 <a href="https://rdrr.io/cran/graph4lg/man/genind_to_structure.html" rel="nofollow noreferrer">genind_to_structure</a> in the <a href="https://rdrr.io/cran/graph4lg/" rel="nofollow noreferrer">graph4lg</a> R package, but I wanted to see if there are other approaches.</p>
<p>An example of what I mean by a &quot;genetic matrix&quot;:</p>
<pre><code># Build a matrix of alleles
temp &lt;- lapply(1:30, function(i) sample(1:9, 4, replace = TRUE))
temp &lt;- sapply(temp, paste, collapse=&quot;&quot;)
temp &lt;- matrix(temp, nrow = 10, dimnames = list(paste(&quot;ind&quot;,1:10), paste(&quot;loc&quot;,1:3)))
temp

# Convert matrix to genind format (for adegenet package)
test &lt;- df2genind(temp, ploidy = 4, sep = &quot;&quot;)
test

# How to convert to STRUCTURE?
</code></pre>
<p>If anyone has input, please chime in! Thanks</p>

CC BY-SA 4.0
2022-06-15T16:35:06.127Z
Structure is a bit old now. There is fastStructure (2014) http://rajanil.github.io/fastStructure/ which is Python. Happy to do this if there's a Pythonic Bio.popgen equivalent geared for fastStructure
M__
CC BY-SA 4.0
2022-06-17T07:25:05.020Z
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?
user438383
<r><file-formats><phylogenetics>
Converting genetic matrix to STRUCTURE
# Converting genetic matrix to STRUCTURE

(Asked by: username_0 on 2022-06-15)

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)))
temp

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

# How to convert to STRUCTURE?

If anyone has input, please chime in! Thanks

**Tags:** r, file-formats, phylogenetics
> **Comments:**
> **username_1**: Structure is a bit old now. There is fastStructure (2014) http://rajanil.github.io/fastStructure/ which is Python. Happy to do this if there's a Pythonic Bio.popgen equivalent geared for fastStructure  
> **username_2**: 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?  

---

## 1 answers

### Answer by username_0 on 2022-07-06. Score: 1 | Accepted answer

I ultimately found [this function](https://github.com/lvclark/R_genetics_conv/blob/master/genind2structure.R), which I used for the purpose of converting a genind to a STRUCTURE file. The graph4lg [genind\_2\_structure](https://rdrr.io/cran/graph4lg/man/genind_to_structure.html) command would regularly error on me (present working directory couldn't be passed as the `output` argument).


---

