use of com.github.lindenb.jvarkit.util.bio.AminoAcids.AminoAcid in project jvarkit by lindenb.
the class TView method paint.
void paint(final PrintStream out) {
final Colorizer colorizer;
switch(this.formatOut) {
case html:
colorizer = new HtmlColorizer(out);
break;
case tty:
colorizer = new AnsiColorizer(out);
break;
case plain:
colorizer = new Colorizer(out);
break;
default:
throw new IllegalStateException();
}
if (interval == null) {
LOG.warn("No interval defined");
return;
}
final GenomicSequence contigSequence;
final Function<Integer, Character> refPosToBase;
if (indexedFastaSequenceFile != null) {
final SAMSequenceDictionary dict = SAMSequenceDictionaryExtractor.extractDictionary(referenceFile);
if (dict.getSequence(this.interval.getContig()) == null) {
LOG.warn("No interval with contig " + interval + " in REF");
return;
}
contigSequence = new GenomicSequence(indexedFastaSequenceFile, interval.getContig());
refPosToBase = POS -> {
if (POS < 0 || POS >= contigSequence.length())
return 'N';
return contigSequence.charAt(POS);
};
} else {
contigSequence = null;
refPosToBase = POS -> 'N';
}
/**
* test if genomic position is in interval
*/
final Predicate<Integer> testInInterval = new Predicate<Integer>() {
@Override
public boolean test(final Integer pos) {
return interval.getStart() <= pos && pos <= interval.getEnd();
}
};
final int pixelWidth = this.interval.length();
final Map<Integer, Integer> genomicpos2insertlen = new TreeMap<>();
final Map<String, List<SAMRecord>> group2record = new TreeMap<>();
for (final SamReader samReader : this.samReaders) {
SAMRecordIterator iter = samReader.query(this.interval.getContig(), this.interval.getStart(), this.interval.getEnd(), false);
while (iter.hasNext()) {
final SAMRecord rec = iter.next();
if (rec.getReadUnmappedFlag())
continue;
if (rec.getCigar() == null)
continue;
if (getRecordFilter().filterOut(rec))
continue;
if (!rec.getContig().equals(interval.getContig()))
continue;
if (right().apply(rec) < this.interval.getStart())
continue;
if (this.interval.getEnd() < left().apply(rec))
continue;
String group = this.groupBy.getPartion(rec);
if (group == null || group.isEmpty()) {
group = "undefined_" + this.groupBy.name();
}
List<SAMRecord> records = group2record.get(group);
if (records == null) {
records = new ArrayList<>();
group2record.put(group, records);
}
records.add(rec);
// loop over cigar, get the longest insert
int refpos = rec.getAlignmentStart();
for (final CigarElement ce : rec.getCigar().getCigarElements()) {
if (!this.showInsertions)
break;
final CigarOperator op = ce.getOperator();
if (op.equals(CigarOperator.I) && testInInterval.test(refpos)) {
final Integer longestInsert = genomicpos2insertlen.get(refpos);
if (longestInsert == null || longestInsert.compareTo(ce.getLength()) < 0) {
genomicpos2insertlen.put(refpos, ce.getLength());
}
}
if (op.consumesReferenceBases()) {
refpos += ce.getLength();
}
if (refpos > interval.getEnd())
break;
}
}
CloserUtil.close(iter);
CloserUtil.close(samReader);
}
/**
* compute where are the insertions
*/
// LOG.debug(genomicpos2insertlen);
final Predicate<Integer> insertIsPresentAtX = SCREENX -> {
int x = 0;
int ref = interval.getStart();
while (x < pixelWidth) {
if (x > SCREENX)
return false;
final Integer insertLen = genomicpos2insertlen.get(ref);
if (insertLen == null) {
++x;
++ref;
} else {
if (x <= SCREENX && SCREENX < x + insertLen)
return true;
// (+1) I DON'T UNDERSTAND WHY, BUT IT WORKS
x += (insertLen + 1);
++ref;
}
}
return false;
};
final Function<Character, AnsiUtils.AnsiColor> base2ansiColor = BASE -> {
switch(Character.toUpperCase(BASE)) {
case 'A':
return AnsiUtils.AnsiColor.BLUE;
case 'T':
return AnsiUtils.AnsiColor.GREEN;
case 'G':
return AnsiUtils.AnsiColor.CYAN;
case 'C':
return AnsiUtils.AnsiColor.YELLOW;
default:
return null;
}
};
/**
* print interval title
*/
out.println(interval.getContig() + ":" + interval.getStart() + "-" + interval.getEnd());
/**
* paint base position
*/
int ref = this.interval.getStart();
int x = 0;
out.print(margin("POS:"));
while (x < pixelWidth) {
if (insertIsPresentAtX.test(x)) {
colorizer.pen(AnsiUtils.AnsiColor.RED).print("^");
++x;
} else if ((ref - this.interval.getStart()) % 10 == 0) {
final String f = String.format("%d", ref);
for (int i = 0; i < f.length() && x < pixelWidth; ++i) {
colorizer.pen(AnsiUtils.AnsiColor.GREEN).print(f.charAt(i));
if (!insertIsPresentAtX.test(x))
++ref;
++x;
}
} else {
out.print(".");
++ref;
++x;
}
}
out.println();
/* paint ref base */
out.print(margin("REF:"));
ref = this.interval.getStart();
x = 0;
while (x < pixelWidth) {
if (insertIsPresentAtX.test(x)) {
colorizer.paper(AnsiUtils.AnsiColor.YELLOW).print("*");
++x;
} else {
char refBase = refPosToBase.apply(ref - 1);
colorizer.pen(base2ansiColor.apply(refBase)).print(refBase);
++ref;
++x;
}
}
out.println();
/* loop over samples **/
for (final String groupName : group2record.keySet()) {
if (this.maxReadRowPerGroup == 0)
continue;
final ConsensusBuilder consensus = new ConsensusBuilder();
int y_group = 0;
final List<List<SAMRecord>> rows = new ArrayList<>();
out.println(margin(""));
switch(this.layoutReads) {
case name:
{
rows.addAll(group2record.get(groupName).stream().sorted((R1, R2) -> R1.getReadName().compareTo(R2.getReadName())).map(R -> Collections.singletonList(R)).collect(Collectors.toList()));
break;
}
default:
{
/* pileup reads */
for (final SAMRecord rec : group2record.get(groupName)) {
int y = 0;
for (y = 0; y < rows.size(); ++y) {
final List<SAMRecord> row = rows.get(y);
final SAMRecord last = row.get(row.size() - 1);
if (right().apply(last) + this.distance_between_reads < left().apply(rec)) {
row.add(rec);
break;
}
}
if (y == rows.size()) {
final List<SAMRecord> row = new ArrayList<>();
row.add(rec);
rows.add(row);
}
}
break;
}
}
// each row is only one read, so we need to print the groupName
if (layoutReads == LayoutReads.name) {
out.print(margin(groupName));
out.println();
}
/* print each row */
for (final List<SAMRecord> row : rows) {
++y_group;
boolean print_this_line = (this.maxReadRowPerGroup < 0 || y_group <= this.maxReadRowPerGroup);
if (print_this_line) {
// each row is only one read, print the read name
if (layoutReads == LayoutReads.name) {
String readName = row.get(0).getReadName();
if (row.get(0).getReadPairedFlag()) {
if (row.get(0).getFirstOfPairFlag()) {
readName += "/1";
}
if (row.get(0).getSecondOfPairFlag()) {
readName += "/2";
}
}
out.print(margin(readName));
} else {
out.print(margin(y_group == 1 ? groupName : ""));
}
}
ref = interval.getStart();
x = 0;
for (final SAMRecord rec : row) {
int readRef = left().apply(rec);
// pad before record
while (x < pixelWidth && ref < readRef && testInInterval.test(ref)) {
if (!insertIsPresentAtX.test(x))
++ref;
++x;
if (print_this_line)
out.print(' ');
consensus.add(' ');
}
int readpos = 0;
/* get read base function */
final Function<Integer, Character> baseAt = new Function<Integer, Character>() {
@Override
public Character apply(final Integer readpos) {
final byte[] readBases = rec.getReadBases();
if (readBases == SAMRecord.NULL_SEQUENCE)
return 'N';
if (readpos < 0 || readpos >= rec.getReadLength())
return '?';
return (char) readBases[readpos];
}
};
for (final CigarElement ce : rec.getCigar()) {
final CigarOperator op = ce.getOperator();
if (op.equals(CigarOperator.PADDING))
continue;
/* IN INSERTION, only print if showInsertions is true */
if (this.showInsertions && op.equals(CigarOperator.I)) {
int cigarIdx = 0;
while (x < pixelWidth && cigarIdx < ce.getLength()) {
if (testInInterval.test(readRef)) {
final char readbase = baseAt.apply(readpos);
if (print_this_line)
colorizer.paper(AnsiUtils.AnsiColor.RED).print(readbase);
consensus.add(readbase);
++x;
}
++cigarIdx;
++readpos;
}
continue;
}
int cigarIdx = 0;
while (x < pixelWidth && cigarIdx < ce.getLength()) {
colorizer.clear();
// pad before base
while (x < pixelWidth && testInInterval.test(readRef) && (insertIsPresentAtX.test(x))) {
++x;
if (print_this_line)
colorizer.paper(AnsiUtils.AnsiColor.YELLOW).print("*");
consensus.add(' ');
continue;
}
switch(op) {
case I:
{
// if visible, processed above
if (showInsertions)
throw new IllegalStateException();
readpos++;
break;
}
case P:
break;
case H:
{
if (showClip) {
if (testInInterval.test(readRef)) {
if (print_this_line)
colorizer.paper(AnsiUtils.AnsiColor.YELLOW).print('N');
// CLIPPED base not part of consensus
consensus.add(' ');
++x;
}
++readRef;
}
break;
}
case S:
{
if (showClip) {
if (testInInterval.test(readRef)) {
final char readBase = baseAt.apply(readpos);
if (print_this_line)
colorizer.paper(AnsiUtils.AnsiColor.YELLOW).print(readBase);
// CLIPPED base not part of consensus
consensus.add(' ');
++x;
}
++readpos;
++readRef;
} else {
readpos++;
}
break;
}
case D:
case N:
{
if (testInInterval.test(readRef)) {
if (print_this_line)
colorizer.paper(AnsiUtils.AnsiColor.RED).print('-');
// deletion not not part of consensus
consensus.add(' ');
++x;
}
++readRef;
break;
}
case EQ:
case M:
case X:
{
if (testInInterval.test(readRef)) {
final char refBase = Character.toUpperCase(refPosToBase.apply(readRef - 1));
char readBase = Character.toUpperCase(baseAt.apply(readpos));
consensus.add(readBase);
colorizer.pen(base2ansiColor.apply(readBase));
if (op.equals(CigarOperator.X) || (refBase != 'N' && readBase != 'N' && readBase != refBase)) {
colorizer.pen(AnsiUtils.AnsiColor.RED);
} else if (hideBases) {
if (rec.getReadNegativeStrandFlag()) {
readBase = ',';
} else {
readBase = '.';
}
}
if (showReadName) {
final String readName = rec.getReadName();
if (readpos < 0 || readpos >= readName.length()) {
readBase = '_';
} else {
readBase = readName.charAt(readpos);
}
}
if (rec.getReadNegativeStrandFlag()) {
readBase = Character.toLowerCase(readBase);
} else {
readBase = Character.toUpperCase(readBase);
}
if (print_this_line)
colorizer.print(readBase);
++x;
}
++readpos;
++readRef;
break;
}
}
++cigarIdx;
}
}
// end of loop cigar
ref = readRef;
}
// out.println( " "+ref+" "+row.get(0).getAlignmentStart()+" "+row.get(0).getCigarString()+" "+row.get(0).getReadString());
while (x < pixelWidth) {
if (print_this_line)
out.print(" ");
++x;
}
if (print_this_line)
out.println();
consensus.eol();
if (out.checkError())
break;
}
if (out.checkError())
break;
if (!this.hideConsensus && consensus.bases.stream().anyMatch(C -> C.getCoverage() > 0)) {
out.print(margin(groupName + " CONSENSUS"));
x = 0;
ref = interval.getStart();
while (x < consensus.bases.size()) {
final char refBase = Character.toUpperCase(refPosToBase.apply(ref - 1));
final char consensusBase = consensus.bases.get(x).getConsensus();
if (Character.isWhitespace(consensusBase)) {
// nothing
} else if (refBase != 'N' && consensusBase != refBase) {
colorizer.pen(AnsiUtils.AnsiColor.RED);
} else {
colorizer.pen(base2ansiColor.apply(consensusBase));
}
if (!insertIsPresentAtX.test(x))
++ref;
colorizer.print(consensusBase);
++x;
}
out.println();
}
if (this.numCoverageRows > 0) {
int minCov = consensus.bases.stream().mapToInt(C -> C.getCoverage()).min().orElse(0);
final int maxCov = consensus.bases.stream().mapToInt(C -> C.getCoverage()).max().orElse(0);
for (int y = 0; maxCov > 0 && y < this.numCoverageRows; ++y) {
if (minCov == maxCov)
minCov--;
double fract = (maxCov - minCov) / ((double) this.numCoverageRows);
int inverse_y = (this.numCoverageRows - 1) - y;
int d0 = (int) ((fract) * inverse_y);
// int d1 = (int)((fract) * (inverse_y+1));
out.print(margin(y == 0 ? groupName + " " + maxCov : (y + 1 == this.numCoverageRows ? String.valueOf(minCov) : "")));
for (x = 0; x < consensus.bases.size(); ++x) {
int depth = consensus.bases.get(x).getCoverage() - minCov;
colorizer.print(depth >= d0 ? BLACK_SQUARE : ' ');
}
out.println();
}
}
}
if (this.tabixKnownGene != null && this.indexedFastaSequenceFile != null) {
final List<KnownGene> genes = this.tabixKnownGene.getItemsInInterval(this.interval);
if (!genes.isEmpty()) {
out.println(this.knownGeneUri);
for (final KnownGene gene : genes) {
final KnownGene.CodingRNA codingRna = gene.getCodingRNA(contigSequence);
final KnownGene.Peptide peptide = codingRna.getPeptide();
out.print(margin(gene.getName()));
x = 0;
int ref0 = this.interval.getStart() - 1;
while (x < pixelWidth) {
if (insertIsPresentAtX.test(x)) {
out.print("*");
++x;
} else {
char pepChar = ' ';
if (ref0 >= gene.getTxStart() && ref0 < gene.getTxEnd()) {
pepChar = (gene.isPositiveStrand() ? '>' : '<');
int pepIdx = peptide.convertGenomicToPeptideCoordinate(ref0);
if (pepIdx != -1) {
final AminoAcid aa = AminoAcids.getAminoAcidFromOneLetterCode(peptide.charAt(pepIdx));
final String aa3 = aa == null ? "***" : aa.getThreeLettersCode();
final int[] offset = peptide.convertToGenomicCoordinates(pepIdx);
if (offset != null && offset.length == 3 && aa3 != null && aa3.length() == 3) {
if (offset[0] == ref0)
pepChar = aa3.charAt(0);
else if (offset[1] == ref0)
pepChar = aa3.charAt(1);
else if (offset[2] == ref0)
pepChar = aa3.charAt(2);
else
pepChar = '?';
} else {
pepChar = '?';
}
}
}
out.print(pepChar);
++ref0;
++x;
}
}
while (x < pixelWidth) {
out.print(" ");
++x;
}
out.println();
}
}
out.println();
}
/**
* variant section
*/
if (!this.vcfReaders.isEmpty() && !out.checkError()) {
final Function<GenotypeType, Character> gTypeToSymbol = new Function<GenotypeType, Character>() {
@Override
public Character apply(final GenotypeType gt) {
switch(gt) {
case NO_CALL:
return '?';
case HOM_REF:
return '0';
case HET:
return '1';
case HOM_VAR:
return '2';
case MIXED:
return 'm';
case UNAVAILABLE:
return 'u';
default:
return '.';
}
}
};
out.println();
for (final VcfSource r : this.vcfReaders) {
if (out.checkError())
break;
final VCFHeader header = r.vcfFileReader.getHeader();
final CloseableIterator<VariantContext> iter = r.vcfFileReader.query(this.interval.getContig(), interval.getStart(), interval.getEnd());
final List<VariantContext> variants = new ArrayList<>();
while (iter.hasNext()) {
variants.add(iter.next());
}
iter.close();
if (variants.isEmpty())
continue;
out.println(r.vcfFile.toString());
if (header.hasGenotypingData()) {
for (final String sample : header.getSampleNamesInOrder()) {
if (!variants.stream().map(V -> V.getGenotype(sample)).filter(G -> !hideNoCall || (hideNoCall && !G.isNoCall())).filter(G -> !hideHomRef || (hideHomRef && !G.isHomRef())).findAny().isPresent()) {
continue;
}
out.print(margin(sample));
ref = this.interval.getStart();
x = 0;
while (x < pixelWidth) {
if (insertIsPresentAtX.test(x)) {
out.print("*");
++x;
} else {
char refBase = ' ';
for (final VariantContext ctx : variants) {
if (ctx.getStart() == ref) {
final Genotype g = ctx.getGenotype(sample);
if (g.isNoCall() && this.hideNoCall)
continue;
if (g.isHomRef() && this.hideHomRef)
continue;
refBase = gTypeToSymbol.apply(g.getType());
break;
}
}
out.print(refBase);
++ref;
++x;
}
}
out.println();
}
} else // no genotype
{
for (final VariantContext ctx : variants) {
out.print(margin(String.valueOf(ctx.getStart()) + ":" + ctx.getReference().getDisplayString() + "/" + ctx.getAlternateAlleles().stream().map(A -> A.getDisplayString()).collect(Collectors.joining(","))));
ref = this.interval.getStart();
x = 0;
while (x < pixelWidth) {
if (insertIsPresentAtX.test(x)) {
out.print("*");
++x;
} else {
out.print(ctx.getStart() == ref ? '+' : ' ');
++ref;
++x;
}
}
out.println();
}
}
}
}
}
Aggregations