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; public class SequenceReader extends CassandraReader { public SequenceReader() { super(); } /** * query: protein sequence * * @param protIn * 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 protIn, String searchtype) { List query = new ArrayList(); if (searchtype.equals("whole")) { ResultSet results = CassandraQuery("SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + protIn + "';"); if (results.isExhausted()) return null; List rows = results.all(); ProteinBean structure = new ProteinBean(protIn, 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 prot = r.getString("Protein"); if (prot.matches("(.*)" + protIn + "(.*)")) { ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class)); structure.setJobid(r.getString("JobID")); query.add(structure); } } } if (searchtype.equals("partial")) { for (ProteinBean entry : query) { entry.setSubProt(CreateSubprotein(entry.getSequence(), protIn)); } } 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; } }