<commit_before>#!/usr/bin/env python

"""
Tools for assisting in reading, manipulating, and plotting data from plate readers for John's R01 due Feb 2016.

"""

#=============================================================================================
# Imports
#=============================================================================================

from lxml import etree
import pandas as pd
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import seaborn as sns
from glob import glob

sns.set(style='white')
sns.set_context('talk')
sns.despine()

#=============================================================================================
# Tecan Infinite plate reader helper functions
#=============================================================================================

#THESE ARE ALL THE FUNCTIONS I MADE AND USED WHILE MAKING FIGURES FOR THE GRANT

#This function allows us to import xml format data files and convert them to a pandas dataframe
def xml2df(file):

    root = etree.parse(file)

    data = []

    reads = root.xpath("/*/Section[1]/*/Well")

    wellIDs = [read.attrib['Pos'] for read in reads]

    data = [(s.text, float(s.attrib['WL']), r.attrib['Pos'])
        for r in reads
        for s in r]

    dataframe = pd.DataFrame(data, columns=['fluorescence','wavelength (nm)','Well'])
            
    ### dataframe_rep replaces 'OVER' (when fluorescence signal maxes out) with '3289277', an arbitrarily high number

    dataframe_rep = dataframe.replace({'OVER':'3289277'})

    dataframe_rep[['fluorescence']] = dataframe_rep[['fluorescence']].astype('float')
            
    dataframe_pivot = pd.pivot_table(dataframe_rep, index = 'wavelength (nm)', columns = ['Well'])
    
    #Rearrange columns so they're in the right order
    cols =  dataframe_pivot['fluorescence'].columns.tolist()
    cols = [cols[0]] + cols[4:12] + cols[1:4] + [cols[12]] + cols[16:24] + cols[13:16]
    dataframe_reindex =  dataframe_pivot.reindex_axis(cols,level='Well',axis=1)
    
    return dataframe_reindex

#This function allows us to plot spectra
def plot_spectra_grid(file_set,protein,ligands,ligand):
    grid = len(protein) + len(ligand)
    
    # pick the correct file
    proteins = file_set.keys()
    index = ligands.index(ligand)
    file = file_set[protein][index]
    
    # pick a title
    title = "%s - %s" %(protein, ligand)
    
    # make a dataframe
    df = xml2df(file)
    
    # plot the spectra
    fig = plt.figure();
    ax = df['fluorescence'].iloc[:,12].plot(ylim=(0,100000),legend=False, linewidth=4,color='m');
    ax.axvline(x=480,color='0.7',linestyle='--');
    for i in range(11):
        #s = df['fluorescence'].iloc[:,i].plot(ylim=(0,100000),linewidth=3,c=cm.hsv(i*15), ax = ax, title=title);
        df['fluorescence'].iloc[:,i].plot(ylim=(0,100000),linewidth=3,c=cm.hsv(i*15), ax = ax);
        df['fluorescence'].iloc[:,11+i].plot(ylim=(0,100000),legend=False, linewidth=4,c=cm.gray(i*15+50), ax = ax, fontsize =20);
    sns.despine()
    plt.xlim(320,600)
    plt.yticks([])
    plt.xlabel('wavelength (nm)', fontsize=20)
    plt.tight_layout();
    plt.savefig('%s.eps'%title, type='eps', dpi=1000)
    # The s = line above and this an attempt at making a color bar that so far has not worked
    #cbar = plt.colorbar(mappable=s, ax=ax)
    #cbar.set_label('My Label')

#This function allows us to get wells from sections, parsing an xml file
def get_wells_from_section(path):
    reads = path.xpath("*/Well")
    wellIDs = [read.attrib['Pos'] for read in reads]

    data = [(float(s.text), r.attrib['Pos'])
         for r in reads
         for s in r]

    datalist = {
      well : value
      for (value, well) in data
    }

    welllist = [
                [
                 datalist[chr(64 + row) + str(col)]          
                 if chr(64 + row) + str(col) in datalist else None
                 for row in range(1,9)
                ]
                for col in range(1,13)
                ]
                
    return welllist

# This function makes a dataframe from our file once it's been parsed. <commit_msg>adjusted plotting in grant.py for use in manuscript repo<commit_after>#!/usr/bin/env python

"""
Tools for assisting in reading, manipulating, and plotting data from plate readers for John's R01 due Feb 2016.

"""

#=============================================================================================
# Imports
#=============================================================================================

from lxml import etree
import pandas as pd
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import seaborn as sns
from glob import glob

sns.set(style='white')
sns.set_context('talk')
sns.despine()

#=============================================================================================
# Tecan Infinite plate reader helper functions
#=============================================================================================

#THESE ARE ALL THE FUNCTIONS I MADE AND USED WHILE MAKING FIGURES FOR THE GRANT

#This function allows us to import xml format data files and convert them to a pandas dataframe
def xml2df(file):

    root = etree.parse(file)

    data = []

    reads = root.xpath("/*/Section[1]/*/Well")

    wellIDs = [read.attrib['Pos'] for read in reads]

    data = [(s.text, float(s.attrib['WL']), r.attrib['Pos'])
        for r in reads
        for s in r]

    dataframe = pd.DataFrame(data, columns=['fluorescence','wavelength (nm)','Well'])
            
    ### dataframe_rep replaces 'OVER' (when fluorescence signal maxes out) with '3289277', an arbitrarily high number

    dataframe_rep = dataframe.replace({'OVER':'3289277'})

    dataframe_rep[['fluorescence']] = dataframe_rep[['fluorescence']].astype('float')
            
    dataframe_pivot = pd.pivot_table(dataframe_rep, index = 'wavelength (nm)', columns = ['Well'])
    
    #Rearrange columns so they're in the right order
    cols =  dataframe_pivot['fluorescence'].columns.tolist()
    cols = [cols[0]] + cols[4:12] + cols[1:4] + [cols[12]] + cols[16:24] + cols[13:16]
    dataframe_reindex =  dataframe_pivot.reindex_axis(cols,level='Well',axis=1)
    
    return dataframe_reindex

#This function allows us to plot spectra
def plot_spectra_grid(file_set,ligands,*args,**kwargs):
    #Example Syntax:
    #   plot_spectra_grid(file_set,ligands,protein=['Src'],ligand=['Bosutinib'])
    #   plot_spectra_grid(file_set,ligands)
    #   plot_spectra_grid(file_set,ligands,output='Figure1')
    protein = kwargs.get('protein', None)
    ligand = kwargs.get('ligand', None)
    output = kwargs.get('output', None)
    
    if protein != None:
        these_proteins = protein
    else:
        these_proteins = file_set.keys()
    if ligand != None:
        these_ligands = ligand
    else:
        these_ligands = ligands
    print these_proteins
    print len(these_proteins)
    print these_ligands
    print len(these_ligands)
    
    grid = len(these_proteins) + len(these_ligands)
    if grid ==2:
        my_figsize = (12,8)
    else:
        my_figsize = (24,18)
    
    fig, axes = plt.subplots(nrows=len(these_ligands), ncols=len(these_proteins), figsize= my_figsize, sharey=True, sharex=True)
    
    for j,protein in enumerate(these_proteins):
        for k,ligand in enumerate(these_ligands):
            index = ligands.index(ligand)
            file = file_set[protein][index]
            
            if grid == 2:
                my_axes = None
            else:
                my_axes = axes[k,j]
    
            # pick a title
            title = "%s - %s" %(protein, ligand)
    
            # make a dataframe
            df = xml2df(file)
      
            # plot the spectra
            #fig = plt.figure();
            for i in range(11):
                df['fluorescence'].iloc[:,i].plot(ylim=(0,100000),xlim=(400,600),linewidth=3, ax = my_axes, c=cm.hsv(i*15), title=title);
                df['fluorescence'].iloc[:,11+i].plot(ylim=(0,100000),xlim=(400,600),legend=False, ax = my_axes, linewidth=4,c=cm.gray(i*15+50),fontsize =20, title=title);

            sns.despine()
            plt.yticks([])
            plt.tight_layout();
            
    if output != None:
        plt.savefig('%s.png'%output, dpi=1000)

#This function allows us to get wells from sections, parsing an xml file
def get_wells_from_section(path):
    reads = path.xpath("*/Well")
    wellIDs = [read.attrib['Pos'] for read in reads]

    data = [(float(s.text), r.attrib['Pos'])
         for r in reads
         for s in r]

    datalist = {
      well : value
      for (value, well) in data
    }

    welllist = [
                [
                 datalist[chr(64 + row) + str(col)]          
                 if chr(64 + row) + str(col) in datalist else None
                 for row in range(1,9)
                ]
                for col in range(1,13)
                ]
                
    return welllist

# This function makes a dataframe from our file once it's been parsed. 
{'fraction_non_alphanumeric': 0.2013512016834644, 'fraction_numerical': 0.02403366928785026, 'mean_word_length': 3.0657361548851867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5173368', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2747, 'n_words': 705}