Skip to content

Instantly share code, notes, and snippets.

View matteoferla's full-sized avatar

Matteo Ferla matteoferla

View GitHub Profile
@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>');
import wikitextparser as wtp
import re
####### code to convert template to dictionary
def arg_to_val(arg):
val = arg.value
for t in arg.templates:
tval = t.arguments[0].value
if t.normal_name() in ('nowrap', 'val'):
import requests, re, csv, pickle
import wikitextparser as wtp
class WikicatParser():
"""
Gets all the pages recursively within a category and parser the content (via a suplied function) and gets pageviews.
>>>pages = WikicatParser(cat_name, custom_page_parser=my_function, extra_fields=[], forbidden_categories_keywords=[...]).get_pages_recursively()
>>>print(pages.data)
>>>pandas.DataFrame.from_records(list(pages.data.values()))
custom_page_parser is for content mining. a function that given wiki text returns a dictionary of whatever it mined.
@matteoferla
matteoferla / progressbar.py
Last active August 8, 2019 19:57
Progressbar (manual or countdown) for Jupyter notebooks
from IPython.display import display, HTML
import uuid
class Progress:
"""
Add a progressbar.
Using https://getbootstrap.com/docs/4.3/components/progress/ not the HTML5 one.
Note that Jupyter uses an older version: 3.4 (https://getbootstrap.com/docs/3.4/components/), but I doubt this will be so for long hence the color via style.
Two uses different uses.
@matteoferla
matteoferla / loop_fixed.py
Created July 4, 2020 16:05
Identify gaps and fill them with pyrosetta
from __future__ import annotations
from dataclasses import dataclass
from typing import List
import pyrosetta
@dataclass
class GapData:
chain: str
start: int # PDB index of first missing residue
previous: int # PDB index of the preceeding present residue
@matteoferla
matteoferla / bindicator.py
Created November 15, 2020 00:21
I am unable to remember which colour bin to put on and to put the bin out in the first place... So the Pi does it for me.
# A raspberry pi has a green and a blue LED connected to D23 and D24
# ============== imports =========================================
from apscheduler.schedulers.background import BackgroundScheduler
from functools import partial
from datetime import datetime
import digitalio
import board
import time
@matteoferla
matteoferla / pick_for_deletion.py
Created February 2, 2021 14:53
For Fragmenstein. A draft of a potential way to remove atoms from a 3D embedded set of molecules, when 2D is too misleading and overlap is key.
# NGLView is a great way to see a set of rdkit.Chem molecules in 3D.
# Right clicking gives green spheres by default. Right clicking on a sphere removes it. So nice way to keep track
# the `ModNGL` object contains the dynamic property `picked_atoms` which lists the atoms that were clicked.
# Clicked twice is removed. However, there is no way to determine the button integer (left, middle, wheel, right).
# Before loading the mols from rdkit it would be nice to fill the PDB info.
# As seen in https://blog.matteoferla.com/2020/03/atom-names-purely-in-rdkit.html
# once one has the atom names one can delete the atoms off a ``rdkit.Chem.RWMol`` making it nice and clear for Fragmenstein.
@matteoferla
matteoferla / rdkit_dummy_masker.py
Last active October 1, 2021 08:56
A context manager that allows operations on a mol containing dummy atoms (R/*) that otherwise would raise an RDKit error.
from rdkit import Chem
from rdkit.Chem import AllChem
class DummyMasker:
"""
A context manager that allows operations on a mol containing dummy atoms (R/*) that
otherwise would raise an RDKit error.
It simply masks and unmasks the dummy atoms.
>>> mol = Chem.MolFromSmiles('*CCC(C)C')
@matteoferla
matteoferla / sphinxify_vars.py
Created February 6, 2021 13:03
Print out what the attributes are for copy pasting for Sphinx documentation
"""
In the docstring for a class the variables can be defined with ``:var xxx:``, ``:ivar xxx:`` and ``:cvar xxx:``.
This prints a block for a given class instance to make it easier.
Dynamic attributes will be marked as class variables.
Private attributes will not show.
NB. Writing ``#:`` is probably easier and saner, but has to be done during coding...
"""
def sphinxify_vars(obj):