Skip to content
Snippets Groups Projects
Commit a0d658e9 authored by Francesco Sabatini's avatar Francesco Sabatini
Browse files

Aligned to new data

parent 8bb08d04
No related branches found
No related tags found
No related merge requests found
......@@ -10,10 +10,18 @@ source("99_HIDDEN_functions.R")
##### PART 1 ####
#### 1. traits data ####
traits0 <- read_delim("_data/Mesobromion/traits3.txt", delim =";", col_names = T, locale = locale(encoding = 'latin1')) %>%
dplyr::select(- c("PR_STAT_Indigen", "PR_STAT_Neophyt", "PR_STAT_Archaeophyt", "URBAN_urbanophob", "URBAN_maessig_urbanophob",
traits0 <- read_delim("_data/Mesobromion/traits3_cov.txt", delim =";", ## manually corrected vowels with umlaut
col_names = T, locale = locale(encoding = 'UTF-8')) %>%
column_to_rownames("X1") %>%
dplyr::select(colnames(.)[which(colSums(., na.rm=T)!=0)]) %>%
dplyr::select(!starts_with("BLM")) %>%
dplyr::select(!starts_with("ZWT")) %>%
dplyr::select(!starts_with("Fabaceae")) %>%
dplyr::select(!starts_with("BEST_")) %>%
dplyr::select(- any_of(c("PR_STAT_Indigen", "PR_STAT_Neophyt", "PR_STAT_Archaeophyt", "URBAN_urbanophob", "URBAN_maessig_urbanophob",
"URBAN_urbanoneutral", "URBAN_maessig_urbanophil", "URBAN_urbanophil", "WUH_von", "WUH_bis", "ARL_c_I_von", "ARL_c_I_bis",
"BL_ANAT_hydromorph")) %>% #empty trait
"BL_ANAT_hydromorph", "WEID_V", "MAHD_V", "TRIT_V"))) %>% #empty trait
rownames_to_column("species") %>%
mutate(species0=species) %>%
rowwise() %>%
# quick and dirty clean up names
......@@ -27,8 +35,8 @@ dim(traits0) #907 obs. of 75 variables:
#keep only traits with >=88 completeness
traits0 <- traits0 %>%
dplyr::select_if(~mean(!is.na(.)) >= 0.88) # 907 x 67
dplyr::select_if(~mean(!is.na(.)) >= 0.88)
dim(traits0)# 902 x 67
......@@ -43,11 +51,13 @@ traits.asym.binary <- c('LEB_F_Makrophanerophyt','LEB_F_Nanophanerophyt',
'V_VER_Innovationsknospe.mit.Speicherwurzel','V_VER_Ausläuferknolle',
'V_VER_Brutsprösschen','V_VER_Fragmentation','V_VER_Turio','V_VER_Sprossknolle',
'V_VER_phyllogener_Spross','V_VER_Rhizompleiokorm','V_VER_Zwiebel',
'V_VER_Ausläuferrhizom','V_VER_Ausläuferzwiebel','V_VER_Bulbille',
'V_VER_Ausläuferrhizom',#'V_VER_Ausläuferzwiebel',
'V_VER_Bulbille',
'ROS_T_rosettenlose.Pflanzen','ROS_T_Halbrosettenpflanze','ROS_T_Ganzrosettenpflanzen',
'BL_AUSD_immergrün','BL_AUSD_sommergrün','BL_AUSD_vorsommergrün',
'BL_AUSD_überwinternd_grün','BL_ANAT_skleromorph','BL_ANAT_mesomorph',
'BL_ANAT_hygromorph','BL_ANAT_hydromorph','BL_ANAT_blattsukkulent','BL_ANAT_helomorph',
'BL_ANAT_hygromorph',#'BL_ANAT_hydromorph',
'BL_ANAT_blattsukkulent','BL_ANAT_helomorph',
'BL_FORM_gelappt_gefiedert_gefingert_mehrfach','BL_FORM_Vollblatt_Normalblatt',
'BL_FORM_Grasblatt_Langblatt','BL_FORM_nadelförmig','BL_FORM_röhrig',
'BL_FORM_schuppenförmig','BL_FORM_schwertförmig','REPR_T_Samen_Sporen',
......@@ -56,7 +66,7 @@ traits.asym.binary <- c('LEB_F_Makrophanerophyt','LEB_F_Nanophanerophyt',
'STRAT_T_CSR','STRAT_T_R','STRAT_T_S','STRAT_T_SR')
traits0 <- traits0 %>%
mutate_at(.vars=vars(any_of(traits.asym.binary)),
mutate_at(.vars=vars(all_of(traits.asym.binary)),
.funs=~(.>0)*1)
......@@ -76,8 +86,8 @@ all.traits <- traits0 %>%
by="species")
traits <- all.traits %>%
filter(!is.na(LeafArea))
dim(all.traits) #[1] 907 81
dim(traits) #[1] 805 2
dim(all.traits) #[1] 902 82
dim(traits) #[1] 801 82
......@@ -102,8 +112,8 @@ env.all <- env
### 3. Import species data ####
# columns in species correspond to those in env
# there is no PlotObservationID (yet)
species0 <- read.table("_data/Mesobromion/GVRD_Mes2_veg1.csv", sep = ",", header=T)
dim(species0) #6868 obs. of 907 variables:
species0 <- read_csv("_data/Mesobromion/GVRD_Mes2_proz2.csv", locale = locale(encoding = 'latin1'))
dim(species0) #6868 obs. of 903 variables:
rownames(species0) <- env0$RELEVE_NR
## select only plots already selected in env
......@@ -117,7 +127,7 @@ species <- env %>%
dplyr::select(colnames(.)[which(colSums(.)!=0)])
#dplyr::select(traits$species0)
dim(species) # [1] 5810 881
dim(species) # [1] 5810 877
releve08trait <- species %>%
rownames_to_column("RELEVE_NR") %>%
......@@ -147,10 +157,10 @@ releve08trait.samp <- sample(releve08trait, round(length(releve08trait)/10), rep
species <- species %>%
rownames_to_column("RELEVE_NR") %>%
filter(RELEVE_NR %in% releve08trait.samp) %>%
#column_to_rownames("RELEVE_NR") %>%
#as.tbl() %>%
dplyr::select(RELEVE_NR, one_of(traits$species0))
dplyr::select(RELEVE_NR, one_of(traits$species0)) %>%
column_to_rownames("RELEVE_NR") %>%
dplyr::select(colnames(.)[which(colSums(.)!=0)])
env <- env %>%
filter(RELEVE_NR %in% releve08trait.samp)
......@@ -235,9 +245,9 @@ traits <- recode.traits(traits)
### ordered factors
dim(species) #558 783
dim(traits) #783 53
dim(env) #558 8
dim(species) #581 509
dim(traits) #509 54
dim(env) #581 8
......@@ -282,74 +292,48 @@ env <- env %>%
species <- species %>%
rownames_to_column("RELEVE_NR")
##export for Valerio
write_delim(species, path="_data/Mesobromion/species.out.10perc.txt", delim="\t")
write_delim(traits, path="_data/Mesobromion/traits.out.10perc.txt", delim="\t")
write_delim(env, path="_data/Mesobromion/env.10perc.cov.txt", delim="\t")
### 5. Transform and export data
## presence absence
species.pa <- species
species.pa[,-1] <- (species[,-1]>0)*1
## sqrt transform cov values
species.cov <- species
species.cov[,-1] <- sqrt(species.cov[,-1])
#transform percentage cover to relative.cover
species.cov <- species.cov %>%
mutate(sumVar = rowSums(.[-1])) %>%
mutate_at(.vars=vars(-RELEVE_NR),
.funs=~./sumVar) %>%
dplyr::select(-sumVar)
dim(species.cov) #[1] 581 510
## version without missing species
empty <- which(colSums(species[,-1])==0)
traits_nozero <- traits[-empty,]
species_nozero <- species[,-(empty+1)]
## export
write_delim(species.pa, path="_data/Mesobromion/species.v2.10perc.pa.txt", delim="\t")
write_delim(species.cov, path="_data/Mesobromion/species.v2.10perc.cov.txt", delim="\t")
write_delim(traits, path="_data/Mesobromion/traits.v2.10perc.txt", delim="\t")
write_delim(env, path="_data/Mesobromion/env.v2.10perc.cov.txt", delim="\t")
write_delim(species_nozero , path="_data/Mesobromion/species.out.10perc_nozero.txt", delim="\t")
write_delim(traits_nozero, path="_data/Mesobromion/traits.out.10perc_nozero.txt", delim="\t")
### create list of relevées
write_delim(species %>%
dplyr::select(RELEVE_NR),
path="_derived/Mesobromion/ReleveList.txt", delim="\t")
path="_data/Mesobromion/ReleveList.txt", delim="\t")
###### 5. Species data with cover values #####
### version with cover values ### 4/08/2020
env0 <- read_delim("_data/Mesobromion/GVRD_MES2_site.csv", delim = ",")
species.proz <- read_csv("_data/Mesobromion/GVRD_Mes2_proz.csv", locale = locale(encoding = 'latin1'))
species.proz$RELEVE_NR <- env0$RELEVE_NR
species.proz <- species.proz %>%
filter(RELEVE_NR %in% (species %>% pull(RELEVE_NR))) %>%
## delete species not appearing in any plot
dplyr::select(colnames(.)[which(colSums(.)!=0)])
dim(species.proz)
write_delim(species.proz , path="_data/Mesobromion/species.out.10perc.proz.txt", delim="\t")
#transform percentage cover to relative.cover
species.proz <- species.proz %>%
mutate(sumVar = rowSums(.[-1])) %>%
mutate_at(.vars=vars(-RELEVE_NR),
.funs=~./sumVar) %>%
dplyr::select(-sumVar)
dim(species.proz) #[1] 558 533
write_delim(species.proz , path="_data/Mesobromion/species.out.10perc.cov.txt", delim="\t")
## align traits to species in species.proz
traits.proz <- recode.traits(all.traits)
traits.proz <- data.frame(species=colnames(species.proz)[-1] ) %>%
### clean species names in both data.frames
mutate(species0=as.character(species)) %>%
rowwise() %>%
# quick and dirty clean up names
mutate(species=gsub(pattern="_agg_|_x_|_spec$|_agg$|_s_|_Sec_|__", replacement="_", x=species)) %>%
mutate(species=gsub(pattern="_$", replacement = "", x = species)) %>%
mutate(species=ifelse(is.na(word(species, 1, 2)), species, word(species, 1, 2))) %>%
ungroup() %>%
left_join(traits.proz %>%
mutate(species=species0) %>%
rowwise() %>%
mutate(species=gsub(pattern="_agg_|_x_|_spec$|_agg$|_s_|_Sec_|__", replacement="_", x=species)) %>%
mutate(species=gsub(pattern="_$", replacement = "", x = species)) %>%
mutate(species=ifelse(is.na(word(species, 1, 2)), species, word(species, 1, 2))) %>%
ungroup() %>%
dplyr::select(-species0),
by="species") %>%
dplyr::select(-species) %>%
dplyr::select(`species0`, everything())
##check for species without trait info
traits.proz %>%
traits %>%
filter_at(.vars=vars(-"species0"),
all_vars(is.na(.))) %>%
dim() ## [1] 16 53 # species with no trait info
write_delim(traits.proz, path="_data/Mesobromion/traits.out.10perc.cov.txt", delim="\t")
nrow() ## [1] no species with no trait info ##
## simply because those 7 species without TRY data were excluded already
traits %>%
filter_at(.vars=vars(-"species0"),
any_vars(is.na(.))) %>%
nrow() ## [1] 109 # species with no at least 1 NA in traits
#### CORRELATION BETWEEN FUZZY WEIGHTED AND BEALS MATRICES
#### WAS RUN IN THE CLUSTER WITH THE SCRIPT 01b_MesobromionCluster.R
......
......@@ -56,7 +56,7 @@ get.best <- function(x, N){
Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY.bootstrap",
combinations=c("all", "sequential"), start.round=NA, relax.round=NA, max.inter.t,
chunkn, chunk.i, nperm=199, ncores){
chunkn, chunk.i, nperm=199, ncores, exclude.na=F){
if(ncores>1){
require(parallel)
......@@ -75,16 +75,23 @@ Mesobromion <- function(species.path, traits.path, output, myfunction="get.corXY
column_to_rownames("RELEVE_NR")
traits <- read_delim(traits.path, delim="\t") %>%
as.data.frame() #%>%
### TEMPORARY FOR TESTING!
#dplyr::select(1:6)
traits <- traits %>%
column_to_rownames("species0") %>%
rename_all(.funs=~gsub(pattern=".mean$", replacement="", x=.)) %>%
mutate_if(~is.character(.), .funs=~as.factor(.))
#temporary ### Use only a subset of traits
#dplyr::select(LeafArea:Disp.unit.leng)
mutate_if(~is.character(.), .funs=~as.factor(.)) %>%
column_to_rownames("species0")
if(exclude.na==T){
print("Excluding NAs and recalculating relative covers")
traits <- traits %>%
filter(complete.cases(.))
species <- species %>%
dplyr::select(rownames(traits)) %>%
mutate(sumVar = rowSums(.)) %>%
mutate_all(.funs=~./sumVar) %>%
dplyr::select(-sumVar)
}
if(combinations=="all") {
## create list of indices for each combination of traits up to a max number of interactions
......
This diff is collapsed.
......@@ -71,6 +71,14 @@ options <- list (
default = NA,
help = "After what round should the selection of new best combos stop being based on c.i.?",
metavar = "4"
),
make_option(
opt_str = c("-e", "--exclude.na"),
dest = "exclude.na",
type = "logical",
default = F,
help = "should species with ANY nas in traits be excluded? (and relative covers recalculated?)",
metavar = "4"
)
)
......@@ -99,10 +107,11 @@ chunkn <- cli$options$chunkn
chunk.i <- cli$options$chunk.i
nperm <- cli$options$nperm
ncores <- cli$options$ncores
exclude.na <- cli$options$exclude.na
# ------------------------------------------------------------------------------
# actual program
# ------------------------------------------------------------------------------
source("01b_MesobromionCluster.R")
Mesobromion(species.path, traits.path, output, myfunction, combinations, start.round, relax.round, max.inter.t, chunkn, chunk.i, nperm, ncores)
Mesobromion(species.path, traits.path, output, myfunction, combinations, start.round, relax.round, max.inter.t, chunkn, chunk.i, nperm, ncores, exclude.na)
species.path <- "_data/Mesobromion/species.out.10perc.cov.txt"
traits.path <- "_data/Mesobromion/traits.out.10perc.cov.txt"
species.path <- "_data/Mesobromion/species.v2.10perc.cov.txt"
traits.path <- "_data/Mesobromion/traits.v2.10perc.txt"
output <- "_derived/Mesobromion/Cover/HIDDENtest"
myfunction <- "get.corXY.bootstrap"
max.inter.t <- 1
chunk.i <- 1
nperm <- 49
nperm <- 3
ncores <- 1
chunkn <- 1
combinations <- "all"
start.round <- 1
relax.round <- 1
exclude.na <- T
source("01b_MesobromionCluster.R")
Mesobromion(species.path, traits.path, output, myfunction,
combinations, start.round, relax.round, max.inter.t,
chunkn, chunk.i, nperm, ncores)
chunkn, chunk.i, nperm, ncores, exclude.na)
......@@ -28,6 +28,7 @@ Rscript \
--ncores ${NSLOTS:-1} \
--start.round 1 \
--relax.round 4 \
--exclude.na T \
/data/splot/HIDDEN/species.out.10perc.cov.txt \
/data/splot/HIDDEN/traits.out.10perc.cov.txt \
"$output" \
......
......@@ -30,9 +30,11 @@ Rscript \
--ncores ${NSLOTS:-1} \
--start.round 1 \
--relax.round 1 \
--exclude.na T \
/data/splot/HIDDEN/species.out.10perc.cov.txt \
/data/splot/HIDDEN/traits.out.10perc.cov.txt \
"$output" \
"get.corXY.bootstrap" \
"all" \
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment