# Converting Data from Phyloseq into MicrobiomeStat

The guide illustrates the procedure to convert a Phyloseq data object into a MicrobiomeStat data object.

### Requirement: Taxa as Rows in Phyloseq

Before converting your Phyloseq object to a MicrobiomeStat data object, it is crucial to ensure that the taxa are represented as rows in your Phyloseq object. This is a requirement for the conversion process to work correctly.

To check if your Phyloseq object has taxa as rows, you can use the `phyloseq::taxa_are_rows()` function:

```r
library(phyloseq)

data(GlobalPatterns)

# Check if taxa are rows in the Phyloseq object
phyloseq::taxa_are_rows(GlobalPatterns)
```

If the function returns `TRUE`, your Phyloseq object is ready for conversion. If it returns `FALSE`, you need to transpose your Phyloseq object to have taxa as rows before proceeding with the conversion.

If your Phyloseq object contains a phylogenetic tree, you also need to load the `phangorn` package for midpoint rooting:

```r
library(phangorn)
```

First, let's take a look at the original structure of our Phyloseq data:

<figure><img src="https://3375851868-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhFAmY4z7d1R15Cv49vXQ%2Fuploads%2FjCY1h3HwrMrLN224JI5e%2FScreenshot%202023-10-10%20at%2010.51.53.png?alt=media&#x26;token=c6ae1a78-c514-498a-95d2-38090bf8c6e5" alt=""><figcaption><p><strong>Figure 1</strong>: Structure of the original Phyloseq data object.</p></figcaption></figure>

Now, initiate the conversion:

```r
# Required package
library(microbiome)

# Load the dataset
data(peerj32)
peerj32.phy <- peerj32$phyloseq

# Convert the Phyloseq object to a MicrobiomeStat data object
data.obj <- mStat_convert_phyloseq_to_data_obj(peerj32.phy)
```

After the conversion, the Phyloseq object is transformed into a MicrobiomeStat data object. Let's examine the structure post-conversion:

<figure><img src="https://3375851868-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhFAmY4z7d1R15Cv49vXQ%2Fuploads%2F8WglzwdwO9NW01YT7RUt%2FScreenshot%202023-10-10%20at%2010.52.44.png?alt=media&#x26;token=15fae760-24f3-4542-9018-2095d0a0b500" alt=""><figcaption><p><strong>Figure 2</strong>: Structure of the MicrobiomeStat data object after converting from Phyloseq.</p></figcaption></figure>

### Caution Regarding Factor Levels

During the conversion, there might be changes in the levels of factors in the metadata. This can potentially alter the order or even result in missing levels.

**Recommendation:** It's important to verify the factors after conversion to ensure data consistency. If there are any changes, the factor levels should be reset:

```r
# Example: Adjusting the 'sex' factor levels
data.obj$meta.dat$sex <- factor(as.factor(data.obj$meta.dat$sex), levels = c("male", "female"))
```

Reorder the factor levels of the `sex` column。

The function `mStat_convert_phyloseq_to_data_obj` returns a MicrobiomeStat data object, which includes:

* **feature.tab**: A matrix representing the feature table (OTU table). Rows with a sum of zero are excluded, retaining only the features present in the samples.
* **meta.dat**: A data frame containing the sample data.
* **feature.ann**: A matrix of the feature annotation table (taxonomy table). Only the rows corresponding to the feature table are included.
* **tree**: A rooted phylogenetic tree. If the tree is not rooted, it will be rooted using the midpoint method. Tips not present in the OTU table are pruned.

After converting the Phyloseq object to the MicrobiomeStat format, the data is now compatible with MicrobiomeStat's analysis tools.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.microbiomestat.wiki/setting-up-microbiomestat-installation-and-data-preparation/laying-the-foundation-creating-the-microbiomestat-data-object/navigating-data-from-phyloseq-into-microbiomestat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
