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 / 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)
@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
@matteoferla
matteoferla / Plotly Jupyter notebook to HTML fix.js
Last active June 28, 2019 22:34
Allow plotly charts in Jupyter notebook to export to .html
%%javascript
//hack to fix export
require.config({
paths: {
d3: 'https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3',
jquery: 'https://code.jquery.com/jquery-3.4.1.min',
plotly: 'https://cdn.plot.ly/plotly-latest.min'
},
shim: {
@matteoferla
matteoferla / output plotly HTML to cell for copy pasting.js
Created June 28, 2019 22:35
output plotly HTML to cell for copy pasting
%%javascript
//remember to append <script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script> to head or top of body.
//output_subarea output_html rendered_html
let index = 0; //or whatever is the number of the graph.
//$('.plotly-graph-div').map((i,el) => element.append('<p>'+$(el).attr('id')+'</p>'))
let chart = $('.plotly-graph-div').eq(index).parent();
/// optionally one could change the dimensions for the .plotly-graph-div and svg elements.
//chart.children('.plotly-graph-div').css('height','525px');
//chart.children('.plotly-graph-div').css('width','100%');
let codeEl = $('<pre id="plotText"><code></code></pre>');