From bd6d36ddb366a3ee427a0c237ebf8f672606ff19 Mon Sep 17 00:00:00 2001
From: zsloan
Date: Mon, 9 Apr 2018 18:13:59 +0000
Subject: Fixed issue that caused and error for certain types of mapping due to
variable not being initialized
Removed a bunch of unused statistical functions from Plot.py and webqtlUtil.py. I figure that if we ever need them again, there's probably some third party library that includes them.
Also removed unused file AJAX_table.py and other unused code from several other files
---
wqflask/utility/webqtlUtil.py | 132 +-----------------------------------------
1 file changed, 1 insertion(+), 131 deletions(-)
(limited to 'wqflask/utility/webqtlUtil.py')
diff --git a/wqflask/utility/webqtlUtil.py b/wqflask/utility/webqtlUtil.py
index 4fc978f5..2336e722 100644
--- a/wqflask/utility/webqtlUtil.py
+++ b/wqflask/utility/webqtlUtil.py
@@ -90,70 +90,6 @@ PROGRESSBAR = HT.Image('/images/waitAnima2.gif', alt='checkblue',align="middle",
# Accessory Functions
#########################################
-def inverseCumul(p):
- #Coefficients in rational approximations.
- a = [-3.969683028665376e+01,2.209460984245205e+02,-2.759285104469687e+02,1.383577518672690e+02,-3.066479806614716e+01,2.506628277459239e+00]
-
- b = [-5.447609879822406e+01,1.615858368580409e+02,-1.556989798598866e+02,6.680131188771972e+01,-1.328068155288572e+01]
-
- c = [-7.784894002430293e-03,-3.223964580411365e-01,-2.400758277161838e+00,-2.549732539343734e+00,4.374664141464968e+00,2.938163982698783e+00]
-
- d = [7.784695709041462e-03,3.224671290700398e-01,2.445134137142996e+00,3.754408661907416e+00]
-
- #Define break-points.
-
- p_low = 0.02425
- p_high = 1 - p_low
-
- #Rational approximation for lower region.
-
- if p > 0 and p < p_low:
- q = sqrt(-2*log(p))
- x = (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) / ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1)
-
-
- #Rational approximation for central region.
-
- elif p>= p_low and p <= p_high:
- q = p - 0.5
- r = q*q
- x = (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /(((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1)
-
- #Rational approximation for upper region.
-
- elif p>p_high and p < 1:
- q = sqrt(-2*log(1-p))
- x = -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1)
-
- else:
- return None
-
- if p>0 and p < 1:
- e = 0.5 * erfcc(-x/sqrt(2)) - p
- u = e * sqrt(2*pi) * exp(x*x/2)
- x = x - u/(1 + x*u/2)
- return x
- else:
- return None
-
-def erfcc(x):
- z=abs(x)
- t=1.0/(1.0+0.5*z)
- ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+t*(-0.82215223+t*0.17087277)))))))))
- if x>=0.0:
- return ans
- else:
- return 2.0-ans
-
-def U(n):
- x=pow(0.5,1.0/n)
- m=[1-x]
- for i in range(2,n):
- a=(i-0.3175)/(n+0.365)
- m.append(a)
- m.append(x)
- return m
-
def decodeEscape(str):
a = str
pattern = re.compile('(%[0-9A-Fa-f][0-9A-Fa-f])')
@@ -952,72 +888,6 @@ def formatField(name):
#name = name.replace("Mb Mm6", "Mb");
return name.replace("Id", "ID")
-#XZ, 03/27/2009: This function is very specific.
-#It is used by AJAX_table.py, correlationPage.py and dataPage.py
-
-
-def genTableObj(tblobj=None, file="", sortby = ("", ""), tableID = "sortable", addIndex = "1", hiddenColumns=[]):
- header = tblobj['header']
- body = tblobj['body']
- field, order = sortby
-
- #ZAS 9/12/2011 - The hiddenColumns array needs to be converted into a string so they can be placed into the javascript of each up/down button
- hiddenColumnsString = ",".join(hiddenColumns)
-
- tbl = HT.TableLite(Class="collap b2", cellspacing=1, cellpadding=5)
-
- hiddenColumnIdx = [] #indices of columns to hide
- idx = -1
- last_idx = 0 #ZS: This is the index of the last item in the regular table header (without any extra parameters). It is used to determine the index of each extra parameter.
- for row in header:
- hr = HT.TR()
- for i, item in enumerate(row):
- if (item.text == '') or (item.text not in hiddenColumns):
- if item.sort and item.text:
- down = HT.Href("javascript:xmlhttpPost('%smain.py?FormID=AJAX_table', '%s', 'sort=%s&order=down&file=%s&tableID=%s&addIndex=%s&hiddenColumns=%s')" % (webqtlConfig.CGIDIR, tableID, item.text, file, tableID, addIndex, hiddenColumnsString),IMGDESC)
- up = HT.Href("javascript:xmlhttpPost('%smain.py?FormID=AJAX_table', '%s', 'sort=%s&order=up&file=%s&tableID=%s&addIndex=%s&hiddenColumns=%s')" % (webqtlConfig.CGIDIR, tableID, item.text, file, tableID, addIndex, hiddenColumnsString),IMGASC)
- if item.text == field:
- idx = item.idx
- last_idx = idx
- if order == 'up':
- up = IMGASCON
- elif order == 'down':
- down = IMGDESCON
- item.html.append(HT.Div(up, down, style="float: bottom;"))
- hr.append(item.html)
- else:
- hiddenColumnIdx.append(i)
- tbl.append(hr)
-
- for i, row in enumerate(body):
- for j, item in enumerate(row):
- if order == 'down':
- if (item.val == '' or item.val == 'x' or item.val == 'None'):
- item.val = 0
- if order == 'up':
- if (item.val == '' or item.val == 'x' or item.val == 'None'):
- item.val = 'zzzzz'
-
- if idx >= 0:
- if order == 'down':
- body.sort(lambda A, B: cmp(B[idx].val, A[idx].val), key=natsort_key)
- elif order == 'up':
- body.sort(lambda A, B: cmp(A[idx].val, B[idx].val), key=natsort_key)
- else:
- pass
-
- for i, row in enumerate(body):
- hr = HT.TR(Id = row[0].text)
- for j, item in enumerate(row):
- if (j not in hiddenColumnIdx):
- if j == 0:
- if addIndex == "1":
- item.html.contents = [i+1] + item.html.contents
- hr.append(item.html)
- tbl.append(hr)
-
- return tbl
-
def natsort_key(string):
r = []
for c in string:
@@ -1027,4 +897,4 @@ def natsort_key(string):
except: r.append(c)
except:
r.append(c)
- return r
+ return r
\ No newline at end of file
--
cgit v1.2.3
From 23ef6a91c1dce4c9258dfb2bc24ee7e0004f509d Mon Sep 17 00:00:00 2001
From: zsloan
Date: Mon, 9 Apr 2018 23:07:33 +0000
Subject: Removed svg.py, which I know isn't used, and webqtlFormData, which
I'm pretty sure also isn't used any more.
Also removed some other unused code from the files listed
---
wqflask/base/webqtlFormData.py | 257 -----
wqflask/utility/Plot.py | 1 -
wqflask/utility/svg.py | 1068 --------------------
wqflask/utility/webqtlUtil.py | 88 --
wqflask/wqflask/correlation/show_corr_results.py | 52 -
.../marker_regression/marker_regression_gn1.py | 16 -
wqflask/wqflask/views.py | 80 +-
7 files changed, 22 insertions(+), 1540 deletions(-)
delete mode 100644 wqflask/base/webqtlFormData.py
delete mode 100644 wqflask/utility/svg.py
(limited to 'wqflask/utility/webqtlUtil.py')
diff --git a/wqflask/base/webqtlFormData.py b/wqflask/base/webqtlFormData.py
deleted file mode 100644
index 1b41b2fc..00000000
--- a/wqflask/base/webqtlFormData.py
+++ /dev/null
@@ -1,257 +0,0 @@
-# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
-#
-# This program is free software: you can redistribute it and/or modify it
-# under the terms of the GNU Affero General Public License
-# as published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-# See the GNU Affero General Public License for more details.
-#
-# This program is available from Source Forge: at GeneNetwork Project
-# (sourceforge.net/projects/genenetwork/).
-#
-# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
-# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
-#
-#
-#
-# This module is used by GeneNetwork project (www.genenetwork.org)
-#
-# Created by GeneNetwork Core Team 2010/08/10
-#
-# Last updated by GeneNetwork Core Team 2010/10/20
-
-from __future__ import print_function
-from pprint import pformat as pf
-
-import string
-import os
-
-import reaper
-
-import webqtlConfig
-from webqtlCaseData import webqtlCaseData
-from utility import webqtlUtil
-
-class webqtlFormData(object):
- 'Represents data from a WebQTL form page, needed to generate the next page'
-
- attrs = ('formID','group','genotype','samplelist','allsamplelist', 'display_variance'
- 'suggestive','significance','submitID','identification', 'enablevariance',
- 'nperm','nboot','email','incparentsf1','genotype_1','genotype_2','traitInfo')
-
- #XZ: Attention! All attribute values must be picklable!
-
- def __init__(self,
- start_vars = None,
- req = None):
- # Todo: rework this whole thing
- for item in webqtlFormData.attrs:
- self.__dict__[item] = None
-
- for item in start_vars:
- self.__dict__[item] = start_vars[item]
-
- #Todo: This can't be good below...rework
- try:
- self.remote_ip = req.connection.remote_ip
- except:
- self.remote_ip = '1.2.3.4'
-
- self.ppolar = None
- self.mpolar = None
-
- if self.group:
- _f1, _f12, self.mpolar, self.ppolar = webqtlUtil.ParInfo[self.group]
-
- def set_number(stringy):
- return int(stringy) if stringy else 2000 # Rob asked to change the default value to 2000
-
- self.nperm = set_number(self.nperm)
- self.nboot = set_number(self.nboot)
-
- if self.allsamplelist:
- self.allsamplelist = self.allsamplelist.split()
-
- if self.group == 'BXD300':
- self.group = 'BXD'
-
- def __getitem__(self, key):
- return self.__dict__[key]
-
- def get(self, key, default=None):
- if key in self.__dict__:
- return self.__dict__[key]
- else:
- return default
-
- def __str__(self):
- rstr = ''
- for item in self.attrs:
- if item != 'genotype':
- rstr += '%s:%s\n' % (item,str(getattr(self,item)))
- return rstr
-
-
- def readGenotype(self):
- '''read genotype from .geno file'''
- if self.group == 'BXD300':
- self.group = 'BXD'
-
- assert self.group, "self.group needs to be set"
-
- #genotype_1 is Dataset Object without parents and f1
- #genotype_2 is Dataset Object with parents and f1 (not for intercross)
-
- self.genotype_1 = reaper.Dataset()
-
- full_filename = locate(self.group + '.geno','genotype')
-
- # reaper barfs on unicode filenames, so here we ensure it's a string
- full_filename = str(full_filename)
- self.genotype_1.read(full_filename)
-
- print("Got to after read")
-
- try:
- # NL, 07/27/2010. ParInfo has been moved from webqtlForm.py to webqtlUtil.py;
- _f1, _f12, _mat, _pat = webqtlUtil.ParInfo[self.group]
- except KeyError:
- _f1 = _f12 = _mat = _pat = None
-
- self.genotype_2 = self.genotype_1
- if self.genotype_1.type == "group" and _mat and _pat:
- self.genotype_2 = self.genotype_1.add(Mat=_mat, Pat=_pat) #, F1=_f1)
-
- #determine default genotype object
- if self.incparentsf1 and self.genotype_1.type != "intercross":
- self.genotype = self.genotype_2
- else:
- self.incparentsf1 = 0
- self.genotype = self.genotype_1
-
- self.samplelist = list(self.genotype.prgy)
- self.f1list = []
- self.parlist = []
-
- if _f1 and _f12:
- self.f1list = [_f1, _f12]
- if _mat and _pat:
- self.parlist = [_mat, _pat]
-
-
- def readData(self, samplelist, incf1=None):
- '''read user input data or from trait data and analysis form'''
-
- if incf1 == None:
- incf1 = []
-
- if not self.genotype:
- self.readGenotype()
- if not samplelist:
- if incf1:
- samplelist = self.f1list + self.samplelist
- else:
- samplelist = self.samplelist
-
- traitfiledata = getattr(self, "traitfile", None)
- traitpastedata = getattr(self, "traitpaste", None)
- variancefiledata = getattr(self, "variancefile", None)
- variancepastedata = getattr(self, "variancepaste", None)
- Nfiledata = getattr(self, "Nfile", None)
-
- #### Todo: Rewrite below when we get to someone submitting their own trait #####
-
- def to_float(item):
- try:
- return float(item)
- except ValueError:
- return None
-
- print("bottle samplelist is:", samplelist)
- if traitfiledata:
- tt = traitfiledata.split()
- values = map(webqtlUtil.StringAsFloat, tt)
- elif traitpastedata:
- tt = traitpastedata.split()
- values = map(webqtlUtil.StringAsFloat, tt)
- else:
- print("mapping formdataasfloat")
- #values = map(self.FormDataAsFloat, samplelist)
- values = [to_float(getattr(self, key)) for key in samplelist]
-
-
- if len(values) < len(samplelist):
- values += [None] * (len(samplelist) - len(values))
- elif len(values) > len(samplelist):
- values = values[:len(samplelist)]
-
- if variancefiledata:
- tt = variancefiledata.split()
- variances = map(webqtlUtil.StringAsFloat, tt)
- elif variancepastedata:
- tt = variancepastedata.split()
- variances = map(webqtlUtil.StringAsFloat, tt)
- else:
- variances = map(self.FormVarianceAsFloat, samplelist)
-
- if len(variances) < len(samplelist):
- variances += [None]*(len(samplelist) - len(variances))
- elif len(variances) > len(samplelist):
- variances = variances[:len(samplelist)]
-
- if Nfiledata:
- tt = string.split(Nfiledata)
- nsamples = map(webqtlUtil.IntAsFloat, tt)
- if len(nsamples) < len(samplelist):
- nsamples += [None]*(len(samplelist) - len(nsamples))
- else:
- nsamples = map(self.FormNAsFloat, samplelist)
-
- ##values, variances, nsamples is obsolete
- self.allTraitData = {}
- for i, _sample in enumerate(samplelist):
- if values[i] != None:
- self.allTraitData[_sample] = webqtlCaseData(
- _sample, values[i], variances[i], nsamples[i])
-
- def informativeStrains(self, samplelist=None, include_variances = None):
- '''if readData was called, use this to output informative samples (sample with values)'''
-
- if not samplelist:
- samplelist = self.samplelist
-
- samples = []
- values = []
- variances = []
-
- for sample in samplelist:
- if sample in self.allTraitData:
- _val, _var = self.allTraitData[sample].value, self.allTraitData[sample].variance
- if _val != None:
- if include_variances:
- if _var != None:
- samples.append(sample)
- values.append(_val)
- variances.append(_var)
- else:
- samples.append(sample)
- values.append(_val)
- variances.append(None)
-
- return samples, values, variances, len(samples)
-
- def FormVarianceAsFloat(self, key):
- try:
- return float(self.formdata.getfirst('V' + key))
- except:
- return None
-
- def FormNAsFloat(self, key):
- try:
- return int(self.formdata.getfirst('N' + key))
- except:
- return None
\ No newline at end of file
diff --git a/wqflask/utility/Plot.py b/wqflask/utility/Plot.py
index dbcee331..529cd117 100644
--- a/wqflask/utility/Plot.py
+++ b/wqflask/utility/Plot.py
@@ -37,7 +37,6 @@ from numarray import ones, array, dot, swapaxes
import reaper
-import svg
import webqtlUtil
import corestats
from base import webqtlConfig
diff --git a/wqflask/utility/svg.py b/wqflask/utility/svg.py
deleted file mode 100644
index 512bc9e6..00000000
--- a/wqflask/utility/svg.py
+++ /dev/null
@@ -1,1068 +0,0 @@
-# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
-#
-# This program is free software: you can redistribute it and/or modify it
-# under the terms of the GNU Affero General Public License
-# as published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-# See the GNU Affero General Public License for more details.
-#
-# This program is available from Source Forge: at GeneNetwork Project
-# (sourceforge.net/projects/genenetwork/).
-#
-# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
-# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
-#
-#
-#
-# This module is used by GeneNetwork project (www.genenetwork.org)
-#
-# Created by GeneNetwork Core Team 2010/08/10
-#
-# Last updated by GeneNetwork Core Team 2010/10/20
-
-#!/usr/bin/env python
-##Copyright (c) 2002, Fedor Baart & Hans de Wit (Stichting Farmaceutische Kengetallen)
-##All rights reserved.
-##
-##Redistribution and use in source and binary forms, with or without modification,
-##are permitted provided that the following conditions are met:
-##
-##Redistributions of source code must retain the above copyright notice, this
-##list of conditions and the following disclaimer.
-##
-##Redistributions in binary form must reproduce the above copyright notice,
-##this list of conditions and the following disclaimer in the documentation and/or
-##other materials provided with the distribution.
-##
-##Neither the name of the Stichting Farmaceutische Kengetallen nor the names of
-##its contributors may be used to endorse or promote products derived from this
-##software without specific prior written permission.
-##
-##THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-##AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-##IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-##DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
-##FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-##DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-##SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-##CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-##OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-##OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-##Thanks to Gerald Rosennfellner for his help and useful comments.
-
-__doc__="""Use SVGdraw to generate your SVGdrawings.
-
-SVGdraw uses an object model drawing and a method toXML to create SVG graphics
-by using easy to use classes and methods usualy you start by creating a drawing eg
-
- d=drawing()
- #then you create a SVG root element
- s=svg()
- #then you add some elements eg a circle and add it to the svg root element
- c=circle()
- #you can supply attributes by using named arguments.
- c=circle(fill='red',stroke='blue')
- #or by updating the attributes attribute:
- c.attributes['stroke-width']=1
- s.addElement(c)
- #then you add the svg root element to the drawing
- d.setSVG(s)
- #and finaly you xmlify the drawing
- d.toXml()
-
-
-this results in the svg source of the drawing, which consists of a circle
-on a white background. Its as easy as that;)
-This module was created using the SVG specification of www.w3c.org and the
-O'Reilly (www.oreilly.com) python books as information sources. A svg viewer
-is available from www.adobe.com"""
-
-__version__="1.0"
-
-# there are two possibilities to generate svg:
-# via a dom implementation and directly using text strings
-# the latter is way faster (and shorter in coding)
-# the former is only used in debugging svg programs
-# maybe it will be removed alltogether after a while
-# with the following variable you indicate whether to use the dom implementation
-# Note that PyXML is required for using the dom implementation.
-# It is also possible to use the standard minidom. But I didn't try that one.
-# Anyway the text based approach is about 60 times faster than using the full dom implementation.
-use_dom_implementation=0
-
-
-import exceptions
-if use_dom_implementation<>0:
- try:
- from xml.dom import implementation
- from xml.dom.ext import PrettyPrint
- except:
- raise exceptions.ImportError, "PyXML is required for using the dom implementation"
-#The implementation is used for the creating the XML document.
-#The prettyprint module is used for converting the xml document object to a xml file
-
-import sys
-assert sys.version_info[0]>=2
-if sys.version_info[1]<2:
- True=1
- False=0
- file=open
-
-sys.setrecursionlimit=50
-#The recursion limit is set conservative so mistakes like s=svg() s.addElement(s)
-#won't eat up too much processor time.
-
-#the following code is pasted form xml.sax.saxutils
-#it makes it possible to run the code without the xml sax package installed
-#To make it possible to have in your text elements, it is necessary to escape the texts
-def _escape(data, entities={}):
- """Escape &, <, and > in a string of data.
-
- You can escape other strings of data by passing a dictionary as
- the optional entities parameter. The keys and values must all be
- strings; each key will be replaced with its corresponding value.
- """
- #data = data.replace("&", "&")
- data = data.replace("<", "<")
- data = data.replace(">", ">")
- for chars, entity in entities.items():
- data = data.replace(chars, entity)
- return data
-
-def _quoteattr(data, entities={}):
- """Escape and quote an attribute value.
-
- Escape &, <, and > in a string of data, then quote it for use as
- an attribute value. The \" character will be escaped as well, if
- necessary.
-
- You can escape other strings of data by passing a dictionary as
- the optional entities parameter. The keys and values must all be
- strings; each key will be replaced with its corresponding value.
- """
- data = _escape(data, entities)
- if '"' in data:
- if "'" in data:
- data = '"%s"' % data.replace('"', """)
- else:
- data = "'%s'" % data
- else:
- data = '"%s"' % data
- return data
-
-
-
-def _xypointlist(a):
- """formats a list of xy pairs"""
- s=''
- for e in a: #this could be done more elegant
- s+=str(e)[1:-1] +' '
- return s
-
-def _viewboxlist(a):
- """formats a tuple"""
- s=''
- for e in a:
- s+=str(e)+' '
- return s
-
-def _pointlist(a):
- """formats a list of numbers"""
- return str(a)[1:-1]
-
-class pathdata:
- """class used to create a pathdata object which can be used for a path.
- although most methods are pretty straightforward it might be useful to look at the SVG specification."""
- #I didn't test the methods below.
- def __init__(self,x=None,y=None):
- self.path=[]
- if x is not None and y is not None:
- self.path.append('M '+str(x)+' '+str(y))
- def closepath(self):
- """ends the path"""
- self.path.append('z')
- def move(self,x,y):
- """move to absolute"""
- self.path.append('M '+str(x)+' '+str(y))
- def relmove(self,x,y):
- """move to relative"""
- self.path.append('m '+str(x)+' '+str(y))
- def line(self,x,y):
- """line to absolute"""
- self.path.append('L '+str(x)+' '+str(y))
- def relline(self,x,y):
- """line to relative"""
- self.path.append('l '+str(x)+' '+str(y))
- def hline(self,x):
- """horizontal line to absolute"""
- self.path.append('H'+str(x))
- def relhline(self,x):
- """horizontal line to relative"""
- self.path.append('h'+str(x))
- def vline(self,y):
- """verical line to absolute"""
- self.path.append('V'+str(y))
- def relvline(self,y):
- """vertical line to relative"""
- self.path.append('v'+str(y))
- def bezier(self,x1,y1,x2,y2,x,y):
- """bezier with xy1 and xy2 to xy absolut"""
- self.path.append('C'+str(x1)+','+str(y1)+' '+str(x2)+','+str(y2)+' '+str(x)+','+str(y))
- def relbezier(self,x1,y1,x2,y2,x,y):
- """bezier with xy1 and xy2 to xy relative"""
- self.path.append('c'+str(x1)+','+str(y1)+' '+str(x2)+','+str(y2)+' '+str(x)+','+str(y))
- def smbezier(self,x2,y2,x,y):
- """smooth bezier with xy2 to xy absolut"""
- self.path.append('S'+str(x2)+','+str(y2)+' '+str(x)+','+str(y))
- def relsmbezier(self,x2,y2,x,y):
- """smooth bezier with xy2 to xy relative"""
- self.path.append('s'+str(x2)+','+str(y2)+' '+str(x)+','+str(y))
- def qbezier(self,x1,y1,x,y):
- """quadratic bezier with xy1 to xy absolut"""
- self.path.append('Q'+str(x1)+','+str(y1)+' '+str(x)+','+str(y))
- def relqbezier(self,x1,y1,x,y):
- """quadratic bezier with xy1 to xy relative"""
- self.path.append('q'+str(x1)+','+str(y1)+' '+str(x)+','+str(y))
- def smqbezier(self,x,y):
- """smooth quadratic bezier to xy absolut"""
- self.path.append('T'+str(x)+','+str(y))
- def relsmqbezier(self,x,y):
- """smooth quadratic bezier to xy relative"""
- self.path.append('t'+str(x)+','+str(y))
- def ellarc(self,rx,ry,xrot,laf,sf,x,y):
- """elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy absolut"""
- self.path.append('A'+str(rx)+','+str(ry)+' '+str(xrot)+' '+str(laf)+' '+str(sf)+' '+str(x)+' '+str(y))
- def relellarc(self,rx,ry,xrot,laf,sf,x,y):
- """elliptival arc with rx and ry rotating with xrot using large-arc-flag and sweep-flag to xy relative"""
- self.path.append('a'+str(rx)+','+str(ry)+' '+str(xrot)+' '+str(laf)+' '+str(sf)+' '+str(x)+' '+str(y))
- def __repr__(self):
- return ' '.join(self.path)
-
-
-
-
-class SVGelement:
- """SVGelement(type,attributes,elements,text,namespace,**args)
- Creates a arbitrary svg element and is intended to be subclassed not used on its own.
- This element is the base of every svg element it defines a class which resembles
- a xml-element. The main advantage of this kind of implementation is that you don't
- have to create a toXML method for every different graph object. Every element
- consists of a type, attribute, optional subelements, optional text and an optional
- namespace. Note the elements==None, if elements = None:self.elements=[] construction.
- This is done because if you default to elements=[] every object has a reference
- to the same empty list."""
- def __init__(self,type='',attributes=None,elements=None,text='',namespace='',cdata=None, **args):
- self.type=type
- if attributes==None:
- self.attributes={}
- else:
- self.attributes=attributes
- if elements==None:
- self.elements=[]
- else:
- self.elements=elements
- self.text=text
- self.namespace=namespace
- self.cdata=cdata
- for arg in args.keys():
- arg2 = arg.replace("__", ":")
- arg2 = arg2.replace("_", "-")
- self.attributes[arg2]=args[arg]
- def addElement(self,SVGelement):
- """adds an element to a SVGelement
-
- SVGelement.addElement(SVGelement)
- """
- self.elements.append(SVGelement)
-
- def toXml(self,level,f):
- f.write('\t'*level)
- f.write('<'+self.type)
- for attkey in self.attributes.keys():
- f.write(' '+_escape(str(attkey))+'='+_quoteattr(str(self.attributes[attkey])))
- if self.namespace:
- f.write(' xmlns="'+ _escape(str(self.namespace))+'" xmlns:xlink="http://www.w3.org/1999/xlink"')
- if self.elements or self.text or self.cdata:
- f.write('>')
- if self.elements:
- f.write('\n')
- for element in self.elements:
- element.toXml(level+1,f)
- if self.cdata:
- f.write('\n'+'\t'*(level+1)+'\n')
- if self.text:
- if type(self.text)==type(''): #If the text is only text
- f.write(_escape(str(self.text)))
- else: #If the text is a spannedtext class
- f.write(str(self.text))
- if self.elements:
- f.write('\t'*level+''+self.type+'>\n')
- elif self.text:
- f.write(''+self.type+'>\n')
- elif self.cdata:
- f.write('\t'*level+''+self.type+'>\n')
- else:
- f.write('/>\n')
-
-class tspan(SVGelement):
- """ts=tspan(text='',**args)
-
- a tspan element can be used for applying formatting to a textsection
- usage:
- ts=tspan('this text is bold')
- ts.attributes['font-weight']='bold'
- st=spannedtext()
- st.addtspan(ts)
- t=text(3,5,st)
- """
- def __init__(self,text=None,**args):
- SVGelement.__init__(self,'tspan',**args)
- if self.text<>None:
- self.text=text
- def __repr__(self):
- s="'
- s+=self.text
- s+=''
- return s
-
-class tref(SVGelement):
- """tr=tref(link='',**args)
-
- a tref element can be used for referencing text by a link to its id.
- usage:
- tr=tref('#linktotext')
- st=spannedtext()
- st.addtref(tr)
- t=text(3,5,st)
- """
- def __init__(self,link,**args):
- SVGelement.__init__(self,'tref',{'xlink:href':link},**args)
- def __repr__(self):
- s="'
- return s
-
-class spannedtext:
- """st=spannedtext(textlist=[])
-
- a spannedtext can be used for text which consists of text, tspan's and tref's
- You can use it to add to a text element or path element. Don't add it directly
- to a svg or a group element.
- usage:
-
- ts=tspan('this text is bold')
- ts.attributes['font-weight']='bold'
- tr=tref('#linktotext')
- tr.attributes['fill']='red'
- st=spannedtext()
- st.addtspan(ts)
- st.addtref(tr)
- st.addtext('This text is not bold')
- t=text(3,5,st)
- """
- def __init__(self,textlist=None):
- if textlist==None:
- self.textlist=[]
- else:
- self.textlist=textlist
- def addtext(self,text=''):
- self.textlist.append(text)
- def addtspan(self,tspan):
- self.textlist.append(tspan)
- def addtref(self,tref):
- self.textlist.append(tref)
- def __repr__(self):
- s=""
- for element in self.textlist:
- s+=str(element)
- return s
-
-class rect(SVGelement):
- """r=rect(width,height,x,y,fill,stroke,stroke_width,**args)
-
- a rectangle is defined by a width and height and a xy pair
- """
- def __init__(self,x=None,y=None,width=None,height=None,fill=None,stroke=None,stroke_width=None,**args):
- if width==None or height==None:
- if width<>None:
- raise ValueError, 'height is required'
- if height<>None:
- raise ValueError, 'width is required'
- else:
- raise ValueError, 'both height and width are required'
- SVGelement.__init__(self,'rect',{'width':width,'height':height},**args)
- if x<>None:
- self.attributes['x']=x
- if y<>None:
- self.attributes['y']=y
- if fill<>None:
- self.attributes['fill']=fill
- if stroke<>None:
- self.attributes['stroke']=stroke
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
-
-class ellipse(SVGelement):
- """e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args)
-
- an ellipse is defined as a center and a x and y radius.
- """
- def __init__(self,cx=None,cy=None,rx=None,ry=None,fill=None,stroke=None,stroke_width=None,**args):
- if rx==None or ry== None:
- if rx<>None:
- raise ValueError, 'rx is required'
- if ry<>None:
- raise ValueError, 'ry is required'
- else:
- raise ValueError, 'both rx and ry are required'
- SVGelement.__init__(self,'ellipse',{'rx':rx,'ry':ry},**args)
- if cx<>None:
- self.attributes['cx']=cx
- if cy<>None:
- self.attributes['cy']=cy
- if fill<>None:
- self.attributes['fill']=fill
- if stroke<>None:
- self.attributes['stroke']=stroke
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
-
-
-class circle(SVGelement):
- """c=circle(x,y,radius,fill,stroke,stroke_width,**args)
-
- The circle creates an element using a x, y and radius values eg
- """
- def __init__(self,cx=None,cy=None,r=None,fill=None,stroke=None,stroke_width=None,**args):
- if r==None:
- raise ValueError, 'r is required'
- SVGelement.__init__(self,'circle',{'r':r},**args)
- if cx<>None:
- self.attributes['cx']=cx
- if cy<>None:
- self.attributes['cy']=cy
- if fill<>None:
- self.attributes['fill']=fill
- if stroke<>None:
- self.attributes['stroke']=stroke
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
-
-class point(circle):
- """p=point(x,y,color)
-
- A point is defined as a circle with a size 1 radius. It may be more efficient to use a
- very small rectangle if you use many points because a circle is difficult to render.
- """
- def __init__(self,x,y,fill='black',**args):
- circle.__init__(self,x,y,1,fill,**args)
-
-class line(SVGelement):
- """l=line(x1,y1,x2,y2,stroke,stroke_width,**args)
-
- A line is defined by a begin x,y pair and an end x,y pair
- """
- def __init__(self,x1=None,y1=None,x2=None,y2=None,stroke=None,stroke_width=None,**args):
- SVGelement.__init__(self,'line',**args)
- if x1<>None:
- self.attributes['x1']=x1
- if y1<>None:
- self.attributes['y1']=y1
- if x2<>None:
- self.attributes['x2']=x2
- if y2<>None:
- self.attributes['y2']=y2
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
- if stroke<>None:
- self.attributes['stroke']=stroke
-
-class polyline(SVGelement):
- """pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
-
- a polyline is defined by a list of xy pairs
- """
- def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
- SVGelement.__init__(self,'polyline',{'points':_xypointlist(points)},**args)
- if fill<>None:
- self.attributes['fill']=fill
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
- if stroke<>None:
- self.attributes['stroke']=stroke
-
-class polygon(SVGelement):
- """pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args)
-
- a polygon is defined by a list of xy pairs
- """
- def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args):
- SVGelement.__init__(self,'polygon',{'points':_xypointlist(points)},**args)
- if fill<>None:
- self.attributes['fill']=fill
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
- if stroke<>None:
- self.attributes['stroke']=stroke
-
-class path(SVGelement):
- """p=path(path,fill,stroke,stroke_width,**args)
-
- a path is defined by a path object and optional width, stroke and fillcolor
- """
- def __init__(self,pathdata,fill=None,stroke=None,stroke_width=None,id=None,**args):
- SVGelement.__init__(self,'path',{'d':str(pathdata)},**args)
- if stroke<>None:
- self.attributes['stroke']=stroke
- if fill<>None:
- self.attributes['fill']=fill
- if stroke_width<>None:
- self.attributes['stroke-width']=stroke_width
- if id<>None:
- self.attributes['id']=id
-
-
-class text(SVGelement):
- """t=text(x,y,text,font_size,font_family,**args)
-
- a text element can bge used for displaying text on the screen
- """
- def __init__(self,x=None,y=None,text=None,font_size=None,font_family=None,text_anchor=None,**args):
- SVGelement.__init__(self,'text',**args)
- if x<>None:
- self.attributes['x']=x
- if y<>None:
- self.attributes['y']=y
- if font_size<>None:
- self.attributes['font-size']=font_size
- if font_family<>None:
- self.attributes['font-family']=font_family
- if text<>None:
- self.text=text
- if text_anchor<>None:
- self.attributes['text-anchor']=text_anchor
-
-
-class textpath(SVGelement):
- """tp=textpath(text,link,**args)
-
- a textpath places a text on a path which is referenced by a link.
- """
- def __init__(self,link,text=None,**args):
- SVGelement.__init__(self,'textPath',{'xlink:href':link},**args)
- if text<>None:
- self.text=text
-
-class pattern(SVGelement):
- """p=pattern(x,y,width,height,patternUnits,**args)
-
- A pattern is used to fill or stroke an object using a pre-defined
- graphic object which can be replicated ("tiled") at fixed intervals
- in x and y to cover the areas to be painted.
- """
- def __init__(self,x=None,y=None,width=None,height=None,patternUnits=None,**args):
- SVGelement.__init__(self,'pattern',**args)
- if x<>None:
- self.attributes['x']=x
- if y<>None:
- self.attributes['y']=y
- if width<>None:
- self.attributes['width']=width
- if height<>None:
- self.attributes['height']=height
- if patternUnits<>None:
- self.attributes['patternUnits']=patternUnits
-
-class title(SVGelement):
- """t=title(text,**args)
-
- a title is a text element. The text is displayed in the title bar
- add at least one to the root svg element
- """
- def __init__(self,text=None,**args):
- SVGelement.__init__(self,'title',**args)
- if text<>None:
- self.text=text
-
-class description(SVGelement):
- """d=description(text,**args)
-
- a description can be added to any element and is used for a tooltip
- Add this element before adding other elements.
- """
- def __init__(self,text=None,**args):
- SVGelement.__init__(self,'desc',**args)
- if text<>None:
- self.text=text
-
-class lineargradient(SVGelement):
- """lg=lineargradient(x1,y1,x2,y2,id,**args)
-
- defines a lineargradient using two xy pairs.
- stop elements van be added to define the gradient colors.
- """
- def __init__(self,x1=None,y1=None,x2=None,y2=None,id=None,**args):
- SVGelement.__init__(self,'linearGradient',**args)
- if x1<>None:
- self.attributes['x1']=x1
- if y1<>None:
- self.attributes['y1']=y1
- if x2<>None:
- self.attributes['x2']=x2
- if y2<>None:
- self.attributes['y2']=y2
- if id<>None:
- self.attributes['id']=id
-
-class radialgradient(SVGelement):
- """rg=radialgradient(cx,cy,r,fx,fy,id,**args)
-
- defines a radial gradient using a outer circle which are defined by a cx,cy and r and by using a focalpoint.
- stop elements van be added to define the gradient colors.
- """
- def __init__(self,cx=None,cy=None,r=None,fx=None,fy=None,id=None,**args):
- SVGelement.__init__(self,'radialGradient',**args)
- if cx<>None:
- self.attributes['cx']=cx
- if cy<>None:
- self.attributes['cy']=cy
- if r<>None:
- self.attributes['r']=r
- if fx<>None:
- self.attributes['fx']=fx
- if fy<>None:
- self.attributes['fy']=fy
- if id<>None:
- self.attributes['id']=id
-
-class stop(SVGelement):
- """st=stop(offset,stop_color,**args)
-
- Puts a stop color at the specified radius
- """
- def __init__(self,offset,stop_color=None,**args):
- SVGelement.__init__(self,'stop',{'offset':offset},**args)
- if stop_color<>None:
- self.attributes['stop-color']=stop_color
-
-class style(SVGelement):
- """st=style(type,cdata=None,**args)
-
- Add a CDATA element to this element for defing in line stylesheets etc..
- """
- def __init__(self,type,cdata=None,**args):
- SVGelement.__init__(self,'style',{'type':type},cdata=cdata, **args)
-
-
-class image(SVGelement):
- """im=image(url,width,height,x,y,**args)
-
- adds an image to the drawing. Supported formats are .png, .jpg and .svg.
- """
- def __init__(self,url,x=None,y=None,width=None,height=None,**args):
- if width==None or height==None:
- if width<>None:
- raise ValueError, 'height is required'
- if height<>None:
- raise ValueError, 'width is required'
- else:
- raise ValueError, 'both height and width are required'
- SVGelement.__init__(self,'image',{'xlink:href':url,'width':width,'height':height},**args)
- if x<>None:
- self.attributes['x']=x
- if y<>None:
- self.attributes['y']=y
-
-class cursor(SVGelement):
- """c=cursor(url,**args)
-
- defines a custom cursor for a element or a drawing
- """
- def __init__(self,url,**args):
- SVGelement.__init__(self,'cursor',{'xlink:href':url},**args)
-
-
-class marker(SVGelement):
- """m=marker(id,viewbox,refX,refY,markerWidth,markerHeight,**args)
-
- defines a marker which can be used as an endpoint for a line or other pathtypes
- add an element to it which should be used as a marker.
- """
- def __init__(self,id=None,viewBox=None,refx=None,refy=None,markerWidth=None,markerHeight=None,**args):
- SVGelement.__init__(self,'marker',**args)
- if id<>None:
- self.attributes['id']=id
- if viewBox<>None:
- self.attributes['viewBox']=_viewboxlist(viewBox)
- if refx<>None:
- self.attributes['refX']=refx
- if refy<>None:
- self.attributes['refY']=refy
- if markerWidth<>None:
- self.attributes['markerWidth']=markerWidth
- if markerHeight<>None:
- self.attributes['markerHeight']=markerHeight
-
-class group(SVGelement):
- """g=group(id,**args)
-
- a group is defined by an id and is used to contain elements
- g.addElement(SVGelement)
- """
- def __init__(self,id=None,**args):
- SVGelement.__init__(self,'g',**args)
- if id<>None:
- self.attributes['id']=id
-
-class symbol(SVGelement):
- """sy=symbol(id,viewbox,**args)
-
- defines a symbol which can be used on different places in your graph using
- the use element. A symbol is not rendered but you can use 'use' elements to
- display it by referencing its id.
- sy.addElement(SVGelement)
- """
-
- def __init__(self,id=None,viewBox=None,**args):
- SVGelement.__init__(self,'symbol',**args)
- if id<>None:
- self.attributes['id']=id
- if viewBox<>None:
- self.attributes['viewBox']=_viewboxlist(viewBox)
-
-class defs(SVGelement):
- """d=defs(**args)
-
- container for defining elements
- """
- def __init__(self,**args):
- SVGelement.__init__(self,'defs',**args)
-
-class switch(SVGelement):
- """sw=switch(**args)
-
- Elements added to a switch element which are "switched" by the attributes
- requiredFeatures, requiredExtensions and systemLanguage.
- Refer to the SVG specification for details.
- """
- def __init__(self,**args):
- SVGelement.__init__(self,'switch',**args)
-
-
-class use(SVGelement):
- """u=use(link,x,y,width,height,**args)
-
- references a symbol by linking to its id and its position, height and width
- """
- def __init__(self,link,x=None,y=None,width=None,height=None,**args):
- SVGelement.__init__(self,'use',{'xlink:href':link},**args)
- if x<>None:
- self.attributes['x']=x
- if y<>None:
- self.attributes['y']=y
-
- if width<>None:
- self.attributes['width']=width
- if height<>None:
- self.attributes['height']=height
-
-
-class link(SVGelement):
- """a=link(url,**args)
-
- a link is defined by a hyperlink. add elements which have to be linked
- a.addElement(SVGelement)
- """
- def __init__(self,link='',**args):
- SVGelement.__init__(self,'a',{'xlink:href':link},**args)
-
-class view(SVGelement):
- """v=view(id,**args)
-
- a view can be used to create a view with different attributes"""
- def __init__(self,id=None,**args):
- SVGelement.__init__(self,'view',**args)
- if id<>None:
- self.attributes['id']=id
-
-class script(SVGelement):
- """sc=script(type,type,cdata,**args)
-
- adds a script element which contains CDATA to the SVG drawing
-
- """
- def __init__(self,type,cdata=None,**args):
- SVGelement.__init__(self,'script',{'type':type},cdata=cdata,**args)
-
-class animate(SVGelement):
- """an=animate(attribute,from,to,during,**args)
-
- animates an attribute.
- """
- def __init__(self,attribute,fr=None,to=None,dur=None,**args):
- SVGelement.__init__(self,'animate',{'attributeName':attribute},**args)
- if fr<>None:
- self.attributes['from']=fr
- if to<>None:
- self.attributes['to']=to
- if dur<>None:
- self.attributes['dur']=dur
-
-class animateMotion(SVGelement):
- """an=animateMotion(pathdata,dur,**args)
-
- animates a SVGelement over the given path in dur seconds
- """
- def __init__(self,pathdata,dur,**args):
- SVGelement.__init__(self,'animateMotion',**args)
- if pathdata<>None:
- self.attributes['path']=str(pathdata)
- if dur<>None:
- self.attributes['dur']=dur
-
-class animateTransform(SVGelement):
- """antr=animateTransform(type,from,to,dur,**args)
-
- transform an element from and to a value.
- """
- def __init__(self,type=None,fr=None,to=None,dur=None,**args):
- SVGelement.__init__(self,'animateTransform',{'attributeName':'transform'},**args)
- #As far as I know the attributeName is always transform
- if type<>None:
- self.attributes['type']=type
- if fr<>None:
- self.attributes['from']=fr
- if to<>None:
- self.attributes['to']=to
- if dur<>None:
- self.attributes['dur']=dur
-class animateColor(SVGelement):
- """ac=animateColor(attribute,type,from,to,dur,**args)
-
- Animates the color of a element
- """
- def __init__(self,attribute,type=None,fr=None,to=None,dur=None,**args):
- SVGelement.__init__(self,'animateColor',{'attributeName':attribute},**args)
- if type<>None:
- self.attributes['type']=type
- if fr<>None:
- self.attributes['from']=fr
- if to<>None:
- self.attributes['to']=to
- if dur<>None:
- self.attributes['dur']=dur
-class set(SVGelement):
- """st=set(attribute,to,during,**args)
-
- sets an attribute to a value for a
- """
- def __init__(self,attribute,to=None,dur=None,**args):
- SVGelement.__init__(self,'set',{'attributeName':attribute},**args)
- if to<>None:
- self.attributes['to']=to
- if dur<>None:
- self.attributes['dur']=dur
-
-
-
-class svg(SVGelement):
- """s=svg(viewbox,width,height,**args)
-
- a svg or element is the root of a drawing add all elements to a svg element.
- You can have different svg elements in one svg file
- s.addElement(SVGelement)
-
- eg
- d=drawing()
- s=svg((0,0,100,100),'100%','100%')
- c=circle(50,50,20)
- s.addElement(c)
- d.setSVG(s)
- d.toXml()
- """
- def __init__(self,viewBox=None, width=None, height=None,**args):
- SVGelement.__init__(self,'svg',**args)
- if viewBox<>None:
- self.attributes['viewBox']=_viewboxlist(viewBox)
- if width<>None:
- self.attributes['width']=width
- if height<>None:
- self.attributes['height']=height
- self.namespace="http://www.w3.org/2000/svg"
-
-class drawing:
- """d=drawing()
-
- this is the actual SVG document. It needs a svg element as a root.
- Use the addSVG method to set the svg to the root. Use the toXml method to write the SVG
- source to the screen or to a file
- d=drawing()
- d.addSVG(svg)
- d.toXml(optionalfilename)
- """
-
- def __init__(self, entity={}):
- self.svg=None
- self.entity = entity
- def setSVG(self,svg):
- self.svg=svg
- #Voeg een element toe aan de grafiek toe.
- if use_dom_implementation==0:
- def toXml(self, filename='',compress=False):
- import cStringIO
- xml=cStringIO.StringIO()
- xml.write("\n")
- xml.write("\n" % (item, self.entity[item]))
- xml.write("]")
- xml.write(">\n")
- self.svg.toXml(0,xml)
- if not filename:
- if compress:
- import gzip
- f=cStringIO.StringIO()
- zf=gzip.GzipFile(fileobj=f,mode='wb')
- zf.write(xml.getvalue())
- zf.close()
- f.seek(0)
- return f.read()
- else:
- return xml.getvalue()
- else:
- if filename[-4:]=='svgz':
- import gzip
- f=gzip.GzipFile(filename=filename,mode="wb", compresslevel=9)
- f.write(xml.getvalue())
- f.close()
- else:
- f=file(filename,'w')
- f.write(xml.getvalue())
- f.close()
-
- else:
- def toXml(self,filename='',compress=False):
- """drawing.toXml() ---->to the screen
- drawing.toXml(filename)---->to the file
- writes a svg drawing to the screen or to a file
- compresses if filename ends with svgz or if compress is true
- """
- doctype = implementation.createDocumentType('svg',"-//W3C//DTD SVG 1.0//EN""",'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd ')
-
- global root
- #root is defined global so it can be used by the appender. Its also possible to use it as an arugument but
- #that is a bit messy.
- root=implementation.createDocument(None,None,doctype)
- #Create the xml document.
- global appender
- def appender(element,elementroot):
- """This recursive function appends elements to an element and sets the attributes
- and type. It stops when alle elements have been appended"""
- if element.namespace:
- e=root.createElementNS(element.namespace,element.type)
- else:
- e=root.createElement(element.type)
- if element.text:
- textnode=root.createTextNode(element.text)
- e.appendChild(textnode)
- for attribute in element.attributes.keys(): #in element.attributes is supported from python 2.2
- e.setAttribute(attribute,str(element.attributes[attribute]))
- if element.elements:
- for el in element.elements:
- e=appender(el,e)
- elementroot.appendChild(e)
- return elementroot
- root=appender(self.svg,root)
- if not filename:
- import cStringIO
- xml=cStringIO.StringIO()
- PrettyPrint(root,xml)
- if compress:
- import gzip
- f=cStringIO.StringIO()
- zf=gzip.GzipFile(fileobj=f,mode='wb')
- zf.write(xml.getvalue())
- zf.close()
- f.seek(0)
- return f.read()
- else:
- return xml.getvalue()
- else:
- try:
- if filename[-4:]=='svgz':
- import gzip
- import cStringIO
- xml=cStringIO.StringIO()
- PrettyPrint(root,xml)
- f=gzip.GzipFile(filename=filename,mode='wb',compresslevel=9)
- f.write(xml.getvalue())
- f.close()
- else:
- f=open(filename,'w')
- PrettyPrint(root,f)
- f.close()
- except:
- print "Cannot write SVG file: " + filename
- def validate(self):
- try:
- import xml.parsers.xmlproc.xmlval
- except:
- raise exceptions.ImportError,'PyXml is required for validating SVG'
- svg=self.toXml()
- xv=xml.parsers.xmlproc.xmlval.XMLValidator()
- try:
- xv.feed(svg)
- except:
- raise "SVG is not well formed, see messages above"
- else:
- print "SVG well formed"
-if __name__=='__main__':
-
-
- d=drawing()
- s=svg((0,0,100,100))
- r=rect(-100,-100,300,300,'cyan')
- s.addElement(r)
-
- t=title('SVGdraw Demo')
- s.addElement(t)
- g=group('animations')
- e=ellipse(0,0,5,2)
- g.addElement(e)
- c=circle(0,0,1,'red')
- g.addElement(c)
- pd=pathdata(0,-10)
- for i in range(6):
- pd.relsmbezier(10,5,0,10)
- pd.relsmbezier(-10,5,0,10)
- an=animateMotion(pd,10)
- an.attributes['rotate']='auto-reverse'
- an.attributes['repeatCount']="indefinite"
- g.addElement(an)
- s.addElement(g)
- for i in range(20,120,20):
- u=use('#animations',i,0)
- s.addElement(u)
- for i in range(0,120,20):
- for j in range(5,105,10):
- c=circle(i,j,1,'red','black',.5)
- s.addElement(c)
- d.setSVG(s)
-
- print d.toXml()
diff --git a/wqflask/utility/webqtlUtil.py b/wqflask/utility/webqtlUtil.py
index 2336e722..94dd7cbf 100644
--- a/wqflask/utility/webqtlUtil.py
+++ b/wqflask/utility/webqtlUtil.py
@@ -34,9 +34,6 @@ from htmlgen import HTMLgen2 as HT
from base import webqtlConfig
-
-
-
# NL, 07/27/2010. moved from webqtlForm.py
#Dict of Parents and F1 information, In the order of [F1, Mat, Pat]
ParInfo ={
@@ -64,43 +61,10 @@ ParInfo ={
'SXM':['SMF1', 'MSF1', 'Steptoe','Morex']
}
-
-# NL, 07/27/2010. moved from template.py
-IMGSTEP1 = HT.Image('/images/step1.gif', alt='STEP 1',border=0) #XZ, Only be used in inputPage.py
-IMGSTEP2 = HT.Image('/images/step2.gif', alt='STEP 2',border=0) #XZ, Only be used in inputPage.py
-IMGSTEP3 = HT.Image('/images/step3.gif', alt='STEP 3',border=0) #XZ, Only be used in inputPage.py
-IMGNEXT = HT.Image('/images/arrowdown.gif', alt='NEXT',border=0) #XZ, Only be used in inputPage.py
-
-IMGASC = HT.Image("/images/sortup.gif", border=0)
-IMGASCON = HT.Image("/images/sortupon.gif", border=0)
-IMGDESC = HT.Image("/images/sortdown.gif", border=0)
-IMGDESCON = HT.Image("/images/sortdownon.gif", border=0)
-
-"""
-IMGASC = HT.Image("/images/sortup_icon.gif", border=0)
-IMGASCON = HT.Image("/images/sortupon.gif", border=0)
-IMGDESC = HT.Image("/images/sortdown_icon.gif", border=0)
-IMGDESCON = HT.Image("/images/sortdownon.gif", border=0)
-IMG_UNSORTED = HT.Image("/images/unsorted_icon.gif", border=0)
-"""
-
-PROGRESSBAR = HT.Image('/images/waitAnima2.gif', alt='checkblue',align="middle",border=0)
-
#########################################
# Accessory Functions
#########################################
-def decodeEscape(str):
- a = str
- pattern = re.compile('(%[0-9A-Fa-f][0-9A-Fa-f])')
- match = pattern.findall(a)
- matched = []
- for item in match:
- if item not in matched:
- a = a.replace(item, '%c' % eval("0x"+item[-2:]))
- matched.append(item)
- return a
-
def exportData(hddn, tdata, NP = None):
for key in tdata.keys():
_val, _var, _N = tdata[key].val, tdata[key].var, tdata[key].N
@@ -136,37 +100,6 @@ def genShortStrainName(RISet='', input_strainName=''):
pass
return strainName
-def toInt(in_str):
- "Converts an arbitrary string to an unsigned integer"
- start = -1
- end = -1
- for i, char in enumerate(in_str):
- if char >= '0' and char <= '9':
- if start < 0:
- start = i
- end = i+1
- else:
- if start >= 0:
- break
- if start < end:
- return int(in_str[start:end])
- else:
- return -1
-
-def transpose(m):
- 'transpose a matrix'
- n = len(m)
- return [[m[j][i] for i in range(len(m[0])) for j in range(n)][k*n:k*n+n] for k in range(len(m[0]))]
-
-def asymTranspose(m):
- 'transpose a matrix'
- t = max(map(len, m))
- n = len(m)
- m2 = [["-"]]*n
- for i in range(n):
- m2[i] = m[i] + [""]*(t- len(m[i]))
- return [[m2[j][i] for i in range(len(m2[0])) for j in range(n)][k*n:k*n+n] for k in range(len(m2[0]))]
-
def genRandStr(prefix = "", length=8, chars=string.letters+string.digits):
from random import choice
_str = prefix[:]
@@ -174,27 +107,6 @@ def genRandStr(prefix = "", length=8, chars=string.letters+string.digits):
_str += choice(chars)
return _str
-def generate_session():
- import sha
- return sha.new(str(time.time())).hexdigest()
-
-def cvt2Dict(x):
- tmp = {}
- for key in x.keys():
- tmp[key] = x[key]
- return tmp
-
-def dump_session(session_obj, filename):
- "It seems mod python can only cPickle most basic data type"
- import cPickle
- session_file = open(filename, 'wb')
- #try:
- # pass
- #except:
- # pass
- cPickle.dump(session_obj, session_file)
- session_file.close()
-
def StringAsFloat(str):
'Converts string to float but catches any exception and returns None'
try:
diff --git a/wqflask/wqflask/correlation/show_corr_results.py b/wqflask/wqflask/correlation/show_corr_results.py
index 73072423..abf9fc89 100644
--- a/wqflask/wqflask/correlation/show_corr_results.py
+++ b/wqflask/wqflask/correlation/show_corr_results.py
@@ -296,60 +296,8 @@ class CorrelationResults(object):
if self.corr_type != "tissue" and self.dataset.type == "ProbeSet" and self.target_dataset.type == "ProbeSet":
self.do_tissue_correlation_for_trait_list()
- #print("self.correlation_results: ", pf(self.correlation_results))
-
self.json_results = generate_corr_json(self.correlation_results, self.this_trait, self.dataset, self.target_dataset)
- #XZ, 09/18/2008: get all information about the user selected database.
- #target_db_name = fd.corr_dataset
- #self.target_db_name = start_vars['corr_dataset']
-
- # Zach said this is ok
- # Auth if needed
- #try:
- # auth_user_for_db(self.db, self.cursor, self.target_db_name, self.privilege, self.userName)
- #except AuthException as e:
- # detail = [e.message]
- # return self.error(detail)
-
- #XZ, 09/18/2008: filter out the strains that have no value.
- #self.sample_names, vals, vars, N = fd.informativeStrains(sample_list)
-
- #print("samplenames is:", pf(self.sample_names))
- #CF - If less than a minimum number of strains/cases in common, don't calculate anything
- #if len(self.sample_names) < self.corr_min_informative:
- # detail = ['Fewer than %d strain data were entered for %s data set. No calculation of correlation has been attempted.' % (self.corr_min_informative, fd.RISet)]
- # self.error(heading=None, detail=detail)
-
- #correlation_method = self.CORRELATION_METHODS[self.method]
- #rankOrder = self.RANK_ORDERS[self.method]
-
- # CF - Number of results returned
- # Todo: Get rid of self.returnNumber
-
- #self.record_count = 0
-
- #myTrait = get_custom_trait(fd, self.cursor)
-
-
- # We will not get Literature Correlations if there is no GeneId because there is nothing
- # to look against
- #self.geneid = self.this_trait.geneid
-
- # We will not get Tissue Correlations if there is no gene symbol because there is nothing to look against
- #self.trait_symbol = myTrait.symbol
-
-
- #XZ, 12/12/2008: if the species is rat or human, translate the geneid to mouse geneid
- #self.input_trait_mouse_gene_id = self.translateToMouseGeneID(self.dataset.group.species, self.geneid)
-
- #XZ: As of Nov/13/2010, this dataset is 'UTHSC Illumina V6.2 RankInv B6 D2 average CNS GI average (May 08)'
- #self.tissue_probeset_freeze_id = 1
-
- #traitList = self.correlate()
-
- #print("Done doing correlation calculation")
-
############################################################################################################################################
def get_formatted_corr_type(self):
diff --git a/wqflask/wqflask/marker_regression/marker_regression_gn1.py b/wqflask/wqflask/marker_regression/marker_regression_gn1.py
index 632c2f8f..81da8976 100644
--- a/wqflask/wqflask/marker_regression/marker_regression_gn1.py
+++ b/wqflask/wqflask/marker_regression/marker_regression_gn1.py
@@ -190,21 +190,6 @@ class MarkerRegression(object):
self.js_data = start_vars['js_data']
self.trimmed_markers = start_vars['trimmed_markers'] #Top markers to display in table
- #ZS: Think I can just get all this from dataset object now
- #RISet and Species
- #if not fd.genotype:
- # fd.readGenotype()
- #
- #fd.parentsf14regression = fd.formdata.getvalue('parentsf14regression')
- #
- #if ((fd.parentsf14regression == 'on') and fd.genotype_2):
- # fd.genotype = fd.genotype_2
- #else:
- # fd.genotype = fd.genotype_1
- #fd.strainlist = list(fd.genotype.prgy)
- #
- #self.species = webqtlDatabaseFunction.retrieveSpecies(cursor=self.cursor, RISet=fd.RISet)
-
if self.dataset.group.species == "rat":
self._ucscDb = "rn3"
elif self.dataset.group.species == "mouse":
@@ -212,7 +197,6 @@ class MarkerRegression(object):
else:
self._ucscDb = ""
-
#####################################
# Options
#####################################
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py
index 3c2cca94..8ff359a7 100644
--- a/wqflask/wqflask/views.py
+++ b/wqflask/wqflask/views.py
@@ -57,7 +57,6 @@ from utility import temp_data
from utility.tools import SQL_URI,TEMPDIR,USE_REDIS,USE_GN_SERVER,GN_SERVER_URL,GN_VERSION,JS_TWITTER_POST_FETCHER_PATH,JS_GUIX_PATH, CSS_PATH
from utility.helper_functions import get_species_groups
-from base import webqtlFormData
from base.webqtlConfig import GENERATED_IMAGE_DIR
from utility.benchmark import Bench
@@ -163,62 +162,37 @@ def css(filename):
def twitter(filename):
return send_from_directory(JS_TWITTER_POST_FETCHER_PATH, filename)
-#@app.route("/data_sharing")
-#def data_sharing_page():
-# logger.info("In data_sharing")
-# fd = webqtlFormData.webqtlFormData(request.args)
-# logger.info("1Have fd")
-# sharingInfoObject = SharingInfo.SharingInfo(request.args['GN_AccessionId'], None)
-# info, htmlfilelist = sharingInfoObject.getBody(infoupdate="")
-# logger.info("type(htmlfilelist):", type(htmlfilelist))
-# htmlfilelist = htmlfilelist.encode("utf-8")
-# #template_vars = SharingInfo.SharingInfo(request.args['GN_AccessionId'], None)
-# logger.info("1 Made it to rendering")
-# return render_template("data_sharing.html",
-# info=info,
-# htmlfilelist=htmlfilelist)
-
-
@app.route("/search", methods=('GET',))
def search_page():
logger.info("in search_page")
logger.info(request.url)
- if 'info_database' in request.args:
- logger.info("Going to sharing_info_page")
- template_vars = sharing_info_page()
- if template_vars.redirect_url:
- logger.info("Going to redirect")
- return flask.redirect(template_vars.redirect_url)
- else:
- return render_template("data_sharing.html", **template_vars.__dict__)
+ result = None
+ if USE_REDIS:
+ with Bench("Trying Redis cache"):
+ key = "search_results:v1:" + json.dumps(request.args, sort_keys=True)
+ logger.debug("key is:", pf(key))
+ result = Redis.get(key)
+ if result:
+ logger.info("Redis cache hit on search results!")
+ result = pickle.loads(result)
else:
- result = None
- if USE_REDIS:
- with Bench("Trying Redis cache"):
- key = "search_results:v1:" + json.dumps(request.args, sort_keys=True)
- logger.debug("key is:", pf(key))
- result = Redis.get(key)
- if result:
- logger.info("Redis cache hit on search results!")
- result = pickle.loads(result)
- else:
- logger.info("Skipping Redis cache (USE_REDIS=False)")
+ logger.info("Skipping Redis cache (USE_REDIS=False)")
- logger.info("request.args is", request.args)
- the_search = search_results.SearchResultPage(request.args)
- result = the_search.__dict__
- valid_search = result['search_term_exists']
+ logger.info("request.args is", request.args)
+ the_search = search_results.SearchResultPage(request.args)
+ result = the_search.__dict__
+ valid_search = result['search_term_exists']
- logger.debugf("result", result)
+ logger.debugf("result", result)
- if USE_REDIS and valid_search:
- Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL))
- Redis.expire(key, 60*60)
+ if USE_REDIS and valid_search:
+ Redis.set(key, pickle.dumps(result, pickle.HIGHEST_PROTOCOL))
+ Redis.expire(key, 60*60)
- if valid_search:
- return render_template("search_result_page.html", **result)
- else:
- return render_template("search_error.html")
+ if valid_search:
+ return render_template("search_result_page.html", **result)
+ else:
+ return render_template("search_error.html")
@app.route("/gsearch", methods=('GET',))
def gsearchact():
@@ -741,7 +715,6 @@ def network_graph_page():
def corr_compute_page():
logger.info("In corr_compute, request.form is:", pf(request.form))
logger.info(request.url)
- #fd = webqtlFormData.webqtlFormData(request.form)
template_vars = show_corr_results.CorrelationResults(request.form)
return render_template("correlation_page.html", **template_vars.__dict__)
@@ -777,15 +750,6 @@ def submit_bnw():
template_vars = get_bnw_input(request.form)
return render_template("empty_collection.html", **{'tool':'Correlation Matrix'})
-# Todo: Can we simplify this? -Sam
-def sharing_info_page():
- """Info page displayed when the user clicks the "Info" button next to the dataset selection"""
- logger.info("In sharing_info_page")
- logger.info(request.url)
- fd = webqtlFormData.webqtlFormData(request.args)
- template_vars = SharingInfoPage.SharingInfoPage(fd)
- return template_vars
-
# Take this out or secure it before putting into production
@app.route("/get_temp_data")
def get_temp_data():
--
cgit v1.2.3
From d8cec0ef94b7683f42946ce182a937484ad1034a Mon Sep 17 00:00:00 2001
From: zsloan
Date: Tue, 10 Apr 2018 21:30:33 +0000
Subject: Most code in webqtlUtil.py has been removed. There is so little left
that the remaining could should be migrated to the files that use them at
some point.
Some of this code was in the correlation code but not being used, so it wss removed from there as well.
---
wqflask/utility/webqtlUtil.py | 700 +--------------------
.../wqflask/correlation/correlation_functions.py | 56 --
wqflask/wqflask/correlation/show_corr_results.py | 30 -
3 files changed, 1 insertion(+), 785 deletions(-)
(limited to 'wqflask/utility/webqtlUtil.py')
diff --git a/wqflask/utility/webqtlUtil.py b/wqflask/utility/webqtlUtil.py
index 94dd7cbf..83fa90b7 100644
--- a/wqflask/utility/webqtlUtil.py
+++ b/wqflask/utility/webqtlUtil.py
@@ -65,41 +65,6 @@ ParInfo ={
# Accessory Functions
#########################################
-def exportData(hddn, tdata, NP = None):
- for key in tdata.keys():
- _val, _var, _N = tdata[key].val, tdata[key].var, tdata[key].N
- if _val != None:
- hddn[key] = _val
- if _var != None:
- hddn['V'+key] = _var
- if NP and _N != None:
- hddn['N'+key] = _N
-
-def genShortStrainName(RISet='', input_strainName=''):
- #aliasStrainDict = {'C57BL/6J':'B6','DBA/2J':'D2'}
- strainName = input_strainName
- if RISet != 'AXBXA':
- if RISet == 'BXD300':
- this_RISet = 'BXD'
- elif RISet == 'BDF2-2005':
- this_RISet = 'CASE05_'
- else:
- this_RISet = RISet
- strainName = string.replace(strainName,this_RISet,'')
- strainName = string.replace(strainName,'CASE','')
- try:
- strainName = "%02d" % int(strainName)
- except:
- pass
- else:
- strainName = string.replace(strainName,'AXB','A')
- strainName = string.replace(strainName,'BXA','B')
- try:
- strainName = strainName[0] + "%02d" % int(strainName[1:])
- except:
- pass
- return strainName
-
def genRandStr(prefix = "", length=8, chars=string.letters+string.digits):
from random import choice
_str = prefix[:]
@@ -107,63 +72,6 @@ def genRandStr(prefix = "", length=8, chars=string.letters+string.digits):
_str += choice(chars)
return _str
-def StringAsFloat(str):
- 'Converts string to float but catches any exception and returns None'
- try:
- return float(str)
- except:
- return None
-
-def IntAsFloat(str):
- 'Converts string to Int but catches any exception and returns None'
- try:
- return int(str)
- except:
- return None
-
-def FloatAsFloat(flt):
- 'Converts float to string but catches any exception and returns None'
- try:
- return float("%2.3f" % flt)
- except:
- return None
-
-def RemoveZero(flt):
- 'Converts string to float but catches any exception and returns None'
- try:
- if abs(flt) < 1e-6:
- return None
- else:
- return flt
- except:
- return None
-
-
-def SciFloat(d):
- 'Converts string to float but catches any exception and returns None'
-
- try:
- if abs(d) <= 1.0e-4:
- return "%1.2e" % d
- else:
- return "%1.5f" % d
- except:
- return None
-
-###To be removed
-def FloatList2String(lst):
- 'Converts float list to string but catches any exception and returns None'
- tt=''
- try:
- for item in lst:
- if item == None:
- tt += 'X '
- else:
- tt += '%f ' % item
- return tt
- except:
- return ""
-
def ListNotNull(lst):
'''Obsolete - Use built in function any (or all or whatever)
@@ -176,427 +84,6 @@ def ListNotNull(lst):
return 1
return None
-###To be removed
-def FileDataProcess(str):
- 'Remove the description text from the input file if theres any'
- i=0
- while i'\x20':
- break
- else:
- i+=1
- str=str[i:]
- str=string.join(string.split(str,'\000'),'')
- i=string.find(str,"*****")
- if i>-1:
- return str[i+5:]
- else:
- return str
-
-def rank(a,lst,offset=0):
- """Calculate the integer rank of a number in an array, can be used to calculate p-value"""
- n = len(lst)
- if n == 2:
- if a lst[1]:
- return offset + 2
- else:
- return offset +1
- elif n == 1:
- if a B.LRS:
- return 1
- elif A.LRS == B.LRS:
- return 0
- else:
- return -1
- except:
- return 0
-
-
-def cmpScanResult2(A,B):
- try:
- if A.LRS < B.LRS:
- return 1
- elif A.LRS == B.LRS:
- return 0
- else:
- return -1
- except:
- return 0
-
-def cmpOrder(A,B):
- try:
- if A[1] < B[1]:
- return -1
- elif A[1] == B[1]:
- return 0
- else:
- return 1
- except:
- return 0
-
-def cmpOrder2(A,B):
- try:
- if A[-1] < B[-1]:
- return -1
- elif A[-1] == B[-1]:
- return 0
- else:
- return 1
- except:
- return 0
-
-
-
-
-def calRank(xVals, yVals, N): ### Zach Sloan, February 4 2010
- """
- Returns a ranked set of X and Y values. These are used when generating
- a Spearman scatterplot. Bear in mind that this sets values equal to each
- other as the same rank.
- """
- XX = []
- YY = []
- X = [0]*len(xVals)
- Y = [0]*len(yVals)
- j = 0
-
- for i in range(len(xVals)):
-
- if xVals[i] != None and yVals[i] != None:
- XX.append((j, xVals[i]))
- YY.append((j, yVals[i]))
- j = j + 1
-
- NN = len(XX)
-
- XX.sort(cmpOrder2)
- YY.sort(cmpOrder2)
-
- j = 1
- rank = 0.0
-
- while j < NN:
-
- if XX[j][1] != XX[j-1][1]:
- X[XX[j-1][0]] = j
- j = j+1
-
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (XX[jt][1] != XX[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- X[XX[ji][0]] = rank
- if (jt == NN-1):
- if (XX[jt][1] == XX[j-1][1]):
- X[XX[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if X[XX[NN-1][0]] == 0:
- X[XX[NN-1][0]] = NN
-
- j = 1
- rank = 0.0
-
- while j < NN:
-
- if YY[j][1] != YY[j-1][1]:
- Y[YY[j-1][0]] = j
- j = j+1
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (YY[jt][1] != YY[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- Y[YY[ji][0]] = rank
- if (jt == NN-1):
- if (YY[jt][1] == YY[j-1][1]):
- Y[YY[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if Y[YY[NN-1][0]] == 0:
- Y[YY[NN-1][0]] = NN
-
- return (X,Y)
-
-def calCorrelationRank(xVals,yVals,N):
- """
- Calculated Spearman Ranked Correlation. The algorithm works
- by setting all tied ranks to the average of those ranks (for
- example, if ranks 5-10 all have the same value, each will be set
- to rank 7.5).
- """
-
- XX = []
- YY = []
- j = 0
-
- for i in range(len(xVals)):
- if (xVals[i]!= None and yVals[i]!= None) and (xVals[i] != "None" and yVals[i] != "None"):
- XX.append((j,xVals[i]))
- YY.append((j,yVals[i]))
- j = j+1
-
- NN = len(XX)
- if NN <6:
- return (0.0,NN)
- XX.sort(cmpOrder2)
- YY.sort(cmpOrder2)
- X = [0]*NN
- Y = [0]*NN
-
- j = 1
- rank = 0.0
- t = 0.0
- sx = 0.0
-
- while j < NN:
-
- if XX[j][1] != XX[j-1][1]:
- X[XX[j-1][0]] = j
- j = j+1
-
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (XX[jt][1] != XX[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- X[XX[ji][0]] = rank
- t = jt-j
- sx = sx + (t*t*t-t)
- if (jt == NN-1):
- if (XX[jt][1] == XX[j-1][1]):
- X[XX[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if X[XX[NN-1][0]] == 0:
- X[XX[NN-1][0]] = NN
-
- j = 1
- rank = 0.0
- t = 0.0
- sy = 0.0
-
- while j < NN:
-
- if YY[j][1] != YY[j-1][1]:
- Y[YY[j-1][0]] = j
- j = j+1
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (YY[jt][1] != YY[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- Y[YY[ji][0]] = rank
- t = jt - j
- sy = sy + (t*t*t-t)
- if (jt == NN-1):
- if (YY[jt][1] == YY[j-1][1]):
- Y[YY[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if Y[YY[NN-1][0]] == 0:
- Y[YY[NN-1][0]] = NN
-
- D = 0.0
-
- for i in range(NN):
- D += (X[i]-Y[i])*(X[i]-Y[i])
-
- fac = (1.0 -sx/(NN*NN*NN-NN))*(1.0-sy/(NN*NN*NN-NN))
-
- return ((1-(6.0/(NN*NN*NN-NN))*(D+(sx+sy)/12.0))/math.sqrt(fac),NN)
-
-
-def calCorrelationRankText(dbdata,userdata,N): ### dcrowell = David Crowell, July 2008
- """Calculates correlation ranks with data formatted from the text file.
- dbdata, userdata are lists of strings. N is an int. Returns a float.
- Used by correlationPage"""
- XX = []
- YY = []
- j = 0
- for i in range(N):
- if (dbdata[i]!= None and userdata[i]!=None) and (dbdata[i]!= 'None' and userdata[i]!='None'):
- XX.append((j,float(dbdata[i])))
- YY.append((j,float(userdata[i])))
- j += 1
- NN = len(XX)
- if NN <6:
- return (0.0,NN)
- XX.sort(cmpOrder2)
- YY.sort(cmpOrder2)
- X = [0]*NN
- Y = [0]*NN
-
- j = 1
- rank = 0.0
- t = 0.0
- sx = 0.0
-
- while j < NN:
-
- if XX[j][1] != XX[j-1][1]:
- X[XX[j-1][0]] = j
- j = j+1
-
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (XX[jt][1] != XX[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- X[XX[ji][0]] = rank
- t = jt-j
- sx = sx + (t*t*t-t)
- if (jt == NN-1):
- if (XX[jt][1] == XX[j-1][1]):
- X[XX[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if X[XX[NN-1][0]] == 0:
- X[XX[NN-1][0]] = NN
-
- j = 1
- rank = 0.0
- t = 0.0
- sy = 0.0
-
- while j < NN:
-
- if YY[j][1] != YY[j-1][1]:
- Y[YY[j-1][0]] = j
- j = j+1
- else:
- jt = j+1
- ji = j
- for jt in range(j+1, NN):
- if (YY[jt][1] != YY[j-1][1]):
- break
- rank = 0.5*(j+jt)
- for ji in range(j-1, jt):
- Y[YY[ji][0]] = rank
- t = jt - j
- sy = sy + (t*t*t-t)
- if (jt == NN-1):
- if (YY[jt][1] == YY[j-1][1]):
- Y[YY[NN-1][0]] = rank
- j = jt+1
-
- if j == NN:
- if Y[YY[NN-1][0]] == 0:
- Y[YY[NN-1][0]] = NN
-
- D = 0.0
-
- for i in range(NN):
- D += (X[i]-Y[i])*(X[i]-Y[i])
-
- fac = (1.0 -sx/(NN*NN*NN-NN))*(1.0-sy/(NN*NN*NN-NN))
-
- return ((1-(6.0/(NN*NN*NN-NN))*(D+(sx+sy)/12.0))/math.sqrt(fac),NN)
-
-
-
-def calCorrelation(dbdata,userdata,N):
- X = []
- Y = []
- for i in range(N):
- if dbdata[i]!= None and userdata[i]!= None:
- X.append(dbdata[i])
- Y.append(userdata[i])
- NN = len(X)
- if NN <6:
- return (0.0,NN)
- sx = reduce(lambda x,y:x+y,X,0.0)
- sy = reduce(lambda x,y:x+y,Y,0.0)
- meanx = sx/NN
- meany = sy/NN
- xyd = 0.0
- sxd = 0.0
- syd = 0.0
- for i in range(NN):
- xyd += (X[i] - meanx)*(Y[i]-meany)
- sxd += (X[i] - meanx)*(X[i] - meanx)
- syd += (Y[i] - meany)*(Y[i] - meany)
- try:
- corr = xyd/(sqrt(sxd)*sqrt(syd))
- except:
- corr = 0
- return (corr,NN)
-
-def calCorrelationText(dbdata,userdata,N): ### dcrowell July 2008
- """Calculates correlation coefficients with values formatted from text files. dbdata, userdata are lists of strings. N is an int. Returns a float
- Used by correlationPage"""
- X = []
- Y = []
- for i in range(N):
- #if (dbdata[i]!= None and userdata[i]!= None) and (dbdata[i]!= 'None' and userdata[i]!= 'None'):
- # X.append(float(dbdata[i]))
- # Y.append(float(userdata[i]))
- if dbdata[i] == None or dbdata[i] == 'None' or userdata[i] == None or userdata[i] == 'None':
- continue
- else:
- X.append(float(dbdata[i]))
- Y.append(float(userdata[i]))
- NN = len(X)
- if NN <6:
- return (0.0,NN)
- sx = sum(X)
- sy = sum(Y)
- meanx = sx/float(NN)
- meany = sy/float(NN)
- xyd = 0.0
- sxd = 0.0
- syd = 0.0
- for i in range(NN):
- x1 = X[i]-meanx
- y1 = Y[i]-meany
- xyd += x1*y1
- sxd += x1**2
- syd += y1**2
- try:
- corr = xyd/(sqrt(sxd)*sqrt(syd))
- except:
- corr = 0
- return (corr,NN)
-
-
def readLineCSV(line): ### dcrowell July 2008
"""Parses a CSV string of text and returns a list containing each element as a string.
Used by correlationPage"""
@@ -605,45 +92,6 @@ def readLineCSV(line): ### dcrowell July 2008
returnList[0]=returnList[0][1:]
return returnList
-
-def cmpCorr(A,B):
- try:
- if abs(A[1]) < abs(B[1]):
- return 1
- elif abs(A[1]) == abs(B[1]):
- return 0
- else:
- return -1
- except:
- return 0
-
-def cmpLitCorr(A,B):
- try:
- if abs(A[3]) < abs(B[3]): return 1
- elif abs(A[3]) == abs(B[3]):
- if abs(A[1]) < abs(B[1]): return 1
- elif abs(A[1]) == abs(B[1]): return 0
- else: return -1
- else: return -1
- except:
- return 0
-
-def cmpPValue(A,B):
- try:
- if A.corrPValue < B.corrPValue:
- return -1
- elif A.corrPValue == B.corrPValue:
- if abs(A.corr) > abs(B.corr):
- return -1
- elif abs(A.corr) < abs(B.corr):
- return 1
- else:
- return 0
- else:
- return 1
- except:
- return 0
-
def cmpEigenValue(A,B):
try:
if A[0] > B[0]:
@@ -655,80 +103,6 @@ def cmpEigenValue(A,B):
except:
return 0
-
-def cmpLRSFull(A,B):
- try:
- if A[0] < B[0]:
- return -1
- elif A[0] == B[0]:
- return 0
- else:
- return 1
- except:
- return 0
-
-def cmpLRSInteract(A,B):
- try:
- if A[1] < B[1]:
- return -1
- elif A[1] == B[1]:
- return 0
- else:
- return 1
- except:
- return 0
-
-
-def cmpPos(A,B):
- try:
- try:
- AChr = int(A.chr)
- except:
- AChr = 20
- try:
- BChr = int(B.chr)
- except:
- BChr = 20
- if AChr > BChr:
- return 1
- elif AChr == BChr:
- if A.mb > B.mb:
- return 1
- if A.mb == B.mb:
- return 0
- else:
- return -1
- else:
- return -1
- except:
- return 0
-
-def cmpGenoPos(A,B):
- try:
- A1 = A.chr
- B1 = B.chr
- try:
- A1 = int(A1)
- except:
- A1 = 25
- try:
- B1 = int(B1)
- except:
- B1 = 25
- if A1 > B1:
- return 1
- elif A1 == B1:
- if A.mb > B.mb:
- return 1
- if A.mb == B.mb:
- return 0
- else:
- return -1
- else:
- return -1
- except:
- return 0
-
def hasAccessToConfidentialPhenotypeTrait(privilege, userName, authorized_users):
access_to_confidential_phenotype_trait = 0
if webqtlConfig.USERDICT[privilege] > webqtlConfig.USERDICT['user']:
@@ -737,76 +111,4 @@ def hasAccessToConfidentialPhenotypeTrait(privilege, userName, authorized_users)
AuthorizedUsersList=map(string.strip, string.split(authorized_users, ','))
if AuthorizedUsersList.__contains__(userName):
access_to_confidential_phenotype_trait = 1
- return access_to_confidential_phenotype_trait
-
-
-class VisualizeException(Exception):
- def __init__(self, message):
- self.message = message
- def __str__(self):
- return self.message
-
-# safeConvert : (string -> A) -> A -> A
-# to convert a string to type A, using the supplied default value
-# if the given conversion function doesn't work
-def safeConvert(f, value, default):
- try:
- return f(value)
- except:
- return default
-
-# safeFloat : string -> float -> float
-# to convert a string to a float safely
-def safeFloat(value, default):
- return safeConvert(float, value, default)
-
-# safeInt: string -> int -> int
-# to convert a string to an int safely
-def safeInt(value, default):
- return safeConvert(int, value, default)
-
-# safeString : string -> (arrayof string) -> string -> string
-# if a string is not in a list of strings to pick a default value
-# for that string
-def safeString(value, validChoices, default):
- if value in validChoices:
- return value
- else:
- return default
-
-# yesNoToInt: string -> int
-# map "yes" -> 1 and "no" -> 0
-def yesNoToInt(value):
- if value == "yes":
- return 1
- elif value == "no":
- return 0
- else:
- return None
-
-# IntToYesNo: int -> string
-# map 1 -> "yes" and 0 -> "no"
-def intToYesNo(value):
- if value == 1:
- return "yes"
- elif value == 0:
- return "no"
- else:
- return None
-
-def formatField(name):
- name = name.replace("_", " ")
- name = name.title()
- #name = name.replace("Mb Mm6", "Mb");
- return name.replace("Id", "ID")
-
-def natsort_key(string):
- r = []
- for c in string:
- try:
- c = int(c)
- try: r[-1] = r[-1] * 10 + c
- except: r.append(c)
- except:
- r.append(c)
- return r
\ No newline at end of file
+ return access_to_confidential_phenotype_trait
\ No newline at end of file
diff --git a/wqflask/wqflask/correlation/correlation_functions.py b/wqflask/wqflask/correlation/correlation_functions.py
index 80a0818c..1ee9b558 100644
--- a/wqflask/wqflask/correlation/correlation_functions.py
+++ b/wqflask/wqflask/correlation/correlation_functions.py
@@ -491,62 +491,6 @@ pcor.rec <- function(x,y,z,method="p",na.rm=T){
return allcorrelations
-
-#XZ, April 30, 2010: The input primaryTrait and targetTrait are instance of webqtlTrait
-#XZ: The primaryTrait and targetTrait should have executed retrieveData function
-def calZeroOrderCorr(primaryTrait, targetTrait, method='pearson'):
-
- #primaryTrait.retrieveData()
-
- #there is no None value in primary_val
- primary_strain, primary_val, primary_var = primaryTrait.exportInformative()
-
- #targetTrait.retrieveData()
-
- #there might be None value in target_val
- target_val = targetTrait.exportData(primary_strain, type="val")
-
- R_primary = rpy2.robjects.FloatVector(range(len(primary_val)))
- for i in range(len(primary_val)):
- R_primary[i] = primary_val[i]
-
- N = len(target_val)
-
- if None in target_val:
- goodIndex = []
- for i in range(len(target_val)):
- if target_val[i] != None:
- goodIndex.append(i)
-
- N = len(goodIndex)
-
- R_primary = rpy2.robjects.FloatVector(range(len(goodIndex)))
- for i in range(len(goodIndex)):
- R_primary[i] = primary_val[goodIndex[i]]
-
- R_target = rpy2.robjects.FloatVector(range(len(goodIndex)))
- for i in range(len(goodIndex)):
- R_target[i] = target_val[goodIndex[i]]
-
- else:
- R_target = rpy2.robjects.FloatVector(range(len(target_val)))
- for i in range(len(target_val)):
- R_target[i] = target_val[i]
-
- R_corr_test = rpy2.robjects.r['cor.test']
-
- if method == 'spearman':
- R_result = R_corr_test(R_primary, R_target, method='spearman')
- else:
- R_result = R_corr_test(R_primary, R_target)
-
- corr_result = []
- corr_result.append( R_result[3][0] )
- corr_result.append( N )
- corr_result.append( R_result[2][0] )
-
- return corr_result
-
#####################################################################################
#Input: primaryValue(list): one list of expression values of one probeSet,
# targetValue(list): one list of expression values of one probeSet,
diff --git a/wqflask/wqflask/correlation/show_corr_results.py b/wqflask/wqflask/correlation/show_corr_results.py
index abf9fc89..85a8c0ef 100644
--- a/wqflask/wqflask/correlation/show_corr_results.py
+++ b/wqflask/wqflask/correlation/show_corr_results.py
@@ -555,21 +555,6 @@ class CorrelationResults(object):
self.record_count = len(traits) #ZS: This isn't a good way to get this value, so I need to change it later
- #XZ, 3/31/2010: Theoretically, we should create one function 'comTissueCorr'
- #to compare each trait by their tissue corr p values.
- #But because the tissue corr p values are generated by permutation test,
- #the top ones always have p value 0. So comparing p values actually does nothing.
- #In addition, for the tissue data in our database, the N is always the same.
- #So it's safe to compare with tissue corr statistic value.
- #That's the same as literature corr.
- #if self.method in [METHOD_LIT, METHOD_TISSUE_PEARSON, METHOD_TISSUE_RANK] and self.gene_id:
- # traits.sort(webqtlUtil.cmpLitCorr)
- #else:
- #if self.method in TISSUE_METHODS:
- # sort(traits, key=lambda A: math.fabs(A.tissue_corr))
- #elif self.method == METHOD_LIT:
- # traits.sort(traits, key=lambda A: math.fabs(A.lit_corr))
- #else:
traits = sortTraitCorrelations(traits, self.method)
# Strip to the top N correlations
@@ -1069,21 +1054,6 @@ class CorrelationResults(object):
return (symbolCorrDict, symbolPvalueDict)
-
- def correlate(self):
- self.correlation_data = collections.defaultdict(list)
- for trait, values in self.target_dataset.trait_data.iteritems():
- values_1 = []
- values_2 = []
- for index,sample in enumerate(self.target_dataset.samplelist):
- target_value = values[index]
- if sample in self.sample_data.keys():
- this_value = self.sample_data[sample]
- values_1.append(this_value)
- values_2.append(target_value)
- correlation = calCorrelation(values_1, values_2)
- self.correlation_data[trait] = correlation
-
def getFileName(self, target_db_name): ### dcrowell August 2008
"""Returns the name of the reference database file with which correlations are calculated.
Takes argument cursor which is a cursor object of any instance of a subclass of templatePage
--
cgit v1.2.3