Skip to content

Instantly share code, notes, and snippets.

View matteoferla's full-sized avatar

Matteo Ferla matteoferla

View GitHub Profile
@matteoferla
matteoferla / Blogger_LCARS
Created October 4, 2015 22:53
A prototype to make Blogger look like Star Trek's LCAR using a modified version of the CSS from https://github.com/Garrett-/lcars
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
<head>
<meta content='IE=EmulateIE7' http-equiv='X-UA-Compatible'/>
<b:if cond='data:blog.isMobile'>
<meta content='width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0' name='viewport'/>
<b:else/>
<meta content='width=1100' name='viewport'/>
</b:if>
@matteoferla
matteoferla / wsgi_hack.py
Created October 14, 2015 18:31
WSGI serve static issue
elif environ['PATH_INFO'].find('/static') != -1:
#ctype = environ['HTTP_ACCEPT'].split(',')[0]
protoctype=environ['PATH_INFO'].split('.')[-1]
if protoctype =='css':
ctype='text/css'
response_body = open('wsgi'+environ['PATH_INFO'],encoding='utf-8').read().encode('utf-8')
elif protoctype =='js':
ctype='text/javascript'
response_body = open('wsgi'+environ['PATH_INFO'],encoding='utf-8').read().encode('utf-8')
elif protoctype =='ico':
##THIS IS PUBLICALLY RELEASED TO DEMONSTRATE A POINT NOT AS A USABLE PIECE OF CODE, HENCE THE LACK OF ANNOTATION.
__doc__ = '''
I have peptide sequences, I need the DNA. plan:
Option A:
* find gi code
* look for gi code in the gbk file.
Option B:
* tblastn online
* download genome sequence
* extract sequence from nucleotide
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Written for python 3, not tested under 2.
"""
Finds all gene symbols (`get_all_genes()` method) and find the genes in common with the word list.
"""
__author__ = "Matteo Ferla. [Github](https://github.com/matteoferla)"
__email__ = "matteo.ferla@gmail.com"
__date__ = "10/05/16"
def zoospotter(zoo,fn='pubmed_scored.csv', assoclist=['infection']):
"""
Method that downloads the number of Pubmed entries associataed with a given species listed in the zoo variable.
The variable assoclist has the fields sought.
:param zoo: list of species names
:param fn: file name to save.
:param assoclist: list of terms that one wishes to see the association. E.g. 'infection' to find pathogenicity.
:return: None.
"""
#debugprint=print
@matteoferla
matteoferla / IP_check.py
Last active August 30, 2021 05:41
A script to be run as, say, an hourly cron job to check wheather the IP address of the machine has changed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__description__ = \
"""
A script to check if the IP address is running.
Use it add your settings to the fields below.
Once you are sure you have all the right keys and it works, install an hour cron job thusly:
$sudo nano crontab -e
0 * * * * python3 ~/Coding/IP_check/IP_check.py >> ~/Coding/IP_check/log.txt
@matteoferla
matteoferla / find_key_or_value_in_nested_dict.py
Created December 19, 2018 14:07
three methods, two to float-up the path to a key or value and one that given a dictionary and a tuple of keys/indices returns the value.
#python 3
# three methods, two to float-up the path to a key or value and one that given a convoluted nested dictionary and a tuple of keys/indices returns the value.
# float_key(dictionary,key)
# float_value(dictionary,key)
# get_value(dictionary,(keys/indices list))
# great for entrez, Uniprot, PDB XML dictionaries
# NB. works for dict and list like objects
import collections
@matteoferla
matteoferla / deep_clean_a_dict.py
Created December 19, 2018 14:31
Some nested dictionaries in Python, say from a XML have https entries in many tags. this method cleans them up.
def deep_clean(element, damn): # the values have the annoying {http} field this removes them.
try:
if isinstance(element, dict):
return {k.replace(damn, '').replace('@', '').replace('#', ''): deep_clean(element[k], damn) for k in
element}
elif isinstance(element, list):
return [deep_clean(e, damn) for e in element]
elif isinstance(element, str):
if re.fullmatch('\d*', element):
return int(element)
# "pseudo"-bound decorator within a class, with the class attribute boolean `prevent_error`.
# it does not inherit self and is not a classmethod or a static method. But will behave like one.
# it is a generator function, whose inner function is a normal bound
# Do note the print requires customisation, i.e. `self.descriptive_attribute`.
def failsafe(func):
def wrapper(self, *args, **kargs):
# the call happned after chekcing if it should croak on error so to make the traceback cleaner.
if self.prevent_error:
try:
return func(self, *args, **kargs)
@matteoferla
matteoferla / ET_uniprot_monkeypatch.py
Last active February 25, 2019 15:21
a collection of monkeypatch methods that helps handle better the ET.Element instances from Uniprot XML files
__description__ = """
This is a collection of methods that helps handle better the ET.Element instances from Uniprot XML files.
They are monkeypatched to the class object itself.
The Element can be monkeypached by importing xmlschema, as opposed to using the __builtin__ workaround.
Basically, I am piggybacking my monkeypatch on it, meaning that I don't need to copypaste from SO.
* `ET.Element.ns_strip()` #returns the tag with the {namespace}
* `ET.Element.is_tag(value)` # boolean fx to check if tag == value
* `ET.Element.describe()` # prints the content of the elemtn for debugging, similarly to dump but better.
* `ET.Element.is_human()` # boolean fx to check if human or dancer