Paracou.R 15.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env Rscript

#===================================================#
#
# Format Paracou traits data
# Ghislain Vieilledent
# <ghislain.vieilledent@cirad.fr>
# September 17th, 2013
#
#===================================================#

# The following files must be in the Paracou raw data folder (../../data/raw/Paracou/)
# 1. "BridgeDATA.ghis.txt" --> Bridge trait data
# 2. "seed.Rdata" --> data for seed mass
# 3. "Autour-de-Paracou-Releves-par-trait-et-taxon.txt" --> PlanTrait data
# 4. "20130717_paracou_taxonomie.csv" --> species taxonmy and code

# Load raw data
19
data.traits <- read.table(file="./data/raw/Paracou/BridgeDATA.ghis.txt",header=TRUE,sep="\t")
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
head(data.traits)

#================
# Leaf N (mg.g-1)
## Unit is OK. See Reich, P. B. & Oleksyn, J. Global patterns of plant
## leaf N and P in relation to temperature and latitude Proceedings of
## the National Academy of Sciences of the United States of America,
## National Acad Sciences, 2004, 101, 11001-11006
data.traits$LeafN <- data.traits$perc_N*10 # Conversion from % to mg.g-1
hist(data.traits$LeafN)

#==============
# SLA (mm2.mg-1)
mean(data.traits$dry_mass,na.rm=TRUE) # 0.72 g, 0K
# -> dry_mass is in g
mean(data.traits$ind_surf_area,na.rm=TRUE) # 197 cm2, OK
# -> ind_surf_area is in cm2 
data.traits$SLA <- (data.traits$ind_surf_area*100)/(data.traits$dry_mass*1000) # Conversion from cm2.g-1 to mm2.mg-1
hist(data.traits$SLA)
mean(data.traits$SLA,na.rm=TRUE) # 32.9 mm2.mg-1
range(data.traits$SLA,na.rm=TRUE) # 1.8 - 142.2 mm2.mg-1

# These values should be fine but do not correspond to the range for
# SLA (3.1-121.1) in Baraloto, C.; Timothy Paine, C. E.; Patiño, S.;
# Bonal, D.; Hérault, B. & Chave, J. Functional trait variation and
# sampling strategies in species-rich plant communities Functional
# Ecology, Blackwell Publishing Ltd, 2010, 24, 208-216.
# There is an error in the paper. The SLA unit should be mm2.mg-1.

#==================
# Seed (mg)
51
load("./data/raw/Paracou/seed.Rdata") # from Mélaine Aubry-Kientz
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
summary(seed_mass)
levels(seed_mass$Taxon)
levels(data.traits$Name)
# edit(seed_mass)
data.traits.2 <- merge(data.traits,seed_mass,by.x="Name",by.y="Taxon",all.x=TRUE)
names(data.traits.2)
data.traits.2$SeedMass <- data.traits.2$Seed*1000 # Conversion from g to mg

#======================
# Wood density (mg.mm-3)
data.traits.2$WSG <- data.traits.2$sapwood_dens*1000/(1000) # mg.mm-3 is equivalent to g.cm-3
summary(data.traits.2)
hist(data.traits.2$WSG)

#======================
# Final data-set

length.noNA <- function(x){sum(!is.na(x))}

#==== At the genus level
ngenus <- length(levels(data.traits.2$Genus))
List.genus <- levels(data.traits.2$Genus)
data.genus <- data.frame(Genus=List.genus,
                         LeafN=NA,LeafN.sd=NA,LeafN.n=NA,
                         SeedMass=NA,SeedMass.sd=NA,SeedMass.n=NA,
                         SLA=NA,SLA.sd=NA,SLA.n=NA,
                         WSG=NA,WSG.sd=NA,WSG.n=NA)

data.genus$LeafN <- tapply(data.traits.2$LeafN,data.traits.2$Genus,mean,na.rm=TRUE)
data.genus$LeafN.sd <- tapply(data.traits.2$LeafN,data.traits.2$Genus,sd,na.rm=TRUE)
data.genus$LeafN.n <- tapply(data.traits.2$LeafN,data.traits.2$Genus,length.noNA)

data.genus$SeedMass <- tapply(data.traits.2$SeedMass,data.traits.2$Genus,mean,na.rm=TRUE)
data.genus$SeedMass.sd <- tapply(data.traits.2$SeedMass,data.traits.2$Genus,sd,na.rm=TRUE)
data.genus$SeedMass.n <- tapply(data.traits.2$SeedMass,data.traits.2$Genus,length.noNA)

data.genus$SLA <- tapply(data.traits.2$SLA,data.traits.2$Genus,mean,na.rm=TRUE)
data.genus$SLA.sd <- tapply(data.traits.2$SLA,data.traits.2$Genus,sd,na.rm=TRUE)
data.genus$SLA.n <- tapply(data.traits.2$SLA,data.traits.2$Genus,length.noNA)

data.genus$WSG <- tapply(data.traits.2$WSG,data.traits.2$Genus,mean,na.rm=TRUE)
data.genus$WSG.sd <- tapply(data.traits.2$WSG,data.traits.2$Genus,sd,na.rm=TRUE)
data.genus$WSG.n <- tapply(data.traits.2$WSG,data.traits.2$Genus,length.noNA)

data.genus[data.genus=="NaN"] <- NA

#==== At the species level
nspecies <- length(levels(data.traits.2$Name))
List.species <- levels(data.traits.2$Name)
data.species <- data.frame(Species=List.species,
                           Genus=NA,
                           LeafN=NA,LeafN.sd=NA,LeafN.n=NA,
                           SeedMass=NA,SeedMass.sd=NA,SeedMass.n=NA,
                           SLA=NA,SLA.sd=NA,SLA.n=NA,
                           WSG=NA,WSG.sd=NA,WSG.n=NA)

data.species$Genus <- List.genus[tapply(data.traits.2$Genus,data.traits.2$Name,unique)]

data.species$LeafN <- tapply(data.traits.2$LeafN,data.traits.2$Name,mean,na.rm=TRUE)
data.species$LeafN.sd <- tapply(data.traits.2$LeafN,data.traits.2$Name,sd,na.rm=TRUE)
data.species$LeafN.n <- tapply(data.traits.2$LeafN,data.traits.2$Name,length.noNA)

data.species$SeedMass <- tapply(data.traits.2$SeedMass,data.traits.2$Name,mean,na.rm=TRUE)
data.species$SeedMass.sd <- tapply(data.traits.2$SeedMass,data.traits.2$Name,sd,na.rm=TRUE)
data.species$SeedMass.n <- tapply(data.traits.2$SeedMass,data.traits.2$Name,length.noNA)

data.species$SLA <- tapply(data.traits.2$SLA,data.traits.2$Name,mean,na.rm=TRUE)
data.species$SLA.sd <- tapply(data.traits.2$SLA,data.traits.2$Name,sd,na.rm=TRUE)
data.species$SLA.n <- tapply(data.traits.2$SLA,data.traits.2$Name,length.noNA)

data.species$WSG <- tapply(data.traits.2$WSG,data.traits.2$Name,mean,na.rm=TRUE)
data.species$WSG.sd <- tapply(data.traits.2$WSG,data.traits.2$Name,sd,na.rm=TRUE)
data.species$WSG.n <- tapply(data.traits.2$WSG,data.traits.2$Name,length.noNA)

data.species[data.species=="NaN"] <- NA

#=========================================================================================
# Completing the data-set with the PlanTrait database from E. Parent and S. Gourlet-Fleury

#= Load libraries
library(reshape)

#= Load PlanTraits
135
PlanTraits <- read.csv("./data/raw/Paracou/Autour-de-Paracou-Releves-par-trait-et-taxon.txt",
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                       stringsAsFactors=FALSE, header=TRUE, sep="\t")
names(PlanTraits)

#= Reformat PlanTraits to one row per species, with each trait as a column
spp.means <- cast(PlanTraits, LIB_TAXON ~ METHO_LIB, value = "MEASURE", fun = mean)
colnames(spp.means)[-1] <- paste(colnames(spp.means)[-1],".mean",sep="")
spp.sds <- (cast(PlanTraits, LIB_TAXON ~ METHO_LIB, value = "MEASURE", fun = sd))
colnames(spp.sds) <- paste(colnames(spp.sds),".sd",sep="")
PlanTraits2 <- cbind(spp.means,spp.sds[,-1])[,c("LIB_TAXON","Leaf nitrogen concentration (standard).mean","Leaf nitrogen concentration (standard).sd",
	"Specific leaf area (standard).mean","Specific leaf area (standard).sd", "Wood density.mean","Wood density.sd")]
colnames(PlanTraits2)[1] <- c("Latin_name")
PlanTraits2 <- PlanTraits2[order(PlanTraits2$Latin_name),]
for (k in 2:ncol(PlanTraits2)) {
    PlanTraits2[,k][!is.finite(PlanTraits2[,k])] <- NA
}
colnames(PlanTraits2) <- c("Latin_name","LeafN.mean","LeafN.sd","SLA.mean","SLA.sd","WSG.mean","WSG.sd")
PlanTraits2$LeafN.mean <- PlanTraits2$LeafN.mean/1 # LeafN already in mg.g-1
PlanTraits2$SLA.mean <- PlanTraits2$SLA.mean/1 # Conversion from m2.kg-1 to mm2.mg-1
PlanTraits2$WSG.mean <- PlanTraits2$WSG.mean/1 # Conversion from g.cm-3 to mg.mm-3

#= Fill up species trait data-base with PlanTraits
ListSpeciesPlanTraits <- levels(as.factor(PlanTraits2$Latin_name))
countsup.LeafN <- countsup.SLA <- countsup.WSG <- 0 # Counter to see how much new trait values come from PlanTraits
for (i in 1:nrow(data.species)) {
    if (is.na(data.species$LeafN[i]) & (data.species$Species[i] %in% ListSpeciesPlanTraits)) {
        data.species$LeafN[i] <- PlanTraits2$LeafN.mean[PlanTraits2$Latin_name==data.species$Species[i]]
        data.species$LeafN.sd[i] <- PlanTraits2$LeafN.sd[PlanTraits2$Latin_name==data.species$Species[i]]
        if (!is.na(data.species$LeafN[i])) {countsup.LeafN <- countsup.LeafN+1}
    }
    if (is.na(data.species$SLA[i]) & (data.species$Species[i] %in% ListSpeciesPlanTraits)) {
        data.species$SLA[i] <- PlanTraits2$SLA.mean[PlanTraits2$Latin_name==data.species$Species[i]]
        data.species$SLA.sd[i] <- PlanTraits2$SLA.sd[PlanTraits2$Latin_name==data.species$Species[i]]
        if (!is.na(data.species$SLA[i])) {countsup.SLA <- countsup.SLA+1}
    }
    if (is.na(data.species$WSG[i]) & (data.species$Species[i] %in% ListSpeciesPlanTraits)) {
        data.species$WSG[i] <- PlanTraits2$WSG.mean[PlanTraits2$Latin_name==data.species$Species[i]]
        data.species$WSG.sd[i] <- PlanTraits2$WSG.sd[PlanTraits2$Latin_name==data.species$Species[i]]
        if (!is.na(data.species$WSG[i])) {countsup.WSG <- countsup.WSG+1}
    }
}

countsup.LeafN # 17
countsup.SLA # 1
countsup.WSG # 1

#========================================
# Mean trait values by genus to fill gaps
data.species$LeafN.gen <- data.species$LeafN
data.species$SeedMass.gen <- data.species$SeedMass
data.species$SLA.gen <- data.species$SLA
data.species$WSG.gen <- data.species$WSG

for (i in 1:nrow(data.species)) {
    if (is.na(data.species$LeafN[i])) {
        data.species$LeafN.gen[i] <- data.genus$LeafN[data.genus$Genus==data.species$Genus[i]]
    }
    if (is.na(data.species$SeedMass[i])) {
        data.species$SeedMass.gen[i] <- data.genus$SeedMass[data.genus$Genus==data.species$Genus[i]]
    }
    if (is.na(data.species$SLA[i])) {
        data.species$SLA.gen[i] <- data.genus$SLA[data.genus$Genus==data.species$Genus[i]]
    }
    if (is.na(data.species$WSG[i])) {
        data.species$WSG.gen[i] <- data.genus$WSG[data.genus$Genus==data.species$Genus[i]]
    }
}

#===================================
# Species code

#= Clean taxonomy (from Fabrice Bénedet) 
207
species.clean <- read.csv("./data/raw/Paracou/20130717_paracou_taxonomie.csv",stringsAsFactors=FALSE, header = T, sep = ";")
208
209
210
species.clean$sp <- species.clean[["idTaxon"]] # Column "sp" is the idTaxon code
species.clean$Latin_name <-  paste(species.clean[["Genre"]],species.clean[["Espece"]],sep=" ")
# Keep only one row pers idTaxon
211
212
species.clean <- subset(species.clean,subset=!duplicated(species.clean[["sp"]]),
                        select=c("sp","Latin_name","Genre","Espece","Famille","idCIRAD"))
Georges Kunstler's avatar
Georges Kunstler committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Get species code
sum(data.species$Species %in% species.clean$Latin_name)

### need to test a fuzzy grep agrep to see if we get more species
fun.get.name <-  function(species,species.clean){
species <- gsub("IND","Indet.",species)    
res <- (1:nrow(species.clean))[ tolower(species.clean$Latin_name)==tolower(species)]
if(length(res)==1){return(return(species.clean$Latin_name[res]))
   }else{
    res <- agrep(species,species.clean$Latin_name,ignore.case=TRUE,max.distance = 0.001)
    if (length(res)==1){return(species.clean$Latin_name[res])}
    if (length(res)>1){return(NA)
                       print(paste("more than one match",species,"for",paste(species.clean$Latin_name[res],collapse=" ")))
                   }
    if (length(res)<1){return(NA)}

   } 
}

Lat.name.good <-unlist( sapply(as.character(data.species$Species),
                               fun.get.name,species.clean))
sum(!is.na(Lat.name.good)) # 368 species with ID taxon
data.species$Latin_name <- Lat.name.good
data.species$Latin_name[is.na(Lat.name.good)] <- as.character(data.species$Species)[is.na(Lat.name.good)]

238

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
## #===================================
## # How many species have trait values
## nspecies
## # Obs at the species level
## nsp.LeafN <- sum(!is.na(data.species$LeafN))
## nsp.SeedMass <- sum(!is.na(data.species$SeedMass))
## nsp.SLA <- sum(!is.na(data.species$SLA))
## nsp.WSG <- sum(!is.na(data.species$WSG))
## nsp.AllTraits <- sum(!is.na(data.species$LeafN) & !is.na(data.species$SeedMass) & !is.na(data.species$SLA) & !is.na(data.species$WSG))
## # Including mean by genus
## nsp.LeafN.gen <- sum(!is.na(data.species$LeafN.gen))
## nsp.SeedMass.gen <- sum(!is.na(data.species$SeedMass.gen))
## nsp.SLA.gen <- sum(!is.na(data.species$SLA.gen))
## nsp.WSG.gen <- sum(!is.na(data.species$WSG.gen))
## nsp.AllTraits.gen <- sum(!is.na(data.species$LeafN.gen) & !is.na(data.species$SeedMass.gen) & !is.na(data.species$SLA.gen) & !is.na(data.species$WSG.gen))
## # Summary in a matrix
## matsum <- as.data.frame(matrix(nrow=2,ncol=6))
## names(matsum) <- c("Total","LeafN","SeedMass","SLA","WSG","AllTraits")
## matsum[1,] <- c(nspecies,nsp.LeafN,nsp.SeedMass,nsp.SLA,nsp.WSG,nsp.AllTraits)
## matsum[2,] <- c(nspecies,nsp.LeafN.gen,nsp.SeedMass.gen,nsp.SLA.gen,nsp.WSG.gen,nsp.AllTraits.gen)
## ## sink("Summary_Traits_Paracou.txt")
## ## matsum
## ## sink()

## #===============================================
## # How many species have trait values and IdTaxon
## nspecies <- sum(!is.na(data.species$sp))
## # Obs at the species level
## nsp.LeafN <- sum(!is.na(data.species$LeafN) & !is.na(data.species$sp))
## nsp.SeedMass <- sum(!is.na(data.species$SeedMass) & !is.na(data.species$sp))
## nsp.SLA <- sum(!is.na(data.species$SLA) & !is.na(data.species$sp))
## nsp.WSG <- sum(!is.na(data.species$WSG) & !is.na(data.species$sp))
## nsp.AllTraits <- sum(!is.na(data.species$LeafN) & !is.na(data.species$SeedMass) & !is.na(data.species$SLA) & !is.na(data.species$WSG) & !is.na(data.species$sp))
## # Including mean by genus
## nsp.LeafN.gen <- sum(!is.na(data.species$LeafN.gen) & !is.na(data.species$sp))
## nsp.SeedMass.gen <- sum(!is.na(data.species$SeedMass.gen) & !is.na(data.species$sp))
## nsp.SLA.gen <- sum(!is.na(data.species$SLA.gen) & !is.na(data.species$sp))
## nsp.WSG.gen <- sum(!is.na(data.species$WSG.gen) & !is.na(data.species$sp))
## nsp.AllTraits.gen <- sum(!is.na(data.species$LeafN.gen) & !is.na(data.species$SeedMass.gen) & !is.na(data.species$SLA.gen) & !is.na(data.species$WSG.gen) & !is.na(data.species$sp))
## # Summary in a matrix
## matsum <- as.data.frame(matrix(nrow=2,ncol=6))
## names(matsum) <- c("Total","LeafN","SeedMass","SLA","WSG","AllTraits")
## matsum[1,] <- c(nspecies,nsp.LeafN,nsp.SeedMass,nsp.SLA,nsp.WSG,nsp.AllTraits)
## matsum[2,] <- c(nspecies,nsp.LeafN.gen,nsp.SeedMass.gen,nsp.SLA.gen,nsp.WSG.gen,nsp.AllTraits.gen)
## ## sink("Summary_Traits_Paracou_idTaxon.txt")
## ## matsum
## ## sink()
286
287
288
289

#================
# Output

Georges Kunstler's avatar
Georges Kunstler committed
290
291


292
293
294
295
296
297
# Change the column names to adapt to Georges conventions
names(data.species) <- c("Species","Genus",
                         "Leaf.N.mean","Leaf.N.sd","Leaf.N.n",
                         "Seed.mass.mean","Seed.mass.sd","Seed.mass.n",
                         "SLA.mean","SLA.sd","SLA.n",
                         "Wood.density.mean","Wood.density.sd","Wood.density.n",
Georges Kunstler's avatar
Georges Kunstler committed
298
                         "Leaf.N.gen","Seed.mass.gen","SLA.gen","Wood.density.gen","Latin_name")
299

300
301
data.species$Max.height.mean <- data.species$Max.height.sd <- data.species$Max.height.genus <- NA

Georges Kunstler's avatar
Georges Kunstler committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
##### FORMAT TRAIT FOR BCI
source("R/find.trait/trait.fun.R")

### read species names
data.tree <- read.csv("output/formatted/Paracou/tree.csv", stringsAsFactors = FALSE)
species.clean2 <- data.frame(sp=data.tree[!duplicated(data.tree[["sp"]]),"sp"],
                             Latin_name=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             Latin_name_syn=data.tree[!duplicated(data.tree[["sp"]]),"sp.name"],
                             stringsAsFactors =FALSE)


## extract
data.traits <- fun.extract.format.sp.traits.NOT.TRY(sp=species.clean2$sp, Latin_name=species.clean2$Latin_name, data=data.species,name.match.traits="Latin_name")

Georges Kunstler's avatar
Georges Kunstler committed
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#### GET THE ANGIO/CONIF AND EVERGREEN/DECIDUOUS
# read try categrocial data
try.cat <- read.csv("data/raw/TRY/TRY_Categorical_Traits_Lookup_Table_2012_03_17_TestRelease.csv",
         stringsAsFactors=FALSE,na.strings = "")
Pheno.Zanne <- read.csv("data/raw/ZanneNature/GlobalLeafPhenologyDatabase.csv",
         stringsAsFactors=FALSE)
# extract
data.cat.extract <- do.call("rbind",lapply(data.traits$sp ,fun.get.cat.var.from.try,
                                           data.traits,try.cat,Pheno.Zanne))
# change category
data.cat.extract <- fun.change.factor.pheno.try(data.cat.extract)
data.cat.extract <- fun.change.factor.angio.try(data.cat.extract)
data.cat.extract <- fun.fill.pheno.try.with.zanne(data.cat.extract)


data.traits <- merge(data.traits,data.cat.extract[,c("sp","Phylo.group","Pheno.T")],by="sp")


335
# write.table(data.genus,file="data.genus.txt",sep="\t",row.names=FALSE)
Georges Kunstler's avatar
Georges Kunstler committed
336
write.csv(data.traits,file="./output/formatted/Paracou/traits.csv",row.names=FALSE)
337