Skip to content

Instantly share code, notes, and snippets.

View clintval's full-sized avatar

Clint Valentine clintval

View GitHub Profile
def is_classmethod(method: FunctionType) -> bool:
"""Determine if a method is a classmethod or not by searching for a classmethod sentinel."""
bound_to = getattr(method, "__self__", None)
if not isinstance(bound_to, type):
return False
name = method.__name__
for clazz in bound_to.__mro__:
descriptor = vars(clazz).get(name)
if descriptor is not None:
return isinstance(descriptor, classmethod)
@clintval
clintval / make-jar-executable.sh
Created April 3, 2024 21:47
Make a JAR executable
{
cat <<- EOM
#!/usr/bin/env sh
set -e
PROP_OPTS=()
MEM_OPTS=()
PASS_ARGS=()
DEFAULT_MEM_OPTS=('-Xms512m' '-XX:+AggressiveHeap')
@clintval
clintval / compile-nf-groovy.sh
Created January 8, 2024 16:41
Compile Nextflow library groovy code linked against Nextflow
#!/bin/bash
# Attempts to compile the groovy present in nextflow/lib/ with the groovy compiler and nextflow
# libararies properly linked (because they dont live in a place groovy can find them by default.)
#
# Must have a conda env with nextflow and groovy installed in order for this script to work.
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
@clintval
clintval / snakegraph.sh
Last active October 20, 2023 04:28
Render Snakemake DAGs in your terminal
# Convert DOT graph data into a terminal-ready visualization
function idot {
dot \
-Tpng -Gdpi=300 \
-Efontsize=18 -Efontname=sans -Nfontname=sans \
-Gbgcolor=black -Gcolor=white -Ecolor=white -Efontcolor=white -Ncolor=white -Nfontcolor=white \
| convert -trim -bordercolor black -border 20 -transparent black -resize "60%" - - \
| imgcat # Or swap with your favorite terminal image viewer
}
/** Creates a fixed size thread pool executor that will rethrow exceptions from submitted jobs. */
class ThreadPoolExecutorWithExceptions(val threads: Int)
extends ThreadPoolExecutor(threads, threads, 0, TimeUnit.SECONDS, new LinkedBlockingDeque[Runnable]) {
/** A place to hold an exception that may have been raised by any of the executed futures. */
protected var exception: Option[Throwable] = None
override protected def afterExecute(runnable: Runnable, throwable: Throwable): Unit = {
if (throwable == null && runnable.isInstanceOf[Future[_]]) try {
val future = runnable.asInstanceOf[Future[_]]
@clintval
clintval / SamLocusIterator.scala
Created January 1, 2022 15:03
Wrap the HTSJDK SAM locus iterator into something more ergonomic
package io.cvbio.sam
import com.fulcrumgenomics.FgBioDef.View
import com.fulcrumgenomics.bam._
import com.fulcrumgenomics.bam.api.{SamRecord, SamSource}
import htsjdk.samtools.SAMRecord
import htsjdk.samtools.filter.{DuplicateReadFilter, FailsVendorReadQualityFilter, SamRecordFilter, SecondaryAlignmentFilter}
import htsjdk.samtools.util.AbstractRecordAndOffset.AlignmentType.{Deletion, Insertion, Match}
import htsjdk.samtools.util.SamLocusIterator.LocusInfo
import htsjdk.samtools.util.{Interval, SamLocusIterator => HtsJdkSamLocusIterator}
@clintval
clintval / Dockerfile
Last active March 28, 2024 04:40
Bioinformatics example of a multi-stage Dockerfile
# syntax=docker/dockerfile:1.3
FROM openjdk:8-slim-buster AS builder
RUN apt-get update && apt-get install -y git
# Not actually needed by any subsequent commands, but shows how you can bake
# things into the builder layer if they are commonly needed by other layers.
RUN mkdir -p -m 0600 ~/.ssh \
&& ssh-keyscan github.com >> ~/.ssh/known_hosts
@clintval
clintval / Interval.scala
Last active September 22, 2021 21:47
A better HTSJDK Interval, MIT License
package io.cvbio.coord
import com.fulcrumgenomics.fasta.SequenceDictionary
import htsjdk.samtools.util.{Interval => HtsJdkInterval, Locatable => HtsJdkLocatable}
/** Any interface that can be stranded. */
trait Stranded { def positiveStrand: Boolean }
/** An intermediate mixin that will provide access to HTSJDK's interval API. */
private[coord] trait IntervalIntermediate extends HtsJdkInterval
import com.fulcrumgenomics.bam.api.SamSource
import com.fulcrumgenomics.commons.CommonsDef._
/** Namespace for SAM/BAM source utilities. */
object SamSourceUtil {
/** Return the only sample in this SAM/BAM source otherwise raise an exception. */
def onlySample(source: SamSource): String = {
validateHasSingleSample(source)
source.header.getReadGroups.map(_.getSample).toSeq.head
package io.cvbio.collection
import io.cvbio.io.Io
import com.fulcrumgenomics.commons.collection.SelfClosingIterator
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent._
import scala.concurrent.duration.{Duration, DurationInt}
import scala.concurrent.{Await, ExecutionContext, Future}