Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scripting-cookbook

A reference collection of the most common, everyday R, Python, Bash, and SQL patterns — the kind of thing you look up every few months and never quite remember the exact syntax for. Every file is a runnable, self-contained script (not just markdown snippets), and every one of them is actually executed on every push via CI, so nothing here is "the internet says this should work" — it's all been verified to actually run.

CI

Why this exists

Most of the other repos in this portfolio (germline-variant-calling-nf, rnaseq-diffexp, SHARE_seq) are full end-to-end pipelines. This one is the opposite: small, individually runnable examples of the day-to-day language mechanics that those pipelines are built out of — useful as a personal quick-reference.

Contents

R (r/)

File Covers
01_data_structures_and_io.R Vectors, lists, data.frames, indexing ([, [[, $), control flow, NA handling, CSV read/write
02_dplyr_tidyr_basics.R filter/select/mutate/arrange, group_by/summarize, joins, pivot_longer/pivot_wider
03_strings_regex_apply.R paste/sprintf, regex (grepl/gsub/regmatches), the apply family (sapply/lapply/vapply/mapply/apply)
04_plotting_ggplot2.R ggplot() grammar of graphics: scatter, boxplot, faceted bar chart
05_functions_and_stats.R Writing functions (defaults, ..., closures), vectorized vs. loop performance, t.test()/lm()

Python (python/)

File Covers
01_data_structures_and_io.py Lists/dicts/sets/tuples, comprehensions, generator expressions, file/CSV I/O
02_pandas_basics.py DataFrame filtering, groupby/agg, merge, pivoting, CSV round-trip
03_strings_regex.py f-strings/.format(), string methods, the re module (match vs. search, findall, sub)
04_functions_classes_errors.py Default args, *args/**kwargs, @dataclass, try/except/finally
05_files_paths_and_cli.py pathlib, subprocess (calling external tools), argparse

Bash (bash/)

File Covers
01_variables_and_control_flow.sh Variables/quoting, set -euo pipefail, if/for/while, functions, exit codes
02_strings_and_arrays.sh Parameter expansion (${var%...}, ${var/.../...}), indexed arrays, splitting delimited strings
03_file_and_text_processing.sh find, grep, cut, sed, awk, sort/uniq
04_pipes_redirection_process_mgmt.sh Redirection (>, >>, 2>&1, <), pipes, xargs, background jobs/wait, trap

SQL (sql/)

All against a small SQLite sample database (00_setup_sample_db.sql) with samples/genes/variants tables, so joins and aggregations have something realistic to operate on. Run bash sql/run_all.sh to build the database fresh and execute every query file against it in order.

File Covers
00_setup_sample_db.sql Schema (PRIMARY KEY/FOREIGN KEY/CHECK/UNIQUE constraints) + seed data
01_basic_queries.sql SELECT/WHERE/ORDER BY/LIMIT/DISTINCT/LIKE/IN/BETWEEN
02_joins.sql INNER JOIN, LEFT JOIN, RIGHT JOIN
03_aggregation_and_grouping.sql COUNT/SUM/AVG, GROUP BY, HAVING vs. WHERE
04_subqueries_and_ctes.sql Scalar/correlated subqueries, WITH CTEs
05_window_functions.sql ROW_NUMBER/RANK/DENSE_RANK, LAG/LEAD, running totals
06_indexes_and_schema_design.sql CREATE INDEX, EXPLAIN QUERY PLAN, foreign key enforcement

Running things

git clone https://github.com/Zach-Girard/scripting-cookbook.git
cd scripting-cookbook

# Run everything (what CI runs on every push)
bash run_all.sh

# Or run a single file directly
Rscript r/02_dplyr_tidyr_basics.R
python3 python/02_pandas_basics.py
bash bash/03_file_and_text_processing.sh
sqlite3 /tmp/cookbook.db < sql/00_setup_sample_db.sql && sqlite3 /tmp/cookbook.db < sql/02_joins.sql

Requirements: R (with dplyr, tidyr, ggplot2), Python 3 (with pandas), Bash, and sqlite3. See .github/workflows/ci.yml for exact setup steps.

A note on Bash portability

bash/02_strings_and_arrays.sh deliberately avoids associative arrays (declare -A), which need bash 4+. macOS ships bash 3.2 by default (Apple stopped bundling newer bash for licensing reasons), so code that assumes bash 4+ features can work everywhere else and then mysteriously break on a Mac. All scripts here are written to run correctly on both bash 3.2 and modern bash, with a comment at the one place that matters explaining the difference.

Skills demonstrated

R (base + tidyverse) · Python (stdlib + pandas) · Bash/POSIX shell scripting · SQL (joins, aggregation, subqueries, CTEs, window functions, indexing) · writing runnable, tested reference code rather than untested snippets · CI that actually executes every example on every push.

About

Runnable, CI-tested reference examples for common R, Python, Bash, and SQL practices (data structures, dplyr/pandas, regex, joins/CTEs/window functions, and more).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages