EdgeExpressDB

 view release on metacpan or  search on metacpan

www/edgeexpress/cgi/edgeXpression.cgi  view on Meta::CPAN

#!/usr/bin/python
import os,sys
import MySQLdb
import cgitb; cgitb.enable()
import cgi

os.environ[ 'HOME' ] = "/tmp"
IMG_DIR = "/data/www/html/4/edgeexpress/tmpimages/"
IMG_URL = "../tmpimages/"
import pylab
from matplotlib.font_manager import FontProperties

timecourses = {'CAGE':['0', '1', '4', '12', '24', '96'],
               'qRT-PCR':['0','1','2','4','6','12','24','48','72','96'],
               'Illumina':['0','1','2','4','6','12','24','48','72','96'],
               'miRNA':['0','1','2','4','6','12','24','48','72','96']}

ylabels = {'CAGE':'Tags Per Million',
           'qRT-PCR':'Copy Number',
           'Illumina':'Expression Ratio',
           'miRNA':'Normalized Intensity'}

linestyle = {'1':'-.',
             '3':'--',
             '6':'-'}
           
fonts = {'title':25,
	 'lw':3,
	 'label':25,
	 'legend':20,
	 'ticks':17,
	 'dpi':35,
         'image_size':''}

markersize = 10

colorcycle = ['b','g','r','c','m','y','k']
markercycle =['','^','o','s','d','>','<','x','h']



def fetchmiRNAData(lib1, lib3, lib6, mature):
	
	if mature:
		cursor.execute("select fl1.mat_mirna_id fid,mat_miRNA as primary_name,series_name,e.value "
		+"from experiment join expression e "
		+"using(experiment_id) join "
		+"(select mat.primary_name mat_miRNA,feature2_id mat_mirna_id,feature1_id mirna_probe_id "
		+"from feature probe join edge on(probe.feature_id = feature1_id) "
		+"join feature mat on(mat.feature_id = feature2_id) where edge_source_id=43 "
		+"and feature2_id in ("+idstring+"))fl1 where feature_id = mirna_probe_id "
		+"and series_name in ("+datasetstring+")")
	else:
		cursor.execute("select substring(fl3.mat_miRNA,length(fl3.mat_miRNA)-1) mat_miRNA,pre_mirna_id fid, pre_miRNA primary_name,series_name,value "
		+"from experiment join expression e "
		+"using(experiment_id) join (select fl1.*, fl2.feature1_id mirna_probe_id "
		+"from edge fl2 join "
		+"(select pre.primary_name pre_miRNA, mat.primary_name mat_miRNA, feature1_id pre_mirna_id, feature2_id mat_mirna_id "
		+"from feature pre join edge on(pre.feature_id = feature1_id) "
		+"join feature mat on(mat.feature_id = feature2_id) "
		+"where edge_source_id=20 and feature1_id in ("+idstring+"))fl1 "
		+"where fl1.mat_mirna_id = fl2.feature2_id)fl3 "
		+"where feature_id = mirna_probe_id "
		+"and series_name in ("+datasetstring+")")

	parseResultSet(lib1, lib3, lib6)

def fetchCAGEData(lib1, lib3, lib6):
    cursor.execute("select feature_id fid, primary_name, series_name, value "
    +"from experiment "
    +"join (select * from expression "
    +"join feature "
    +"using(feature_id) "
    +"where feature.feature_id in ("+idstring+"))e "
    +"using(experiment_id) "
    +"where platform='CAGE' and series_name in ("+datasetstring+") "
    +"and datatype_id=2 "
    +"order by feature_id, series_name, series_point;")

    parseResultSet(lib1, lib3, lib6)
    
def fetchQRTData(lib1, lib3, lib6):
	cursor.execute("SELECT enz.feature_id fid, enz.primary_name primary_name, "
	+"series_name, value, f2.primary_name primer_name, series_point "
	+"FROM feature enz "
	+"JOIN edge fl1 "
	+"on(fl1.feature2_id=enz.feature_id) JOIN feature f2 "
	+"on(fl1.feature1_id=f2.feature_id) JOIN expression fe "
	+"on(f2.feature_id = fe.feature_id) JOIN experiment using(experiment_id ) "
	+"WHERE fl1.edge_source_id=25 and enz.feature_id in ("+idstring+") "

www/edgeexpress/cgi/edgeXpression.cgi  view on Meta::CPAN


            if thistype=='CAGE' and promoterAlias.has_key(id) and fonts['image_size']=='':
                lib[id][1]=promoterAlias[id]
                
                           
            if fonts['image_size']=='L':
            	probeprimer = lib[id][0].split('_')
            	if thistype=='Illumina' or thistype=='qRT-PCR':
			label = probeprimer[1]+":R"+dataset
		else:
			label = lib[id][1]+":R"+dataset
            elif thistype=='miRNA':
            	if len(lib)>1:
            		label = lib[id][1][0:2]+":R"+dataset
            	else:
            		label = 'R'+dataset
            else:
            	label = lib[id][1]+":R"+dataset


            values = lib[id][2:]

            targetGenes.append(lib[id][:2]+[dataset]+values)
            

	    if thistype=='Illumina':
                fvalues=[float(i) for i in values]
                med=pylab.median(fvalues)
                values=["%1.4f"%(f/med) for f in fvalues]  

            if not imageExists:
                    if not lineColors.has_key(geneid):
                            lineColors[geneid]= colorcycle[colcycleindex]
                            colcycleindex = colcycleindex+1
                            colcycleindex%=len(colorcycle)
                        
                    color = lineColors[geneid] 
                    pylab.plot(values,
                               lw=fonts['lw'],
                               ls=linestyle[dataset],
                               ms=markersize,
                               marker=markercycle[markercycleindex],
                               c=color)

                    labels.append(label)
                    
        if thistype!='CAGE':
                markercycleindex=markercycleindex+1
                     
                 
    graphData.append({'type':thistype,
                      'imageName':imageName,
                      'timecourse':timecourse,
                      'values':targetGenes})

    if not imageExists:
            
            pylab.xlabel('Timecourse',fontsize=fonts['label'])
            pylab.ylabel(ylabels[thistype],fontsize=fonts['label'])
            if thistype=='CAGE':
            	pylab.legend(labels,'center left',axespad=1.03,labelsep=0.001,shadow=True, prop = FontProperties(size=fonts['legend']))
            else:
            	pylab.legend(labels,'best',shadow=True, prop = FontProperties(size=fonts['legend']))

            n = len(timecourse)
            pylab.yticks(fontsize=fonts['ticks'])
            pylab.xticks(pylab.arange(n), timecourse, fontsize=fonts['ticks'])
            
            if thistype=='miRNA':
            	pylab.title('Agilent miRNA',fontsize=fonts['title'])
            else:
                pylab.title(thistype, fontsize=fonts['title'] )
            
            pylab.savefig(IMG_DIR+imageName,dpi=fonts['dpi'])
        

def writeTableData():
    if isHTML:
        for graph in graphData:
            if len( graph['values'] )==0:
                continue
            print "<img src=\""+IMG_URL+graph['imageName']+"\">"
        print "<br><pre>"
        print """<br><br><br>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;<br>&lt;expressionGraphs&gt;"""
    else:
        print """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<expressionGraphs>"""

    for graph in graphData:
        
        if len( graph['values'] )==0:
            continue
        
        if isHTML:
            print "<br>&nbsp;&nbsp;&lt;graph url=\""+graph['imageName']+"\" type=\""+graph['type']+"\"&gt;"
        else:
            print "<graph url=\""+graph['imageName']+"\" type=\""+graph['type']+"\">"

        
        for gene in graph['values']:

            if isHTML:
                gstring="<br>&nbsp;&nbsp;&nbsp;&nbsp;&lt;"
            else:
                gstring="<"

            gstring += "gene id=\""
            
            if graph['type']!='CAGE':
                ids = gene[0].split('_')
                gstring += ids[0]+"\""
            else:
                gstring += gene[0]+"\""

            gstring+=" name=\""+gene[1]+"\""

            if graph['type']=='Illumina':
                gstring+=" probeId=\""+ids[1]+"\""
            elif graph['type']=='qRT-PCR':
                gstring+=" primerName=\""+ids[1]+"\""
             
                
            gstring += " dataset=\""+gene[2]+"\" data=\""

www/edgeexpress/cgi/edgeXpression.cgi  view on Meta::CPAN

promoterAlias = {}
idstring = ""
ids = []

if form.has_key('names'):

	names = form.getfirst("names","").split(",")
	for name in names:
		alias = name.split(":")
		id = name
		if len(alias)>1:
			id = alias[1]
			alias = alias[0]+":"
		else:
			alias = ""

		cursor.execute("SELECT feature_id from feature where primary_name='"
				+id+"' and feature_source_id in(31,48,49) ")
		rs = cursor.fetchallDict()
		for row in rs:
			ids.append(alias+str(row['feature_id']))

if form.has_key('ids'):
    ids.extend(form.getfirst("ids","").split(","))

for i in range(len(ids)):
	alias = ids[i].split(":")
	if len(idstring)>1:
	    idstring+=","
	if len(alias)>1:
	    promoterAlias[alias[1]]=alias[0]
	    idstring+=alias[1]
	    ids[i] = alias[1]
	else:
	    idstring+=alias[0]


if form.has_key('type'):
   type = form.getfirst("type", "").split(",");
else:
   type = ""

if form.has_key('dataset'):
        datasets = form.getfirst("dataset", "").split(",");
        datasets.sort()
else:
        datasets = ['1','3','6']

datasetstring=""
for ds in datasets:
    if len(datasetstring)>0:
        datasetstring=datasetstring+','
    datasetstring=datasetstring+'\'RIKEN'+ds+'\''

       
if form.has_key('size'):
    #Its either big or small in this version
    fonts['title']=16
    fonts['lw']=1
    fonts['label']=12
    fonts['legend']=9
    fonts['ticks']=10
    fonts['dpi']=100
    fonts['image_size']='L'
    markersize=5



graphData = [];
if len(idstring)>0:
	for thistype in type:  
	    lib1 ={}
	    lib3 ={}
	    lib6 ={}
	    if thistype.lower()=="cage":
		thistype = "CAGE"
		fetchCAGEData(lib1, lib3, lib6)

	    elif thistype.lower()=="qrt-pcr":
		thistype = "qRT-PCR"
		fetchQRTData(lib1, lib3, lib6)

	    elif thistype.lower()=="illumina":
		thistype = "Illumina"
		fetchIlluminaData(lib1, lib3, lib6)

	    elif thistype.lower()=="mirna":
		mature = 0
		if form.has_key('mature') and form.getfirst("mature") == 'true':
			mature = 1
		thistype = "miRNA"
		fetchmiRNAData(lib1, lib3, lib6, mature)
	    else:
		continue
	    plotGraph()

	writeTableData()

cursor.close()
conn.close()



( run in 2.020 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )