This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ε. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip, io | |
from typing import Iterator | |
def lines(file_name: str, known_encoding='utf-8') -> Iterator[str]: | |
"""Lazily read a text file or gzipped text file, decode, and strip any newline character (\n or \r). | |
If the file name ends with '.gz' or '.gzip', assumes the file is Gzipped. | |
Arguments: | |
known_encoding: Applied only when decoding gzip | |
""" | |
if file_name.endswith('.gz') or file_name.endswith('.gzip'): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MAT | |
using Lumberjack | |
@doc """ | |
Using MAT.jl, converts any MATLAB version >=5 .mat file to an HDF5-compatible MATLAB version 7 .mat file. | |
Warns if the file already exists. | |
""" -> | |
function convert_to_matlab7(input_file:: AbstractString, output_file:: AbstractString) | |
if ispath(output_file) | |
warn("File $output_file already exists") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private implicit class Paddable(string: String) { | |
/** Left-pad this string. */ | |
def ^(end: Int): String = " " * (end - string.length) + string | |
/** Right-pad this string. */ | |
def $(end: Int): String = string + " " * (end - string.length) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalacheck.Gen | |
import org.scalatest.{PropSpec, Matchers} | |
import org.scalatest.prop.PropertyChecks | |
class MinimalScalaCheckExample extends PropSpec with PropertyChecks with Matchers { | |
property("A string's length should be constant") { | |
forAll { (s: String) => | |
s.length should equal(s.length) | |
} | |
} |
OlderNewer