use of uk.ac.babraham.SeqMonk.DataTypes.Genome.AnnotationSet in project SeqMonk by s-andrews.
the class GenericAnnotationParser method parseAnnotation.
/* (non-Javadoc)
* @see uk.ac.babraham.SeqMonk.AnnotationParsers.AnnotationParser#parseAnnotation(java.io.File, uk.ac.babraham.SeqMonk.DataTypes.Genome.Genome)
*/
public AnnotationSet[] parseAnnotation(File file, Genome genome) throws Exception {
this.file = file;
if (options == null) {
options = new JDialog(SeqMonkApplication.getInstance());
options.setModal(true);
options.setContentPane(new GenericAnnotationParserOptions(options));
options.setSize(700, 400);
options.setLocationRelativeTo(null);
}
// We have to set cancel to true as a default so we don't try to
// proceed with processing if the user closes the options using
// the X on the window.
options.setTitle("Format for " + file.getName() + "...");
cancel = true;
options.setVisible(true);
if (cancel) {
progressCancelled();
return null;
}
// We keep track of how many types have been added
// to catch if someone sets the wrong field and makes
// loads of different feature types.
HashSet<String> addedTypes = new HashSet<String>();
BufferedReader br;
if (file.getName().toLowerCase().endsWith(".gz")) {
br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
} else {
br = new BufferedReader(new FileReader(file));
}
String line;
// First skip the header lines
for (int i = 0; i < startRowValue; i++) {
line = br.readLine();
if (line == null) {
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 (typeColValue > maxIndexValue)
maxIndexValue = typeColValue;
if (nameColValue > maxIndexValue)
maxIndexValue = nameColValue;
if (descriptionColValue > maxIndexValue)
maxIndexValue = descriptionColValue;
Vector<AnnotationSet> annotationSets = new Vector<AnnotationSet>();
AnnotationSet currentAnnotation = new AnnotationSet(genome, file.getName());
annotationSets.add(currentAnnotation);
int lineCount = 0;
// Now process the rest of the file
while ((line = br.readLine()) != null) {
++lineCount;
if (cancel) {
progressCancelled();
return null;
}
if (lineCount % 1000 == 0) {
progressUpdated("Read " + lineCount + " lines from " + file.getName(), 0, 1);
}
if (lineCount > 1000000 && lineCount % 1000000 == 0) {
progressUpdated("Caching...", 0, 1);
currentAnnotation.finalise();
currentAnnotation = new AnnotationSet(genome, file.getName() + "[" + annotationSets.size() + "]");
annotationSets.add(currentAnnotation);
}
String[] sections = line.split(delimitersValue, -1);
// 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;
try {
start = Integer.parseInt(sections[startColValue]);
end = Integer.parseInt(sections[endColValue]);
// End must always be later than start
if (end < start) {
int temp = start;
start = end;
end = temp;
}
if (useStrand) {
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;
}
} catch (NumberFormatException e) {
progressWarningReceived(new SeqMonkException("Location " + sections[startColValue] + "-" + sections[endColValue] + " was not an integer"));
continue;
}
ChromosomeWithOffset c;
try {
c = genome.getChromosome(sections[chrColValue]);
} catch (IllegalArgumentException sme) {
progressWarningReceived(sme);
continue;
}
end = c.position(end);
start = c.position(start);
// 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;
}
// Now we can add the type, name and description
String type;
// If there's a column containing the type then use that
if (typeColValue >= 0) {
type = sections[typeColValue];
} else // If not then use the manually specified type if they set it
if (featureType != null && featureType.length() > 0) {
type = featureType;
} else // If all else fails use the file name as the type
{
type = file.getName();
}
if (!addedTypes.contains(type)) {
addedTypes.add(type);
if (addedTypes.size() > 100) {
throw new SeqMonkException("More than 100 different types of feature added - you don't want to do this!");
}
}
String name = null;
if (nameColValue >= 0) {
name = sections[nameColValue];
}
String description = null;
if (descriptionColValue >= 0) {
description = sections[descriptionColValue];
}
// We can now make the new annotation
Feature feature = new Feature(type, c.chromosome().name());
if (name != null) {
feature.addAttribute("name", name);
}
if (description != null)
feature.addAttribute("description", description);
feature.setLocation(new Location(start, end, strand));
currentAnnotation.addFeature(feature);
// System.out.println("Added probe "+newProbe.name()+" on "+newProbe.chromosome()+" at pos "+newProbe.position());
}
// We're finished with the file.
br.close();
options.dispose();
options = null;
return annotationSets.toArray(new AnnotationSet[0]);
}
Aggregations