extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/MBafServlet.java

Code
Comments
Other
Rev Date Author Line
5029 16 Oct 18 nicklas 1 package net.sf.basedb.reggie.servlet;
5029 16 Oct 18 nicklas 2
5029 16 Oct 18 nicklas 3 import java.io.IOException;
5029 16 Oct 18 nicklas 4 import java.util.ArrayList;
5029 16 Oct 18 nicklas 5 import java.util.List;
5029 16 Oct 18 nicklas 6
5029 16 Oct 18 nicklas 7 import javax.servlet.ServletException;
5029 16 Oct 18 nicklas 8 import javax.servlet.http.HttpServlet;
5029 16 Oct 18 nicklas 9 import javax.servlet.http.HttpServletRequest;
5029 16 Oct 18 nicklas 10 import javax.servlet.http.HttpServletResponse;
5029 16 Oct 18 nicklas 11
5029 16 Oct 18 nicklas 12 import org.json.simple.JSONArray;
5029 16 Oct 18 nicklas 13 import org.json.simple.JSONObject;
5029 16 Oct 18 nicklas 14
5029 16 Oct 18 nicklas 15 import net.sf.basedb.core.ItemList;
5030 16 Oct 18 nicklas 16 import net.sf.basedb.core.ItemNotFoundException;
5029 16 Oct 18 nicklas 17 import net.sf.basedb.core.DbControl;
5029 16 Oct 18 nicklas 18 import net.sf.basedb.core.DerivedBioAssay;
5029 16 Oct 18 nicklas 19 import net.sf.basedb.core.ItemQuery;
5030 16 Oct 18 nicklas 20 import net.sf.basedb.core.Job;
5029 16 Oct 18 nicklas 21 import net.sf.basedb.core.SessionControl;
5030 16 Oct 18 nicklas 22 import net.sf.basedb.core.Software;
5029 16 Oct 18 nicklas 23 import net.sf.basedb.core.query.Hql;
5029 16 Oct 18 nicklas 24 import net.sf.basedb.core.query.Orders;
5029 16 Oct 18 nicklas 25 import net.sf.basedb.core.snapshot.SnapshotManager;
5030 16 Oct 18 nicklas 26 import net.sf.basedb.opengrid.JobDefinition;
5030 16 Oct 18 nicklas 27 import net.sf.basedb.opengrid.OpenGridCluster;
5030 16 Oct 18 nicklas 28 import net.sf.basedb.opengrid.service.OpenGridService;
5029 16 Oct 18 nicklas 29 import net.sf.basedb.reggie.JsonUtil;
5029 16 Oct 18 nicklas 30 import net.sf.basedb.reggie.Reggie;
5029 16 Oct 18 nicklas 31 import net.sf.basedb.reggie.counter.CounterService;
5029 16 Oct 18 nicklas 32 import net.sf.basedb.reggie.dao.AlignedSequences;
5029 16 Oct 18 nicklas 33 import net.sf.basedb.reggie.dao.Annotationtype;
5029 16 Oct 18 nicklas 34 import net.sf.basedb.reggie.dao.BiomaterialList;
5029 16 Oct 18 nicklas 35 import net.sf.basedb.reggie.dao.Library;
5029 16 Oct 18 nicklas 36 import net.sf.basedb.reggie.dao.ReggieRole;
5029 16 Oct 18 nicklas 37 import net.sf.basedb.reggie.dao.Rna;
5029 16 Oct 18 nicklas 38 import net.sf.basedb.reggie.dao.Subtype;
5030 16 Oct 18 nicklas 39 import net.sf.basedb.reggie.grid.MBafJobCreator;
5030 16 Oct 18 nicklas 40 import net.sf.basedb.reggie.grid.ScriptUtil;
5029 16 Oct 18 nicklas 41 import net.sf.basedb.util.Values;
5029 16 Oct 18 nicklas 42 import net.sf.basedb.util.error.ThrowableUtil;
5029 16 Oct 18 nicklas 43
5029 16 Oct 18 nicklas 44
5029 16 Oct 18 nicklas 45 public class MBafServlet 
5029 16 Oct 18 nicklas 46   extends HttpServlet 
5029 16 Oct 18 nicklas 47 {
5029 16 Oct 18 nicklas 48
5029 16 Oct 18 nicklas 49   private static final long serialVersionUID = -7602946534106510026L;
5029 16 Oct 18 nicklas 50
5029 16 Oct 18 nicklas 51   public MBafServlet()
5029 16 Oct 18 nicklas 52   {}
5029 16 Oct 18 nicklas 53
5029 16 Oct 18 nicklas 54   @SuppressWarnings("unchecked")
5029 16 Oct 18 nicklas 55   @Override
5029 16 Oct 18 nicklas 56   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
5029 16 Oct 18 nicklas 57     throws ServletException, IOException 
5029 16 Oct 18 nicklas 58   {
5029 16 Oct 18 nicklas 59     String cmd = req.getParameter("cmd");
5029 16 Oct 18 nicklas 60     JsonUtil.setJsonResponseHeaders(resp);
5029 16 Oct 18 nicklas 61     
5029 16 Oct 18 nicklas 62     JSONObject json = new JSONObject();
5029 16 Oct 18 nicklas 63     json.put("status", "ok");
5029 16 Oct 18 nicklas 64     
5029 16 Oct 18 nicklas 65     final SessionControl sc = Reggie.getSessionControl(req);
5029 16 Oct 18 nicklas 66     DbControl dc = null;
5029 16 Oct 18 nicklas 67     try
5029 16 Oct 18 nicklas 68     {
5029 16 Oct 18 nicklas 69       if ("GetAlignedSequencesForMBafAnalysis".equals(cmd))
5029 16 Oct 18 nicklas 70       {
6335 15 Jun 21 nicklas 71         dc = sc.newDbControl(":Start mBAF analysis");
5029 16 Oct 18 nicklas 72         
5029 16 Oct 18 nicklas 73         List<AlignedSequences> list = null;
5029 16 Oct 18 nicklas 74         String items = Values.getStringOrNull(req.getParameter("items"));
5029 16 Oct 18 nicklas 75         if (items == null)
5029 16 Oct 18 nicklas 76         {
5029 16 Oct 18 nicklas 77           ItemList mbafPipeline = BiomaterialList.MBAF_PIPELINE.load(dc);
6183 26 Mar 21 nicklas 78           ItemQuery<DerivedBioAssay> query = mbafPipeline.getMembers();
5029 16 Oct 18 nicklas 79           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5029 16 Oct 18 nicklas 80           Subtype.ALIGNED_SEQUENCES.addFilter(dc, query);
5029 16 Oct 18 nicklas 81           // Join LibPlate for sorting
5029 16 Oct 18 nicklas 82           query.join(Hql.innerJoin(null, "extract", "lib", true));
5029 16 Oct 18 nicklas 83           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
5029 16 Oct 18 nicklas 84           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
5029 16 Oct 18 nicklas 85           query.order(Orders.asc(Hql.property("libPlate", "id")));
5029 16 Oct 18 nicklas 86           query.order(Orders.asc(Hql.property("name")));
5029 16 Oct 18 nicklas 87           query.setMaxResults(250);
5029 16 Oct 18 nicklas 88           list = AlignedSequences.toList(query.list(dc));
5029 16 Oct 18 nicklas 89         }
5029 16 Oct 18 nicklas 90         else
5029 16 Oct 18 nicklas 91         {
5029 16 Oct 18 nicklas 92           Integer[] ids = Values.getInt(items.split(","));
5029 16 Oct 18 nicklas 93           list = new ArrayList<AlignedSequences>();
5029 16 Oct 18 nicklas 94           for (Integer id : ids)
5029 16 Oct 18 nicklas 95           {
5029 16 Oct 18 nicklas 96             list.add(AlignedSequences.getById(dc, id));
5029 16 Oct 18 nicklas 97           }
5029 16 Oct 18 nicklas 98         }
5029 16 Oct 18 nicklas 99         SnapshotManager manager = new SnapshotManager();
5029 16 Oct 18 nicklas 100         JSONArray jsonAlignedSequences = new JSONArray();
5029 16 Oct 18 nicklas 101         for (AlignedSequences as : list)
5029 16 Oct 18 nicklas 102         {
5547 07 Aug 19 nicklas 103           as.loadAnnotations(dc, "pipeline", Annotationtype.PIPELINE, null);
5029 16 Oct 18 nicklas 104           Library lib = as.getLibrary(dc);
5029 16 Oct 18 nicklas 105           lib.loadBioPlateLocation();
5029 16 Oct 18 nicklas 106           Rna r = lib.getRna(dc, true);
5547 07 Aug 19 nicklas 107           if (r != null) lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
5029 16 Oct 18 nicklas 108           as.setAnnotation("lib", lib.asJSONObject());
5029 16 Oct 18 nicklas 109           as.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
5029 16 Oct 18 nicklas 110           as.loadDoNotUseAnnotations(dc, manager);
5029 16 Oct 18 nicklas 111           jsonAlignedSequences.add(as.asJSONObject());
5029 16 Oct 18 nicklas 112         }
5029 16 Oct 18 nicklas 113         json.put("alignedSequences", jsonAlignedSequences);
5029 16 Oct 18 nicklas 114       }
5029 16 Oct 18 nicklas 115
5029 16 Oct 18 nicklas 116     
5029 16 Oct 18 nicklas 117     }
5029 16 Oct 18 nicklas 118     catch (Throwable t)
5029 16 Oct 18 nicklas 119     {
5029 16 Oct 18 nicklas 120       t.printStackTrace();
5029 16 Oct 18 nicklas 121       json.clear();
5029 16 Oct 18 nicklas 122       json.put("status", "error");
5029 16 Oct 18 nicklas 123       json.put("message", t.getMessage());
5029 16 Oct 18 nicklas 124       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5029 16 Oct 18 nicklas 125     }
5029 16 Oct 18 nicklas 126     finally
5029 16 Oct 18 nicklas 127     {
5029 16 Oct 18 nicklas 128       if (dc != null) dc.close();
5029 16 Oct 18 nicklas 129       json.writeJSONString(resp.getWriter());
5029 16 Oct 18 nicklas 130     }
5029 16 Oct 18 nicklas 131     
5029 16 Oct 18 nicklas 132   }
5029 16 Oct 18 nicklas 133
5029 16 Oct 18 nicklas 134   @Override
5029 16 Oct 18 nicklas 135   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
5029 16 Oct 18 nicklas 136     throws ServletException, IOException 
5029 16 Oct 18 nicklas 137   {
5029 16 Oct 18 nicklas 138     String cmd = req.getParameter("cmd");
5029 16 Oct 18 nicklas 139     JsonUtil.setJsonResponseHeaders(resp);
5029 16 Oct 18 nicklas 140     
5029 16 Oct 18 nicklas 141     JSONObject json = new JSONObject();
5029 16 Oct 18 nicklas 142     json.put("status", "ok");
5029 16 Oct 18 nicklas 143     JSONArray jsonMessages = new JSONArray();
5029 16 Oct 18 nicklas 144   
5029 16 Oct 18 nicklas 145     final SessionControl sc = Reggie.getSessionControl(req);
5029 16 Oct 18 nicklas 146     DbControl dc = null;
5029 16 Oct 18 nicklas 147     try
5029 16 Oct 18 nicklas 148     {
5029 16 Oct 18 nicklas 149
5029 16 Oct 18 nicklas 150       if ("StartMBafAnalysis".equals(cmd))
5029 16 Oct 18 nicklas 151       {
6335 15 Jun 21 nicklas 152         dc = sc.newDbControl(":Start mBAF analysis");
5029 16 Oct 18 nicklas 153
5029 16 Oct 18 nicklas 154         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.ADMINISTRATOR);
5029 16 Oct 18 nicklas 155
5030 16 Oct 18 nicklas 156         JSONObject jsonReq = JsonUtil.parseRequest(req);
5030 16 Oct 18 nicklas 157         JSONArray jsonAligned = (JSONArray)jsonReq.get("alignedSequences");
5030 16 Oct 18 nicklas 158       
5057 29 Oct 18 nicklas 159         Number mBafSoftwareId = (Number)jsonReq.get("mBafSoftware");
5030 16 Oct 18 nicklas 160       
5030 16 Oct 18 nicklas 161         String clusterId = (String)jsonReq.get("cluster");
5030 16 Oct 18 nicklas 162         boolean debug = Boolean.TRUE.equals(jsonReq.get("debug"));
5030 16 Oct 18 nicklas 163         boolean autoConfirm = Boolean.TRUE.equals(jsonReq.get("autoConfirm"));
5030 16 Oct 18 nicklas 164         Number priority = (Number)jsonReq.get("priority");
6981 17 Jan 23 nicklas 165         String partition = Values.getStringOrNull((String)jsonReq.get("partition"));
7372 06 Oct 23 nicklas 166         String submitOptions = Values.getStringOrNull((String)jsonReq.get("submitOptionsOverride"));
5030 16 Oct 18 nicklas 167
5030 16 Oct 18 nicklas 168         OpenGridCluster cluster = OpenGridService.getInstance().getClusterById(dc, clusterId);
5030 16 Oct 18 nicklas 169         if (cluster == null)
5030 16 Oct 18 nicklas 170         {
5030 16 Oct 18 nicklas 171           throw new ItemNotFoundException("OpenGridCluster[" + clusterId + "]");
5030 16 Oct 18 nicklas 172         }
5030 16 Oct 18 nicklas 173
5030 16 Oct 18 nicklas 174         // Load common items
5057 29 Oct 18 nicklas 175         Software mBafSoftware = mBafSoftwareId != null ? Software.getById(dc, mBafSoftwareId.intValue()) : null;
5030 16 Oct 18 nicklas 176
5030 16 Oct 18 nicklas 177         MBafJobCreator jobCreator = new MBafJobCreator();
5030 16 Oct 18 nicklas 178         jobCreator.setDebug(debug);
5035 17 Oct 18 nicklas 179         jobCreator.setAutoConfirm(autoConfirm);
5030 16 Oct 18 nicklas 180         jobCreator.setPriority(priority == null ? null : priority.intValue());
6981 17 Jan 23 nicklas 181         jobCreator.setPartition(partition);
5057 29 Oct 18 nicklas 182         jobCreator.setSoftware(mBafSoftware);
7372 06 Oct 23 nicklas 183         jobCreator.setSubmitOptionsOverride(submitOptions);
5029 16 Oct 18 nicklas 184         
5030 16 Oct 18 nicklas 185         List<AlignedSequences> alignedSequences = new ArrayList<AlignedSequences>();
5030 16 Oct 18 nicklas 186         for (int alNo = 0; alNo < jsonAligned.size(); alNo++)
5030 16 Oct 18 nicklas 187         {
5030 16 Oct 18 nicklas 188           JSONObject jsonAl = (JSONObject)jsonAligned.get(alNo);
5030 16 Oct 18 nicklas 189           Number alignedId = (Number)jsonAl.get("id");
5030 16 Oct 18 nicklas 190         
5030 16 Oct 18 nicklas 191           AlignedSequences as = AlignedSequences.getById(dc, alignedId.intValue());
5030 16 Oct 18 nicklas 192           alignedSequences.add(as);
5030 16 Oct 18 nicklas 193
5030 16 Oct 18 nicklas 194           // Reset AUTO_PROCESSING annotation
5030 16 Oct 18 nicklas 195           DerivedBioAssay aligned = as.getDerivedBioAssay();
5030 16 Oct 18 nicklas 196           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, aligned, null);
5030 16 Oct 18 nicklas 197         }
5030 16 Oct 18 nicklas 198
5030 16 Oct 18 nicklas 199         List<JobDefinition> jobDefs = jobCreator.createMBafJobs(dc, cluster, alignedSequences);
5030 16 Oct 18 nicklas 200         List<Job> jobs = ScriptUtil.submitJobs(dc, cluster, jobDefs);
5030 16 Oct 18 nicklas 201         
5030 16 Oct 18 nicklas 202         for (Job job : jobs)
5030 16 Oct 18 nicklas 203         {
5030 16 Oct 18 nicklas 204           if (job.getStatus() == Job.Status.ERROR)
5030 16 Oct 18 nicklas 205           {
5030 16 Oct 18 nicklas 206             jsonMessages.add("[Error]Job submission for '" + job.getName() + "' failed: " + job.getStatusMessage());
5030 16 Oct 18 nicklas 207           }
5030 16 Oct 18 nicklas 208           else
5030 16 Oct 18 nicklas 209           {
5030 16 Oct 18 nicklas 210             jsonMessages.add("Submitted mBAF analysis job to " + cluster.getConnectionInfo().getName() + " with id " + job.getExternalId());
5030 16 Oct 18 nicklas 211           }
5030 16 Oct 18 nicklas 212         }
5030 16 Oct 18 nicklas 213         
5029 16 Oct 18 nicklas 214         dc.commit();
5029 16 Oct 18 nicklas 215       }
5029 16 Oct 18 nicklas 216
5029 16 Oct 18 nicklas 217       json.put("messages", jsonMessages);
5029 16 Oct 18 nicklas 218       CounterService.getInstance().setForceCount();
5029 16 Oct 18 nicklas 219     }
5029 16 Oct 18 nicklas 220     catch (Throwable t)
5029 16 Oct 18 nicklas 221     {
5029 16 Oct 18 nicklas 222       t.printStackTrace();
5029 16 Oct 18 nicklas 223       json.clear();
5029 16 Oct 18 nicklas 224       json.put("status", "error");
5029 16 Oct 18 nicklas 225       json.put("message", t.getMessage());
5029 16 Oct 18 nicklas 226       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5029 16 Oct 18 nicklas 227     }
5029 16 Oct 18 nicklas 228     finally
5029 16 Oct 18 nicklas 229     {
5029 16 Oct 18 nicklas 230       if (dc != null) dc.close();
5029 16 Oct 18 nicklas 231       json.writeJSONString(resp.getWriter());
5029 16 Oct 18 nicklas 232     }
5029 16 Oct 18 nicklas 233     
5029 16 Oct 18 nicklas 234   }
5029 16 Oct 18 nicklas 235   
5029 16 Oct 18 nicklas 236   
5029 16 Oct 18 nicklas 237
5029 16 Oct 18 nicklas 238 }