Skip to content

Instantly share code, notes, and snippets.

import csv
import requests
def build_ensembl_biomart_dict(dataset_name, key_attr, val_attr):
# see http://ensembl.org/biomart/martview/ for the web application
biomart_request_url_template = \
'''http://ensembl.org/biomart/martservice?query=''' \
'''<?xml version="1.0" encoding="UTF-8"?>''' \
'''<!DOCTYPE Query>''' \
'''<Query virtualSchemaName="default" formatter="CSV" header="0" uniqueRows="0" count="" datasetConfigVersion="0.6">''' \
@keithshep
keithshep / jscheats.js
Last active May 12, 2016 13:18
Some JavaScript Cheats
// forward slashes don't work well in URI components even when encoded as %2F. Many server side implementations
// still stumble on them. We can encode/decode the string with a simple scheme like:
// 'hi / there / \\ dude / \\f\\b\\\\'.replace(/\\/g, '\\b').replace(/\//g, '\\f').replace(/\\f/g, '/').replace(/\\b/g, '\\');
function encURIComp(str) {
return encodeURIComponent(str.replace(/\\/g, '\\b').replace(/\//g, '\\f'));
}
function decURIComp(str) {
return decodeURIComponent(str).replace(/\\f/g, '/').replace(/\\b/g, '\\');
@keithshep
keithshep / filedropify.js
Last active August 29, 2015 14:15
some code to simplify file drag-and-drop implementation in JavaScript
/**
* This callback will be called with the file that was dropped.
* @callback dropCallback
* @param file the file object
*/
/**
* Create a file drop area
* @param activeElement
* the element that the user can drop on to trigger a callback
* @param highlightElement
@keithshep
keithshep / csv_to_hdf5.py
Created November 5, 2014 17:33
convert CSV file to HDF5 using h5py
#!/usr/bin/env python -O
import argparse
import sys
import numpy
import h5py
import csv
class ColType:
UNKNOWN = 1
@keithshep
keithshep / cppcheats.cpp
Created September 27, 2014 03:16
c++ cheats
// creating a priority queue with a lambda less operator
auto pt_less = [&mat](const cv::Point& pt1, const cv::Point& pt2) -> bool {
return mat.at<uint8_t>(pt1) < mat.at<uint8_t>(pt2);
};
priority_queue<cv::Point, vector<cv::Point>, decltype(pt_less)> erosion_candidates(pt_less);
@keithshep
keithshep / tristate.js
Last active August 29, 2015 14:03
A little javascript class to help out with tri-state checkboxes.
/**
* A little "class" to help out with tri state checkboxes.
*/
function TriStateCheckbox(checkbox) {
var self = this;
var checkedVal = checkbox.prop("checked");
var indeterminatePropName = "indeterminate";
/**
@keithshep
keithshep / querybiomartexample.py
Created December 3, 2013 20:05
A small example for how to create XML queries for biomart using python
import requests
def main():
exampleTaxonomy = "mmusculus_gene_ensembl"
exampleGene = "ENSMUSG00000086981"
urlTemplate = \
'''http://ensembl.org/biomart/martservice?query=''' \
'''<?xml version="1.0" encoding="UTF-8"?>''' \
'''<!DOCTYPE Query>''' \
import requests
import json
# a helper function that returns the error message in case there is one and
# None otherwise
def get_err_msg(x):
if 'errorMessage' in x:
return x['errorMessage']
else:
return None
@keithshep
keithshep / hellowsgi.py
Created March 20, 2013 16:43
a minimal "hello world" wsgi application
from cgi import parse_qs, escape
def application(environ, start_response):
parameters = parse_qs(environ.get('QUERY_STRING', ''))
if 'subject' in parameters:
subject = escape(parameters['subject'][0])
else:
subject = 'World'
start_response('200 OK', [('Content-Type', 'text/html')])
return ['Hello %(subject)s' % {'subject': subject}]
@keithshep
keithshep / python-cheats.md
Last active October 13, 2020 16:53
python cheats

Angle between two vectors

Note that this doesn't give you the direction the angle sweeps in

def angle_deg_between_vecs(vec1, vec2):
    """
    Computes an angle in degrees between two vectors
    :param vec1: the first vector (numpy array of length n >= 2)
    :param vec2: the second vector (numpy array of length n >= 2)

:return: the angle in radians