Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@simonthompson99
simonthompson99 / pdf-to-png.sh
Last active January 15, 2021 12:22
[Convert pdf to pngs] Convert pdf to numbered png images #imagemagick #bash #file_operations
#-- convert pdf into single page png images with p01 numbering
for file in *.pdf; do
echo 'converting $file'
convert -density 400 -colorspace sRGB $file -scene 1 -alpha off ${file%.pdf}_p%02d.png;
done
@simonthompson99
simonthompson99 / git-oneliners.sh
Last active March 4, 2021 16:53
[git Oneliners] git one-liners #git #oneliner
# rename repository
# change name on remote repo, copy ssh url from the clone button on main page
mv <old folder> <new folder>
git remote set-url origin git@github.com:genomicsengland/<new repo name>
# compare local copy of file to remote
git fetch origin
git diff origin/master -- <path to file>
# list all branches
@simonthompson99
simonthompson99 / vim-oneliners.txt
Last active August 8, 2023 23:25
[VIM one-liners] Useful VIM one-liners #vim #oneliner
# open up current file (useful for opening markdowns in chrome)
:!open %
# search across folder tree for test
# From within buffer at top most level of folder tree to search
:grep '<searchstring>' **/*.<fileending to search> or grep -R <searchstring> . to recursievely search all files in project
:copen to open the quickfix list, and :ccl to close it
:cn to go to the next on the quickfix list
:cp to go to the previous
# OR
@simonthompson99
simonthompson99 / sql-one-liners.sql
Last active January 15, 2021 12:24
[SQL one-liners] SQL one-liners #sql #oneliner #database
-- pg dump of a specific schema from metrics db on index
pg_dump metrics -h 10.1.24.39 -p 5433 -U cdt_user -n <schema_name> > <out>.sql
-- select a random percentage of rows of table
select * from my_table tablesample bernoulli(<percentage in numbers, e.g. 10>)
-- or to select random rows in query
select * from query order by random() limit 100
-- useful aggregate functions to use with GROUP BY
string_agg(<field>, ';') /* equivalent to R's paste(<field>, collapse = ";") */
@simonthompson99
simonthompson99 / jenkins-cron.txt
Last active January 15, 2021 12:25
[Jenkins CRON commands] Commands for scheduling Jenkins jobs at particular times #jenkins
# run three times a day (between 9am and 4pm), Mon - Fri
H H(9-16)/3 * * 1-5
@simonthompson99
simonthompson99 / batch-git-mv.sh
Last active January 15, 2021 12:26
[Batch git mv] Batch rename files with git mv #bash #git
# batch renames files with hyphens to underscores
for i in `ls *-*`
do
NEW=`echo $i|tr '-' '_'`
git mv $i $NEW
done
# or do sed on the filename with multiple options
for i in `ls *.sql`
do
@simonthompson99
simonthompson99 / imagemagick-one-liners.sh
Last active February 12, 2021 17:13
[Imagemagick One-liners] Onelines for Imagemagick for image manipulation #imagemagick #oneliner
# make white transparent
convert <in>.png -transparent white <out>.png
# change image to a4-sized pdf
convert -density 300 -page a4 <in>.png <out>.pdf
# remove large white backgrounds from multiple images
mogrify -trim +repage *.png
# make image grayscale
@simonthompson99
simonthompson99 / send-email.py
Last active January 15, 2021 12:27
[Send Email Over SMTP] Sends email over smtp, works for qmul email, but not NHS #python #email
# set up the SMTP server
# for qmul:
# host = smtp-mail.outlook.com
# port = 587
# username, password = full hh..@qmul.ac.uk email address
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@simonthompson99
simonthompson99 / read-template-file.py
Last active January 15, 2021 12:28
[Read template file] Read a text file and return it as a Template object #python
# Template objects allows you to add ${FIELD_NAME} tags into the file
# and then values can be substituted with
# template_object.substitute(FIELD_NAME = <field_value>)
def read_template(filename):
"""
Returns a Template object comprising the contents of the
file specified by filename.
"""
import Template
with open(filename, 'r', encoding='utf-8') as template_file:
@simonthompson99
simonthompson99 / send-df-to-slack.r
Last active January 15, 2021 12:30
[Send Dataframe to Slack] Send a dataframe to be rendered as table in Slack #r #slack
#-- use the CDT Bot API token see https://cnfl.extge.co.uk/display/CDT/Generic+Login+Credentials+Information#GenericLoginCredentialsInformation-SlackCDTBotUser
#-- function to send df (d) to a specific channel with optional message, will break up if there are too many rows
send_df_to_slack_as_msg <- function(d, channel, api_token, msg = NA){
require(knitr)
require(slackr)
slackr_setup(channel = channel,
api_token = api_token)
d <- kable(d, format = 'rst')
if(!is.na(msg)){
slackr_msg(msg)