Skip to content

Commit

Permalink
Merge pull request #138 from overture-stack/score-137-view-command-bu…
Browse files Browse the repository at this point in the history
…gfix

Score 137 view command bugfix
  • Loading branch information
andricDu authored Feb 8, 2019
2 parents 70154ad + da522a7 commit 176623a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
import java.util.Optional;

import static bio.overture.score.client.cli.Parameters.checkParameter;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toList;
import static org.springframework.util.StringUtils.isEmpty;

@Slf4j
@Component
Expand Down Expand Up @@ -158,8 +160,9 @@ public int execute() throws Exception {

if (sequenceFile != null) {
v = new Viewer(referenceFile);
val indexFileExists = !isNull(indexFile);
val builder = configureBuilder(v.getBuilder(sequenceFile,indexFile));
build(builder);
build(builder, indexFileExists);
} else if (objectId != null) {
// Ad-hoc single - supercedes --manifest
if (manifestResource != null) {
Expand Down Expand Up @@ -211,10 +214,17 @@ private Entity getEntity(String oid) {

public SamFileBuilder configureBuilder(SamFileBuilder builder) {
builder = builder.programName(PROGRAM_NAME)
.version(VersionUtils.getScmInfo().get("git.commit.id.describe")).programId(ICGC).commandLine(getCommandLine())
.containedOnly(containedOnly).useOriginalHeader(useOriginalHeader).outputFormat(outputFormat).queries(query)
.outputDir(outputDir).bedFile(bedFile).outputIndex(outputIndex).stdout(stdout);

.version(VersionUtils.getScmInfo().get("git.commit.id.describe"))
.programId(ICGC)
.commandLine(getCommandLine())
.containedOnly(containedOnly)
.useOriginalHeader(useOriginalHeader)
.outputFormat(outputFormat)
.outputDir(outputDir)
.outputIndex(outputIndex)
.stdout(stdout)
.queries(query)
.bedFile(bedFile);
log.info("Constructed SamFileBuilder: " + builder.toString());
return builder;
}
Expand All @@ -224,18 +234,26 @@ int process(String oid) {
val entity = getEntity(oid);
val urls = getPresignedUrls(entity);

val indexExists = !isNull(urls.index);
val inputStream = Viewer.openInputStream(urls.file);
val indexStream = Viewer.openIndexStream(urls.index);
val indexStream = indexExists ? Viewer.openIndexStream(urls.index) : null;
val isCram = isCRAM(entity.getFileName());
val viewer = new Viewer(referenceFile);

val builder = configureBuilder(viewer.getBuilder(inputStream, indexStream, isCram));
return build(builder.entity(entity));
return build(builder.entity(entity), indexExists);
}

private boolean isQueryDefined(){
return !isEmpty(bedFile) || !isNull(query) && !query.isEmpty();
}

@SneakyThrows
int build(SamFileBuilder builder) {
if (headerOnly) {
int build(SamFileBuilder builder, boolean hasIndex) {
if (isQueryDefined() && !hasIndex){
log.warn("Supplied query or bedfile will not be used since no index is available");
}
if (headerOnly || !hasIndex) {
builder.buildHeaderOnly();
} else {
switch (outputType) {
Expand Down Expand Up @@ -272,11 +290,9 @@ private Optional<Entity> fetchEntity(String oid) {

public PresignedUrls getPresignedUrls(Entity entity) {
val indexEntity = metadataService.getIndexEntity(entity);
checkParameter(indexEntity.isPresent(), "No index file associated with BAM/CRAM file with object id '%s'",
entity.getId());

val bamFileUrl = downloadService.getUrl(entity.getId());
val indexFileUrl = downloadService.getUrl(indexEntity.get().getId());
val indexFileUrl = indexEntity.map(x -> downloadService.getUrl(x.getId())).orElse(null);

return new PresignedUrls(bamFileUrl, indexFileUrl);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) 2016 The Ontario Institute for Cancer Research. All rights reserved.
*
* Copyright (c) 2016 The Ontario Institute for Cancer Research. All rights reserved.
*
* This program and the accompanying materials are made available under the terms of the GNU Public License v3.0.
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package bio.overture.score.client.slicing;
Expand All @@ -22,8 +22,18 @@
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import htsjdk.samtools.*;
import htsjdk.samtools.QueryInterval;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMFileHeader.SortOrder;
import htsjdk.samtools.SAMFileWriter;
import htsjdk.samtools.SAMFileWriterFactory;
import htsjdk.samtools.SAMProgramRecord;
import htsjdk.samtools.SAMReadGroupRecord;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SamInputResource;
import htsjdk.samtools.SamReader;
import htsjdk.samtools.SamReaderFactory;
import htsjdk.samtools.ValidationStringency;
import htsjdk.samtools.cram.ref.ReferenceSource;
import htsjdk.samtools.util.RuntimeIOException;
import lombok.Cleanup;
Expand Down Expand Up @@ -305,7 +315,7 @@ private void createTrimmed(SamReader reader, Entity entity, List<String> queries

/**
* Temporarily changed to public
*
*
* @param reader
* @param queries
* @return
Expand Down Expand Up @@ -339,7 +349,7 @@ public QueryInterval[] normalizeQueries(SamReader reader, List<String> queries)

/**
* Temporarily changed to public
*
*
* Returns list of SAMRecord alignments that satisfy the specified list of queries. Refers to the <b>containedOnly</b>
* member variable.
* @param reader Instance of the SAM Reader open to source SAM/BAM file
Expand Down Expand Up @@ -523,7 +533,7 @@ String generateHeaderOnlyOutputFileName(@NonNull Entity entity) {
/**
* In case of a large number of queries, we truncate the output filename if it is longer than MAX_FILENAME_LENGTH
* characters long.
*
*
* @param originalName - file name including all query slices
* @return file name truncated at MAX_FILENAME_LENGTH but retaining extension. Includes '~' to indicate that it has
* been truncated
Expand All @@ -550,10 +560,7 @@ private String encodeQuery(String query) {
return query.replace(":", "_");
}

@SneakyThrows
public void buildHeaderOnly() {
@Cleanup
val reader = createSamReader();
createHeaderOnly(reader, entity); // use original filename and no query
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void create(@NonNull File uploadFile, @NonNull ObjectSpecification
}

protected static String getStateName() {
return "uploadid";
return "uploadId";
}

public static Optional<String> fetchUploadId(@NonNull File uploadFile, @NonNull String objectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,39 @@
import htsjdk.samtools.ValidationStringency;
import htsjdk.samtools.cram.ref.ReferenceSource;
import htsjdk.samtools.seekablestream.SeekableStream;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;

import java.io.File;
import java.io.InputStream;
import java.net.URL;

import static java.util.Objects.isNull;

public class Viewer {
File referenceFile=null;

public Viewer(File referenceFile) {
this.referenceFile=referenceFile;
}

public static SamInputResource getFileResource(File bamFile, File baiFile) {
if (baiFile != null) {
public static SamInputResource getFileResource(@NonNull File bamFile, File baiFile) {
if (isNull(baiFile)) {
return SamInputResource.of(bamFile);
} else {
return SamInputResource.of(bamFile).index(baiFile);
}
}

public static SamInputResource getStreamResource(@NonNull SeekableStream inputStream, SeekableStream indexStream) {
if (isNull(indexStream)) {
return SamInputResource.of(inputStream);
} else {
return SamInputResource.of(bamFile);
return SamInputResource.of(inputStream).index(indexStream);
}
}

public static InputStream openInputStream(URL url) {
public static SeekableStream openInputStream(URL url) {
return new NullSourceSeekableHTTPStream(url);
}

Expand All @@ -40,18 +50,23 @@ public static SeekableStream openIndexStream(URL url) {
}

@SneakyThrows
public SamFileBuilder getBuilder(File sequenceFile, File indexFile) {
public SamFileBuilder getBuilder(@NonNull File sequenceFile, File indexFile) {
val entity = new Entity();
entity.setFileName(sequenceFile.toString());
val reference=new ReferenceSource(referenceFile);
val resource = getFileResource(sequenceFile, indexFile);
val builder=new SamFileBuilder().samInput(resource).cramReferenceSource(reference);
val builder = new SamFileBuilder()
.entity(entity)
.samInput(resource)
.cramReferenceSource(reference);
val reader = builder.createSamReader();
builder.reader(reader);
return builder;
}

@SneakyThrows
public SamFileBuilder getBuilder(InputStream inputStream, SeekableStream indexStream, boolean isCram) {
val resource = SamInputResource.of(inputStream).index(indexStream);
public SamFileBuilder getBuilder(@NonNull SeekableStream inputStream, SeekableStream indexStream, boolean isCram) {
val resource = getStreamResource(inputStream, indexStream);
val builder = new SamFileBuilder().samInput(resource);

if (isCram) {
Expand Down

0 comments on commit 176623a

Please sign in to comment.