<jupyter_start><jupyter_text>dataframe of glycan imagesLazily create a dataframe containing the images for all the glycans in array v 5.0.This may be usefulfor analysis later.<jupyter_code>%reset -f
## import all required dependencies
# standard imports
import urllib2
import os
import sys
import json
import StringIO
import pickle

# dataframe and numerical
import pandas as pd
import numpy as np

# plotting
import matplotlib.pyplot as plt
%matplotlib inline
# plotting params
from matplotlib import rcParams
rcParams["figure.figsize"] = 10, 8

#scipy
from scipy import stats
from scipy.special import erf
from scipy import sqrt

# glypy
import glypy
from glypy.plot import plot
from glypy.io import glycoct

# other modules
sys.path.append('../scripts/')
import post_glycan_convert as pgc

# Use an example array - using the galectin example

samples_in="../data/galectin-3/galectin-3_5.0_human.json"
results_dir = "../results/galectin-3/"
dataframe_out=results_dir+"dataframes_galectin.pkl"
dataframefile=dataframe_out
# Check whether or not the dataframes exist

subdir="./"
dataframefile=dataframe_out

if not os.path.isfile(dataframefile):
    print "calling the notebook that loads the data"
    %run download_cfg_for_galectin.ipynb
with open(os.path.join(subdir, dataframefile)) as f:
    dataframes = pickle.load(f)
    
    
dataframes[0]["sample"]
frame=dataframes[0]["dataframe"]
frame.head()
# create a data frame with the glycan and the spacer as separate columns

Structure="Structure on Masterlist"

df=pd.concat([frame["Chart Number"], frame[Structure]], axis=1)
              
df.head()
#frame["Structure"]
df[Structure].str.extract('(.*-)')
df["Glycan"]=df[Structure].str.extract('(.*-)').str.strip('-')

#df['Structure'].str.extract('(-Sp.+?$)')
df["Spacer"]=df[Structure].str.split('.*-').str[1]
df
# create a function that plots using glypy


def plotter(func):
    """
    A decorator that plots the function .
    (it actually just prints it, but it could be logging!)
    """
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        #print func.__name__, args, kwargs
        #plot(res,label=True)
        return res
    return wrapper

@plotter
def get_gly_iupac(iupacstring):
    try:
        kchandle = StringIO.StringIO(iupacstring)
        kcf=pgc.mechanise_glycan_convert(kchandle, "Kcf", "text")
        kchandle2 = StringIO.StringIO(kcf)
        gct=pgc.mechanise_glycan_convert(kchandle2, "Glycoct", "text")
        return glycoct.loads(gct)
    except:
        return iupacstring
high_binders_from_paper=[372,
 543,
 545,
 547,
 549,
 550,
 551,
 565,
 566,
 569,
 576,
 577,
 578,
 579,
 580,
 581,
 582,
 583,
 584,
 585,
 586,
 587,
 588,
 589]
highbinders_df = df[df["Chart Number"].isin(high_binders_from_paper)]

#highbinders_df["glycoct"]=get_gly_iupac(highbinders_df["Glycan"])
highbinders_df["glycoct"]=highbinders_df["Glycan"]
highbinders_df.head()
highbinders_df
highbinders_df["glycoct"]=highbinders_df["Glycan"]
# create a function called to http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html
# maps apply to the showl frame.... 
def map_glycoct(x):
    # that, if x is a string,
    return get_gly_iupac(x)
   
#. commenting out for now as takes a while. plus not all conversions occur
#newslice = highbinders_df['glycoct'].map(map_glycoct) # http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html

# Not always able to get iupac-> kcf-> glycoct
# why not just pull imaged from kcf-> image
import post_kcf_to_image as pki
def get_gly_image(iupacstring):
    try:
        kchandle = StringIO.StringIO(iupacstring)
        kcf=pgc.mechanise_glycan_convert(kchandle, "Kcf", "text")
        kchandle2 = StringIO.StringIO(kcf)
        gct=pki.post_rings_kcf_to_image(kchandle2)
        return pki.get_first_image_from_html(gct).encode("base64")
    except:
        return iupacstring

aimg= get_gly_image(df["Glycan"][372])
from IPython.display import Image
Image(aimg) 
highbinders_from_analysis={543,
 547,
 549,
 550,
 551,
 565,
 566,
 569,
 577,
 578,
 579,
 580,
 581,
 582,
 588,
 589}

import urllib
highbinders2_df = df[df["Chart Number"].isin(highbinders_from_analysis)]
highbinders2_df["Image"]=highbinders2_df["Glycan"]
# create a function called to http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html
# maps apply to the showl frame.... 
def map_image(x):
    # that, if x is a string,
    return '<html><img src="data:image/png;base64,'+urllib.quote(get_gly_image(x).rstrip('\n'))+'"/></html>'
    #return '<html><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAACWCAYAAABNcIgQAAAHM0lEQVR4nO3d32vXhR7H8ZdaSwoxCSLCYEOaw12YtIIIphe7S/+C8iIIIQI3+6GjIJ0gbWL12SC6MAhd3Xhp367sotKKKLd1EWOSFARRQiCMaivmztWR0+Gc06mjfj2+H4/r7fN5fQbjyeez7bMVy8vLywGAola2ewAAtJMQAlCaEAJQmhACUJoQAlCaEA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EA<KEY>EAJQmhACUJoQAlCaEAJQmhBCG83NzWVubq7dM6A0IYQ2OHPmTPr6+nL//ffnvvvuS19fXz766KN2z4KShBCuoQsXLmTXrl3ZunVrzp49m+7u7vT09OTs2bPp7+/Prl27cuHChXbPhFKEEK6BX375JQcOHEhXV1eOHj2azs7OnDx5MlNTU5mens7JkyfT<KEY>>'


newslice = highbinders2_df['Image'].map(map_image) # http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html
pd.set_option('display.max_colwidth', 10000)
from IPython.display import Image, HTML
newslice
HTML(newslice.to_frame().to_html(escape=False))
def get_gly_kcf(iupacstring):
    try:
        kchandle = StringIO.StringIO(iupacstring)
        kcf=pgc.mechanise_glycan_convert(kchandle, "Kcf", "text")
        return kcf
    except:
        return iupacstring
    
highbinders2_df["KCF"]=highbinders2_df["Glycan"]
# create a function called to http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html
# maps apply to the showl frame.... 
def map_kcf(x):
    # that, if x is a string,
    return get_gly_kcf(x)
    


newslice2 = highbinders2_df['KCF'].map(map_kcf) # http://chrisalbon.com/python/pandas_apply_operations_to_dataframes.html
kcfdict=newslice2.to_dict()
kcfdict.keys()
kcflist=[]
for key in kcfdict:
    kcflist.append(kcfdict[key])

print "".join(kcflist) # can paste this into http://rings.t.soka.ac.jp/cgi-bin/tools/MCAW/mcaw_index.pl<jupyter_output>ENTRY         Glycan
NODE  16
     1  GlcNAc   -64   -6
     2  Gal   -56   -6
     3  GlcNAc   -48   -6
     4  Gal   -40   -6
     5  GlcNAc   -32   -6
     6  Man   -24   -6
     7  GlcNAc   -64   -2
     8  Gal   -56   -2
     9  GlcNAc   -48   -2
     10  Gal   -40   -2
     11  GlcNAc   -32   -2
     12  Man   -24   -2
     13  Man   -16   -4
     14  GlcNAc   -8   -4
     15  Fuc   -8   4
     16  GlcNAc   0   0
EDGE  15
     1  1:b1  2:3
     2  2:b1  3:4
     3  3:b1  4:3
     4  4:b1  5:4
     5  5:b1  6:2
     6  6:a1  13:6
     7  7:b1  8:3
     8  8:b1  9:4
     9  9:b1  10:3
     10  10:b1  11:4
     11  11:b1  12:2
     12  12:a1  13:3
     13  13:b1  14:4
     14  14:b1  16:4
     15  15:a1  16:6
///

ENTRY         Glycan
NODE  18
     1  Gal   -72   -6
     2  GlcNAc   -64   -6
     3  Gal   -56   -6
     4  GlcNAc   -48   -6
     5  Gal   -40   -6
     6  GlcNAc   -32   -6
     7  Man   -24   -6
     8  Gal   -72   -2
     9  GlcNAc   -64   -2
     10  Gal   -56   -2
[...]
{'fraction_non_alphanumeric': 0.1143054595715273, 'fraction_numerical': 0.0608154803040774, 'mean_word_length': 3.4365419987737584, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '26507', 'license': 'MIT', 'n_tokens_mistral': 3216, 'n_tokens_neox': 2957, 'n_words': 616, 'path': 'notebooks/glycan_images_v5.0.ipynb', 'repo_name': 'chrisbarnettster/cfg_array_analysis_notebooks', 'size': '9239'}