Commit 863dc1d1 authored by Nijveen, Harm's avatar Nijveen, Harm
Browse files

first commit

parents
"""AraQTL URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('main.urls')),
url(r'^main/', include('main.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^cistrans/', include('cistrans.urls')),
url(r'^coregulation/', include('coregulation.urls')),
url(r'^correlation/', include('correlation.urls')),
url(r'^multiexperimentplot/', include('multiexperimentplot.urls')),
url(r'^multiplot/', include('multiplot.urls')),
]
"""
WSGI config for QTL project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""
import os
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "AraQTL.settings")
os.environ["DJANGO_SETTINGS_MODULE"] = "AraQTL.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css"/>
<link rel="stylesheet" href="{% static 'css/jquery-ui.min.css' %}" type="text/css"/>
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}"></script>
<title>eQTL visualization and investigation</title>
</head>
<body>
<header>
<div>
<!--<div id="logo" style="width:226px;height:35px;float:left;"><img src="{% static 'cistrans/image/wur.png' %}" style="max-width:100%;max-height:100%;"></div>-->
</div>
</header> <!-- /#banner -->
{% block content %} {% endblock %}
</body>
</html>
This diff is collapsed.
'''
Created on Apr 29, 2015
@author: jiao
'''
from django.conf.urls import patterns, include, url
from cistrans.views import eQTLPlotView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
url(r'^', eQTLPlotView),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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.conf import settings
from main.models import ArraySpot,Experiment,Transcript,Marker
from main.parser import outputJSON
# Create your views here.
def eQTLPlotView(request):
'''
plot eQTL maping
'''
experiments = Experiment.objects.all().values_list('experiment_name',flat=True)
if request.method=='GET':
if request.GET.get('experiment_name'):
exp_name = request.GET.get('experiment_name')
probe_out_path = os.path.join(settings.MEDIA_ROOT,'data','%s' % exp_name,'probe')
#if not os.path.exists(probe_out_path):
# try:
# os.makedirs(probe_out_path)
# except OSError as e:
# print e.errno
# 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>')
else:
lodthld = Experiment.objects.get(experiment_name = exp_name).lodthld
return render_to_response('eQTL.html',{'experiment_name': exp_name,
'experiments':experiments,
'lodthld':lodthld})
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.
@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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css"/>
<link rel="stylesheet" href="{% static 'css/jquery-ui.min.css' %}" type="text/css"/>
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}"></script>
<title>eQTL visualization and investigation</title>
</head>
<body>
<header>
<div>
<!--<div id="logo" style="width:226px;height:35px;float:left;"><img src="{% static 'cistrans/image/wur.png' %}" style="max-width:100%;max-height:100%;"></div>-->
</div>
</header> <!-- /#banner -->
<div id ="nav">
<div class = "topbar">
<div class = "topbar-wrapper">
<div class = "search-container">
</div>
</div>
</div>
</div>
{% block content %} {% endblock %}
</body>
</html>
{% extends 'base.html' %}
{% block content %}
{% load staticfiles %}
<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 src="http://d3js.org/queue.v1.min.js"></script>
<script>
$(function(){
$(document).tooltip();
});
</script>
<div class="back2main" onclick="location.href='/AraQTL/';"> main menu </div>
<center>
<h3>Shared peaks</h3>
<i>Show genes that have a LOD score at the selected marker above the threshold</i>
</center>
{% if experiment_name %}
<form action="" method="get">
Experiment name:
<select id="experiment_name" name="experiment_name">
{% for experiment in experiments %}
<option value= "{{ experiment }}" {%if experiment == experiment_name %} selected {% endif %}>{{ experiment }}</option>
{% endfor %}
</select>
Marker :<input type="text" id="marker" title ="select a marker" name="marker" value ="{{ marker }}"/>
LOD threshold:<input type="text" id="thld" title ="default adjusted LOD threshold using FDR control at 0.05 " name="thld" value="{{ lodthld }} "/>
<input type="submit" id="coregulation_submit" class="btn_analysis" value="Show genes" />
</form>
<div id ="multiexperimentplot"></div>
<div id="container" align="center">
<p>
<table cellspacing='0'>
<tr><th>Transcript&nbsp;ID (count={{ gene_list|length }})</th><th>LOD score</th><th>Description</th></tr>
{% 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>{{ gene.description }}</td></tr>
{% endfor %}
</table>
</div>
<script type="text/javascript">
(function() {
var draw;
draw = function(data) {
var altpink, axislabels, bigRad, bottom, c, checkerboard, chrGap, chrLowXScale, chrXScale, chrYScale, chrindex,
ci, cj, cur, darkGray, darkblue, darkgreen, darkred, draw_probe, g, gene, h, hLegend, i, j, labelcolor, legend,
left, leftLegend, lightGray, m, maincolor, martip, maxlod, newg, nodig, onedig, p, pad, pink, probe, purple,
right, rightLegend, svg, tickHeight, titlecolor, top, topLegend, totalChrLength, totalh, totalw, twodig,
w, wLegend, xloc, yloc, _i, _j, _k, _l,
_len, _len1, _len10, _len2, _len3, _len4, _len5, _len6, _len7, _len8, _len9,
_m, _n, _o, _p, _q, _r, _ref, _ref1, _ref10, _ref4, _ref5, _ref8, _ref9, _s;
totalw = 1000;
totalh = 400;
chrGap = 8;
bigRad = 8;
pad = {
left: 60,
top: 40,
right: 40,
bottom: 40,
inner: 10
};
left = 60;
right = 760;
leftLegend = right + 10;
top = 41;
topLegend = 41;
bottom = 341;
h = 300;
hLegend = 200;
w = 700;
wLegend = 300;
//NUMBER FORMATING//////////////////
nodig = d3.format(".0f");
onedig = d3.format(".1f");
twodig = d3.format(".2f");
//END///////////////////////////////
//SET DEFAULT COLOR/////////////////
lightGray = d3.rgb(230, 230, 230);
darkGray = d3.rgb(200, 200, 200);
darkblue = "darkslateblue";
darkgreen = "darkslateblue";
pink = "hotpink";
altpink = "#E9CFEC";
purple = "#8C4374";
darkred = "crimson";
labelcolor = "black";
titlecolor = "blue";
maincolor = "darkblue";
//END///////////////////////////////
svg = d3.select("div#multiexperimentplot").append("svg").attr("width", totalw).attr("height", totalh);
svg.append("rect").attr("x", left).attr("y", top).attr("height", h).attr("width", w).attr("class", "innerBox");
svg.append("rect").attr("x", leftLegend).attr("y", topLegend).attr("height", hLegend).attr("width", wLegend).attr("class", "innerBox");
//#######################end 1###################################################
//###########################2.define chessboard layout##########################
checkerboard = svg.append("g").attr("id", "checkerboard");// define plot as checkerboard
legend = svg.append("g").attr("id", "legend");// define legend
//########## Calculate total chromosome length (in bp) SUM #########
totalChrLength = 0;
_ref = data.chrnames; //{"chr" : {"1": {"start": 3631,"end": 30425192}...}
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
//chromosome length in cM
data.chr[c].length_bp = data.chr[c].end - data.chr[c].start;
totalChrLength += data.chr[c].length_bp;
}
//#################################end###############################
//################################lower plot chess board box property############################
chrLowXScale = {};
cur = Math.round(pad.left + chrGap / 2); // (60+8/2)
_ref4 = data.chrnames;
for (_k = 0, _len4 = _ref4.length; _k < _len4; _k++) {
c = _ref4[_k];
data.chr[c].start_lowerXpixel = cur; // 64
// 64 + round(900-8*nr_chr/total_chr_length*chr_cM)
data.chr[c].end_lowerXpixel = cur + Math.round((w - chrGap * data.chrnames.length) / totalChrLength * data.chr[c].length_bp);
chrLowXScale[c] = d3.scale.linear().domain([data.chr[c].start, data.chr[c].end]).range([data.chr[c].start_lowerXpixel, data.chr[c].end_lowerXpixel]);
cur = data.chr[c].end_lowerXpixel + chrGap; // each loop cursor move to the end position +2
}
//#####################################end#######################################
//###################draw chess board layout for lower plot#################
_ref5 = data.chrnames;
for (i = _p = 0, _len5 = _ref5.length; _p < _len5; i = ++_p) {
ci = _ref5[i];
if (i % 2 === 0) {
checkerboard.append("rect").
attr("x", data.chr[ci].start_lowerXpixel - chrGap / 2).
attr("width", data.chr[ci].end_lowerXpixel - data.chr[ci].start_lowerXpixel + chrGap).
attr("y", top).
attr("height", h).
attr("stroke", "none").
attr("fill", darkGray).
style("pointer-events", "none");
}
}
//###################################end#########################################
//#########################draw axis of plot###########################
//################################### X axis #######################################
axislabels = svg.append("g").attr("id", "axislabels").style("pointer-events", "none");
//################################### END #######################################
//################################### Y axis #######################################
//################# x axis text of plot################
axislabels.append("g").attr("id", "bottomX").selectAll("empty").data(data.chrnames).enter().append("text").text(function(d) {
return d;
}).attr("x", function(d) {
return (data.chr[d].start_lowerXpixel + data.chr[d].end_lowerXpixel) / 2;
}).attr("y", bottom + pad.bottom * 0.3).attr("fill", labelcolor);
//################################### END #######################################
//############################### add x-axis name #######################################
axislabels.append("text").text("Marker position").
attr("x", (left + right) / 2).
attr("y", bottom + pad.bottom * 0.75).
attr("fill", titlecolor).
attr("text-anchor", "middle");
//############################### add Y-axis name #######################################
xloc = left - pad.left * 0.65;