Skip to content

Instantly share code, notes, and snippets.

View dmyersturnbull's full-sized avatar

Douglas Myers-Turnbull dmyersturnbull

  • Stanford University
  • Stanford, CA
View GitHub Profile
@dmyersturnbull
dmyersturnbull / chemspider_unique_search.py
Last active January 20, 2017 04:12
Fetch unique compounds from ChemSpider.
# Douglas Myers-Turnbull wrote this while at UCSF. Because of this, the list of copyright owners is unknown and is not licensed (sorry!).
import chemspipy
from chemspipy import ChemSpider
import warnings
from typing import Iterable, Mapping, Optional
import warnings
import time
# use your API key for fetching from ChemSpider
@dmyersturnbull
dmyersturnbull / converge.py
Created March 28, 2016 17:15
Repeatedly sample something until the mean (or other statistic) converges to within ε.
import warnings
import numpy as np
from typing import Iterable, Mapping, Callable, Any, Tuple
def converge(sampler: Callable[[None], Iterable[float]],
statistic:Callable[[np.ndarray], float]=np.mean,
ε:float=0.01, min_iters:int=3, max_iters:int=50,
noter:Callable[[int, float, float, Iterable[float]], Any]=lambda i, estimate, delta, samples: print('Iteration {}: {:.3f}, δ=={:.3f}'.format(i, estimate, delta))
) -> Tuple[float, Iterable[float]]:
"""Repeatedly sample something until the mean (or other statistic) converges to within ε.
@dmyersturnbull
dmyersturnbull / silenced.py
Last active August 16, 2016 17:29
Silence stdout and/or stderr in Python 3 using with statement
import contextlib
import sys
from io import StringIO
@contextlib.contextmanager
def silenced(no_stdout=True, no_stderr=True):
"""
Suppresses output to stdout and/or stderr.
Always resets stdout and stderr, even on an exception.
Usage:
@dmyersturnbull
dmyersturnbull / uniprot_go_terms_at_level.py
Last active March 22, 2016 19:25
Get all of the GO terms at the specified level and type associated with a UniProt ID. Uses Python 3.5 Typing.
# Requires https://gist.github.com/dmyersturnbull/efe32052bf4cf06df915
import pandas as pd
from typing import Iterable, Union, Mapping
from goatools import obo_parser # uses https://github.com/tanghaibao/goatools
from goatools.obo_parser import GOTerm # NOT the same as FlatGoTerm, which has no knowledge of hierarchy
if not os.path.exists('gene_ontology.1_2.obo'):
import wget
@dmyersturnbull
dmyersturnbull / uniprot_go_terms.py
Last active June 18, 2024 17:13
Build a Pandas DataFrame of Gene Ontology term objects from a UniProt ID. Uses Python 3.5 typing.
# Requires https://gist.github.com/dmyersturnbull/c0717406eb46158401a9:
from silenced import silenced
import re
import uniprot # from https://github.com/boscoh/uniprot
import pandas as pd
import contextlib
import sys
from io import StringIO
from typing import Iterable, Union, Mapping
@dmyersturnbull
dmyersturnbull / UnorderedPair.scala
Created March 21, 2016 20:29
Unordered pair in Scala
case class UnorderedPair[A](a: A, b: A) {
override def equals(o: Any) = o match {
case that: UnorderedPair[A] => that.a == a && that.b == b || that.a == b && that.b == a
case _ => false
}
override def hashCode = a.hashCode * b.hashCode // commutative and unique
}