Attempt to resolve the problem with java on cluster...
[proteocache.git] / server / compbio / controllers / SequenceController.java
1 package compbio.controllers;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.regex.Pattern;
7
8 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestMethod;
11 import org.springframework.web.bind.annotation.RequestParam;
12
13 import compbio.cassandra.DataBase;
14 import compbio.cassandra.ProteinBean;
15 import compbio.cassandra.Total;
16 import compbio.cassandra.TotalByCounterBean;
17 import compbio.statistic.CassandraRequester;
18
19 /**
20  * Spring controller for sequence queries. This version works in the servlet
21  * style.
22  * 
23  * @author Alexander Sherstnev
24  * @author Natasha Sherstneva
25  * 
26  * @since 0.5
27  * @version 1.0 December 2013
28  */
29 @Controller
30 @RequestMapping("/sequence")
31 public class SequenceController extends BasicController {
32
33         /**
34          * pattern for NON-protein alphabet symbols
35          */
36         private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
37
38         @RequestMapping(value = "query", method = RequestMethod.GET)
39         public String formSequenceQuery(Map<String, Object> model) {
40                 model.put("username", getPrincipalName());
41                 model.put("value", "AAAAA");
42                 return "query/Sequence";
43         }
44
45         @RequestMapping(value = "counts/query", method = RequestMethod.GET)
46         public String formCounterQuery(Map<String, Object> model) {
47                 model.put("username", getPrincipalName());
48                 model.put("value", 5);
49                 return "query/SequenceCounts";
50         }
51
52         @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
53         public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
54                         Map<String, Object> model) {
55                 model.put("username", getPrincipalName());
56                 final long startTime = System.currentTimeMillis();
57
58                 // input checks
59                 String trimmedsequence = sequence.replaceAll("\\s", "");
60                 if (trimmedsequence.equalsIgnoreCase("")) {
61                         model.put("error", "The sequence cann't be empty");
62                         model.put("value", sequence);
63                         return "query/Sequence";
64                 }
65                 if (NONPROTEIN.matcher(trimmedsequence).find()) {
66                         model.put("error", "The sequence contains symbols not from the standard protein alphabet");
67                         model.put("value", sequence);
68                         return "query/Sequence";
69                 }
70
71                 model.put("njobs", 0);
72                 model.put("prot", trimmedsequence);
73                 model.put("searchtype", searchtype);
74                 String csvline = "";
75                 if (0 < trimmedsequence.length()) {
76                         CassandraRequester cr = new CassandraRequester();
77                         List<ProteinBean> r = cr.readProteins(trimmedsequence, searchtype);
78                         model.put("results", r);
79                         if (null != r) {
80                                 if (searchtype.equals("whole"))
81                                         model.put("njobs", r.get(0).getJobid().size());
82                                 else
83                                         model.put("njobs", r.size());
84                                 csvline = "\'Job\',\'Annotation\',\'Sequence\'%0A";
85                         }
86                         // form CSV file string
87                         for (ProteinBean entry : r) {
88                                 List<String> jobs = entry.getJobid();
89                                 String protein = entry.getSequence();
90                                 LinkedHashMap<String, String> predictions = entry.getPredictions();
91                                 for (String job : jobs) {
92                                         csvline += "\'" + job + "\',\'Sequence\',\'" + protein + "\',\'%0A";
93                                         for (Map.Entry<String, String> pr : predictions.entrySet()) {
94                                                 csvline += "\'\',\'" + pr.getKey() + "\',\'" + pr.getValue() + "\'%0A";
95                                         }
96                                 }
97                         }
98                 }
99                 model.put("csvfile", csvline);
100
101                 final long endTime = System.currentTimeMillis();
102                 model.put("timeExecution", (endTime - startTime));
103                 return "reportProteinSequences";
104         }
105
106         @RequestMapping(value = "counts/results", method = RequestMethod.GET)
107         public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
108                 model.put("username", getPrincipalName());
109                 final long startTime = System.currentTimeMillis();
110
111                 if (counter.equals("")) {
112                         model.put("error", "The value must not be empty");
113                         model.put("value", counter);
114                         return "query/SequenceCounts";
115                 }
116
117                 int realcounter;
118                 try {
119                         realcounter = Integer.parseInt(counter.trim());
120                 } catch (NumberFormatException e) {
121                         model.put("error", "The value must be an integer number");
122                         model.put("value", counter);
123                         return "query/SequenceCounts";
124                 }
125
126                 if (realcounter < 1) {
127                         model.put("error", "The value must be greater than 0");
128                         model.put("value", counter);
129                         return "query/SequenceCounts";
130                 }
131
132                 CassandraRequester cr = new CassandraRequester();
133                 List<TotalByCounterBean> r = cr.readProteinByCounter(realcounter);
134                 model.put("njobs", 0);
135                 String csvline = "";
136                 if (null != r) {
137                         model.put("njobs", r.size());
138                         csvline = "\'Job%20 count\', \'Protein%20Sequence\'%0A";
139                 }
140                 // form line for CSV file
141
142                 for (TotalByCounterBean b : r) {
143                         if (b.getName().equals("")) {
144                                 csvline += "\'" + b.getTotaljobs() + "\',\'Alignment%20job\'%0A";
145                                 // fix problem with records without protein sequence (alignment
146                                 // jobs)
147                                 b.setName("Alignment job");
148                         } else {
149                                 csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
150                         }
151                 }
152                 model.put("csvfile", csvline);
153
154                 model.put("results", r);
155                 final long endTime = System.currentTimeMillis();
156                 model.put("timeExecution", (endTime - startTime));
157                 model.put("counter", realcounter);
158                 return "reportProteinSequencesCounter";
159         }
160
161 }