Skip to content
Snippets Groups Projects
---
title: "AAA_MasterScript"
author: "Francesco Maria Sabatini"
date: "1/14/2020"
output: html_document
---

Annotated script to analyze data for Paper #11 - sPlot

## Step 1 - Make 99 selections of non-neighbouring plots
Resample plots based on spatial proximity based on a buffer of 20 km. After selecting one plot, all plots in the 20+20km radius cannot be selected for the analysis. Repeats procedure 99 times. Returns list of 99 vectors with 1300-1800 plots each. Launch twice, for forest and non forest. Change output name and 'vegtype' variable in submit.Resampling.sh (vegtype either 'for' or 'nonfor').  
To be performed in HPC
```{bash eval=F}
qsub submit-Resampling.sh
```

## ~~Step 2 - Run Beals and fit SAR curves on each plot~~
~~Launches array job, which spreads the 99 resamplings on 99 nodes. Make sure to change the output path in submit.SpeciesPool_sPlot-array-job.sh 
Set vegtype to 'all' [correct??]~~
Deprecated. Now we use directly resampled data to fit BRT
```{bash eval=F}
#qsub -t 1-99 submit-SpeciesPool_sPlot-array-job.sh /data/splot/_data/Forest_99ResSpeciesPool-6119322.RData
#qsub -t 1-99 submit-SpeciesPool_sPlot-array-job.sh /data/splot/_data/NonForest_99ResSpeciesPool-6119883.RData
```

## Step 3 - Organize data for BRTs
Reimport output from Step 2 into iDiv server, and Run script A02_SpeciesPool_DataPrep_BRT.Rmd. Use scp <frontend1.eve.ufz.de:'from.path'> <to.path> to import from HPC to server. Run Script A02 to create `Mydata.<vegtype>.RData` and `world.over.RData`. Extracting predictors, especially for world, takes long computation time. It was therefore done once in the HPC, and the script loads these results. Predictors were extracted using the Script `A98_PredictorsExtract.R`. For all rasters it was done in this way:
```{bash, eval=F}
for filename in /data/project/sPlot/_data/Predictors/*.tif
do
  sub submit-A98.sh $filename
done
```
For Shapefiles, it needs some adaptations, though. Script A98 should be double-checked as I modified it to work both with rasters and shapefiles, but I did not thoroughly test it.  
After Knitting A02, export `Mydata.<vegtype>.RData` and `world.over.RData` files to HPC and proceed with BRT fitting.

## Step 4 - Fit BRTs
Run Script `03_BRTs_fornonf.R` in HPC, for the two vegtypes ('for', 'nonfor') and their combination ('all'). ~~Repeat for each metric of diversity (i.e., grain) (i.e., sr10, sr100, sr400, sr1000, sr1ha, Asym.gomp, iChao2)~~
```{bash}
for fornonf in all for nonfor
  do 
    sbatch submit_03_BRTs_SLURM.sh $fornonf
  done
```

## Step 5 - Create output and graphs
Import all of step 4's output from HPC into Rstudio server. Run `04_BRT_plotting.R`.