Merge branch 'servlets'
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Mon, 25 Nov 2013 11:04:31 +0000 (11:04 +0000)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Mon, 25 Nov 2013 11:04:31 +0000 (11:04 +0000)
Conflicts:
datadb/compbio/cassandra/StructureProteinPrediction.java
server/compbio/statistic/CassandraRequester.java

1  2 
datadb/compbio/cassandra/CassandraReader.java
server/compbio/statistic/CassandraRequester.java

@@@ -51,6 -51,30 +51,30 @@@ public class CassandraReader 
        }
  
        /*
+        * getting data from the db
+        */
+       public List<Pair<String, String>> ReadProteinData(long day) {
+               final long startTime = System.currentTimeMillis();
+               String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               if (results.isExhausted())
+                       return null;
+               final long queryTime = System.currentTimeMillis();
+               List<Row> rows = results.all();
+               System.out.println("Query time is " + (queryTime - startTime) + " msec");
+               List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
+               int c = 0;
+               for (Row r : rows) {
+                       Pair<String, String> pair = new Pair<String, String>(r.getString("JobID"), r.getString("Protein"));
+                       res.add(pair);
+                       ++c;
+               }
+               final long endTime = System.currentTimeMillis();
+               System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
+               return res;
+       }
+       /*
         * getting data from the db JobDateInfo
         */
        public List<Long> ReadDateTable(long queryDate) {
@@@ -73,7 -97,7 +97,7 @@@
        /*
         * getting whole protein sequence from the db ProteinRow
         */
 -      public List<StructureProteinPrediction> ReadWholeSequence(String queryProtein) {
 +      public List<AnnotatedProteinSequenceBean> ReadWholeSequence(String queryProtein) {
                final long startTime = System.currentTimeMillis();
                String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
                System.out.println("Command: " + com);
                List<Row> rows = results.all();
                System.out.println("Query time is " + (queryTime - startTime) + " msec");
                System.out.println(" rows analysed,  " + rows.size());
 -              List<StructureProteinPrediction> res = new ArrayList<StructureProteinPrediction>();
 +              List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
                int c = 0;
                for (Row r : rows) {
 -                      StructureProteinPrediction structure = new StructureProteinPrediction(queryProtein, r.getString("JobID"), r.getMap(
 +                      AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(queryProtein, r.getString("JobID"), r.getMap(
                                        "Predictions", String.class, String.class));
+                       if (structure.getPrediction().containsKey("jnetpred"))
+                               structure.setJnetpred(structure.getPrediction().get("jnetpred"));
+                       else
+                               structure.setJnetpred("");
                        res.add(structure);
                        ++c;
                }
        /*
         * getting part of protein sequence from the db ProteinRow
         */
 -      public List<StructureProteinPrediction> ReadPartOfSequence(String queryProtein) {
 +      public List<AnnotatedProteinSequenceBean> ReadPartOfSequence(String queryProtein) {
                final long startTime = System.currentTimeMillis();
                String com = "SELECT * FROM ProteinRow;";
                System.out.println("Command: " + com);
                List<Row> rows = results.all();
                System.out.println("Query time is " + (queryTime - startTime) + " msec");
                System.out.println(" rows analysed,  " + rows.size());
 -              List<StructureProteinPrediction> res = new ArrayList<StructureProteinPrediction>();
 +              List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
                int c = 0;
                for (Row r : rows) {
                        String prot = r.getString("Protein");
                        if (prot.matches("(.*)" + queryProtein + "(.*)")) {
 -                              StructureProteinPrediction structure = new StructureProteinPrediction(prot, r.getString("JobID"), r.getMap("Predictions",
 +                              AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(prot, r.getString("JobID"), r.getMap("Predictions",
                                                String.class, String.class));
                                res.add(structure);
                                ++c;
        }
  
        /*
-        * getting protein sequences by counter
+        * getting log info for jobid
         */
        public StructureJobLog ReadJobLog(String jobid) {
                final long startTime = System.currentTimeMillis();
@@@ -9,18 -9,20 +9,20 @@@ import java.util.Iterator
  import java.util.List;
  import java.util.Map;
  
 +import compbio.cassandra.AnnotatedProteinSequenceBean;
  import compbio.cassandra.CassandraNativeConnector;
  import compbio.cassandra.CassandraReader;
  import compbio.cassandra.DataBase;
  import compbio.cassandra.Pair;
  import compbio.cassandra.StructureJobLog;
 -import compbio.cassandra.StructureProteinPrediction;
  
  public class CassandraRequester {
        private CassandraReader db = new CassandraReader();
        private ArrayList<DataBase> query;
        private static long currentDate = 0;
        private static long earlestDate = 0;
+       private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
+       private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
  
        /*
         * query: execution time for the period from date1 till date2
                if (null == date2) {
                        date1 = "2100/1/1";
                }
-               if (!isThisDateValid(date1) || !isThisDateValid(date2)) {
+               if (!isThisDateValid(date1,formatYYMMDD) || !isThisDateValid(date2,formatYYMMDD)) {
                        System.out.println("Wrong date: point 3");
                        return null;
                }
                SetDateRange();
                int nbins = 5;
-               long dateStart = DateParsing(date1);
-               long dateEnd = DateParsing(date2);
+               long dateStart = DateParsing(date1, formatYYMMDD);
+               long dateEnd = DateParsing(date2, formatYYMMDD);
                if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
                        return null;
                if (dateStart < earlestDate)
                if (null == date2) {
                        date1 = "2100/1/1";
                }
-               if (!isThisDateValid(date1) || !isThisDateValid(date2)) {
+               if (!isThisDateValid(date1, formatYYMMDD) || !isThisDateValid(date2, formatYYMMDD)) {
                        System.out.println("Wrong date: point 3");
                        return null;
                }
                SetDateRange();
-               long dateStart = DateParsing(date1);
-               long dateEnd = DateParsing(date2);
+               long dateStart = DateParsing(date1, formatYYMMDD);
+               long dateEnd = DateParsing(date2, formatYYMMDD);
                if (dateEnd < earlestDate || dateStart > currentDate || dateStart > dateEnd)
                        return null;
                if (dateStart < earlestDate)
                System.out.println("StatisticsProt.readLength: total number of dates = " + query.size());
                return query;
        }
 -      
 -      /*
 -       * query: jobs and sequence at date
 -       */
 -      public List<DataBase> readJobByDay (String date) {
 -              System.out.println(date);
 -              if (null == date) {
 -                      return null;
 -              }
 -              if (!isThisDateValid(date, formatDDMMYY)) {
 -                      System.out.println("Wrong date: point 3");
 -                      return null;
 -              }
 -              SetDateRange();
 -              long day = DateParsing(date, formatDDMMYY);
 -              System.out.println(day);
 -              if (day < earlestDate || day > currentDate)
 -                      return null;    
 -              List<Pair<String, String>>      res = db.ReadProteinData(day);
 -              if (res == null)
 -                      return null;
 -              query = new ArrayList<DataBase>();
 -              for (Pair<String, String> entry : res) {                
 -                      DataBase bean = new DataBase();
 -                      bean.setDate(date);
 -                      bean.setId(entry.getElement0());
 -                      bean.setProt(entry.getElement1());
 -                      query.add(bean);
 -              }
 -              System.out.println("StatisticsProt.readLength: total number of dates = " + query.size());
 -              return query;
 -      }
 +
        /* 
         * query: protein sequence
         * */
        public List<DataBase> readProteins(String protIn, String flag) {
+               if (protIn == null)
+                       return null;
+               System.out.println(protIn.length());
                query = new ArrayList<DataBase>();
 -              List<StructureProteinPrediction> res;
 +              List<AnnotatedProteinSequenceBean> res;
                if (flag.equals("whole")) 
                        res = db.ReadWholeSequence(protIn);
                 else 
-                       res = db.ReadPartOfSequence(protIn);
+                        res = (protIn.length() > 0) ? db.ReadPartOfSequence(protIn) : null;
                if (res == null)
                        return null;
 -              for (StructureProteinPrediction entry : res) {
 -                              DataBase bean = new DataBase();
 -                              bean.setProt(entry.getSequence());
 -                              bean.setId(entry.getJobid());
 -                              bean.setJpred(entry.getJnetpred());
 -                              if (flag.equals("part"))
 -                                      bean.setSubProt(CreateSubprot (entry.getSequence(), protIn));                           
 -                              query.add(bean);
 -                      
 +              for (AnnotatedProteinSequenceBean entry : res) {
 +                      DataBase db = new DataBase();
 +                      db.setProt(entry.getSequence());
 +                      db.setPredictions(entry);
 +                      db.setId(entry.getJobid());
 +                      if (flag.equals("part")) {
 +                              db.setSubProt(CreateSubprot (entry.getSequence(), protIn));
 +                      }
 +                      query.add(db);
                }
                return query;
        }
 -      
 +
        /* 
         * query protein sequences with number of jobs
         */
         * query jobs log info
         */
        public DataBase readJobLog(String jobid) {
-       //      query = new ArrayList<DataBase>();
+               if (jobid == null)
+                       return null;
                StructureJobLog res = db.ReadJobLog(jobid);
+               if (res == null)
+                       return null;
                DataBase query = new DataBase();
                query.setLogInfo(res);
-       //      query.setres);
                return query;
        }
+       
        /*
         * create list of parts of protein sequence;
         */
        /*
         * convert String date into long date (miliseconds since the epoch start)
         */
-       private static long DateParsing(String datInput) {
+       private static long DateParsing(String datInput, SimpleDateFormat formatter) {
                if (datInput == null) {
                        return 0;
                }
                long dateWorkSt = 0;
-               SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
+               
                try {
                        dateWorkSt = formatter.parse(datInput).getTime();
                } catch (ParseException e) {
         */
        private static void SetDateRange() {
                Calendar cal = Calendar.getInstance();
-               currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH));
+               currentDate = DateParsing(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH), formatYYMMDD);
                if (0 == earlestDate) {
                        CassandraRequester cr = new CassandraRequester();
                        earlestDate = cr.earliestDate();
                }
        }
  
-       public boolean isThisDateValid(String dateToValidate) {
+       public boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
                if (dateToValidate == null || dateToValidate.equals("")) {
                        System.out.println("Undefined date");
                        return false;
                }
-               SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                try {
                        // if not valid, this will throw ParseException
                        sdf.setLenient(false);