Simplify the code
[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                                         foundsequence.setJobid(r.getString("JobID"));
60                                         query.add(foundsequence);
61                                 }
62                         }
63                         for (ProteinBean entry : query) {
64                                 entry.setSubProt(CreateSubprotein(entry.getSequence(), sequence));
65                         }
66                 }
67                 return query;
68         }
69
70         /**
71          * create a list of parts of protein sequence for highlighting current value
72          * in report;
73          * 
74          * @param protein
75          *            protein sequence
76          * @param subprot
77          *            partial of protein sequence
78          * 
79          * @return List<String>
80          * 
81          **/
82         private static List<String> CreateSubprotein(String protein, String subprot) {
83                 List<String> sub = new ArrayList<String>();
84                 String subStr = protein;
85                 while (subStr.length() > 0 && subStr.contains(subprot)) {
86                         String first = subStr.substring(0, subStr.indexOf(subprot));
87                         if (first.length() > 0)
88                                 sub.add(first);
89                         sub.add(subprot);
90                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
91                 }
92                 if (subStr.length() > 0)
93                         sub.add(subStr);
94                 return sub;
95         }
96 }