Skip to content

Commit 0a7acd2

Browse files
committed
Add ability to delete unused NT records directly
1 parent 1e97a42 commit 0a7acd2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

SequenceAnalysis/resources/queries/sequenceanalysis/ref_nt_sequences.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,21 @@ function beforeDelete(row, errors){
6161
if (!this.extraContext.deleteFromServer) {
6262
errors._form = 'You cannot directly delete reference sequences. To delete these records, use the delete button above the sequences grid.';
6363
}
64+
else {
65+
const errorMessage = triggerHelper.canDeleteNtRecord(row.rowid);
66+
if (errorMessage) {
67+
errors._form = 'The sequence ' + row.rowid + ' cannot be deleted: ' + errorMessage;
68+
}
69+
}
70+
}
71+
72+
function afterDelete(row, errors){
73+
if (this.extraContext.deleteFromServer) {
74+
// Ensure no genomes use this, and then delete the sequence file:
75+
triggerHelper.safeDeleteNtRecord(row.rowid)
76+
}
77+
else {
78+
// This should be blocked in beforeDelete
79+
errors._form = 'You cannot directly delete reference sequences. To delete these records, use the delete button above the sequences grid.';
80+
}
6481
}

SequenceAnalysis/src/org/labkey/sequenceanalysis/query/SequenceTriggerHelper.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.biojava.nbio.core.sequence.compound.NucleotideCompound;
1212
import org.biojava.nbio.core.sequence.template.Sequence;
1313
import org.biojava.nbio.core.sequence.transcription.TranscriptionEngine;
14+
import org.jetbrains.annotations.Nullable;
1415
import org.junit.Assert;
1516
import org.junit.Test;
1617
import org.labkey.api.collections.IntHashMap;
@@ -29,9 +30,11 @@
2930
import org.labkey.api.query.QueryService;
3031
import org.labkey.api.security.User;
3132
import org.labkey.api.security.UserManager;
33+
import org.labkey.api.security.permissions.DeletePermission;
3234
import org.labkey.api.sequenceanalysis.RefNtSequenceModel;
3335
import org.labkey.api.util.FileUtil;
3436
import org.labkey.api.util.Path;
37+
import org.labkey.api.view.UnauthorizedException;
3538
import org.labkey.sequenceanalysis.ReadDataImpl;
3639
import org.labkey.sequenceanalysis.SequenceAnalysisSchema;
3740
import org.labkey.sequenceanalysis.SequenceAnalysisServiceImpl;
@@ -42,6 +45,7 @@
4245
import java.io.File;
4346
import java.io.IOException;
4447
import java.io.StringWriter;
48+
import java.nio.file.Files;
4549
import java.util.Arrays;
4650
import java.util.Date;
4751
import java.util.List;
@@ -324,4 +328,61 @@ public void createReaddataForSra(int readsetId, String sraAccessions)
324328
Table.insert(_user, rd, rd1);
325329
}
326330
}
331+
332+
public @Nullable String canDeleteNtRecord(int rowId)
333+
{
334+
RefNtSequenceModel model = RefNtSequenceModel.getForRowId(rowId);
335+
if (model == null)
336+
{
337+
return "Unknown ref nt record: " + rowId;
338+
}
339+
340+
Container c = ContainerManager.getForId(model.getContainer());
341+
if (c == null)
342+
{
343+
return "Unable to find container for: " + model.getContainer();
344+
}
345+
346+
if (new TableSelector(SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_REF_LIBRARY_MEMBERS), new SimpleFilter(FieldKey.fromString("ref_nt_id"), rowId), null).exists())
347+
{
348+
return "Sequence " + rowId + " is still used by genomes, cannot delete";
349+
}
350+
351+
if (!c.hasPermission(_user, DeletePermission.class))
352+
{
353+
return "The user does not have sufficient permissions to delete record: " + rowId;
354+
}
355+
356+
return null;
357+
}
358+
359+
public void safeDeleteNtRecord(int rowId)
360+
{
361+
RefNtSequenceModel model = RefNtSequenceModel.getForRowId(rowId);
362+
if (model == null || model.getSequenceFile() == null)
363+
{
364+
return;
365+
}
366+
367+
ExpData d = ExperimentService.get().getExpData(model.getSequenceFile());
368+
if (d == null || d.getFile() == null)
369+
{
370+
return;
371+
}
372+
373+
// This assumes canDeleteNtRecord() was called earlier
374+
try
375+
{
376+
if (d.getFile().exists())
377+
{
378+
Files.delete(d.getFile().toPath());
379+
}
380+
}
381+
catch (IOException e)
382+
{
383+
_log.error(e.getMessage(), e);
384+
}
385+
386+
d.delete(_user, false);
387+
}
327388
}

0 commit comments

Comments
 (0)