use of exceptions.InvalidGenomicCoordsException in project ASCIIGenome by dariober.
the class GenomicCoords method parseStringToGenomicCoords.
/**
* Parse string to return coordinates. This method simply populates the fields chrom, from, to by
* parsing the input string.
* This object can't be used as such as there is no check for valid input. It should be used only by the constructor.
* @return
* @throws InvalidGenomicCoordsException
* @throws IOException
*/
private GenomicCoords parseStringToGenomicCoords(String x) throws InvalidGenomicCoordsException, IOException {
// Default start/end coords.
Integer from = 1;
Integer to = this.getTerminalWidth();
GenomicCoords xgc = new GenomicCoords(this.getTerminalWidth());
if (x == null || x.isEmpty()) {
x = "Undefined_contig";
}
x = x.trim();
int nsep = StringUtils.countMatches(x, ":");
if (nsep == 0) {
// Only chrom present. It will not handle well chrom names containing ':'
xgc.chrom = x.trim();
xgc.from = from;
xgc.to = to;
if (xgc.samSeqDict != null && xgc.to > samSeqDict.getSequence(xgc.chrom).getSequenceLength()) {
xgc.to = samSeqDict.getSequence(xgc.chrom).getSequenceLength();
}
} else {
xgc.chrom = StringUtils.substringBeforeLast(x, ":").trim();
// Strip chromosome name, remove commas from integers.
String fromTo = StringUtils.substringAfterLast(x, ":").replaceAll(",", "").trim();
nsep = StringUtils.countMatches(fromTo, "-");
if (nsep == 0) {
// Only start position given
xgc.from = Integer.parseInt(StringUtils.substringBefore(fromTo, "-").trim());
xgc.to = xgc.from + this.getTerminalWidth() - 1;
} else if (nsep == 1) {
// From and To positions given.
xgc.from = Integer.parseInt(StringUtils.substringBefore(fromTo, "-").trim());
xgc.to = Integer.parseInt(StringUtils.substringAfter(fromTo, "-").trim());
} else {
InvalidGenomicCoordsException e = new InvalidGenomicCoordsException();
System.err.println("\nUnexpected format for region " + x + "\n");
e.printStackTrace();
throw e;
}
}
return xgc;
}
use of exceptions.InvalidGenomicCoordsException in project ASCIIGenome by dariober.
the class GenomicCoords method getChromIdeogram.
/**
* Produce a string representation of the current position on the chromosome
* @param nDist: Distance between labels
* @throws IOException
* @throws InvalidGenomicCoordsException
* @throws InvalidColourException
*/
public String getChromIdeogram(int nDist, boolean noFormat) throws InvalidGenomicCoordsException, IOException, InvalidColourException {
if (this.samSeqDict == null || this.samSeqDict.size() == 0) {
return null;
}
List<Double> positionMap = null;
try {
positionMap = Utils.seqFromToLenOut(1, this.samSeqDict.getSequence(this.chrom).getSequenceLength(), this.getUserWindowSize());
} catch (NullPointerException e) {
throw new InvalidGenomicCoordsException();
}
// This code taken from printableRuler() above.
String numberLine = "";
int prevLen = 0;
int j = 0;
while (j < positionMap.size()) {
int num = (int) Math.rint(Utils.roundToSignificantFigures(positionMap.get(j), 2));
// String.valueOf(num);
String posMark = Utils.parseIntToMetricSuffix(num);
if (j == 0) {
numberLine = posMark;
j += posMark.length();
} else if ((numberLine.length() - prevLen) >= nDist) {
prevLen = numberLine.length();
numberLine = numberLine + posMark;
j += posMark.length();
} else {
numberLine = numberLine + SPACER;
j++;
}
}
List<String> map = new ArrayList<String>();
for (int i = 0; i < numberLine.length(); i++) {
map.add(numberLine.charAt(i) + "");
}
// ------------------
int fromTextPos = Utils.getIndexOfclosestValue(this.from, positionMap);
int toTextPos = Utils.getIndexOfclosestValue(this.to, positionMap);
boolean isFirst = true;
int lastTick = -1;
for (int i = fromTextPos; i <= toTextPos; i++) {
if (isFirst || map.get(i).equals(SPACER)) {
map.set(i, TICKED);
isFirst = false;
}
lastTick = i;
}
map.set(lastTick, TICKED);
String ideogram = StringUtils.join(map, "");
if (ideogram.length() > this.getUserWindowSize()) {
ideogram = ideogram.substring(0, this.getUserWindowSize());
}
if (!noFormat) {
ideogram = "\033[48;5;" + Config.get256Color(ConfigKey.background) + ";38;5;" + Config.get256Color(ConfigKey.chrom_ideogram) + "m" + ideogram;
}
return ideogram;
}
use of exceptions.InvalidGenomicCoordsException in project ASCIIGenome by dariober.
the class InteractiveInput method setGenome.
private void setGenome(List<String> cmdTokens, TrackProcessor proc) throws InvalidGenomicCoordsException, IOException, InvalidCommandLineException {
List<String> tokens = new ArrayList<String>(cmdTokens);
tokens.remove(0);
if (tokens.size() == 0) {
// Try to read fasta from history file
ASCIIGenomeHistory ag = new ASCIIGenomeHistory();
try {
tokens.add(ag.getReference().get(0));
System.err.println("Using " + ag.getReference().get(0));
} catch (Exception e) {
System.err.println("A previous reference file was not found.");
throw new InvalidCommandLineException();
}
}
GenomicCoords testSeqDict = new GenomicCoords("default", Utils.getTerminalWidth(), null, null);
testSeqDict.setGenome(tokens, true);
if (testSeqDict.getSamSeqDict() != null) {
proc.getGenomicCoordsHistory().setGenome(tokens);
} else {
System.err.println(Utils.padEndMultiLine("Cannot set genome from " + tokens, Utils.getTerminalWidth()));
this.interactiveInputExitCode = ExitCode.ERROR;
}
}
use of exceptions.InvalidGenomicCoordsException in project ASCIIGenome by dariober.
the class TrackSet method addIntervalFeatureTrackFromVCF.
private void addIntervalFeatureTrackFromVCF(String sourceName, GenomicCoords gc, String trackTag) throws ClassNotFoundException, IOException, InvalidGenomicCoordsException, InvalidRecordException, SQLException {
int idForTrack = this.getNextTrackId();
// String trackId= new File(sourceName).getName() + "#" + idForTrack;
String trackId = sourceName + "#" + idForTrack;
// If this VCF file has sequence dictionary, check the coordinates in gc are compatible
// If they are not, throw an exception which force resetting the coords.
SAMSequenceDictionary seqDict = Utils.getVCFHeader(sourceName).getSequenceDictionary();
if (seqDict != null && seqDict.getSequence(gc.getChrom()) == null) {
throw new InvalidGenomicCoordsException();
}
TrackIntervalFeature tif = new TrackIntervalFeature(sourceName, gc);
tif.setTrackTag(trackId);
this.trackList.add(tif);
}
use of exceptions.InvalidGenomicCoordsException in project ASCIIGenome by dariober.
the class UcscGenePred method getCDS.
private List<IntervalFeature> getCDS(List<IntervalFeature> exons, final int cdsStart, final int cdsEnd, String cdsStartStat, String cdsEndStat) throws InvalidGenomicCoordsException {
if (cdsStart > cdsEnd) {
// There are no CDS in this transcript
return new ArrayList<IntervalFeature>();
}
// Iterate through exons checking whether at least part of it is containing in the interval cdsStart:cdsEnd.
// If so, take the exon slice inside the interval cdsStart:cdsEnd and add it to the list of CDSs
List<IntervalFeature> cds = new ArrayList<IntervalFeature>();
for (int i = 0; i < exons.size(); i++) {
IntervalFeature exon = exons.get(i);
if (exon.getTo() < cdsStart || exon.getFrom() > cdsEnd) {
// Exon is not in interval cdsStart:cdsEnd
continue;
}
int cdsFrom = exon.getFrom();
if (cdsFrom < cdsStart) {
// If only part of exon is CDS
cdsFrom = cdsStart;
}
int cdsTo = exon.getTo();
if (cdsTo > cdsEnd) {
// If only part of exon is CDS
cdsTo = cdsEnd;
}
String attr = Lists.newArrayList(Splitter.on("\t").omitEmptyStrings().split(exon.getRaw())).get(8);
// Build the interval feature object
String[] gff = new String[9];
gff[0] = exons.get(0).getChrom();
gff[1] = exons.get(0).getSource();
gff[2] = "CDS";
gff[3] = Integer.toString(cdsFrom);
gff[4] = Integer.toString(cdsTo);
gff[5] = ".";
gff[6] = String.valueOf(exons.get(0).getStrand());
// It's unclear to me how frames are assigned so leave it N/A.
gff[7] = ".";
gff[8] = attr;
IntervalFeature x = new IntervalFeature(Joiner.on("\t").join(gff), TrackFormat.GTF, null);
cds.add(x);
}
// and remove the remainder from the following CDS.
if (exons.get(0).getStrand() == '+' && cdsEndStat.equals("cmpl")) {
IntervalFeature stopCds = cds.get(cds.size() - 1);
int newStop = stopCds.getTo() - 3;
int remainder = -(newStop - stopCds.getFrom());
if (remainder > 0) {
// If remainder is > 0, this CDS doesn't exist at all and must be removed. This happens if the
// stop codon is split across two exons (rare but it happens).
// We also need to chip off "remainder" from the previous CDS.
cds.remove(stopCds);
stopCds = cds.get(cds.size() - 1);
// I'm not sure why you need +1 to make it work!
newStop = stopCds.getTo() - remainder + 1;
}
if (newStop <= 0 || newStop < stopCds.getFrom()) {
// Sanity check
throw new InvalidGenomicCoordsException();
}
// We create an intervalFeature from scratch that will replace the old one.
List<String> raw = Lists.newArrayList(Splitter.on("\t").omitEmptyStrings().splitToList(stopCds.getRaw()));
// Replace end coord
raw.set(4, Integer.toString(newStop));
// Replace last element.
cds.set(cds.size() - 1, new IntervalFeature(Joiner.on("\t").join(raw), TrackFormat.GTF, null));
} else if (exons.get(0).getStrand() == '-' && cdsStartStat.equals("cmpl")) {
// same as above. This time apply to first CDS whose start has to be increased by 3
IntervalFeature stopCds = cds.get(0);
int newStop = stopCds.getFrom() + 3;
int remainder = newStop - stopCds.getTo();
if (remainder > 0) {
// If remainder is >= 0, this CDS doesn't exist at all and must be removed. This happens if the
// stop codon is split across two exons (rare but it happens).
// We also need to chip off "remainder" from the next CDS.
cds.remove(stopCds);
stopCds = cds.get(0);
// Not sure why -1 works!
newStop = stopCds.getFrom() + remainder - 1;
}
if (newStop <= 0 || newStop > stopCds.getTo()) {
// Sanity check
throw new InvalidGenomicCoordsException();
}
// We create an intervalFeature from scratch that will replace the old one.
List<String> raw = Lists.newArrayList(Splitter.on("\t").omitEmptyStrings().splitToList(stopCds.getRaw()));
// Replace start coord
raw.set(3, Integer.toString(newStop));
// Replace last element.
cds.set(0, new IntervalFeature(Joiner.on("\t").join(raw), TrackFormat.GTF, null));
}
return cds;
}
Aggregations