Skip to content

Instantly share code, notes, and snippets.

View glwinsor's full-sized avatar

Geoff Winsor glwinsor

  • Simon Fraser University
  • Burnaby, BC, Canada
View GitHub Profile
@glwinsor
glwinsor / replace_extensions.sh
Created January 29, 2020 19:27
Bulk replace file extensions from .txt to .gbk
# Rename all *.txt to *.text
for f in *.txt; do
mv -- "$f" "${f%.txt}.gbk"
done
@glwinsor
glwinsor / system_call_capture_stdout.py
Last active August 27, 2018 20:12
[system call and capture STDOUT]
#! /usr/bin/python3
from subprocess import Popen,PIPE,STDOUT
pmidString=''
command='/home/glwinsor/edirect/efetch -db pubmed -id 153904'
#captures STDOUT without having to use a redirect to a filename
#note that it captures bytes, not a string. Therefore need to specify universtal_newlines=True in order to retain newlines
@glwinsor
glwinsor / writefile.py
Last active May 24, 2018 16:36
[write to file]
#!/usr/bin/python3
f = open('filename.txt', 'w')
f.write()
f.close()
@glwinsor
glwinsor / connect.py
Created May 23, 2018 23:41
[python3 db connection] Connect to mysql using python3 #pymysql
from optparse import OptionParser
import pymysql
# Get options from the command line
parser = OptionParser()
parser.add_option("-d", "--database", dest="database", action="store", type="string", help="Name of database you wish to connect to", metavar="DATABASE")
parser.add_option("-H", "--host", dest="host", action="store", type="string", help="Database host", metavar="HOST")
parser.add_option("-u", "--user", dest="user", action="store", type="string", help="User name", metavar="NAME")
parser.add_option("-p", "--password", dest="password", action="store", type="string", help="Password", metavar="PASSWORD")
@glwinsor
glwinsor / fasta_reader.py
Created May 22, 2018 23:25
[read fasta] Reads in a fasta file using biopython and prints the sequence id and sequence string #biopython
from Bio import SeqIO
# read in the fasta file and create seq object. File name is stored in fasta variable and the following "fasta" in quotes means the type of format being input
for seq_record in SeqIO.parse(fasta,"fasta"):
# Prints the sequence ID
print(seq_record.id)
# Prints string representation of the sequence
print(str(seq_record.seq))
@glwinsor
glwinsor / openfile.py
Last active May 22, 2018 23:07
open and read file
import sys
from optparse import OptionParser
try:
# assumes file was specified as argument in the command line using optparse with --file option
file=open(options.file, 'r')
except FileNotFoundError: # works for python3
print("Could not find file")
sys.exit()
@glwinsor
glwinsor / exists.py
Last active May 31, 2018 16:36
file or directory exists
import os
if not os.path.isfile("./file.txt"):
raise FileNotFoundError("Could not find ./file.txt")
if not os.path.isdir('./dir'):
raise FileNotFoundError("Could not find ./dir")
@glwinsor
glwinsor / optparse.py
Last active May 22, 2018 21:48
optparse get options
from optparse import OptionParser
# Get options from the command line
parser = OptionParser()
parser.add_option("-f", "--flag", dest="flag", action="store", type="string", help="Description of flag", metavar="FLAG")
(options, args) = parser.parse_args()
if options.flag == None:
parser.error("Missing argument for --flag ")
@glwinsor
glwinsor / read_config.pl
Last active May 19, 2018 00:22
[config file] Read simple config file (key=value pairs) and assign to hash
// vim: syntax=perl
my %CONF;
open my $configfile, '<', 'config.txt' or die $!;
while (<$configfile>) {
chomp;
my ( $key, @value ) = split( "=", $_ );
$CONF{$key} = join '=', @value;
}
@glwinsor
glwinsor / create-mysql-connection.pl
Last active May 19, 2018 00:03
mysql db handle from command line params
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use DBI;
my $database = '';
my $password = '';
my $user = '';