Commit 682f5caf authored by Nijveen, Harm's avatar Nijveen, Harm
Browse files

various fixes

parent 48f9c234
Pipeline #1918 skipped
......@@ -29,15 +29,15 @@ def eQTLPlotView(request):
# outputJSON(lod_dic,probe_out_path)# write probe profile file in probe folder
if request.GET.get('thld'):
qtl_file = 'lod.txt'
try:
thld = float(request.GET.get('thld'))
output = 'lod%s.json' % request.GET.get('thld').encode('ascii','ignore')
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT,'data','%s' % exp_name,output)):
outputJson(exp_name,qtl_file,thld,output)
return render_to_response('eQTL.html',{'experiment_name':exp_name,
'experiments':experiments})
except ValueError:
return HttpResponse('<h1> invalid LOD threshold </h1>')
#try:
thld = float(request.GET.get('thld'))
output = 'lod%s.json' % request.GET.get('thld').encode('ascii','ignore')
if not os.path.isfile(os.path.join(settings.MEDIA_ROOT,'data','%s' % exp_name,output)):
outputJson(exp_name,qtl_file,thld,output)
return render_to_response('eQTL.html',{'experiment_name':exp_name,
'experiments':experiments})
#except ValueError:
#return HttpResponse('<h1> invalid LOD threshold %s</h1>'%thld)
else:
lodthld = Experiment.objects.get(experiment_name = exp_name).lodthld
......@@ -46,49 +46,85 @@ def eQTLPlotView(request):
'lodthld':lodthld})
else:
return render_to_response('eQTL.html',{'experiments':experiments})
def getMarkers(experiment):
marker_list = Marker.objects.filter(expierment__experiment_name = experiment).order_by('chromosome','start').values_list('name',flat=True)
return marker_list
def getChrMarkers(experiment,i):
marker_list = Marker.objects.filter(experiment__experiment_name = experiment,chromosome__name=i).order_by('start').values_list('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('chromosome','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__experiment_name = exp,name = marker).marker_start
def getMarkerNames(experiment_name):
return Marker.objects.filter(experiment__experiment_name = experiment_name).values_list("name", flat=True)
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 outputJson(experiment_name,qtl_file,thld,output):
'''
description: parse upload information into JSON file format later which later will be used for multiexperimentplot 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
@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']
chromosomes = Chromosome.objects.filter(species__species_name = species_name)
output_dic['chrnames'] = sorted(list(chromosomes.values_list("name", flat=True)))
chr_dic = dict()
for chromosome in chromosomes:
chr_dic[chromosome.name] = {"start": chromosome.start, "end": chromosome.end}
output_dic['chr'] = chr_dic
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:
......@@ -98,58 +134,58 @@ def outputJson(experiment_name,qtl_file,thld,output):
# define KEY markers
markers = list(getMarkerNames(experiment_name))
output_dic['markers'] = markers
# define KEY pmark
marker_list_dic = {}
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
m_info ={'chr':m.chromosome,'start':int(m.start)}
marker_list_dic[m.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_list = ArraySpot.objects.filter(experiment__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}
geneInfo = GeneInfo.objects.get(gene_id= spot)
if geneInfo.start !='' and geneInfo.chr !='' :
spots_dic[spot.spot_id] = {'chr':geneInfo.chr,'start':int(geneInfo.start),'end':int(geneInfo.end),'ref':geneInfo.ref_id,'transcript':geneInfo.transcript_name}
else:
spots_dic[spot.spot_id] = {'chr':'1','start':0,'end':0,'ref':trans_ins.ref_id,'transcript':trans_ins.transcript_name}
spots_dic[spot.spot_id] = {'chr':'1','start':0,'end':0,'ref':geneInfo.ref_id,'transcript':geneInfo.gene_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']
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:
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:]]
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
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 = []
......@@ -157,47 +193,47 @@ def outputJson(experiment_name,qtl_file,thld,output):
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
#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
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
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])
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])
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])
......@@ -205,36 +241,36 @@ def outputJson(experiment_name,qtl_file,thld,output):
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])
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['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']
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['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]
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
......
import json
import os
from experiment.Arabidopsis import Arabidopsis
from experiment.Celegans import Celegans
from django.shortcuts import render,render_to_response,HttpResponse
from django.shortcuts import render_to_response, HttpResponse
from django.conf import settings
from main.models import Experiment,Transcript,Marker,GeneInfo
from main.models import Experiment, GeneInfo, Species, Marker
# Create your views here.
class GeneInfoMarker:
......@@ -16,17 +14,22 @@ class GeneInfoMarker:
gene_name = ""
LOD = 0
def selectMarker(request):
'''
select a marker, experiment and threshold
'''
experiments = Experiment.objects.filter(species__species_name = "Arabidopsis thaliana").values_list('experiment_name',flat=True)
if request.method=='GET':
species_name = "Arabidopsis thaliana"
experiments = Experiment.objects.filter(species__species_name=species_name).values_list('experiment_name',
flat=True)
species_short_name = Species.objects.get(species_name=species_name).short_name
if request.method == 'GET':
if request.GET.get('experiment_name') and request.GET.get('marker'):
exp_name = request.GET.get('experiment_name')
marker = request.GET.get('marker')
lodthld = Experiment.objects.get(experiment_name = exp_name).lodthld
marker_name = request.GET.get('marker')
lodthld = Experiment.objects.get(experiment_name=exp_name).lodthld
if request.GET.get('thld'):
try:
......@@ -34,50 +37,54 @@ def selectMarker(request):
except ValueError:
return HttpResponse('<h1> invalid LOD threshold </h1>')
lodscores = getLodScoresForMarker(exp_name,marker)
if len(lodscores) == 0:
return HttpResponse('<h1> No information for marker %s</h1>'%marker)
lodscores = getLodScoresForMarker(exp_name, marker_name)
if len(lodscores) == 0:
return HttpResponse('<h1> No information for marker %s</h1>' % marker_name)
genes = {g: l for (g, l) in lodscores.items() if l >= lodthld}
genes = {g: l for (g,l) in lodscores.items() if l >= lodthld}
# genelist = GeneInfo.objects.filter(gene_id__in = genes.keys())
genelist = GeneInfo.objects.filter(gene_id__in = genes.keys())
genelist = list()
for gene in genes:
g = GeneInfoMarker()
g.gene_id = gene
g.LOD = genes[gene]
gi = GeneInfo.objects.filter(gene_id=gene)
if gi:
g.description = gi[0].description
g.gene_name = gi[0].gene_name
genelist.append(g)
genelist = list()
for gene in genes:
g = GeneInfoMarker()
g.transcript_name = gene
g.LOD = genes[gene]
gi = GeneInfo.objects.filter(gene_id = gene )
if gi:
g.description = gi[0].description
g.gene_name = gi[0].gene_name
genelist.append(g)
marker = Marker.objects.get(name = marker_name, experiment__experiment_name=exp_name)
return render_to_response('selectmarker.html',{'experiment_name': exp_name,
'experiments':experiments,
'marker':marker,
'gene_list':sorted(genelist, key=lambda x:x.LOD, reverse=True),
'lodthld':lodthld})
return render_to_response('peakmarker.html', {'experiment_name': exp_name,
'experiments': experiments,
'species': species_short_name,
'marker': marker,
'gene_list': sorted(genelist, key=lambda x: x.LOD,
reverse=True),
'lodthld': lodthld})
else:
return render_to_response('selectmarker.html',{'experiments':experiments})
def getLodScoresForMarker(experiment,marker):
lod_path = Experiment.objects.get(experiment_name = experiment).lod_file
lod_file_path = os.path.join(settings.MEDIA_ROOT,lod_path)
return render_to_response('peakmarker.html', {'experiments': experiments})
lodscores = dict()
with open(lod_file_path) as lod_file:
header = lod_file.readline().split('\t')
try:
mi = header.index(marker)
except ValueError:
return lodscores
def getLodScoresForMarker(experiment, marker):
lod_path = Experiment.objects.get(experiment_name=experiment).lod_file
lod_file_path = os.path.join(settings.MEDIA_ROOT, lod_path)
for line in lod_file.readlines():
fields = line.strip().split('\t')
geneID = fields[0].upper()
lodscores[fields[0]] = float(fields[mi])
lodscores = dict()
with open(lod_file_path) as lod_file:
header = lod_file.readline().split('\t')
try:
mi = header.index(marker)
except ValueError:
return lodscores
return lodscores
for line in lod_file.readlines():
fields = line.strip().split('\t')
geneID = fields[0].upper()
lodscores[fields[0]] = float(fields[mi])
return lodscores
......@@ -10,12 +10,8 @@
<script type="text/javascript" src="{% static 'js/d3.js' %}"></script>
<script type="text/javascript" src="{% static 'js/d3-tip.js' %}"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script type="text/javascript" src="{% static 'js/multiplot.js' %}"></script>
<script>
$(function(){
$(document).tooltip();
});
</script>
<div class="back2main" onclick="location.href='/AraQTL/';"> main menu </div>
<center>
<h3>Correlating eQTL profiles</h3>
......@@ -23,10 +19,22 @@ $(function(){
</center>
{% if experiment_name %}
<script>
var titletxt = "{{ queryGene.id }}{% if queryGene.name %}({{ queryGene.name }}){% endif %}";
var dot = {};
dot["label"] = "{{ queryGene.id }}";
dot["chr"] = {{ queryGene.chromosome }};
dot["pos"] = {{ queryGene.start }};
var multiexperiment = false;
var species = "{{ species }}";
var genes = [];
{% for gene in gene_list %}
genes.push({"gene": "{{ gene.gene_id }}", "experiment" : "{{ experiment_name }}"});
var show = 0
{% if forloop.counter < 10 %}
show = 1;
{% endif %}
genes.push({ "gene": "{{ gene.gene_id }}",
"show": show,
"experiment" : "{{ experiment_name }}"});
{% endfor %}
</script>
......@@ -37,7 +45,7 @@ genes.push({"gene": "{{ gene.gene_id }}", "experiment" : "{{ experiment_name }}"
<option value= "{{ experiment }}" {%if experiment == experiment_name %} selected {% endif %}>{{ experiment }}</option>
{% endfor %}
</select>
Gene :<input type="text" id="gene_name" title ="select a gene" name="gene_name" value ="{{ gene_name }}"/>
Gene :<input type="text" id="gene_id" title ="select a gene" name="gene_id" value ="{{ queryGene.id }}"/>
Correlation threshold:<input type="text" id="corrthld" title ="default correlation threshold is 0.9" name="corrthld" value="{{ corrthld }}"/>
<input type="submit" id="correlation_submit" class="btn_analysis" value="Show correlating genes" />
......@@ -47,17 +55,51 @@ genes.push({"gene": "{{ gene.gene_id }}", "experiment" : "{{ experiment_name }}"
<div id="container" align="center">
<p>
<table cellspacing='0'>
<tr><th>Transcript&nbsp;ID (count={{ gene_list|length }})</th><th>Gene Name</th><th>Pearson's Correlation Coefficient</th><th>Description</th></tr>
<tr>
<th><input type="checkbox" name="allgenes_checkbox" value="all"></th>
<th>Gene&nbsp;ID (count={{ gene_list|length }})</th>
<th>Gene Name</th>
<th>Pearson's Correlation Coefficient</th>
<th>Description</th>
</tr>
{% for gene in gene_list %}
<tr><td><A href="/AraQTL/multiexperimentplot/?gene_name={{ gene.gene_id|upper }}" TARGET="_blank">{{ gene.gene_id }}</a></td>
<tr>
<td><input type="checkbox" name="gene_checkbox" value="{{ forloop.counter0 }}"{% if forloop.counter < 10 %} checked{% endif %}></td>
<td><A href="/AraQTL/multiexperimentplot/?gene_name={{ gene.gene_id|upper }}" TARGET="_blank">{{ gene.gene_id }}</a></td>
<td>{{ gene.gene_name }}</td>
<td>{{ gene.correlation|floatformat:2 }}</td>
<td align=left>{{ gene.description }}</td></tr>
{% endfor %}
</table>
</div>
<script type="text/javascript" src="{% static 'js/multiplot.js' %}"></script>
<script>
draw_qtl_plot();
function readCheckboxes() {
var checkedValues = $('input[name="gene_checkbox"]:checked').map(function() {
return this.value;
}).get();
var genesLength = genes.length;
for (var i = 0; i < genesLength; i++) {
genes[i].show = 0;
}
var checkedValuesLength = checkedValues.length
for (var j = 0; j < checkedValuesLength; j++) {
genes[checkedValues[j]].show = 1;
}
draw_qtl_plot();
}
$('input[name="allgenes_checkbox"]').click(function() {
if ($('input[name="allgenes_checkbox"]').is(':checked')) {
$('input[name="gene_checkbox"]').prop("checked", true);
} else {
$('input[name="gene_checkbox"]').prop("checked", false);
}
readCheckboxes();
});
$('input[name="gene_checkbox"]').click(function() {
readCheckboxes();
});
</script>
{% else %}
<form action="" method="get">
Experiment:<select id="experiment_name" name="experiment_name">
......@@ -65,12 +107,11 @@ genes.push({"gene": "{{ gene.gene_id }}", "experiment" : "{{ experiment_name }}"
<option value= "{{ experiment }}">{{ experiment }}</option>
{% endfor %}
</select>
Gene :<input type="text" id="gene_name" title ="select a gene" name="gene_name" value="AT1G35614"/>
Gene :<input type="text" id="gene_id" title ="select a gene" name="gene_id" value="AT1G35614"/>
Correlation threshold:<input type="text" id="corrthld" title ="default correlation threshold is 0.9" name="corrthld" placeholder="0.9"/>
<input type="submit" id="correlation_submit" class="btn_analysis" value="Show correlating genes" />
</form>
<script type="text/javascript" src="{% static 'js/multiplot.js' %}"></script>
{% endif %}
{% endblock %}
......
import json
import os
from django.shortcuts import render,render_to_response,HttpResponse
from django.shortcuts import render, render_to_response, HttpResponse
from django.conf import settings
from main.models import Experiment, GeneInfo, Species
......@@ -10,6 +9,7 @@ import numpy
import re
import json
# Create your views here.
class GeneInfoCorrelation:
......@@ -18,142 +18,152 @@ class GeneInfoCorrelation:
gene_name = ""
correlation = 0
def correlation(request):
'''
select a gene and experiment, and find the top genes that show correlation
'''
species_name = "Arabidopsis thaliana"
experiments = Experiment.objects.filter(species__species_name = species_name).values_list('experiment_name',flat=True)