df4c98720e03b1936a2a2653aa919a50ccb354e1
[proteocache.git] / server / compbio / controllers / SequenceController.java
1 package compbio.controllers;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.regex.Pattern;
6
7 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestMethod;
10 import org.springframework.web.bind.annotation.RequestParam;
11
12 import compbio.cassandra.ProteinBean;
13 import compbio.cassandra.TotalByCounterBean;
14 import compbio.statistic.CassandraRequester;
15
16 /**
17  * Spring controller for sequence queries. This version works in the servlet
18  * style.
19  * 
20  * @author Alexander Sherstnev
21  * @author Natasha Sherstneva
22  * 
23  * @since 0.5
24  * @version 1.0 December 2013
25  */
26 @Controller
27 @RequestMapping("/sequence")
28 public class SequenceController extends BasicController {
29
30         /**
31          * pattern for NON-protein alphabet symbols
32          */
33         private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
34
35         @RequestMapping(value = "query", method = RequestMethod.GET)
36         public String formSequenceQuery(Map<String, Object> model) {
37                 model.put("username", getPrincipalName());
38                 model.put("value", "AAAAA");
39                 return "query/Sequence";
40         }
41
42         @RequestMapping(value = "counts/query", method = RequestMethod.GET)
43         public String formCounterQuery(Map<String, Object> model) {
44                 model.put("username", getPrincipalName());
45                 model.put("value", 5);
46                 return "query/SequenceCounts";
47         }
48
49         @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
50         public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
51                         Map<String, Object> model) {
52                 model.put("username", getPrincipalName());
53                 final long startTime = System.currentTimeMillis();
54
55                 // input checks
56                 String trimmedsequence = sequence.replaceAll("\\s", "");
57                 if (trimmedsequence.equalsIgnoreCase("")) {
58                         model.put("error", "The sequence cann't be empty");
59                         model.put("value", sequence);
60                         return "query/Sequence";
61                 }
62                 if (NONPROTEIN.matcher(trimmedsequence).find()) {
63                         model.put("error", "The sequence contains symbols not from the standard protein alphabet");
64                         model.put("value", sequence);
65                         return "query/Sequence";
66                 }
67
68                 model.put("njobs", 0);
69                 model.put("prot", trimmedsequence);
70                 model.put("searchtype", searchtype);
71
72                 if (0 < trimmedsequence.length()) {
73                         CassandraRequester cr = new CassandraRequester();
74                         List<ProteinBean> r = cr.readProteins(trimmedsequence, searchtype);
75                         model.put("results", r);
76                         if (null != r) {
77                                 if (searchtype.equals("whole"))
78                                         model.put("njobs", r.get(0).getJobid().size());
79                                 else
80                                         model.put("njobs", r.size());
81                         }
82                 }
83                 final long endTime = System.currentTimeMillis();
84                 model.put("timeExecution", (endTime - startTime));
85                 return "reportProteinSequences";
86         }
87
88         @RequestMapping(value = "counts/results", method = RequestMethod.GET)
89         public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
90                 model.put("username", getPrincipalName());
91                 final long startTime = System.currentTimeMillis();
92
93                 if (counter.equals("")) {
94                         model.put("error", "The value must not be empty");
95                         model.put("value", counter);
96                         return "query/SequenceCounts";
97                 }
98
99                 int realcounter;
100                 try {
101                         realcounter = Integer.parseInt(counter.trim());
102                 } catch (NumberFormatException e) {
103                         model.put("error", "The value must be an integer number");
104                         model.put("value", counter);
105                         return "query/SequenceCounts";
106                 }
107
108                 if (realcounter < 1) {
109                         model.put("error", "The value must be greater than 0");
110                         model.put("value", counter);
111                         return "query/SequenceCounts";
112                 }
113
114                 CassandraRequester cr = new CassandraRequester();
115                 List<TotalByCounterBean> r = cr.readProteinByCounter(realcounter);
116                 model.put("results", r);
117                 model.put("njobs", 0);
118                 if (null != r) {
119                         model.put("njobs", r.size());
120                 }
121                 final long endTime = System.currentTimeMillis();
122                 model.put("timeExecution", (endTime - startTime));
123                 model.put("counter", realcounter);
124                 return "reportProteinSequencesCounter";
125         }
126
127 }