Skip to content

Commit 1fac863

Browse files
authored
Option to create readsets from SRA (#355)
* Option to create readsets from SRA * Test fix
1 parent 35b3ae0 commit 1fac863

File tree

3 files changed

+128
-5
lines changed

3 files changed

+128
-5
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
/*
2-
* Copyright (c) 2012 LabKey Corporation
3-
*
4-
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
5-
*/
1+
var LABKEY = require("labkey");
2+
3+
var triggerHelper = new org.labkey.sequenceanalysis.query.SequenceTriggerHelper(LABKEY.Security.currentUser.id, LABKEY.Security.currentContainer.id);
64

75
function beforeDelete(row, errors){
86
if (!this.extraContext.deleteFromServer){
97
errors._form = 'You cannot directly delete readsets. To delete these records, use the delete button above the readset grid.';
108
}
9+
}
10+
11+
function afterInsert(row, errors) {
12+
if (row.sraAccessions) {
13+
triggerHelper.createReaddataForSra(row.rowid, row.sraAccessions);
14+
}
1115
}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import org.biojava.nbio.core.sequence.transcription.TranscriptionEngine;
1414
import org.junit.Assert;
1515
import org.junit.Test;
16+
import org.labkey.api.assay.AssayFileWriter;
1617
import org.labkey.api.data.Container;
1718
import org.labkey.api.data.ContainerManager;
1819
import org.labkey.api.data.SimpleFilter;
20+
import org.labkey.api.data.Table;
1921
import org.labkey.api.data.TableInfo;
2022
import org.labkey.api.data.TableSelector;
2123
import org.labkey.api.exp.api.DataType;
@@ -28,12 +30,19 @@
2830
import org.labkey.api.security.User;
2931
import org.labkey.api.security.UserManager;
3032
import org.labkey.api.sequenceanalysis.RefNtSequenceModel;
33+
import org.labkey.api.util.FileUtil;
34+
import org.labkey.api.util.Path;
35+
import org.labkey.sequenceanalysis.ReadDataImpl;
3136
import org.labkey.sequenceanalysis.SequenceAnalysisSchema;
37+
import org.labkey.sequenceanalysis.SequenceAnalysisServiceImpl;
38+
import org.labkey.sequenceanalysis.SequenceReadsetImpl;
39+
import org.labkey.vfs.FileLike;
3240

3341
import java.io.File;
3442
import java.io.IOException;
3543
import java.io.StringWriter;
3644
import java.util.Arrays;
45+
import java.util.Date;
3746
import java.util.HashMap;
3847
import java.util.List;
3948
import java.util.Map;
@@ -254,4 +263,64 @@ public int createExpData(String relPath) {
254263

255264
return d.getRowId();
256265
}
266+
267+
public void createReaddataForSra(int readsetId, String sraAccessions)
268+
{
269+
SequenceReadsetImpl rs = SequenceAnalysisServiceImpl.get().getReadset(readsetId, _user);
270+
if (rs == null)
271+
{
272+
throw new IllegalArgumentException("Unable to find readset: " + readsetId);
273+
}
274+
275+
TableInfo rd = SequenceAnalysisSchema.getTable(SequenceAnalysisSchema.TABLE_READ_DATA);
276+
277+
String[] tokens = StringUtils.split(sraAccessions, ",");
278+
for (String token : tokens)
279+
{
280+
if (rs.getReadData() != null && !rs.getReadData().isEmpty())
281+
{
282+
throw new IllegalArgumentException("Did not expect readset to have existing readdata: " + rs.getReadsetId());
283+
}
284+
285+
// Create new:
286+
ReadDataImpl rd1 = new ReadDataImpl();
287+
rd1.setReadset(readsetId);
288+
rd1.setContainer(rs.getContainer());
289+
rd1.setCreated(new Date());
290+
rd1.setModified(new Date());
291+
rd1.setCreatedBy(_user.getUserId());
292+
rd1.setModifiedBy(_user.getUserId());
293+
rd1.setSra_accession(token);
294+
rd1.setArchived(true);
295+
296+
// NOTE: this is a fragile assumption. We might need to eventually query SRA to figure out whether data is paired:
297+
Container c = ContainerManager.getForId(rs.getContainer());
298+
PipeRoot pr = PipelineService.get().findPipelineRoot(c);
299+
if (pr == null)
300+
{
301+
throw new IllegalStateException("Unable to find pipeline root for: " + c.getPath());
302+
}
303+
304+
String folderName = "SequenceImport_" + FileUtil.getTimestamp();
305+
FileLike outDir = AssayFileWriter.findUniqueFileName(folderName, pr.getRootFileLike());
306+
307+
FileLike expectedFile1 = FileUtil.appendPath(outDir, Path.parse(token + "_1.fastq.gz"));
308+
ExpData exp1 = ExperimentService.get().createData(c, new DataType("Data"));
309+
exp1.setDataFileURI(expectedFile1.toURI());
310+
exp1.setContainer(c);
311+
exp1.setName(expectedFile1.getName());
312+
exp1.save(_user);
313+
rd1.setFileId1(exp1.getRowId());
314+
315+
FileLike expectedFile2 = FileUtil.appendPath(outDir, Path.parse(token + "_2.fastq.gz"));
316+
ExpData exp2 = ExperimentService.get().createData(c, new DataType("Data"));
317+
exp2.setDataFileURI(expectedFile2.toURI());
318+
exp2.setContainer(c);
319+
exp2.setName(expectedFile2.getName());
320+
exp2.save(_user);
321+
rd1.setFileId2(exp2.getRowId());
322+
323+
Table.insert(_user, rd, rd1);
324+
}
325+
}
257326
}

SequenceAnalysis/test/src/org/labkey/test/tests/external/labModules/SequenceTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,44 @@ private void importReadsetMetadata()
195195
log("verifying readset count correct");
196196
waitForText("Sequence Readsets");
197197
waitForElement(LabModuleHelper.getNavPanelItem("Sequence Readsets:", _readsetCt.toString()));
198+
199+
// Repeat, adding SRA accessions:
200+
goToProjectHome();
201+
waitAndClick(Locator.linkWithText("Plan Sequence Run (Create Readsets)"));
202+
new Window.WindowFinder(getDriver()).withTitle("Create Readsets").waitFor();
203+
waitAndClickAndWait(Ext4Helper.Locators.ext4Button("Submit"));
204+
205+
_helper.waitForField("Sample Id", WAIT_FOR_PAGE);
206+
_ext4Helper.clickTabContainingText("Import Spreadsheet");
207+
waitForText("Copy/Paste Data");
208+
209+
setFormElementJS(Locator.name("text"), getIlluminaSRANames());
210+
211+
waitAndClick(Ext4Helper.Locators.ext4Button("Upload"));
212+
new Window.WindowFinder(getDriver()).withTitle("Success").waitFor();
213+
_readsetCt += 3;
214+
assertTextPresent("Success!");
215+
waitAndClickAndWait(Ext4Helper.Locators.ext4Button("OK"));
216+
217+
// This is scoped to this workbook:
218+
log("verifying readset count correct");
219+
waitForText("Sequence Readsets");
220+
waitAndClickAndWait(LabModuleHelper.getNavPanelItem("Sequence Readsets:", "3"));
221+
222+
DataRegionTable.DataRegion(getDriver()).withName("query").waitFor();
223+
224+
//verify CSV file creation
225+
DataRegionTable.DataRegion(getDriver()).find().goToView("SRA Info");
226+
DataRegionTable dr = DataRegionTable.DataRegion(getDriver()).withName("query").waitFor();
227+
waitForElement(Locator.tagContainingText("a", "SRA0"));
228+
waitForElement(Locator.tagContainingText("a", "SRA1"));
229+
waitForElement(Locator.tagContainingText("a", "SRA2"));
230+
231+
dr.checkAllOnPage();
232+
dr.clickHeaderButtonAndWait("Delete");
233+
clickButton("OK");
234+
235+
_readsetCt -= 3;
198236
}
199237

200238
/**
@@ -320,6 +358,18 @@ private String getIlluminaNames()
320358
return sb.toString();
321359
}
322360

361+
private String getIlluminaSRANames()
362+
{
363+
StringBuilder sb = new StringBuilder("Name\tPlatform\tsraAccessions\n");
364+
int i = 0;
365+
while (i < 3)
366+
{
367+
sb.append("IlluminaSRA" + (i + 1) + "\tILLUMINA\tSRA" + i + "\n");
368+
i++;
369+
}
370+
return sb.toString();
371+
}
372+
323373
/**
324374
* This test will kick off a pipeline import using the illumina pipeline. Verification of the result
325375
* is performed by readsetFeaturesTest()

0 commit comments

Comments
 (0)