Skip to content

Commit f9755d6

Browse files
Minor code cleanup and correctness improvements (#1186)
1 parent 34366e7 commit f9755d6

2 files changed

Lines changed: 61 additions & 39 deletions

File tree

api-src/org/labkey/api/targetedms/model/QCMetricConfiguration.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616
package org.labkey.api.targetedms.model;
1717

1818
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
1920
import org.json.JSONObject;
2021

2122
public class QCMetricConfiguration implements Comparable<QCMetricConfiguration>
2223
{
24+
public enum TimeValueOption
25+
{
26+
First, Last, Min, Max
27+
}
28+
2329
private int _id;
2430
private String _name;
2531
private String _queryName;
@@ -124,6 +130,21 @@ public void setTimeValueOption(String timeValueOption)
124130
_timeValueOption = timeValueOption;
125131
}
126132

133+
@Nullable
134+
public TimeValueOption getParsedTimeValueOption()
135+
{
136+
if (_timeValueOption == null)
137+
return null;
138+
try
139+
{
140+
return TimeValueOption.valueOf(_timeValueOption);
141+
}
142+
catch (IllegalArgumentException e)
143+
{
144+
return null;
145+
}
146+
}
147+
127148
public Double getTraceValue()
128149
{
129150
return _traceValue;

src/org/labkey/targetedms/TargetedMSManager.java

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private TargetedMSManager()
163163
* client rendering the overview, all of which need to know the enabled configs.
164164
*/
165165
private static final Cache<Container, List<QCMetricConfiguration>> _metricCache = CacheManager.getBlockingCache(1000, TimeUnit.HOURS.toMillis(1), "Enabled QC metric configs",
166-
(c, argument) ->
166+
(_, argument) ->
167167
{
168168
try
169169
{
@@ -1048,7 +1048,7 @@ public static TargetedMSRun getRunByFileName(String fileName, Container containe
10481048
List<TargetedMSRun> matches = new TableSelector(TargetedMSManager.getTableInfoRuns(), filter, null).getArrayList(TargetedMSRun.class);
10491049
if (matches.size() == 1)
10501050
{
1051-
return matches.get(0);
1051+
return matches.getFirst();
10521052
}
10531053
return null;
10541054
}
@@ -1084,7 +1084,7 @@ else if(representativeState == RunRepresentativeDataState.Representative_Peptide
10841084
// for the documents not yet imported.
10851085
updateSql.append(" AND StatusId = ? ");
10861086
updateSql.add(SkylineDocImporter.STATUS_SUCCESS);
1087-
updateSql.append(" AND Id NOT IN ("+StringUtils.join(representativeRunIds, ",")+")");
1087+
updateSql.append(" AND Id NOT IN (").append(StringUtils.join(representativeRunIds, ",")).append(")");
10881088

10891089
new SqlExecutor(TargetedMSManager.getSchema()).execute(updateSql);
10901090
}
@@ -1125,7 +1125,7 @@ private static List<Long> getProteinRepresentativeRunIds(Container container, in
11251125
reprRunIdSql.append(TargetedMSManager.getTableInfoRuns(), "runs");
11261126
String states = StringUtils.join(stateArray, ',');
11271127

1128-
reprRunIdSql.append(" WHERE pepgrp.RepresentativeDataState IN (" + states + ")");
1128+
reprRunIdSql.append(" WHERE pepgrp.RepresentativeDataState IN (").append(states).append(")");
11291129
reprRunIdSql.append(" AND runs.Container = ?");
11301130
reprRunIdSql.add(container);
11311131
reprRunIdSql.append(" AND runs.Id = pepgrp.RunId");
@@ -1160,7 +1160,7 @@ private static List<Long> getPeptideRepresentativeRunIds(Container container, in
11601160

11611161
String states = StringUtils.join(stateArray, ',');
11621162

1163-
reprRunIdSql.append(" WHERE gp.RepresentativeDataState IN (" + states + ")");
1163+
reprRunIdSql.append(" WHERE gp.RepresentativeDataState IN (").append(states).append(")");
11641164
reprRunIdSql.append(" AND gp.GeneralMoleculeId = gm.Id");
11651165
reprRunIdSql.append(" AND gm.PeptideGroupId = pepgrp.Id");
11661166
reprRunIdSql.append(" AND Container = ?");
@@ -1918,19 +1918,15 @@ private static void purgeDeletedRuns()
19181918
// Delete from all list-related tables
19191919
deleteListDependent();
19201920

1921+
// Get a list of runs to be deleted so that we can flush them from the cache
1922+
SQLFragment sql = new SQLFragment("SELECT Id FROM " + getTableInfoRuns() + " WHERE Deleted = ?", true);
1923+
List<Long> deletedRunIds = new SqlSelector(getSchema(), sql).getArrayList(Long.class);
1924+
19211925
// Delete from runs
19221926
execute("DELETE FROM " + getTableInfoRuns() + " WHERE Deleted = ?", true);
19231927

19241928
// Remove any cached results for the deleted runs
1925-
removeCachedResults();
1926-
}
1927-
1928-
private static void removeCachedResults()
1929-
{
1930-
// Get a list of deleted runs
1931-
SQLFragment sql = new SQLFragment("SELECT Id FROM " + getTableInfoRuns() + " WHERE Deleted = ?", true);
1932-
List<Long> deletedRunIds = new SqlSelector(getSchema(), sql).getArrayList(Long.class);
1933-
if(!deletedRunIds.isEmpty())
1929+
if (!deletedRunIds.isEmpty())
19341930
{
19351931
ModificationManager.removeRunCachedResults(deletedRunIds);
19361932
PeptideManager.removeRunCachedResults(deletedRunIds);
@@ -2155,18 +2151,22 @@ public static void renameRun(long runId, String newDescription, User user) throw
21552151
if (newDescription == null || newDescription.isEmpty())
21562152
return;
21572153

2158-
new SqlExecutor(getSchema()).execute("UPDATE " + getTableInfoRuns() + " SET Description=? WHERE Id = ?",
2159-
newDescription, runId);
2160-
TargetedMSRun run = getRun(runId);
2161-
if (run != null)
2154+
try (DbScope.Transaction t = getSchema().getScope().ensureTransaction())
21622155
{
2163-
// Keep the experiment run wrapper in sync
2164-
ExpRun expRun = ExperimentService.get().getExpRun(run.getExperimentRunLSID());
2165-
if (expRun != null)
2156+
new SqlExecutor(getSchema()).execute("UPDATE " + getTableInfoRuns() + " SET Description=? WHERE Id = ?",
2157+
newDescription, runId);
2158+
TargetedMSRun run = getRun(runId);
2159+
if (run != null)
21662160
{
2167-
expRun.setName(newDescription);
2168-
expRun.save(user);
2161+
// Keep the experiment run wrapper in sync
2162+
ExpRun expRun = ExperimentService.get().getExpRun(run.getExperimentRunLSID());
2163+
if (expRun != null)
2164+
{
2165+
expRun.setName(newDescription);
2166+
expRun.save(user);
2167+
}
21692168
}
2169+
t.commit();
21702170
}
21712171
}
21722172

@@ -2199,7 +2199,7 @@ public static SampleFile getSampleFile(long id, Container container)
21992199
{
22002200
throw new IllegalStateException("More than one SampleFile for Id " + id);
22012201
}
2202-
return matches.isEmpty() ? null : matches.get(0);
2202+
return matches.isEmpty() ? null : matches.getFirst();
22032203
}
22042204

22052205
public static List<SampleFile> getSampleFiles(Container container, @Nullable SQLFragment whereClause)
@@ -2813,13 +2813,6 @@ public static TransitionSettings.FullScanSettings getTransitionFullScanSettings(
28132813
.getObject(TransitionSettings.FullScanSettings.class);
28142814
}
28152815

2816-
public static TransitionSettings.Predictor getReplicatePredictor(long predictorId)
2817-
{
2818-
return new TableSelector(TargetedMSManager.getTableInfoTransitionFullScanSettings(),
2819-
new SimpleFilter(FieldKey.fromParts("Id"), predictorId), null)
2820-
.getObject(TransitionSettings.Predictor.class);
2821-
}
2822-
28232816
private static List<Long> getPredictorsToDelete()
28242817
{
28252818
SQLFragment sql = new SQLFragment("Select Id FROM " + getTableInfoPredictor() + " WHERE " +
@@ -2871,12 +2864,12 @@ public static List<QCTraceMetricValues> calculateTraceMetricValues(List<QCMetric
28712864
Double minTimeValue = qcMetricConfiguration.getMinTimeValue();
28722865
Double maxTimeValue = qcMetricConfiguration.getMaxTimeValue();
28732866
Double traceValue = qcMetricConfiguration.getTraceValue();
2874-
String timeValueOption = qcMetricConfiguration.getTimeValueOption();
2867+
QCMetricConfiguration.TimeValueOption timeValueOption = qcMetricConfiguration.getParsedTimeValueOption();
28752868
float minValue = Float.MAX_VALUE;
2876-
float maxValue = Float.MIN_VALUE;
2869+
float maxValue = -Float.MAX_VALUE;
28772870
for (int i = 0; i < times.length; i++)
28782871
{
2879-
if (timeValueOption != null && timeValueOption.equals("First"))
2872+
if (timeValueOption == QCMetricConfiguration.TimeValueOption.First)
28802873
{
28812874
// first value after the minTimeValue
28822875
if (minTimeValue != null && times[i] > minTimeValue)
@@ -2891,7 +2884,7 @@ else if (minTimeValue == null)
28912884
break;
28922885
}
28932886
}
2894-
else if (timeValueOption != null && timeValueOption.equals("Last"))
2887+
else if (timeValueOption == QCMetricConfiguration.TimeValueOption.Last)
28952888
{
28962889
// last value before the maxTimeValue
28972890
if (maxTimeValue != null && times[i] < maxTimeValue)
@@ -2904,7 +2897,7 @@ else if (maxTimeValue == null)
29042897
valuesToStore.put(sampleFileChromInfo, values[i]);
29052898
}
29062899
}
2907-
else if (timeValueOption != null && timeValueOption.equals("Min"))
2900+
else if (timeValueOption == QCMetricConfiguration.TimeValueOption.Min)
29082901
{
29092902
// minValue between minTimeValue and maxTimeValue
29102903
if (minTimeValue != null && maxTimeValue != null && times[i] >= minTimeValue && times[i] <= maxTimeValue)
@@ -2929,9 +2922,8 @@ else if (minTimeValue == null && maxTimeValue == null && values[i] < minValue)
29292922
{
29302923
minValue = values[i];
29312924
}
2932-
valuesToStore.put(sampleFileChromInfo, minValue);
29332925
}
2934-
else if (timeValueOption != null && timeValueOption.equals("Max"))
2926+
else if (timeValueOption == QCMetricConfiguration.TimeValueOption.Max)
29352927
{
29362928
// maxValue between minTimeValue and maxTimeValue
29372929
if (minTimeValue != null && maxTimeValue != null && times[i] >= minTimeValue && times[i] <= maxTimeValue)
@@ -2956,7 +2948,6 @@ else if (minTimeValue == null && maxTimeValue == null && values[i] > maxValue)
29562948
{
29572949
maxValue = values[i];
29582950
}
2959-
valuesToStore.put(sampleFileChromInfo, maxValue);
29602951
}
29612952

29622953
else if (traceValue != null && values[i] >= traceValue)
@@ -2965,6 +2956,16 @@ else if (traceValue != null && values[i] >= traceValue)
29652956
break;
29662957
}
29672958
}
2959+
2960+
// Store Min/Max only if at least one data point matched the time filter
2961+
if (timeValueOption == QCMetricConfiguration.TimeValueOption.Min && minValue != Float.MAX_VALUE)
2962+
{
2963+
valuesToStore.put(sampleFileChromInfo, minValue);
2964+
}
2965+
else if (timeValueOption == QCMetricConfiguration.TimeValueOption.Max && maxValue != -Float.MAX_VALUE)
2966+
{
2967+
valuesToStore.put(sampleFileChromInfo, maxValue);
2968+
}
29682969
}
29692970
}
29702971

0 commit comments

Comments
 (0)