Apply formatting
[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
64          * in report;
65          * 
66          * @param protein
67          *            protein sequence
68          * @param subprot
69          *            partial of protein sequence
70          * 
71          * @return List<String>
72          * 
73          **/
74         private static List<String> CreateSubprotein(String protein, String subprot) {
75                 List<String> sub = new ArrayList<String>();
76                 String subStr = protein;
77                 while (subStr.length() > 0 && subStr.contains(subprot)) {
78                         String first = subStr.substring(0, subStr.indexOf(subprot));
79                         if (first.length() > 0)
80                                 sub.add(first);
81                         sub.add(subprot);
82                         subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length());
83                 }
84                 if (subStr.length() > 0)
85                         sub.add(subStr);
86                 return sub;
87         }
88 }