Changeset 20774


Ignore:
Timestamp:
03/03/10 11:05:59 (2 years ago)
Author:
contrino
Message:

modmine web: cleaned and improved feature location checking

Location:
trunk/modmine/webapp/src/org/modmine/web
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/modmine/webapp/src/org/modmine/web/FeaturesAction.java

    r20768 r20774  
    1111 */ 
    1212 
    13 import java.util.ArrayList; 
    1413import java.util.HashSet; 
    1514import java.util.List; 
    16 import java.util.Map; 
    1715import java.util.Set; 
    1816 
     
    2119import javax.servlet.http.HttpSession; 
    2220 
    23 import org.apache.log4j.Logger; 
    2421import org.apache.struts.action.ActionForm; 
    2522import org.apache.struts.action.ActionForward; 
     
    6461     *  an exception 
    6562     */ 
    66      
    67      
    68      
    69     private static final Logger LOG = Logger.getLogger(MetadataCache.class); 
    70  
    71      
     63 
     64//    private static final Logger LOG = Logger.getLogger(MetadataCache.class); 
     65 
    7266    public ActionForward execute(ActionMapping mapping, 
    73                                  ActionForm form, 
    74                                  HttpServletRequest request, 
    75                                  @SuppressWarnings("unused") HttpServletResponse response) 
     67            ActionForm form, 
     68            HttpServletRequest request, 
     69            @SuppressWarnings("unused") HttpServletResponse response) 
    7670    throws Exception { 
    7771        HttpSession session = request.getSession(); 
     
    7973        ObjectStore os = im.getObjectStore(); 
    8074        Model model = im.getModel(); 
    81          
     75 
    8276        String type = (String) request.getParameter("type"); 
    8377        String featureType = (String) request.getParameter("feature"); 
     
    8579        String dccId = null; 
    8680        String experimentName = null; 
    87          
     81 
    8882        PathQuery q = new PathQuery(model); 
    89          
     83 
    9084        if (type.equals("experiment")) { 
    9185            experimentName = (String) request.getParameter("experiment"); 
    9286            DisplayExperiment exp = MetadataCache.getExperimentByName(os, experimentName); 
    93              
     87 
    9488            List<String> expSubsIds = exp.getSubmissionsDccId(); 
    95             List<String> notLocated = new ArrayList<String>(); 
    96  
    97             LOG.info("HERE THEY ARE: EXP " + expSubsIds + "<-"); 
     89            Set<String> allUnlocated = new HashSet<String>(); 
    9890 
    9991            for (String subId : expSubsIds){ 
    100                  
    101                 Integer id = Integer.valueOf(subId);  
    102                 LOG.info("HERE THEY ARE: FOR " + subId + "<->" + id +"|"); 
    103                  
    10492                if (MetadataCache.getUnlocatedFeatureTypes(os).containsKey(new Integer(subId))){ 
    105 //                if (MetadataCache.getUnlocatedFeatureTypes(os).get(subId) != null) 
    106                     LOG.info("HERE THEY ARE: LIST " + subId + "<-" ); 
    107                  
    108                     notLocated.addAll(MetadataCache.getUnlocatedFeatureTypes(os).get(new Integer(subId))); 
     93                    allUnlocated.addAll(MetadataCache.getUnlocatedFeatureTypes(os).get(new Integer(subId))); 
    10994                } 
    11095            } 
    111              
    112             Set allUnlocated =  
    113                 new HashSet(notLocated); 
    114              
    115             LOG.info("HERE THEY ARE: UNL " + allUnlocated.toString()+ "<-"); 
    116              
    117              
    118 //            Set<String> subWithNotLocatedFeats = MetadataCache.getUnlocatedFeatureTypes(os).keySet(); 
    119 //             
    120 //            Set allExpSubs =  
    121 //                new HashSet(expSubsIds); 
    122 //             
    123 //            Set<String> intersection = new HashSet<String>(allExpSubs); 
    124 //            intersection.retainAll(subWithNotLocatedFeats); 
    125 // 
    126 //            if (!intersection.isEmpty()){ 
    127 //             Set unLocatedFTypes =   MetadataCache.getUnlocatedFeatureTypes(os).entrySet()  
    128 //            } 
    129              
    13096 
    13197            if (allUnlocated.contains(featureType)){ 
     
    136102                q.addConstraint(featureType + ".submissions.experiment.name",  
    137103                        Constraints.eq(experimentName)); 
    138             
    139                 String ef = getFactors(exp);             
    140                 String description = "All " + featureType + " features generated by experiment '" 
    141                 + exp.getName() + "' in " + StringUtil.prettyList(exp.getOrganisms())  
    142                 + " (" + exp.getPi() + ")." + ef; 
    143                 q.setDescription(description); 
    144                  
     104 
    145105            } else { 
    146106                q.addView(featureType + ".primaryIdentifier"); 
     
    156116                q.addOrderBy(featureType + ".chromosome.primaryIdentifier");  
    157117                q.addOrderBy(featureType + ".chromosomeLocation.start");  
    158             
    159                 String ef = getFactors(exp);             
    160                 String description = "All " + featureType + " features generated by experiment '" 
    161                 + exp.getName() + "' in " + StringUtil.prettyList(exp.getOrganisms())  
    162                 + " (" + exp.getPi() + ")." + ef; 
    163                 q.setDescription(description); 
    164                  
    165             } 
    166              
    167              
    168                          
    169 //            q.addView(featureType + ".primaryIdentifier"); 
    170 //            q.addView(featureType + ".score"); 
    171 //            q.addView(featureType + ".chromosome.primaryIdentifier"); 
    172 //            q.addView(featureType + ".chromosomeLocation.start"); 
    173 //            q.addView(featureType + ".chromosomeLocation.end"); 
    174 //            q.addView(featureType + ".chromosomeLocation.strand"); 
    175 //            q.addView(featureType + ".submissions:experimentalFactors.type"); 
    176 //            q.addView(featureType + ".submissions:experimentalFactors.name"); 
    177 //            q.addConstraint(featureType + ".submissions.experiment.name",  
    178 //                    Constraints.eq(experimentName)); 
    179 //            q.addOrderBy(featureType + ".chromosome.primaryIdentifier");  
    180 //            q.addOrderBy(featureType + ".chromosomeLocation.start");  
    181 //        
    182 //            String ef = getFactors(exp);             
    183 //            String description = "All " + featureType + " features generated by experiment '" 
    184 //            + exp.getName() + "' in " + StringUtil.prettyList(exp.getOrganisms())  
    185 //            + " (" + exp.getPi() + ")." + ef; 
    186 //            q.setDescription(description); 
    187              
     118 
     119            } 
     120 
     121            String ef = getFactors(exp);             
     122            String description = "All " + featureType + " features generated by experiment '" 
     123            + exp.getName() + "' in " + StringUtil.prettyList(exp.getOrganisms())  
     124            + " (" + exp.getPi() + ")." + ef; 
     125            q.setDescription(description); 
     126 
    188127        } else if (type.equals("submission")) { 
    189128            dccId = (String) request.getParameter("submission"); 
    190129            Submission sub = MetadataCache.getSubmissionByDccId(os, new Integer(dccId)); 
    191130 
    192 //            Map <Integer, String> QQ = MetadataCache.getLocatedFeatureTypes(os); 
    193 //            String  locFeature = MetadataCache.getLocatedFeatureTypes(os).get(dccId); 
    194 //            int dccNr = new Integer(dccId).intValue(); 
    195 //            String qq = QQ.get(dccNr); 
    196  
    197             List<String>  locFeatures = MetadataCache.getLocatedFeatureTypes(os).get(new Integer(dccId)); 
    198  
    199 //          Map <Integer, String> QQ = MetadataCache.getLocatedFeatureTypes(os); 
    200 //          String  locFeature = MetadataCache.getLocatedFeatureTypes(os).get(ddccId); 
    201  
    202             //            LOG.info("HERE THEY ARE: " +QQ + "|" +qq + "<-"); 
    203              
    204             LOG.info("HERE THEY ARE: " + dccId + "|" +locFeatures + "<-"); 
    205              
    206             if (locFeatures.contains(featureType)){ 
     131            List<String>  unlocFeatures = MetadataCache.getUnlocatedFeatureTypes(os).get(new Integer(dccId)); 
     132 
     133            if (!unlocFeatures.contains(featureType)){ 
    207134                q.addView(featureType + ".primaryIdentifier"); 
    208135                q.addView(featureType + ".score"); 
     
    216143                q.addOrderBy(featureType + ".chromosome.primaryIdentifier");  
    217144                q.addOrderBy(featureType + ".chromosomeLocation.start");  
    218                  
    219                 String experimentType = ""; 
    220                 if (sub.getExperimentType() != null) { 
    221                     experimentType = StringUtil.indefiniteArticle(sub.getExperimentType())  
    222                     + " " + sub.getExperimentType() + " experiment in"; 
    223                 } 
    224                  
    225                 String efSub = ""; 
    226                 if (SubmissionHelper.getExperimentalFactorString(sub).length() > 1){ 
    227                     efSub = " using " + SubmissionHelper.getExperimentalFactorString(sub); 
    228                 } 
    229                  
    230                 String description = "All " + featureType + " features generated by submission " + dccId 
    231                     + ", " + experimentType + " " 
    232                     + sub.getOrganism().getShortName() + efSub  
    233                     + " (" + sub.getProject().getSurnamePI() + ")."; 
    234                 q.setDescription(description); 
    235145 
    236146            } else { 
     
    241151                q.addView(featureType + ".submissions:experimentalFactors.name"); 
    242152                q.addConstraint(featureType + ".submissions.DCCid", Constraints.eq(new Integer(dccId))); 
    243                  
    244                 String experimentType = ""; 
    245                 if (sub.getExperimentType() != null) { 
    246                     experimentType = StringUtil.indefiniteArticle(sub.getExperimentType())  
    247                     + " " + sub.getExperimentType() + " experiment in"; 
    248                 } 
    249                  
    250                 String efSub = ""; 
    251                 if (SubmissionHelper.getExperimentalFactorString(sub).length() > 1){ 
    252                     efSub = " using " + SubmissionHelper.getExperimentalFactorString(sub); 
    253                 } 
    254                  
    255                 String description = "All " + featureType + " features generated by submission " + dccId 
    256                     + ", " + experimentType + " " 
    257                     + sub.getOrganism().getShortName() + efSub  
    258                     + " (" + sub.getProject().getSurnamePI() + ")."; 
    259                 q.setDescription(description); 
    260             } 
    261              
    262              
    263         } 
    264          
     153            } 
     154 
     155            String experimentType = ""; 
     156            if (sub.getExperimentType() != null) { 
     157                experimentType = StringUtil.indefiniteArticle(sub.getExperimentType())  
     158                + " " + sub.getExperimentType() + " experiment in"; 
     159            } 
     160 
     161            String efSub = ""; 
     162            if (SubmissionHelper.getExperimentalFactorString(sub).length() > 1){ 
     163                efSub = " using " + SubmissionHelper.getExperimentalFactorString(sub); 
     164            } 
     165 
     166            String description = "All " + featureType + " features generated by submission " + dccId 
     167            + ", " + experimentType + " " 
     168            + sub.getOrganism().getShortName() + efSub  
     169            + " (" + sub.getProject().getSurnamePI() + ")."; 
     170            q.setDescription(description); 
     171        } 
     172 
    265173        if (action.equals("results")) { 
    266174            String qid = SessionMethods.startQueryWithTimeout(request, false, q); 
     
    272180        } else if (action.equals("export")) { 
    273181            String format = request.getParameter("format"); 
    274              
     182 
    275183            Profile profile = SessionMethods.getProfile(session); 
    276184            WebResultsExecutor executor = im.getWebResultsExecutor(profile); 
     
    296204            // will get only required export data 
    297205            return null; 
    298              
     206 
    299207        } else if (action.equals("list")) { 
    300208            // need to select just id of featureType to create list 
     
    306214 
    307215            String bagName = (dccId != null ? "submission_" + dccId : experimentName) 
    308                 + "_" + featureType + "_features";             
     216            + "_" + featureType + "_features";             
    309217            bagName = NameUtil.generateNewName(profile.getSavedBags().keySet(), bagName); 
    310218            BagHelper.createBagFromPathQuery(q, bagName, q.getDescription(), featureType, profile, 
  • trunk/modmine/webapp/src/org/modmine/web/MetadataCache.java

    r20768 r20774  
    107107    /** 
    108108     * Fetch experiment details for display. 
    109      * @param os the production objectstore 
     109     * @param os the production objectStore 
    110110     * @return a list of experiments 
    111111     */ 
     
    131131 
    132132    /** 
    133      * Fetch input/output file names per submission. 
    134      * @param os the production objectstore 
    135      * @return map 
     133     * Fetch unlocated feature types per submission. 
     134     * @param os the production objectStore 
     135     * @return map of unlocated feature types 
    136136     */ 
    137137    public static synchronized Map<Integer, List<String>> getUnlocatedFeatureTypes(ObjectStore os) { 
    138138        if (submissionUnlocatedFeatureTypes == null) { 
    139             LOG.info("BEFORE readUNL "); 
    140              
    141139            readUnlocatedFeatureTypes(os); 
    142             LOG.info("AFTER readUNL "); 
    143140        } 
    144141        return submissionUnlocatedFeatureTypes; 
     
    147144    /** 
    148145     * Fetch input/output file names per submission. 
    149      * @param os the production objectstore 
     146     * @param os the production objectStore 
    150147     * @return map 
    151148     */ 
     
    159156    /** 
    160157     * Fetch number of input/output file per submission. 
    161      * @param os the production objectstore 
     158     * @param os the production objectStore 
    162159     * @return map 
    163160     */ 
     
    178175 
    179176    /** 
    180      * Fetch which featureType are located for each sub 
    181      * TODO: the opposite (not located feature types) 
    182      * @param os the production objectstore 
    183      * @return map 
    184      */ 
    185     public static synchronized Map<Integer, List<String>> getLocatedFeatureTypes(ObjectStore os) { 
    186         if (submissionLocatedFeatureTypes == null) { 
    187             LOG.info("BEFORE readSubmissionLocatedFeature "); 
    188             readSubmissionLocatedFeature(os); 
    189             LOG.info("AFTER readSubmissionLocatedFeature "); 
     177     * Fetch a list of file names for a given submission. 
     178     * @param os the objectStore 
     179     * @param dccId the modENCODE submission id 
     180     * @return a list of file names 
     181     */ 
     182    public static synchronized List<String> getFilesByDccId(ObjectStore os, 
     183            Integer dccId) { 
     184        if (submissionFilesCache == null) { 
     185            readSubmissionFiles(os); 
     186        } 
     187        return new ArrayList<String>(submissionFilesCache.get(dccId)); 
     188    } 
     189 
     190    /** 
     191     * Fetch a list of file names for a given submission. 
     192     * @param dccId the modENCODE submission id 
     193     * @return a list of file names 
     194     */ 
     195    public static synchronized List<GBrowseTrack> getTracksByDccId(Integer dccId) { 
     196        if (submissionTracksCache == null) { 
     197            readGBrowseTracks(); 
     198        } 
     199        return new ArrayList<GBrowseTrack>(submissionTracksCache.get(dccId)); 
     200    } 
     201 
     202    /** 
     203     * Fetch a map from feature type to count for a given submission. 
     204     * @param os the objectStore 
     205     * @param dccId the modENCODE submission id 
     206     * @return a map from feature type to count 
     207     */ 
     208    public static synchronized Map<String, Long> getSubmissionFeatureCounts(ObjectStore os, 
     209            Integer dccId) { 
     210        if (submissionFeatureCounts == null) { 
     211            readSubmissionFeatureCounts(os); 
     212        } 
     213        return submissionFeatureCounts.get(dccId); 
     214    } 
     215 
     216    /** 
     217     * Fetch a submission by the modENCODE submission ids 
     218     * @param os the objectStore 
     219     * @param dccId the modENCODE submission id 
     220     * @return the requested submission 
     221     * @throws ObjectStoreException if error reading database 
     222     */ 
     223    public static synchronized Submission getSubmissionByDccId(ObjectStore os, Integer dccId) 
     224    throws ObjectStoreException { 
     225        if (submissionIdCache == null) { 
     226            readSubmissionFeatureCounts(os); 
     227        } 
     228        return (Submission) os.getObjectById(submissionIdCache.get(dccId)); 
     229    } 
     230 
     231    /** 
     232     * Get experiment information by name 
     233     * @param os the objectStore 
     234     * @param name of the experiment to fetch 
     235     * @return details of the experiment 
     236     * @throws ObjectStoreException if error reading database 
     237     */ 
     238    public static synchronized DisplayExperiment getExperimentByName(ObjectStore os, String name) 
     239    throws ObjectStoreException { 
     240        if (experimentCache == null) { 
     241            readExperiments(os); 
     242        } 
     243        return experimentCache.get(name); 
     244    } 
     245 
     246    
     247    /** 
     248     * Method to obtain the map of unlocated feature types by submission id 
     249     * 
     250     * @param os the objectStore 
     251     * @return submissionUnlocatedFeatureTypes 
     252     */ 
     253    private static Map<Integer, List<String>> readUnlocatedFeatureTypes(ObjectStore os) { 
     254        try { 
     255 
     256            if (submissionUnlocatedFeatureTypes != null) { 
     257                return submissionUnlocatedFeatureTypes; 
     258            } 
     259 
     260            submissionUnlocatedFeatureTypes = new HashMap<Integer, List<String>>(); 
     261 
    190262             
    191         } 
    192         return submissionLocatedFeatureTypes; 
     263            if (submissionLocatedFeatureTypes == null) { 
     264                readSubmissionLocatedFeature(os); 
     265            } 
     266 
     267            if (submissionFeatureCounts == null) { 
     268                readSubmissionFeatureCounts(os); 
     269            } 
     270 
     271            for (Integer subId : submissionFeatureCounts.keySet()) { 
     272 
     273                Set<String> allFeatures = submissionFeatureCounts.get(subId).keySet(); 
     274                Set<String> difference = new HashSet<String>(allFeatures); 
     275                difference.removeAll(submissionLocatedFeatureTypes.get(subId)); 
     276                 
     277                if (!difference.isEmpty()){ 
     278                    List <String> thisUnlocated = new ArrayList<String>(); 
     279 
     280                    for (String fType : difference){ 
     281                        thisUnlocated.add(fType); 
     282                    } 
     283                    submissionUnlocatedFeatureTypes.put(subId, thisUnlocated); 
     284                } 
     285            } 
     286        } catch (Exception err) { 
     287            err.printStackTrace(); 
     288        } 
     289        return submissionUnlocatedFeatureTypes; 
     290    } 
     291 
     292     
     293    public static Map<String, List<GBrowseTrack>> getExperimentGBrowseTracks(ObjectStore os) { 
     294        Map<String, List<GBrowseTrack>> tracks = new HashMap<String, List<GBrowseTrack>>(); 
     295 
     296        Map<Integer, List<GBrowseTrack>> subTracksMap = getGBrowseTracks(); 
     297 
     298        for (DisplayExperiment exp : getExperiments(os)) { 
     299            List<GBrowseTrack> expTracks = new ArrayList<GBrowseTrack>(); 
     300            tracks.put(exp.getName(), expTracks); 
     301            for (Submission sub : exp.getSubmissions()) { 
     302                List<GBrowseTrack> subTracks = subTracksMap.get(sub.getdCCid()); 
     303                if (subTracks != null) { 
     304                    // check so it is unique 
     305                    // expTracks.addAll(subTracks); 
     306                    addToList(expTracks, subTracks); 
     307                } 
     308            } 
     309        } 
     310        return tracks; 
     311    } 
     312 
     313    /** 
     314     * adds the elements of a list i to a list l only if they are not yet 
     315     * there 
     316     * @param l the receiving list 
     317     * @param i the donating list 
     318     */ 
     319    private static void addToList(List<GBrowseTrack> l, List<GBrowseTrack> i) { 
     320        Iterator <GBrowseTrack> it  = i.iterator(); 
     321        while (it.hasNext()) { 
     322            GBrowseTrack thisId = it.next(); 
     323            if (!l.contains(thisId)) { 
     324                l.add(thisId); 
     325            } 
     326        } 
     327    } 
     328 
     329 
     330 
     331 
     332 
     333    /** 
     334     * Fetch a map from project name to experiment. 
     335     * @param os the production ObjectStore 
     336     * @return a map from project name to experiment 
     337     */ 
     338    public static Map<String, List<DisplayExperiment>> 
     339    getProjectExperiments(ObjectStore os) { 
     340        long startTime = System.currentTimeMillis(); 
     341        Map<String, List<DisplayExperiment>> projectExperiments 
     342        = new TreeMap<String, List<DisplayExperiment>>(); 
     343        for (DisplayExperiment exp : getExperiments(os)) { 
     344            List<DisplayExperiment> exps = projectExperiments.get(exp.getProjectName()); 
     345            if (exps == null) { 
     346                exps = new ArrayList<DisplayExperiment>(); 
     347                projectExperiments.put(exp.getProjectName(), exps); 
     348            } 
     349            exps.add(exp); 
     350        } 
     351        long totalTime = System.currentTimeMillis() - startTime; 
     352        LOG.info("Made project map: " + projectExperiments.size() 
     353                + " took: " + totalTime + " ms."); 
     354        return projectExperiments; 
     355    } 
     356 
     357    private static void readExperiments(ObjectStore os) { 
     358        long startTime = System.currentTimeMillis(); 
     359        Map <String, Map<String, Long>> featureCounts = getExperimentFeatureCounts(os); 
     360 
     361        try { 
     362            Query q = new Query(); 
     363            QueryClass qcProject = new QueryClass(Project.class); 
     364            QueryField qcName = new QueryField(qcProject, "name"); 
     365 
     366            q.addFrom(qcProject); 
     367            q.addToSelect(qcProject); 
     368 
     369            QueryClass qcExperiment = new QueryClass(Experiment.class); 
     370            q.addFrom(qcExperiment); 
     371            q.addToSelect(qcExperiment); 
     372 
     373            QueryCollectionReference projExperiments = new QueryCollectionReference(qcProject, 
     374            "experiments"); 
     375            ContainsConstraint cc = new ContainsConstraint(projExperiments, ConstraintOp.CONTAINS, 
     376                    qcExperiment); 
     377 
     378            q.setConstraint(cc); 
     379            q.addToOrderBy(qcName); 
     380 
     381            Results results = os.execute(q); 
     382 
     383            experimentCache = new HashMap<String, DisplayExperiment>(); 
     384 
     385            Iterator i = results.iterator(); 
     386            while (i.hasNext()) { 
     387                ResultsRow row = (ResultsRow) i.next(); 
     388 
     389                Project project = (Project) row.get(0); 
     390                Experiment experiment = (Experiment) row.get(1); 
     391 
     392                Map<String, Long> expFeatureCounts = featureCounts.get(experiment.getName()); 
     393                DisplayExperiment displayExp = new DisplayExperiment(experiment, project, 
     394                        expFeatureCounts, os); 
     395                experimentCache.put(displayExp.getName(), displayExp); 
     396            } 
     397        } catch (Exception err) { 
     398            err.printStackTrace(); 
     399        } 
     400        long timeTaken = System.currentTimeMillis() - startTime; 
     401        LOG.info("Primed experiment cache, took: " + timeTaken + "ms"); 
     402    } 
     403 
     404 
     405    private static Map<String, Map<String, Long>> getExperimentFeatureCounts(ObjectStore os) { 
     406        long startTime = System.currentTimeMillis(); 
     407        Query q = new Query(); 
     408        q.setDistinct(false); 
     409 
     410        QueryClass qcExp = new QueryClass(Experiment.class); 
     411        QueryClass qcSub = new QueryClass(Submission.class); 
     412        QueryClass qcLsf = new QueryClass(LocatedSequenceFeature.class); 
     413 
     414        QueryField qfName = new QueryField(qcExp, "name"); 
     415        QueryField qfClass = new QueryField(qcLsf, "class"); 
     416 
     417        q.addFrom(qcSub); 
     418        q.addFrom(qcLsf); 
     419        q.addFrom(qcExp); 
     420 
     421        q.addToSelect(qfName); 
     422        q.addToSelect(qfClass); 
     423        q.addToSelect(new QueryFunction()); 
     424 
     425        q.addToGroupBy(qfName); 
     426        q.addToGroupBy(qfClass); 
     427 
     428        q.addToOrderBy(qfName); 
     429        q.addToOrderBy(qfClass); 
     430 
     431        ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); 
     432 
     433        QueryCollectionReference submissions = new QueryCollectionReference(qcExp, "submissions"); 
     434        ContainsConstraint ccSubs = new ContainsConstraint(submissions, ConstraintOp.CONTAINS, 
     435                qcSub); 
     436        cs.addConstraint(ccSubs); 
     437 
     438        QueryCollectionReference features = new QueryCollectionReference(qcSub, "features"); 
     439        ContainsConstraint ccFeats = new ContainsConstraint(features, ConstraintOp.CONTAINS, qcLsf); 
     440        cs.addConstraint(ccFeats); 
     441 
     442        q.setConstraint(cs); 
     443 
     444        Results results = os.execute(q); 
     445 
     446        Map<String, Map<String, Long>> featureCounts = 
     447            new LinkedHashMap<String, Map<String, Long>>(); 
     448 
     449        // for each classes set the values for jsp 
     450        for (Iterator<ResultsRow> iter = results.iterator(); iter.hasNext(); ) { 
     451            ResultsRow row = iter.next(); 
     452            String expName = (String) row.get(0); 
     453            Class feat = (Class) row.get(1); 
     454            Long count = (Long) row.get(2); 
     455 
     456            Map<String, Long> expFeatureCounts = featureCounts.get(expName); 
     457            if (expFeatureCounts == null) { 
     458                expFeatureCounts = new HashMap<String, Long>(); 
     459                featureCounts.put(expName, expFeatureCounts); 
     460            } 
     461            expFeatureCounts.put(TypeUtil.unqualifiedName(feat.getName()), count); 
     462        } 
     463        long timeTaken = System.currentTimeMillis() - startTime; 
     464        LOG.info("Read experiment feature counts, took: " + timeTaken + "ms"); 
     465 
     466        return featureCounts; 
     467    } 
     468 
     469    private static void readSubmissionFeatureCounts(ObjectStore os) { 
     470        long startTime = System.currentTimeMillis(); 
     471 
     472        submissionFeatureCounts = new LinkedHashMap<Integer, Map<String, Long>>(); 
     473        submissionIdCache = new HashMap<Integer, Integer>(); 
     474 
     475        Query q = new Query(); 
     476        q.setDistinct(false); 
     477 
     478        QueryClass qcSub = new QueryClass(Submission.class); 
     479        QueryClass qcLsf = new QueryClass(LocatedSequenceFeature.class); 
     480 
     481        QueryField qfClass = new QueryField(qcLsf, "class"); 
     482 
     483        q.addFrom(qcSub); 
     484        q.addFrom(qcLsf); 
     485 
     486        q.addToSelect(qcSub); 
     487        q.addToSelect(qfClass); 
     488        q.addToSelect(new QueryFunction()); 
     489 
     490        q.addToGroupBy(qcSub); 
     491        q.addToGroupBy(qfClass); 
     492 
     493        q.addToOrderBy(qcSub); 
     494        q.addToOrderBy(qfClass); 
     495 
     496        ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); 
     497 
     498        QueryCollectionReference features = new QueryCollectionReference(qcSub, "features"); 
     499        ContainsConstraint ccFeats = new ContainsConstraint(features, ConstraintOp.CONTAINS, qcLsf); 
     500        cs.addConstraint(ccFeats); 
     501 
     502        q.setConstraint(cs); 
     503 
     504        Results results = os.execute(q); 
     505 
     506        // for each classes set the values for jsp 
     507        for (Iterator<ResultsRow> iter = results.iterator(); iter.hasNext(); ) { 
     508            ResultsRow row = iter.next(); 
     509            Submission sub = (Submission) row.get(0); 
     510            Class feat = (Class) row.get(1); 
     511            Long count = (Long) row.get(2); 
     512 
     513            submissionIdCache.put(sub.getdCCid(), sub.getId()); 
     514 
     515            Map<String, Long> featureCounts = submissionFeatureCounts.get(sub.getdCCid()); 
     516            if (featureCounts == null) { 
     517                featureCounts = new HashMap<String, Long>(); 
     518                submissionFeatureCounts.put(sub.getdCCid(), featureCounts); 
     519            } 
     520            featureCounts.put(TypeUtil.unqualifiedName(feat.getName()), count); 
     521        } 
     522        long timeTaken = System.currentTimeMillis() - startTime; 
     523        LOG.info("Primed submission cache, took: " + timeTaken + "ms"); 
    193524    } 
    194525 
     
    262593    } 
    263594 
    264     public static Map<String, List<GBrowseTrack>> getExperimentGBrowseTracks(ObjectStore os) { 
    265         Map<String, List<GBrowseTrack>> tracks = new HashMap<String, List<GBrowseTrack>>(); 
    266  
    267         Map<Integer, List<GBrowseTrack>> subTracksMap = getGBrowseTracks(); 
    268  
    269         for (DisplayExperiment exp : getExperiments(os)) { 
    270             List<GBrowseTrack> expTracks = new ArrayList<GBrowseTrack>(); 
    271             tracks.put(exp.getName(), expTracks); 
    272             for (Submission sub : exp.getSubmissions()) { 
    273                 List<GBrowseTrack> subTracks = subTracksMap.get(sub.getdCCid()); 
    274                 if (subTracks != null) { 
    275                     // check so it is unique 
    276                     // expTracks.addAll(subTracks); 
    277                     addToList(expTracks, subTracks); 
    278                 } 
    279             } 
    280         } 
    281         return tracks; 
    282     } 
    283  
    284     /** 
    285      * adds the elements of a list i to a list l only if they are not yet 
    286      * there 
    287      * @param l the receiving list 
    288      * @param i the donating list 
    289      */ 
    290     private static void addToList(List<GBrowseTrack> l, List<GBrowseTrack> i) { 
    291         Iterator <GBrowseTrack> it  = i.iterator(); 
    292         while (it.hasNext()) { 
    293             GBrowseTrack thisId = it.next(); 
    294             if (!l.contains(thisId)) { 
    295                 l.add(thisId); 
    296             } 
    297         } 
    298     } 
    299  
    300  
    301     /** 
    302      * Fetch a list of file names for a given submission. 
    303      * @param os the objectstore 
    304      * @param dccId the modENCODE submission id 
    305      * @return a list of file names 
    306      */ 
    307     public static synchronized List<String> getFilesByDccId(ObjectStore os, 
    308             Integer dccId) { 
    309         if (submissionFilesCache == null) { 
    310             readSubmissionFiles(os); 
    311         } 
    312         return new ArrayList<String>(submissionFilesCache.get(dccId)); 
    313     } 
    314  
    315     /** 
    316      * Fetch a list of file names for a given submission. 
    317      * @param dccId the modENCODE submission id 
    318      * @return a list of file names 
    319      */ 
    320     public static synchronized List<GBrowseTrack> getTracksByDccId(Integer dccId) { 
    321         if (submissionTracksCache == null) { 
    322             readGBrowseTracks(); 
    323         } 
    324         return new ArrayList<GBrowseTrack>(submissionTracksCache.get(dccId)); 
    325     } 
    326  
    327  
    328     /** 
    329      * Fetch a map from feature type to count for a given submission. 
    330      * @param os the objectstore 
    331      * @param dccId the modENCODE submission id 
    332      * @return a map from feature type to count 
    333      */ 
    334     public static synchronized Map<String, Long> getSubmissionFeatureCounts(ObjectStore os, 
    335             Integer dccId) { 
    336         if (submissionFeatureCounts == null) { 
    337             readSubmissionFeatureCounts(os); 
    338         } 
    339         return submissionFeatureCounts.get(dccId); 
    340     } 
    341  
    342     /** 
    343      * Fetch a submission by the modENCODE submission ids 
    344      * @param os the objectstore 
    345      * @param dccId the modENCODE submission id 
    346      * @return the requested submission 
    347      * @throws ObjectStoreException if error reading database 
    348      */ 
    349     public static synchronized Submission getSubmissionByDccId(ObjectStore os, Integer dccId) 
    350     throws ObjectStoreException { 
    351         if (submissionIdCache == null) { 
    352             readSubmissionFeatureCounts(os); 
    353         } 
    354         return (Submission) os.getObjectById(submissionIdCache.get(dccId)); 
    355     } 
    356  
    357     /** 
    358      * Get experiment information by name 
    359      * @param os the objectstore 
    360      * @param name of the experiment to fetch 
    361      * @return details of the experiment 
    362      * @throws ObjectStoreException if error reading database 
    363      */ 
    364     public static synchronized DisplayExperiment getExperimentByName(ObjectStore os, String name) 
    365     throws ObjectStoreException { 
    366         if (experimentCache == null) { 
    367             readExperiments(os); 
    368         } 
    369         return experimentCache.get(name); 
    370     } 
    371  
    372     /** 
    373      * Fetch a map from project name to experiment. 
    374      * @param os the production ObjectStore 
    375      * @return a map from project name to experiment 
    376      */ 
    377     public static Map<String, List<DisplayExperiment>> 
    378     getProjectExperiments(ObjectStore os) { 
    379         long startTime = System.currentTimeMillis(); 
    380         Map<String, List<DisplayExperiment>> projectExperiments 
    381         = new TreeMap<String, List<DisplayExperiment>>(); 
    382         for (DisplayExperiment exp : getExperiments(os)) { 
    383             List<DisplayExperiment> exps = projectExperiments.get(exp.getProjectName()); 
    384             if (exps == null) { 
    385                 exps = new ArrayList<DisplayExperiment>(); 
    386                 projectExperiments.put(exp.getProjectName(), exps); 
    387             } 
    388             exps.add(exp); 
    389         } 
    390         long totalTime = System.currentTimeMillis() - startTime; 
    391         LOG.info("Made project map: " + projectExperiments.size() 
    392                 + " took: " + totalTime + " ms."); 
    393         return projectExperiments; 
    394     } 
    395  
    396     private static void readExperiments(ObjectStore os) { 
    397         long startTime = System.currentTimeMillis(); 
    398         Map <String, Map<String, Long>> featureCounts = getExperimentFeatureCounts(os); 
    399  
    400         try { 
    401             Query q = new Query(); 
    402             QueryClass qcProject = new QueryClass(Project.class); 
    403             QueryField qcName = new QueryField(qcProject, "name"); 
    404  
    405             q.addFrom(qcProject); 
    406             q.addToSelect(qcProject); 
    407  
    408             QueryClass qcExperiment = new QueryClass(Experiment.class); 
    409             q.addFrom(qcExperiment); 
    410             q.addToSelect(qcExperiment); 
    411  
    412             QueryCollectionReference projExperiments = new QueryCollectionReference(qcProject, 
    413             "experiments"); 
    414             ContainsConstraint cc = new ContainsConstraint(projExperiments, ConstraintOp.CONTAINS, 
    415                     qcExperiment); 
    416  
    417             q.setConstraint(cc); 
    418             q.addToOrderBy(qcName); 
    419  
    420             Results results = os.execute(q); 
    421  
    422             experimentCache = new HashMap<String, DisplayExperiment>(); 
    423  
    424             Iterator i = results.iterator(); 
    425             while (i.hasNext()) { 
    426                 ResultsRow row = (ResultsRow) i.next(); 
    427  
    428                 Project project = (Project) row.get(0); 
    429                 Experiment experiment = (Experiment) row.get(1); 
    430  
    431                 Map<String, Long> expFeatureCounts = featureCounts.get(experiment.getName()); 
    432                 DisplayExperiment displayExp = new DisplayExperiment(experiment, project, 
    433                         expFeatureCounts, os); 
    434                 experimentCache.put(displayExp.getName(), displayExp); 
    435             } 
    436         } catch (Exception err) { 
    437             err.printStackTrace(); 
    438         } 
    439         long timeTaken = System.currentTimeMillis() - startTime; 
    440         LOG.info("Primed experiment cache, took: " + timeTaken + "ms"); 
    441     } 
    442  
    443  
    444     private static Map<String, Map<String, Long>> getExperimentFeatureCounts(ObjectStore os) { 
    445         long startTime = System.currentTimeMillis(); 
    446         Query q = new Query(); 
    447         q.setDistinct(false); 
    448  
    449         QueryClass qcExp = new QueryClass(Experiment.class); 
    450         QueryClass qcSub = new QueryClass(Submission.class); 
    451         QueryClass qcLsf = new QueryClass(LocatedSequenceFeature.class); 
    452  
    453         QueryField qfName = new QueryField(qcExp, "name"); 
    454         QueryField qfClass = new QueryField(qcLsf, "class"); 
    455  
    456         q.addFrom(qcSub); 
    457         q.addFrom(qcLsf); 
    458         q.addFrom(qcExp); 
    459  
    460         q.addToSelect(qfName); 
    461         q.addToSelect(qfClass); 
    462         q.addToSelect(new QueryFunction()); 
    463  
    464         q.addToGroupBy(qfName); 
    465         q.addToGroupBy(qfClass); 
    466  
    467         q.addToOrderBy(qfName); 
    468         q.addToOrderBy(qfClass); 
    469  
    470         ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); 
    471  
    472         QueryCollectionReference submissions = new QueryCollectionReference(qcExp, "submissions"); 
    473         ContainsConstraint ccSubs = new ContainsConstraint(submissions, ConstraintOp.CONTAINS, 
    474                 qcSub); 
    475         cs.addConstraint(ccSubs); 
    476  
    477         QueryCollectionReference features = new QueryCollectionReference(qcSub, "features"); 
    478         ContainsConstraint ccFeats = new ContainsConstraint(features, ConstraintOp.CONTAINS, qcLsf); 
    479         cs.addConstraint(ccFeats); 
    480  
    481         q.setConstraint(cs); 
    482  
    483         Results results = os.execute(q); 
    484  
    485         Map<String, Map<String, Long>> featureCounts = 
    486             new LinkedHashMap<String, Map<String, Long>>(); 
    487  
    488         // for each classes set the values for jsp 
    489         for (Iterator<ResultsRow> iter = results.iterator(); iter.hasNext(); ) { 
    490             ResultsRow row = iter.next(); 
    491             String expName = (String) row.get(0); 
    492             Class feat = (Class) row.get(1); 
    493             Long count = (Long) row.get(2); 
    494  
    495             Map<String, Long> expFeatureCounts = featureCounts.get(expName); 
    496             if (expFeatureCounts == null) { 
    497                 expFeatureCounts = new HashMap<String, Long>(); 
    498                 featureCounts.put(expName, expFeatureCounts); 
    499             } 
    500             expFeatureCounts.put(TypeUtil.unqualifiedName(feat.getName()), count); 
    501         } 
    502         long timeTaken = System.currentTimeMillis() - startTime; 
    503         LOG.info("Read experiment feature counts, took: " + timeTaken + "ms"); 
    504  
    505         return featureCounts; 
    506     } 
    507  
    508     private static void readSubmissionFeatureCounts(ObjectStore os) { 
    509         long startTime = System.currentTimeMillis(); 
    510  
    511         submissionFeatureCounts = new LinkedHashMap<Integer, Map<String, Long>>(); 
    512         submissionIdCache = new HashMap<Integer, Integer>(); 
    513  
    514         Query q = new Query(); 
    515         q.setDistinct(false); 
    516  
    517         QueryClass qcSub = new QueryClass(Submission.class); 
    518         QueryClass qcLsf = new QueryClass(LocatedSequenceFeature.class); 
    519  
    520         QueryField qfClass = new QueryField(qcLsf, "class"); 
    521  
    522         q.addFrom(qcSub); 
    523         q.addFrom(qcLsf); 
    524  
    525         q.addToSelect(qcSub); 
    526         q.addToSelect(qfClass); 
    527         q.addToSelect(new QueryFunction()); 
    528  
    529         q.addToGroupBy(qcSub); 
    530         q.addToGroupBy(qfClass); 
    531  
    532         q.addToOrderBy(qcSub); 
    533         q.addToOrderBy(qfClass); 
    534  
    535         ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); 
    536  
    537         QueryCollectionReference features = new QueryCollectionReference(qcSub, "features"); 
    538         ContainsConstraint ccFeats = new ContainsConstraint(features, ConstraintOp.CONTAINS, qcLsf); 
    539         cs.addConstraint(ccFeats); 
    540  
    541         q.setConstraint(cs); 
    542  
    543         Results results = os.execute(q); 
    544  
    545  
    546         // for each classes set the values for jsp 
    547         for (Iterator<ResultsRow> iter = results.iterator(); iter.hasNext(); ) { 
    548             ResultsRow row = iter.next(); 
    549             Submission sub = (Submission) row.get(0); 
    550             Class feat = (Class) row.get(1); 
    551             Long count = (Long) row.get(2); 
    552  
    553             submissionIdCache.put(sub.getdCCid(), sub.getId()); 
    554  
    555             Map<String, Long> featureCounts = submissionFeatureCounts.get(sub.getdCCid()); 
    556             if (featureCounts == null) { 
    557                 featureCounts = new HashMap<String, Long>(); 
    558                 submissionFeatureCounts.put(sub.getdCCid(), featureCounts); 
    559             } 
    560             featureCounts.put(TypeUtil.unqualifiedName(feat.getName()), count); 
    561         } 
    562         long timeTaken = System.currentTimeMillis() - startTime; 
    563         LOG.info("Primed submission cache, took: " + timeTaken + "ms"); 
    564     } 
    565  
    566  
    567      
    568595    private static void readSubmissionLocatedFeature(ObjectStore os) { 
    569596        long startTime = System.currentTimeMillis(); 
     
    606633            Class feat = (Class) row.get(1); 
    607634  
    608 //            addToMap(submissionLocatedFeatureTypes,sub.getdCCid(), 
    609 //            feat.getName().replace("org.intermine.model.bio.", "")); 
    610    
    611635            addToMap(submissionLocatedFeatureTypes,sub.getdCCid(), 
    612636                    TypeUtil.unqualifiedName(feat.getName())); 
    613              
    614              
    615 //            submissionLocatedFeatureTypes.put(sub.getdCCid().toString(),  
    616 //                    feat.getName().replace("org.intermine.model.bio.", ""));             
    617637        }      
    618638        long timeTaken = System.currentTimeMillis() - startTime; 
    619639        LOG.info("Primed located features cache, took: " + timeTaken + "ms"); 
    620          
    621         LOG.info("HERE THEY ARE: " + submissionLocatedFeatureTypes); 
    622          
    623     } 
    624      
    625     
    626      
    627     /** 
    628      * Method to obtain the map of unlocated feature types by submission id 
    629      * 
    630      * @param os the objectStore 
    631      * @return submissionUnlocatedFeatureTypes 
    632      */ 
    633     private static Map<Integer, List<String>> readUnlocatedFeatureTypes(ObjectStore os) { 
    634         try { 
    635  
    636             if (submissionUnlocatedFeatureTypes != null) { 
    637                 return submissionUnlocatedFeatureTypes; 
    638             } 
    639  
    640             submissionUnlocatedFeatureTypes = new HashMap<Integer, List<String>>(); 
    641  
    642              
    643             if (submissionLocatedFeatureTypes == null) { 
    644                 readSubmissionLocatedFeature(os); 
    645             } 
    646  
    647             if (submissionFeatureCounts == null) { 
    648                 readSubmissionFeatureCounts(os); 
    649             } 
    650  
    651             LOG.info("INTO readUNL " + submissionFeatureCounts.keySet()); 
    652  
    653              
    654             for (Integer subId : submissionFeatureCounts.keySet()) { 
    655  
    656                 Set<String> allFeatures = submissionFeatureCounts.get(subId).keySet(); 
    657                 LOG.info("INTO readUNL SFC keys:" + submissionFeatureCounts.keySet()); 
    658                  
    659                  
    660                 LOG.info("INTO readUNL difference 1 " + subId); 
    661                 Set<String> difference = new HashSet<String>(allFeatures); 
    662                 LOG.info("INTO readUNL difference 2 " + difference); 
    663  
    664                 LOG.info("INTO readUNL difference ARG " + submissionLocatedFeatureTypes.get(subId)); 
    665                 
    666                 difference.removeAll(submissionLocatedFeatureTypes.get(subId)); 
    667                  
    668                 LOG.info("INTO readUNL difference 3 " + subId); 
    669                 LOG.info("INTO readUNL difference-> " + difference); 
    670  
    671                 if (!difference.isEmpty()){ 
    672                     List <String> thisUnlocated = new ArrayList<String>(); 
    673  
    674                     for (String fType : difference){ 
    675                         LOG.info("INTO readUNL ftype: " + subId + "|" + fType); 
    676                          
    677                         thisUnlocated.add(fType); 
    678                     } 
    679                     submissionUnlocatedFeatureTypes.put(subId, thisUnlocated); 
    680                 } 
    681             } 
    682         } catch (Exception err) { 
    683             err.printStackTrace(); 
    684         } 
    685         return submissionUnlocatedFeatureTypes; 
    686     } 
    687  
     640    } 
    688641     
    689642    /** 
Note: See TracChangeset for help on using the changeset viewer.