new readers for the queries
[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 public class SequenceReader extends CassandraReader {
12
13         public SequenceReader() {
14                 super();
15         }
16         
17         /**
18          * query: protein sequence
19          *  
20          * @param protIn
21          *           protein sequence or partial of protein sequence
22          * @param searchtype
23          *           "whole" or "partial" of protein sequence
24          * 
25          * @return List<ProteinBean> to the controller SequenceController
26          * 
27          **/
28         public List<ProteinBean> readProteins(String protIn, String searchtype) {
29                 List<ProteinBean> query = new ArrayList<ProteinBean>();
30                 if (searchtype.equals("whole")) {
31                         ResultSet results = CassandraQuery("SELECT  JobID, Predictions FROM ProteinRow WHERE Protein = '" + protIn + "';");
32                         if (results.isExhausted())
33                                 return null;
34                         List<Row> rows = results.all();
35                         ProteinBean structure = new ProteinBean(protIn, rows.get(0).getMap("Predictions", String.class, String.class));
36                         for (Row r : rows) {
37                                 structure.setJobid(r.getString("JobID"));
38                         }
39                         query.add(structure);
40                 } else {
41                         ResultSet results = CassandraQuery("SELECT  * FROM ProteinRow;");
42                         if (results.isExhausted())
43                                 return null;
44                         List<Row> rows = results.all();
45                         for (Row r : rows) {
46                                 String prot = r.getString("Protein");
47                                 if (prot.matches("(.*)" + protIn + "(.*)")) {
48                                         ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class));
49                                         structure.setJobid(r.getString("JobID"));
50                                         query.add(structure);
51                                 }
52                         }
53                 }
54                 if (searchtype.equals("partial")) {
55                         for (ProteinBean entry : query) {
56                                 entry.setSubProt(CreateSubprotein(entry.getSequence(), protIn));
57                         }
58                 }
59                 return query;
60         }
61         
62         /**
63          * create a list of parts of protein sequence for highlighting current value in report;
64          *  
65          * @param protein
66          *           protein sequence 
67          * @param subprot
68          *           partial of protein sequence
69          * 
70          * @return List<String> 
71          * 
72          **/
73         private static List<String> CreateSubprotein(String protein, String subprot) {
74                 List<String> sub = new ArrayList<String>();
75                 String subStr = protein;
76                 while (subStr.length() > 0 && subStr.contains(subprot)) {
77                         String first = subStr.substring(0, subStr.indexOf(subprot));
78                         if (first.length() > 0)
79                                 sub.add(first);
80                         sub.add(subprot);
81                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
82                 }
83                 if (subStr.length() > 0)
84                         sub.add(subStr);
85                 return sub;
86         }
87 }