Last active
January 29, 2019 00:25
-
-
Save conchoecia/73dbd82ff98b098b3b402e6c2eb16e02 to your computer and use it in GitHub Desktop.
Plots_chromatograms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
fraction lum NaCl_Conc type | |
1 4213 0.06 step | |
2 5123 0.06 step | |
3 7813 0.06 step | |
4 12988 0.06 step | |
5 24373 0.06 step | |
6 24843 0.1 slope | |
7 7513 0.1 slope | |
8 5358 0.2 step | |
9 16858 0.2 step | |
10 18888 0.3 step | |
11 102928 0.3 step | |
12 119864 0.4 step | |
13 771153 0.4 step | |
Takes data like the table above and makes a nice chromatogram. | |
Mostly for plotting bioluminescence chromatograms for anion exchange chromatography. | |
""" | |
import matplotlib | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
import matplotlib.patches as mplpatches | |
import matplotlib.lines as mlines | |
filename = "fraction_results.tsv" | |
this_y_label = "RLU" | |
this_fig_title = "Ostracod luciferase purification \n strong anion exchange Jan 23 2019" | |
frac_rxn_vol = 100 | |
buff_rxn_vol = 70 | |
buff_rxn_mol = .75 | |
bar_width=0.95 | |
df = pd.read_csv(filename, | |
sep='\t', header=0) | |
#mols rxn | |
mol_rxn = frac_rxn_vol * df['NaCl_Conc'] | |
mol_buff = buff_rxn_vol * buff_rxn_mol | |
tot_mol = mol_rxn + mol_buff | |
#total_reaction_volume | |
trv = buff_rxn_vol + frac_rxn_vol | |
df['rxn_mol'] = tot_mol/trv | |
#set the figure dimensions | |
figWidth = 5.5 | |
figHeight = 4.5 | |
magic_num = 0.5 | |
plt.figure(figsize=(figWidth,figHeight)) | |
#set the panel dimensions | |
panelWidth = figWidth-magic_num | |
panelHeight = figHeight-magic_num | |
#find the margins to center the panel in figure | |
leftMargin = magic_num | |
bottomMargin = magic_num | |
x_lims = [0,max(df.fraction) + 1] | |
y_lims = [0,(max(df.lum) + 1)*1.05] | |
#set the left panel for 5' splice sites | |
panel0=plt.axes([leftMargin/figWidth, #left | |
bottomMargin/figHeight, #bottom | |
panelWidth/figWidth, #width | |
panelHeight/figHeight]) #height | |
panel0.tick_params(axis='both',which='both',\ | |
bottom=True, labelbottom=True,\ | |
left=True, labelleft=True, \ | |
right=False, labelright=False,\ | |
top=False, labeltop=False) | |
panel0.set_ylabel(this_y_label) | |
panel0.set_title(this_fig_title) | |
panel0.set_xlim(x_lims) | |
panel0.set_ylim(y_lims) | |
panel0.set_xlabel("Fraction") | |
panel1 = panel0.twinx() | |
prev_slope = False | |
for index, row in df.iterrows(): | |
rectangle1=mplpatches.Rectangle((row['fraction'],0),bar_width,row['lum'], | |
linewidth=0,\ | |
linestyle='-',\ | |
edgecolor='black',\ | |
facecolor='teal') | |
panel0.add_patch(rectangle1) | |
if index < (len(df) -1): | |
if df.loc[index, 'type'] == "step": | |
prev_slope = False | |
#first horizontal line | |
panel1.plot( [df.loc[index, 'fraction'], df.loc[index+1, 'fraction']], | |
[df.loc[index, 'NaCl_Conc'], df.loc[index, 'NaCl_Conc']], | |
color='r', linestyle='-', linewidth=2) | |
#second vertical line | |
panel1.plot( [df.loc[index+1, 'fraction'], df.loc[index+1, 'fraction']], | |
[df.loc[index, 'NaCl_Conc'], df.loc[index+1, 'NaCl_Conc']], | |
color='r', linestyle='-', linewidth=2) | |
elif (df.loc[index, 'type'] == "slope") and not prev_slope: | |
prev_slope = True | |
for j in range (index+1, (len(df))): | |
if (df.loc[j, 'type'] == "step") or (j == (len(df) -1) ): | |
panel1.plot( [df.loc[index, 'fraction'], df.loc[j, 'fraction']], | |
[df.loc[index, 'NaCl_Conc'], df.loc[j, 'NaCl_Conc']], | |
color='r', linestyle='-', linewidth=2) | |
break | |
panel1.scatter(df['fraction'] + (bar_width/2), df['rxn_mol'], color='#bc0f12', marker = 'x') | |
panel1.set_ylabel('NaCl Molarity', color='r') | |
panel1.tick_params('y', colors='r') | |
red_line = mlines.Line2D([], [], color='r', marker=None, | |
markersize=15, label='Gradient NaCl Molarity') | |
red_x = mlines.Line2D([], [], color='#bc0f12', marker='x', linewidth = 0, | |
markersize=8, label='Assay NaCl Molarity') | |
blue_patch = mplpatches.Patch(color='#23807f', label='RLU(20s)') | |
plt.legend(handles=[red_line, red_x, blue_patch]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment