Skip to content

Commit

Permalink
update per bdg-formats 0.15.0-SNAPSHOT
Browse files Browse the repository at this point in the history
  • Loading branch information
heuermh committed Oct 2, 2019
1 parent fd058a1 commit 40d4af2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
import org.bdgenomics.convert.ConversionException;
import org.bdgenomics.convert.ConversionStringency;

import org.bdgenomics.formats.avro.AlignmentRecord;
import org.bdgenomics.formats.avro.Alignment;

import org.slf4j.Logger;

/**
* Convert AlignmentRecord to htsjdk SAMRecord.
* Convert Alignment to htsjdk SAMRecord.
*/
public final class AlignmentRecordToSamRecord extends AbstractConverter<AlignmentRecord, SAMRecord> {
public final class AlignmentToSamRecord extends AbstractConverter<Alignment, SAMRecord> {

/** Regex to capture attributes. */
private static final Pattern ATTRIBUTE = Pattern.compile("([^:]{2,4}):([AifZHB]):(.*)");
Expand All @@ -53,41 +53,41 @@ public final class AlignmentRecordToSamRecord extends AbstractConverter<Alignmen


/**
* Create a new AlignmentRecord to htsjdk SAMRecord converter with the specified header.
* Create a new Alignment to htsjdk SAMRecord converter with the specified header.
*
* @param header header, must not be null
*/
@Inject
public AlignmentRecordToSamRecord(@Assisted final SAMFileHeader header) {
super(AlignmentRecord.class, SAMRecord.class);
public AlignmentToSamRecord(@Assisted final SAMFileHeader header) {
super(Alignment.class, SAMRecord.class);

checkNotNull(header);
this.header = header;
}


@Override
public SAMRecord convert(final AlignmentRecord alignmentRecord,
public SAMRecord convert(final Alignment alignment,
final ConversionStringency stringency,
final Logger logger) throws ConversionException {

if (alignmentRecord == null) {
warnOrThrow(alignmentRecord, "must not be null", null, stringency, logger);
if (alignment == null) {
warnOrThrow(alignment, "must not be null", null, stringency, logger);
return null;
}

SAMRecord builder = new SAMRecord(header);
builder.setReadName(alignmentRecord.getReadName());
builder.setReadString(alignmentRecord.getSequence());
builder.setReadName(alignment.getReadName());
builder.setReadString(alignment.getSequence());

if (alignmentRecord.getQualityScores() == null) {
if (alignment.getQualityScores() == null) {
builder.setBaseQualityString("*");
}
else {
builder.setBaseQualityString(alignmentRecord.getQualityScores());
builder.setBaseQualityString(alignment.getQualityScores());
}

String readGroupId = alignmentRecord.getReadGroupId();
String readGroupId = alignment.getReadGroupId();
if (readGroupId != null) {
builder.setAttribute("RG", readGroupId);

Expand All @@ -100,96 +100,96 @@ public SAMRecord convert(final AlignmentRecord alignmentRecord,
}
}

if (alignmentRecord.getMateReferenceName() != null) {
builder.setMateReferenceName(alignmentRecord.getMateReferenceName());
if (alignment.getMateReferenceName() != null) {
builder.setMateReferenceName(alignment.getMateReferenceName());
}
if (alignmentRecord.getMateAlignmentStart() != null) {
builder.setMateAlignmentStart(alignmentRecord.getMateAlignmentStart().intValue() + 1);
if (alignment.getMateAlignmentStart() != null) {
builder.setMateAlignmentStart(alignment.getMateAlignmentStart().intValue() + 1);
}
if (alignmentRecord.getInsertSize() != null) {
builder.setInferredInsertSize(alignmentRecord.getInsertSize().intValue());
if (alignment.getInsertSize() != null) {
builder.setInferredInsertSize(alignment.getInsertSize().intValue());
}

if (alignmentRecord.getReadPaired() != null) {
boolean readPaired = alignmentRecord.getReadPaired();
if (alignment.getReadPaired() != null) {
boolean readPaired = alignment.getReadPaired();
builder.setReadPairedFlag(readPaired);

if (readPaired) {
if (alignmentRecord.getMateNegativeStrand() != null) {
builder.setMateNegativeStrandFlag(alignmentRecord.getMateNegativeStrand());
if (alignment.getMateNegativeStrand() != null) {
builder.setMateNegativeStrandFlag(alignment.getMateNegativeStrand());
}
if (alignmentRecord.getMateMapped() != null) {
builder.setMateUnmappedFlag(!alignmentRecord.getMateMapped());
if (alignment.getMateMapped() != null) {
builder.setMateUnmappedFlag(!alignment.getMateMapped());
}
if (alignmentRecord.getProperPair() != null) {
builder.setProperPairFlag(alignmentRecord.getProperPair());
if (alignment.getProperPair() != null) {
builder.setProperPairFlag(alignment.getProperPair());
}
if (alignmentRecord.getReadInFragment() != null) {
builder.setFirstOfPairFlag(alignmentRecord.getReadInFragment() == 0);
builder.setSecondOfPairFlag(alignmentRecord.getReadInFragment() == 1);
if (alignment.getReadInFragment() != null) {
builder.setFirstOfPairFlag(alignment.getReadInFragment() == 0);
builder.setSecondOfPairFlag(alignment.getReadInFragment() == 1);
}
}
}

if (alignmentRecord.getDuplicateRead() != null) {
builder.setDuplicateReadFlag(alignmentRecord.getDuplicateRead());
if (alignment.getDuplicateRead() != null) {
builder.setDuplicateReadFlag(alignment.getDuplicateRead());
}

if (alignmentRecord.getReadMapped() != null) {
boolean readMapped = alignmentRecord.getReadMapped();
if (alignment.getReadMapped() != null) {
boolean readMapped = alignment.getReadMapped();
builder.setReadUnmappedFlag(!readMapped);

if (alignmentRecord.getReadNegativeStrand() != null) {
builder.setReadNegativeStrandFlag(alignmentRecord.getReadNegativeStrand());
if (alignment.getReadNegativeStrand() != null) {
builder.setReadNegativeStrandFlag(alignment.getReadNegativeStrand());
}

if (readMapped) {
if (alignmentRecord.getReferenceName() == null) {
warnOrThrow(alignmentRecord, "referenceName must not be null if read aligned", null, stringency, logger);
if (alignment.getReferenceName() == null) {
warnOrThrow(alignment, "referenceName must not be null if read aligned", null, stringency, logger);
}
else {
builder.setReferenceName(alignmentRecord.getReferenceName());
builder.setReferenceName(alignment.getReferenceName());
}

if (alignmentRecord.getCigar() != null) {
builder.setCigarString(alignmentRecord.getCigar());
if (alignment.getCigar() != null) {
builder.setCigarString(alignment.getCigar());
}
if (alignmentRecord.getPrimaryAlignment() != null) {
builder.setNotPrimaryAlignmentFlag(!alignmentRecord.getPrimaryAlignment());
if (alignment.getPrimaryAlignment() != null) {
builder.setNotPrimaryAlignmentFlag(!alignment.getPrimaryAlignment());
}
if (alignmentRecord.getSupplementaryAlignment() != null) {
builder.setSupplementaryAlignmentFlag(alignmentRecord.getSupplementaryAlignment());
if (alignment.getSupplementaryAlignment() != null) {
builder.setSupplementaryAlignmentFlag(alignment.getSupplementaryAlignment());
}
if (alignmentRecord.getStart() != null) {
builder.setAlignmentStart(alignmentRecord.getStart().intValue() + 1);
if (alignment.getStart() != null) {
builder.setAlignmentStart(alignment.getStart().intValue() + 1);
}
if (alignmentRecord.getMappingQuality() != null) {
builder.setMappingQuality(alignmentRecord.getMappingQuality());
if (alignment.getMappingQuality() != null) {
builder.setMappingQuality(alignment.getMappingQuality());
}
}
else {
builder.setMappingQuality(0);
}
}

if (alignmentRecord.getFailedVendorQualityChecks() != null) {
builder.setReadFailsVendorQualityCheckFlag(alignmentRecord.getFailedVendorQualityChecks());
if (alignment.getFailedVendorQualityChecks() != null) {
builder.setReadFailsVendorQualityCheckFlag(alignment.getFailedVendorQualityChecks());
}
if (alignmentRecord.getMismatchingPositions() != null) {
builder.setAttribute("MD", alignmentRecord.getMismatchingPositions());
if (alignment.getMismatchingPositions() != null) {
builder.setAttribute("MD", alignment.getMismatchingPositions());
}
if (alignmentRecord.getOriginalQualityScores() != null) {
builder.setOriginalBaseQualities(SAMUtils.fastqToPhred(alignmentRecord.getOriginalQualityScores()));
if (alignment.getOriginalQualityScores() != null) {
builder.setOriginalBaseQualities(SAMUtils.fastqToPhred(alignment.getOriginalQualityScores()));
}
if (alignmentRecord.getOriginalCigar() != null) {
builder.setAttribute("OC", alignmentRecord.getOriginalCigar());
if (alignment.getOriginalCigar() != null) {
builder.setAttribute("OC", alignment.getOriginalCigar());
}
if (alignmentRecord.getOriginalStart() != null) {
builder.setAttribute("OP", alignmentRecord.getOriginalStart().intValue() + 1);
if (alignment.getOriginalStart() != null) {
builder.setAttribute("OP", alignment.getOriginalStart().intValue() + 1);
}

if (alignmentRecord.getAttributes() != null) {
String[] tokens = alignmentRecord.getAttributes().split("\t");
if (alignment.getAttributes() != null) {
String[] tokens = alignment.getAttributes().split("\t");
for (String token : tokens) {
Matcher m = ATTRIBUTE.matcher(token);
if (m.matches()) {
Expand Down Expand Up @@ -228,7 +228,7 @@ public SAMRecord convert(final AlignmentRecord alignmentRecord,
case "B:f,":
builder.setAttribute(tagName, splitToFloatArray(value));
default:
warnOrThrow(alignmentRecord, "invalid attribute type " + tagType, null, stringency, logger);
warnOrThrow(alignment, "invalid attribute type " + tagType, null, stringency, logger);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@

import org.bdgenomics.convert.Converter;

import org.bdgenomics.formats.avro.AlignmentRecord;
import org.bdgenomics.formats.avro.Alignment;

/**
* Factory for creating AlignmentRecord to htsjdk SAMRecord converters, which
* Factory for creating Alignment to htsjdk SAMRecord converters, which
* require late binding for a SAMFileHeader.
*
* Thus instead of a converter instance, a converter factory is available via injection:
* <pre>
* final class MyClass {
* private final AlignmentRecordToSamRecordFactory alignmentRecordToSamRecordFactory;
* private final AlignmentToSamRecordFactory alignmentToSamRecordFactory;
*
* @Inject
* MyClass(final AlignmentRecordToSamRecordFactory alignmentRecordToSamRecordFactory) {
* this.alignmentRecordToSamRecordFactory = alignmentRecordToSamRecordFactory;
* MyClass(final AlignmentToSamRecordFactory alignmentToSamRecordFactory) {
* this.alignmentToSamRecordFactory = alignmentToSamRecordFactory;
* }
*
* void doIt() {
* Converter&lt;AlignmentRecord, SAMRecord&gt; converter = alignmentRecordToSamRecordFactory.create(header);
* SAMRecord record = converter.convert(alignmentRecord, stringency, logger);
* Converter&lt;Alignment, SAMRecord&gt; converter = alignmentToSamRecordFactory.create(header);
* SAMRecord record = converter.convert(alignment, stringency, logger);
* // ...
* </pre>
*/
public interface AlignmentRecordToSamRecordFactory {
public interface AlignmentToSamRecordFactory {

/**
* Create a new AlignmentRecord to htsjdk SAMRecord converter with the specified header.
* Create a new Alignment to htsjdk SAMRecord converter with the specified header.
*
* @param header header, must not be null
*/
Converter<AlignmentRecord, SAMRecord> create(SAMFileHeader header);
Converter<Alignment, SAMRecord> create(SAMFileHeader header);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.bdgenomics.convert.Converter;
import org.bdgenomics.convert.ConversionStringency;

import org.bdgenomics.formats.avro.AlignmentRecord;
import org.bdgenomics.formats.avro.Alignment;
import org.bdgenomics.formats.avro.Genotype;
import org.bdgenomics.formats.avro.ProcessingStep;
import org.bdgenomics.formats.avro.ReadGroup;
Expand All @@ -56,8 +56,8 @@ public final class HtsjdkModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder()
.implement(new TypeLiteral<Converter<AlignmentRecord, SAMRecord>>() {}, AlignmentRecordToSamRecord.class)
.build(AlignmentRecordToSamRecordFactory.class));
.implement(new TypeLiteral<Converter<Alignment, SAMRecord>>() {}, AlignmentToSamRecord.class)
.build(AlignmentToSamRecordFactory.class));

install(new FactoryModuleBuilder()
.implement(new TypeLiteral<Converter<List<Genotype>, VariantContext>>() {}, GenotypesToVariantContext.class)
Expand Down Expand Up @@ -87,8 +87,8 @@ Converter<ValidationStringency, ConversionStringency> createValidationStringency
}

@Provides @Singleton
Converter<SAMRecord, AlignmentRecord> createSamRecordToAlignmentRecord() {
return new SamRecordToAlignmentRecord();
Converter<SAMRecord, Alignment> createSamRecordToAlignment() {
return new SamRecordToAlignment();
}

@Provides @Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import org.bdgenomics.convert.ConversionException;
import org.bdgenomics.convert.ConversionStringency;

import org.bdgenomics.formats.avro.AlignmentRecord;
import org.bdgenomics.formats.avro.Alignment;

import org.slf4j.Logger;

/**
* Convert htsjdk SAMRecord to AlignmentRecord.
* Convert htsjdk SAMRecord to Alignment.
*/
public final class SamRecordToAlignmentRecord extends AbstractConverter<SAMRecord, AlignmentRecord> {
public final class SamRecordToAlignment extends AbstractConverter<SAMRecord, Alignment> {

/** Regex to capture the first cigar operation. */
private static final Pattern FIRST_CIGAR_OPERATION = Pattern.compile("^([0-9]*)([A-Z]).*$");
Expand All @@ -52,15 +52,15 @@ public final class SamRecordToAlignmentRecord extends AbstractConverter<SAMRecor


/**
* Create a new htsjdk SAMRecord to AlignmentRecord converter.
* Create a new htsjdk SAMRecord to Alignment converter.
*/
public SamRecordToAlignmentRecord() {
super(SAMRecord.class, AlignmentRecord.class);
public SamRecordToAlignment() {
super(SAMRecord.class, Alignment.class);
}


@Override
public AlignmentRecord convert(final SAMRecord samRecord,
public Alignment convert(final SAMRecord samRecord,
final ConversionStringency stringency,
final Logger logger) throws ConversionException {

Expand All @@ -69,7 +69,7 @@ public AlignmentRecord convert(final SAMRecord samRecord,
return null;
}

AlignmentRecord.Builder builder = AlignmentRecord.newBuilder()
Alignment.Builder builder = Alignment.newBuilder()
.setReadName(samRecord.getReadName())
.setSequence(samRecord.getReadString());

Expand Down
Loading

0 comments on commit 40d4af2

Please sign in to comment.