# Notebook from schallerdavid/kinodata
Path: human-kinases/human_kinases.ipynb

# Obtain Uniprot IDs for all human kinases

The definition of _kinase_ is not as straightforward as one could think. Throughout the literature you can find several manuscripts that list an authoritative list of kinases to query in an assay or include in domain-specific database. Surprisingly, the criteria used to nominate how a kinase makes it to the list or not is not consistent or obvious, and this results into slightly divergent definitions of what the human kinome really represents. In this notebook we aim to discuss these discrepancies and figure out a way to consistently agree on a definition for our future kinase-centric studies.

> To that avail, we will query online databases, web services and documents using `urllib`, `requests`, `beautifulsoup4` and `pandas`._____no_output_____

# Some imports and helper globals
import urllib.request
from pathlib import Path

import requests
import bs4
import pandas as pd
from matplotlib import pyplot as plt

REPO = (Path(_dh[-1]) / "..").resolve()
DATA = REPO / 'data'

SOURCES = {}_____no_output_____

## Enumerate all possible sources for human kinases

There are several nomenclatures commonly used to refer to kinases: xName, Manning, HGNC... While there are some overlaps, there's no uniquely identifying naming system, so instead we will use Uniprot's identifiers as our primary keys. These can be mapped to other identifiers whenever available.

There are several sources that can be used to identify the human kinome in Uniprot:

* __A)__ [KinHub](http://kinhub.org/kinases.html) (based on Eid, 2017)
* __B)__ [KLIFS](https://klifs.vu-compmedchem.nl/) (based on Kooistra, 2015 and Linden 2013)
* __C)__ [Pkinfam](https://www.uniprot.org/docs/pkinfam.txt) - Uniprot's _Human and mouse protein kinases: classification and index_.
* __D1)__ Dunbrack's structurally validated kinase alignment  (based on Dunbrack, 2019)
* __D2)__ Dunbrack's `kincore` webserver (based on Dunbrack, 2019)
* __E)__  Kinbase (based on Manning, 2002)
* __F)__ Handcrafted UniProt query

Other possible resources we have not checked yet:

* Kanev, 2019 "The Landscape of Atypical and Eukaryotic Protein Kinases"
* Barton, 2007 "Classification and functional annotation of eukaryotic protein kinases" -- maybe uninformative
* Hunter, 2000 "Signaling-2000 and Beyond" -- maybe uninformative

These online references might help shed light on this issue:

* https://www.biostars.org/p/131138/
* https://github.com/macarthur-lab/gene_lists_____no_output_____### A) KinHub

[Kinhub.org](http://kinhub.org/kinases.html) maintains a HTML table in its webpage that can be retrieved with `requests` and `beautifulsoup`.

> __Citation__:
> Eid S., Turk S., Volkamer A., Rippmann F., and Fulle S. (2017)
> KinMap: a web-based tool for interactive navigation through human kinome data. BMC Bioinformatics, 18:16. [DOI: 10.1186/s12859-016-1433-7 ](https://bmcbioinformatics.biomedcentral.com/articles/10.1186/s12859-016-1433-7)_____no_output_____

r = requests.get("http://kinhub.org/kinases.html")
r.raise_for_status()
html = bs4.BeautifulSoup(r.text)_____no_output_____table = html.find('table')
columns = [cell.text.strip() for cell in table.find('thead').find_all('th')]
records = [[cell.text.strip() for cell in row.find_all('td')] for row in table.find('tbody').find_all('tr')]
kinhub = SOURCES['kinhub'] = pd.DataFrame.from_records(records, columns=columns).sort_values(by="xName")
kinhub_____no_output_____

### B) KLIFS

[KLIFS](https://klifs.vu-compmedchem.nl/) maintains their own internal list of kinase names as part of their database. This can be retrieved via their JSON API as follows. It requires two steps: 1) getting the list of names, 2) getting the metadata associated to each name. All of this is then dumped into a dataframe.

> __Citations__
>
> Oscar P. J. van Linden, Albert J. Kooistra, Rob Leurs, Iwan J. P. de Esch, and Chris de Graaf* (2013). KLIFS: A Knowledge-Based Structural Database To Navigate Kinase–Ligand Interaction Space. J. Med. Chem. 2014, 57, 2, 249–277. [DOI: 10.1021/jm400378w](https://doi.org/10.1021/jm400378w)
>
> Albert J. Kooistra, Georgi K. Kanev, Oscar P.J. van Linden, Rob Leurs, Iwan J.P. de Esch, Chris de Graaf (2015). KLIFS: a structural kinase-ligand interaction database. Nucleic Acids Research, Volume 44, Issue D1, 4 January 2016, Pages D365–D371. [DOI: 10.1093/nar/gkv1082](https://doi.org/10.1093/nar/gkv1082)
>
> Georgi K. Kanev, Chris de Graaf, Iwan J.P. de Esch, Rob Leurs, Thomas Würdinger, Bart A. Westerman, Albert J.Kooistra (2019). The Landscape of Atypical and Eukaryotic Protein Kinases. Trends in Pharmacological Sciences, Volume 40, Issue 11, November 2019, Pages 818-832. [DOI: 10.1016/j.tips.2019.09.002](https://doi.org/10.1016/j.tips.2019.09.002)_____no_output_____

r = requests.get("https://klifs.net/api/kinase_names?species=HUMAN")
r.raise_for_status()
data = r.json()
all_ids = [record["kinase_ID"] for record in data]_____no_output_____r = requests.get(f"https://klifs.net/api/kinase_information?kinase_ID={','.join(map(str, all_ids))}")
r.raise_for_status()
data = r.json()_____no_output_____items = []
for record in data:
    items.append({
        "KLIFS Name": record["name"],
        "HGNC Name": record["HGNC"],
        "Family": record["family"],
        "Group": record["group"],
        "Class": record["kinase_class"],
        "Kinase Name": record["full_name"],
        "UniprotID": record["uniprot"],
        "IUPHAR": record["iuphar"],
        "Pocket": record["pocket"],
        "KLIFS ID": record["kinase_ID"]
    })
klifs = SOURCES['KLIFS'] = pd.DataFrame.from_dict(items).sort_values(by="KLIFS Name")
klifs_____no_output_____klifs.Group.unique()_____no_output_____

### C) Pkinfam

UniProt maintains [a txt file](https://www.uniprot.org/docs/pkinfam.txt) with a kinase index: `pkinfam`. We can try to parse it and see what information we have there.

> References:
>
> >
> Hunter T.
> Signaling -- 2000 and beyond.
> Cell 100:113-127(2000).
>
> Manning G., Whyte D.B., Martinez R., Hunter T., Sudarsanam S.
> The protein kinase complement of the human genome.
> Science 298:1912-1934(2002).
>
> Miranda-Saavedra D., Barton G.J.
> "Classification and functional annotation of eukaryotic protein
> kinases.
> Proteins 68:893-914(2007)._____no_output_____

r = requests.get("https://www.uniprot.org/docs/pkinfam.txt")
r.raise_for_status()
text = r.text_____no_output_____items = []
current_category = None
lines = iter(text.splitlines())
for line in lines:
    if line.startswith("==="):  # this is a header
        line = next(lines)
        current_category = line.strip()
        next(lines)
        next(lines)
        continue
    if not line.strip() or not current_category or '_HUMAN' not in line:
        continue
    if line.startswith("---") and current_category:
        break

    fields = line.split()
    name = fields[0]
    uniprot_id = fields[2].replace("(", "").replace(")", "")
    items.append({
        'Name': name,
        'Family': current_category,
        'UniprotID': uniprot_id,
    })
pkinfam = SOURCES['pkinfam'] = pd.DataFrame.from_dict(items).sort_values(by="Name")
pkinfam_____no_output_____pkinfam.Family.unique()_____no_output_____

### D1) Dunbrack's structurally validated kinase alignment

Dataset extracted from:

> Modi, V., Dunbrack, R.L. A Structurally-Validated Multiple Sequence Alignment of 497 Human Protein Kinase Domains. Sci Rep 9, 19790 (2019). https://doi.org/10.1038/s41598-019-56499-4_____no_output_____

url = "https://static-content.springer.com/esm/art%3A10.1038%2Fs41598-019-56499-4/MediaObjects/41598_2019_56499_MOESM4_ESM.txt"
dunbrack_msa = SOURCES["dunbrack_msa"] = pd.read_csv(url, sep=r"\s+").rename(columns={'5_Uni_acc': "UniprotID"})
dunbrack_msa_____no_output_____

### D2) Dunbrack's `kincore` webserver

The [Kinase Conformation Resource](http://dunbrack3.fccc.edu/kincore/) (`kincore`) is the accompanying website for 2019's kinase-focused works by Modi and Dunbrack:

> Defining a new nomenclature for the structures of active and inactive kinases. Vivek Modi and Roland L. Dunbrack, PNAS 2019, 116(14) 6818-6827.
> Structurally-Validated Multiple Sequence Alignment of 497 Human Protein Kinase Domains. Vivek Modi and Roland L. Dunbrack, Sci Rep 2019, 9, 19790.

All kinases thereby listed are programmably accessible via a TSV file in this url:

```
http://dunbrack3.fccc.edu/kincore/static/downloads/text-files/Human_Allgroups_Allspatials_Alldihedrals_All.tab
```_____no_output_____

url = "http://dunbrack3.fccc.edu/kincore/static/downloads/text-files/Human_Allgroups_Allspatials_Alldihedrals_All.tab"
dunbrack_kincore = SOURCES["dunbrack_kincore"] = pd.read_csv(url, sep="\t")
dunbrack_kincore_____no_output_____

Unfortunately, this database only contains kinases for which structural data is available, so the number of unique UniprotIDs is considerably smaller, so we won't include it in our analysis._____no_output_____

dunbrack_kincore["UniprotID"].unique().shape[0]_____no_output_____

### E) KinBase

Old website for kinase.com provides a `kinbase` dataset in form of Fasta sequences. However, it doesn't provide identifiers such as UniprotIDs, so we won't be able to use it directly. We can map the Gene names though...

The kinase.com project history can be found [here](http://kinase.com/web/current/about/). The main publication they refer to is:

> G Manning, DB Whyte, R Martinez, T Hunter, S Sudarsanam (2002).
> The Protein Kinase Complement of the Human Genome.
> Science 298:1912-1934. [DOI: 10.1126/science.1075762](http://doi.org/10.1126/science.1075762)_____no_output_____

r = requests.get("http://kinase.com/kinbase/FastaFiles/Human_kinase_protein.fasta")
r.raise_for_status()_____no_output_____header = ["Gene", "Comment", "Sequence"]
records = []
for line in r.text.splitlines():
    line = line.strip()
    if not line:
        continue
    if line.startswith(">"):
        name, comment = line.split(None, 1)
        records.append({"Gene": name[1:], "Comment": comment, "sequence": ""})
    else:
        records[-1]["sequence"] += line
kinbase = pd.DataFrame.from_dict(records)
kinbase_____no_output_____kinbase.Comment.unique()_____no_output_____

Fortunately, in their new website, they do provide more information in an Excel spreadsheet. This doesn't include Uniprot IDs, but other identifies that can potentially be mapped to (in `Entrez_dbXrefs`)._____no_output_____

kinbasex = pd.read_excel("http://kinase.com/static/colt/data/human/kinome/tables/Kincat_Hsap.08.02.xls")
kinbasex_____no_output_____

All Entrez-validated protein coding kinase genes:_____no_output_____

kinbasex[kinbasex["Entrez_gene_type"] == "protein-coding"]["Name"].unique().shape[0]_____no_output_____

### F) Handcrafted UniProt query

Searching UniProt for all [human proteins with confirmed _protein_ kinase activity](https://www.uniprot.org/uniprot/?query=family%3A%22protein+kinase+superfamily%22+AND+reviewed%3Ayes+AND+organism%3A%22Homo+sapiens+%28Human%29+%5B9606%5D%22&sort=score) shows 493 entries (queried on 2021.02.16). Programmatically, we can use the URL API for now.

> Info: [More details on how to write URL queries](https://www.uniprot.org/help/uniprotkb_column_names)
>

We might want to refine the query to provide more precise GO annotations / activity profiles. Albert J. Kooistra (KLIFS) generously provided some hints over email correspondence:

* "For the atypical kinases we tried to capture all that were present in the “new kinbase” at kinase.com. Next we extended the list with closely related new proteins in the same family from UniProt and/or the Guide to pharmacology."
* "For the eukaryotic protein kinases the main criterium was the presence of a catalytic kinase domain. To create this list we started with the listing from Manning and extended via UniProt based on the PFAM PF00069 motif (PF00069). In addition we also tried to capture some annotations from the new kinbase."
* "All in all, it wasn’t an exact science, but several rounds of manual annotation".
* "... you have to update all the UniProt codes to the most recent version (using the UniProt API or their website). [...] there are always a few that are updated/merged/deprecated etc."

_____no_output_____

from urllib.parse import quote
url = ("https://www.uniprot.org/uniprot/?query="
       + quote(r'family:"protein kinase superfamily" AND reviewed:yes AND organism:"Homo sapiens (Human) [9606]"') +
       "&format=tab&force=true&columns=id,entry%20name,reviewed,protein%20names,genes&sort=name")
uniprot = pd.read_csv(url, sep="\t").rename(columns={"Entry": "UniprotID"})
reviewed_uniprot = SOURCES["our_uniprot"] = uniprot[uniprot.Status=="reviewed"].drop("Status", axis=1)_____no_output_____reviewed_uniprot_____no_output_____reviewed_uniprot[reviewed_uniprot["Protein names"].str.contains("atypical", case=False)]_____no_output_____

## Inspect the results included in each source

All relevant datasets have been included in the `SOURCES` dictionary for easy comparison. Let's see how many unique UniprotIDs each dataset suggests:_____no_output_____### Unique entries per dataset_____no_output_____

y = []
labels = []
for key, source in SOURCES.items():
    entries = source["UniprotID"].unique().shape[0]
    y.append(entries)
    labels.append(key)
    diff = source["UniprotID"].shape[0] - source["UniprotID"].unique().shape[0]
    if diff:
        print(f"{key} contained {diff} duplicate entries")kinhub contained 14 duplicate entries
KLIFS contained 13 duplicate entries
dunbrack_msa contained 13 duplicate entries
dunbrack_kincore contained 6852 duplicate entries

Interestingly, some datasets include the same Uniprot ID several times (?)

> TODO: Gather details on why!_____no_output_____

plt.bar(labels, y)
plt.xticks(rotation=45)
plt.title("Number of unique Uniprot IDs in each source");_____no_output_____

Most datasets sit around the ~500 mark, with the exception of Dunbrack's Kincore, which has less since it only considers Uniprot IDs for which structural information is available.

As a result, we won't use Kincore for now since it's probably a subset of something else.

Let's continue by analyzing the overlaps._____no_output_____### Dataset overlap_____no_output_____

datasets = {
    "kinhub": kinhub,
    "klifs": klifs,
    "pkinfam": pkinfam,
    "reviewed_uniprot": reviewed_uniprot,
    "dunbrack_msa": dunbrack_msa
}
uniprot_ids = {key: set(df.UniprotID.tolist()) for key, df in datasets.items()}_____no_output_____

With the dictionary above (`name --> set of Uniprot IDs`), we can now analyze the overlap between sets:_____no_output_____

import venn
from venn import venn as plot_venn
v = plot_venn(uniprot_ids)
v;_____no_output_____def compute_petals(dict_of_datasets):
    """
    Function adapted from `venn._venn.generate_petal_labels`:
        https://github.com/LankyCyril/pyvenn/blob/master/venn/_venn.py#L56
    """
    from venn._venn import generate_logics
    labels = list(dict_of_datasets.keys())
    datasets = list(dict_of_datasets.values())
    n_sets = len(datasets)
    dataset_union = set.union(*datasets)
    universe_size = len(dataset_union)
    petal_labels = {}
    for logic in generate_logics(n_sets):
        included_sets = [datasets[i] for i in range(n_sets) if logic[i] == "1"]
        excluded_sets = [datasets[i] for i in range(n_sets) if logic[i] == "0" ]
        involved_labels = tuple([label for label, i in zip(labels, list(logic)) if i == "1"])
        petal_labels[involved_labels] = (dataset_union & set.intersection(*included_sets)) - set.union(set(), *excluded_sets)
    return petal_labels_____no_output_____petals = compute_petals(uniprot_ids)
for tag, values in sorted(petals.items(), key=lambda kv: len(kv[0]), reverse=True):
    if not values:
        continue
    print(f"{len(values):4d} unique Uniprot IDs in ({len(tag)}-way overlap) {', '.join(tag)}") 473 unique Uniprot IDs in (5-way overlap) kinhub, klifs, pkinfam, reviewed_uniprot, dunbrack_msa
   4 unique Uniprot IDs in (4-way overlap) klifs, pkinfam, reviewed_uniprot, dunbrack_msa
   5 unique Uniprot IDs in (4-way overlap) kinhub, klifs, pkinfam, dunbrack_msa
  14 unique Uniprot IDs in (4-way overlap) kinhub, klifs, pkinfam, reviewed_uniprot
   2 unique Uniprot IDs in (3-way overlap) pkinfam, reviewed_uniprot, dunbrack_msa
  10 unique Uniprot IDs in (3-way overlap) kinhub, klifs, pkinfam
   5 unique Uniprot IDs in (2-way overlap) klifs, pkinfam
   1 unique Uniprot IDs in (2-way overlap) kinhub, pkinfam
  12 unique Uniprot IDs in (2-way overlap) kinhub, klifs
  19 unique Uniprot IDs in (1-way overlap) klifs
   7 unique Uniprot IDs in (1-way overlap) kinhub

We can observe several interesting points here:

* There's a set of 473 kinases on which all datasets agree._____no_output_____#### Overlap for eukaryotic kinases only

It might be that the disagreements are reduced if we only consider eukaryotic kinases._____no_output_____

e_datasets = {
    "kinhub": kinhub[kinhub.Group != "Atypical"],
    "klifs": klifs[klifs.Group != "Atypical"],
    "pkinfam": pkinfam[~pkinfam.Family.str.contains("atypical", case=False)],
    "reviewed_uniprot": reviewed_uniprot[~reviewed_uniprot["Protein names"].str.contains("atypical", case=False)],
    "dunbrack_msa": dunbrack_msa[~dunbrack_msa["14_Group_in_Manning"].str.contains("atypical", case=False)]
}
e_uniprot_ids = {key: set(df.UniprotID.tolist()) for key, df in e_datasets.items()}
plot_venn(e_uniprot_ids);_____no_output_____epetals = compute_petals(e_uniprot_ids)
for tag, values in sorted(epetals.items(), key=lambda kv: len(kv[0]), reverse=True):
    if not values:
        continue
    print(f"{len(values):4d} unique Uniprot IDs in ({len(tag)}-way overlap) {', '.join(tag)}") 470 unique Uniprot IDs in (5-way overlap) kinhub, klifs, pkinfam, reviewed_uniprot, dunbrack_msa
   4 unique Uniprot IDs in (4-way overlap) klifs, pkinfam, reviewed_uniprot, dunbrack_msa
   8 unique Uniprot IDs in (4-way overlap) kinhub, klifs, pkinfam, dunbrack_msa
   2 unique Uniprot IDs in (3-way overlap) pkinfam, reviewed_uniprot, dunbrack_msa
  12 unique Uniprot IDs in (1-way overlap) reviewed_uniprot
   2 unique Uniprot IDs in (1-way overlap) klifs

#### Overlap for atypical kinases only_____no_output_____

a_datasets = {
    "kinhub": kinhub[kinhub.Group == "Atypical"],
    "klifs": klifs[klifs.Group == "Atypical"],
    "pkinfam": pkinfam[pkinfam.Family.str.contains("atypical", case=False)],
    "reviewed_uniprot": reviewed_uniprot[reviewed_uniprot["Protein names"].str.contains("atypical", case=False)],
    "dunbrack_msa": dunbrack_msa[dunbrack_msa["14_Group_in_Manning"].str.contains("atypical", case=False)]
}
a_uniprot_ids = {key: set(df.UniprotID.tolist()) for key, df in a_datasets.items()}
plot_venn(a_uniprot_ids);_____no_output_____apetals = compute_petals(a_uniprot_ids)
for tag, values in sorted(apetals.items(), key=lambda kv: len(kv[0]), reverse=True):
    if not values:
        continue
    print(f"{len(values):4d} unique Uniprot IDs in ({len(tag)}-way overlap) {', '.join(tag)}")   2 unique Uniprot IDs in (4-way overlap) kinhub, klifs, pkinfam, reviewed_uniprot
  22 unique Uniprot IDs in (3-way overlap) kinhub, klifs, pkinfam
   5 unique Uniprot IDs in (2-way overlap) klifs, pkinfam
   1 unique Uniprot IDs in (2-way overlap) kinhub, pkinfam
  12 unique Uniprot IDs in (2-way overlap) kinhub, klifs
   3 unique Uniprot IDs in (1-way overlap) reviewed_uniprot
  17 unique Uniprot IDs in (1-way overlap) klifs
   7 unique Uniprot IDs in (1-way overlap) kinhub

### Dissect the contents of each petal

Let's try to obtain information on the solitary leaves that include exclusive entries in each dataset. We can query the `petals` dictionary for information on the specific IDs. For example, which are those 3 unique IDs in KLIFS?

> Note: You need to query using _tuples_ of the dataset keys, even for single-set petals_____no_output_____

print(*petals[("klifs",)])Q8TBX8 P48426 O75747 P78356 Q8N8J0 Q9BTU6 P35557 O00750 P42356 O14986 Q9UJY1 O60331 Q99755 Q9H6X2 O43921 Q8TCG2 Q9UBF8 Q8NEB9 A4QPH2

Thanks to the Uniprot API we can check the GO annotations if we parse the lines starting with `DR   GO` that also contain `F:` (for function-related annotations):_____no_output_____

def query_uniprot_for_go(*uniprot_ids):
    for i, entry in enumerate(uniprot_ids, 1):
        r = requests.get(fr"https://www.uniprot.org/uniprot/{entry}.txt")
        r.raise_for_status()
        print(i, entry)
        for line in r.text.splitlines():
            if line.startswith(("ID", "DE")):
                print(line)
            elif line.startswith("DR   GO;") and "; F:" in line:
                print(line)
        print()_____no_output_____query_uniprot_for_go(*petals[("klifs",)])1 Q8TBX8
ID   PI42C_HUMAN             Reviewed;         421 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 gamma {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000305|PubMed:26774281};
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II gamma;
DE            Short=PI(5)P 4-kinase type II gamma;
DE            Short=PIP4KII-gamma;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IEA:UniProtKB-EC.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0042802; F:identical protein binding; IPI:IntAct.

2 P48426
ID   PI42A_HUMAN             Reviewed;         406 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 alpha {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000269|PubMed:9367159};
DE   AltName: Full=1-phosphatidylinositol 5-phosphate 4-kinase 2-alpha;
DE   AltName: Full=Diphosphoinositide kinase 2-alpha;
DE   AltName: Full=PIP5KIII;
DE   AltName: Full=Phosphatidylinositol 5-Phosphate 4-Kinase;
DE            Short=PI5P4Kalpha;
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II alpha;
DE            Short=PI(5)P 4-kinase type II alpha;
DE            Short=PIP4KII-alpha;
DE   AltName: Full=PtdIns(4)P-5-kinase B isoform;
DE   AltName: Full=PtdIns(4)P-5-kinase C isoform;
DE   AltName: Full=PtdIns(5)P-4-kinase isoform 2-alpha;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IDA:FlyBase.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

3 O75747
ID   P3C2G_HUMAN             Reviewed;        1445 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 3-kinase C2 domain-containing subunit gamma;
DE            Short=PI3K-C2-gamma;
DE            Short=PtdIns-3-kinase C2 subunit gamma;
DE            EC=2.7.1.154;
DE   AltName: Full=Phosphoinositide 3-kinase-C2-gamma;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IBA:GO_Central.
DR   GO; GO:0035005; F:1-phosphatidylinositol-4-phosphate 3-kinase activity; IBA:GO_Central.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0035091; F:phosphatidylinositol binding; IEA:InterPro.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.
DR   GO; GO:0016307; F:phosphatidylinositol phosphate kinase activity; NAS:UniProtKB.

4 P78356
ID   PI42B_HUMAN             Reviewed;         416 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 beta {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000305|PubMed:26774281};
DE   AltName: Full=1-phosphatidylinositol 5-phosphate 4-kinase 2-beta;
DE   AltName: Full=Diphosphoinositide kinase 2-beta;
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II beta;
DE            Short=PI(5)P 4-kinase type II beta;
DE            Short=PIP4KII-beta;
DE   AltName: Full=PtdIns(5)P-4-kinase isoform 2-beta;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IEA:UniProtKB-EC.
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0005525; F:GTP binding; IDA:UniProtKB.
DR   GO; GO:0042803; F:protein homodimerization activity; IDA:CAFA.

5 Q8N8J0
ID   PI4P1_HUMAN             Reviewed;         262 AA.
DE   RecName: Full=Putative inactive phosphatidylinositol 4-kinase alpha-like protein P1;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

6 Q9BTU6
ID   P4K2A_HUMAN             Reviewed;         479 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase type 2-alpha;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:11279162, ECO:0000269|PubMed:20388919, ECO:0000269|PubMed:24675427, ECO:0000269|PubMed:25168678};
DE   AltName: Full=Phosphatidylinositol 4-kinase type II-alpha;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IDA:UniProtKB.
DR   GO; GO:0035651; F:AP-3 adaptor complex binding; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0000287; F:magnesium ion binding; NAS:UniProtKB.

7 P35557
ID   HXK4_HUMAN              Reviewed;         465 AA.
DE   RecName: Full=Hexokinase-4 {ECO:0000305};
DE            Short=HK4 {ECO:0000305};
DE            EC=2.7.1.1 {ECO:0000269|PubMed:11916951, ECO:0000269|PubMed:15277402, ECO:0000269|PubMed:17082186, ECO:0000269|PubMed:18322640, ECO:0000269|PubMed:19146401, ECO:0000269|PubMed:25015100, ECO:0000269|PubMed:8325892};
DE   AltName: Full=Glucokinase {ECO:0000303|PubMed:1354840};
DE   AltName: Full=Hexokinase type IV {ECO:0000250|UniProtKB:P17712};
DE            Short=HK IV {ECO:0000250|UniProtKB:P17712};
DE   AltName: Full=Hexokinase-D {ECO:0000250|UniProtKB:P17712};
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0008865; F:fructokinase activity; IBA:GO_Central.
DR   GO; GO:0004340; F:glucokinase activity; IDA:UniProtKB.
DR   GO; GO:0005536; F:glucose binding; IDA:UniProtKB.
DR   GO; GO:0019158; F:mannokinase activity; IBA:GO_Central.

8 O00750
ID   P3C2B_HUMAN             Reviewed;        1634 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 3-kinase C2 domain-containing subunit beta;
DE            Short=PI3K-C2-beta;
DE            Short=PtdIns-3-kinase C2 subunit beta;
DE            EC=2.7.1.137 {ECO:0000269|PubMed:10805725, ECO:0000269|PubMed:11533253, ECO:0000269|PubMed:9830063};
DE            EC=2.7.1.154 {ECO:0000269|PubMed:10805725, ECO:0000269|PubMed:9830063};
DE   AltName: Full=C2-PI3K;
DE   AltName: Full=Phosphoinositide 3-kinase-C2-beta;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IDA:UniProtKB.
DR   GO; GO:0035005; F:1-phosphatidylinositol-4-phosphate 3-kinase activity; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0001727; F:lipid kinase activity; IEA:Ensembl.
DR   GO; GO:0035091; F:phosphatidylinositol binding; IEA:InterPro.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

9 P42356
ID   PI4KA_HUMAN             Reviewed;        2102 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase alpha;
DE            Short=PI4-kinase alpha;
DE            Short=PI4K-alpha;
DE            Short=PtdIns-4-kinase alpha;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:10101268};
DE   AltName: Full=Phosphatidylinositol 4-Kinase III alpha;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; ISS:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0045296; F:cadherin binding; HDA:BHF-UCL.
DR   GO; GO:0016301; F:kinase activity; IDA:MGI.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

10 O14986
ID   PI51B_HUMAN             Reviewed;         540 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 beta {ECO:0000305|PubMed:8955136};
DE            Short=PIP5K1-beta {ECO:0000303|PubMed:8955136};
DE            Short=PtdIns(4)P-5-kinase 1 beta {ECO:0000305|PubMed:8955136};
DE            EC=2.7.1.68 {ECO:0000250|UniProtKB:P70181};
DE   AltName: Full=Phosphatidylinositol 4-phosphate 5-kinase type I beta {ECO:0000305|PubMed:8955136};
DE            Short=PIP5KIbeta {ECO:0000303|PubMed:8955136};
DE   AltName: Full=Protein STM-7 {ECO:0000303|PubMed:8841185};
DE   AltName: Full=Type I phosphatidylinositol 4-phosphate 5-kinase beta {ECO:0000305|PubMed:8955136};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; ISS:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

11 Q9UJY1
ID   HSPB8_HUMAN             Reviewed;         196 AA.
DE   RecName: Full=Heat shock protein beta-8;
DE            Short=HspB8;
DE   AltName: Full=Alpha-crystallin C chain;
DE   AltName: Full=E2-induced gene 1 protein;
DE   AltName: Full=Protein kinase H11;
DE   AltName: Full=Small stress protein-like protein HSP22;
DR   GO; GO:0042802; F:identical protein binding; IPI:IntAct.
DR   GO; GO:0042803; F:protein homodimerization activity; IDA:ARUK-UCL.

12 O60331
ID   PI51C_HUMAN             Reviewed;         668 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 gamma {ECO:0000305|PubMed:12422219};
DE            Short=PIP5K1gamma {ECO:0000305|PubMed:12422219};
DE            Short=PtdIns(4)P-5-kinase 1 gamma {ECO:0000305|PubMed:12422219};
DE            EC=2.7.1.68 {ECO:0000269|PubMed:12422219, ECO:0000269|PubMed:22942276};
DE   AltName: Full=Type I phosphatidylinositol 4-phosphate 5-kinase gamma {ECO:0000250|UniProtKB:O70161};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; TAS:Reactome.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

13 Q99755
ID   PI51A_HUMAN             Reviewed;         562 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 alpha {ECO:0000305|PubMed:19158393};
DE            Short=PIP5K1-alpha {ECO:0000303|PubMed:19158393, ECO:0000303|PubMed:8955136};
DE            Short=PtdIns(4)P-5-kinase 1 alpha {ECO:0000305|PubMed:19158393};
DE            EC=2.7.1.68 {ECO:0000269|PubMed:21477596, ECO:0000269|PubMed:22942276, ECO:0000269|PubMed:8955136};
DE   AltName: Full=68 kDa type I phosphatidylinositol 4-phosphate 5-kinase alpha {ECO:0000303|PubMed:8955136};
DE   AltName: Full=Phosphatidylinositol 4-phosphate 5-kinase type I alpha {ECO:0000305|PubMed:19158393};
DE            Short=PIP5KIalpha {ECO:0000303|PubMed:19158393, ECO:0000303|PubMed:8955136};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0019900; F:kinase binding; IDA:UniProtKB.

14 Q9H6X2
ID   ANTR1_HUMAN             Reviewed;         564 AA.
DE   RecName: Full=Anthrax toxin receptor 1;
DE   AltName: Full=Tumor endothelial marker 8;
DE   Flags: Precursor;
DR   GO; GO:0051015; F:actin filament binding; IDA:UniProtKB.
DR   GO; GO:0005518; F:collagen binding; IDA:UniProtKB.
DR   GO; GO:0046872; F:metal ion binding; IEA:UniProtKB-KW.
DR   GO; GO:0004888; F:transmembrane signaling receptor activity; IDA:UniProtKB.

15 O43921
ID   EFNA2_HUMAN             Reviewed;         213 AA.
DE   RecName: Full=Ephrin-A2;
DE   AltName: Full=EPH-related receptor tyrosine kinase ligand 6;
DE            Short=LERK-6;
DE   AltName: Full=HEK7 ligand;
DE            Short=HEK7-L;
DE   Flags: Precursor;
DR   GO; GO:0046875; F:ephrin receptor binding; IBA:GO_Central.

16 Q8TCG2
ID   P4K2B_HUMAN             Reviewed;         481 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase type 2-beta;
DE            EC=2.7.1.67;
DE   AltName: Full=Phosphatidylinositol 4-kinase type II-beta;
DE            Short=PI4KII-BETA;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

17 Q9UBF8
ID   PI4KB_HUMAN             Reviewed;         816 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase beta {ECO:0000305};
DE            Short=PI4K-beta;
DE            Short=PI4Kbeta;
DE            Short=PtdIns 4-kinase beta;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:11277933, ECO:0000269|PubMed:9405935};
DE   AltName: Full=NPIK;
DE   AltName: Full=PI4K92;
DE   AltName: Full=PI4KIII {ECO:0000303|PubMed:22124328, ECO:0000303|PubMed:22258260};
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IDA:UniProtKB.
DR   GO; GO:0071889; F:14-3-3 protein binding; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

18 Q8NEB9
ID   PK3C3_HUMAN             Reviewed;         887 AA.
DE   RecName: Full=Phosphatidylinositol 3-kinase catalytic subunit type 3;
DE            Short=PI3-kinase type 3;
DE            Short=PI3K type 3;
DE            Short=PtdIns-3-kinase type 3;
DE            EC=2.7.1.137 {ECO:0000269|PubMed:7628435};
DE   AltName: Full=Phosphatidylinositol 3-kinase p100 subunit;
DE   AltName: Full=Phosphoinositide-3-kinase class 3;
DE   AltName: Full=hVps34;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IMP:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0016301; F:kinase activity; IDA:MGI.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

19 A4QPH2
ID   PI4P2_HUMAN             Reviewed;         592 AA.
DE   RecName: Full=Putative phosphatidylinositol 4-kinase alpha-like protein P2;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

#### Dissect eukariotic petals_____no_output_____

print(*epetals[("klifs",)])P35557 O43921
query_uniprot_for_go(*epetals[("klifs",)])1 P35557
ID   HXK4_HUMAN              Reviewed;         465 AA.
DE   RecName: Full=Hexokinase-4 {ECO:0000305};
DE            Short=HK4 {ECO:0000305};
DE            EC=2.7.1.1 {ECO:0000269|PubMed:11916951, ECO:0000269|PubMed:15277402, ECO:0000269|PubMed:17082186, ECO:0000269|PubMed:18322640, ECO:0000269|PubMed:19146401, ECO:0000269|PubMed:25015100, ECO:0000269|PubMed:8325892};
DE   AltName: Full=Glucokinase {ECO:0000303|PubMed:1354840};
DE   AltName: Full=Hexokinase type IV {ECO:0000250|UniProtKB:P17712};
DE            Short=HK IV {ECO:0000250|UniProtKB:P17712};
DE   AltName: Full=Hexokinase-D {ECO:0000250|UniProtKB:P17712};
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0008865; F:fructokinase activity; IBA:GO_Central.
DR   GO; GO:0004340; F:glucokinase activity; IDA:UniProtKB.
DR   GO; GO:0005536; F:glucose binding; IDA:UniProtKB.
DR   GO; GO:0019158; F:mannokinase activity; IBA:GO_Central.

2 O43921
ID   EFNA2_HUMAN             Reviewed;         213 AA.
DE   RecName: Full=Ephrin-A2;
DE   AltName: Full=EPH-related receptor tyrosine kinase ligand 6;
DE            Short=LERK-6;
DE   AltName: Full=HEK7 ligand;
DE            Short=HEK7-L;
DE   Flags: Precursor;
DR   GO; GO:0046875; F:ephrin receptor binding; IBA:GO_Central.

#### Dissect atypical petals_____no_output_____

print(*apetals[("klifs",)])Q9H6X2 A4QPH2 O75747 Q8TBX8 Q8TCG2 O00750 P42356 O14986 P48426 Q9UJY1 P78356 O60331 Q9UBF8 Q8NEB9 Q8N8J0 Q99755 Q9BTU6
query_uniprot_for_go(*apetals[("klifs",)])1 Q9H6X2
ID   ANTR1_HUMAN             Reviewed;         564 AA.
DE   RecName: Full=Anthrax toxin receptor 1;
DE   AltName: Full=Tumor endothelial marker 8;
DE   Flags: Precursor;
DR   GO; GO:0051015; F:actin filament binding; IDA:UniProtKB.
DR   GO; GO:0005518; F:collagen binding; IDA:UniProtKB.
DR   GO; GO:0046872; F:metal ion binding; IEA:UniProtKB-KW.
DR   GO; GO:0004888; F:transmembrane signaling receptor activity; IDA:UniProtKB.

2 A4QPH2
ID   PI4P2_HUMAN             Reviewed;         592 AA.
DE   RecName: Full=Putative phosphatidylinositol 4-kinase alpha-like protein P2;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

3 O75747
ID   P3C2G_HUMAN             Reviewed;        1445 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 3-kinase C2 domain-containing subunit gamma;
DE            Short=PI3K-C2-gamma;
DE            Short=PtdIns-3-kinase C2 subunit gamma;
DE            EC=2.7.1.154;
DE   AltName: Full=Phosphoinositide 3-kinase-C2-gamma;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IBA:GO_Central.
DR   GO; GO:0035005; F:1-phosphatidylinositol-4-phosphate 3-kinase activity; IBA:GO_Central.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0035091; F:phosphatidylinositol binding; IEA:InterPro.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.
DR   GO; GO:0016307; F:phosphatidylinositol phosphate kinase activity; NAS:UniProtKB.

4 Q8TBX8
ID   PI42C_HUMAN             Reviewed;         421 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 gamma {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000305|PubMed:26774281};
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II gamma;
DE            Short=PI(5)P 4-kinase type II gamma;
DE            Short=PIP4KII-gamma;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IEA:UniProtKB-EC.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0042802; F:identical protein binding; IPI:IntAct.

5 Q8TCG2
ID   P4K2B_HUMAN             Reviewed;         481 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase type 2-beta;
DE            EC=2.7.1.67;
DE   AltName: Full=Phosphatidylinositol 4-kinase type II-beta;
DE            Short=PI4KII-BETA;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

6 O00750
ID   P3C2B_HUMAN             Reviewed;        1634 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 3-kinase C2 domain-containing subunit beta;
DE            Short=PI3K-C2-beta;
DE            Short=PtdIns-3-kinase C2 subunit beta;
DE            EC=2.7.1.137 {ECO:0000269|PubMed:10805725, ECO:0000269|PubMed:11533253, ECO:0000269|PubMed:9830063};
DE            EC=2.7.1.154 {ECO:0000269|PubMed:10805725, ECO:0000269|PubMed:9830063};
DE   AltName: Full=C2-PI3K;
DE   AltName: Full=Phosphoinositide 3-kinase-C2-beta;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IDA:UniProtKB.
DR   GO; GO:0035005; F:1-phosphatidylinositol-4-phosphate 3-kinase activity; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0001727; F:lipid kinase activity; IEA:Ensembl.
DR   GO; GO:0035091; F:phosphatidylinositol binding; IEA:InterPro.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

7 P42356
ID   PI4KA_HUMAN             Reviewed;        2102 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase alpha;
DE            Short=PI4-kinase alpha;
DE            Short=PI4K-alpha;
DE            Short=PtdIns-4-kinase alpha;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:10101268};
DE   AltName: Full=Phosphatidylinositol 4-Kinase III alpha;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; ISS:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0045296; F:cadherin binding; HDA:BHF-UCL.
DR   GO; GO:0016301; F:kinase activity; IDA:MGI.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

8 O14986
ID   PI51B_HUMAN             Reviewed;         540 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 beta {ECO:0000305|PubMed:8955136};
DE            Short=PIP5K1-beta {ECO:0000303|PubMed:8955136};
DE            Short=PtdIns(4)P-5-kinase 1 beta {ECO:0000305|PubMed:8955136};
DE            EC=2.7.1.68 {ECO:0000250|UniProtKB:P70181};
DE   AltName: Full=Phosphatidylinositol 4-phosphate 5-kinase type I beta {ECO:0000305|PubMed:8955136};
DE            Short=PIP5KIbeta {ECO:0000303|PubMed:8955136};
DE   AltName: Full=Protein STM-7 {ECO:0000303|PubMed:8841185};
DE   AltName: Full=Type I phosphatidylinositol 4-phosphate 5-kinase beta {ECO:0000305|PubMed:8955136};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; ISS:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

9 P48426
ID   PI42A_HUMAN             Reviewed;         406 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 alpha {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000269|PubMed:9367159};
DE   AltName: Full=1-phosphatidylinositol 5-phosphate 4-kinase 2-alpha;
DE   AltName: Full=Diphosphoinositide kinase 2-alpha;
DE   AltName: Full=PIP5KIII;
DE   AltName: Full=Phosphatidylinositol 5-Phosphate 4-Kinase;
DE            Short=PI5P4Kalpha;
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II alpha;
DE            Short=PI(5)P 4-kinase type II alpha;
DE            Short=PIP4KII-alpha;
DE   AltName: Full=PtdIns(4)P-5-kinase B isoform;
DE   AltName: Full=PtdIns(4)P-5-kinase C isoform;
DE   AltName: Full=PtdIns(5)P-4-kinase isoform 2-alpha;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IDA:FlyBase.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

10 Q9UJY1
ID   HSPB8_HUMAN             Reviewed;         196 AA.
DE   RecName: Full=Heat shock protein beta-8;
DE            Short=HspB8;
DE   AltName: Full=Alpha-crystallin C chain;
DE   AltName: Full=E2-induced gene 1 protein;
DE   AltName: Full=Protein kinase H11;
DE   AltName: Full=Small stress protein-like protein HSP22;
DR   GO; GO:0042802; F:identical protein binding; IPI:IntAct.
DR   GO; GO:0042803; F:protein homodimerization activity; IDA:ARUK-UCL.

11 P78356
ID   PI42B_HUMAN             Reviewed;         416 AA.
DE   RecName: Full=Phosphatidylinositol 5-phosphate 4-kinase type-2 beta {ECO:0000305};
DE            EC=2.7.1.149 {ECO:0000305|PubMed:26774281};
DE   AltName: Full=1-phosphatidylinositol 5-phosphate 4-kinase 2-beta;
DE   AltName: Full=Diphosphoinositide kinase 2-beta;
DE   AltName: Full=Phosphatidylinositol 5-phosphate 4-kinase type II beta;
DE            Short=PI(5)P 4-kinase type II beta;
DE            Short=PIP4KII-beta;
DE   AltName: Full=PtdIns(5)P-4-kinase isoform 2-beta;
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0016309; F:1-phosphatidylinositol-5-phosphate 4-kinase activity; IEA:UniProtKB-EC.
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0005525; F:GTP binding; IDA:UniProtKB.
DR   GO; GO:0042803; F:protein homodimerization activity; IDA:CAFA.

12 O60331
ID   PI51C_HUMAN             Reviewed;         668 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 gamma {ECO:0000305|PubMed:12422219};
DE            Short=PIP5K1gamma {ECO:0000305|PubMed:12422219};
DE            Short=PtdIns(4)P-5-kinase 1 gamma {ECO:0000305|PubMed:12422219};
DE            EC=2.7.1.68 {ECO:0000269|PubMed:12422219, ECO:0000269|PubMed:22942276};
DE   AltName: Full=Type I phosphatidylinositol 4-phosphate 5-kinase gamma {ECO:0000250|UniProtKB:O70161};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; TAS:Reactome.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.

13 Q9UBF8
ID   PI4KB_HUMAN             Reviewed;         816 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase beta {ECO:0000305};
DE            Short=PI4K-beta;
DE            Short=PI4Kbeta;
DE            Short=PtdIns 4-kinase beta;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:11277933, ECO:0000269|PubMed:9405935};
DE   AltName: Full=NPIK;
DE   AltName: Full=PI4K92;
DE   AltName: Full=PI4KIII {ECO:0000303|PubMed:22124328, ECO:0000303|PubMed:22258260};
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IDA:UniProtKB.
DR   GO; GO:0071889; F:14-3-3 protein binding; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

14 Q8NEB9
ID   PK3C3_HUMAN             Reviewed;         887 AA.
DE   RecName: Full=Phosphatidylinositol 3-kinase catalytic subunit type 3;
DE            Short=PI3-kinase type 3;
DE            Short=PI3K type 3;
DE            Short=PtdIns-3-kinase type 3;
DE            EC=2.7.1.137 {ECO:0000269|PubMed:7628435};
DE   AltName: Full=Phosphatidylinositol 3-kinase p100 subunit;
DE   AltName: Full=Phosphoinositide-3-kinase class 3;
DE   AltName: Full=hVps34;
DR   GO; GO:0016303; F:1-phosphatidylinositol-3-kinase activity; IMP:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0016301; F:kinase activity; IDA:MGI.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

15 Q8N8J0
ID   PI4P1_HUMAN             Reviewed;         262 AA.
DE   RecName: Full=Putative inactive phosphatidylinositol 4-kinase alpha-like protein P1;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IBA:GO_Central.
DR   GO; GO:0052742; F:phosphatidylinositol kinase activity; IBA:GO_Central.

16 Q99755
ID   PI51A_HUMAN             Reviewed;         562 AA.
DE   RecName: Full=Phosphatidylinositol 4-phosphate 5-kinase type-1 alpha {ECO:0000305|PubMed:19158393};
DE            Short=PIP5K1-alpha {ECO:0000303|PubMed:19158393, ECO:0000303|PubMed:8955136};
DE            Short=PtdIns(4)P-5-kinase 1 alpha {ECO:0000305|PubMed:19158393};
DE            EC=2.7.1.68 {ECO:0000269|PubMed:21477596, ECO:0000269|PubMed:22942276, ECO:0000269|PubMed:8955136};
DE   AltName: Full=68 kDa type I phosphatidylinositol 4-phosphate 5-kinase alpha {ECO:0000303|PubMed:8955136};
DE   AltName: Full=Phosphatidylinositol 4-phosphate 5-kinase type I alpha {ECO:0000305|PubMed:19158393};
DE            Short=PIP5KIalpha {ECO:0000303|PubMed:19158393, ECO:0000303|PubMed:8955136};
DR   GO; GO:0016308; F:1-phosphatidylinositol-4-phosphate 5-kinase activity; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IEA:UniProtKB-KW.
DR   GO; GO:0019900; F:kinase binding; IDA:UniProtKB.

17 Q9BTU6
ID   P4K2A_HUMAN             Reviewed;         479 AA.
DE   RecName: Full=Phosphatidylinositol 4-kinase type 2-alpha;
DE            EC=2.7.1.67 {ECO:0000269|PubMed:11279162, ECO:0000269|PubMed:20388919, ECO:0000269|PubMed:24675427, ECO:0000269|PubMed:25168678};
DE   AltName: Full=Phosphatidylinositol 4-kinase type II-alpha;
DR   GO; GO:0004430; F:1-phosphatidylinositol 4-kinase activity; IDA:UniProtKB.
DR   GO; GO:0035651; F:AP-3 adaptor complex binding; IDA:UniProtKB.
DR   GO; GO:0005524; F:ATP binding; IDA:UniProtKB.
DR   GO; GO:0000287; F:magnesium ion binding; NAS:UniProtKB.

### Interactive Venn diagrams_____no_output_____

from ipywidgets import SelectMultiple, HBox, HTML, interact, fixed, Output
def interactive_venn(datasets, showkey=None):
    petals = {tuple(sorted(k)): v for (k, v) in compute_petals(datasets).items()}
    key = tuple(sorted(showkey))

    value = "Clickable UniprotIDs will appear here when you click on one more datasets"
    if showkey:
        uniprotids = list(petals[key])
        lines = [
            f"{','.join(key)} ({len(uniprotids)}):
", "
"
        ]
        lines += [f"
{i}
" for i in uniprotids]
        lines.append("
")
        value = "".join(lines)
    text = HTML(
        value=value,
        placeholder="Clickable UniprotIDs will appear here when you click on one more datasets",
        disabled=True,
        layout={"height": "95%"}
    )

    out = Output()
    with out:
        plot = plot_venn(datasets)
        plt.show()

    display(HBox([out, text], layout={"height": "100%"}))_____no_output_____selection = SelectMultiple(
    options=list(uniprot_ids.keys()),
    value=[],
    description="Datasets",
)
interact(interactive_venn, datasets=fixed(uniprot_ids), showkey=selection);_____no_output_____

## Discussion_____no_output_____Looks like KLIFS is selecting some non-protein kinases like:

* phosphatidylinositol kinases
* saccharide kinases
* other proteins that bind ATP

> TODO: Why?_____no_output_____## Generate outputs

Since there'no agreement to what a kinase *is*, we will output everything already considered by the five listings we have analyzed._____no_output_____Let's build the final dataframe with all kinases:_____no_output_____

kinases = pd.concat(
    [
        kinhub[["HGNC Name", "UniprotID"]].rename(columns={"HGNC Name": "Name"}).assign(kinhub=1),
        klifs[["HGNC Name", "UniprotID"]].rename(columns={"HGNC Name": "Name"}).assign(klifs=1),
        pkinfam[["Name", "UniprotID"]].assign(pkinfam=1),
        reviewed_uniprot[["Entry name", "UniprotID"]].rename(columns={"Entry name": "Name"}).assign(reviewed_uniprot=1),
        dunbrack_msa[["4_Uni_entry", "UniprotID"]].rename(columns={"4_Uni_entry": "Name"}).assign(dunbrack_msa=1)
    ]
)
kinases_____no_output_____

Since there are different names assigned to each Uniprot ID depending on the source, we will just concatenate them in the same column_____no_output_____

keys = ["kinhub", "klifs", "pkinfam", "reviewed_uniprot", "dunbrack_msa"]
kinases["Name"] = kinases.transform({'Name': lambda v: v.replace("_HUMAN", "")})
boolsum = lambda v: v.sum() > 0
joinset = lambda v: '|'.join([x for x in set(v) if x])
grouped = kinases.groupby(["UniprotID"], as_index=False).agg(dict(**{'Name': joinset}, **{k: boolsum for k in keys}))
grouped_____no_output_____# example Uniprot ID with different names
grouped[grouped.UniprotID == "Q96LW2"]_____no_output_____

We'll export the multi-kinase dataframe to a CSV in the repo:_____no_output_____

grouped.to_csv(DATA / "human_kinases.aggregated.csv")_____no_output_____


# Notebook from jacknlliu/lyapunovearner
Path: notes/.ipynb_checkpoints/torobo_control-checkpoint.ipynb

import PyKDL

# create a vector
v = PyKDL.Vector(1,3,5)

# create a rotation from Roll Pitch, Yaw angles
r1 = PyKDL.Rotation.RPY(1.2, 3.4, 0)

# create a rotation from XYZ Euler angles
r2 = PyKDL.Rotation.EulerZYX(0, 1, 0)

# create a rotation from a rotation matrix
r3 = PyKDL.Rotation(1,0,0, 0,1,0, 0,0,1)

# create a frame from a vector and a rotation
f = PyKDL.Frame(r2, v)_____no_output_____import numpy as np
import time, sys, os
import h5py
import logging
import scipy.linalg
import scipy.linalg as LA
from scipy.optimize import minimize
import matplotlib

import os
from os.path import join, expanduser
%matplotlib inline
import matplotlib.pyplot as plt
##LOGGER = logging.getLogger(__name__)_____no_output_____

#### gather all the cartesian points and velocities
+ data is thus arranged:
  - data = [
             \zeta^0, \zeta^1, \zeta^\star;
             \dot{\zeta^0}, \dot{\zeta^1}, \dot{\zeta^\star}
            ]
  - where \zeta \in R^n, n being the dimension in cartesian coordinates of the
  - note that our data is in 2D for now, i.e. x and y axes in Cartesian space

  In other words, data is thus shaped:

  data =
                  point p1                               point p2                          point p3                  target joint space angles
         [x1,  x2,  x3,  x4,  x5,  x6,  x7 | x1,  x2,  x3,  x4,  x5,  x6,  x7 | x1,  x2,  x3,  x4,  x5,  x6,  x7 | x1,  x2,  x3,  x4,  x5,  x6,  x7 ]
         [y1,  y2,  y3,  y4,  y5,  y6,  y7 | y1,  y2,  y3,  y4,  y5,  y6,  y7 | y1,  y2,  y3,  y4,  y5,  y6,  y7 | y1,  y2,  y3,  y4,  y5,  y6,  y7 ]
         [x1d, x2d, x3d, x4d, x5d, x6d, x7d| x1d, x2d, x3d, x4d, x5d, x6d, x7d| x1d, x2d, x3d, x4d, x5d, x6d, x7d| x1d, x2d, x3d, x4d, x5d, x6d, x7d]
         [y1d, y2d, y3d, y4d, y5d, y6d, y7d| y1d, y2d, y3d, y4d, y5d, y6d, y7d| y1d, y2d, y3d, y4d, y5d, y6d, y7d| y1d, y2d, y3d, y4d, y5d, y6d, y7d]_____no_output_____

filename = '../scripts/{}.h5'.format('torobo_processed_data')
with h5py.File(filename, 'r+') as f:
    data = f['data/data'].value
print(data.shape)

# gmm = GMM()(4, 21)
x0 = data[:, :14]
xT = data[:, 14:]_____no_output_____

#### now compute the priors, mus and sigmas of the Gaussian Mixture Model_____no_output_____

def guess_init_lyap(data, Vxf0, b_initRandom=False):
    """
    This function guesses the initial lyapunov function
    """
    # allocate spaces for incoming arrays
    Vxf0['Mu']  =  np.zeros(( Vxf0['d'], Vxf0['L']+1 )) # will be 2x2
    Vxf0['P']   =  np.zeros(( Vxf0['d'], Vxf0['d'], Vxf0['L']+1)) # wil be 2x2x3

    if b_initRandom:
        lengthScale = np.sqrt(np.var(data[:Vxf0['d'],:].T, axis=0))
        lengthScale = np.ravel(lengthScale)
        '''
         If `rowvar` is True (default), then each row represents a
        variable, with observations in the columns. Otherwise, the relationship
        is transposed: each column represents a variable, while the rows
        contain observations.
        '''
        #tempcov = np.cov(np.var(data[:Vxf0['d'],:], axis=0), rowvar=False)
        lengthScaleMatrix = LA.sqrtm(np.cov(np.var(data[:Vxf0['d'],:], axis=0), rowvar=False))
        Vxf0['Priors'] = np.random.rand(Vxf0['L']+1,1)

        for l in range(Vxf0['L']+1):
            tempMat = np.random.randn(Vxf0['d'], Vxf0['d'])
            Vxf0['Mu'][:,l] = np.multiply(np.random.randn(Vxf0['d'],1), lengthScale)
            Vxf0['P'][:,:,l] = lengthScaleMatrix.dot((tempMat * tempMat.T)).dot(lengthScaleMatrix)
    else:
        Vxf0['Priors'] = np.ones((Vxf0['L']+1, 1))
        Vxf0['Priors'] = Vxf0['Priors']/np.sum(Vxf0['Priors'])
        Vxf0['Mu'] = np.zeros((Vxf0['d'], Vxf0['L']+1))

        Vxf0['P']   =  np.zeros(( Vxf0[ 'd'], Vxf0['d'], Vxf0['L']+1)) # wil be 2x2x3
        for l in range(Vxf0['L']+1):
            Vxf0['P'][:,:,l] = np.eye((Vxf0['d']))

    Vxf0.update(Vxf0)

    return Vxf0_____no_output_____Vxf0 = {
    'L': None,
    'd': None,
    'w': 1e-4, #A positive scalar weight regulating the priority between the two objectives of the opitmization. Please refer to the page 7 of the paper for further information.
    'Mu': np.array(()),
    'P': np.array(()),
}

options = {
    'tol_mat_bias': 1e-1,
    'display': 1,
    'tol_stopping': 1e-10,
    'max_iter': 500,
    'optimizePriors': True,
    'upperBoundEigenValue': True,
}

hyperparams = {
    'use_cvxopt': True, #whether to use cvxopt package, fmincon or otherwise
}

options = {
    'tol_mat_bias': 1e-1,
    'display': 1,
    'tol_stopping': 1e-10,
    'max_iter':500,
    'optimizePriors': True,
    'upperBoundEigenValue': True
}

""" This file defines a Gaussian mixture model class. """

def logsum(vec, axis=0, keepdims=True):
    #TODO: Add a docstring.
    maxv = np.max(vec, axis=axis, keepdims=keepdims)
    maxv[maxv == -float('inf')] = 0
    return np.log(np.sum(np.exp(vec-maxv), axis=axis, keepdims=keepdims)) + maxv

def check_sigma(A):
    """
        checks if the sigma matrix is symmetric
        positive definite before inverting via cholesky decomposition
    """
    eigval = np.linalg.eigh(A)[0]
    if np.array_equal(A, A.T) and np.all(eigval>0):
        # LOGGER.debug("sigma is pos. def. Computing cholesky factorization")
        return A
    else:
        # find lowest eigen value
        eta = 1e-6  # regularizer for matrix multiplier
        low = np.amin(np.sort(eigval))
        Anew = low * A + eta * np.eye(A.shape[0])
        return Anew

class GMM(object):
    """ Gaussian Mixture Model. """
    def __init__(self, init_sequential=False, eigreg=False, warmstart=True):
        self.init_sequential = init_sequential
        self.eigreg = eigreg
        self.warmstart = warmstart
        self.sigma = None

    def inference(self, pts):
        """
        Evaluate dynamics prior.
        Args:
            pts: A N x D array of points.
        """
        # Compute posterior cluster weights.
        logwts = self.clusterwts(pts)

        # Compute posterior mean and covariance.
        mu0, Phi = self.moments(logwts)

        # Set hyperparameters.
        m = self.N
        n0 = m - 2 - mu0.shape[0]

        # Normalize.
        m = float(m) / self.N
        n0 = float(n0) / self.N
        return mu0, Phi, m, n0

    def clusterwts(self, data):
        """
        Compute cluster weights for specified points under GMM.
        Args:
            data: An N x D array of points
        Returns:
            A K x 1 array of average cluster log probabilities.
        """
        # Compute probability of each point under each cluster.
        logobs = self.estep(data)

        # Renormalize to get cluster weights.
        logwts = logobs - logsum(logobs, axis=1)

        # Average the cluster probabilities.
        logwts = logsum(logwts, axis=0) - np.log(data.shape[0])
        return logwts.T

    def estep(self, data):
        """
        Compute log observation probabilities under GMM.
        Args:
            data: A N x D array of points.
        Returns:
            logobs: A N x K array of log probabilities (for each point
                on each cluster).
        """
        # Constants.
        N, D = data.shape
        K = self.sigma.shape[0]

        logobs = -0.5*np.ones((N, K))*D*np.log(2*np.pi)
        for i in range(K):
            mu, sigma = self.mu[i], self.sigma[i]
            sigma = sigma
            L = scipy.linalg.cholesky(sigma, lower=True)
            logobs[:, i] -= np.sum(np.log(np.diag(L)))
            diff = (data - mu).T
            soln = scipy.linalg.solve_triangular(L, diff, lower=True)
            logobs[:, i] -= 0.5*np.sum(soln**2, axis=0)

        logobs += self.logmass.T
        return logobs

    def moments(self, logwts):
        """
            Compute the moments of the cluster mixture with logwts.
            Args:
                logwts: A K x 1 array of log cluster probabilities.
            Returns:
                mu: A (D,) mean vector.
                sigma: A D x D covariance matrix.
        """
        # Exponentiate.
        wts = np.exp(logwts)

        # Compute overall mean.
        mu = np.sum(self.mu * wts, axis=0)

        # Compute overall covariance.
        diff = self.mu - np.expand_dims(mu, axis=0)
        diff_expand = np.expand_dims(self.mu, axis=1) * \
                np.expand_dims(diff, axis=2)
        wts_expand = np.expand_dims(wts, axis=2)
        sigma = np.sum((self.sigma + diff_expand) * wts_expand, axis=0)
        return mu, sigma

    def update(self, data, K, max_iterations=100):
        """
        Run EM to update clusters.
        Args:
            data: An N x D data matrix, where N = number of data points.
            K: Number of clusters to use.
        """
        # Constants.
        N  = data.shape[0]
        Do = data.shape[1]

        LOGGER.debug('Fitting GMM with %d clusters on %d points.', K, N)

        if (not self.warmstart or self.sigma is None or K != self.sigma.shape[0]):
            # Initialization.
            LOGGER.debug('Initializing GMM.')
            self.sigma = np.zeros((K, Do, Do))
            self.mu = np.zeros((K, Do))
            self.logmass = np.log(1.0 / K) * np.ones((K, 1))
            self.mass = (1.0 / K) * np.ones((K, 1))
            self.N = data.shape[0]
            N = self.N

            # Set initial cluster indices.
            if not self.init_sequential:
                cidx = np.random.randint(0, K, size=(1, N))
            else:
                raise NotImplementedError()

            # Initialize.
            for i in range(K):
                cluster_idx = (cidx == i)[0]
                mu = np.mean(data[cluster_idx, :], axis=0)
                diff = (data[cluster_idx, :] - mu).T
                sigma = (1.0 / K) * (diff.dot(diff.T))
                self.mu[i, :] = mu
                self.sigma[i, :, :] = sigma + np.eye(Do) * 2e-6

        prevll = -float('inf')
        for itr in range(max_iterations):
            # E-step: compute cluster probabilities.
            logobs = self.estep(data)

            # Compute log-likelihood.
            ll = np.sum(logsum(logobs, axis=1))
            LOGGER.debug('GMM itr %d/%d. Log likelihood: %f',
                         itr, max_iterations, ll)
            if ll < prevll:
                # TODO: Why does log-likelihood decrease sometimes?
                LOGGER.debug('Log-likelihood decreased! Ending on itr=%d/%d',
                             itr, max_iterations)
                break
            if np.abs(ll-prevll) < 1e-5*prevll:
                LOGGER.debug('GMM converged on itr=%d/%d',
                             itr, max_iterations)
                break
            prevll = ll

            # Renormalize to get cluster weights.
            logw = logobs - logsum(logobs, axis=1)
            assert logw.shape == (N, K)

            # Renormalize again to get weights for refitting clusters.
            logwn = logw - logsum(logw, axis=0)
            assert logwn.shape == (N, K)
            w = np.exp(logwn)

            # M-step: update clusters.
            # Fit cluster mass.
            self.logmass = logsum(logw, axis=0).T
            self.logmass = self.logmass - logsum(self.logmass, axis=0)
            assert self.logmass.shape == (K, 1)
            self.mass = np.exp(self.logmass)

            # Reboot small clusters.
            w[:, (self.mass < (1.0 / K) * 1e-4)[:, 0]] = 1.0 / N
            # Fit cluster means.
            w_expand = np.expand_dims(w, axis=2)
            data_expand = np.expand_dims(data, axis=1)
            self.mu = np.sum(w_expand * data_expand, axis=0)
            # Fit covariances.
            wdata = data_expand * np.sqrt(w_expand)
            assert wdata.shape == (N, K, Do)
            for i in range(K):
                # Compute weighted outer product.
                XX = wdata[:, i, :].T.dot(wdata[:, i, :])
                mu = self.mu[i, :]
                self.sigma[i, :, :] = XX - np.outer(mu, mu)

                if self.eigreg:  # Use eigenvalue regularization.
                    raise NotImplementedError()
                else:  # Use quick and dirty regularization.
                    sigma = self.sigma[i, :, :]
                    self.sigma[i, :, :] = 0.5 * (sigma + sigma.T) + \
                            1e-6 * np.eye(Do)

def gaussPDF(data, mu, sigma):
    nbVar, nbdata = data.shape

    data = data.T - np.tile(mu.T, [nbdata,1])
    prob = np.sum((data/sigma)*data, axis=1);
    prob = np.exp(-0.5*prob) / np.sqrt((2*np.pi)**nbVar *
                                       (np.abs(np.linalg.det(sigma))+
                                        np.finifo(np.float64).min))
    return prob

def GMR(Priors, Mu, Sigma, x, inp, out, nargout=3):
    nbData = x.shape[1]
    nbVar = Mu.shape[0]
    nbStates = Sigma.shape[2]

    Pxi = np.zeros_like(Priors)
    for i in range(nbStates):
        Pxi[:,i] = Priors[i] * gaussPDF(x, Mu[inp,i], Sigma[inp,inp,i])

    beta = Pxi / np.tile(np.sum(Pxi,axis=1) +
                         np.finfo(np.float32).min, [1,nbStates])
    #########################################################################
    for j in range(nbStates):
        y_tmp[:,:,j] = np.tile(Mu[out,j],[1,nbData]) \
                     + Sigma[out,inp,j]/(Sigma[inp,inp,j]).dot(x-np.tile(Mu[inp,j],[1,nbData]))

    beta_tmp = beta.reshape(1, beta.shape)
    y_tmp2 = np.tile(beta_tmp,[matlength(out), 1, 1]) * y_tmp
    y = np.sum(y_tmp2,axis=2)
    ## Compute expected covariance matrices Sigma_y, given input x
    #########################################################################
    if nargout > 1:
        for j in range(nbStates):
            Sigma_y_tmp[:,:,0,j] = Sigma[out,out,j] \
                                   - (Sigma[out,inp,j]/(Sigma[inp,inp,j])  \
                                   * Sigma[inp,out,j])

        beta_tmp = beta.reshape(1, 1, beta.shape)
        Sigma_y_tmp2 = np.tile(beta_tmp * beta_tmp, \
                               [matlength(out), matlength(out), 1, 1]) * \
                                np.tile(Sigma_y_tmp,[1, 1, nbData, 1])
        Sigma_y = np.sum(Sigma_y_tmp2, axis=3)

    return y, Sigma_y, beta

def obj(p, x, xd, d, L, w, options):
    Vxf         = shape_DS(p,d,L,options)
    _, Vx       = computeEnergy(x, None, Vxf)
    Vdot        = np.sum(Vx*xd, axis=0)  #derivative of J w.r.t. xd
    norm_Vx     = np.sqrt(np.sum(Vx * Vx, axis=0))
    norm_xd     = np.sqrt(np.sum(xd * xd, axis=0))
    J           = np.divide(Vdot, np.multiply(norm_Vx, norm_xd))#.squeeze()

    # projections onto positive orthant
    J[np.where(norm_xd==0)] = 0
    J[np.where(norm_Vx==0)] = 0
    J[np.where(Vdot>0)]     = J[np.where(Vdot>0)]**2      # solves psi(t,n)**2
    J[np.where(Vdot<0)]     = -w*J[np.where(Vdot<0)]**2   # # J should be (1, 750)
    J                       = np.sum(J)
    dJ                      = None

    return J#, dJ

def optimize(obj_handle, p0):
    opt = minimize(
        obj_handle,
        x0=p0,
        method='L-BFGS-B',
        jac=False,
        bounds=[(0.0, None) for _ in range(len(p0))], # no negative p values
        #bounds = Bounds(ctr_handle(p0), keep_feasible=True), # will produce c, ceq as lb and ub
        options={'ftol': 1e-4, 'disp': True}
        )
    return opt

def gmm_2_parameters(Vxf, options):
    # transforming optimization parameters into a column vector
    d = Vxf['d']
    if Vxf['L'] > 0:
        if options['optimizePriors']:
            p0 = np.vstack((
                           np.expand_dims(np.ravel(Vxf['Priors']), axis=1), # will be a x 1
                           np.expand_dims(Vxf['Mu'][:,1:], axis=1).reshape(Vxf['L']*d,1)
                        ))
        else:
            p0 = Vxf['Mu'][:,2:].reshape(Vxf['L']*d, 1) #print(p0) # p0 will be 4x1
    else:
        p0 = []

    for k in range(Vxf['L']):
        p0 = np.vstack((
                      p0,
                      Vxf['P'][:,:,k+1].reshape(d**2,1)
                    ))
    return p0

def parameters_2_gmm(popt, d, L, options):
    # transforming the column of parameters into Priors, Mu, and P
    Vxf = shape_DS(popt, d, L, options)

    return Vxf

def shape_DS(p,d,L,options):
    # transforming the column of parameters into Priors, Mu, and P
    P = np.zeros((d,d,L+1))
    optimizePriors = options['optimizePriors']
    if L == 0:
        Priors = 1
        Mu = np.zeros((d,1))
        i_c = 1
    else:
        if options['optimizePriors']:
            Priors = p[:L+1]
            i_c = L+1
        else:
            Priors = np.ones((L+1,1))
            i_c = 0

        Priors = np.divide(Priors, np.sum(Priors))
        Mu = np.hstack((np.zeros((d,1)), p[[i_c+ x for x in range(d*L)]].reshape(d,L)))
        i_c = i_c+d*L+1

    for k in range(L):
        P[:,:,k+1] = p[range(i_c+k*(d**2)-1,i_c+(k+1)*(d**2)-1)].reshape(d,d)

    Vxf           = dict()
    Vxf['Priors'] = Priors
    Vxf['Mu']     = Mu
    Vxf['P']      = P
    Vxf['SOS']    = 0

    return Vxf

def matVecNorm(x):
    return np.sqrt(np.sum(x**2, axis=0))

def matlength(x):
  # find the max of a numpy matrix dims
  return np.max(x.shape)

def ctr_eigenvalue(p,d,L,options):
    Vxf = dict()
    if L == -1: # SOS
        Vxf['d'] = d
        Vxf['n'] = int(np.sqrt(matlength(p)/d**2))
        Vxf['P'] = p.reshape(Vxf['n']*d,Vxf['n']*d)
        Vxf['SOS'] = 1
        c  = np.zeros((Vxf['n']*d))
        ceq = []
    else:
        Vxf = shape_DS(p,d,L,options)
        if L > 0:
            c  = np.zeros([(L+1)*d+(L+1)*options['optimizePriors']])  #+options.variableSwitch
            if options['upperBoundEigenValue']:
                ceq = np.zeros((L+1))
            else:
                ceq = []
        else:
            c  = np.zeros((d))
            ceq = Vxf['P'].T.ravel().dot(Vxf['P'].ravel()) - 2

    dc = []
    dceq = []

    if L == -1:  # SOS
        c = -LA.eigvals(Vxf['P'] + Vxf['P'].T - np.eye(Vxf['n']*d)*options['tol_mat_bias'])
    else:
        for k in range(L):
            lambder = LA.eigvals(Vxf['P'][:,:,k+1] + (Vxf['P'][:,:,k+1]).T)/2.0
            c[k*d:(k+1)*d] = -lambder.real + options['tol_mat_bias']
            if options['upperBoundEigenValue']:
                ceq[k+1] = 1.0 - np.sum(lambder.real)

    if L > 0 and options['optimizePriors']:
        c[(L+1)*d:(L+1)*d+L+1] = -Vxf['Priors'].squeeze()

    return c, ceq

def learnEnergy(Vxf0, Data, options):
    d = int(Data.shape[0]/2)
    x = Data[:d,:]
    xd = Data[d:2*d,:]
    Vxf0['SOS'] = False

    # Transform the Lyapunov model to a vector of optimization parameters
    if Vxf0['SOS']:
        p0 = npr.randn(d*Vxf0['n'], d*Vxf0['n']);
        p0 = p0.dot(p0.T)
        p0 = np.ravel(p0)
        Vxf0['L'] = -1; # to distinguish sos from other methods
    else:
        for l in range(Vxf0['L']):
            try:
                Vxf0['P'][:,:,l+1] = scipy.linalg.solve(Vxf0['P'][:,:,l+1], np.eye(d))
                #print('Vxf0[:,:,l+1]: ', Vxf0['P'][:,:,l+1])
            except LA.LinAlgError as e:
                LOGGER.debug('LinAlgError: %s', e)

        # in order to set the first component to be the closest Gaussian to origin
        to_sort = matVecNorm(Vxf0['Mu'])
        idx = np.argsort(to_sort, kind='mergesort')
        Vxf0['Mu'] = Vxf0['Mu'][:,idx]
        Vxf0['P']  = Vxf0['P'][:,:,idx]
        p0 = gmm_2_parameters(Vxf0,options)

    obj_handle = lambda p: obj(p, x, xd, d, Vxf0['L'], Vxf0['w'], options)
    ctr_handle = lambda p: ctr_eigenvalue(p, d, Vxf0['L'], options)

    optim_res = optimize(obj_handle, p0)
    popt, J = optim_res.x, optim_res.fun

    if Vxf0['SOS']:
        Vxf['d']    = d
        Vxf['n']    = Vxf0['n']
        Vxf['P']    = popt.reshape(Vxf['n']*d,Vxf['n']*d)
        Vxf['SOS']  = 1
        Vxf['p0']   = compute_Energy(zeros(d,1),[],Vxf)
        #check_constraints(popt,ctr_handle,d,0,options)
    else:
        # transforming back the optimization parameters into the GMM model
        Vxf             = parameters_2_gmm(popt,d,Vxf0['L'],options)
        Vxf['Mu'][:,0]  = 0
        Vxf['L']        = Vxf0['L']
        Vxf['d']        = Vxf0['d']
        Vxf['w']        = Vxf0['w']
        #check_constraints(popt,ctr_handle,d,Vxf['L'],options)

    sumDet = 0
    for l in range(Vxf['L']+1):
        sumDet += np.linalg.det(Vxf['P'][:,:,l])

    Vxf['P'][:,:,0] = Vxf['P'][:,:,0]/sumDet
    Vxf['P'][:,:,1:] = Vxf['P'][:,:,1:]/np.sqrt(sumDet)

    return Vxf, J

def computeEnergy(X,Xd,Vxf, nargout=2):
    d = X.shape[0]
    nDemo = 1
    if nDemo>1:
        X = X.reshape(d,-1)
        Xd = Xd.reshape(d,-1)

    if Vxf['SOS']:
        V, dV = sos_lyapunov(X, Vxf['P'], Vxf['d'], Vxf['n'])
        if 'p0' in Vxf:
            V -= Vxf['p0']
    else:
        V, dV = gmr_lyapunov(X, Vxf['Priors'], Vxf['Mu'], Vxf['P'])

    if nargout > 1:
        if not Xd:
            Vdot = dV
        else:
            Vdot = np.sum(Xd*dV, axis=0)
    if nDemo>1:
        V = V.reshape(-1, nDemo).T
        if nargout > 1:
            Vdot = Vdot.reshape(-1, nDemo).T

    return V, Vdot

def gmr_lyapunov(x, Priors, Mu, P):
    # print('x.shape: ', x.shape)
    nbData = x.shape[1]
    d = x.shape[0]
    L = P.shape[2]-1;

    # Compute the influence of each GMM component, given input x
    for k in range(L):
        P_cur               = P[:,:,k+1]
        if k                == 0:
            V_k             = np.sum(x * (P_cur.dot(x)), axis=0)
            V               = Priors[k+1]*(V_k)
            Vx              = Priors[k+1]*((P_cur+P_cur.T).dot(x))
        else:
            x_tmp           = x - np.tile(Mu[:,k+1], [nbData, 1]).T
            V_k             = np.sum(P_cur.dot(x_tmp)*x, axis=0)
            V_k[V_k < 0]    = 0
            V              += Priors[k+1] * (V_k ** 2)
            temp            = (2 * Priors[k+1]) * (V_k)
            Vx              = Vx + np.tile(temp, [d,1])*(P_cur.dot(x_tmp) + P_cur.T.dot(x))

    return V, Vx
_____no_output_____def dsStabilizer(X, fn_handle, Vxf, rho0, kappa0):
    d = Vxf['d']
    if X.shape[0] == 2*d:
        Xd     = X[d:2*d,:]
        X      = X[:d,:]
    else:
        if (len(getfullargspec(fn_handle).args) == 1):
            Xd, _, _ = fn_handle(X)
        elif (len(getfullargspec(fn_handle).args) == 2):
            t  = X[d,:]
            X  = X[d:]
            Xd, _, _ = fn_handle(t,X)
        else:
            logger.CRITICAL('Unknown function handle!')

    V,Vx    = computeEnergy(X,[],Vxf)
    norm_Vx = np.sum(V ** 2, axis=0)
    norm_x  = np.sum(X ** 2,axis=0)

    Vdot    = np.sum(Vx * Xd,axis=0)
    rho     = rho0 * (1-np.exp(-kappa0*norm_x)) * np.sqrt(norm_Vx)
    ind     = (Vdot + rho) >= 0
    #ind     = np.where((Vdot + rho) >= 0)
    u       = Xd * 0

    print(np.sum(ind))
    if np.sum(ind)>0:
        lambder   = (Vdot[ind] + rho[ind]) / norm_Vx[ind]
        u[:,ind]  = -np.tile(lambder,[d,1]) * Vx[:,ind]
        Xd[:,ind] = Xd[:,ind] + u[:,ind]

#     if args:
#         dt = args[0]
#         xn = X + np.dot(Xd, dt)
#         Vn = computeEnergy(xn,[],Vxf)
#         ind = (Vn >= V)
#         i = 0

#         while(np.any(ind) and i < 10):
#             alpha = np.divide(V[ind], Vn[ind])
#             Xd[:,ind] = np.tile(alpha,[d,1]) * Xd[:,ind] - \
#                         np.tile(alpha * np.sum(Xd[:,ind] * \
#                         Vx[:,ind], axis=0)/norm_Vx[ind],[d,1])*Vx[:,ind]
#             xn = X + np.dot(Xd,dt)
#             Vn = computeEnergy(xn,np.array(()),Vxf)
#             ind = (Vn >= V)
#             i = i + 1

    return Xd, u_____no_output_____Vxf0['L'] = 2   # number of asymmetric quadratic components for L >= 0
Vxf0['d'] = int(data.shape[0]/2)
Vxf0.update(Vxf0)
# A set of options that will be passed to the solver
options = {
    'tol_mat_bias': 1e-1,
    'display': 1,
    'tol_stopping': 1e-10,
    'max_iter':500,
    'optimizePriors': True,
    'upperBoundEigenValue': True
}

Vxf0 = guess_init_lyap(data, Vxf0, b_initRandom=False)
# for k, v in Vxf0.items():
#     print(k, v)
# print('\n')
Vxf, J = learnEnergy(Vxf0, data, options)
# for k, v in Vxf.items():
#     print(k, v)
# print('J: ', J)`
# print('\n')
gmm.update(data.T, K=6, max_iterations=100)
mu, sigma, priors = gmm.mu, gmm.sigma, gmm.logmass

print(mu.shape, sigma.shape, priors.shape)
inp = range(0, Vxf['d'])
out = range(Vxf['d'], 2* Vxf['d'])
gmr_handle = lambda x: GMR(priors, mu, sigma, x, inp, out)
rho0, kappa0 = 1.0, 1.0
Xd, u = dsStabilizer(data, gmr_handle, Vxf, rho0, kappa0)
print(Xd.shape, u.shape)/home/olalekan/anaconda3/envs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:292: RuntimeWarning: invalid value encountered in true_divide
/home/olalekan/anaconda3/envs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:473: RuntimeWarning: invalid value encountered in sqrt
def ctr_eigenvalue(p,d,L,options):
    Vxf = dict()
    if L == -1: # SOS
        Vxf['d'] = d
        Vxf['n'] = int(np.sqrt(matlength(p)/d**2))
        Vxf['P'] = p.reshape(Vxf['n']*d,Vxf['n']*d)
        Vxf['SOS'] = 1
        c  = np.zeros(( Vxf['n']*d, 1 ))
        ceq = []
    else:
        Vxf = shape_DS(p,d,L,options)
        if L > 0:
            c  = np.zeros(((L+1)*d+(L+1)*options['optimizePriors'],1))
            if options['upperBoundEigenValue']:
                ceq = np.zeros((L+1,1))
            else:
                ceq = []
        else:
            c  = np.zeros((d,1))
            ceq = (np.ravel(Vxf['P']).T).dot(np.ravel(Vxf['P'])) -2

    dc, dceq = [], []

    if L == -1:  # SOS
        c = -np.linalg.eigvals(Vxf['P'] + Vxf['P'].T - np.eye(Vxf['n']*d)*options['tol_mat_bias'])
    else:
        for k in range(L):
            lambder = sp.linalg.eigvals(Vxf['P'][:,:,k+1] + (Vxf['P'][:,:,k+1]).T).real/2.0
            lambder = np.expand_dims(lambder, axis=1)
            c[k*d:(k+1)*d] = -lambder + options['tol_mat_bias']
            if options['upperBoundEigenValue']:
                ceq[k+1] = 1.0 - np.sum(lambder.real) # + Vxf.P(:,:,k+1)'

        if L > 0 and options['optimizePriors']:
            c[(L+1)*d:(L+1)*d+L+1] = -Vxf['Priors']

    return c, ceq, dc, dceq_____no_output_____import numpy as np
ind = np.array([ True,  True , True , True , True , True  ,True  ,True ,False , True ,False,
 False  ,True,  True , True,  True  ,True , True,  True,  True])
print(ind.shape)
print(np.sum(ind))(20,)
17
import sys
sys.path.append('/Users/Lekan/catkin_ws/src/torobo/tampy')
sys.path.append('/Users/Lekan/catkin_ws/src/dp_planning')
from tampy.tampy import Tampy
from tampy.tampy import ORDER_SERVO_ON, ORDER_SERVO_OFF, ORDER_RUN_MODE, CTRL_MODE_CURRENT

class ToroboEnvironment(object):
    def __init__(self, home_pos):
        """
         in update(self, currents) currents are inindividual currents to each joint
         According to Ryo-san, the max current is 0.5A and you should start with 0.05A or sth and gradually increase
        """
        self.tampy = Tampy()
        self.home_pos = home_pos
        self.control_freq = 30.0
        self.latest_control = time.time()

    def set_position(self, positions):
        self.tampy.move_to(positions)

    def update(self, currents):
        self.tampy.send_currents(currents)
        time.sleep(max(
            self.latest_control + 1.0 / self.control_freq - time.time(),
            0
        ))
        self.latest_control = time.time()
        return self.get_state()

    def get_state(self):
        rx = self.tampy.get_latest_rx()
        positions = [j.position for j in rx.joints]
        velocities = [j.velocity for j in rx.joints]
        return positions, velocities

    def __enter__(self):
        self.set_position(self.home_pos)
        self.tampy.send(ORDER_RUN_MODE, value1=CTRL_MODE_CURRENT)
        self.tampy.send(ORDER_SERVO_ON)
        self.latest_control = time.time()
        return self

    def __exit__(self, type, value, tb):
        self.tampy.send_currents([0] * 7)
        # TODO: why do we need multiple calls to kill?
        for _ in range(3):
            self.tampy.send(ORDER_SERVO_OFF)
_____no_output_____def robot_exec(x0, xT, ds_stab, options):
    d = x0.shape[0]
    nbSPoint = x0.shape[1]

    if not xT:
        xT = np.zeros((d, 1))

    if d != xT.shape[0]:
        logger.critical('Error: length x0 should be length xT')
        x, xd, t = [], [], []
        return

    #################################################
    #                   starting values             #
    #################################################
    x  = np.zeros((d, 0, nbSPoint))
    xd = np.zeros((d, 0, nbSPoint))
    for i in range(nbSPoint):
        x[:, 0, i] = x0[:,i]
    xd = np.zeros(x.shape)

    if xT.shape == x0.shape:
        XT = xT
    else:
        XT = np.tile(xT, [1, nbSPoint])

    t = 0
    # robot run
    i = 0
#     while True:
#         xd[:,i,:] = np.tile(ds_stab(np.squeeze(x[:,i,:])-XT), [d, 1, nbSPoint])

#         ### integration ###
#         x[:,i+1,:] = x[:,i,:] + xd[:,i,:] * options['dt']
#         t[i+1] = t[i]+options['dt']

#         i += 1

#         # check convergence
#         if i > 3 and (np.all(np.all(np.all(abs(xd[:,-1-3:-1,:])))))
    while np.linalg.norm():

     _____no_output_____filepath = join(expanduser('~'), 'Documents', 'LyapunovLearner', 'ToroboTakahashi', 'data')
name = 'state_joint.npy'
filename = join(filepath, name)
print(filename)/home/olalekan/Documents/LyapunovLearner/ToroboTakahashi/data/state_joint.npy
os.listdir(filepath)_____no_output_____data = np.load(filename)
print(data.shape, data.dtype)(10001, 25) float64

np.save(filepath + '/state_joint_pos_only.npy', data[:, 1:8])
np.savetxt(filepath + '/state_joint_pos_only.csv', data[:,1:8], delimiter=" ", newline="\n")

pos_data = np.loadtxt(filepath + '/state_joint_pos_only.csv')
pos_data.shape

new_data = np.ravel(pos_data)
rec_data = new_data.reshape(10001, 7)
print(rec_data)_____no_output_____### prepro new csv data saved from c++_____no_output_____

filepath = join(expanduser('~'), 'Documents', 'LyapunovLearner', 'scripts', 'data')
name = 'cart_pos.csv'
filename = join(filepath, name)
print(os.path.isfile(filename))True
with open(filename, 'r+') as foo:
    #print(filename)
    data = foo.readlines()
_____no_output_____# gather data into right shape
proper_data = []

for i in range(len(data)):
    temp = data[i].rsplit(sep=',')
    temp[-1] = temp[-1].rsplit(sep='\n')[0]
    temp = [float(x) for x in temp]
    proper_data.append(temp)

proper_data = np.array(proper_data)
print(proper_data.shape)(10000, 6)
# gather 2d data
data2d = np.hstack([proper_data[:, :2], proper_data[:, 3:5]]).T
print(data2d.shape)(4, 10000)
proper_data.shape_____no_output_____