Commit fb3a10ed authored by Nijveen, Harm's avatar Nijveen, Harm
Browse files

refactored the javascript functionality back to coffeescript for the multiplot section

made the multiplot more flexible, taking most input from the json files
parent faa69d39
Pipeline #1648 skipped
......@@ -7,7 +7,7 @@ from experiment.Celegans import Celegans
from django.shortcuts import render,render_to_response,HttpResponse
from django.conf import settings
from main.models import ArraySpot,Experiment,Transcript,Marker
from main.models import ArraySpot,Experiment,Transcript,Marker, Species, Chromosome, GeneInfo
from main.parser import outputJSON
# Create your views here.
......@@ -47,49 +47,9 @@ def eQTLPlotView(request):
else:
return render_to_response('eQTL.html',{'experiments':experiments})
def getMarkers(experiment):
marker_list = Marker.objects.filter(Experiment__experiment_name = experiment).order_by('marker_chr','marker_start').values_list('marker_name',flat=True)
return marker_list
def getChrMarkers(experiment,i):
marker_list = Marker.objects.filter(Experiment__experiment_name = experiment,marker_chr=i).order_by('marker_start').values_list('marker_name',flat=True)
encode_marker_list = [marker.encode('ascii','ignore') for marker in marker_list]
return encode_marker_list
def getMarkerObjects(experiment):
marker_list = Marker.objects.filter(Experiment__experiment_name = experiment).order_by('marker_chr','marker_start')
return marker_list
def getSpecies(exp):
species_name = Experiment.objects.get(experiment_name = exp).species.species_name
return species_name
def getMarkerPos(exp,marker):
return Marker.objects.get(experiment_name = exp,marker_name = marker).marker_start
def ranges(series_list):
'''
identify groups of continuous numbers in a list and group the continuous numbers as a sub-list
for example,
[1 ,2 ,6, 7, 8, 9, 10, 11]
[(1,2),(6, 11)]
'''
start,end = series_list[0],series_list[0]
for n in series_list[1:]:
if n-1 ==end: # Part of the group, bump the end
end = n
else: # Not part of the group, yield current group and start a new
yield start,end
start = end = n
yield start,end #Yield the last group
def getSpecies(exp):
species_name = Experiment.objects.get(experiment_name = exp).species.species_name
return species_name
def outputJson(experiment_name,qtl_file,thld,output):
'''
description: parse upload information into JSON file format later which later will be used for cistrans plot.
description: parse upload information into JSON file format later which later will be used for multiexperimentplot plot.
@type experiment_name: name of experiment
@param experiment_name: string
......@@ -136,7 +96,7 @@ def outputJson(experiment_name,qtl_file,thld,output):
output_dic['pmarknames'] = chr_marker_list_dic
# define KEY markers
markers = list(getMarkers(experiment_name))
markers = list(getMarkerNames(experiment_name))
output_dic['markers'] = markers
# define KEY pmark
......
......@@ -7,7 +7,7 @@
<link rel="stylesheet" href="{% static 'css/multiexperimentplot.css' %}" type="text/css"/>
<link rel="stylesheet" href="{% static 'css/coregulation.css' %}" type="text/css"/>
<script type="text/javascript" src="{% static 'js/d3.js' %}"></script>
<script type="text/javascript" src="{% static 'js/d3.tip.old.js' %}"></script>
<script type="text/javascript" src="{% static 'js/d3-tip.js' %}"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script>
......@@ -22,7 +22,7 @@ $(function(){
</center>
{% if experiment_name %}
<form action="" method="get">
Experiment name:
Experiment:
<select id="experiment_name" name="experiment_name">
{% for experiment in experiments %}
<option value= "{{ experiment }}" {%if experiment == experiment_name %} selected {% endif %}>{{ experiment }}</option>
......@@ -35,19 +35,33 @@ $(function(){
</form>
<div id ="multiexperimentplot"></div>
<div id="container" align="center">
<div id="genes" align="center">
<p>
<input class="search" placeholder="Search" />
<table cellspacing='0'>
<tr><th>Transcript&nbsp;ID (count={{ gene_list|length }})</th><th>LOD score</th><th>Description</th></tr>
<tr>
<th class="sort" data-sort="gene_name">Transcript&nbsp;ID (count={{ gene_list|length }})</th>
<th class="sort" data-sort="lodscore">LOD score</th>
<th>Description</th></tr>
<tbody class="list">
{% for gene in gene_list %}
<tr><td><A href="/AraQTL/multiexperimentplot/?gene_name={{ gene.transcript_name|upper }}" TARGET="_blank">{{ gene.transcript_name }}</a></td>
<td>{{ gene.LOD|floatformat:2 }}</td>
<td align=left>{{ gene.description }}</td></tr>
<tr><td class="gene_name"><A href="/AraQTL/multiexperimentplot/?gene_name={{ gene.transcript_name|upper }}" TARGET="_blank">{{ gene.transcript_name }}</a></td>
<td class="lodscore">{{ gene.LOD|floatformat:2 }}</td>
<td class="description" align=left>{{ gene.description }}</td></tr>
{% endfor %}
</tbody>
</table>
<script src="http://listjs.com/no-cdn/list.js"></script>
</div>
<script type="text/javascript">
var options = {
valueNames: [ 'gene_name', 'lodscore', 'description' ]
};
var userList = new List('genes', options);
(function() {
var draw;
......@@ -196,13 +210,14 @@ $(function(){
attr("fill", titlecolor);
//################################### END #######################################
//marker tooltip//////////////////////
martip = d3.svg.tip().orient("right").padding(3).text(function(z) {
return z;
}).attr("class", "d3-tip").attr("id", "martip");
//marker tooltip//////////////////////
martip = d3.tip().attr("class", "d3-tip")
.offset([-10, 0])
.html(function(y,z) {
return "marker: " +y + " (" + z + ")<br><i>click for genes peaking here</i>";
})
//end/////////////////////////////////
cur = 0;
_ref8 = data.chrnames;
for (_q = 0, _len8 = _ref8.length; _q < _len8; _q++) {
......@@ -347,6 +362,7 @@ $(function(){
//########################## end ############################
_ref13 = probe_data_set;
svg.call(martip)
{% for gene in gene_list|slice:":10" %}
svg.append("g").
attr("id", "markerCircle").
......@@ -362,10 +378,12 @@ $(function(){
if (!markerClick[td]) {
d3.select(this).attr("opacity", 1);
}
return martip.call(this, "marker: "+td + "(" + probe_data_set[{{ forloop.counter0 }}].probe + ")");
return martip.show(td,probe_data_set[{{ forloop.counter0 }}].probe);
}).on("mouseout", function(td) {
d3.select(this).attr("opacity", markerClick[td]);
return d3.selectAll("#martip").remove();
return martip.hide();
}).on("click", function(d) {
location.href="/AraQTL/coregulation/?experiment_name={{ experiment_name }}&marker="+d;
});
//end draw LOD score plot
{% endfor %}
......@@ -387,8 +405,7 @@ $(function(){
{% else %}
<form action="" method="get">
Experiment name:
<select id="experiment_name" name="experiment_name">
Experiment:<select id="experiment_name" name="experiment_name">
{% for experiment in experiments %}
<option value= "{{ experiment }}">{{ experiment }}</option>
{% endfor %}
......
......@@ -7,8 +7,7 @@ from experiment.Celegans import Celegans
from django.shortcuts import render,render_to_response,HttpResponse
from django.conf import settings
from main.models import ArraySpot,Experiment,Transcript,Marker,GeneInfo
from main.parser import outputJSON
from main.models import Experiment,Transcript,Marker,GeneInfo
# Create your views here.
class GeneInfoMarker:
......@@ -31,7 +30,7 @@ def selectMarker(request):
if request.GET.get('thld'):
try:
thld = float(request.GET.get('thld'))
lodthld = float(request.GET.get('thld'))
except ValueError:
return HttpResponse('<h1> invalid LOD threshold </h1>')
......@@ -82,195 +81,3 @@ def getLodScoresForMarker(experiment,marker):
return lodscores
def outputJson(experiment_name,qtl_file,thld,output):
'''
description: parse upload information into JSON file format later which later will be used for cistrans plot.
@type experiment_name: name of experiment
@param experiment_name: string
@type qtl_file: qtl mapping output
@param qtl_file: file
@type thld: LOD theshold
@param thld: decimal
@rtype output: path of output file
@return output: string
'''
in_path = os.path.join(settings.MEDIA_ROOT,'data/%s/%s' % (experiment_name,qtl_file))
out_path = os.path.join(settings.MEDIA_ROOT,'data/%s/%s' % (experiment_name,output))
output_dic = {}
#define KEY chrnames and chr
species_name = getSpecies(experiment_name)
chr_nr = None
if species_name == 'Arabidopsis Thaliana':
arabidopsis = Arabidopsis()
output_dic['chrnames'] = arabidopsis.getChr()
output_dic['chr'] = arabidopsis.getChrLen()
chr_nr = output_dic['chrnames']
elif species_name == 'Caenorhabditis Elegans':
celegans = Celegans()
output_dic['chrnames'] = celegans.getChr()
output_dic['chr'] = celegans.getChrLen()
chr_nr = output_dic['chrnames']
#define KEY spec
output_dic['spec'] = species_name;
#define KEY pmarknames
chr_marker_list_dic = {}
for i in chr_nr:
chr_marker_list_dic[i] = list(getChrMarkers(experiment_name,i)) #django.db.models.query.ValuesListQuerySet
output_dic['pmarknames'] = chr_marker_list_dic
# define KEY markers
markers = list(getMarkers(experiment_name))
output_dic['markers'] = markers
# define KEY pmark
marker_list_dic = {}
marker_queryset_list = getMarkerObjects(experiment_name)
for m in marker_queryset_list:
m_info ={'chr':m.marker_chr,'start':int(m.marker_start)}
marker_list_dic[m.marker_name.encode('ascii','ignore')] = m_info
output_dic['pmark'] = marker_list_dic
# define KEY spot/gene
spots_list = ArraySpot.objects.filter(experiment_name = experiment_name)
spots_dic = {}
for spot in spots_list:
trans_ins = Transcript.objects.get(spot_id= spot)
if trans_ins.start !='' and trans_ins.chr !='' :
spots_dic[spot.spot_id] = {'chr':trans_ins.chr,'start':int(trans_ins.start),'end':int(trans_ins.end),'ref':trans_ins.ref_id,'transcript':trans_ins.transcript_name}
else:
spots_dic[spot.spot_id] = {'chr':'1','start':0,'end':0,'ref':trans_ins.ref_id,'transcript':trans_ins.transcript_name}
output_dic['spot'] = spots_dic
with open(in_path) as fi:
i = 0
j = 0
markers_lod = []
peaks_list = []
chr_names = output_dic['chrnames']
with open(out_path,'w') as fo:
first_line = fi.readline().rstrip()
markers_lod = first_line.split('\t')[1:]
lines = fi.readlines()
#count number of markers per chromosome
chr_nr_of_markers = {}
nr_of_markers = 0
for chr_ref1 in chr_names:
chr_nr_of_markers[chr_ref1] = len(output_dic['pmarknames'][chr_ref1])
for line in lines:
j+=1
col = line.rstrip().split('\t')
# assign LOD value to 0 if null
lod_list = [ float(lod) if lod!='' else 0.0 for lod in col[1:]]
k = 0 # splice index
lod_list_chr = {} #initialise lod_list_chr
#splice lod_list to sub_lod_list per chromosome
for chr_ref2 in chr_names:
lod_list_chr[chr_ref2] = lod_list[k:k+chr_nr_of_markers[chr_ref2]]
k += chr_nr_of_markers[chr_ref2]
for chr_ref3 in chr_names: # lod list per chromosome
#check if any significant LOD value in the list per chromosome
if any(x for x in lod_list_chr[chr_ref3] if abs(x)>=thld):
interval_list = []
ref_index_list = []
for m in range(len(lod_list_chr[chr_ref3])):
if abs(lod_list_chr[chr_ref3][m])>=thld:
ref_index_list.append(m)
#group continuous significant LOD
interval_list = list(ranges(ref_index_list))
# if any intervals
for interval in interval_list:
i+=1
start,end = interval[0],interval[1]
#LOD peak and LOD index
#find the max LOD value per significant LOD interval per chromosome
qtl_lod = max(lod_list_chr[chr_ref3][start:end+1])
qtl_index = lod_list_chr[chr_ref3].index(qtl_lod) # LOD peak
#initialize eQTL interval
inv_start = inv_start_lod_support = output_dic['chr'][chr_ref3]['start']
inv_end = inv_end_lod_support = output_dic['chr'][chr_ref3]['end']
#1 LOD support interval
lod_support = qtl_lod-1
##initialize eQTL interval
inv_start_ind_lod_support = inv_end_ind_lod_support = qtl_index
for ind in range(qtl_index,start-1 if start!=0 else start):
if lod_list_chr[chr_ref3][ind]>lod_support:
inv_start_ind_lod_support = ind
for ind in range(qtl_index,end+1): #end+1 will not raise list index exception
if lod_list_chr[chr_ref3][ind]>lod_support:
inv_end_ind_lod_support = ind
#define LOD interval
if start>0 and end<len(lod_list_chr[chr_ref3])-1:
inv_start = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][start-1])
inv_end = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][end+1])
if start==0 and end<len(lod_list_chr[chr_ref3])-1:
inv_end = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][end+1])
if start>0 and end==len(lod_list_chr[chr_ref3])-1:
inv_start = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][start-1])
#define one LOD support interval
if inv_start_ind_lod_support ==0 and inv_end_ind_lod_support != len(lod_list_chr[chr_ref3])-1:
inv_end_lod_support = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][inv_end_ind_lod_support+1])
if inv_start_ind_lod_support !=0 and inv_end_ind_lod_support== len(lod_list_chr[chr_ref3])-1:
inv_start_lod_support = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][inv_start_ind_lod_support-1])
if inv_start_ind_lod_support !=0 and inv_end_ind_lod_support!= len(lod_list_chr[chr_ref3])-1:
inv_start_lod_support = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][inv_start_ind_lod_support-1])
inv_end_lod_support = getMarkerPos(experiment_name,output_dic['pmarknames'][chr_ref3][inv_end_ind_lod_support+1])
#plot only peak LOD
lod_dic={} #empty dictionary for each iteration
#save to lod_dic
lod_dic['interval'] = '%s:%s-%s' % (chr_ref3,inv_start,inv_end)
lod_dic['lod_support_interval'] = '%s:%s-%s' % (chr_ref3,inv_start_lod_support,inv_end_lod_support)
lod_dic['spot'] = col[0].upper()
lod_dic['marker'] = output_dic['pmarknames'][chr_ref3][qtl_index]
lod_dic['lod'] = lod_list_chr[chr_ref3][qtl_index]
lod_dic['transcript'] = output_dic['spot'][col[0].upper()]['transcript']
lod_dic['ref'] = output_dic['spot'][col[0].upper()]['ref']
peaks_list.append(lod_dic)
'''
#plot every significant LOD scores
for step in range(start,end+1):
lod_dic={} #empty dictionary for each iteration
#save to lod_dic
lod_dic['interval'] = '%s:%s-%s' % (chr_ref3,inv_start,inv_end)
lod_dic['spot'] = col[0]
lod_dic['marker'] = output_dic['pmarknames'][chr_ref3][step]
lod_dic['lod'] = lod_list_chr[chr_ref3][step]
peaks_list.append(lod_dic)
'''
output_dic['peaks'] = peaks_list
with open(out_path,'w') as fo:
json.dump(output_dic,fo,indent=4)
print i,j
......@@ -8,7 +8,7 @@
<link rel="stylesheet" href="{% static 'css/multiexperimentplot.css' %}" type="text/css"/>
<link rel="stylesheet" href="{% static 'css/correlation.css' %}" type="text/css"/>
<script type="text/javascript" src="{% static 'js/d3.js' %}"></script>
<script type="text/javascript" src="{% static 'js/d3.tip.old.js' %}"></script>
<script type="text/javascript" src="{% static 'js/d3-tip.js' %}"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script>
......@@ -200,10 +200,13 @@ $(function(){
//################################### END #######################################
//marker tooltip//////////////////////
martip = d3.svg.tip().orient("right").padding(3).text(function(z) {
return z;
}).attr("class", "d3-tip").attr("id", "martip");
martip = d3.tip().attr("class", "d3-tip")
.offset([-10, 0])
.html(function(y,z) {
return "marker: " +y + " (" + z + ")<br><i>click for genes peaking here</i>";
})
//end/////////////////////////////////
cur = 0;
......@@ -367,6 +370,7 @@ $(function(){
//########################## end ############################
_ref13 = probe_data_set;
svg.call(martip)
{% for gene in gene_list|slice:":10" %}
svg.append("g").
attr("id", "markerCircle").
......@@ -382,10 +386,12 @@ $(function(){
if (!markerClick[td]) {
d3.select(this).attr("opacity", 1);
}
return martip.call(this, "marker: "+td + "(" + probe_data_set[{{ forloop.counter0 }}].probe + ")");
return martip.show(td,probe_data_set[{{ forloop.counter0 }}].probe);
}).on("mouseout", function(td) {
d3.select(this).attr("opacity", markerClick[td]);
return d3.selectAll("#martip").remove();
return martip.hide();
}).on("click", function(d) {
location.href="/AraQTL/coregulation/?experiment_name={{ experiment_name }}&marker="+d;
});
//end draw LOD score plot
{% endfor %}
......
......@@ -19,6 +19,17 @@ class Species(models.Model):
db_table = 'main_species'
class Chromosome(models.Model):
name = models.CharField(max_length=50)
start = models.IntegerField()
end = models.IntegerField()
species = models.ForeignKey(Species)
class Meta:
managed = False
db_table = 'main_chromosome'
class Experiment(models.Model):
species = models.ForeignKey(Species)
experiment_name = models.CharField(max_length=50)
......@@ -77,8 +88,10 @@ class GeneInfo(models.Model):
class Transcript(models.Model):
geneinfo = models.ForeignKey(GeneInfo)
transcript_name = models.CharField(max_length=30, blank=True, null=True)
transcript_id = models.CharField(max_length=20, blank=True, null=True)
chr = models.CharField(max_length=3)
start = models.IntegerField(blank=True, null=True)
end = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
......
import json
import os
import sys
from main.models import Experiment, GeneGO,ArraySpot,Transcript,Marker, Species, Chromosome, GeneInfo
from math import factorial
from django.conf import settings
def testGOaccession(exp_name,genelist,GO_accession):
GI = GeneInfo.objects.filter(arrayspot__experiment__experiment_name = exp_name)
ctotalGenes = GI.count()
GIGO = GI.filter(genego__term_accession = GO_accession)
ctotalGenesAccession = GIGO.count()
cgenelistAccession = GIGO.filter(gene_id__in = genelist).count()
cgenelist = len(genelist)
N = ctotalGenes
m1 = 1
m2 = N - m1
n1 = 1
n2 = 1
a = 1
b = 1
c = 1
d = 1
pvalue = factorial(m1)*factorial(m2)*factorial(n1)*factorial(n2)/(factorial(N)*factorial(a)*factorial(b)*(factorial(c)*(factorial(d))))
return pvalue
def loadGO_OBO(filename):
with open(filename) as GO_OBO_File:
for line in GO_OBO_File:
pass
def getChrMarkers(experiment_name, chromosome):
return Marker.objects.filter(experiment__experiment_name = experiment_name, marker_chr = chromosome)
def getMarkerObjects(experiment_name):
return Marker.objects.filter(experiment__experiment_name = experiment_name)
def getSpeciesForExperiment(experiment_name):
return Species.objects.get(experiment__experiment_name = experiment_name).species_name
def getMarkerNames(experiment_name):
return Marker.objects.filter(experiment__experiment_name = experiment_name).values_list("marker_name", flat=True)
def outputJsonForExperiment(experiment_name,qtl_file,output):
'''
description: parse upload information into JSON file format later which later will be used for multiexperimentplot plot.
@type experiment_name: name of experiment
@param experiment_name: string
@type qtl_file: qtl mapping output
@param qtl_file: file
@type thld: LOD theshold
@param thld: decimal
@rtype output: path of output file
@return output: string
'''
in_path = os.path.join(settings.MEDIA_ROOT,'data/%s/%s' % (experiment_name,qtl_file))
output_dic = {}
species_name = getSpeciesForExperiment(experiment_name)
output_dic['chrnames'] = Chromosome.objects.filter(species__species_name=species_name).values_list('name', flat=True)
output_dic['chr'] = Chromosome.objects.filter(species__species_name=species_name).count()
#define KEY spec
output_dic['species'] = species_name
with open(in_path) as fi:
first_line = fi.readline().rstrip()
markers = first_line.split('\t')[1:]
for line in fi:
output_dic = {}
fields = line.split('\t')
gene = fields[0]
output_dic["gene"] = gene
geneInfo_list = GeneInfo.objects.filter(gene_id = gene, species__species_name=species_name)
if len(geneInfo_list) == 0:
sys.stderr.write("gene_id not in database: %s, (species_name: %s)\n"%(gene, species_name))
continue
geneInfo = geneInfo_list[0]
output_dic["name"] = geneInfo.gene_name
output_dic["chr"] = geneInfo.chr
output_dic["start"] = geneInfo.start
output_dic["end"] = geneInfo.end
output_dic["experiment"] = experiment_name
marker_dict = {}
for idx, markerName in enumerate(markers):
markerList = Marker.objects.filter(experiment__experiment_name=experiment_name, marker_name=markerName)
if len(markerList) == 0:
sys.stderr.write("marker not in database: %s\n" % markerName)
continue
marker = markerList[0]
marker_dict[markerName] = dict(chr=marker.marker_chr, start=marker.marker_start, end=marker.marker_end,
lod=fields[idx + 1])
output_dic["lodscores"] = marker_dict
out_file = os.path.join(settings.MEDIA_ROOT, 'data/%s/genes/%s.json' % (experiment_name, gene))
with open(out_file, 'w') as fo:
json.dump(output_dic, fo, indent=4)
......@@ -8,7 +8,6 @@ from django.shortcuts import render,render_to_response,HttpResponse
from django.conf import settings
from main.models import ArraySpot,Experiment,Transcript,Marker, GeneInfo
from main.parser import outputJSON
# Create your views here.
def lodPlot(request):
......@@ -34,235 +33,3 @@ def lodPlot(request):
return render_to_response('plotLod.html')
def getMarkers(experiment):
marker_list = Marker.objects.filter(experiment_name = experiment).order_by('marker_chr','marker_start').values_list('marker_name',flat=True)
return marker_list
def getChrMarkers(experiment,i):
marker_list = Marker.objects.filter(experiment_name = experiment,marker_chr=i).order_by('marker_start').values_list('marker_name',flat=True)