Skip to content

Commit

Permalink
style fixes, add SAMRecord converter to module
Browse files Browse the repository at this point in the history
  • Loading branch information
heuermh committed May 13, 2019
1 parent c769cb7 commit a19f440
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* Convert AlignmentRecord to htsjdk SAMRecord.
*/
public final class AlignmentRecordToSAMRecord extends AbstractConverter<AlignmentRecord, SAMRecord> {
public final class AlignmentRecordToSamRecord extends AbstractConverter<AlignmentRecord, SAMRecord> {

/** Regex to capture attributes. */
private static final Pattern ATTRIBUTE = Pattern.compile("([^:]{2,4}):([AifZHB]):(.*)");
Expand All @@ -53,7 +53,7 @@ public final class AlignmentRecordToSAMRecord extends AbstractConverter<Alignmen
*
* @param header header, must not be null
*/
public AlignmentRecordToSAMRecord(final SAMFileHeader header) {
public AlignmentRecordToSamRecord(final SAMFileHeader header) {
super(AlignmentRecord.class, SAMRecord.class);

checkNotNull(header);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.bdgenomics.convert.htsjdk;

import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.ValidationStringency;

import com.google.inject.AbstractModule;
Expand All @@ -26,6 +27,8 @@
import org.bdgenomics.convert.Converter;
import org.bdgenomics.convert.ConversionStringency;

import org.bdgenomics.formats.avro.AlignmentRecord;

/**
* Guice module for the org.bdgenomics.convert.htsjdk package.
*/
Expand All @@ -44,4 +47,9 @@ Converter<ConversionStringency, ValidationStringency> createConversionStringency
Converter<ValidationStringency, ConversionStringency> createValidationStringencyToConversionStringency() {
return new ValidationStringencyToConversionStringency();
}

@Provides @Singleton
Converter<SAMRecord, AlignmentRecord> createSamRecordToAlignmentRecord() {
return new SamRecordToAlignmentRecord();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* Convert htsjdk SAMRecord to AlignmentRecord.
*/
public final class SAMRecordToAlignmentRecord extends AbstractConverter<SAMRecord, AlignmentRecord> {
public final class SamRecordToAlignmentRecord extends AbstractConverter<SAMRecord, AlignmentRecord> {

/** Regex to capture the first cigar operation. */
private static final Pattern FIRST_CIGAR_OPERATION = Pattern.compile("^([0-9]*)([A-Z]).*$");
Expand All @@ -54,7 +54,7 @@ public final class SAMRecordToAlignmentRecord extends AbstractConverter<SAMRecor
/**
* Create a new htsjdk SAMRecord to AlignmentRecord converter.
*/
public SAMRecordToAlignmentRecord() {
public SamRecordToAlignmentRecord() {
super(SAMRecord.class, AlignmentRecord.class);
}

Expand Down Expand Up @@ -175,7 +175,6 @@ public AlignmentRecord convert(final SAMRecord samRecord,
}

String attributes = encodeAttributes(getBinaryAttributes(samRecord));
//String attributes = encodeAttributes(samRecord.getBinaryAttributes());
if (!attributes.isEmpty()) {
builder.setAttributes(attributes);
}
Expand Down Expand Up @@ -244,7 +243,6 @@ static SAMBinaryTagAndValue getBinaryAttributes(final SAMRecord samRecord) {
catch (Exception e) {
return null;
}
//return (SAMBinaryTagAndValue) org.apache.commons.lang3.reflect.FieldUtils.readDeclaredField(samRecord, "mAttributes", true);
}

/**
Expand All @@ -263,17 +261,21 @@ static String encodeUnsignedArray(final String tag, final Object value) {
catch (Exception e) {
return null;
}
// return (String) MethodUtils.invokeMethod(TAG_CODEC, true, "encodeUnsignedArray", new Object[] { tag, value }, new Class<?>[] { String.class, Object.class });
}

/**
* Encode the specified attribute and its child attributes to a string.
*
* @param attribute attribute
* @return the specified attribute and its child attributes to a string
*/
static String encodeAttributes(SAMBinaryTagAndValue attribute) {
StringBuilder sb = new StringBuilder();
while (attribute != null) {
if (!skipTag(attribute.tag)) {
final String encodedTag;
if (attribute.isUnsignedArray()) {
encodedTag = encodeUnsignedArray(SAMTag.makeStringTag(attribute.tag), attribute.value);
// encodedTag = TAG_CODEC.encodeUnsignedArray(SAMTag.makeStringTag(attribute.tag), attribute.value);
}
else {
encodedTag = TAG_CODEC.encode(SAMTag.makeStringTag(attribute.tag), attribute.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
import org.slf4j.LoggerFactory;

/**
* Unit test for AlignmentRecordToSAMRecord.
* Unit test for AlignmentRecordToSamRecord.
*/
public final class AlignmentRecordToSAMRecordTest {
private final Logger logger = LoggerFactory.getLogger(AlignmentRecordToSAMRecordTest.class);
public final class AlignmentRecordToSamRecordTest {
private final Logger logger = LoggerFactory.getLogger(AlignmentRecordToSamRecordTest.class);

SAMFileHeader header;
Converter<AlignmentRecord, SAMRecord> converter;
Expand All @@ -53,7 +53,7 @@ public void setUp() {
SAMSequenceRecord sequenceRecord = new SAMSequenceRecord("1", 3000000);
header.getSequenceDictionary().addSequence(sequenceRecord);

converter = new AlignmentRecordToSAMRecord(header);
converter = new AlignmentRecordToSamRecord(header);
}

@Test
Expand All @@ -63,7 +63,7 @@ public void testConstructor() {

@Test(expected=NullPointerException.class)
public void testConstructorNullHeader() {
new AlignmentRecordToSAMRecord(null);
new AlignmentRecordToSamRecord(null);
}

@Test(expected=ConversionException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.inject.Injector;
import com.google.inject.Guice;

import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.ValidationStringency;

import org.junit.Before;
Expand All @@ -32,6 +33,8 @@
import org.bdgenomics.convert.Converter;
import org.bdgenomics.convert.ConversionStringency;

import org.bdgenomics.formats.avro.AlignmentRecord;

/**
* Unit test for HtsjdkModule.
*/
Expand All @@ -54,6 +57,7 @@ public void testHtsjdkModule() {
Target target = injector.getInstance(Target.class);
assertNotNull(target.getConversionStringencyToValidationStringency());
assertNotNull(target.getValidationStringencyToConversionStringency());
assertNotNull(target.getSamRecordToAlignmentRecord());
}

/**
Expand All @@ -62,13 +66,16 @@ public void testHtsjdkModule() {
static class Target {
final Converter<ConversionStringency, ValidationStringency> conversionStringencyToValidationStringency;
final Converter<ValidationStringency, ConversionStringency> validationStringencyToConversionStringency;
final Converter<SAMRecord, AlignmentRecord> samRecordToAlignmentRecord;

@Inject
Target(final Converter<ConversionStringency, ValidationStringency> conversionStringencyToValidationStringency,
final Converter<ValidationStringency, ConversionStringency> validationStringencyToConversionStringency) {
final Converter<ValidationStringency, ConversionStringency> validationStringencyToConversionStringency,
final Converter<SAMRecord, AlignmentRecord> samRecordToAlignmentRecord) {

this.conversionStringencyToValidationStringency = conversionStringencyToValidationStringency;
this.validationStringencyToConversionStringency = validationStringencyToConversionStringency;

this.samRecordToAlignmentRecord = samRecordToAlignmentRecord;
}

Converter<ConversionStringency, ValidationStringency> getConversionStringencyToValidationStringency() {
Expand All @@ -78,6 +85,10 @@ Converter<ConversionStringency, ValidationStringency> getConversionStringencyToV
Converter<ValidationStringency, ConversionStringency> getValidationStringencyToConversionStringency() {
return validationStringencyToConversionStringency;
}

Converter<SAMRecord, AlignmentRecord> getSamRecordToAlignmentRecord() {
return samRecordToAlignmentRecord;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import org.slf4j.LoggerFactory;

/**
* Unit test for SAMRecordToAlignmentRecord.
* Unit test for SamRecordToAlignmentRecord.
*/
public final class SAMRecordToAlignmentRecordTest {
private final Logger logger = LoggerFactory.getLogger(SAMRecordToAlignmentRecordTest.class);
public final class SamRecordToAlignmentRecordTest {
private final Logger logger = LoggerFactory.getLogger(SamRecordToAlignmentRecordTest.class);

Converter<SAMRecord, AlignmentRecord> converter;
SAMFileHeader header;
Expand All @@ -54,7 +54,7 @@ public void setUp() {
SAMSequenceRecord sequenceRecord = new SAMSequenceRecord("1", 3000000);
header.getSequenceDictionary().addSequence(sequenceRecord);

converter = new SAMRecordToAlignmentRecord();
converter = new SamRecordToAlignmentRecord();
}

@Test
Expand Down

0 comments on commit a19f440

Please sign in to comment.