use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class GenomeParser method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
singleGenomes = new SingleGenome[baseLocations.length];
for (int i = 0; i < baseLocations.length; i++) {
singleGenomes[i] = new SingleGenome(baseLocations[i]);
}
} catch (SeqMonkException ex) {
Enumeration<ProgressListener> en = listeners.elements();
while (en.hasMoreElements()) {
en.nextElement().progressExceptionReceived(ex);
return;
}
}
for (int g = 0; g < singleGenomes.length; g++) {
File cacheCompleteFile = new File(baseLocations[g].getAbsoluteFile() + "/cache/cache.complete");
if (cacheCompleteFile.exists()) {
boolean cacheFailed = false;
try {
// Check the version inside the cache.complete file
BufferedReader br = new BufferedReader(new FileReader(cacheCompleteFile));
String line = br.readLine();
br.close();
if (line == null || line.length() == 0) {
// If there's no version in there then re-parse
cacheFailed = true;
}
// We re-parse if the cache was made by a different version
if (!SeqMonkApplication.VERSION.equals(line)) {
System.err.println("Version mismatch between cache ('" + line + "') and current version ('" + SeqMonkApplication.VERSION + "') - reparsing");
cacheFailed = true;
}
} catch (IOException ioe) {
cacheFailed = true;
}
// Check to see if the .dat files have changed since the cache
// file was saved
File[] files = baseLocations[g].listFiles(new FileFilter() {
public boolean accept(File f) {
if (f.getName().toLowerCase().endsWith(".dat") || f.getName().toLowerCase().endsWith(".gff") || f.getName().toLowerCase().endsWith(".gff3") || f.getName().toLowerCase().endsWith(".gtf") || f.getName().toLowerCase().endsWith(".gff.gz") || f.getName().toLowerCase().endsWith(".gff3.gz") || f.getName().toLowerCase().endsWith(".gtf.gz")) {
return true;
} else {
return false;
}
}
});
boolean datFilesUpdated = false;
for (int f = 0; f < files.length; f++) {
if (files[f].lastModified() > cacheCompleteFile.lastModified()) {
System.err.println("Modification on " + files[f] + " is newer than on " + cacheCompleteFile + " " + files[f].lastModified() + " vs " + cacheCompleteFile.lastModified());
datFilesUpdated = true;
break;
}
}
if (cacheFailed || datFilesUpdated) {
if (!cacheCompleteFile.delete()) {
System.err.println("Failed to delete the existing cache.complete file");
}
// System.err.println("Dat files updated - reparsing");
parseGenomeFiles(singleGenomes[g], baseLocations[g]);
} else {
reloadCacheFiles(singleGenomes[g], baseLocations[g]);
}
} else {
System.err.println("File '" + cacheCompleteFile + "' doesn't exist - reparsing");
parseGenomeFiles(singleGenomes[g], baseLocations[g]);
}
File aliasesFile = new File(baseLocations[g].getAbsoluteFile() + "/aliases.txt");
if (aliasesFile.exists()) {
try {
readAliases(aliasesFile, singleGenomes[g]);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
Genome genomeToReturn;
if (singleGenomes.length == 1) {
genomeToReturn = singleGenomes[0];
} else {
genomeToReturn = new MultiGenome(singleGenomes);
}
Enumeration<ProgressListener> en = listeners.elements();
while (en.hasMoreElements()) {
en.nextElement().progressComplete("load_genome", genomeToReturn);
}
}
use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class BedFileParser method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// System.err.println("Started parsing BED files");
int extendBy = prefs.extendReads();
try {
File[] probeFiles = getFiles();
DataSet[] newData = new DataSet[probeFiles.length];
for (int f = 0; f < probeFiles.length; f++) {
BufferedReader br;
if (probeFiles[f].getName().toLowerCase().endsWith(".gz")) {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(probeFiles[f]))));
} else {
br = new BufferedReader(new FileReader(probeFiles[f]));
}
String line;
if (prefs.isHiC()) {
newData[f] = new PairedDataSet(probeFiles[f].getName(), probeFiles[f].getCanonicalPath(), prefs.removeDuplicates(), prefs.hiCDistance(), prefs.hiCIgnoreTrans());
} else {
newData[f] = new DataSet(probeFiles[f].getName(), probeFiles[f].getCanonicalPath(), prefs.removeDuplicates());
}
int lineCount = 0;
// Now process the file
while ((line = br.readLine()) != null) {
if (cancel) {
br.close();
progressCancelled();
return;
}
// Ignore blank lines
if (line.trim().length() == 0)
continue;
++lineCount;
if (lineCount % 100000 == 0) {
progressUpdated("Read " + lineCount + " lines from " + probeFiles[f].getName(), f, probeFiles.length);
}
String[] sections = line.split("\t");
// Check to see if we've got enough data to work with
if (sections.length < 3) {
progressWarningReceived(new SeqMonkException("Not enough data from line '" + line + "'"));
// Skip this line...
continue;
}
int strand;
int start;
int end;
try {
// The start is zero indexed so we need to add 1 to get genomic positions
start = Integer.parseInt(sections[1]) + 1;
// The end is zero indexed, but not included in the feature position so
// we need to add one to get genomic coordinates, but subtract one to not
// include the final base.
end = Integer.parseInt(sections[2]);
// End must always be later than start
if (start > end) {
progressWarningReceived(new SeqMonkException("End position " + end + " was lower than start position " + start));
int temp = start;
start = end;
end = temp;
}
if (sections.length >= 6) {
if (sections[5].equals("+")) {
strand = Location.FORWARD;
} else if (sections[5].equals("-")) {
strand = Location.REVERSE;
} else {
progressWarningReceived(new SeqMonkException("Unknown strand character '" + sections[5] + "' marked as unknown strand"));
strand = Location.UNKNOWN;
}
if (extendBy > 0) {
if (strand == Location.REVERSE) {
start -= extendBy;
} else if (strand == Location.FORWARD) {
end += extendBy;
}
}
} else {
strand = Location.UNKNOWN;
}
} catch (NumberFormatException e) {
progressWarningReceived(new SeqMonkException("Location " + sections[0] + "-" + sections[1] + " was not an integer"));
continue;
}
try {
ChromosomeWithOffset c = dataCollection().genome().getChromosome(sections[0]);
// We also don't allow readings which are beyond the end of the chromosome
start = c.position(start);
end = c.position(end);
if (end > c.chromosome().length()) {
int overrun = end - c.chromosome().length();
progressWarningReceived(new SeqMonkException("Reading position " + end + " was " + overrun + "bp beyond the end of chr" + c.chromosome().name() + " (" + c.chromosome().length() + ")"));
continue;
}
// We can now make the new reading
long read = SequenceRead.packPosition(start, end, strand);
newData[f].addData(c.chromosome(), read);
} catch (IllegalArgumentException iae) {
progressWarningReceived(iae);
} catch (SeqMonkException sme) {
progressWarningReceived(sme);
continue;
}
}
// We're finished with the file.
br.close();
// Cache the data in the new dataset
progressUpdated("Caching data from " + probeFiles[f].getName(), f, probeFiles.length);
newData[f].finalise();
}
processingFinished(newData);
} catch (Exception ex) {
progressExceptionReceived(ex);
return;
}
}
use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class GenericSeqReadParser method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
// This call just makes sure that the options panel exists if
// it's never been called for before.
getOptionsPanel();
int removeDuplicates = optionsPanel.removeDuplicates();
int extendBy = optionsPanel.extendBy();
File[] probeFiles = getFiles();
DataSet[] newData = new DataSet[probeFiles.length];
for (int f = 0; f < probeFiles.length; f++) {
BufferedReader br;
if (probeFiles[f].getName().toLowerCase().endsWith(".gz")) {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(probeFiles[f]))));
} else {
br = new BufferedReader(new FileReader(probeFiles[f]));
}
String line;
// First skip the header lines
for (int i = 0; i < startRowValue; i++) {
line = br.readLine();
if (line == null) {
br.close();
throw new Exception("Ran out of file before skipping all of the header lines");
}
}
int maxIndexValue = 0;
if (chrColValue > maxIndexValue)
maxIndexValue = chrColValue;
if (startColValue > maxIndexValue)
maxIndexValue = startColValue;
if (endColValue > maxIndexValue)
maxIndexValue = endColValue;
if (strandColValue > maxIndexValue)
maxIndexValue = strandColValue;
if (countColValue > maxIndexValue)
maxIndexValue = countColValue;
if (optionsPanel.isHiC()) {
int distance = 0;
if (optionsPanel.hiCDistance.getText().length() > 0) {
distance = Integer.parseInt(optionsPanel.hiCDistance.getText());
}
// TODO: Add an option to remove trans hits when importing from the generic parser.
newData[f] = new PairedDataSet(probeFiles[f].getName(), probeFiles[f].getCanonicalPath(), removeDuplicates, distance, false);
} else {
newData[f] = new DataSet(probeFiles[f].getName(), probeFiles[f].getCanonicalPath(), removeDuplicates);
}
int lineCount = 0;
// Now process the rest of the file
while ((line = br.readLine()) != null) {
if (cancel) {
br.close();
progressCancelled();
return;
}
++lineCount;
if (lineCount % 100000 == 0) {
progressUpdated("Read " + lineCount + " lines from " + probeFiles[f].getName(), f, probeFiles.length);
}
String[] sections = line.split(delimitersValue);
// Check to see if we've got enough data to work with
if (maxIndexValue >= sections.length) {
progressWarningReceived(new SeqMonkException("Not enough data (" + sections.length + ") to get a probe name on line '" + line + "'"));
// Skip this line...
continue;
}
int strand;
int start;
int end;
int count = 1;
try {
start = Integer.parseInt(sections[startColValue].replaceAll(" ", ""));
end = Integer.parseInt(sections[endColValue].replaceAll(" ", ""));
// End must always be later than start
if (end < start) {
int temp = start;
start = end;
end = temp;
}
if (countColValue != -1 && sections[countColValue].length() > 0) {
try {
count = Integer.parseInt(sections[countColValue].replaceAll(" ", ""));
} catch (NumberFormatException e) {
progressWarningReceived(new SeqMonkException("Count value " + sections[countColValue] + " was not an integer"));
continue;
}
}
if (useStrand) {
sections[strandColValue] = sections[strandColValue].replaceAll(" ", "");
if (sections[strandColValue].equals("+") || sections[strandColValue].equals("1") || sections[strandColValue].equals("FF") || sections[strandColValue].equals("F")) {
strand = Location.FORWARD;
} else if (sections[strandColValue].equals("-") || sections[strandColValue].equals("-1") || sections[strandColValue].equals("RF") || sections[strandColValue].equals("R")) {
strand = Location.REVERSE;
} else {
progressWarningReceived(new SeqMonkException("Unknown strand character '" + sections[strandColValue] + "' marked as unknown strand"));
strand = Location.UNKNOWN;
}
} else {
strand = Location.UNKNOWN;
}
if (extendBy > 0) {
if (strand == Location.REVERSE) {
start -= extendBy;
} else {
end += extendBy;
}
}
} catch (NumberFormatException e) {
progressWarningReceived(new SeqMonkException("Location '" + sections[startColValue] + "'-'" + sections[endColValue] + "' was not an integer"));
continue;
}
ChromosomeWithOffset c;
try {
c = dataCollection().genome().getChromosome(sections[chrColValue]);
} catch (IllegalArgumentException sme) {
progressWarningReceived(sme);
continue;
}
start = c.position(start);
end = c.position(end);
// We also don't allow readings which are beyond the end of the chromosome
if (end > c.chromosome().length()) {
int overrun = end - c.chromosome().length();
progressWarningReceived(new SeqMonkException("Reading position " + end + " was " + overrun + "bp beyond the end of chr" + c.chromosome().name() + " (" + c.chromosome().length() + ")"));
continue;
}
if (start < 1) {
progressWarningReceived(new SeqMonkException("Reading start position " + start + " was less than 1"));
continue;
}
// We can now make the new reading
try {
long read = SequenceRead.packPosition(start, end, strand);
for (int i = 0; i < count; i++) {
newData[f].addData(c.chromosome(), read);
}
} catch (SeqMonkException e) {
progressWarningReceived(e);
continue;
}
// System.out.println("Added probe "+newProbe.name()+" on "+newProbe.chromosome()+" at pos "+newProbe.position());
}
// We're finished with the file.
br.close();
// Cache the data in the new dataset
progressUpdated("Caching data from " + probeFiles[f].getName(), f, probeFiles.length);
newData[f].finalise();
}
processingFinished(newData);
} catch (Exception ex) {
progressExceptionReceived(ex);
return;
}
}
use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class MethylKitFileParser method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
File[] methylKitFiles = getFiles();
DataSet[] newData = new DataSet[methylKitFiles.length];
for (int f = 0; f < methylKitFiles.length; f++) {
BufferedReader br;
if (methylKitFiles[f].getName().toLowerCase().endsWith(".gz")) {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(methylKitFiles[f]))));
} else {
br = new BufferedReader(new FileReader(methylKitFiles[f]));
}
String line;
newData[f] = new DataSet(methylKitFiles[f].getName(), methylKitFiles[f].getCanonicalPath(), prefs.removeDuplicates());
int lineCount = 0;
// Now process the file
while ((line = br.readLine()) != null) {
if (cancel) {
br.close();
progressCancelled();
return;
}
// Ignore blank lines
if (line.trim().length() == 0)
continue;
// In case it has comments
if (line.startsWith("#"))
continue;
// This is the start of the header
if (line.startsWith("chrBase"))
continue;
++lineCount;
if (lineCount % 100000 == 0) {
progressUpdated("Read " + lineCount + " lines from " + methylKitFiles[f].getName(), f, methylKitFiles.length);
}
String[] sections = line.split("\t");
// Check to see if we've got enough data to work with
if (sections.length < 6) {
progressWarningReceived(new SeqMonkException("Not enough data from line '" + line + "'"));
// Skip this line...
continue;
}
int position;
int totalCount;
int methCount;
int unmethCount;
try {
position = Integer.parseInt(sections[2]);
totalCount = Integer.parseInt(sections[4]);
methCount = Math.round((Float.parseFloat(sections[5]) / 100) * totalCount);
unmethCount = Math.round((Float.parseFloat(sections[6]) / 100) * totalCount);
} catch (NumberFormatException e) {
progressWarningReceived(new SeqMonkException("Failed to parse position and counts from " + line));
continue;
}
try {
ChromosomeWithOffset c = dataCollection().genome().getChromosome(sections[1]);
// We also don't allow readings which are beyond the end of the chromosome
if (position > c.chromosome().length()) {
int overrun = position - c.chromosome().length();
progressWarningReceived(new SeqMonkException("Reading position " + position + " was " + overrun + "bp beyond the end of chr" + c.chromosome().name() + " (" + c.chromosome().length() + ")"));
continue;
}
// We can now make the new reads
long methRead = SequenceRead.packPosition(position, position, Location.FORWARD);
long unmethRead = SequenceRead.packPosition(position, position, Location.REVERSE);
for (int i = 0; i < methCount; i++) {
newData[f].addData(c.chromosome(), methRead);
}
for (int i = 0; i < unmethCount; i++) {
newData[f].addData(c.chromosome(), unmethRead);
}
} catch (IllegalArgumentException iae) {
progressWarningReceived(iae);
} catch (SeqMonkException sme) {
progressWarningReceived(sme);
continue;
}
}
// We're finished with the file.
br.close();
// Cache the data in the new dataset
progressUpdated("Caching data from " + methylKitFiles[f].getName(), f, methylKitFiles.length);
newData[f].finalise();
}
processingFinished(newData);
} catch (Exception ex) {
progressExceptionReceived(ex);
return;
}
}
use of uk.ac.babraham.SeqMonk.SeqMonkException in project SeqMonk by s-andrews.
the class SeqMonkParser method parseLists.
/**
* Parses the set of probe lists.
*
* @param sections The tab split initial line from the probe lists section
* @throws SeqMonkException
* @throws IOException Signals that an I/O exception has occurred.
*/
private void parseLists(String[] sections) throws SeqMonkException, IOException {
if (sections.length != 2) {
throw new SeqMonkException("Probe Lists line didn't contain 2 sections");
}
int n = Integer.parseInt(sections[1]);
ProbeList[] lists = new ProbeList[n];
// We also store the probe lists in their appropriate linkage position
// to recreate the links between probe lists. The worst case scenario
// is that we have one big chain of linked lists so we make a linkage
// list which is the same size as the number of probe lists.
ProbeList[] linkage = new ProbeList[n + 1];
// The 0 linkage list will always be the full ProbeSet
linkage[0] = application.dataCollection().probeSet();
for (int i = 0; i < n; i++) {
String line = br.readLine();
if (line == null) {
throw new SeqMonkException("Ran out of probe data at line " + i + " (expected " + n + " probes)");
}
String[] listSections = line.split("\\t", -1);
// we allow for that not being present.
if (thisDataVersion < 5) {
lists[i] = new ProbeList(application.dataCollection().probeSet(), listSections[0], "", listSections[1]);
if (listSections.length > 2) {
lists[i].setDescription(listSections[2]);
} else {
lists[i].setDescription("No description");
}
} else {
lists[i] = new ProbeList(linkage[Integer.parseInt(listSections[0]) - 1], listSections[1], listSections[3], listSections[2]);
if (listSections.length > 4) {
lists[i].setComments(listSections[4].replaceAll("`", "\n"));
}
linkage[Integer.parseInt(listSections[0])] = lists[i];
}
}
// Next we reach the probe list data. These comes as a long list of values
// the first of which is the probe name, then either a numerical value if
// the probe is contained in that list, or a blank if it isn't.
String line = br.readLine();
if (line == null) {
throw new SeqMonkException("Couldn't find probe line for list data");
}
sections = line.split("\\t");
if (sections.length != 2) {
throw new SeqMonkException("Probe line didn't contain 2 sections");
}
if (!sections[0].equals("Probes")) {
throw new SeqMonkException("Couldn't find expected probe lists probe line");
}
n = Integer.parseInt(sections[1]);
for (int i = 0; i < n; i++) {
sections = br.readLine().split("\\t", -1);
if (sections.length != lists.length + 1) {
throw new SeqMonkException("Expected " + (lists.length + 1) + " sections in probe list section of data file for " + sections[0] + " but got " + sections.length);
}
if (i % 1000 == 0) {
Enumeration<ProgressListener> e = listeners.elements();
while (e.hasMoreElements()) {
e.nextElement().progressUpdated("Processed list data for " + i + " probes", i, n);
}
}
Probe p = probes[i];
if (p == null) {
continue;
}
for (int j = 0; j < lists.length; j++) {
if (sections[j + 1].length() > 0) {
if (sections[j + 1].equals("NaN")) {
lists[j].addProbe(p, null);
} else {
lists[j].addProbe(p, new Float(Float.parseFloat(sections[j + 1])));
}
}
}
}
}
Aggregations