Skip to content

Instantly share code, notes, and snippets.

@arnavm
arnavm / EfficientRandIndex.py
Created July 16, 2020 00:09
A memory-efficient Rand index calculator for two segmentations
import numpy as np
from ruptures.metrics import randindex # For comparison
# Rand index for two different segmentations
def arithmeticRandIndex(intervals_A, intervals_B, overlaps):
# All three inputs are lists of contiguous interval lengths
assert np.sum(intervals_A) == np.sum(intervals_B)
total = np.sum(intervals_A)
return 1 - (np.sum(intervals_A ** 2) + np.sum(intervals_B ** 2) - 2 * np.sum(overlaps ** 2)) / (total ** 2)
@arnavm
arnavm / remakeBed
Created July 10, 2020 21:40
One-liner to rescale BED features to a constant-width intervals
# Nifty one-liner for rescaling BED features (new size designated by s)
# Solution from https://www.biostars.org/p/241744/#241748
alias rescaleBed="awk -F '\t' '{X=s; mid=(int(\$2)+int(\$3))/2;printf(\"%s\t%d\t%d\n\",\$1,(mid-X<0?0:mid-X),mid+X);}'"
# Example usage to get centered, 200 bp intervals: rescaleBed s=100 test.bed
@arnavm
arnavm / kmer.cc
Last active October 10, 2019 04:48 — forked from gatoravi/2mer.cc
Takes a k-mer and FASTA as input and outputs BED file containing all position within the FASTA for that k-mer
/*
Copyright 2019 Avinash Ramu & Arnav Moudgil
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 furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
@arnavm
arnavm / warnings_debug.py
Last active July 31, 2019 05:10
Debug warnings in Python
# Example of catching warnings to set debugging breakpoints; based on
# https://docs.python.org/3/library/warnings.html#testing-warnings
import warnings
# Toy function to throw a warning
def fxn():
warnings.warn("deprecated", DeprecationWarning)
# Context manager to catch warning
with warnings.catch_warnings(record=True) as w:
@arnavm
arnavm / README.md
Last active October 17, 2017 18:22 — forked from nforrester/README.md
Bayesian Blocks Algorithm