Skip to content

Instantly share code, notes, and snippets.

View clintval's full-sized avatar

Clint Valentine clintval

View GitHub Profile
/** 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}
@clintval
clintval / FullyPeekableIterator.scala
Last active January 31, 2023 13:16
Peek as much as you'd like, memory allowing
/*
* The MIT License
*
* Copyright © 2022 Clint Valentine
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@clintval
clintval / download.sh
Created August 1, 2021 19:14
Download COSMIC
export TOKEN=???
curl -s -X GET \
-H "Content-Type: application/octet-stream" \
-H "Authorization: Bearer ${TOKEN}" "https://my.qiagendigitalinsights.com/bbp/data/files/cosmic" \
-o files.txt
parallel -k \
'curl -X GET \
-H "Content-Type: application/octet-stream" \
@clintval
clintval / BiMap.scala
Created July 30, 2021 02:32
MIT License (see fgbio/commons for inspiration)
package com.twinstrandbio.collection
import scala.collection.compat._
import scala.collection.{StrictOptimizedIterableOps, mutable}
/** A mutable bi-directional map. There is a 1:1 mapping between keys and values. */
class BiMap[K, V]() extends mutable.Map[K, V]
with mutable.MapOps[K, V, mutable.Map, BiMap[K, V]]
with StrictOptimizedIterableOps[(K, V), mutable.Iterable, BiMap[K, V]] {
private val forward = mutable.HashMap.empty[K, V]
@clintval
clintval / rasterize.md
Created February 23, 2021 01:34
Will rasterize PDFs, 4 at a time recursively, without anti-aliasing

Rasterize Recursively

 ❯ parallel -j4 convert -density 400 +antialias {} {.}.raster.pdf ::: **/*.pdf