1. Fix problem with slow servlets (wrong forming CVS line)
[proteocache.git] / server / compbio / controllers / SSFeaturesController.java
1 package compbio.controllers;
2
3 import java.util.Map;
4
5 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RequestMethod;
8 import org.springframework.web.bind.annotation.RequestParam;
9
10 import compbio.statistic.CassandraRequester;
11
12 /**
13  * Spring controller for protein secondary structure features. This version
14  * works in the servlet style.
15  * 
16  * @author Alexander Sherstnev
17  * @author Natasha Sherstneva
18  * 
19  * @since 0.5
20  * @version 1.0 December 2013
21  */
22 @Controller
23 @RequestMapping("/features")
24 public class SSFeaturesController extends BasicController {
25
26         /**
27          * form a query page for protein secondary structure feaatures: Proteins
28          * with given fraction of H/E (helix/beta sheets)
29          * 
30          * @param model
31          *            MVC model object
32          * 
33          * @return link to the report JSP page
34          */
35         @RequestMapping(value = "query", method = RequestMethod.GET)
36         public String formCounterQuery(Map<String, Object> model) {
37                 model.put("username", getPrincipalName());
38                 model.put("type", 'E');
39                 model.put("value", 50);
40                 return "query/SSFeatures";
41         }
42
43         /**
44          * form a results page for protein secondary structure features: Proteins
45          * with given fraction of H/E (helix/beta sheets) All proteins with E/H
46          * higher than a given percent of the length are returned
47          * 
48          * @param model
49          *            MVC model object
50          * @param typeFeature
51          *            type of SS: H/E (helix/beta sheets)
52          * @param percent
53          *            fraction of the protein length predicted as H/E (helix/beta
54          *            sheets)
55          * 
56          * @return link to the report JSP page
57          */
58         @RequestMapping(value = "results", method = RequestMethod.GET)
59         public String countSequences(@RequestParam("TypeFeatures") String typeFeature, @RequestParam("Percent") String percent,
60                         Map<String, Object> model) {
61                 model.put("username", getPrincipalName());
62                 final long startTime = System.currentTimeMillis();
63
64                 if (percent.equals("")) {
65                         model.put("error", "The value must not be empty");
66                         model.put("type", typeFeature);
67                         model.put("value", percent);
68                         return "query/SSFeatures";
69                 }
70
71                 int realpercent;
72                 try {
73                         realpercent = Integer.parseInt(percent.trim());
74                 } catch (NumberFormatException e) {
75                         model.put("error", "The value must be an integer number");
76                         model.put("value", percent);
77                         return "query/SSFeatures";
78                 }
79
80                 if (realpercent < 1) {
81                         model.put("error", "The value must be greater than 0");
82                         model.put("value", percent);
83                         return "query/SSFeatures";
84                 }
85
86                 if (realpercent > 100) {
87                         model.put("error", "The value must be less than 100");
88                         model.put("value", percent);
89                         return "query/SSFeatures";
90                 }
91
92                 CassandraRequester cr = new CassandraRequester();
93                 Map<String, String> r = cr.readProteinsPrediction(typeFeature, realpercent);
94                 model.put("results", r);
95                 model.put("njobs", 0);
96                 StringBuilder csvline = new StringBuilder("");
97                 if (null != r) {
98                         model.put("njobs", r.size());
99                         csvline.append("\'Prediction%20number\',\'Protein%20Sequence\', \'Secondary%20Structure%20Prediction\'%0A");
100
101                         // form line for CSV file
102                         int counter = 1;
103                         for (Map.Entry<String, String> entry : r.entrySet()) {
104                                 csvline.append("\'" + counter + "\',\'" + entry.getKey() + "\',\'" + entry.getValue() + "\'%0A");
105                                 ++counter;
106                         }
107                 }
108                 model.put("csvfile", csvline.toString());
109
110                 final long endTime = System.currentTimeMillis();
111                 model.put("timeExecution", (endTime - startTime));
112                 model.put("feature", typeFeature);
113                 model.put("percent", realpercent);
114                 return "reports/SSFeatures";
115         }
116
117 }