Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / md5.py
Last active January 15, 2021 12:11
[Generate md5sum of file] Generate MD5 sum for a given file path #python
import hashlib
def md5(fname):
"""
returns the md5 for a filepath
"""
BLOCKSIZE = 65536
hasher = hashlib.md5()
with open(fname, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
@simonthompson99
simonthompson99 / run-query.r
Last active June 10, 2021 08:47
[Run query from file] Run query from sql file on specific connection #r #sql #database
run_query <- function(conn, f) {
# run the contents of sql file - f- on a given db connection - conn
require(DBI)
q <- trimws(readLines(f))
# remove lines starting with --
q <- gsub("^--.*$", "", q)
d <- dbGetQuery(conn, paste(q, collapse = " "))
cat(paste0(f, ' - ', nrow(d), 'Rx', ncol(d), 'C\n'))
return(d)
}
@simonthompson99
simonthompson99 / update-query-with-join.sql
Last active January 15, 2021 12:09
[Update query with join] Update db table based on joins #sql #database
-- update a table using a standard query
with t as (
select <the primary key of the table> as rowid, <value to use to update> as update_value
from <table>
left join ....
where ....
)
update <table to update>
set <field to update> = t.update_value
from t
@simonthompson99
simonthompson99 / send-file-to-slack.py
Last active January 15, 2021 12:07
[Send file to Slack] Upload a file to slack channel #python #slack
def send_file_to_slack(fp, channel, fn = None):
"""
Send a file to a slack channel
- fp - path to file
- channel - channel to send to
- fn - the filename
"""
import requests
import os