X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=datadb%2Fcompbio%2Fcassandra%2Freaders%2FSequenceReader.java;fp=datadb%2Fcompbio%2Fcassandra%2Freaders%2FSequenceReader.java;h=7e77126c6c2f7c55cd68dcf8e60db0eddbbfd57f;hb=9bb6ee99ca7f738fac1087190b5481b8fe6e8d9f;hp=0000000000000000000000000000000000000000;hpb=2e3f6b76be585306f1003d849831840c0adb3360;p=proteocache.git diff --git a/datadb/compbio/cassandra/readers/SequenceReader.java b/datadb/compbio/cassandra/readers/SequenceReader.java new file mode 100644 index 0000000..7e77126 --- /dev/null +++ b/datadb/compbio/cassandra/readers/SequenceReader.java @@ -0,0 +1,98 @@ +package compbio.cassandra.readers; + +import java.util.ArrayList; +import java.util.List; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; + +import compbio.beans.ProteinBean; + +/** + * Reader class for making requests on protein sequences to cassandra. + * + * @author Alexander Sherstnev + * @author Natasha Sherstneva + * + * @since 0.5 + * @version 1.0 + * @since December 2013 + */ +public class SequenceReader extends CassandraReader { + + public SequenceReader() { + super(); + } + + /** + * query: protein sequence + * + * @param sequence + * protein sequence or partial of protein sequence + * @param searchtype + * "whole" or "partial" of protein sequence + * + * @return List to the controller SequenceController + * + **/ + public List readProteins(String sequence, String searchtype) { + List query = new ArrayList(); + if (searchtype.equals("whole")) { + ResultSet results = CassandraQuery("SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + sequence + "';"); + if (results.isExhausted()) + return null; + List rows = results.all(); + ProteinBean structure = new ProteinBean(sequence, rows.get(0).getMap("Predictions", String.class, String.class)); + for (Row r : rows) { + structure.setJobid(r.getString("JobID")); + } + query.add(structure); + } else { + ResultSet results = CassandraQuery("SELECT * FROM ProteinRow;"); + if (results.isExhausted()) + return null; + List rows = results.all(); + for (Row r : rows) { + String protein = r.getString("Protein"); + if (protein.matches("(.*)" + sequence + "(.*)")) { + ProteinBean foundsequence = new ProteinBean(protein, r.getMap("Predictions", String.class, String.class)); + foundsequence.setJobid(r.getString("JobID")); + query.add(foundsequence); + } + } + } + if (searchtype.equals("partial")) { + for (ProteinBean entry : query) { + entry.setSubProt(CreateSubprotein(entry.getSequence(), sequence)); + } + } + return query; + } + + /** + * create a list of parts of protein sequence for highlighting current value + * in report; + * + * @param protein + * protein sequence + * @param subprot + * partial of protein sequence + * + * @return List + * + **/ + private static List CreateSubprotein(String protein, String subprot) { + List sub = new ArrayList(); + String subStr = protein; + while (subStr.length() > 0 && subStr.contains(subprot)) { + String first = subStr.substring(0, subStr.indexOf(subprot)); + if (first.length() > 0) + sub.add(first); + sub.add(subprot); + subStr = subStr.substring(subStr.indexOf(subprot) + subprot.length(), subStr.length()); + } + if (subStr.length() > 0) + sub.add(subStr); + return sub; + } +}