/
newAPI.py
431 lines (353 loc) · 14.5 KB
/
newAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""
API operations on a history.
.. seealso:: :class:`galaxy.model.History`
"""
# from sqlalchemy import true, false
import json
from operator import itemgetter
# from gold.application.HBAPI import doAnalysis
from galaxy import exceptions
from galaxy.web import _future_expose_api as expose_api
from galaxy.web import _future_expose_api_anonymous as expose_api_anonymous
from galaxy.web import _future_expose_api_anonymous_and_sessionless as expose_api_anonymous_and_sessionless
from galaxy.web import _future_expose_api_raw as expose_api_raw
from galaxy.web.base.controller import BaseAPIController
from gold.statistic.CountSegmentStat import CountSegmentStat
from quick.application.ExternalTrackManager import ExternalTrackManager
from galaxy.managers import histories, citations, users
from quick.util.GenomeInfo import GenomeInfo
from gold.description.AnalysisDefHandler import AnalysisSpec
from gold.track.Track import Track
from gold.application.StatRunner import AnalysisDefJob
from quick.application.UserBinSource import UserBinSource
from gold.statistic.CountStat import CountStat
from gold.statistic.RawOverlapStat import RawOverlapStat
from quick.util import TrackReportCommon
from quick.statistic.AvgSegLenStat import AvgSegLenStat
from quick.statistic.StartEndStat import StartEndStat
import ast
import logging
log = logging.getLogger( __name__ )
import itertools
import re
import numpy as np
from quick.statistic.SegmentDistancesStat import SegmentDistancesStat
from gold.statistic.SegmentLengthsStat import SegmentLengthsStat
from gold.statistic.ProportionCountStat import ProportionCountStat
from proto.hyperbrowser.HtmlCore import HtmlCore
from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile
from quick.util.CommonFunctions import ensurePathExists
from collections import OrderedDict
from third_party.alphanum import alphanum
class VisualisationController( BaseAPIController):
def __init__( self, app ):
super( VisualisationController, self ).__init__( app )
self.citations_manager = citations.CitationsManager( app )
self.user_manager = users.UserManager( app )
# ************* Generic Methods*************
@staticmethod
def getQueryTrack(trackName):
trackTemp = json.loads(trackName)
return [Track(trackTemp)]
@classmethod
def yieldDictElements(cls, mydict):
for key, val in mydict.iteritems():
yield key, val
@classmethod
def yieldListElements(cls, mylist):
for i, val in enumerate(mylist):
yield i, val
@classmethod
def replaceItem(cls, mydictorlist):
from copy import copy
retdictorlist = copy(mydictorlist)
if isinstance(mydictorlist, dict):
yieldFunc = cls.yieldDictElements
elif isinstance(mydictorlist, list):
yieldFunc = cls.yieldListElements
else:
raise TypeError('Not list or dict')
for key, val in yieldFunc(mydictorlist):
if isinstance(val, list) or isinstance(val, dict):
retdictorlist[key] = cls.replaceItem(val)
elif isinstance(val, np.ndarray):
retdictorlist[key] = np.nan_to_num(val).tolist()
return retdictorlist
@classmethod
def replaceItem1(cls, mydict):
from copy import copy
retdict = copy(mydict)
for key, val in mydict.iteritems():
if isinstance(val, dict):
retdict[key] = cls.replaceItem(val)
elif isinstance(val, np.ndarray):
retdict[key] = np.nan_to_num(val).tolist()
return retdict
@classmethod
def doAnalysisFromDef(cls, trackNames, analysisDef, regSpec, binSpec, genome, flag):
userBinSource = UserBinSource(regSpec, binSpec, genome)
job = AnalysisDefJob(analysisDef.getDefAfterChoices(), trackNames[0].trackName, None, userBinSource,
galaxyFn=None)
result = job.run()
unorderedResults = [(str(key), result[key]) for key in result.getAllRegionKeys()]
return cls.sortDictionaries(unorderedResults, flag)
@classmethod
def sortDictionaries(cls, unorderedResults, flag):
if flag == 0:
return dict(unorderedResults)
elif flag == 1:
return list(sorted(unorderedResults, cmp=alphanum, key=itemgetter(0)))
else:
return "assign flag value"
@staticmethod
def getAnalysisDefFromStat(stat):
if stat == 'CountStat':
return (AnalysisSpec(CountStat), 1)
elif stat == 'StartEndStat':
return (AnalysisSpec(StartEndStat), 1)
elif stat == 'SegmentDistancesStat':
return (AnalysisSpec(SegmentDistancesStat), 0)
elif stat == 'SegmentLengthsStat':
return (AnalysisSpec(SegmentLengthsStat), 0)
elif stat == 'AvgSegLenStat':
return (AnalysisSpec(AvgSegLenStat), 0)
elif stat == 'CountSegmentStat':
return (AnalysisSpec(CountSegmentStat), 1)
elif stat == 'ProportionCountStat':
return (AnalysisSpec(ProportionCountStat), 1)
elif stat == 'RawOverlapStat':
return (AnalysisSpec(RawOverlapStat), 1)
else:
return "Something went wrong"
@classmethod
def getBinSize(cls, binS, **kwd):
if binS:
return binS
else:
if (kwd.get('reg')).find(chr) == 1:
return '1m'
else:
return kwd.get('bin')
@classmethod
def getRegSize(cls, **kwd):
return kwd.get('reg')
@classmethod
def initializeValues(cls, **kwd):
binS = kwd.get('bin')
genome = kwd.get('genome')
firstTrack = kwd.get('firstTrack')
stat = kwd.get('stat')
regSpec = cls.getRegSize(**kwd)
binSpec = cls.getBinSize(binS, **kwd)
secondDataset = ''
track1 = cls.getQueryTrack(firstTrack)
if stat!='RawOverlapStat':
tracks = track1
else:
secondTrack = kwd.get('secondTrack')
track2 = cls.getQueryTrack(secondTrack)
tracks = track1 + track2
myList = []
for var in [tracks, regSpec, binSpec, genome]:
myList.append(var)
return myList
@classmethod
def getAnalysisResults(cls, list, stat):
if stat!='RawOverlapStat':
analysisDef, flag = cls.getAnalysisDefFromStat(stat)
analysisResults = cls.doAnalysisFromDef(list[0], analysisDef, list[1], list[2], list[3], flag)
return analysisResults
else:
analysisDef, flag = cls.getAnalysisDefFromStat(stat)
analysisResults = cls.doAnalysisFromDefTwo(list[0], analysisDef, list[1], list[2], list[3], flag)
return analysisResults
@classmethod
def getOverlapResults(cls, intResults):
resultsBoth = dict()
resultsNone = dict()
resultsOnlyone = dict()
resultsOnlytwo = dict()
processedResults = OrderedDict()
for refTrackName, refTrackResults in intResults:
processedResults[refTrackName] = []
both = int(refTrackResults['Both'])
query = int(refTrackResults['Only1']) + both
processedResults[refTrackName].append(both)
processedResults[refTrackName].append(query)
return cls.sortDictionaries(processedResults, 1)
@classmethod
def resultRunAnalysis(cls, **kwd):
varList = cls.initializeValues(**kwd)
stat = kwd.get('stat')
if stat != 'RawOverlapStat':
analysisResult = cls.getAnalysisResults(varList, stat)
if stat == 'StartEndStat':
newDict = cls.mergeKeysValuesListsStartEndStat(cls, analysisResult)
return cls.getResultFromDict(cls, newDict)
elif stat == 'CountStat':
return cls.mergeKeysValuesListsCountStat2(cls, analysisResult)
elif stat == 'SegmentDistancesStat':
return cls.mergeKeysValuesListsSegmentDistancesStat(cls, analysisResult)
elif stat == 'SegmentLengthsStat':
return cls.mergeKeysValuesListsSegmentLengthsStat(cls, analysisResult)
elif stat == 'AvgSegLenStat':
return cls.mergeKeysValuesListsCountStat(cls, analysisResult)
elif stat == 'CountSegmentStat':
return cls.mergeKeysValuesListsCountStat2(cls, analysisResult)
elif stat == 'ProportionCountStat':
return cls.mergeKeysValuesListsCountStat2(cls, analysisResult)
else:
return "Something went wrong in ResultRunAnalysis method"
else:
analysisResult = cls.getAnalysisResults(varList, stat)
result = cls.overlapResultsInLists(analysisResult)
return result
# ************* General Methods End*************
# ************* CountStat Start*************
@staticmethod
def makeDictToList(dict):
'''Making Lists from the Track data in Dictionary form'''
list = [[k, v] for k, v in dict.items()]
return list
@staticmethod
def separateResultValues(list):
'''Taking out result values from the Track List'''
results = []
for i in range(len(list)):
results.append(list[i][1])
return results
@staticmethod
def makeListofCountValues(results):
'''Making a list of CountStat results'''
resultV = []
for d in results:
for key in d:
resultV.append(d[key])
return resultV
# ************* CountStat End*************
# ************* StartEndStat Start*************
@staticmethod
def makeListofStartEndValues(results):
'''Making a list of StartEndStat results'''
resultV = []
for d in results:
for key in d:
resultV.append(d[key])
return resultV
@staticmethod
def makeListofKeys(list):
''' Take keys from the Track list in a list '''
resultK = []
for i in list:
resultK.append(i[0])
return resultK
@staticmethod
def extractChrStartFromKeys(stringKey):
startNum = stringKey.split(':')
startNum = startNum[1].split('-')
return int(startNum[0]) - 1
@staticmethod
def clearNaNFromList(listInput, older, newer):
listInput[:] = [newer if x == older else x for x in listInput]
return listInput
@staticmethod
def mergeKeysValuesListsCountStat(cls, data):
result = cls.replaceItem1(data)
return result
@staticmethod
def extractChrNameFromKey(name):
return name.split(':')[0]
@staticmethod
def hasNumbers(inputString):
return bool(re.search(r'\d', inputString))
@staticmethod
def distributeListsChrStartEnd(self, newData):
keyList = [value for key,value in enumerate(newData) if key%2==0]
resultList = [value for key, value in enumerate(newData) if key%2!=0]
totalList = [keyList, resultList]
return totalList
@staticmethod
def flattenListEachChr(myList):
return list(itertools.chain.from_iterable(myList))
@staticmethod
def mergeKeysValuesListsStartEndStat(cls, data):
newData = cls.flattenListEachChr(data)
newDict = cls.distributeListsChrStartEnd(cls, newData)
return newDict
@staticmethod
def getResultFromDict(cls, newDict):
chrNames = []
myResult = ""
for keys, values in itertools.izip(newDict[0], newDict[1]):
for start, end in itertools.izip(values["Result"][0], values["Result"][1]):
myResult += "{'x':" + str(start) + ", 'x2': " + \
str(end) + ", 'y': " +str(newDict[0].index(keys)) + "},"
chrNames.append(cls.extractChrNameFromKey(keys))
finalResult = list([(ast.literal_eval(myResult))] + [chrNames])
return finalResult
# ************* StartEndStat End*************
# ************* RawOverlapStat Start*************
@classmethod
def doAnalysisFromDefTwo(cls, tracks, analysisDef, regSpec, binSpec, genome, flag):
userBinSource = UserBinSource(regSpec, binSpec, genome)
job = AnalysisDefJob(analysisDef.getDefAfterChoices(), tracks[0].trackName, tracks[1].trackName, userBinSource,
galaxyFn=None)
result = job.run()
unorderedResults = [(str(key), result[key]) for key in result.getAllRegionKeys()]
return list(cls.sortDictionaries(unorderedResults, flag))
@classmethod
def overlapResultsInLists(cls, inputList):
dictInput = cls.flattenListEachChr(inputList)
result = cls.getUsefulResult(dictInput)
return result
@classmethod
def getUsefulResult(cls, inputList):
keyList = [value for key, value in enumerate(inputList) if key%2==0]
bothList = [value['Both'] for key, value in enumerate(inputList) if key%2!=0]
only1List = [value['Only1'] for key, value in enumerate(inputList) if key%2!=0]
totalList = [keyList, bothList, only1List]
return totalList
# ************* RawOverlapStat End *************
# ************* SegmentDistancesStat Start ***************
@classmethod
def removeUnwantedTypes(cls, data):
a = []
b = []
for keys in data:
a.append(keys)
b.append(data[keys]['Result'])
newDict = dict(zip(a,b))
return newDict
@staticmethod
def mergeKeysValuesListsSegmentDistancesStat(cls, data ):
data1 = cls.removeUnwantedTypes(data)
results1 = cls.replaceItem(data1)
list = cls.makeDictToList(results1)
results = cls.separateResultValues(list)
return cls.flattenListEachChr(results)
# ************* SegmentDistancesStat End ***************
@staticmethod
def mergeKeysValuesListsSegmentLengthsStat(cls, data):
data1 = cls.removeUnwantedTypes(data)
results1 = cls.replaceItem(data1)
list = cls.makeDictToList(results1)
results = cls.separateResultValues(list)
return cls.flattenListEachChr(results)
@staticmethod
def mergeKeysValuesListsCountStat2(cls, data):
return data
@staticmethod
def mergeKeysValuesListsCountSegmentStat(cls, data):
results = cls.replaceItem(data)
return results
@expose_api_anonymous
def selectedTrack(self, trans, **kwd):
results = kwd.get('fired_button')
return results
@expose_api_anonymous
def question(self, trans, **kwd):
results = self.resultRunAnalysis(**kwd)
return results
@expose_api_anonymous
def index( self, trans, **kwd ):
return self.resultRunAnalysis(**kwd)