move jsp for reports to a separate dir
[proteocache.git] / server / compbio / controllers / SequenceController.java
index ee71258..6db97ef 100644 (file)
@@ -1,6 +1,6 @@
 package compbio.controllers;
 
-import java.security.Principal;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
@@ -10,9 +10,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import compbio.cassandra.ProteinBean;
-import compbio.cassandra.TotalByCounterBean;
-import compbio.statistic.CassandraRequester;
+import compbio.beans.ProteinBean;
+import compbio.beans.TotalByCounterBean;
+import compbio.cassandra.readers.ReaderByCounter;
+import compbio.cassandra.readers.SequenceReader;
 
 /**
  * Spring controller for sequence queries. This version works in the servlet
@@ -26,30 +27,59 @@ import compbio.statistic.CassandraRequester;
  */
 @Controller
 @RequestMapping("/sequence")
-public class SequenceController {
+public class SequenceController extends BasicController {
 
        /**
         * pattern for NON-protein alphabet symbols
         */
        private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
 
+       /**
+        * form a query page for search protein sequence. The servlet should be
+        * available for users and admins only.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "query", method = RequestMethod.GET)
-       public String formSequenceQuery(Map<String, Object> model, Principal principal) {
-               model.put("username", principal.getName());
+       public String formSequenceQuery(Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                model.put("value", "AAAAA");
-               return "queryProteinSequence";
+               return "query/Sequence";
        }
 
+       /**
+        * form a query page for statistics: Protein by job count. The servlet
+        * should be available for users and admins only.
+        * 
+        * @param model
+        *            MVC model
+        * @return link to the JSP query page
+        */
        @RequestMapping(value = "counts/query", method = RequestMethod.GET)
-       public String formCounterQuery(Map<String, Object> model, Principal principal) {
-               model.put("username", principal.getName());
+       public String formCounterQuery(Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                model.put("value", 5);
-               return "queryProteinSequenceCounter";
+               return "query/SequenceCounts";
        }
 
+       /**
+        * form a report page for search protein sequence.
+        * 
+        * @param model
+        *            MVC model object
+        * @param sequence
+        *            protein sequence or part of sequence
+        * @param searchtype
+        *            defined whether the whole sequence or part of sequence would
+        *            be searched
+        * @return link to the report JSP page
+        */
        @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
-       public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("protein") String flag, Map<String, Object> model, Principal principal) {
-               model.put("username", principal.getName());
+       public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
+                       Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
 
                // input checks
@@ -57,43 +87,67 @@ public class SequenceController {
                if (trimmedsequence.equalsIgnoreCase("")) {
                        model.put("error", "The sequence cann't be empty");
                        model.put("value", sequence);
-                       return "queryProteinSequence";
+                       return "query/Sequence";
                }
                if (NONPROTEIN.matcher(trimmedsequence).find()) {
                        model.put("error", "The sequence contains symbols not from the standard protein alphabet");
                        model.put("value", sequence);
-                       return "queryProteinSequence";
+                       return "query/Sequence";
                }
 
                model.put("njobs", 0);
                model.put("prot", trimmedsequence);
-               model.put("flag", flag);
-
+               model.put("searchtype", searchtype);
+               String csvline = "";
                if (0 < trimmedsequence.length()) {
-                       CassandraRequester cr = new CassandraRequester();
-                       List<ProteinBean> r = cr.readProteins(trimmedsequence, flag);
-                       model.put("results", r);
-                       if (null != r) {
-                               if (flag.equals("whole"))
-                                       model.put("njobs", r.get(0).getJobid().size());
+                       SequenceReader reader = new SequenceReader();
+                       List<ProteinBean> result = reader.readProteins(trimmedsequence, searchtype);
+                       model.put("results", result);
+                       if (null != result) {
+                               if (searchtype.equals("whole"))
+                                       model.put("njobs", result.get(0).getJobid().size());
                                else
-                                       model.put("njobs", r.size());
+                                       model.put("njobs", result.size());
+                               csvline = "\'Job\',\'Annotation\',\'Sequence\'%0A";
+                       }
+                       // form CSV file string
+                       for (ProteinBean entry : result) {
+                               List<String> jobs = entry.getJobid();
+                               String protein = entry.getSequence();
+                               LinkedHashMap<String, String> predictions = entry.getPredictions();
+                               for (String job : jobs) {
+                                       csvline += "\'" + job + "\',\'Sequence\',\'" + protein + "\',\'%0A";
+                                       for (Map.Entry<String, String> pr : predictions.entrySet()) {
+                                               csvline += "\'\',\'" + pr.getKey() + "\',\'" + pr.getValue() + "\'%0A";
+                                       }
+                               }
                        }
                }
+               model.put("csvfile", csvline);
+
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
-               return "reportProteinSequences";
+               return "reports/Sequences";
        }
 
+       /**
+        * form a report page for statistics: Protein by job count.
+        * 
+        * @param model
+        *            MVC model object
+        * @param counter
+        * 
+        * @return link to the report JSP page
+        */
        @RequestMapping(value = "counts/results", method = RequestMethod.GET)
-       public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model, Principal principal) {
-               model.put("username", principal.getName());
+       public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
+               model.put("username", getPrincipalName());
                final long startTime = System.currentTimeMillis();
 
                if (counter.equals("")) {
                        model.put("error", "The value must not be empty");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/SequenceCounts";
                }
 
                int realcounter;
@@ -102,26 +156,43 @@ public class SequenceController {
                } catch (NumberFormatException e) {
                        model.put("error", "The value must be an integer number");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/SequenceCounts";
                }
 
                if (realcounter < 1) {
                        model.put("error", "The value must be greater than 0");
                        model.put("value", counter);
-                       return "queryIPStatistics";
+                       return "query/SequenceCounts";
                }
 
-               CassandraRequester cr = new CassandraRequester();
-               List<TotalByCounterBean> r = cr.readProteinByCounter(realcounter);
+               ReaderByCounter reader = new ReaderByCounter();
+               List<TotalByCounterBean> r = reader.readProteinByCounter(realcounter);
                model.put("results", r);
                model.put("njobs", 0);
+               String csvline = "";
                if (null != r) {
                        model.put("njobs", r.size());
+                       csvline = "\'Job%20 count\', \'Protein%20Sequence\'%0A";
                }
+               // form line for CSV file
+
+               for (TotalByCounterBean b : r) {
+                       if (b.getName().equals("")) {
+                               csvline += "\'" + b.getTotaljobs() + "\',\'Alignment%20job\'%0A";
+                               // fix problem with records without protein sequence (alignment
+                               // jobs)
+                               b.setName("Alignment job");
+                       } else {
+                               csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
+                       }
+               }
+               model.put("csvfile", csvline);
+
+               model.put("results", r);
                final long endTime = System.currentTimeMillis();
                model.put("timeExecution", (endTime - startTime));
                model.put("counter", realcounter);
-               return "reportProteinSequencesCounter";
+               return "reports/SequencesStatistics";
        }
 
 }