Initial port to python3
[htsworkflow.git] / htsworkflow / frontend / analysis / main.py
1 # some core functions of analysis manager module
2
3 from datetime import datetime
4 from string import *
5 import re
6
7 from django.conf import settings
8 from django.core.exceptions import ObjectDoesNotExist
9 from django.http import HttpResponse
10
11 from htsworkflow.frontend.analysis.models import Task, Project
12
13 def updStatus(request):
14     ClIP = request.META['REMOTE_ADDR']
15     #Check client access permission                                                                                                                                       
16     granted = False
17     if (ClIP in settings.ALLOWED_ANALYS_IPS):  granted = True
18     if not granted: return HttpResponse("access denied.")
19
20     output=''
21     taskid=-1;
22     # Check required param
23     if 'taskid' in request: taskid = request['taskid']
24     else:  return HttpResponse('missing param task id')
25
26     try:
27       rec = Task.objects.get(id=taskid)
28       mytimestamp = datetime.now().__str__()
29       mytimestamp = re.sub(pattern=":[^:]*$",repl="",string=mytimestamp)
30       if 'msg' in request:
31         rec.task_status += ", "+request['msg']+" ("+mytimestamp+")"
32       else :
33         rec.task_status = "Registered ("+mytimestamp+")"
34       rec.save()
35       output = "Hello "+settings.ALLOWED_ANALYS_IPS[ClIP]+". Updated status for task "+taskid
36     except ObjectDoesNotExist:
37       output = "entry not found: taskid="+taskid
38
39     return HttpResponse(output)
40     
41       
42 def getProjects(request):
43     ClIP = request.META['REMOTE_ADDR']
44     #Check client access permission 
45     granted = False
46     if (ClIP in settings.ALLOWED_ANALYS_IPS):  granted = True
47     if not granted: return HttpResponse("access denied.")
48
49     outputfile = ''
50     
51     All=False
52     if ('mode' in request):
53       if request['mode']=='all':
54         All=True
55
56     try:                                                                                   
57       if(All):
58         rec = Project.objects.all().distinct()
59       else:
60         rec = Project.objects.filter(tasks__task_status__exact='defined').distinct()
61       
62       outputfile = '<?xml version="1.0" ?>'
63       outputfile += '\n<Projects Client="'+settings.ALLOWED_ANALYS_IPS[ClIP]+'">'
64       for p in rec:
65         outputfile += '\n'
66         outputfile += '\n<Project ProjectId="'+p.id.__str__()+'" Name="'+p.project_name+'">'
67         prj_tasks = p.tasks.all()
68         for t in prj_tasks:
69           outputfile += '\n'
70           if (t.apply_calc == 'QuEST' or t.apply_calc == 'WingPeaks' or t.apply_calc == 'MACS'):
71             outputfile += '\n<PeakCalling TaskId="'+t.id.__str__()+'" Name="'+t.task_name+'" Caller="'+t.apply_calc+'" Genome="'+t.subject1.library_species.use_genome_build+'">'
72             if t.subject1:
73               outputfile += '\n<Signal Library="'+t.subject1.id+'"/>'
74               if t.subject2:
75                 outputfile += '\n<Background Library="'+t.subject2.id+'"/>'
76               else:
77                 outputfile += '\n<Err>Background Library Missing</Err>'
78             else:
79               outputfile += '\n<Err>Signal Library Missing</Err>'
80             outputfile += '\n<params>'+t.task_params.__str__()+'</params>'
81             outputfile += '\n</PeakCalling>'
82           
83           if (t.apply_calc == 'Methylseq'):
84             outputfile += '\n<Methylseq TaskId="'+t.id.__str__()+'" Name="'+t.task_name+'" Genome="'+t.subject1.library_species.use_genome_build+'">'
85             if t.subject1:
86               outputfile += '\n<Hpa2 Library="'+t.subject1.id+'"/>'
87               if t.subject2:
88                 outputfile += '\n<Msp1 Library="'+t.subject2.id+'"/>'
89               else:
90                 outputfile += '\n<Err>Msp1 Library Missing</Err>'
91             else:
92               outputfile += '\n<Err>Hpa2 Library Missing</Err>'
93             outputfile += '\n<params>'+t.task_params.__str__()+'</params>'
94             outputfile += '\n</Methylseq>' 
95
96           if (t.apply_calc == 'ProfileReads' or t.apply_calc == 'qPCR'):
97             outputfile += '\n<'+t.apply_calc+' TaskId="'+t.id.__str__()+'" Name="'+t.task_name+'" Genome="'+t.subject1.library_species.use_genome_build+'" Library="'+t.subject1.id+'"/>'
98
99           if (t.apply_calc == 'CompareLibs'):
100             outputfile += '\n<CompareLibraries TaskId="'+t.id.__str__()+'" TF="'+t.task_name+'" Genome="'+t.subject1.library_species.use_genome_build+'">'
101             if t.subject1:
102               outputfile += '\n<Library Library="'+t.subject1.id+'"/>'
103             else:
104               outputfile += '\n<Err>Library Missing</Err>'
105             if t.subject2:
106               outputfile += '\n<Library Library="'+t.subject2.id+'"/>'
107             else:
108               outputfile += '\n<Err>Library Missing</Err>'
109             outputfile += '\n<params>'+t.task_params.__str__()+'</params>'
110             outputfile += '\n</CompareLibraries>'
111
112           #if (t.apply_calc == 'ComparePeakCalls'):                                                                                                                            
113           # <ComparePeakCalls Genome="hg18" Caller1="QuEST" Set1="A549 GR Dex ChIP" Caller2="QuEST" Set2="A549 GR EtOH ChIP" />                                                
114           # outputfile += '\n<ComparePeakCalls TaskId='+t.id.__str__()+' Genome="'+t.subject1.library_species.use_genome_build+'" Caller1="'+t.pcaller1+'" Caller1="'+t.pcaller1+'" Caller2="'+t.pcaller2+'" Set1="'+t.set1+'" Set1="'+t.set2+'"/>' 
115           # TO DO: Define these new fields in Task: PCaller1 (QuEST,WingPeaks), PCaller2, Set1(FK to self), Set2 (FK..) ALL NULL=TRUE                                  
116         outputfile += '\n</Project>'
117       outputfile += '\n</Projects>'
118     except ObjectDoesNotExist:
119       outputfile = "<?xml version='1.0' ?><Projects></Projects>"
120
121     return HttpResponse(outputfile, mimetype='text/plain')