Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@simonthompson99
simonthompson99 / send-slack-msg.py
Last active January 15, 2021 12:12
[Send message to Slack] Send a message to slack channel #python #slack
def send_slack_msg(m, api_token, channel):
"""
Send a message to a slack channel, requires a valid API token
"""
import requests
data = {
'token': api_token,
'channel':channel,
'as_user': True,
'text':m
@simonthompson99
simonthompson99 / dates-in-r.md
Last active January 15, 2021 12:13
[Datetime Cheatsheet] Date and time cheatsheet for R #r #cheatsheet

To format a date into a string - format(<date>, <format_string>)

To read in dates in a format - as.Date(<date_string>, format = <format_string>)

Symbol Meaning Example
%d day as a number (0-31) Jan-31
%a abbreviated weekday Mon
%A unabbreviated weekday Monday
%m month (00-12) 00-12
@simonthompson99
simonthompson99 / pdftk-one-liners.sh
Last active January 15, 2021 12:15
[pdftk one-liners] PDF toolkit one-liners #pdftk #oneliner
# Combine pdfs in afolder into a single file
pdftk *.pdf cat output combined.pdf
# Extract single page r2 (reverse 2) gives penultimate page
pdftk in.pdf cat r2 output penultimatepage.pdf
# Split multipage doc into single pages
pdftk in.pdf burst
# remove first page from multiple pdfs
for file in *.pdf ; do pdftk "$file" cat 1 output "pg-1-${file%.pdf}.pdf" ; done
# get number of pages in pdf
pdftk in.pdf dump_data | grep NumberOfPages
@simonthompson99
simonthompson99 / local_pg_mgmt.sql
Last active October 15, 2021 10:21
[Refresh local postgres] Drop databases, add cdt_user role and create new databases on local postgres #sql #database #workflow
/*
Fresh install:
brew install postgresql
brew services start postgresql
simonthompson is the superuser, no password has been created for them.
If wiping:
@simonthompson99
simonthompson99 / jira-ticket-creation-urls.md
Last active January 15, 2021 12:18
[Creating urls to create JIRA ticket] How to create urls for creating JIRA tickets #jira #html
  1. Stem of the link is the jira instance ticket will be created in i.e. https://jiraservicedesk.extge.co.uk or https://jira.extge.co.uk;
  2. Then /secure/CreateIssueDetails!init.jspa? if you want them to add info, or if you just want the ticket created remove !init (will still need them to click on 'Retry Operation'. Can get around this by adding in token (Dimitris from Valiantys to advise on how to get this)
  3. Then list of key=value pairs separated by &. Easiest way to get these is by creating a ticket and then looking at what comes back from API (i.e. visit <url stem>/rest/api/2/issue/<issue-key>. Often a bit of trial and error, replace spaces with %20, use ID numbers for things like project (pid) or issuetype.

Examples: https://jiraservicedesk.extge.co.uk/secure/CreateIssueDetails!init.jspa?pid=10102&issuetype=10608&customfield_10799=test&summary=DQ%20Report%20Rule%20Failure%20Override

`https://jira.extge.co.uk/secure/CreateIssueDetails!init.jspa?reporter=sthompson&issuetype=3&assignee=

@simonthompson99
simonthompson99 / ggplot-commands.r
Last active January 15, 2021 12:19
[ggplot Cheatsheet] Some ggplot commands #r #ggplot #cheatsheet
#-- stacked bar plot of proportions with totals above
# d is listing of single row per participants with eth_cat and quarter columns
# totals is d %>% group_by(quarter) %>% summarise(total = n())
ggplot(d, aes(fill = eth_cat, x = quarter)) +
geom_bar(position = "fill") +
scale_fill_brewer(palette = "RdYlBu") +
labs(title = "Participant ethnicity per quarter of 100,000 Genomes Project",
x = "Quarter",
y = "Proportion",
fill = "Ethnicity",
@simonthompson99
simonthompson99 / install-old-package.md
Last active January 20, 2021 12:25
[Install old version of R package] Install an older version of a particular package #r #environment
  1. download old .tar.gz from the package archive
  2. run R CMD INSTALL .tar.gz or to get the dependencies installed run install.packages(".tar.gz", repos = NULL, type = "source")
@simonthompson99
simonthompson99 / dplyr.r
Last active January 15, 2021 12:20
[dplyr Cheatsheet Some commands for dplyr #r #dplyr #cheatsheet
#-- convert df to tibble
tbl <- tbl_df(df)
#-- aggregate by multiple groups
d <- df %>% filter(<filter_conditional>) %>%
group_by(<grouping_variable1>, ...) %>%
summarise(<out_var> = <out_var_function_call>,...)
#-- get crosstabs table and replace low counts
tab_ldp <- d %>%
@simonthompson99
simonthompson99 / get-latest-rds.r
Last active January 15, 2021 12:21
[Get Latest rds File] Get the latest rds file from a a directory with time-stamped files #r
#-- get latest rds file from filelisting with datestamps
get_latest_rds <- function(d, kw){
ls <- list.files(d, pattern = "*.rds", full.names = T)
f <- sort(ls[grepl(kw, ls)], decreasing = T)[1]
d <- readRDS(f)
cat(paste("Fetched", f, "--", nrow(d), "rows", ncol(d), "columns\n"))
return(d)
}
@simonthompson99
simonthompson99 / transfer-db-to-local.sh
Last active January 15, 2021 12:21
[Transfer db from index to local] Dump, transfer, then load database from index to local #sql #database #genomics_england
-- on query
pg_dump -h 10.1.24.39 -p 5433 --schema <schema-name> <db_name> > ~/scratch/<dump_file>.sql
-- local
cd ~/scratch
scp sthompson@10.1.24.38:~/scratch/<dump_file>.sql .
sed -i .bak 's/OWNER TO cdt_user;/OWNER TO simon;/g' <dump_file>.sql
psql -d <target_db> -h localhost -p 5432 -U simon < <dump_file>.sql