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