Skip to content

Instantly share code, notes, and snippets.

View ivan-krukov's full-sized avatar
🗃️
Getting there

Ivan Krukov ivan-krukov

🗃️
Getting there
View GitHub Profile
@ivan-krukov
ivan-krukov / corr.pl
Created May 15, 2013 21:00
Solution to Rosalind CORR problem
#!/usr/bin/env perl
use v5.14;
use warnings;
sub hamming {
my @a = split "", shift;
my @b = split "", shift;
my $distance = 0;
for (my $n = 0; $n < scalar(@a); $n++) {
unless (($a[$n]) eq ($b[$n])) {
@ivan-krukov
ivan-krukov / cons.py
Last active December 17, 2015 15:19
fancy consensus builder
#!/usr/bin/env python
#http://rosalind.info/problems/cons/
import fastaparse
letters = ["A","C","G","T"]
sequences = [i.seq for i in fastaparse.sequences("temp2")]
#create empty profile matrix
@ivan-krukov
ivan-krukov / time-update.sh
Created May 28, 2013 22:05
time resync and hwclock update (arch)
sudo ntpd -gq
sudo hwclock --systohc --utc
@ivan-krukov
ivan-krukov / fastaparse.py
Created May 29, 2013 21:04
Different ways of parsing fasta files. Last one is my favorite - generators and named tuples.
#load everythin in memory
#split
def split_fasta(input_file):
with open(input_file) as fasta_file:
text = fasta_file.read().split(">")[1:]
data = []
for entry in text:
header,sequence = entry.split("\n",1)
sequence = sequence.replace("\n","")
@ivan-krukov
ivan-krukov / qw.py
Created May 31, 2013 20:23
A little perl-like qw function for interactive work
def qw(s,t=str):
return list(map(t,s.split()))
@ivan-krukov
ivan-krukov / lcsm.py
Created June 5, 2013 19:04
lcsm problem on Rosalind
#!/usr/bin/env python
import fastaparse
import sys
sequences = [i.seq for i in fastaparse.parse_fasta(sys.argv[1])]
def substrings(string):
n = len(string)
for length in range(n,0,-1):
@ivan-krukov
ivan-krukov / ftp-download.py
Created June 6, 2013 03:50
Complete download file over FTP example. Complete with write-report closure in python3 style, writing stdout to same line and nice verbosity handling
#!/usr/bin/env python3
from ftplib import FTP
def ftp_download(server,remote_path,local_path,username="anonymous",password="",verbose=True):
#new ft object
ftp = FTP(server)
if verbose: print("Connected to %s"%server)
#notice that default login is "anonymous" with no password. works on most public servers
@ivan-krukov
ivan-krukov / templater.py
Last active December 18, 2015 09:38
Template string wrapper. uses 'inspect' to get the caller namespace dict
import inspect
def render(string,variables=None):
"""Wrapper method for String Template:
name="Jeff"
render("Hello, $name")
>>Hello, Jeff
"""
t = Template(string)
if not variables:
@ivan-krukov
ivan-krukov / grepseq.py
Last active December 20, 2015 13:59
Fasta grep. Search a fasta file with a pattern. Either a single query or a file with regexes
#get sequences by regex match from a fasta file
from argparse import ArgumentParser
import re
parser = ArgumentParser(description="grep for fasta IDs")
group = parser.add_mutually_exclusive_group()
group.add_argument("--query_string","-q")
group.add_argument("--query_file","-f")
@ivan-krukov
ivan-krukov / perl-cpp-arrays.pl
Created November 5, 2013 20:26
Using Perl Inline::CPP and perlapi to pass around array data
#!/usr/bin/perl
use warnings;
#not sure how to use strict here - does not allow bareword in "Inline CPP"
use Inline CPP;
my @data = (0..10);
my $result_ref = do_stuff(\@data);
my @result = @$result_ref;
print "@result\n";
__END__
__CPP__