## Defining settings
There's a lot of experience with performing PCRs and therefore we know which physicochemical properties of primers are favorable for amplification and which are not. In openPrimeR, the desired ranges of values of these properties are called `constraints` and they are stored in a `DesignSettings` object. 

Let's define some constraints by loading one of the XML settings files that are shipped with the package by storing the result of `read_settings()` in the `settings` variable:

```{r read_settings, exercise = TRUE}
# Load the specified XML file into the 'settings' variable:
xml.file <- system.file("extdata", "settings", "B_Taq_PCR_evaluate.xml",
              package = "openPrimeR")
```

```{r read_settings-solution}
# Load the specified XML file into the 'settings' variable:
xml.file <- system.file("extdata", "settings", "B_Taq_PCR_evaluate.xml",
              package = "openPrimeR")
settings <- read_settings(xml.file)
```

```{r settings_setup}
xml.file <- system.file("extdata", "settings", "B_Taq_PCR_evaluate.xml",
              package = "openPrimeR")
settings <- read_settings(xml.file)
```

Let's take a look at the structure of `settings`:

```{r settings_structure, exercise = TRUE, exercise.setup = "settings_setup"}
print(settings)
```

As you can see, the `DesignSettings` object contains far more information than just the constraint settings, but let's focus on the constraints first. 

### Defining constraints on physicochemical properties

You can change which physicochemical properties are considered and the desired range of property values by using the `constraints()` function:

```{r settings_constraints, exercise = TRUE, exercise.setup = "settings_setup"}
# View the constraints specified in 'settings':
```

```{r settings_constraints-solution}
constraints(settings)
```

The `constraints()` function returns a named list where the names provide the identifiers of the active constraints and the constraints are specified as named vectors containing the entries `min` and `max` where `min` indicates the smallest allowed value and `max` indicates the maximal allowed value of a property. If either `min` or `max` is missing, this indicates that the corresponding range is unlimited. For example, the extent of the GC clamp should be between `r constraints(settings)$gc_clamp["min"]` and `r constraints(settings)$gc_clamp["max"]`, while the coverage of every primer should be at least `r constraints(settings)$primer_coverage["min"]` (there's no upper limit). 

You can customize the constraint settings by using `constraints()` as a setter. Why don't you try to exclude the GC clamp property from consideration and increase the required number of coverage events per primer to 5? Note that you always have to provide named (`min` and/or `max`) numeric vectors when modifying constraints.

```{r modify_constraints, exercise = TRUE}
# Remove the GC clamp constraint and set the minimal primer coverage to 5
```

```{r modify_constraints-solution}
# Remove the GC clamp constraint and set the minimal primer coverage to 5
constraints(settings) <- constraints(settings)[names(constraints(settings)) != "gc_clamp"]
constraints(settings)$primer_coverage <- c("min" = 5)
constraints(settings)
```

### Modifying the coverage constraints

```{r settings_config}
xml.file <- system.file("extdata", "settings", "B_Taq_PCR_evaluate.xml",
              package = "openPrimeR")
settings <- read_settings(xml.file)
constraints(settings) <- constraints(settings)[names(constraints(settings)) != "gc_clamp"]
constraints(settings)$primer_coverage <- c("min" = 5)
```

The coverage constraints determine under which circumstances a primer is considered to cover a template, which means that the primer is likely to successfully amplify the corresponding template. Depending on the selected coverage constraints, the estimate of amplified templates can be more sensitive (e.g. free energy of annealing) or more specific (modeling terminal mismatches). You can modify the coverage conditions using `cvg_constraints()`:

```{r cvg_constraints, exercise = TRUE, exercise.setup = "settings_config"}
# View the active coverage constraints
```


```{r cvg_constraints-solution}
# View the active coverage constraints
cvg_constraints(settings)
```

We see that the maximal false positive rate for calling coverage events is set to `r paste0(round(cvg_constraints(settings)$coverage_model * 100, 2), "%")`. This means that only coverage events whose estimated probability of being false positives is below `r paste0(round(cvg_constraints(settings)$coverage_model * 100, 2), "%")` are retained, while all other events are removed. `stop_codon` and `substitution` are codon design constraints, where 1 denominates coverage events inducing stop codons or substitutions and 0 indicates the absence of such events. Since the maxima of both constraints are set to 1, we do not filter coverage events according to these events at the moment. To learn about other possible coverage constraints, we refer to the openPrimeR manual or the output of viewing the `DesignSettings` object, which indicates the inactive constraints.

### Modifying the constraint options

The constraint options define additional options for the computation of some constraints. Let us now review the active constraint options via `conOptions()`:


```{r con_options, exercise = TRUE, exercise.setup = "settings_config"}
# View the active constraint options
```

```{r con_options-solution}
# View the active constraint options
conOptions(settings)
```

In the constraint options, `allowed_mismatches` provides the maximal number of mismatches between primers and templates, `allowed_other_binding_ratio` sets the maximal ratio of off-target binding events, and `allowed_region_definition` determines whether primers are required to bind within the specified binding region or may also only overlap with the target region. 

Please specify the following settings now:

* Set `allowed_mismatches` to 0 to ensure that only fully complementary primers are considered to cover the templates
* Set `allowed_other_binding_ratio` to 0 to ensure that only primers binding to the target region are considered to cover the templates
* Set `allowed_region_definition` to `any` to consider the coverage of primers that only overlap with the target region

```{r con_options_mod, exercise = TRUE, exercise.setup = "settings_config"}
# Modify the constraint options as suggested
```

```{r con_options_mod-solution}
conOptions(settings)$allowed_mismatches <- 0
conOptions(settings)$allowed_other_binding_ratio <- 0
conOptions(settings)$allowed_region_definition <- "any"
conOptions(settings)
```

```{r settings_config2}
xml.file <- system.file("extdata", "settings", "B_Taq_PCR_evaluate.xml",
              package = "openPrimeR")
settings <- read_settings(xml.file)
constraints(settings) <- constraints(settings)[names(constraints(settings)) != "gc_clamp"]
constraints(settings)$primer_coverage <- c("min" = 5)
conOptions(settings)$allowed_mismatches <- 0
conOptions(settings)$allowed_other_binding_ratio <- 0
conOptions(settings)$allowed_region_definition <- "any"
```

### Adjusting the PCR conditions

Last, we should take a look at the current PCR conditions using `PCR()`:

```{r settings_PCR,  exercise = TRUE, exercise.setup = "settings_config2"}
# Investigate the PCR conditions
```

```{r settings_PCR-solution}
# Investigate the PCR conditions
PCR(settings)
```

When performing an analysis, please ensure that all ion concentrations (molar) and other PCR conditions (e.g. the annealing temperature) reflect your experimental setup correctly. We will retain the default settings and just note that the PCR settings can be adjusted analogously to the other settings.
