---
title: "Building a Metaweb"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Building a Metaweb}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Introduction

A metaweb represents the set of all potential consumer–resource
interactions in a system. In the context of paleoecology, the metaweb
captures interactions that are trait-compatible, even if they may
not all occur simultaneously in a realised community.

The PFWIM workflow allows users to infer these potential interactions
from species trait data and categorical feeding rules.

This vignette demonstrates how to:
* Infer a metaweb edgelist using infer_edgelist()
* Convert the edgelist into an igraph network
* Visualise the resulting food web

```{r setup, message=FALSE, warning=FALSE}

library(pfwim)
library(dplyr)
library(igraph)

data("traits", package = "pfwim")
data("feeding_rules", package = "pfwim")

```

# Inferring the Metaweb

The function `infer_edgelist()` evaluates trait compatibility between
species according to a set of categorical feeding rules. It returns an
edgelist, where each row represents a feasible interaction.

```{r}


metaweb_el <- infer_edgelist(
  data = traits,
  cat_combo_list = feeding_rules,
  col_taxon = "species",
  certainty_req = "all",
  hide_printout = TRUE
)

head(metaweb_el)

```

Typical edgelists contain:

* *consumer* – the feeding species
* *resource* – the prey species

These edges define the structure of the potential food web.

# Converting the Edgelist to an igraph Network

The edgelist can be directly converted into an igraph object, which
allows users to compute network statistics and generate visualisations.

``` {r}


metaweb_graph <- graph_from_data_frame(
  metaweb_el,
  directed = TRUE
)

metaweb_graph

```

# Visualising the Metaweb

Food webs are directed networks where edges flow from resource → consumer.

```{r}


set.seed(66)

plot(
  metaweb_graph,
  vertex.size = 35,
  vertex.label.cex = 0.6,
  edge.arrow.size = 0.3,
  layout = layout_with_fr(metaweb_graph),
  main = "Metaweb"
)

```

This visualisation represents the potential trophic structure of the
community based purely on trait compatibility.

Because all feasible interactions are included, metawebs are typically
much denser than observed ecological networks.
