Permalink
Please sign in to comment.
Browse files
HTSJDK SamReader implementation using the API and example of its usage
Added ant build and updated HTSJDK
- Loading branch information...
Showing
with
680 additions
and 24 deletions.
- +4 −1 .gitignore
- +145 −0 build.xml
- BIN lib/{htsjdk-1.118.jar → htsjdk-1.121.jar}
- +7 −2 src/main/java/com/google/cloud/genomics/gatk/common/GA4GHUrl.java
- +45 −21 src/main/java/com/google/cloud/genomics/gatk/common/GenomicsApiDataSource.java
- +82 −0 src/main/java/com/google/cloud/genomics/gatk/htsjdk/GA4GHQueryInterval.java
- +25 −0 src/main/java/com/google/cloud/genomics/gatk/htsjdk/GA4GHReaderFactory.java
- +182 −0 src/main/java/com/google/cloud/genomics/gatk/htsjdk/GA4GHSamReader.java
- +148 −0 src/main/java/com/google/cloud/genomics/gatk/htsjdk/GA4GHSamRecordIterator.java
- +42 −0 src/main/java/com/google/cloud/genomics/gatk/htsjdk/SamReaderExample.java
| @@ -1,4 +1,7 @@ | ||
| bin | ||
| .classpath | ||
| .project | ||
| - | ||
| +.jar_tmp | ||
| +classes | ||
| +dist | ||
| +gatk-tools-java.version.properties |
| @@ -0,0 +1,145 @@ | ||
| +<?xml version="1.0"?> | ||
| +<project name="gatk-tools-java" basedir="." default="all"> | ||
| + | ||
| + <property name="src" value="src/main/java"/> | ||
| + <property name="lib" value="lib"/> | ||
| + <property name="dist" value="dist"/> | ||
| + <property name="classes" value="classes"/> | ||
| + <property name="jar_tmp" value=".jar_tmp"/> | ||
| + | ||
| + <property name="javac.target" value="1.6"/> | ||
| + <property name="javac.debug" value="true"/> | ||
| + | ||
| + <!-- Get GIT hash, if available, otherwise leave it blank. --> | ||
| + <exec executable="git" outputproperty="repository.revision" failifexecutionfails="true" errorproperty=""> | ||
| + <arg value="log"/> | ||
| + <arg value="-1"/> | ||
| + <arg value="--pretty=format:%H_%at"/> | ||
| + </exec> | ||
| + <property name="repository.revision" value=""/> | ||
| + <property name="gatk-tools-java-version" value="1.0"/> | ||
| + <property name="gatk-tools-java-version-file" value="gatk-tools-java.version.properties"/> | ||
| + | ||
| + <!-- VERSION PROPERTY --> | ||
| + <target name="write-version-property"> | ||
| + <propertyfile | ||
| + file="${gatk-tools-java-version-file}" | ||
| + comment="gatk-tools-java version"> | ||
| + <entry key="gatk-tools-java-version" value="${gatk-tools-java-version}"/> | ||
| + </propertyfile> | ||
| + </target> | ||
| + | ||
| + <!-- INIT --> | ||
| + <target name="init" depends="write-version-property"> | ||
| + <path id="classpath"> | ||
| + <fileset dir="${lib}"> | ||
| + <include name="**/*.jar"/> | ||
| + </fileset> | ||
| + </path> | ||
| + </target> | ||
| + | ||
| + <!-- CLEAN --> | ||
| + <target name="clean"> | ||
| + <delete dir="${classes}"/> | ||
| + <delete dir="${dist}"/> | ||
| + <delete dir="javadoc"/> | ||
| + <delete dir="${jar_tmp}"/> | ||
| + <delete file="${gatk-tools-java-version-file}"/> | ||
| + </target> | ||
| + | ||
| + <!-- COMPILE --> | ||
| + <target name="compile" depends="init" | ||
| + description="Compile files without cleaning"> | ||
| + <compile-src includes="**/*.*"/> | ||
| + </target> | ||
| + | ||
| + | ||
| + <target name="gatk-tools-java-jar" depends="compile" | ||
| + description="Builds gatk-tools-java-${gatk-tools-java-version}.jar for inclusion in other projects"> | ||
| + <mkdir dir="${dist}"/> | ||
| + <delete dir="${jar_tmp}"/> | ||
| + <mkdir dir="${jar_tmp}"/> | ||
| + <copy todir="${jar_tmp}"> | ||
| + <fileset dir="${classes}" includes="**/*"/> | ||
| + </copy> | ||
| + <unzip dest="${jar_tmp}"> | ||
| + <fileset dir="${lib}"> | ||
| + <include name="*.jar"/> | ||
| + </fileset> | ||
| + </unzip> | ||
| + <jar destfile="${dist}/gatk-tools-java-${gatk-tools-java-version}.jar" compress="no"> | ||
| + <fileset dir="${jar_tmp}" includes="**/*.*"/> | ||
| + <manifest> | ||
| + <attribute name="Implementation-Title" value="@gatk-tools-java"/> | ||
| + <attribute name="Implementation-Version" value="${gatk-tools-java-version}(${repository.revision})"/> | ||
| + </manifest> | ||
| + </jar> | ||
| + <copy todir="${dist}"> | ||
| + <fileset dir="lib" includes="*.jar"/> | ||
| + </copy> | ||
| + </target> | ||
| + | ||
| + | ||
| + | ||
| + <!-- ALL --> | ||
| + <target name="all" depends="compile, gatk-tools-java-jar" description="Default build target"> | ||
| + </target> | ||
| + | ||
| + <!-- ************************************************************************************** --> | ||
| + <!-- ************************************************************************************** --> | ||
| + <!-- Beginning of taskdefs that are used elsewhere in the build file --> | ||
| + <!-- ************************************************************************************** --> | ||
| + <!-- ************************************************************************************** --> | ||
| + | ||
| + <!-- Compile source files specified by includes, from source root. Can specifically | ||
| + include or exclude--> | ||
| + <macrodef name="compile-src"> | ||
| + <attribute name="includes" default=""/> | ||
| + <attribute name="excludes" default=""/> | ||
| + <attribute name="destdir" default="${classes}"/> | ||
| + <attribute name="compile.classpath" default="classpath"/> | ||
| + <attribute name="compiler.args" default=""/> | ||
| + <sequential> | ||
| + <mkdir dir="${classes}"/> | ||
| + <!-- unset the sourcepath attribute in order to compile only files explicitly specified and disable javac's default searching mechanism --> | ||
| + <javac destdir="@{destdir}" | ||
| + optimize="${javac.opt}" | ||
| + debug="${javac.debug}" | ||
| + sourcepath="" | ||
| + srcdir="${src}" | ||
| + includes="@{includes}" | ||
| + excludes="@{excludes}" | ||
| + source="${javac.target}" | ||
| + target="${javac.target}" | ||
| + includeantruntime="false"> | ||
| + <classpath refid="@{compile.classpath}"/> | ||
| + <compilerarg line="@{compiler.args}" /> | ||
| + </javac> | ||
| + </sequential> | ||
| + </macrodef> | ||
| + | ||
| + <macrodef name="compile-tests"> | ||
| + <attribute name="includes" default=""/> | ||
| + <attribute name="excludes" default=""/> | ||
| + <attribute name="compiler.args" default=""/> | ||
| + | ||
| + <sequential> | ||
| + <mkdir dir="${classes.test}"/> | ||
| + <javac destdir="${classes.test}" | ||
| + optimize="${javac.opt}" | ||
| + debug="${javac.debug}" | ||
| + srcdir="${src.test.java}" | ||
| + includes="@{includes}" | ||
| + excludes="@{excludes}" | ||
| + source="${javac.target}" | ||
| + target="${javac.target}" | ||
| + includeantruntime="false"> | ||
| + <classpath> | ||
| + <path refid="classpath"/> | ||
| + <pathelement location="${classes}"/> | ||
| + </classpath> | ||
| + <compilerarg line="@{compiler.args}"/> | ||
| + </javac> | ||
| + </sequential> | ||
| + </macrodef> | ||
| +</project> |
Binary file not shown.
| @@ -0,0 +1,82 @@ | ||
| +package com.google.cloud.genomics.gatk.htsjdk; | ||
| + | ||
| +import htsjdk.samtools.util.CoordMath; | ||
| +import htsjdk.samtools.SAMRecord; | ||
| + | ||
| +/** | ||
| + * Similar to HTSJDK's QueryInterval but allows specifying sequence name | ||
| + * (as opposed to index in the header) and adds ability to check if a given read | ||
| + * matches the interval. | ||
| + */ | ||
| +public class GA4GHQueryInterval { | ||
| + private String sequence; | ||
| + private int start; | ||
| + private int end; | ||
| + | ||
| + public enum ReadPositionConstraint { | ||
| + OVERLAPPING, | ||
| + CONTAINED, | ||
| + START_AT | ||
| + } | ||
| + private ReadPositionConstraint readPositionConstraint; | ||
| + | ||
| + public GA4GHQueryInterval(String sequence, int start, int end, | ||
| + ReadPositionConstraint readPositionConstraint) { | ||
| + super(); | ||
| + this.sequence = sequence; | ||
| + this.start = start; | ||
| + this.end = end; | ||
| + this.readPositionConstraint = readPositionConstraint; | ||
| + } | ||
| + | ||
| + public String getSequence() { | ||
| + return sequence; | ||
| + } | ||
| + | ||
| + public void setSequence(String sequence) { | ||
| + this.sequence = sequence; | ||
| + } | ||
| + | ||
| + public int getStart() { | ||
| + return start; | ||
| + } | ||
| + | ||
| + public void setStart(int start) { | ||
| + this.start = start; | ||
| + } | ||
| + | ||
| + public int getEnd() { | ||
| + return end; | ||
| + } | ||
| + | ||
| + public void setEnd(int end) { | ||
| + this.end = end; | ||
| + } | ||
| + | ||
| + public ReadPositionConstraint getReadPositionConstraint() { | ||
| + return readPositionConstraint; | ||
| + } | ||
| + | ||
| + public void setReadPositionConstraint(ReadPositionConstraint readPositionConstraint) { | ||
| + this.readPositionConstraint = readPositionConstraint; | ||
| + } | ||
| + | ||
| + /** | ||
| + * Returns true iff the read specified by the record matches the interval | ||
| + * given the interval's constraints and the read position. | ||
| + */ | ||
| + public boolean matches(SAMRecord record) { | ||
| + int myEnd = end == 0 ? Integer.MAX_VALUE : end; | ||
| + switch (readPositionConstraint) { | ||
| + case OVERLAPPING: | ||
| + return CoordMath.overlaps(start, myEnd, | ||
| + record.getAlignmentStart(), record.getAlignmentEnd()); | ||
| + case CONTAINED: | ||
| + return CoordMath.encloses(start, myEnd, | ||
| + record.getAlignmentStart(), record.getAlignmentEnd()); | ||
| + case START_AT: | ||
| + return start == record.getAlignmentStart(); | ||
| + } | ||
| + return false; | ||
| + } | ||
| +} |
| @@ -0,0 +1,25 @@ | ||
| +package com.google.cloud.genomics.gatk.htsjdk; | ||
| + | ||
| +import htsjdk.samtools.CustomReaderFactory; | ||
| +import htsjdk.samtools.SamReader; | ||
| + | ||
| +import java.net.URL; | ||
| +import java.util.logging.Logger; | ||
| +/** | ||
| + * HTSJDK CustomReaderFactory implementation. | ||
| + * Returns a SamReader that reads data from GA4GH API. | ||
| + */ | ||
| +public class GA4GHReaderFactory implements CustomReaderFactory.ICustomReaderFactory { | ||
| + private static final Logger LOG = Logger.getLogger(GA4GHReaderFactory.class.getName()); | ||
| + | ||
| + @Override | ||
| + public SamReader open(URL url) { | ||
| + try { | ||
| + return new GA4GHSamReader(url); | ||
| + } catch (Exception ex) { | ||
| + LOG.warning("Error creating SamReader " + ex.toString()); | ||
| + return null; | ||
| + } | ||
| + } | ||
| + | ||
| +} |
Oops, something went wrong.
0 comments on commit
4784f90