-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code and data for each species in the CAPS table #2
- Loading branch information
Showing
2 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ebcf7b28-b2b2-4fff-89b9-692f55207cee", | ||
"metadata": {}, | ||
"source": [ | ||
"# Fire response traits in plants from field samples\n", | ||
"\n", | ||
"This script contains examples of R code to query tables in the database\n", | ||
"\n", | ||
"## Load libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "f24bd72f-1086-4c75-a7b8-395ac8eb79ce", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Loading required package: DBI\n", | ||
"\n", | ||
"\n", | ||
"Attaching package: ‘dplyr’\n", | ||
"\n", | ||
"\n", | ||
"The following objects are masked from ‘package:stats’:\n", | ||
"\n", | ||
" filter, lag\n", | ||
"\n", | ||
"\n", | ||
"The following objects are masked from ‘package:base’:\n", | ||
"\n", | ||
" intersect, setdiff, setequal, union\n", | ||
"\n", | ||
"\n", | ||
"Loading required package: tidyr\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"library(RPostgreSQL)\n", | ||
"library(ggplot2)\n", | ||
"##library(forcats)\n", | ||
"library(dplyr)\n", | ||
"#library(data.table)\n", | ||
"require(tidyr)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "108e8a20-008a-45c8-8941-80f815b790f1", | ||
"metadata": {}, | ||
"source": [ | ||
"## Connect to the database" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "9cf99ffd-c436-4c2f-b575-71f6e5b25db2", | ||
"metadata": {}, | ||
"source": [ | ||
"Read database credentials" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "5759a6bf-b2c5-42e2-80a0-3cf57cdc897f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if (file.exists(\"../secrets/database.ini\")) {\n", | ||
" tmp <- readLines(\"../secrets/database.ini\")[-1]\n", | ||
" tmp <- strsplit(tmp,'=',fixed=2)\n", | ||
" dbinfo <- unlist(lapply(tmp,function(x) x[2]))\n", | ||
" names(dbinfo) <- unlist(lapply(tmp,function(x) x[1]))\n", | ||
" dbinfo <- data.frame(t(dbinfo),stringsAsFactors=F)\n", | ||
" rm(tmp)\n", | ||
"} else {\n", | ||
" cat(\"No database information found\")\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8ade77df-7be2-493f-bb25-6e9e0b6fa6cb", | ||
"metadata": {}, | ||
"source": [ | ||
"Connection to the postgresql server (remember to update .pgpass file)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "f50fdfe2-2932-46fe-a483-f2bc29714bc6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"drv <- dbDriver(\"PostgreSQL\") ## \n", | ||
"con <- dbConnect(drv, dbname = dbinfo$database,\n", | ||
" host = dbinfo$host, port = dbinfo$port,\n", | ||
" user = dbinfo$user)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "e7a36330-4a00-4a82-8566-71fa84b48ef9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"qry <- \n", | ||
"'SELECT family, genus, \"speciesID\", \n", | ||
"count(distinct q.record_id) as nquadrat, \n", | ||
"count(distinct g8.record_id) as germ8, \n", | ||
"count(distinct r2.record_id) as rect2,\n", | ||
"count(distinct g1.record_id) as germ1, \n", | ||
"count(distinct gw1.record_id) as grow1, \n", | ||
"count(distinct r4.record_id) as repr4, \n", | ||
"count(distinct s5.record_id) as surv5, \n", | ||
"count(distinct s6.record_id) as surv6, \n", | ||
"count(distinct s7.record_id) as surv7, \n", | ||
"count(distinct d1.record_id) as disp1, \n", | ||
"count(distinct r3a.record_id) as repr3a, \n", | ||
"count(distinct r3.record_id) as repr3, \n", | ||
"count(distinct s4.record_id) as surv4, \n", | ||
"count(distinct s1.record_id) as surv1\n", | ||
"FROM species.caps \n", | ||
"LEFT JOIN species_traits s \n", | ||
" ON \"speciesCode_Synonym\"=s.species_code::text \n", | ||
"LEFT JOIN form.quadrat_samples q \n", | ||
" ON \"speciesCode_Synonym\"=q.species_code::text \n", | ||
"LEFT JOIN litrev.germ8 g8 ON \"speciesCode_Synonym\"=g8.species_code::text \n", | ||
"LEFT JOIN litrev.rect2 r2 ON \"speciesCode_Synonym\"=r2.species_code::text \n", | ||
"LEFT JOIN litrev.germ1 g1 ON \"speciesCode_Synonym\"=g1.species_code::text \n", | ||
"LEFT JOIN litrev.grow1 gw1 ON \"speciesCode_Synonym\"=gw1.species_code::text \n", | ||
"LEFT JOIN litrev.repr4 r4 ON \"speciesCode_Synonym\"=r4.species_code::text \n", | ||
"LEFT JOIN litrev.surv5 s5 ON \"speciesCode_Synonym\"=s5.species_code::text \n", | ||
"LEFT JOIN litrev.surv6 s6 ON \"speciesCode_Synonym\"=s6.species_code::text \n", | ||
"LEFT JOIN litrev.surv7 s7 ON \"speciesCode_Synonym\"=s7.species_code::text \n", | ||
"LEFT JOIN litrev.disp1 d1 ON \"speciesCode_Synonym\"=d1.species_code::text \n", | ||
"LEFT JOIN litrev.repr3a r3a ON \"speciesCode_Synonym\"=r3a.species_code::text \n", | ||
"LEFT JOIN litrev.repr3 r3 ON \"speciesCode_Synonym\"=r3.species_code::text \n", | ||
"LEFT JOIN litrev.surv4 s4 ON \"speciesCode_Synonym\"=s4.species_code::text \n", | ||
"LEFT JOIN litrev.surv1 s1 ON \"speciesCode_Synonym\"=s1.species_code::text \n", | ||
"\n", | ||
"GROUP BY fam'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "906884e5-92c3-46c6-aba7-c5a7d6fada17", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"'data.frame':\t368 obs. of 17 variables:\n", | ||
" $ fam : chr \"Acanthaceae\" \"Acrobolbaceae\" \"Actinidiaceae\" \"Adoxaceae\" ...\n", | ||
" $ nspp : num 79 6 1 10 12 67 3 15 19 4 ...\n", | ||
" $ litrev : num 16 0 0 3 1 30 1 5 0 0 ...\n", | ||
" $ quadrat: num 1 0 0 1 0 0 1 0 0 0 ...\n", | ||
" $ germ8 : num 0 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ rect2 : num 2 0 0 0 0 2 0 0 0 0 ...\n", | ||
" $ germ1 : num 2 0 0 0 0 1 0 0 0 0 ...\n", | ||
" $ grow1 : num 0 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ repr4 : num 0 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ surv5 : num 4 0 0 0 0 4 0 0 0 0 ...\n", | ||
" $ surv6 : num 0 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ surv7 : num 0 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ disp1 : num 12 0 0 3 0 26 1 5 0 0 ...\n", | ||
" $ repr3a : num 2 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ repr3 : num 1 0 0 0 0 2 0 0 0 0 ...\n", | ||
" $ surv4 : num 4 0 0 0 0 0 0 0 0 0 ...\n", | ||
" $ surv1 : num 16 0 0 3 1 29 1 4 0 0 ...\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"my.table<- dbGetQuery(con, qry)\n", | ||
"str(my.table)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "5b64901b-b582-402f-9ba9-c01ba064f135", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"saveRDS(file='../data/Summary-traits-family.rds',my.table)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "f6c75c45-cf6c-4711-ba3e-1edede998c92", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"TRUE" | ||
], | ||
"text/latex": [ | ||
"TRUE" | ||
], | ||
"text/markdown": [ | ||
"TRUE" | ||
], | ||
"text/plain": [ | ||
"[1] TRUE" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"dbDisconnect(con)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "913933a6-a64c-4a30-9756-f2539840d9a9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "R", | ||
"language": "R", | ||
"name": "ir" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "r", | ||
"file_extension": ".r", | ||
"mimetype": "text/x-r-source", | ||
"name": "R", | ||
"pygments_lexer": "r", | ||
"version": "3.6.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Binary file not shown.