use of htsjdk.samtools.SAMRecord.SAMTagAndValue in project polyGembler by c-zhou.
the class SAMtools method run.
@Override
public void run() {
// TODO Auto-generated method stub
final SamReaderFactory factory = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.SILENT);
final SamReader inputSam = factory.open(new File(mySamFile));
samHeader = inputSam.getFileHeader();
samHeader.setSortOrder(SortOrder.unsorted);
SAMRecordIterator iter = inputSam.iterator();
Set<Entry<String, String>> attr = samHeader.getAttributes();
List<SAMReadGroupRecord> rgs = samHeader.getReadGroups();
SAMReadGroupRecord rg = new SAMReadGroupRecord("cz1");
rg.setSample("cz1");
samHeader.addReadGroup(rg);
// samHeader.setAttribute("RG", "cz1");
final SAMFileWriter outSam = new SAMFileWriterFactory().makeSAMOrBAMWriter(samHeader, true, new File(myOutput));
for (int i = 0; i < 100; i++) {
SAMRecord record = iter.next();
List<SAMTagAndValue> tags = record.getAttributes();
record.setAttribute("RG", "cz1");
List<SAMTagAndValue> tags2 = record.getAttributes();
outSam.addAlignment(record);
}
myLogger.info("exit...");
try {
inputSam.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outSam.close();
}
use of htsjdk.samtools.SAMRecord.SAMTagAndValue in project gridss by PapenfussLab.
the class SplitReadIdentificationHelper method convertToSupplementaryAlignmentRecord.
private static void convertToSupplementaryAlignmentRecord(SAMRecord primary, SAMRecord supplementary) {
supplementary.setReadName(primary.getReadName());
supplementary.setMateAlignmentStart(primary.getMateAlignmentStart());
supplementary.setMateReferenceIndex(primary.getMateReferenceIndex());
supplementary.setReadPairedFlag(primary.getReadPairedFlag());
if (primary.getReadPairedFlag()) {
supplementary.setProperPairFlag(primary.getProperPairFlag());
supplementary.setFirstOfPairFlag(primary.getFirstOfPairFlag());
supplementary.setSecondOfPairFlag(primary.getSecondOfPairFlag());
supplementary.setMateUnmappedFlag(primary.getMateUnmappedFlag());
supplementary.setMateNegativeStrandFlag(primary.getMateNegativeStrandFlag());
}
supplementary.setNotPrimaryAlignmentFlag(primary.getNotPrimaryAlignmentFlag());
supplementary.setDuplicateReadFlag(primary.getDuplicateReadFlag());
supplementary.setReadFailsVendorQualityCheckFlag(primary.getReadFailsVendorQualityCheckFlag());
supplementary.setSupplementaryAlignmentFlag(true);
// attributes not already set by the supplementary alignment
for (SAMTagAndValue attr : primary.getAttributes()) {
if (supplementary.getAttribute(attr.tag) == null) {
supplementary.setAttribute(attr.tag, attr.value);
}
}
}
use of htsjdk.samtools.SAMRecord.SAMTagAndValue in project gridss by PapenfussLab.
the class CollectTagMetrics method acceptRead.
@Override
protected void acceptRead(final SAMRecord rec, final ReferenceSequence ref) {
for (SAMTagAndValue attr : rec.getAttributes()) {
String tag = attr.tag;
TagSummaryMetrics metric = tags.get(tag);
if (metric == null) {
metric = new TagSummaryMetrics();
metric.TAG = tag;
metric.COUNT = 0;
tags.put(tag, metric);
}
metric.COUNT++;
}
}
use of htsjdk.samtools.SAMRecord.SAMTagAndValue in project jvarkit by lindenb.
the class BWAMemNOp method doWork.
@Override
public int doWork(List<String> args) {
SamReader r = null;
SAMFileWriter w = null;
try {
r = super.openSamReader(oneFileOrNull(args));
SAMFileHeader header = r.getFileHeader();
OtherCanonicalAlignFactory ocaf = new OtherCanonicalAlignFactory(header);
w = writingBamArgs.openSAMFileWriter(outputFile, header, true);
SAMRecordFactory samRecordFactory = new DefaultSAMRecordFactory();
SAMRecordIterator iter = r.iterator();
while (iter.hasNext()) {
SAMRecord rec = iter.next();
if (rec.getSupplementaryAlignmentFlag()) {
continue;
}
if (rec.getReadUnmappedFlag()) {
if (!print_only_spit_read)
w.addAlignment(rec);
continue;
}
Cigar cigar1 = rec.getCigar();
if (cigar1 == null || cigar1.isEmpty() || !(cigar1.getCigarElement(cigar1.numCigarElements() - 1).getOperator().equals(CigarOperator.S) || cigar1.getCigarElement(0).getOperator().equals(CigarOperator.S))) // last or first is soft clipping
{
if (!print_only_spit_read)
w.addAlignment(rec);
continue;
}
rec.getAlignmentStart();
List<OtherCanonicalAlign> xps = ocaf.getXPAligns(rec);
if (xps.isEmpty()) {
if (!print_only_spit_read)
w.addAlignment(rec);
continue;
}
boolean found_one = false;
for (OtherCanonicalAlign xp : xps) {
if (!rec.getReferenceName().equals(xp.getReferenceName()))
continue;
if (xp.getReadNegativeStrandFlag() != rec.getReadNegativeStrandFlag())
continue;
Cigar cigar2 = xp.getCigar();
if (cigar2 == null || cigar2.isEmpty()) {
continue;
}
SAMRecord newrec = null;
List<CigarEvt> L1 = null;
List<CigarEvt> L2 = null;
if (cigar1.getCigarElement(cigar1.numCigarElements() - 1).getOperator().equals(CigarOperator.S) && cigar1.getCigarElement(cigar1.numCigarElements() - 1).getLength() >= this.min_soft_clip_length && cigar2.getCigarElement(0).getOperator().equals(CigarOperator.S) && cigar2.getCigarElement(0).getLength() >= this.min_soft_clip_length && rec.getAlignmentEnd() < xp.getAlignmentStart()) {
newrec = samRecordFactory.createSAMRecord(header);
int ref1 = rec.getAlignmentStart();
newrec.setAlignmentStart(ref1);
L1 = cigarEvents(0, ref1, cigar1);
L2 = cigarEvents(0, xp.getAlignmentStart(), cigar2);
} else if (cigar2.getCigarElement(cigar2.numCigarElements() - 1).getOperator().equals(CigarOperator.S) && cigar2.getCigarElement(cigar2.numCigarElements() - 1).getLength() >= this.min_soft_clip_length && cigar1.getCigarElement(0).getOperator().equals(CigarOperator.S) && cigar1.getCigarElement(0).getLength() >= this.min_soft_clip_length && xp.getAlignmentEnd() < rec.getAlignmentStart()) {
newrec = samRecordFactory.createSAMRecord(header);
int ref1 = xp.getAlignmentStart();
newrec.setAlignmentStart(ref1);
L1 = cigarEvents(0, ref1, cigar2);
L2 = cigarEvents(0, rec.getAlignmentStart(), cigar1);
}
if (newrec == null)
continue;
newrec.setFlags(rec.getFlags());
newrec.setReadName(rec.getReadName());
newrec.setReadBases(rec.getReadBases());
newrec.setMappingQuality(rec.getMappingQuality());
newrec.setReferenceIndex(rec.getReferenceIndex());
newrec.setBaseQualities(rec.getBaseQualities());
if (found_one) {
newrec.setNotPrimaryAlignmentFlag(true);
}
found_one = true;
for (SAMTagAndValue tav : rec.getAttributes()) {
if (tav.tag.equals(ocaf.getAttributeKey()))
continue;
if (tav.tag.equals("NM"))
continue;
newrec.setAttribute(tav.tag, tav.value);
}
if (rec.getReadPairedFlag() && !rec.getMateUnmappedFlag()) {
newrec.setMateAlignmentStart(rec.getMateAlignmentStart());
newrec.setMateReferenceIndex(rec.getMateReferenceIndex());
newrec.setInferredInsertSize(rec.getInferredInsertSize());
}
while (!L1.isEmpty() && (L1.get(L1.size() - 1).op.equals(CigarOperator.S) || L1.get(L1.size() - 1).op.equals(CigarOperator.D) || L1.get(L1.size() - 1).op.equals(CigarOperator.H))) {
L1.remove(L1.size() - 1);
}
while (!L2.isEmpty() && L2.get(0).read0 <= L1.get(L1.size() - 1).read0) {
L2.remove(0);
}
List<CigarElement> cigarElements = new ArrayList<CigarElement>();
int i = 0;
while (i < L1.size()) {
int j = i + 1;
while (j < L1.size() && L1.get(i).op.equals(L1.get(j).op)) {
j++;
}
cigarElements.add(new CigarElement(j - i, L1.get(i).op));
i = j;
}
// add 'N'
cigarElements.add(new CigarElement((L2.get(0).ref1 - L1.get(L1.size() - 1).ref1) - 1, CigarOperator.N));
i = 0;
while (i < L2.size()) {
int j = i + 1;
while (j < L2.size() && L2.get(i).op.equals(L2.get(j).op)) {
j++;
}
cigarElements.add(new CigarElement(j - i, L2.get(i).op));
i = j;
}
// cleanup : case where 'S' is close to 'N'
i = 0;
while (i + 1 < cigarElements.size()) {
CigarElement ce1 = cigarElements.get(i);
CigarElement ce2 = cigarElements.get(i + 1);
if (i > 0 && ce1.getOperator().equals(CigarOperator.S) && ce2.getOperator().equals(CigarOperator.N)) {
cigarElements.set(i, new CigarElement(ce1.getLength(), CigarOperator.X));
} else if (i + 2 < cigarElements.size() && ce1.getOperator().equals(CigarOperator.N) && ce2.getOperator().equals(CigarOperator.S)) {
cigarElements.set(i + 1, new CigarElement(ce2.getLength(), CigarOperator.X));
}
i++;
}
newrec.setCigar(new Cigar(cigarElements));
List<SAMValidationError> validations = newrec.isValid();
if (validations != null) {
for (SAMValidationError err : validations) {
LOG.warning(err.getType() + ":" + err.getMessage());
}
}
w.addAlignment(newrec);
}
if (!found_one) {
if (!print_only_spit_read)
w.addAlignment(rec);
}
}
iter.close();
return 0;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(r);
CloserUtil.close(w);
}
}
use of htsjdk.samtools.SAMRecord.SAMTagAndValue in project jvarkit by lindenb.
the class BamStage method createMetaDataTable.
private TableView<SAMTagAndValue> createMetaDataTable() {
final TableView<SAMTagAndValue> table = new TableView<>();
table.getColumns().add(makeColumn("Key", O -> O.tag));
table.getColumns().add(makeColumn("Value", O -> O.value));
table.setPlaceholder(new Label("No Meta-data."));
return table;
}
Aggregations