In [25]:
%load_ext autoreload
%autoreload 2
%load_ext sql 

import xlwt
import pandas as pd
import bio.hts.apredica as apr
from bio.hts.htsdb import *

HCI_DIR = '/share/server_data/project/HCI/HepRn-1/'
DAT_DIR = HCI_DIR+'data/'
INC_DIR = DAT_DIR+'incoming/'

mng.register_connection("hts-db","htsdb",username="ishah",
                        password="ishah",host='localhost')


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
The sql extension is already loaded. To reload it, use:
  %reload_ext sql

In [27]:
HtsPlate.objects.count()


Out[27]:
416

In [12]:
!ls $DAT_DIR


apr-heprn-sep-2013.csv	incoming

In [13]:
# FIles created in Apredica Oct 2013
R_HepRn = pd.read_csv(DAT_DIR+'apr-heprn-sep-2013.csv')

In [14]:
R_HepRn.head()


Out[14]:
sample_id sample_rep_id chemical_id chemical_casrn chemical_name assay_name assay_plate_id row_index col_index time conc value source_file sample_tech_rep_id ctrl cell feature lconc phase
0 Bupivacaine Bupivacaine NaN NaN NaN CLM_Hepat_Apoptosis_1hr 02S01T01P000501 4 23 1 800.0 76.42 cellumen_conc_response_v2_10Dec2009.txt Bupivacaine_02S01T01P000501_rep_1 0 HepRn Apoptosis 2.90309 1
1 Bupivacaine Bupivacaine NaN NaN NaN CLM_Hepat_Apoptosis_1hr 02S01T01P000501 5 23 1 400.0 57.72 cellumen_conc_response_v2_10Dec2009.txt Bupivacaine_02S01T01P000501_rep_1 0 HepRn Apoptosis 2.60206 1
2 Bupivacaine Bupivacaine NaN NaN NaN CLM_Hepat_Apoptosis_1hr 02S01T01P000501 6 23 1 200.0 53.52 cellumen_conc_response_v2_10Dec2009.txt Bupivacaine_02S01T01P000501_rep_1 0 HepRn Apoptosis 2.30103 1
3 Bupivacaine Bupivacaine NaN NaN NaN CLM_Hepat_Apoptosis_1hr 02S01T01P000501 7 23 1 100.0 59.90 cellumen_conc_response_v2_10Dec2009.txt Bupivacaine_02S01T01P000501_rep_1 0 HepRn Apoptosis 2.00000 1
4 Bupivacaine Bupivacaine NaN NaN NaN CLM_Hepat_Apoptosis_1hr 02S01T01P000501 8 23 1 50.0 60.83 cellumen_conc_response_v2_10Dec2009.txt Bupivacaine_02S01T01P000501_rep_1 0 HepRn Apoptosis 1.69897 1

In [17]:
print 'HepRn',R_HepRn.phase.unique()
print 'Times',R_HepRn.time.unique()
print 'Assays',R_HepRn.feature.unique()


HepRn [1]
Times [ 1 24 48]
Assays ['Apoptosis' 'CellLoss' 'DNADamage' 'DNATexture' 'LysosomalMass' 'MitoFxnI'
 'NuclearSize' 'Steatosis']

In [18]:
#A=True
#if A:
#    HtsChem.drop_collection()
#    HtsExp.drop_collection()
#    HtsAssay.drop_collection()
#    HtsWell.drop_collection()
#    HtsPlate.drop_collection()
#    HtsConcRespCurveNrm.drop_collection()
#    HtsAssayResult.drop_collection()
#    HtsAnalysisMethod.drop_collection()
#    HtsAnalysisStep.drop_collection()

In [11]:
Exp=apr.createAprExperiment(name='Apredica High Content Imaging in Rat primary hepatocytes (Phase I)',
                            org ='rat',cell='HepRn',tags=['HCS','PhI'],
                            eid='APR-HepRn-PhI')
apr.storeAprExperiment(R_HepRn,Exp,dbg=True)


I. Storing 120 plates
> Plate: 02S01T01P000501 (   0/ 120)
> Plate: 02S01T01P000507 (   1/ 120)
> Plate: 02S01T01P000513 (   2/ 120)
> Plate: 02S01T01P000519 (   3/ 120)
> Plate: 02S1T01P000407 (   4/ 120)
> Plate: 02S1T01P000413 (   5/ 120)
> Plate: 02S1T01P000419 (   6/ 120)
> Plate: 02S1T01P000425 (   7/ 120)
> Plate: 02S1T01P000431 (   8/ 120)
> Plate: 02S1T01P000437 (   9/ 120)
> Plate: 02S1T01P000477 (  10/ 120)
> Plate: 02S1T01P000495 (  11/ 120)
> Plate: 02S1T01P000559 (  12/ 120)
> Plate: 02S1T01P000565 (  13/ 120)
> Plate: 02S1T01P000571 (  14/ 120)
> Plate: 02S1T01P000755 (  15/ 120)
> Plate: 02S1T01P000761 (  16/ 120)
> Plate: 02S1T01P000767 (  17/ 120)
> Plate: 02S1T01P000773 (  18/ 120)
> Plate: 02S1T01P000779 (  19/ 120)
> Plate: 02S1T24P000409 (  20/ 120)
> Plate: 02S1T24P000415 (  21/ 120)
> Plate: 02S1T24P000421 (  22/ 120)
> Plate: 02S1T24P000427 (  23/ 120)
> Plate: 02S1T24P000433 (  24/ 120)
> Plate: 02S1T24P000439 (  25/ 120)
> Plate: 02S1T24P000479 (  26/ 120)
> Plate: 02S1T24P000497 (  27/ 120)
> Plate: 02S1T24P000561 (  28/ 120)
> Plate: 02S1T24P000567 (  29/ 120)
> Plate: 02S1T24P000573 (  30/ 120)
> Plate: 02S1T24P000757 (  31/ 120)
> Plate: 02S1T24P000763 (  32/ 120)
> Plate: 02S1T24P000769 (  33/ 120)
> Plate: 02S1T24P000775 (  34/ 120)
> Plate: 02S1T24P000781 (  35/ 120)
> Plate: 02S24T01P000503 (  36/ 120)
> Plate: 02S24T01P000509 (  37/ 120)
> Plate: 02S24T01P000515 (  38/ 120)
> Plate: 02S24T01P000521 (  39/ 120)
> Plate: 02S01T48P000505 (  40/ 120)
> Plate: 02S01T48P000511 (  41/ 120)
> Plate: 02S01T48P000517 (  42/ 120)
> Plate: 02S01T48P000523 (  43/ 120)
> Plate: 02S1T48P000411 (  44/ 120)
> Plate: 02S1T48P000417 (  45/ 120)
> Plate: 02S1T48P000423 (  46/ 120)
> Plate: 02S1T48P000429 (  47/ 120)
> Plate: 02S1T48P000435 (  48/ 120)
> Plate: 02S1T48P000441 (  49/ 120)
> Plate: 02S1T48P000481 (  50/ 120)
> Plate: 02S1T48P000499 (  51/ 120)
> Plate: 02S1T48P000563 (  52/ 120)
> Plate: 02S1T48P000569 (  53/ 120)
> Plate: 02S1T48P000575 (  54/ 120)
> Plate: 02S1T48P000759 (  55/ 120)
> Plate: 02S1T48P000765 (  56/ 120)
> Plate: 02S1T48P000771 (  57/ 120)
> Plate: 02S1T48P000777 (  58/ 120)
> Plate: 02S1T48P000783 (  59/ 120)
> Plate: 02S02T01P000502 (  60/ 120)
> Plate: 02S02T01P000508 (  61/ 120)
> Plate: 02S02T01P000514 (  62/ 120)
> Plate: 02S02T01P000520 (  63/ 120)
> Plate: 02S2T01P000408 (  64/ 120)
> Plate: 02S2T01P000414 (  65/ 120)
> Plate: 02S2T01P000420 (  66/ 120)
> Plate: 02S2T01P000426 (  67/ 120)
> Plate: 02S2T01P000432 (  68/ 120)
> Plate: 02S2T01P000438 (  69/ 120)
> Plate: 02S2T01P000478 (  70/ 120)
> Plate: 02S2T01P000496 (  71/ 120)
> Plate: 02S2T01P000560 (  72/ 120)
> Plate: 02S2T01P000566 (  73/ 120)
> Plate: 02S2T01P000572 (  74/ 120)
> Plate: 02S2T01P000756 (  75/ 120)
> Plate: 02S2T01P000762 (  76/ 120)
> Plate: 02S2T01P000768 (  77/ 120)
> Plate: 02S2T01P000774 (  78/ 120)
> Plate: 02S2T01P000780 (  79/ 120)
> Plate: 02S02T24P000504 (  80/ 120)
> Plate: 02S02T24P000510 (  81/ 120)
> Plate: 02S02T24P000516 (  82/ 120)
> Plate: 02S02T24P000522 (  83/ 120)
> Plate: 02S2T24P000410 (  84/ 120)
> Plate: 02S2T24P000416 (  85/ 120)
> Plate: 02S2T24P000422 (  86/ 120)
> Plate: 02S2T24P000428 (  87/ 120)
> Plate: 02S2T24P000434 (  88/ 120)
> Plate: 02S2T24P000440 (  89/ 120)
> Plate: 02S2T24P000480 (  90/ 120)
> Plate: 02S2T24P000498 (  91/ 120)
> Plate: 02S2T24P000562 (  92/ 120)
> Plate: 02S2T24P000568 (  93/ 120)
> Plate: 02S2T24P000574 (  94/ 120)
> Plate: 02S2T24P000758 (  95/ 120)
> Plate: 02S2T24P000764 (  96/ 120)
> Plate: 02S2T24P000770 (  97/ 120)
> Plate: 02S2T24P000776 (  98/ 120)
> Plate: 02S2T24P000782 (  99/ 120)
> Plate: 02S2T48P000412 ( 100/ 120)
> Plate: 02S2T48P000418 ( 101/ 120)
> Plate: 02S2T48P000424 ( 102/ 120)
> Plate: 02S2T48P000430 ( 103/ 120)
> Plate: 02S2T48P000436 ( 104/ 120)
> Plate: 02S2T48P000442 ( 105/ 120)
> Plate: 02S2T48P000482 ( 106/ 120)
> Plate: 02S2T48P000500 ( 107/ 120)
> Plate: 02S2T48P000564 ( 108/ 120)
> Plate: 02S2T48P000570 ( 109/ 120)
> Plate: 02S2T48P000576 ( 110/ 120)
> Plate: 02S2T48P000760 ( 111/ 120)
> Plate: 02S2T48P000766 ( 112/ 120)
> Plate: 02S2T48P000772 ( 113/ 120)
> Plate: 02S2T48P000778 ( 114/ 120)
> Plate: 02S2T48P000784 ( 115/ 120)
> Plate: 02S48T01P000506 ( 116/ 120)
> Plate: 02S48T01P000512 ( 117/ 120)
> Plate: 02S48T01P000518 ( 118/ 120)
> Plate: 02S48T01P000524 ( 119/ 120)

>Done.

In [13]:
# update chemicals
HtsChem.objects.update(set__ctrl=0)
CTRL = pd.read_csv(DAT_DIR+'apr-ctrl.csv')
for x in CTRL.to_records():
    print x['chemical_name']
    HtsChem.objects(name__iexact=x['chemical_name']).update(set__ctrl=x['ctrl'])

for HC in HtsChem.objects(chid__exists=0):
    C = Chemical.objects(Q(casrn=HC.casrn) | Q(name__iexact=HC.name)).first()
    if C:
        print 'found',HC.name
        HC.chid=C.id
        HC.save()
    else:
        print 'not found',HC.name


Anisomycin
Camptothecin
DMSO
Etoposide
CCCP
Demeclocyclin
Taxol
0
Bupivacaine
Chloroquine
Tunicamycin
Cyclosporin A

In [14]:
# update treatment duration of 
for P1 in HtsPlate.objects:
    TH = set([i.timeh for i in P1.wells])
    if len(TH)>1: 
        print 'Multiple times:',P1.eid
    P1.timeh=TH.pop()
    P1.save()

In [287]:
CHID = [i.id for i in Chemical.objects(tags='tx-pharma')]
HtsChem.objects(ctrl=0,chid__in=CHID).count()
CASRN=map(str,Chemical.objects(tags='tx-pharma').distinct('casrn'))
HtsChem.objects(ctrl=0,casrn__in=CASRN).count()
#CASRN[:10]


Out[287]:
111

In [29]:
# Create the analysis method
Steps = [HtsAnalysisStep(step=1,name='average-conc-reps',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Average assay response across replicate wells'),
         HtsAnalysisStep(step=2,name='-conc-response',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Smooth the assay response at each concentration using a moving window and weights (convolution)',
                         params=[HtsParam(name='window_len',value=7),HtsParam(name='smoother',value='np.hamming')]),
         HtsAnalysisStep(step=3,name='smooth-conc-response',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Smooth the assay response at each concentration using a moving window and weights (convolution)',
                         params=[HtsParam(name='window_len',value=7),HtsParam(name='smoother',value='np.hamming')]),
         HtsAnalysisStep(step=4,name='normalize-by-plate-median',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Normalize by median response of assay across non-control wells report as log2(FC)',
                         params=[HtsParam(name='normby',value='median')]),
         HtsAnalysisStep(step=5,name='calc-pct-resp',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Calculate the percentage change. Min=treated.quantile(0.02) and Max=posctrl.quantile(0.98)',
                         params=[HtsParam(name='pct_qb',desc='min_resp_quantile',value=0.02),HtsParam(name='pct_qt',desc='max_resp_quantile',value=0.98)]),
         HtsAnalysisStep(step=6,name='calc-zscore-resp',pkg='bio.hts.apredica',fun='analyzeHtsPlateConcResp',desc='Calculate the z-score using the median and std of assay response on plate')
         ]
S2 = []
for S in Steps: S2.append(S.save())
          
CR_meth=HtsAnalysisMethod(name='plate-window-smoothing-median-norm-pct',desc="Process Apredica plate: (a) Hamming smoother with window-length=7, (b) Median normalization, (c) Percentage change calculation ",
                          steps=S2)
CR_meth.save()


Out[29]:
<HtsAnalysisMethod: HtsAnalysisMethod object>

In [30]:
CR_meth=HtsAnalysisMethod.objects(name='plate-window-smoothing-median-norm-pct').first()

In [24]:
# Generate Conc Resp Points
HtsConcRespCurveNrm.drop_collection()
CR_meth=HtsAnalysisMethod.objects(name='plate-window-smoothing-median-norm-pct').first()
N = HtsPlate.objects.count()
for i,pid in enumerate(HtsPlate.objects.distinct('eid')):
    print '%10s %3d/%d' %(pid,i,N)
    apr.storeHtsPlateConcResp(pid,meth=CR_meth,window_len=7,smoother=np.hanning,dbg=False)


01S01T24P0082   0/416
01S01T24P0086   1/416
01S01T24P0090   2/416
01S01T24P0094   3/416
01S01T24P0098   4/416
01S01T24P0102   5/416
01S01T24P0106   6/416
01S01T24P0220   7/416
01S01T24P0224   8/416
01S01T24P0228   9/416
01S01T24P0232  10/416
01S01T24P0236  11/416
01S01T24P0240  12/416
01S01T24P0252  13/416
01S01T24P0281  14/416
01S01T24P0285  15/416
01S01T24P0289  16/416
01S01T24P0301  17/416
01S01T24P0305  18/416
01S01T24P0313  19/416
01S01T48P0221  20/416
01S01T48P0225  21/416
01S01T48P0229  22/416
01S01T72P0084  23/416
01S01T72P0088  24/416
01S01T72P0092  25/416
01S01T72P0096  26/416
01S01T72P0100  27/416
01S01T72P0104  28/416
01S01T72P0108  29/416
01S01T72P0233  30/416
01S01T72P0237  31/416
01S01T72P0241  32/416
01S01T72P0253  33/416
01S01T72P0282  34/416
01S01T72P0286  35/416
01S01T72P0287  36/416
01S01T72P0290  37/416
01S01T72P0291  38/416
01S01T72P0302  39/416
01S01T72P0306  40/416
01S01T72P0314  41/416
01S02T24P0083  42/416
01S02T24P0087  43/416
01S02T24P0091  44/416
01S02T24P0095  45/416
01S02T24P0099  46/416
01S02T24P0103  47/416
01S02T24P0107  48/416
01S02T24P0222  49/416
01S02T24P0226  50/416
01S02T24P0231  51/416
01S02T24P0234  52/416
01S02T24P0238  53/416
01S02T24P0242  54/416
01S02T24P0254  55/416
01S02T24P0283  56/416
01S02T24P0303  57/416
01S02T24P0307  58/416
01S02T24P0315  59/416
01S02T48P0223  60/416
01S02T48P0227  61/416
01S02T48P0230  62/416
01S02T72P0085  63/416
01S02T72P0089  64/416
01S02T72P0093  65/416
01S02T72P0097  66/416
01S02T72P0101  67/416
01S02T72P0105  68/416
01S02T72P0109  69/416
01S02T72P0235  70/416
01S02T72P0239  71/416
01S02T72P0243  72/416
01S02T72P0255  73/416
01S02T72P0284  74/416
01S02T72P0288  75/416
01S02T72P0292  76/416
01S02T72P0304  77/416
01S02T72P0308  78/416
01S02T72P0316  79/416
01S1T24P0248  80/416
01S1T24P0268  81/416
01S1T24P0293  82/416
01S1T24P0297  83/416
01S1T24P0309  84/416
01S1T24P0347  85/416
01S1T24P0351  86/416
01S1T24P0355  87/416
01S1T24P0407  88/416
01S1T24P0411  89/416
01S1T24P0415  90/416
01S1T24P0423  91/416
01S1T24P0427  92/416
01S1T24P0435  93/416
01S1T24P0439  94/416
01S1T24P0443  95/416
01S1T24P0451  96/416
01S1T24P0455  97/416
01S1T24P0459  98/416
01S1T24P0463  99/416
01S1T24P0467 100/416
01S1T24P0471 101/416
01S1T24P0475 102/416
01S1T24P0479 103/416
01S1T72P0249 104/416
01S1T72P0270 105/416
01S1T72P0294 106/416
01S1T72P0298 107/416
01S1T72P0310 108/416
01S1T72P0348 109/416
01S1T72P0352 110/416
01S1T72P0356 111/416
01S1T72P0408 112/416
01S1T72P0412 113/416
01S1T72P0416 114/416
01S1T72P0424 115/416
01S1T72P0428 116/416
01S1T72P0436 117/416
01S1T72P0440 118/416
01S1T72P0444 119/416
01S1T72P0452 120/416
01S1T72P0456 121/416
01S1T72P0460 122/416
01S1T72P0464 123/416
01S1T72P0468 124/416
01S1T72P0472 125/416
01S1T72P0476 126/416
01S1T72P0480 127/416
01S2T24P0250 128/416
01S2T24P0269 129/416
01S2T24P0295 130/416
01S2T24P0299 131/416
01S2T24P0311 132/416
01S2T24P0349 133/416
01S2T24P0353 134/416
01S2T24P0357 135/416
01S2T24P0409 136/416
01S2T24P0413 137/416
01S2T24P0417 138/416
01S2T24P0425 139/416
01S2T24P0429 140/416
01S2T24P0437 141/416
01S2T24P0441 142/416
01S2T24P0445 143/416
01S2T24P0453 144/416
01S2T24P0457 145/416
01S2T24P0461 146/416
01S2T24P0465 147/416
01S2T24P0469 148/416
01S2T24P0473 149/416
01S2T24P0477 150/416
01S2T24P0481 151/416
01S2T72P0251 152/416
01S2T72P0271 153/416
01S2T72P0296 154/416
01S2T72P0300 155/416
01S2T72P0312 156/416
01S2T72P0350 157/416
01S2T72P0354 158/416
01S2T72P0358 159/416
01S2T72P0410 160/416
01S2T72P0414 161/416
01S2T72P0418 162/416
01S2T72P0426 163/416
01S2T72P0430 164/416
01S2T72P0438 165/416
01S2T72P0442 166/416
01S2T72P0446 167/416
01S2T72P0454 168/416
01S2T72P0458 169/416
01S2T72P0462 170/416
01S2T72P0466 171/416
01S2T72P0470 172/416
01S2T72P0474 173/416
01S2T72P0478 174/416
01S2T72P0482 175/416
02S01T01P000501 176/416
02S01T01P000507 177/416
02S01T01P000513 178/416
02S01T01P000519 179/416
02S01T48P000505 180/416
02S01T48P000511 181/416
02S01T48P000517 182/416
02S01T48P000523 183/416
02S02T01P000502 184/416
02S02T01P000508 185/416
02S02T01P000514 186/416
02S02T01P000520 187/416
02S02T24P000504 188/416
02S02T24P000510 189/416
02S02T24P000516 190/416
02S02T24P000522 191/416
02S1T01P000407 192/416
02S1T01P000413 193/416
02S1T01P000419 194/416
02S1T01P000425 195/416
02S1T01P000431 196/416
02S1T01P000437 197/416
02S1T01P000477 198/416
02S1T01P000495 199/416
02S1T01P000559 200/416
02S1T01P000565 201/416
02S1T01P000571 202/416
02S1T01P000755 203/416
02S1T01P000761 204/416
02S1T01P000767 205/416
02S1T01P000773 206/416
02S1T01P000779 207/416
02S1T24P000409 208/416
02S1T24P000415 209/416
02S1T24P000421 210/416
02S1T24P000427 211/416
02S1T24P000433 212/416
02S1T24P000439 213/416
02S1T24P000479 214/416
02S1T24P000497 215/416
02S1T24P000561 216/416
02S1T24P000567 217/416
02S1T24P000573 218/416
02S1T24P000757 219/416
02S1T24P000763 220/416
02S1T24P000769 221/416
02S1T24P000775 222/416
02S1T24P000781 223/416
02S1T48P000411 224/416
02S1T48P000417 225/416
02S1T48P000423 226/416
02S1T48P000429 227/416
02S1T48P000435 228/416
02S1T48P000441 229/416
02S1T48P000481 230/416
02S1T48P000499 231/416
02S1T48P000563 232/416
02S1T48P000569 233/416
02S1T48P000575 234/416
02S1T48P000759 235/416
02S1T48P000765 236/416
02S1T48P000771 237/416
02S1T48P000777 238/416
02S1T48P000783 239/416
02S24T01P000503 240/416
02S24T01P000509 241/416
02S24T01P000515 242/416
02S24T01P000521 243/416
02S2T01P000408 244/416
02S2T01P000414 245/416
02S2T01P000420 246/416
02S2T01P000426 247/416
02S2T01P000432 248/416
02S2T01P000438 249/416
02S2T01P000478 250/416
02S2T01P000496 251/416
02S2T01P000560 252/416
02S2T01P000566 253/416
02S2T01P000572 254/416
02S2T01P000756 255/416
02S2T01P000762 256/416
02S2T01P000768 257/416
02S2T01P000774 258/416
02S2T01P000780 259/416
02S2T24P000410 260/416
02S2T24P000416 261/416
02S2T24P000422 262/416
02S2T24P000428 263/416
02S2T24P000434 264/416
02S2T24P000440 265/416
02S2T24P000480 266/416
02S2T24P000498 267/416
02S2T24P000562 268/416
02S2T24P000568 269/416
02S2T24P000574 270/416
02S2T24P000758 271/416
02S2T24P000764 272/416
02S2T24P000770 273/416
02S2T24P000776 274/416
02S2T24P000782 275/416
02S2T48P000412 276/416
02S2T48P000418 277/416
02S2T48P000424 278/416
02S2T48P000430 279/416
02S2T48P000436 280/416
02S2T48P000442 281/416
02S2T48P000482 282/416
02S2T48P000500 283/416
02S2T48P000564 284/416
02S2T48P000570 285/416
02S2T48P000576 286/416
02S2T48P000760 287/416
02S2T48P000766 288/416
02S2T48P000772 289/416
02S2T48P000778 290/416
02S2T48P000784 291/416
02S48T01P000506 292/416
02S48T01P000512 293/416
02S48T01P000518 294/416
02S48T01P000524 295/416
070917TP31CP1-24 296/416
070917TP31CP1-60 297/416
070917TP31CP1-72 298/416
070917TP31CP2-24 299/416
070917TP31CP2-60 300/416
070917TP31CP2-72 301/416
070917TP31DP1-24 302/416
070917TP31DP1-60 303/416
070917TP31DP1-72 304/416
070917TP31DP2-24 305/416
070917TP31DP2-60 306/416
070917TP31DP2-72 307/416
070917TP31EP1-24 308/416
070917TP31EP1-60 309/416
070917TP31EP1-72 310/416
070917TP31EP2-24 311/416
070917TP31EP2-60 312/416
070917TP31EP2-72 313/416
070917TP31FP1-24 314/416
070917TP31FP1-60 315/416
070917TP31FP1-72 316/416
070917TP31FP2-24 317/416
070917TP31FP2-60 318/416
070917TP31FP2-72 319/416
070922TP33GP1-24 320/416
070922TP33GP1-60 321/416
070922TP33GP1-72 322/416
070922TP33GP2-24 323/416
070922TP33GP2-60 324/416
070922TP33GP2-72 325/416
070922TP33HP1-24 326/416
070922TP33HP1-60 327/416
070922TP33HP1-72 328/416
070922TP33HP2-24 329/416
070922TP33HP2-60 330/416
070922TP33HP2-72 331/416
070922TP33IP1-24 332/416
070922TP33IP1-60 333/416
070922TP33IP1-72 334/416
070922TP33IP2-24 335/416
070922TP33IP2-60 336/416
070922TP33IP2-72 337/416
070922TP33JP1-24 338/416
070922TP33JP1-60 339/416
070922TP33JP1-72 340/416
070922TP33JP2-24 341/416
070922TP33JP2-60 342/416
070922TP33JP2-72 343/416
071001TP33KP1-24 344/416
071001TP33KP1-60 345/416
071001TP33KP1-72 346/416
071001TP33KP2-24 347/416
071001TP33KP2-60 348/416
071001TP33KP2-72 349/416
071001TP33LP1-24 350/416
071001TP33LP1-60 351/416
071001TP33LP1-72 352/416
071001TP33LP2-24 353/416
071001TP33LP2-60 354/416
071001TP33LP2-72 355/416
071001TP35MP1-24 356/416
071001TP35MP1-60 357/416
071001TP35MP1-72 358/416
071001TP35MP2-24 359/416
071001TP35MP2-60 360/416
071001TP35MP2-72 361/416
071001TP35NP1-24 362/416
071001TP35NP1-60 363/416
071001TP35NP1-72 364/416
071001TP35NP2-24 365/416
071001TP35NP2-60 366/416
071001TP35NP2-72 367/416
071007TP31AP1-24 368/416
071007TP31AP1-60 369/416
071007TP31AP1-72 370/416
071007TP31AP2-24 371/416
071007TP31AP2-60 372/416
071007TP31AP2-72 373/416
071007TP31BP1-24 374/416
071007TP31BP1-60 375/416
071007TP31BP1-72 376/416
071007TP31BP2-24 377/416
071007TP31BP2-60 378/416
071007TP31BP2-72 379/416
071008TP35OP1-24 380/416
071008TP35OP1-60 381/416
071008TP35OP1-72 382/416
071008TP35OP2-24 383/416
071008TP35OP2-60 384/416
071008TP35OP2-72 385/416
071008TP35PP1-24 386/416
071008TP35PP1-60 387/416
071008TP35PP1-72 388/416
071008TP35PP2-24 389/416
071008TP35PP2-60 390/416
071008TP35PP2-72 391/416
071008TP35QP1-24 392/416
071008TP35QP1-60 393/416
071008TP35QP1-72 394/416
071008TP35QP2-24 395/416
071008TP35QP2-60 396/416
071008TP35QP2-72 397/416
071008TP35RP1-24 398/416
071008TP35RP1-60 399/416
071008TP35RP1-72 400/416
071008TP35RP2-24 401/416
071008TP35RP2-60 402/416
071008TP35RP2-72 403/416
071015TP37SP1-24 404/416
071015TP37SP1-60 405/416
071015TP37SP1-72 406/416
071015TP37SP2-24 407/416
071015TP37SP2-60 408/416
071015TP37SP2-72 409/416
071015TP37TP1-24 410/416
071015TP37TP1-60 411/416
071015TP37TP1-72 412/416
071015TP37TP2-24 413/416
071015TP37TP2-60 414/416
071015TP37TP2-72 415/416

In [36]:
# Create the analysis method
# tol0=1e-4,lfc0=0.2,mit0=200,roots='fsolve',
Steps = [HtsAnalysisStep(step=1,name='interp-spline-conc-resp',pkg='bio.hts.apredica',fun='analyzeChemConcResp',
                         desc='Use a spline to interpolate response values: fold change (FC) and z-score (ZS)',
                         params=[HtsParam(name='spline_smooth',value=0)]),
         HtsAnalysisStep(step=2,name='calc-conc-prod-sig-effect',pkg='bio.hts.apredica',fun='analyzeChemConcResp',
                         desc='Calculate the LEC as the minimum concentration at which the z-score reaches Z0 and abs(log2(FC)) > lfc0.' + 
                              'Solve for the root(s) of the interpolated response i.e. ZS-Z0=0. In case of multiple roots pick lowest conc',
                         params=[HtsParam(name='Z0',value=1,desc='The z-score threshold for significance'),
                                 HtsParam(name='lfc0',value=0.2,desc='The log2(FC) threshold for significance (used in conjunction with Z0'),
                                 HtsParam(name='roots',value='fsolve',desc='Root finding function from scipy.optimize')]),
         HtsAnalysisStep(step=3,name='calc-potency',pkg='bio.hts.apredica',fun='analyzeChemConcResp',
                         desc='Calculate the maximum lfc (top)')
         ]
S2 = []
for S in Steps: S2.append(S.save())
          
CR_hit_meth=HtsAnalysisMethod(name='apr-calc-chem-assay-plate-lec-1',desc="Calculate the LEC from APR concentration response curves by deviation from background.",
                          steps=S2)
CR_hit_meth.save()


Out[36]:
<HtsAnalysisMethod: HtsAnalysisMethod object>

In [2]:
CR_hit_meth=HtsAnalysisMethod.objects(name__iexact='apr-calc-chem-assay-plate-lec-1').first()
#CR_hit_meth.delete()
CR_hit_meth


Out[2]:
<HtsAnalysisMethod: HtsAnalysisMethod object>

In [3]:
import bio.hts.apredica as apr
from bio.hts.htsdb import *

HtsAssayResult.drop_collection()
N = HtsConcRespCurveNrm.objects.count()
for k,Crc in enumerate(HtsConcRespCurveNrm.objects):
    if k and k % 300==0:
        print '%4d/%4d' % (k,N)
    #if HtsAssayResult.objects(crc=Crc): continue
    try:
        apr.storeChemConcResp(Crc,llec_max=6.0,lec_max=1e6,analysis_meth=CR_hit_meth,Z0=1,lfc0=0.2,dbg=False)
    except:
        print "Failed: ",Crc.assay.name,Crc.chem.name,Crc.plate.eid


 300/41656
 600/41656
 900/41656
1200/41656
1500/41656
1800/41656
2100/41656
2400/41656
2700/41656
3000/41656
3300/41656
3600/41656
3900/41656
4200/41656
4500/41656
4800/41656
5100/41656
5400/41656
5700/41656
6000/41656
6300/41656
6600/41656
6900/41656
7200/41656
7500/41656
7800/41656
8100/41656
8400/41656
8700/41656
9000/41656
9300/41656
9600/41656
9900/41656
10200/41656
10500/41656
10800/41656
11100/41656
11400/41656
11700/41656
12000/41656
12300/41656
12600/41656
12900/41656
13200/41656
13500/41656
13800/41656
14100/41656
14400/41656
14700/41656
15000/41656
15300/41656
15600/41656
15900/41656
16200/41656
16500/41656
16800/41656
17100/41656
17400/41656
17700/41656
18000/41656
18300/41656
18600/41656
18900/41656
19200/41656
19500/41656
19800/41656
20100/41656
20400/41656
20700/41656
21000/41656
21300/41656
21600/41656
21900/41656
22200/41656
22500/41656
22800/41656
23100/41656
23400/41656
23700/41656
24000/41656
24300/41656
24600/41656
24900/41656
25200/41656
25500/41656
25800/41656
26100/41656
26400/41656
26700/41656
27000/41656
27300/41656
27600/41656
27900/41656
28200/41656
28500/41656
28800/41656
29100/41656
29400/41656
29700/41656
30000/41656
30300/41656
30600/41656
30900/41656
31200/41656
31500/41656
31800/41656
32100/41656
32400/41656
32700/41656
33000/41656
33300/41656
33600/41656
33900/41656
34200/41656
34500/41656
34800/41656
35100/41656
35400/41656
35700/41656
36000/41656
36300/41656
36600/41656
36900/41656
37200/41656
37500/41656
37800/41656
38100/41656
38400/41656
38700/41656
39000/41656
39300/41656
39600/41656
39900/41656
40200/41656
40500/41656
40800/41656
41100/41656
41400/41656

In [222]:
Cas_cnt = dict( ((c,HtsChem.objects(casrn=c).count()) for c in HtsChem.objects.distinct('casrn') ))
REPS = [(HtsChem.objects(casrn=c).first().name,c,n,HtsChem.objects(casrn=c).distinct('eid')) for c,n in Cas_cnt.iteritems() if n>1]
[(HtsChem.objects(casrn=c).first().name,c,n) for c,n in Cas_cnt.iteritems() if n>1]


Out[222]:
[(u'3-Iodo-2-propynylbutylcarbamate', u'55406-53-6', 3),
 (u'Prohexadione-calcium', u'127277-53-6', 2),
 (u'Diquat dibromide', u'85-00-7', 2),
 (u'Metsulfuron-methyl', u'74223-64-6', 2),
 (u'Oxasulfuron', u'144651-06-9', 2),
 (u'Chlorsulfuron', u'64902-72-3', 4),
 (u'Tribenuron-methyl', u'101200-48-0', 2),
 (u'Metiram-zinc', u'9006-42-2', 2),
 (u'Triflusulfuron-methyl', u'126535-15-7', 2),
 (u'Mesosulfuron-methyl', u'208465-21-8', 2),
 (u'Bensulide', u'741-58-2', 5),
 (u'Triasulfuron', u'82097-50-5', 2),
 (u'Primisulfuron-methyl', u'86209-51-0', 2),
 (u'Foramsulfuron', u'173159-57-4', 2),
 (u'EPTC', u'759-94-4', 3),
 (u'Bisphenol A', u'80-05-7', 2),
 (u'Halosulfuron-methyl', u'100784-20-1', 2),
 (u'Ethametsulfuron methyl', u'97780-06-8', 2),
 (u'Rimsulfuron', u'122931-48-0', 2),
 (u'ANIS', u'nan', 7),
 (u'Dibutyl phthalate', u'84-74-2', 3),
 (u'Prosulfuron', u'94125-34-5', 6),
 (u'Fenoxaprop-ethyl', u'66441-23-4', 3),
 (u'Diclofop-methyl', u'51338-27-3', 5),
 (u'Bensulfuron-methyl', u'83055-99-6', 2)]

In [297]:
#PX = analyzeHtsPlateConcResp('071015TP37SP2-72',window_len=7,smoother=np.bartlett,dbg=True)
#set([len(i.resp) for i in HtsConcRespCurveNrm.objects])
    #print cr.lconc,cr.ppct,cr.slfc
#HtsConcRespCurveNrm.objects[20].plate.eid
#PX = analyzeHtsPlateConcResp(HtsConcRespCurveNrm.objects[20].plate.eid,window_len=7,smoother=np.bartlett,dbg=True)
E1 = HtsExp.objects[0]
CRC = HtsConcRespCurveNrm.objects(plate__in=HtsPlate.objects(exp=E1)).first()


Out[297]:
[<HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>,
 <HtsConcRespNrm: HtsConcRespNrm object>]

In [340]:
#DZS1(LC1[:10])
nc


Out[340]:
10

In [48]:
HtsAssayResult.objects(chem=HtsChem.objects(name__icontains='bisphenol').first()).count()
for AR in HtsAssayResult.objects(chem__in=HtsChem.objects(name__icontains='glitazone')):
    lec = next(i for i in AR.res if i.name=='lec')
    if lec.value>1e5: continue
    eff = next(i for i in AR.res if i.name=='efficacy')
    print AR.chem.name,AR.chem.eid,AR.crc.plate.exp.cell,AR.crc.plate.timeh,AR.assay.name,lec.value,lec.units,eff.value


Pioglitazone hydrochloride TX009773 HepG2 24.0 Apredica CellLoss 0.000190719648484 M -0.94340302749
Pioglitazone hydrochloride TX009773 HepG2 72.0 Apredica CellLoss 0.000130707165987 M -0.989978064268
Pioglitazone hydrochloride TX009773 HepG2 24.0 Apredica CellLoss 0.000197897748878 M -0.920740001791
Pioglitazone hydrochloride TX009773 HepG2 72.0 Apredica CellLoss 0.000122479812927 M -1.11320397729
Troglitazone TX006205 HepG2 72.0 Apredica OxidativeStress 0.000107308613472 M 1.36533914873
Troglitazone TX006205 HepG2 72.0 Apredica p53Act 0.000115138888034 M 1.82902730408
Troglitazone TX006205 HepG2 72.0 Apredica CellLoss 7.52424087812e-05 M -0.996423819228
Troglitazone TX006205 HepG2 24.0 Apredica MitoMembPot 0.000105420204787 M -1.549927212
Troglitazone TX006205 HepG2 72.0 Apredica CellLoss 6.4709021138e-05 M -1.03399549138
Troglitazone TX006205 HepG2 72.0 Apredica MitoticArrest 7.23633979862e-05 M 1.70929417548

In [28]:
#HtsConcRespCurveNrm.objects(assay__in=(HtsAssay.objects(name__icontains='CellLoss')),plate__in=(HtsPlate.objects(timeh=24))).count()
#set([i.chem.name for i in HtsAssayResult.objects((Q(res__name='llec') & Q(res__value__lt=-7)))])
#HtsAssayResult.objects(__raw__={'chem.name':{'$regex':'phthalate','$options':'i'}}).count()
HtsAssayResult.objects(__raw__={'res':{'$elemMatch':{'name':'llec','value':{'$lt':-7}}}},).count()


Out[28]:
565

In [68]:
D1,T1,C1 = getPlateData('071015TP37SP2-72')
D1.ctrl.unique()
T1.groupby(level=1).median()
FC1 = T1/T1.quantile(q=0.5)
FC1.hist()


Out[68]:
array([[Axes(0.125,0.672059;0.227941x0.227941),
        Axes(0.398529,0.672059;0.227941x0.227941),
        Axes(0.672059,0.672059;0.227941x0.227941)],
       [Axes(0.125,0.398529;0.227941x0.227941),
        Axes(0.398529,0.398529;0.227941x0.227941),
        Axes(0.672059,0.398529;0.227941x0.227941)],
       [Axes(0.125,0.125;0.227941x0.227941),
        Axes(0.398529,0.125;0.227941x0.227941),
        Axes(0.672059,0.125;0.227941x0.227941)]], dtype=object)

In [22]:
P1 = HtsPlate.objects(eid='01S2T24P0299').first()
W1 = P1.wells[100].chem
W1._data
HtsChem.objects(ctrl=None).count()


Out[22]:
1031

In [18]:
# Create ToxCast APR assay 'parent' for the feature irrespective of direction / time.
import copy
for A in Assay.objects(tech__iexact='apr'):
    A1 = copy.deepcopy(A)
    A1.id=None
    A1.eid = re.sub('_\d+.*','',A1.eid)
    A1.name= re.sub('\s+\d+.*','',A1.name)
    if not Assay.objects(name=A1.name,eid=A1.eid):
        print ' Saving:',A1.eid,A1.name
        A1.save()


 Saving: APR_MitoMembPot APR MitoMembPot
 Saving: APR_NuclearSize APR NuclearSize
 Saving: APR_StressKinase APR StressKinase
 Saving: APR_MicrotubuleCSK APR MicrotubuleCSK
 Saving: APR_p53Act APR p53Act
 Saving: APR_OxidativeStress APR OxidativeStress

In [20]:
for A in Assay.objects(tech__iexact='apr'):
    print A.eid


APR_CellCycleArrest_24hr_dn
APR_MitoticArrest_72hr_up
APR_MitoticArrest_24hr_up
APR_MitoMass_24hr_dn
APR_CellLoss_1hr_up
APR_MitoticArrest_24hr_dn
APR_MitoMass_72hr_up
APR_MitoMass_24hr_up
APR_CellLoss_24hr_dn
APR_CellLoss_1hr_dn
APR_MitoMembPot_24hr_dn
APR_MitoMass_72hr_dn
APR_NuclearSize_24hr_dn
APR_CellLoss_24hr_up
APR_CellCycleArrest_72hr_up
APR_CellLoss_72hr_up
APR_CellCycleArrest_24hr_up
APR_MitoticArrest_1hr_dn
APR_CellCycleArrest_1hr_up
APR_MitoMembPot_1hr_dn
APR_NuclearSize_1hr_up
APR_CellCycleArrest_1hr_dn
APR_NuclearSize_24hr_up
APR_NuclearSize_72hr_up
APR_MitoMembPot_24hr_up
APR_MitoMembPot_1hr_up
APR_CellCycleArrest_72hr_dn
APR_CellLoss_72hr_dn
APR_MitoMembPot_72hr_up
APR_NuclearSize_1hr_dn
APR_MitoticArrest_1hr_up
APR_MitoMass_1hr_up
APR_NuclearSize_72hr_dn
APR_MitoticArrest_72hr_dn
APR_MitoMass_1hr_dn
APR_MitoMembPot_72hr_dn
APR_CellCycleArrest
APR_MitoticArrest
APR_MitoMass
APR_CellLoss
APR_MitoMembPot
APR_NuclearSize
APR_StressKinase
APR_MicrotubuleCSK
APR_p53Act
APR_OxidativeStress
APR_StressKinase_1hr_dn
APR_StressKinase_24hr_dn
APR_MicrotubuleCSK_72hr_up
APR_MicrotubuleCSK_1hr_dn
APR_StressKinase_72hr_up
APR_p53Act_1hr_dn
APR_p53Act_24hr_up
APR_MicrotubuleCSK_1hr_up
APR_p53Act_72hr_up
APR_p53Act_1hr_up
APR_StressKinase_72hr_dn
APR_OxidativeStress_1hr_up
APR_StressKinase_24hr_up
APR_OxidativeStress_24hr_up
APR_MicrotubuleCSK_72hr_dn
APR_OxidativeStress_24hr_dn
APR_p53Act_24hr_dn
APR_StressKinase_1hr_up
APR_OxidativeStress_72hr_dn
APR_p53Act_72hr_dn
APR_MicrotubuleCSK_24hr_up
APR_MicrotubuleCSK_24hr_dn
APR_OxidativeStress_1hr_dn
APR_OxidativeStress_72hr_up