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
from Bio import SeqIO | |
from Bio.Seq import Seq | |
sequence_generator = SeqIO.parse("br_sequences.fasta", "fasta") | |
all_sequences = list(sequence_generator) | |
# * How many records are in the file? | |
print("Number of records:", len(all_sequences)) | |
# * How many records have a sequence of length 249? |
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
"Functions missing from Python's string/list types that are handy for bio-informatics." | |
def codonise(seq): | |
'''Returns a list of codons, not including trailing 1/2n. | |
To get codons starting from letter X, pass seq[X:].''' | |
mylist = [] | |
for i in range(0, len(seq), 3): | |
this_codon = seq[i:i+3] | |
# This bit ensures that only whole codons, | |
# not trailing bits, are added: |
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 sys | |
from Bio import SeqIO | |
from Bio.Seq import Seq | |
filename = sys.argv[1] | |
usersequence = Seq(sys.argv[2]) | |
usersequence = usersequence.upper() | |
user_reverse = usersequence.reverse_complement() | |
records = SeqIO.parse(filename, "fasta") |
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 sys as s, itertools as i | |
t,u,i2b,o=b'T',b'U',lambda i:i.to_bytes(1,'big'),[s.stdout.buffer.write,lambda s:0] | |
A,e=s.argv,dict(zip((''.join(x).encode()for x in i.product(*('ACGT',)*4)),map(i2b,range(256)))) | |
with open(A[2],'rb')as I:D,Rr=b''.join(I.read().strip().split()),lambda s:s.replace(t,u) | |
S,R,M,o,d=D[:-1],D[-1]&4,D[-1]&2,o[::-1]if A[1]=="d"else o,dict(zip(e.values(),e)).get | |
[o[0](e.get(q.replace(u,t)+(b'A'*(4-len(q)))))for q in(D[i: i+4] for i in range(0,len(D),4))] | |
o[0](i2b((len( [D[i: i+4] for i in range(0,len(D),4)][-1] )%4)|(4 if u in S else 0))) | |
o[1](b''.join(Rr(d(i2b(x)))if R else d(x)for x in S)[:-M if M else None]+b'\n') |
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
class Prune: | |
'Treat like a function; call Prune.prune on any datatype to prune NoneTypes and empty Tuples/Lists/Dicts.' | |
_t = tuple() # Empty tuple, as "(,)" literal doesn't work. | |
@classmethod | |
def prune(self, some_data): | |
if isinstance(some_data, (list, tuple)): | |
return self.prune_sequence(some_data) | |
elif isinstance(some_data, dict): | |
return self.prune_tree(some_data) |
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
def strict(func): | |
'''A decorator for methods or functions that requires annotations for all | |
arguments and the return value, throws typeerrors on deviations. | |
Remember that for more than one return value, the return type is "tuple". | |
Container-type arguments or return values are only inspected at top-level. | |
Note that as written, this does not handle catchall argument types "*args", or "**kwargs". | |
''' | |
import inspect, collections | |
NoneType = type(None) | |
def die_on_untyped_annotation(par, ann_type="argument annotation"): |
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 random | |
import shutil | |
import tty | |
import sys | |
import termios | |
def getchar(): | |
'Linux-only: Could not be bothered making arrow-getting code WinMac-compatible.' | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) |
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
# This is a micro-language designed to be embedded in JSON, allowing | |
# data in the JSON tree to specify a formula for how it should be derived | |
# based on the rest of the tree. Formulas are constructed of prefix-notation | |
# lists naming a function and passing arguments, which are recursively | |
# evaluated. | |
# | |
# Operations are mostly mathematical, with one ternary function that allows | |
# for simple conditional operations or code-branching. | |
# | |
# An example: |
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
Bio-rad.com has one of those intensely aggravating landing dialogs that demands | |
to know where you are so it can forcibly remove you from what you're trying to | |
view, and perhaps gouge you for more money. | |
This is annoying, invasive and generally not-cool, so here's a little | |
bookmarklet to remove the dialog and get on with things. | |
To use, just copy the below code into a new bookmark using your browser's | |
bookmark manager. Don't omit the "javascript:" bit or the final empty | |
parentheses: "()". |
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
#!/usr/bin/env python3 | |
# by Cathal Garvey, copyright 2015, released under the AGPL: https://gnu.org/licenses/agpl.txt | |
# Commissioned by a 4chan user on reddit /r/linux who wanted backups but wget couldn't fetch most JS/CSS | |
# correctly. Only tested on 4chan in keeping with request. | |
# Usage e.g. (papercraft sub on 4chan): | |
# wget --recursive --no-clobber --page-requisites --html-extension --convert-links --no-parent http://boards.4chan.org/po/ | |
# cd boards.4chan.org | |
# # (Directory contains subdirectory "po/" which contains all HTML) | |
# # (Provide root domain of crawled site to help resolve relative links, and target folder) | |
# python3 <this script> boards.4chan.org po |
OlderNewer