Skip to content

Instantly share code, notes, and snippets.

@radaniba
radaniba / SmithWaterman.py
Created April 18, 2014 01:03
Smith Waterman Implementation in Python
#!/Users/Rad/anaconda/bin/python
# (c) 2013 Ryan Boehning
'''A Python implementation of the Smith-Waterman algorithm for local alignment
of nucleotide sequences.
'''
import argparse
@radaniba
radaniba / silent-output.py
Created March 22, 2013 14:22
The essential problem is how to not get output from a program. Let me explain: Ete2, a Python module for representing phylogenies, has a number of dependencies (MySQLdb, Numpy, PyQt, etc.) that it doesn't necessarily need and it xcan be installed without them. If you don't use the associated functionality, you won't need these dependencies. But,…
import os, sys
class SuppressAllOutput (object):
def __enter__(self):
sys.stderr.flush()
self.old_stderr = sys.stderr
sys.stderr = open('/dev/null', 'a+', 0)
sys.stdout.flush()
self.old_stdout = sys.stdout
sys.stdout = open('/dev/null', 'a+', 0)
@radaniba
radaniba / color-node-labels.rb
Created March 22, 2013 14:15
For when you have to label the tips of a phylogeny in a systematic way. Rather than "point and click" within Dendroscope, this script takes a .den/dendro file and colors the tips according to a "color description" file. This is a simple csv file with taxa labels and a corresponding color. The color may either be an RGB triplet or a scalar value …
#!/usr/bin/env ruby
# Color the node labels in a Dendroscope tree.
### IMPORTS
require 'test/unit/assertions'
require 'optparse'
require 'pp'
require 'csv'
require 'ostruct'
@radaniba
radaniba / index-sequence.py
Created March 22, 2013 14:10
When reading in sequences, you may want to arrange or index them in some way (rather than just get one big list o' sequences). Fortunately Biopython's SeqIO has a useful function for this: "to_dict" returns a dictionary where the keys are derived from the SeqRecords that are the values.
from Bio import SeqIO
handle = open("example.fasta", "rU")
record_dict = SeqIO.to_dict (SeqIO.parse (handle, "fasta"))
handle.close()
# you now have dict where the keys are the sequence IDs, e.g. record_dict["gi:12345678"]
# you can index in other ways with the "key_function".
# for example, if you wanted to index by the description of the sequence
@radaniba
radaniba / fetch_seq_by_id.rb
Created February 26, 2013 21:34
A simple script to grab bioseqs by accession. Requires BioRuby. This just wraps the BioRuby fetch functionality in a friendly commandline interface. In brief, it can accept accession ids on the commandline or from a piped file (one accession per line) and save the corresponding sequences from the db. Sequences may be downloaded via the bioruby o…
#!/usr/bin/env ruby
# download sequences from db by id
### IMPORTS
require 'bio'
require 'ostruct'
require 'timeout'
require 'pp'
require 'test/unit/assertions'
@radaniba
radaniba / index_sequence.py
Created February 26, 2013 21:25
When reading in sequences, you may want to arrange or index them in some way (rather than just get one big list o' sequences). Fortunately Biopython's SeqIO has a useful function for this: "to_dict" returns a dictionary where the keys are derived from the SeqRecords that are the values.
from Bio import SeqIO
handle = open("example.fasta", "rU")
record_dict = SeqIO.to_dict (SeqIO.parse (handle, "fasta"))
handle.close()
# you now have dict where the keys are the sequence IDs, e.g. record_dict["gi:12345678"]
# you can index in other ways with the "key_function".
# for example, if you wanted to index by the description of the sequence
@radaniba
radaniba / check_alignments_ambiguous_characters.rb
Created February 21, 2013 16:18
A number of programs won't handle sequences with gaps or ambiguous characters. This then is a script that reports those problems (e.g. telling you the problem is at residue x in sequence y)and can optionally patch it with the consensus sequence at that location.
#!/usr/bin/env ruby
# Check alignment for gaps or ambiguous characters and possibly fix them.
### IMPORTS
require 'bio'
require 'test/unit/assertions'
require 'optparse'
require 'pp'
@radaniba
radaniba / reduceseqtosnp.rb
Created February 21, 2013 16:12
A largely self-explanatory script: This will "shrink" an alignment, deleting all sites that don't contain a polymorphism in some member sequence. A little bit of script candy as well, this takes any number of files and saves the results in a new file named according to a definable schema. Actually, the whole thing is very over-engineered ...
#!/usr/bin/env ruby
# Reduce a sequence to solely the SNP sites.
### IMPORTS
require 'test/unit/assertions'
require 'optparse'
require 'pp'
require 'ostruct'
require 'date'
@radaniba
radaniba / pdf2png.py
Last active December 13, 2015 21:58
A simple script to convert named PDFs to PNGs of the equivalent name.the use case here is that I had a program that churned out graphs as PDFs (and PDFs only) and needed to include these graphs in another document (webpages in this case). This script takes the PDFs and runs them on the commandline through a simple programs called "sips", naming …
import sys
from os import path, system
# for each file
for infile in sys.argv[1:]:
# extract filename components
base, ext = path.splitext (infile)
new_name = base + ".png"
print "Converting %s to %s ..." % (infile, new_name)
system ("sips -s format png \"%s\" --out \"%s\"" % (infile, new_name))
@radaniba
radaniba / create_temp_files.py
Last active December 13, 2015 19:09
Some assorted functions for creating temporary files.This wraps and enhances the facilities of the standard "tempfile" library, written mostly for the purposes of calling external applications that will use and produce a lot of temporary files. So, this makes it easy to write the necessary inputs for an external program somewhere where it won't …
__docformat__ = 'restructuredtext en'
### IMPORTS ###
import exceptions
import tempfile
import os
import sys
import shutil