Exemple #1
0
def get_sites_as_csv(motif_id):
    geneId = "entrez"
    sites = mirv_db.read_sites(motif_id)

    # a nasty hack to add alternative IDs to sites
    job_uuid = mirv_db.get_job_id_from_motif_id(motif_id)
    if (job_uuid):
        params = mirv_db.read_parameters(job_uuid)
        if (params.has_key('geneId')):
            geneId = params['geneId']
        gene_mapping = mirv_db.get_gene_mapping(job_uuid)
        if (gene_mapping):
            for site in sites:
                site['symbol'] = gene_mapping[site['gene']]['symbol']
                site['name'] = gene_mapping[site['gene']]['name']

    return sites_to_csv(sites, geneId)
def results(req):
    # need weederPSSMs1
    # need topRet
    # need mV
    # miRNADict[j.strip()] ??
    # functions: conv2rna, reverseComplement
    id = str(req.form.getfirst('id',''))
    motifs = read_motifs(id)
    parameters = read_parameters(id)
    if (len(parameters) == 0):
        util.redirect(req, req.construct_url("/error/%s/" % (id)))
        return
    topRet = parameters['topRet']
    genesSubmitted = parameters['genes_submitted']
    annotatedSequences = parameters['annotated_sequences']
    mirbase_species = parameters['species']
    geneId = parameters.get('geneId', 'entrez')
    qualityThreshold = float(parameters['quality'])
    if 'viral' in parameters.keys() and parameters['viral']=='True':
        viral = True
    else:
        viral = False
    gene_mapping = get_gene_mapping(id)

    # read mirbase miRNAs so we can link back to mirbase
    viralSpecies = []
    if viral==True:
        inFile = open(conf.viral_species_filename,'r')
        for line in inFile.readlines():
            splitUp = line.strip().split('\t')
            if splitUp[1]=='VRL':
                viralSpecies.append(splitUp[0])
        inFile.close()
    import gzip
    miRNAFile = gzip.open(conf.mirna_filename,'r')
    miRNADict = {}
    while 1:
        miRNALine = miRNAFile.readline()
        seqLine = miRNAFile.readline()
        if not miRNALine:
            break
        # Get the miRNA name
        miRNAData = miRNALine.lstrip('>').split(' ')
        curMiRNA = miRNAData[0]
        if (curMiRNA.split('-'))[0]==mirbase_species:
            miRNADict[curMiRNA] = miRNAData[1]
        if viral==True and ((curMiRNA.split('-'))[0] in viralSpecies):
            miRNADict[curMiRNA] = miRNAData[1]
    miRNAFile.close()

    s = '<html>\n'
    s += '<head>'
    s += '<title>miRvestigator Framework</title>\n'
    s += '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>\n'
    s += '<script src="/mirvestigator.js"></script>\n'
    s += '<script type="text/javascript">\n  var _gaq = _gaq || [];\n  _gaq.push([\'_setAccount\', \'UA-19292534-1\']);\n  _gaq.push([\'_trackPageview\']);\n  (function() {\n    var ga = document.createElement(\'script\');\n    ga.type = \'text/javascript\';\n    ga.async = true;\n    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';\n    var s = document.getElementsByTagName(\'script\')[0];\n    s.parentNode.insertBefore(ga, s);\n  })();\n'
    s += 'function toggleVisible(id, link_elem) {\n'
    s += '  if (document.getElementById) {\n'
    s += '    obj = document.getElementById(id);\n'
    s += '    if (obj) {\n'
    s += '      if (obj.style.display == \'none\') {\n'
    s += '        obj.style.display = \'\'\n'
    s += '        if (link_elem) {\n'
    s += '          link_elem.innerHTML = \'[-]\';\n'
    s += '        }\n'
    s += '      } else {\n'
    s += '        obj.style.display = \'none\'\n'
    s += '        if (link_elem) {\n'
    s += '          link_elem.innerHTML = \'[+]\';\n'
    s += '        }\n'
    s += '      }\n'
    s += '    }\n'
    s += '  }\n'
    s += '}\n'

    s += 'function toggleVisibleSubParams() {\n'
    s += '  subParams = $("#subParams");\n'
    s += '  if ( subParams.css("display") == "none" ) {\n'
    s += '    subParams.css("display", "");\n'
    s += '    $("#subParams_toggle").text("[-]");\n'
    s += '  } else {\n'
    s += '    subParams.css("display", "none");\n'
    s += '    $("#subParams_toggle").text("[+]");\n'
    s += '  }\n'
    s += '}\n'
    s += '  window.onload=function() {\n'
    s += '    var url = window.location.pathname;\n'
    s += '    var m = url.match(/\/results\/([0-9a-fA-F\-]+)\/(.?)/);\n'
    s += '    if (m && m.length > 1) {\n'
    s += '      job_id = m[1];\n'
    s += '        if (job_id) {\n'
    s += '          getParameters(job_id);\n'
    s += '        }\n'
    s += '    }\n'
    s += '  }\n'
    s += '</script>\n<style>\n  body { font-family: arial, sans-serif; }\n</style>\n</head>\n'
    s += '<body bgcolor=\'#333333\' link=\'cc0000\' vlink=\'cc0000\'>\n'
    s += '<center>\n'
    s += '<table><tr><td>\n'
    s += '<table cellpadding=\'15%\' cellspacing=\'5\' bgcolor=\'#999966\'>\n'
    s += '<tr>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/\'>H</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/\'>ome</a></b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#999966\' width=\'80\'><b>Results</b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/help\'>H</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/help\'>elp</a></b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/tutorial\'>T</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/tutorial\'>utorial</a></b>\n'
    s += '</td>\n'
    s += '<td align="center" valign="center" bgcolor="#333333" width="0"><b>'
    s += '<a style="color: rgb(255,0,0); text-decoration:none" href="/citation">C</a><a style="color: rgb(204,204,0); text-decoration:none" href="/citation">itation</a></b></td>\n'
    s += '</tr>\n'
    s += '</table>\n'
    s += '<table bgcolor=\'#999966\' cellpadding=\'10%\'>\n'
    s += '<tr>\n'
    s += '<td>'
    s += '<table width=\'100%\' cellpadding=\'15%\'>\n'
    s += '<tr>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\'><font size=6><b><font color=\'#ff0000\'>miR</font><font color=\'#cccc00\'>vestigator Framework Results</font></b></font>\n'
    s += '</td>\n'
    s += '</tr>\n'
    s += '</table>\n'
    s +='<p><table width=\'100%\' cellpadding=\'10%\'>\n'
    s += '<tr><td bgcolor=\'#000000\'><center><font color=\'#cccc00\' size=4><b><a href="#subParams" onclick="javascript: toggleVisibleSubParams(); return false;" style=\'color: rgb(204,204,0); text-decoration: none\'>Submission Parameters <font id="subParams_toggle" color=\'#ff0000\'>[+]</font></a></b></font></center></td></tr>\n'
    s += '<tr id="subParams" style="display: none;"><td bgcolor=\'#cccccc\'>\n'
    s += '<table width=\'100%\' cellpadding=\'5%\'>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>Basic Parameters</font></b></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Job Name:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="job_name"></span></center></td></tr></table></td></tr>\n'
    s+= '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Job ID:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="job_id"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Genes Submitted:</b></font> <span style="color:#ff0000"><b>(<a href="/genes/' + id + '/">gene mapping as CSV</a>)</b></span>'
    s += '</td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="genes_submitted"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Genes Annotated with Sequences:</b></font></td><td bgcolor=\'#66666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="annotated_sequences"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td colspan=2></td></tr>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>Weeder Parameters</font></b></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Motif Sizes:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="motif_sizes"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Background Model:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="background_model"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td colspan=2></td></tr>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>miRvestigator HMM Parameters</font></b></td></tr>\n'
    s+= '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Seed Models:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="seed_model"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Model Wobble Base-Pairing:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="model_wobble"></span></center></td></tr></table></td></tr>\n'
    s += '</table>\n'
    s += '</td></tr>\n'
    s += '<tr><td></td></tr>\n'
    s += '<tr><td bgcolor=\'#000000\'><center><font color=\'#cccc00\' size=4><b>Summary of Results</b></font></center></td></tr>\n'
    # Make a table that looks like this:
    # motif consensus | top miRNA | seed | length | vitPValue | # seqs with site (% of seqs with site)
    if (len(motifs)==0):
        s+= '<div style="text-align:center;background:#333333;color:#cc0000;font-weight:bold;padding-top:6ex;padding-bottom:6ex;">No motifs found.</div>'

    s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Motif</font></b></center></td><td bgcolor=\'#333333\'><\
center><b><font color=\'#ffffff\'>Top miRNA</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementary Base-Pairing</font></b></center></td><td bgcolor=\'#3333\
33\'><center><b><font color=\'#ffffff\'>Complementarity</br>P-Value</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>% of Input</br>Sequences with Site</font>\
</b></center></td></tr>\n'

    for motif in motifs:
        scoreList = read_mirvestigator_scores(motif['motif_id'])
        if topRet=='all':
            topRet = len(scoreList)
        else:
            topRet = int(topRet)

        top_score_i = 1
        top_score_viterbi_p = float(scoreList[0]['vitPValue'])
        row_count = 1
        while float(scoreList[top_score_i]['vitPValue']) == top_score_viterbi_p:
            row_count +=1
            top_score_i += 1

        top_score_i = 0
        i = scoreList[top_score_i]
        align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
        if not os.path.exists(conf.pssm_images_dir+'/'+id+'_'+motif['name']+'.png'):
            plotPssmMatrix(motif['matrix'],conf.pssm_images_dir+'/'+id+'_'+motif['name']+'.png')

        s += '<tr>'
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center><a href=\'#'+motif['name']+'_miRNAs\'><img src=\'/images\
/pssms/'+id+'_'+motif['name']+'.png\' alt=\''+conv2rna(str(motif['name']))+'\'></center></td>'
        s += '<td bgcolor=\'#ffffff\'><center>'
        miRNAs = []
        for miRNA_name in i['miRNA.name'].split('_'):
            miRNA_name = miRNA_name.strip()
            if miRNADict.has_key(miRNA_name):
                miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
            else:
                miRNAs.append(miRNA_name)
        s += '</br>'.join(miRNAs)
        s += '</center></td>\n'

        s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>\n'
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center>'+str('%.1e' % float(i['vitPValue']))+'</center></td>\n'
        # Get number or sequences with a hit                                                                                                                                         
        genesWithSite = []
        for site in motif['sites']:
            if float(site['match']) >= qualityThreshold and not site['gene'] in genesWithSite:
                genesWithSite.append(site['gene'])
        if (annotatedSequences==0):
            percGenes = "?error?"
        else:
            percGenes = (float(len(genesWithSite))/float(annotatedSequences))*float(100)
            if percGenes==float(100):
                percGenes = str(100)
            else:
                percGenes = str('%.2g' % percGenes)
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center><a href=\'#'+motif['name']+'_sites\'>'+percGenes+'%</a></center></td>\n'
        s += '</tr>\n'
        top_score_i += 1

        while float(scoreList[top_score_i]['vitPValue']) == top_score_viterbi_p:
            i = scoreList[top_score_i]
            align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
            s += '<tr>'
            s += '<td bgcolor=\'#ffffff\'><center>'
            miRNAs = []
            for miRNA_name in i['miRNA.name'].split('_'):
                miRNA_name = miRNA_name.strip()
                if miRNADict.has_key(miRNA_name):
                    miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
                else:
                    miRNAs.append(miRNA_name)
            s += '</br>'.join(miRNAs)
            s += '</center></td>\n'
                    
            s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>\n'
            s += '</tr>\n'
            top_score_i += 1

    s += '</table></p>\n'


    #s += '</td></tr>\n'
    #s += '</table>\n'
    for motif in motifs:
        s += '<p  id=\''+motif['name']+'_miRNAs\'><table width=\'100%\'cellpadding=\'15%\'>\n<tr>\n<td align=\'center\' valign=\'center\' bgcolor=\'#000000\'>\n<font size=4><b><font color=\'#cccc00\'>'
        if not topRet=='all':
            s += 'Top <font color=\'#ff0000\' size=4>'+str(topRet)+'</font>'
        elif topRet=='all':
            s += '<font color=\'#ff0000\' size=4>All</font>'
        s += ' miRNAs Complementary to the Weeder Motif</font> <font color=\'#ff0000\'>'+conv2rna(motif['name'])+'</font> &nbsp;'
        s += '<a href="#results" onclick="toggleVisible(\'results\', this); return false;" style=\'color: #ff0000; text-decoration: none\'>[+]</a></b></font></td></tr>\n'
        s += '<tr id="results" style="display: none;" width=600>\n'
        s += '<td bgcolor="#333333">\n'
        s += '<font color="#ffffff"><b>What do the columns mean?</b> <p><ul><li><b>miRNA Name</b> = The name of the name(s) for the unique seed sequence. There may be more than one miRNA annotated for a unique seed sequence because they vary in the 3\' terminus of the mature miRNA. Each miRNA is a link to it\'s entry on <a href="http://www.mirbase.org" style="color: rgb(204,204,0)" target="_blank">miRBase</a></li>&nbsp;</br> <li><b>miRNA Seed</b> = The sequence for seed that is the most complementary to the over-represented motif. The seed will be as long as the seed model described in the next column.</li>&nbsp;</br> <li><b>Seed Model</b> = Base-pairing models for the seed regions of a miRNA to the 3\' UTR of target transcripts. The 8mer, 7mer-m8, and 7mer-a1 models are the canonical models of miRNA to mRNA base-pairing. The 6mer models are considered marginal models as they typically have a reduced efficacy and are more likely to occur by chance alone. By default all of the seed models are used. The seed models are described in this figure:</br>&nbsp;</br><center><img src="/seedModels.gif" width=400></center></li>&nbsp;</br><li><b>Length of Complementarity</b> = The length of complementarity (or wobble if enabled) base-pairing between the sequence motif and the miRNA seed sequence.</li>&nbsp;</br> <li><b>Complementary Base-Pairing</b> = The complementarity of the over-represented sequence motif on top 5\'&rArr;3\' to the miRNA seed sequence given the seed model 3\'&rArr;5\'. (<b>Note:</b> <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#ff0000">|</font></b>&nbsp;</span> = a complementary, <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#0000ff">:</font></b>&nbsp;</span> = a wobble, <span style=\'background-color: rgb(255,255,255);\'><font color="#000000">"</font> <font color="#000000">"</font></span> (space) = not complementary, and for the sequences <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#cccccc">-</font></b>&nbsp;</span> = a gapping at the start or end.)</li>&nbsp;</br> <li><b>Complementarity P-Value</b> = Significance of complementarity between the over-represented sequence motif and the miRNA seed sequence. (<b>Note:</b> A perfectly complementary 8mer seed model has a Complementarity p-value of 1.5e-05, for a 7mer seed model 6.1e-05, and for a 6mer seed model 2.4e-04.)</li></ul> <b>What is considered good complementarity?</b> <p>This will depend upon your data and what downstream analysis you plan to do with it. But a good rule of thumb is that if you find perfect complementarity for a 7mer or 8mer (Complementary P-Value = <font color="#ff0000"><b>6.1e-05</b></font> and <font color="#ff0000"><b>1.5e-05</b></font>; respectively) this is likely to be of interest. Follow up with experimental studies will help to determine the false discovery rate for your dataset.</p></font>\n'
        s += '</td>\n'
        s += '</tr>\n'
        s += '</table>\n'
        scoreList = read_mirvestigator_scores(motif['motif_id'])
        if topRet=='all':
            topRet = len(scoreList)
        else:
            topRet = int(topRet)
        s += '<table width="100%%" cellpadding="5%%"><tr><td style="text-align:center; font-size:10pt; background:#cccccc;"><a href="/scores/csv/%s">Download table as CSV</a></td></tr></table>\n' % (motif['motif_id'],)
        s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>miRNA Name</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>miRNA Seed</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Seed Model</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Length of</br>Complementarity</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementary Base-Pairing</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementarity</br>P-Value</font></b></center></td></tr>'
        for k in range(topRet):
            i = scoreList[k]
            align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
            s += '<tr>'
            s += '<td bgcolor=\'#ffffff\'><center>'
            
            # generate links to mirbase miRNAs
            miRNAs = []
            for miRNA_name in i['miRNA.name'].split('_'):
                miRNA_name = miRNA_name.strip()
                if miRNADict.has_key(miRNA_name):
                    miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
                else:
                    miRNAs.append(miRNA_name)
            
            s += '</br>'.join(miRNAs)
            s += '</center></td>'

            s += '<td bgcolor=\'#ffffff\'><center>'+conv2rna(reverseComplement(str(i['miRNA.seed'])))+'</center></td>'
            s += '<td bgcolor=\'#ffffff\'><center>'+str(i['model'])+'</center></td><td bgcolor=\'#ffffff\'><center>'+str(align1[3])+'</center></td>'
            s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>'
            s += '<td bgcolor=\'#ffffff\'><center>'+str('%.1e' % float(i['vitPValue']))+'</center></td>'
            s += '</tr>'
        s += '</table></p>'
        #'gene':seqDict[splitUp[0]], 'strand':splitUp[1], 'site':splitUp[2], 'start':splitUp[3], 'match':splitUp[4].lstrip('(').rstrip(')')
        # pssm1.nsites
        s += '<p id=\''+motif['name']+'_sites\'><table width=\'100%\' bgcolor=\'#000000\' cellpadding=\'15%\'><tr><td align=\'center\' valign=\'center\'><font size=4><b><font color=\'#cccc00\'>Position of Putative miRNA Binding Sites in Submitted Genes</br>for the Weeder Motif</font> <font color=\'#ff0000\'>'+conv2rna(motif['name'])+'</font> &nbsp;'
        s += '<a href="#sites" onclick="toggleVisible(\'sites\', this); return false;" style=\'color: #ff0000; text-decoration: none\'>[+]</a></b></font></td></tr>\n'
        s += '<tr id="sites" style="display: none;" width=600><td bgcolor="#333333"><font color="#ffffff">\n'
        s += '<b>Where do these sites come from?</b>\n<p>As part of the miRvestigator framework <a href="http://159.149.109.9/modtools/" style="color: rgb(204,204,0)" target="_blank">Weeder</a> provides predicted miRNA binging sites in the 3\' untranslated regions (UTRs) of the analyzed genes. Predicted binding sites were split into three different similarity bins:  <font color="#ff0000">High quality</font> - &#8805; 95% similarity to the miRNA seed sequence (red), <font color="#cccc00">Medium quality</font> 95% &#8805; similarity &#8805; 90% to the miRNA seed sequence (yellow), and <font color="#00ff00">Fair quality</font> 90% &#8805; similarity &#8805; 85% to the miRNA seed sequence (green). These sites can be used to develop follow-up experiments such as luciferase reporter assays to validate the efficacy of these sites.</p>\n<b>What do the columns mean?</b>\n<p><ul><li><b>Gene</b> = gene identifier from the input set that links to the mapped Entrez identifier page on NCBI.</li></br>&nbsp;</br>\n<li><b>Gene Symbol</b> = Official gene symbol if available.</li></br>&nbsp;</br>\n<li><b>Site</b> = The sequence for site identified by Weeder. If it is in square brackets indicates that the site is of lower similarity.</li></br>&nbsp;</br>\n<li><b>Start Relative to Stop Codon</b> = The 3\' UTR begins following the stop codon (which is set at 0 base-pairs (bp)). Thus the values in this column describe the start of the site in bp after the stop codon.</li></br>&nbsp;</br>\n<li><b>% Similarity to Consensus Motif</b> = The similarity of the predicted site to the consensus motif is computed as a percentage. Predicted binding sites were split into three different similarity to consensus bins:  <font color="#ff0000">High quality</font> - &#8805; 95% similarity to the miRNA seed sequence (red), <font color="#cccc00">Medium quality</font> 95% &#8805; similarity &#8805; 90% to the miRNA seed sequence (yellow), and <font color="#00ff00">Fair quality</font> 90% &#8805; similarity &#8805; 85% to the miRNA seed sequence (green).</li></br>&nbsp;</br>\n<li><b>Minimum Free Energy (MFE) of mRNA-miRNA Duplex</b> = the minimum free energy (MFE) of duplexing for the reverse complement of the motif consensus sequence and putative target site sequences using the ViennaRNA package.</li></ul></p>'
        s += '</font></td></td></table>'
        s += '<table width="100%%" cellpadding="5%%"><tr><td style="text-align:center; font-size:10pt; background:#cccccc;"><a href="/sites/csv/%s">Download table as CSV</a></td></tr></table>\n' % (motif['motif_id'],)
        s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Gene</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Gene symbol</font><\
/b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Sequence of Site</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Start Relative to</br>Stop Codon (bp)</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>% Similarity to Consensus Motif</br>(Quality = </font><font color=\'#cc0000\'>High</font><font color=\'#ffffff\'> | </font><font color=\'#cccc00\'>Medium</font><font color=\'#ffffff\'> | </font><font color=\'#00cc00\'>Fair</font><font color=\'#ffffff\'>)</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Minimum Free Energy (MFE)</br>of mRNA-miRNA Duplex</font></b></center></td></tr>'
        for i in motif['sites']:
            if float(i['match']) >= qualityThreshold:
                col1 = '#000000'
                if float(i['match']) >= float(95):
                    col1 = '#cc0000'
                elif float(i['match']) >= float(90):
                    col1 = '#cccc00'
                elif float(i['match']) >= float(85):
                    col1 = '#00cc00'
                gene_name_map = gene_mapping.get(i['gene'], None)
                if (gene_name_map==None or geneId=='symbol' or geneId=='entrez'):
                    gene_name = i['gene']
                else:
                    gene_name = gene_name_map['name']
                if (gene_name_map==None or gene_name_map['symbol'] == None):
                    gene_symbol = ''
                else:
                    gene_symbol = ' ' + gene_name_map['symbol']
                s += '<tr><td bgcolor=\'#ffffff\'><center>'+str('<a href=\'http://www.ncbi.nlm.nih.gov/gene/'+str(i['gene'])+'\' target=\'_blank\'>'+gene_name+'</a>')+'<td bgcolor=\'#ffffff\'><center>'+gene_symbol+'</center></td>'+'</center></td><td bgcolor=\'#ffffff\'><center>'+conv2rna(str(i['site']))+'</center></td><td bgcolor=\'#ffffff\'><center>'+str(i['start'])+'</center></td><td bgcolor=\'#ffffff\'><font color=\''+str(col1)+'\'><center><b>'+i['match']+'</b></center></font></td><td bgcolor=\'#ffffff\'><center>'+str(i['mfe'])+'</center></td></tr>'
        s += '</table></p>'
    s += '<p><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#c0c0c0\'><center>Need help? Please contact <font color=\'#0000ff\'>cplaisier(at)systemsbiology.org</font> or <font color=\'#0000ff\'>cbare(at)systemsbiology.org</font> if you have any questions, comments or concerns.<br>Developed at the <a href=\'http://www.systemsbiology.org\' target=\'_blank\' style=\'color: rgb(0,0,255)\'>Institute for Systems Biology</a> in the <a href=\'http://baliga.systemsbiology.net/\' target=\'_blank\' style=\'color: rgb(0,0,255)\'>Baliga Lab</a>.</center></td></tr></table></p>'
    s += '</center></td></tr></table></td></tr></table></center></body></html>'
    return s
def parameters(req):
    id = str(req.form.getfirst('id',''))
    req.content_type='application/json'
    return json.dumps(read_parameters(id))
def results(req):
    # need weederPSSMs1
    # need topRet
    # need mV
    # miRNADict[j.strip()] ??
    # functions: conv2rna, reverseComplement
    config = __get_config(req)
    id = str(req.form.getfirst('id',''))
    motifs = read_motifs(id)
    parameters = read_parameters(id)
    if (len(parameters) == 0):
        util.redirect(req, req.construct_url("/error/%s/" % (id)))
        return
    topRet = parameters['topRet']
    genesSubmitted = parameters['genes_submitted']
    annotatedSequences = parameters['annotated_sequences']
    mirbase_species = parameters['species']
    geneId = parameters.get('geneId', 'entrez')
    qualityThreshold = float(parameters['quality'])
    if 'viral' in parameters.keys() and parameters['viral']=='True':
        viral = True
    else:
        viral = False
    gene_mapping = get_gene_mapping(id)

    # read mirbase miRNAs so we can link back to mirbase
    viralSpecies = []
    if viral:
        viral_species_filename = config.get('General', 'viral_species_filename')
        with open(viral_species_filename,'r') as inFile:
            for line in inFile.readlines():
                splitUp = line.strip().split('\t')
                if splitUp[1]=='VRL':
                    viralSpecies.append(splitUp[0])

    import gzip
    mirna_filename = config.get('General', 'mirna_filename')
    with gzip.open(mirna_filename,'r') as miRNAFile:
        miRNADict = {}
        while 1:
            miRNALine = miRNAFile.readline()
            seqLine = miRNAFile.readline()
            if not miRNALine:
                break
            # Get the miRNA name
            miRNAData = miRNALine.lstrip('>').split(' ')
            curMiRNA = miRNAData[0]
            if (curMiRNA.split('-'))[0]==mirbase_species:
                miRNADict[curMiRNA] = miRNAData[1]
            if viral==True and ((curMiRNA.split('-'))[0] in viralSpecies):
                miRNADict[curMiRNA] = miRNAData[1]

    s = '<html>\n'
    s += '<head>'
    s += '<title>miRvestigator Framework</title>\n'
    s += '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>\n'
    s += '<script src="/mirvestigator.js"></script>\n'
    s += '<script type="text/javascript">\n  var _gaq = _gaq || [];\n  _gaq.push([\'_setAccount\', \'UA-19292534-1\']);\n  _gaq.push([\'_trackPageview\']);\n  (function() {\n    var ga = document.createElement(\'script\');\n    ga.type = \'text/javascript\';\n    ga.async = true;\n    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';\n    var s = document.getElementsByTagName(\'script\')[0];\n    s.parentNode.insertBefore(ga, s);\n  })();\n'
    s += 'function toggleVisible(id, link_elem) {\n'
    s += '  if (document.getElementById) {\n'
    s += '    obj = document.getElementById(id);\n'
    s += '    if (obj) {\n'
    s += '      if (obj.style.display == \'none\') {\n'
    s += '        obj.style.display = \'\'\n'
    s += '        if (link_elem) {\n'
    s += '          link_elem.innerHTML = \'[-]\';\n'
    s += '        }\n'
    s += '      } else {\n'
    s += '        obj.style.display = \'none\'\n'
    s += '        if (link_elem) {\n'
    s += '          link_elem.innerHTML = \'[+]\';\n'
    s += '        }\n'
    s += '      }\n'
    s += '    }\n'
    s += '  }\n'
    s += '}\n'

    s += 'function toggleVisibleSubParams() {\n'
    s += '  subParams = $("#subParams");\n'
    s += '  if ( subParams.css("display") == "none" ) {\n'
    s += '    subParams.css("display", "");\n'
    s += '    $("#subParams_toggle").text("[-]");\n'
    s += '  } else {\n'
    s += '    subParams.css("display", "none");\n'
    s += '    $("#subParams_toggle").text("[+]");\n'
    s += '  }\n'
    s += '}\n'
    s += '  window.onload=function() {\n'
    s += '    var url = window.location.pathname;\n'
    s += '    var m = url.match(/\/results\/([0-9a-fA-F\-]+)\/(.?)/);\n'
    s += '    if (m && m.length > 1) {\n'
    s += '      job_id = m[1];\n'
    s += '        if (job_id) {\n'
    s += '          getParameters(job_id);\n'
    s += '        }\n'
    s += '    }\n'
    s += '  }\n'
    s += '</script>\n<style>\n  body { font-family: arial, sans-serif; }\n</style>\n</head>\n'
    s += '<body bgcolor=\'#333333\' link=\'cc0000\' vlink=\'cc0000\'>\n'
    s += '<center>\n'
    s += '<table><tr><td>\n'
    s += '<table cellpadding=\'15%\' cellspacing=\'5\' bgcolor=\'#999966\'>\n'
    s += '<tr>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/\'>H</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/\'>ome</a></b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#999966\' width=\'80\'><b>Results</b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/help\'>H</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/help\'>elp</a></b>\n'
    s += '</td>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\' width=\'80\'><b><a style=\'color: rgb(255,0,0); text-decoration:none\' href=\'/tutorial\'>T</a><a style=\'color: rgb(204,204,0); text-decoration:none\' href=\'/tutorial\'>utorial</a></b>\n'
    s += '</td>\n'
    s += '<td align="center" valign="center" bgcolor="#333333" width="0"><b>'
    s += '<a style="color: rgb(255,0,0); text-decoration:none" href="/citation">C</a><a style="color: rgb(204,204,0); text-decoration:none" href="/citation">itation</a></b></td>\n'
    s += '</tr>\n'
    s += '</table>\n'
    s += '<table bgcolor=\'#999966\' cellpadding=\'10%\'>\n'
    s += '<tr>\n'
    s += '<td>'
    s += '<table width=\'100%\' cellpadding=\'15%\'>\n'
    s += '<tr>\n'
    s += '<td align=\'center\' valign=\'center\' bgcolor=\'#333333\'><font size=6><b><font color=\'#ff0000\'>miR</font><font color=\'#cccc00\'>vestigator Framework Results</font></b></font>\n'
    s += '</td>\n'
    s += '</tr>\n'
    s += '</table>\n'
    s +='<p><table width=\'100%\' cellpadding=\'10%\'>\n'
    s += '<tr><td bgcolor=\'#000000\'><center><font color=\'#cccc00\' size=4><b><a href="#subParams" onclick="javascript: toggleVisibleSubParams(); return false;" style=\'color: rgb(204,204,0); text-decoration: none\'>Submission Parameters <font id="subParams_toggle" color=\'#ff0000\'>[+]</font></a></b></font></center></td></tr>\n'
    s += '<tr id="subParams" style="display: none;"><td bgcolor=\'#cccccc\'>\n'
    s += '<table width=\'100%\' cellpadding=\'5%\'>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>Basic Parameters</font></b></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Job Name:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="job_name"></span></center></td></tr></table></td></tr>\n'
    s+= '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Job ID:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="job_id"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Genes Submitted:</b></font> <span style="color:#ff0000"><b>(<a href="/genes/' + id + '/">gene mapping as CSV</a>)</b></span>'
    s += '</td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="genes_submitted"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Genes Annotated with Sequences:</b></font></td><td bgcolor=\'#66666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="annotated_sequences"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td colspan=2></td></tr>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>Weeder Parameters</font></b></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Motif Sizes:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="motif_sizes"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Background Model:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="background_model"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td colspan=2></td></tr>\n'
    s += '<tr><td colspan=2 bgcolor=\'#333333\' align=\'center\' valign=\'center\'><b><font color=\'#cccc00\' size=4>miRvestigator HMM Parameters</font></b></td></tr>\n'
    s+= '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Seed Models:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="seed_model"></span></center></td></tr></table></td></tr>\n'
    s += '<tr><td bgcolor=\'#333333\' align=\'center\' valign=\'center\'><font color=\'#ffffff\'><b>Model Wobble Base-Pairing:</b></font></td><td bgcolor=\'#666666\' align=\'center\' valign=\'center\'><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#ffffff\'><center><span id="model_wobble"></span></center></td></tr></table></td></tr>\n'
    s += '</table>\n'
    s += '</td></tr>\n'
    s += '<tr><td></td></tr>\n'
    s += '<tr><td bgcolor=\'#000000\'><center><font color=\'#cccc00\' size=4><b>Summary of Results</b></font></center></td></tr>\n'
    # Make a table that looks like this:
    # motif consensus | top miRNA | seed | length | vitPValue | # seqs with site (% of seqs with site)
    if (len(motifs)==0):
        s+= '<div style="text-align:center;background:#333333;color:#cc0000;font-weight:bold;padding-top:6ex;padding-bottom:6ex;">No motifs found.</div>'

    s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Motif</font></b></center></td><td bgcolor=\'#333333\'><\
center><b><font color=\'#ffffff\'>Top miRNA</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementary Base-Pairing</font></b></center></td><td bgcolor=\'#3333\
33\'><center><b><font color=\'#ffffff\'>Complementarity</br>P-Value</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>% of Input</br>Sequences with Site</font>\
</b></center></td></tr>\n'

    pssm_images_dir = config.get('General', 'pssm_images_dir')
    for motif in motifs:
        scoreList = read_mirvestigator_scores(motif['motif_id'])
        if topRet=='all':
            topRet = len(scoreList)
        else:
            topRet = int(topRet)

        top_score_i = 1
        top_score_viterbi_p = float(scoreList[0]['vitPValue'])
        row_count = 1
        while float(scoreList[top_score_i]['vitPValue']) == top_score_viterbi_p:
            row_count +=1
            top_score_i += 1

        top_score_i = 0
        i = scoreList[top_score_i]
        align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
        png_path = os.path.join(pssm_images_dir, '%s_%s.png' % (id, motif['name']))
        if not os.path.exists(png_path):
            plotPssmMatrix(motif['matrix'], png_path)

        s += '<tr>'
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center><a href=\'#'+motif['name']+'_miRNAs\'><img src=\'/images\
/pssms/'+id+'_'+motif['name']+'.png\' alt=\''+conv2rna(str(motif['name']))+'\'></center></td>'
        s += '<td bgcolor=\'#ffffff\'><center>'
        miRNAs = []
        for miRNA_name in i['miRNA.name'].split('_'):
            miRNA_name = miRNA_name.strip()
            if miRNADict.has_key(miRNA_name):
                miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
            else:
                miRNAs.append(miRNA_name)
        s += '</br>'.join(miRNAs)
        s += '</center></td>\n'

        s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>\n'
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center>'+str('%.1e' % float(i['vitPValue']))+'</center></td>\n'
        # Get number or sequences with a hit                                                                                                                                         
        genesWithSite = []
        for site in motif['sites']:
            if float(site['match']) >= qualityThreshold and not site['gene'] in genesWithSite:
                genesWithSite.append(site['gene'])
        if (annotatedSequences==0):
            percGenes = "?error?"
        else:
            percGenes = (float(len(genesWithSite))/float(annotatedSequences))*float(100)
            if percGenes==float(100):
                percGenes = str(100)
            else:
                percGenes = str('%.2g' % percGenes)
        s += '<td bgcolor=\'#ffffff\' rowspan="' + str(row_count) + '"><center><a href=\'#'+motif['name']+'_sites\'>'+percGenes+'%</a></center></td>\n'
        s += '</tr>\n'
        top_score_i += 1

        while float(scoreList[top_score_i]['vitPValue']) == top_score_viterbi_p:
            i = scoreList[top_score_i]
            align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
            s += '<tr>'
            s += '<td bgcolor=\'#ffffff\'><center>'
            miRNAs = []
            for miRNA_name in i['miRNA.name'].split('_'):
                miRNA_name = miRNA_name.strip()
                if miRNADict.has_key(miRNA_name):
                    miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
                else:
                    miRNAs.append(miRNA_name)
            s += '</br>'.join(miRNAs)
            s += '</center></td>\n'
                    
            s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>\n'
            s += '</tr>\n'
            top_score_i += 1

    s += '</table></p>\n'

    for motif in motifs:
        s += '<p  id=\''+motif['name']+'_miRNAs\'><table width=\'100%\'cellpadding=\'15%\'>\n<tr>\n<td align=\'center\' valign=\'center\' bgcolor=\'#000000\'>\n<font size=4><b><font color=\'#cccc00\'>'
        if not topRet=='all':
            s += 'Top <font color=\'#ff0000\' size=4>'+str(topRet)+'</font>'
        elif topRet=='all':
            s += '<font color=\'#ff0000\' size=4>All</font>'
        s += ' miRNAs Complementary to the Weeder Motif</font> <font color=\'#ff0000\'>'+conv2rna(motif['name'])+'</font> &nbsp;'
        s += '<a href="#results" onclick="toggleVisible(\'results\', this); return false;" style=\'color: #ff0000; text-decoration: none\'>[+]</a></b></font></td></tr>\n'
        s += '<tr id="results" style="display: none;" width=600>\n'
        s += '<td bgcolor="#333333">\n'
        s += '<font color="#ffffff"><b>What do the columns mean?</b> <p><ul><li><b>miRNA Name</b> = The name of the name(s) for the unique seed sequence. There may be more than one miRNA annotated for a unique seed sequence because they vary in the 3\' terminus of the mature miRNA. Each miRNA is a link to it\'s entry on <a href="http://www.mirbase.org" style="color: rgb(204,204,0)" target="_blank">miRBase</a></li>&nbsp;</br> <li><b>miRNA Seed</b> = The sequence for seed that is the most complementary to the over-represented motif. The seed will be as long as the seed model described in the next column.</li>&nbsp;</br> <li><b>Seed Model</b> = Base-pairing models for the seed regions of a miRNA to the 3\' UTR of target transcripts. The 8mer, 7mer-m8, and 7mer-a1 models are the canonical models of miRNA to mRNA base-pairing. The 6mer models are considered marginal models as they typically have a reduced efficacy and are more likely to occur by chance alone. By default all of the seed models are used. The seed models are described in this figure:</br>&nbsp;</br><center><img src="/images/seedModels.gif" width=400></center></li>&nbsp;</br><li><b>Length of Complementarity</b> = The length of complementarity (or wobble if enabled) base-pairing between the sequence motif and the miRNA seed sequence.</li>&nbsp;</br> <li><b>Complementary Base-Pairing</b> = The complementarity of the over-represented sequence motif on top 5\'&rArr;3\' to the miRNA seed sequence given the seed model 3\'&rArr;5\'. (<b>Note:</b> <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#ff0000">|</font></b>&nbsp;</span> = a complementary, <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#0000ff">:</font></b>&nbsp;</span> = a wobble, <span style=\'background-color: rgb(255,255,255);\'><font color="#000000">"</font> <font color="#000000">"</font></span> (space) = not complementary, and for the sequences <span style=\'background-color: rgb(255,255,255);\'>&nbsp;<b><font color="#cccccc">-</font></b>&nbsp;</span> = a gapping at the start or end.)</li>&nbsp;</br> <li><b>Complementarity P-Value</b> = Significance of complementarity between the over-represented sequence motif and the miRNA seed sequence. (<b>Note:</b> A perfectly complementary 8mer seed model has a Complementarity p-value of 1.5e-05, for a 7mer seed model 6.1e-05, and for a 6mer seed model 2.4e-04.)</li></ul> <b>What is considered good complementarity?</b> <p>This will depend upon your data and what downstream analysis you plan to do with it. But a good rule of thumb is that if you find perfect complementarity for a 7mer or 8mer (Complementary P-Value = <font color="#ff0000"><b>6.1e-05</b></font> and <font color="#ff0000"><b>1.5e-05</b></font>; respectively) this is likely to be of interest. Follow up with experimental studies will help to determine the false discovery rate for your dataset.</p></font>\n'
        s += '</td>\n'
        s += '</tr>\n'
        s += '</table>\n'
        scoreList = read_mirvestigator_scores(motif['motif_id'])
        if topRet=='all':
            topRet = len(scoreList)
        else:
            topRet = int(topRet)
        s += '<table width="100%%" cellpadding="5%%"><tr><td style="text-align:center; font-size:10pt; background:#cccccc;"><a href="/scores/csv/%s">Download table as CSV</a></td></tr></table>\n' % (motif['motif_id'],)
        s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>miRNA Name</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>miRNA Seed</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Seed Model</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Length of</br>Complementarity</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementary Base-Pairing</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Complementarity</br>P-Value</font></b></center></td></tr>'
        for k in range(topRet):
            i = scoreList[k]
            align1 = alignSeed(i['statePath'], i['miRNA.seed'], motif['name'])
            s += '<tr>'
            s += '<td bgcolor=\'#ffffff\'><center>'
            
            # generate links to mirbase miRNAs
            miRNAs = []
            for miRNA_name in i['miRNA.name'].split('_'):
                miRNA_name = miRNA_name.strip()
                if miRNADict.has_key(miRNA_name):
                    miRNAs.append('<a href=\'http://mirbase.org/cgi-bin/mature.pl?mature_acc='+str(miRNADict[miRNA_name])+'\' target=\'_blank\'>'+miRNA_name+'</a>')
                else:
                    miRNAs.append(miRNA_name)
            
            s += '</br>'.join(miRNAs)
            s += '</center></td>'

            s += '<td bgcolor=\'#ffffff\'><center>'+conv2rna(reverseComplement(str(i['miRNA.seed'])))+'</center></td>'
            s += '<td bgcolor=\'#ffffff\'><center>'+str(i['model'])+'</center></td><td bgcolor=\'#ffffff\'><center>'+str(align1[3])+'</center></td>'
            s += '<td bgcolor=\'#ffffff\'><center><pre>'+str(align1[0])+'\n'+str(align1[1])+'\n'+str(align1[2])+'</pre></center></td>'
            s += '<td bgcolor=\'#ffffff\'><center>'+str('%.1e' % float(i['vitPValue']))+'</center></td>'
            s += '</tr>'
        s += '</table></p>'
        #'gene':seqDict[splitUp[0]], 'strand':splitUp[1], 'site':splitUp[2], 'start':splitUp[3], 'match':splitUp[4].lstrip('(').rstrip(')')
        # pssm1.nsites
        s += '<p id=\''+motif['name']+'_sites\'><table width=\'100%\' bgcolor=\'#000000\' cellpadding=\'15%\'><tr><td align=\'center\' valign=\'center\'><font size=4><b><font color=\'#cccc00\'>Position of Putative miRNA Binding Sites in Submitted Genes</br>for the Weeder Motif</font> <font color=\'#ff0000\'>'+conv2rna(motif['name'])+'</font> &nbsp;'
        s += '<a href="#sites" onclick="toggleVisible(\'sites\', this); return false;" style=\'color: #ff0000; text-decoration: none\'>[+]</a></b></font></td></tr>\n'
        s += '<tr id="sites" style="display: none;" width=600><td bgcolor="#333333"><font color="#ffffff">\n'
        s += '<b>Where do these sites come from?</b>\n<p>As part of the miRvestigator framework <a href="http://159.149.109.9/modtools/" style="color: rgb(204,204,0)" target="_blank">Weeder</a> provides predicted miRNA binging sites in the 3\' untranslated regions (UTRs) of the analyzed genes. Predicted binding sites were split into three different similarity bins:  <font color="#ff0000">High quality</font> - &#8805; 95% similarity to the miRNA seed sequence (red), <font color="#cccc00">Medium quality</font> 95% &#8805; similarity &#8805; 90% to the miRNA seed sequence (yellow), and <font color="#00ff00">Fair quality</font> 90% &#8805; similarity &#8805; 85% to the miRNA seed sequence (green). These sites can be used to develop follow-up experiments such as luciferase reporter assays to validate the efficacy of these sites.</p>\n<b>What do the columns mean?</b>\n<p><ul><li><b>Gene</b> = gene identifier from the input set that links to the mapped Entrez identifier page on NCBI.</li></br>&nbsp;</br>\n<li><b>Gene Symbol</b> = Official gene symbol if available.</li></br>&nbsp;</br>\n<li><b>Site</b> = The sequence for site identified by Weeder. If it is in square brackets indicates that the site is of lower similarity.</li></br>&nbsp;</br>\n<li><b>Start Relative to Stop Codon</b> = The 3\' UTR begins following the stop codon (which is set at 0 base-pairs (bp)). Thus the values in this column describe the start of the site in bp after the stop codon.</li></br>&nbsp;</br>\n<li><b>% Similarity to Consensus Motif</b> = The similarity of the predicted site to the consensus motif is computed as a percentage. Predicted binding sites were split into three different similarity to consensus bins:  <font color="#ff0000">High quality</font> - &#8805; 95% similarity to the miRNA seed sequence (red), <font color="#cccc00">Medium quality</font> 95% &#8805; similarity &#8805; 90% to the miRNA seed sequence (yellow), and <font color="#00ff00">Fair quality</font> 90% &#8805; similarity &#8805; 85% to the miRNA seed sequence (green).</li></br>&nbsp;</br>\n<li><b>Minimum Free Energy (MFE) of mRNA-miRNA Duplex</b> = the minimum free energy (MFE) of duplexing for the reverse complement of the motif consensus sequence and putative target site sequences using the ViennaRNA package.</li></ul></p>'
        s += '</font></td></td></table>'
        s += '<table width="100%%" cellpadding="5%%"><tr><td style="text-align:center; font-size:10pt; background:#cccccc;"><a href="/sites/csv/%s">Download table as CSV</a></td></tr></table>\n' % (motif['motif_id'],)
        s += '<table width=\'100%\' cellpadding=\'15%\'><tr><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Gene</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Gene symbol</font><\
/b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Sequence of Site</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Start Relative to</br>Stop Codon (bp)</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>% Similarity to Consensus Motif</br>(Quality = </font><font color=\'#cc0000\'>High</font><font color=\'#ffffff\'> | </font><font color=\'#cccc00\'>Medium</font><font color=\'#ffffff\'> | </font><font color=\'#00cc00\'>Fair</font><font color=\'#ffffff\'>)</font></b></center></td><td bgcolor=\'#333333\'><center><b><font color=\'#ffffff\'>Minimum Free Energy (MFE)</br>of mRNA-miRNA Duplex</font></b></center></td></tr>'
        for i in motif['sites']:
            if float(i['match']) >= qualityThreshold:
                col1 = '#000000'
                if float(i['match']) >= float(95):
                    col1 = '#cc0000'
                elif float(i['match']) >= float(90):
                    col1 = '#cccc00'
                elif float(i['match']) >= float(85):
                    col1 = '#00cc00'
                gene_name_map = gene_mapping.get(i['gene'], None)
                if (gene_name_map==None or geneId=='symbol' or geneId=='entrez'):
                    gene_name = i['gene']
                else:
                    gene_name = gene_name_map['name']
                if (gene_name_map==None or gene_name_map['symbol'] == None):
                    gene_symbol = ''
                else:
                    gene_symbol = ' ' + gene_name_map['symbol']
                s += '<tr><td bgcolor=\'#ffffff\'><center>'+str('<a href=\'http://www.ncbi.nlm.nih.gov/gene/'+str(i['gene'])+'\' target=\'_blank\'>'+gene_name+'</a>')+'<td bgcolor=\'#ffffff\'><center>'+gene_symbol+'</center></td>'+'</center></td><td bgcolor=\'#ffffff\'><center>'+conv2rna(str(i['site']))+'</center></td><td bgcolor=\'#ffffff\'><center>'+str(i['start'])+'</center></td><td bgcolor=\'#ffffff\'><font color=\''+str(col1)+'\'><center><b>'+i['match']+'</b></center></font></td><td bgcolor=\'#ffffff\'><center>'+str(i['mfe'])+'</center></td></tr>'
        s += '</table></p>'
    s += '<p><table width=\'100%\' cellpadding=\'5%\'><tr><td bgcolor=\'#c0c0c0\'><center>Need help? Please contact <font color=\'#0000ff\'>wwu(at)systemsbiology.org</font> if you have any questions, comments or concerns.<br>Developed at the <a href=\'http://www.systemsbiology.org\' target=\'_blank\' style=\'color: rgb(0,0,255)\'>Institute for Systems Biology</a> in the <a href=\'http://baliga.systemsbiology.net/\' target=\'_blank\' style=\'color: rgb(0,0,255)\'>Baliga Lab</a>.</center></td></tr></table></p>'
    s += '</center></td></tr></table></td></tr></table></center></body></html>'
    return s
def parameters(req):
    id = str(req.form.getfirst('id',''))
    req.content_type='application/json'
    return json.dumps(read_parameters(id))