1. Fix problem with slow servlets (wrong forming CVS line)
[proteocache.git] / datadb / compbio / cassandra / readers / SequenceReader.java
1 package compbio.cassandra.readers;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.datastax.driver.core.ResultSet;
7 import com.datastax.driver.core.Row;
8
9 import compbio.beans.ProteinBean;
10
11 /**
12  * Reader class for making requests on protein sequences to cassandra.
13  * 
14  * @author Alexander Sherstnev
15  * @author Natasha Sherstneva
16  * 
17  * @since 0.5
18  * @version 1.0 
19  * @since December 2013
20  */
21 public class SequenceReader extends CassandraReader {
22
23         public SequenceReader() {
24                 super();
25         }
26
27         /**
28          * query: protein sequence
29          * 
30          * @param sequence
31          *            protein sequence or partial of protein sequence
32          * @param searchtype
33          *            "whole" or "partial" of protein sequence
34          * 
35          * @return List<ProteinBean> to the controller SequenceController
36          * 
37          **/
38         public List<ProteinBean> readProteins(String sequence, String searchtype) {
39                 List<ProteinBean> query = new ArrayList<ProteinBean>();
40                 if (searchtype.equals("whole")) {
41                         ResultSet results = CassandraQuery("SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + sequence + "';");
42                         if (results.isExhausted())
43                                 return null;
44                         List<Row> rows = results.all();
45                         ProteinBean structure = new ProteinBean(sequence, rows.get(0).getMap("Predictions", String.class, String.class));
46                         for (Row r : rows) {
47                                 structure.setJobid(r.getString("JobID"));
48                         }
49                         query.add(structure);
50                 } else {
51                         ResultSet results = CassandraQuery("SELECT * FROM ProteinRow;");
52                         if (results.isExhausted())
53                                 return null;
54                         List<Row> rows = results.all();
55                         for (Row r : rows) {
56                                 String protein = r.getString("Protein");
57                                 if (protein.matches("(.*)" + sequence + "(.*)")) {
58                                         ProteinBean foundsequence = new ProteinBean(protein, r.getMap("Predictions", String.class, String.class));
59                                         query.add(foundsequence);
60                                 }
61                         }
62                 }
63                 if (searchtype.equals("partial")) {
64                         for (ProteinBean entry : query) {
65                                 entry.setSubProt(CreateSubprotein(entry.getSequence(), sequence));
66                         }
67                 }
68                 return query;
69         }
70
71         /**
72          * create a list of parts of protein sequence for highlighting current value
73          * in report;
74          * 
75          * @param protein
76          *            protein sequence
77          * @param subprot
78          *            partial of protein sequence
79          * 
80          * @return List<String>
81          * 
82          **/
83         private static List<String> CreateSubprotein(String protein, String subprot) {
84                 List<String> sub = new ArrayList<String>();
85                 String subStr = protein;
86                 while (subStr.length() > 0 && subStr.contains(subprot)) {
87                         String first = subStr.substring(0, subStr.indexOf(subprot));
88                         if (first.length() > 0)
89                                 sub.add(first);
90                         sub.add(subprot);
91                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
92                 }
93                 if (subStr.length() > 0)
94                         sub.add(subStr);
95                 return sub;
96         }
97 }