Form CSV file if results are not null
[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
54          * should be 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
76          *            be searched
77          * @return link to the report JSP page
78          */
79         @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
80         public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
81                         Map<String, Object> model) {
82                 model.put("username", getPrincipalName());
83                 final long startTime = System.currentTimeMillis();
84
85                 // input checks
86                 String trimmedsequence = sequence.replaceAll("\\s", "");
87                 if (trimmedsequence.equalsIgnoreCase("")) {
88                         model.put("error", "The sequence cann't be empty");
89                         model.put("value", sequence);
90                         return "query/Sequence";
91                 }
92                 if (NONPROTEIN.matcher(trimmedsequence).find()) {
93                         model.put("error", "The sequence contains symbols not from the standard protein alphabet");
94                         model.put("value", sequence);
95                         return "query/Sequence";
96                 }
97
98                 model.put("njobs", 0);
99                 model.put("prot", trimmedsequence);
100                 model.put("searchtype", searchtype);
101                 String csvline = "";
102                 if (0 < trimmedsequence.length()) {
103                         SequenceReader reader = new SequenceReader();
104                         List<ProteinBean> result = reader.readProteins(trimmedsequence, searchtype);
105                         model.put("results", result);
106                         if (null != result) {
107                                 if (searchtype.equals("whole"))
108                                         model.put("njobs", result.get(0).getJobid().size());
109                                 else
110                                         model.put("njobs", result.size());
111                                 csvline = "\'Job\',\'Annotation\',\'Sequence\'%0A";
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                 }
126                 model.put("csvfile", csvline);
127
128                 final long endTime = System.currentTimeMillis();
129                 model.put("timeExecution", (endTime - startTime));
130                 return "reports/Sequences";
131         }
132
133         /**
134          * form a report page for statistics: Protein by job count.
135          * 
136          * @param model
137          *            MVC model object
138          * @param counter
139          * 
140          * @return link to the report JSP page
141          */
142         @RequestMapping(value = "counts/results", method = RequestMethod.GET)
143         public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
144                 model.put("username", getPrincipalName());
145                 final long startTime = System.currentTimeMillis();
146
147                 if (counter.equals("")) {
148                         model.put("error", "The value must not be empty");
149                         model.put("value", counter);
150                         return "query/SequenceCounts";
151                 }
152
153                 int realcounter;
154                 try {
155                         realcounter = Integer.parseInt(counter.trim());
156                 } catch (NumberFormatException e) {
157                         model.put("error", "The value must be an integer number");
158                         model.put("value", counter);
159                         return "query/SequenceCounts";
160                 }
161
162                 if (realcounter < 1) {
163                         model.put("error", "The value must be greater than 0");
164                         model.put("value", counter);
165                         return "query/SequenceCounts";
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 "reports/SequencesStatistics";
196         }
197
198 }