Merge branch 'DAO'
[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.beans.ProteinBean;
14 import compbio.beans.TotalByCounterBean;
15 import compbio.cassandra.readers.ReaderByCounter;
16 import compbio.cassandra.readers.SequenceReader;
17
18 /**
19  * Spring controller for sequence queries. This version works in the servlet
20  * style.
21  * 
22  * @author Alexander Sherstnev
23  * @author Natasha Sherstneva
24  * 
25  * @since 0.5
26  * @version 1.0 December 2013
27  */
28 @Controller
29 @RequestMapping("/sequence")
30 public class SequenceController extends BasicController {
31
32         /**
33          * pattern for NON-protein alphabet symbols
34          */
35         private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
36
37         /**
38          * form a query page for search protein sequence. The servlet should be
39          * available for users and admins only. 
40          * 
41          * @param model
42          *            MVC model
43          * @return link to the JSP query page
44          */
45         @RequestMapping(value = "query", method = RequestMethod.GET)
46         public String formSequenceQuery(Map<String, Object> model) {
47                 model.put("username", getPrincipalName());
48                 model.put("value", "AAAAA");
49                 return "query/Sequence";
50         }
51
52         /**
53          * form a query page for statistics: Protein by job count. The servlet should be
54          * available for users and admins only. 
55          * 
56          * @param model
57          *            MVC model
58          * @return link to the JSP query page
59          */
60         @RequestMapping(value = "counts/query", method = RequestMethod.GET)
61         public String formCounterQuery(Map<String, Object> model) {
62                 model.put("username", getPrincipalName());
63                 model.put("value", 5);
64                 return "query/SequenceCounts";
65         }
66
67         /**
68          * form a report page for search protein sequence.
69          * 
70          * @param model
71          *            MVC model object
72          * @param sequence
73          *            protein sequence or part of sequence
74          * @param searchtype
75          *            defined whether the whole sequence or part of sequence would be searched
76          * @return link to the report JSP page
77          */
78         @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
79         public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
80                         Map<String, Object> model) {
81                 model.put("username", getPrincipalName());
82                 final long startTime = System.currentTimeMillis();
83
84                 // input checks
85                 String trimmedsequence = sequence.replaceAll("\\s", "");
86                 if (trimmedsequence.equalsIgnoreCase("")) {
87                         model.put("error", "The sequence cann't be empty");
88                         model.put("value", sequence);
89                         return "query/Sequence";
90                 }
91                 if (NONPROTEIN.matcher(trimmedsequence).find()) {
92                         model.put("error", "The sequence contains symbols not from the standard protein alphabet");
93                         model.put("value", sequence);
94                         return "query/Sequence";
95                 }
96
97                 model.put("njobs", 0);
98                 model.put("prot", trimmedsequence);
99                 model.put("searchtype", searchtype);
100                 String csvline = "";
101                 if (0 < trimmedsequence.length()) {
102                         SequenceReader reader = new SequenceReader();
103                         List<ProteinBean> result = reader.readProteins(trimmedsequence, searchtype);
104                         model.put("results", result);
105                         if (null != result) {
106                                 if (searchtype.equals("whole"))
107                                         model.put("njobs", result.get(0).getJobid().size());
108                                 else
109                                         model.put("njobs", result.size());
110                                 csvline = "\'Job\',\'Annotation\',\'Sequence\'%0A";
111                         }
112                         // form CSV file string
113                         for (ProteinBean entry : result) {
114                                 List<String> jobs = entry.getJobid();
115                                 String protein = entry.getSequence();
116                                 LinkedHashMap<String, String> predictions = entry.getPredictions();
117                                 for (String job : jobs) {
118                                         csvline += "\'" + job + "\',\'Sequence\',\'" + protein + "\',\'%0A";
119                                         for (Map.Entry<String, String> pr : predictions.entrySet()) {
120                                                 csvline += "\'\',\'" + pr.getKey() + "\',\'" + pr.getValue() + "\'%0A";
121                                         }
122                                 }
123                         }
124                 }
125                 model.put("csvfile", csvline);
126
127                 final long endTime = System.currentTimeMillis();
128                 model.put("timeExecution", (endTime - startTime));
129                 return "reportProteinSequences";
130         }
131
132         /**
133          * form a report page for statistics: Protein by job count.
134          * 
135          * @param model
136          *            MVC model object
137          * @param counter
138          *           
139          * @return link to the report JSP page
140          */
141         @RequestMapping(value = "counts/results", method = RequestMethod.GET)
142         public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
143                 model.put("username", getPrincipalName());
144                 final long startTime = System.currentTimeMillis();
145
146                 if (counter.equals("")) {
147                         model.put("error", "The value must not be empty");
148                         model.put("value", counter);
149                         return "query/SequenceCounts";
150                 }
151
152                 int realcounter;
153                 try {
154                         realcounter = Integer.parseInt(counter.trim());
155                 } catch (NumberFormatException e) {
156                         model.put("error", "The value must be an integer number");
157                         model.put("value", counter);
158                         return "query/SequenceCounts";
159                 }
160
161                 if (realcounter < 1) {
162                         model.put("error", "The value must be greater than 0");
163                         model.put("value", counter);
164                         return "query/SequenceCounts";
165                 }
166
167
168                 ReaderByCounter reader = new ReaderByCounter();
169                 List<TotalByCounterBean> r = reader.readProteinByCounter(realcounter);
170                 model.put("results", r);
171                 model.put("njobs", 0);
172                 String csvline = "";
173                 if (null != r) {
174                         model.put("njobs", r.size());
175                         csvline = "\'Job%20 count\', \'Protein%20Sequence\'%0A";
176                 }
177                 // form line for CSV file
178
179                 for (TotalByCounterBean b : r) {
180                         if (b.getName().equals("")) {
181                                 csvline += "\'" + b.getTotaljobs() + "\',\'Alignment%20job\'%0A";
182                                 // fix problem with records without protein sequence (alignment
183                                 // jobs)
184                                 b.setName("Alignment job");
185                         } else {
186                                 csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
187                         }
188                 }
189                 model.put("csvfile", csvline);
190
191                 model.put("results", r);
192                 final long endTime = System.currentTimeMillis();
193                 model.put("timeExecution", (endTime - startTime));
194                 model.put("counter", realcounter);
195                 return "reportProteinSequencesCounter";
196         }
197
198 }