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