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.beans.ProteinBean;
14 import compbio.beans.TotalByCounterBean;
15 import compbio.cassandra.readers.ReaderByCounter;
16 import compbio.cassandra.readers.SequenceReader;
19 * Spring controller for supporting sequence queries. This version works in the
22 * @author Alexander Sherstnev
23 * @author Natasha Sherstneva
26 * @version 1.0 December 2013
29 @RequestMapping("/sequence")
30 public class SequenceController extends BasicController {
33 * pattern for NON-protein alphabet symbols
35 private final Pattern NONPROTEIN = Pattern.compile("[^ARNDCQEGHILKMFPSTWYV]+", Pattern.CASE_INSENSITIVE);
38 * form a query page for search protein sequence. The servlet should be
39 * available for users and admins only.
43 * @return link to the JSP query page
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";
53 * form a query page for statistics: Protein by job count. The servlet
54 * should be available for users and admins only.
58 * @return link to the JSP query page
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";
68 * form a report page for search protein sequence.
73 * protein sequence or part of sequence
75 * defined whether the whole sequence or part of sequence would
77 * @return link to the report JSP page
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();
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";
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";
98 model.put("njobs", 0);
99 model.put("prot", trimmedsequence);
100 model.put("searchtype", searchtype);
101 StringBuilder csvline = new StringBuilder("");
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());
110 model.put("njobs", result.size());
112 csvline.append("\'Job\',\'Annotation\',\'Sequence\'%0A");
113 for (ProteinBean entry : result) {
114 List<String> jobs = entry.getJobid();
115 String protein = entry.getSequence();
116 LinkedHashMap<String, String> predictions = entry.getPredictions();
117 StringBuilder jobline = new StringBuilder();
118 for (Map.Entry<String, String> pr : predictions.entrySet()) {
119 jobline.append("\'\',\'" + pr.getKey() + "\',\'" + pr.getValue() + "\'%0A");
121 for (String job : jobs) {
122 csvline.append("\'" + job + "\',\'Sequence\',\'" + protein + "\',\'%0A" + jobline.toString());
127 model.put("csvfile", csvline.toString());
129 final long endTime = System.currentTimeMillis();
130 model.put("timeExecution", (endTime - startTime));
131 return "reports/Sequences";
135 * form a report page for statistics: Protein by job count.
141 * @return link to the report JSP page
143 @RequestMapping(value = "counts/results", method = RequestMethod.GET)
144 public String countSequences(@RequestParam("counterJob") String counter, Map<String, Object> model) {
145 model.put("username", getPrincipalName());
146 final long startTime = System.currentTimeMillis();
148 if (counter.equals("")) {
149 model.put("error", "The value must not be empty");
150 model.put("value", counter);
151 return "query/SequenceCounts";
156 realcounter = Integer.parseInt(counter.trim());
157 } catch (NumberFormatException e) {
158 model.put("error", "The value must be an integer number");
159 model.put("value", counter);
160 return "query/SequenceCounts";
163 if (realcounter < 1) {
164 model.put("error", "The value must be greater than 0");
165 model.put("value", counter);
166 return "query/SequenceCounts";
169 ReaderByCounter reader = new ReaderByCounter();
170 List<TotalByCounterBean> r = reader.readProteinByCounter(realcounter);
171 model.put("results", r);
172 model.put("njobs", 0);
173 StringBuilder csvline = new StringBuilder("");
175 model.put("njobs", r.size());
176 // form line for CSV file
177 csvline.append("\'Job%20 count\', \'Protein%20Sequence\'%0A");
178 for (TotalByCounterBean b : r) {
179 if (b.getName().equals("")) {
180 // fix problem with records without protein sequence
182 csvline.append("\'" + b.getTotaljobs() + "\',\'Alignment%20job\'%0A");
183 b.setName("Alignment job");
185 csvline.append("\'" + b.getTotaljobs() + "\',\'" + b.getName() + "\'%0A");
189 model.put("csvfile", csvline.toString());
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";