1 package compbio.controllers;
3 import java.util.LinkedHashMap;
6 import java.util.regex.Pattern;
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;
13 import compbio.cassandra.DataBase;
14 import compbio.cassandra.ProteinBean;
15 import compbio.cassandra.Total;
16 import compbio.cassandra.TotalByCounterBean;
17 import compbio.statistic.CassandraRequester;
20 * Spring controller for sequence queries. This version works in the servlet
23 * @author Alexander Sherstnev
24 * @author Natasha Sherstneva
27 * @version 1.0 December 2013
30 @RequestMapping("/sequence")
31 public class SequenceController extends BasicController {
34 * pattern for NON-protein alphabet symbols
36 private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
38 @RequestMapping(value = "query", method = RequestMethod.GET)
39 public String formSequenceQuery(Map<String, Object> model) {
40 model.put("username", getPrincipalName());
41 model.put("value", "AAAAA");
42 return "query/Sequence";
45 @RequestMapping(value = "counts/query", method = RequestMethod.GET)
46 public String formCounterQuery(Map<String, Object> model) {
47 model.put("username", getPrincipalName());
48 model.put("value", 5);
49 return "query/SequenceCounts";
52 @RequestMapping(value = "sequence/results", method = RequestMethod.GET)
53 public String findSequence(@RequestParam("sequence") String sequence, @RequestParam("searchtype") String searchtype,
54 Map<String, Object> model) {
55 model.put("username", getPrincipalName());
56 final long startTime = System.currentTimeMillis();
59 String trimmedsequence = sequence.replaceAll("\\s", "");
60 if (trimmedsequence.equalsIgnoreCase("")) {
61 model.put("error", "The sequence cann't be empty");
62 model.put("value", sequence);
63 return "query/Sequence";
65 if (NONPROTEIN.matcher(trimmedsequence).find()) {
66 model.put("error", "The sequence contains symbols not from the standard protein alphabet");
67 model.put("value", sequence);
68 return "query/Sequence";
71 model.put("njobs", 0);
72 model.put("prot", trimmedsequence);
73 model.put("searchtype", searchtype);
75 if (0 < trimmedsequence.length()) {
76 CassandraRequester cr = new CassandraRequester();
77 List<ProteinBean> r = cr.readProteins(trimmedsequence, searchtype);
78 model.put("results", r);
80 if (searchtype.equals("whole"))
81 model.put("njobs", r.get(0).getJobid().size());
83 model.put("njobs", r.size());
84 csvline = "\'Job\',\'Annotation\',\'Sequence\'%0A";
86 // form CSV file string
87 for (ProteinBean entry : r) {
88 List<String> jobs = entry.getJobid();
89 String protein = entry.getSequence();
90 LinkedHashMap<String, String> predictions = entry.getPredictions();
91 for (String job : jobs) {
92 csvline += "\'" + job + "\',\'Sequence\',\'" + protein + "\',\'%0A";
93 for (Map.Entry<String, String> pr : predictions.entrySet()) {
94 csvline += "\'\',\'" + pr.getKey() + "\',\'" + pr.getValue() + "\'%0A";
99 model.put("csvfile", csvline);
101 final long endTime = System.currentTimeMillis();
102 model.put("timeExecution", (endTime - startTime));
103 return "reportProteinSequences";
106 @RequestMapping(value = "counts/results", method = RequestMethod.GET)
107 public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
108 model.put("username", getPrincipalName());
109 final long startTime = System.currentTimeMillis();
111 if (counter.equals("")) {
112 model.put("error", "The value must not be empty");
113 model.put("value", counter);
114 return "query/SequenceCounts";
119 realcounter = Integer.parseInt(counter.trim());
120 } catch (NumberFormatException e) {
121 model.put("error", "The value must be an integer number");
122 model.put("value", counter);
123 return "query/SequenceCounts";
126 if (realcounter < 1) {
127 model.put("error", "The value must be greater than 0");
128 model.put("value", counter);
129 return "query/SequenceCounts";
132 CassandraRequester cr = new CassandraRequester();
133 List<TotalByCounterBean> r = cr.readProteinByCounter(realcounter);
134 model.put("njobs", 0);
137 model.put("njobs", r.size());
138 csvline = "\'Job%20 count\', \'Protein%20Sequence\'%0A";
140 // form line for CSV file
142 for (TotalByCounterBean b : r) {
143 if (b.getName().equals("")) {
144 csvline += "\'" + b.getTotaljobs() + "\',\'Alignment%20job\'%0A";
145 // fix problem with records without protein sequence (alignment
147 b.setName("Alignment job");
149 csvline += "\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A";
152 model.put("csvfile", csvline);
154 model.put("results", r);
155 final long endTime = System.currentTimeMillis();
156 model.put("timeExecution", (endTime - startTime));
157 model.put("counter", realcounter);
158 return "reportProteinSequencesCounter";