Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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
@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 / 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 / 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")