Java Code Examples for htsjdk.samtools.CigarOperator#H

The following examples show how to use htsjdk.samtools.CigarOperator#H . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SAMRecordUtils.java    From abra2 with MIT License 5 votes vote down vote up
/**
 * Replace hard clips with soft clips.
 */
public static void replaceHardClips(SAMRecord read) {
	Cigar cigar = read.getCigar();
	
	if (cigar.getCigarElements().size() > 0) {
		CigarElement firstElement = cigar.getCigarElement(0);
		CigarElement lastElement  = cigar.getCigarElement(cigar.numCigarElements()-1);
		
		if ((firstElement.getOperator() == CigarOperator.H) ||
			(lastElement.getOperator() == CigarOperator.H)) {
			
			Cigar newCigar = new Cigar();
			
			boolean isFirst = true;
			
			for (CigarElement element : cigar.getCigarElements()) {
				if (element.getOperator() != CigarOperator.H) {
					newCigar.add(element);
				} else {
					CigarElement softClip = new CigarElement(element.getLength(), CigarOperator.S);
					newCigar.add(softClip);
					
					if (isFirst) {
						read.setReadString(padBases(element.getLength()) + read.getReadString());
					} else {
						read.setReadString(read.getReadString() + padBases(element.getLength()));							
					}
				}
				
				isFirst = false;
			}
			
			read.setCigar(newCigar);
		}
	}
}
 
Example 2
Source File: CigarModifierTest.java    From VarDictJava with MIT License 5 votes vote down vote up
@Test(dataProvider = "cigar")
public void getCigarOperatorTest(Object cigarObject) {
    Cigar cigar = (Cigar) cigarObject;
    CigarOperator[] operators = new CigarOperator[] {
            CigarOperator.M,
            CigarOperator.S,
            CigarOperator.I,
            CigarOperator.D,
            CigarOperator.N,
            CigarOperator.H,
    };
    for (int i = 0; i < cigar.numCigarElements(); i++) {
        Assert.assertEquals(CigarParser.getCigarOperator(cigar, i), operators[i]);
    }
}
 
Example 3
Source File: MultiHitAlignedReadIterator.java    From picard with MIT License 5 votes vote down vote up
/** Replaces hard clips with soft clips and fills in bases and qualities with dummy values as needed. */
 private void replaceHardWithSoftClips(final SAMRecord rec) {
     if (rec.getReadUnmappedFlag()) return;
     if (rec.getCigar().isEmpty()) return;

     List<CigarElement> elements = rec.getCigar().getCigarElements();
     final CigarElement first = elements.get(0);
     final CigarElement last  = elements.size() == 1 ? null : elements.get(elements.size()-1);
     final int startHardClip = first.getOperator() == CigarOperator.H ? first.getLength() : 0;
     final int endHardClip   = (last != null && last.getOperator() == CigarOperator.H) ? last.getLength() : 0;

     if (startHardClip + endHardClip > 0) {
         final int len = rec.getReadBases().length + startHardClip + endHardClip;

         // Fix the basecalls
         final byte[] bases = new byte[len];
         Arrays.fill(bases, (byte) 'N');
         System.arraycopy(rec.getReadBases(), 0, bases, startHardClip, rec.getReadBases().length);

         // Fix the quality scores
         final byte[] quals = new byte[len];
         Arrays.fill(quals, (byte) 2  );
         System.arraycopy(rec.getBaseQualities(), 0, quals, startHardClip, rec.getBaseQualities().length);

         // Fix the cigar!
         elements = new ArrayList<CigarElement>(elements); // make it modifiable
         if (startHardClip > 0) elements.set(0, new CigarElement(first.getLength(), CigarOperator.S));
         if (endHardClip   > 0) elements.set(elements.size()-1, new CigarElement(last.getLength(), CigarOperator.S));

         // Set the update structures on the new record
         rec.setReadBases(bases);
         rec.setBaseQualities(quals);
         rec.setCigar(new Cigar(elements));
     }
}
 
Example 4
Source File: AlignmentUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Count the number of bases hard clipped from read
 *
 * If read's cigar is null, return 0
 *
 * @param r a non-null read
 * @return a positive integer
 */
public static int getNumHardClippedBases(final GATKRead r) {
    if ( r == null ) throw new IllegalArgumentException("Read cannot be null");

    int n = 0;
    final Cigar cigar = r.getCigar();
    if (cigar == null) return 0;

    for (final CigarElement e : cigar.getCigarElements())
        if (e.getOperator() == CigarOperator.H)
            n += e.getLength();

    return n;
}
 
Example 5
Source File: SAMRecordUtils.java    From abra2 with MIT License 4 votes vote down vote up
private static boolean isClip(CigarElement elem) {
	return elem.getOperator() == CigarOperator.S || elem.getOperator() == CigarOperator.H;
}
 
Example 6
Source File: CigarUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isClipOrPaddingOperator(final CigarOperator op) {
    return op == CigarOperator.S || op == CigarOperator.H || op == CigarOperator.P;
}
 
Example 7
Source File: LocusIteratorByStateBaseTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isPadding(final CigarElement elt) {
    return elt.getOperator() == CigarOperator.P || elt.getOperator() == CigarOperator.H || elt.getOperator() == CigarOperator.S;
}