use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.
the class AnnotatedListReport method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
String annotationTypeValue = (String) annotationType.getSelectedItem();
int distanceLimit = 0;
// Check what to do with unannotated probes
boolean includeAll = true;
if (((String) excludes.getSelectedItem()).equals("Exclude")) {
includeAll = false;
}
String annotationPositionValue = (String) annotationPosition.getSelectedItem();
// We're going to set up a set of booleans which tell us which kinds
// of relationships we're allowed to look for later.
boolean surrounding = true;
boolean upstream = true;
boolean downstream = true;
boolean matchname = false;
boolean exactmatch = false;
boolean skipAnnotation = false;
if (annotationPositionValue.equals("[Don't annotate]")) {
upstream = false;
downstream = false;
surrounding = false;
skipAnnotation = true;
} else if (annotationPositionValue.equals("overlapping")) {
upstream = false;
downstream = false;
} else if (annotationPositionValue.equals("exactly overlapping")) {
upstream = false;
downstream = false;
exactmatch = true;
} else if (annotationPositionValue.equals("surrounding or upstream")) {
downstream = false;
} else if (annotationPositionValue.equals("surrounding or downstream")) {
upstream = false;
} else if (annotationPositionValue.equals("upstream")) {
surrounding = false;
downstream = false;
} else if (annotationPositionValue.equals("downstream")) {
surrounding = false;
upstream = false;
} else if (annotationPositionValue.equals("closest")) {
// Leave things as they are!
} else if (annotationPositionValue.equals("name matched")) {
matchname = true;
upstream = false;
surrounding = false;
downstream = false;
} else {
System.err.println("Didn't recognise position value '" + annotationPositionValue + "'");
}
// surrounding.
if (!annotationPositionValue.equals("surrounding")) {
if (annotationLimit.getText().length() > 0) {
distanceLimit = Integer.parseInt(annotationLimit.getText());
}
}
Vector<ProbeAnnotation> annotations = new Vector<ProbeAnnotation>();
// Since we're going to be making the annotations on the
// basis of position we should go through all probes one
// chromosome at a time.
Chromosome[] chrs = collection.genome().getAllChromosomes();
for (int c = 0; c < chrs.length; c++) {
progressUpdated("Processing Chr" + chrs[c].name(), c, chrs.length);
Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(chrs[c]);
Feature[] features = new Feature[0];
if (!skipAnnotation) {
features = collection.genome().annotationCollection().getFeaturesForType(chrs[c], annotationTypeValue);
}
// We can now step through the probes looking for the best feature match
for (int p = 0; p < probes.length; p++) {
if (cancel) {
progressCancelled();
return;
}
String nameWithoutExtensions = "";
String nameWithoutTranscript = "";
if (matchname) {
nameWithoutExtensions = probes[p].name().replaceFirst("_upstream$", "").replaceAll("_downstream$", "").replaceAll("_gene$", "");
nameWithoutTranscript = nameWithoutExtensions.replaceAll("-\\d\\d\\d$", "");
}
Feature bestFeature = null;
int closestDistance = 0;
String relationshipType = "Not found";
for (int f = 0; f < features.length; f++) {
if (matchname) {
// Simplest check is if the name matches exactly
if (features[f].name().equals(probes[p].name()) || features[f].name().equals(nameWithoutExtensions) || features[f].name().equals(nameWithoutTranscript)) {
bestFeature = features[f];
closestDistance = 0;
relationshipType = "Name match";
break;
}
}
if (surrounding) {
if (probes[p].start() <= features[f].location().end() && probes[p].end() >= features[f].location().start()) {
// If this is an exact overlap then we check to see that the positions exactly align.
if ((!exactmatch) || (probes[p].start() == features[f].location().start() && probes[p].end() == features[f].location().end())) {
bestFeature = features[f];
closestDistance = 0;
relationshipType = "overlapping";
// Once we've found an overlapping feature we quit.
break;
}
}
}
if (downstream) {
// Check if the feature is downstream
// Get the distance to the start
int d = 0;
if (features[f].location().strand() == Location.FORWARD) {
d = features[f].location().start() - probes[p].end();
} else {
d = probes[p].start() - features[f].location().end();
}
if (d >= 0) {
if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
continue;
}
// See if this is the closest feature we have so far...
if (bestFeature == null || d < closestDistance) {
bestFeature = features[f];
relationshipType = "downstream";
closestDistance = d;
}
continue;
}
}
if (upstream) {
// Check if the feature is upstream
// Get the distance to the start
int d = 0;
if (features[f].location().strand() == Location.FORWARD) {
d = probes[p].start() - features[f].location().end();
} else {
d = features[f].location().start() - probes[p].end();
}
if (d >= 0) {
if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
continue;
}
// See if this is the closest feature we have so far...
if (bestFeature == null || d < closestDistance) {
bestFeature = features[f];
relationshipType = "upstream";
closestDistance = d;
}
continue;
}
}
}
if (bestFeature == null && (!includeAll)) {
continue;
}
annotations.add(new ProbeAnnotation(probes[p], bestFeature, closestDistance, relationshipType));
}
}
if (includeAll) {
Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(null);
for (int p = 0; p < probes.length; p++) {
annotations.add(new ProbeAnnotation(probes[p], null, 0, "Not found"));
}
}
DataStore[] stores = new DataStore[0];
if (((String) data.getSelectedItem()).equals("Include")) {
stores = storesToAnnotate;
}
TableModel model = new AnnotationTableModel(annotations.toArray(new ProbeAnnotation[0]), collection.probeSet().getActiveList(), stores);
reportComplete(model);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.
the class ProbeGroupReport method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
String annotationTypeValue = (String) annotationType.getSelectedItem();
int distanceLimit = 0;
// Check what to do with unannotated probes
boolean includeAll = true;
if (((String) excludes.getSelectedItem()).equals("Exclude")) {
includeAll = false;
}
String annotationPositionValue = (String) annotationPosition.getSelectedItem();
// We're going to set up a set of booleans which tell us which kinds
// of relationships we're allowed to look for later.
boolean annotateIndividualProbes = true;
boolean justEnclosed = false;
boolean surrounding = true;
boolean upstream = true;
boolean downstream = true;
if (annotationPositionValue.equals("[Don't annotate]")) {
// It's easier to leave this true as we can bail out the annotation more quickly
annotateIndividualProbes = true;
justEnclosed = false;
surrounding = false;
upstream = false;
downstream = false;
} else if (annotationPositionValue.equals("all overlapping")) {
annotateIndividualProbes = false;
justEnclosed = false;
} else if (annotationPositionValue.equals("all enclosed")) {
annotateIndividualProbes = false;
justEnclosed = true;
} else if (annotationPositionValue.equals("surrounding")) {
upstream = false;
downstream = false;
} else if (annotationPositionValue.equals("surrounding or upstream")) {
downstream = false;
} else if (annotationPositionValue.equals("surrounding or downstream")) {
upstream = false;
} else if (annotationPositionValue.equals("upstream")) {
surrounding = false;
downstream = false;
} else if (annotationPositionValue.equals("downstream")) {
surrounding = false;
upstream = false;
} else if (annotationPositionValue.equals("closest")) {
// Leave things as they are!
} else {
System.err.println("Didn't recognise position value '" + annotationPositionValue + "'");
}
// surrounding.
if (!annotationPositionValue.equals("surrounding")) {
if (annotationLimit.getText().length() > 0) {
distanceLimit = Integer.parseInt(annotationLimit.getText());
}
}
// Find out how closely to group probes
int groupDistance = Integer.parseInt(probeDistanceLimit.getText());
Vector<ProbeGroupAnnotation> annotations = new Vector<ProbeGroupAnnotation>();
// Since we're going to be making the annotations on the
// basis of position we should go through all probes one
// chromosome at a time.
Chromosome[] chrs = collection.genome().getAllChromosomes();
for (int c = 0; c < chrs.length; c++) {
progressUpdated("Processing Chr" + chrs[c].name(), c, chrs.length);
Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(chrs[c]);
Feature[] features = collection.genome().annotationCollection().getFeaturesForType(chrs[c], annotationTypeValue);
ProbeGroupAnnotation group = new ProbeGroupAnnotation();
int lastPosition = -1;
// We can now step through the probes looking for the best feature match
for (int p = 0; p < probes.length; p++) {
if (cancel) {
progressCancelled();
return;
}
if (lastPosition == -1 || probes[p].start() - groupDistance <= lastPosition) {
// We're in the same group
group.addProbe(probes[p]);
if (probes[p].end() > lastPosition)
lastPosition = probes[p].end();
// System.err.println("Adding to existing group");
} else {
// group annotations
if (!annotateIndividualProbes) {
annotateGroup(group, justEnclosed, annotationTypeValue);
}
if (includeAll || group.featureNames().length() > 0) {
// System.err.println("Adding group to keepers");
annotations.add(group);
} else {
// System.err.println("Not adding this group");
}
// Create a new group and add the current probe to start it off.
group = new ProbeGroupAnnotation();
group.addProbe(probes[p]);
lastPosition = probes[p].end();
}
if (!annotateIndividualProbes)
continue;
Feature bestFeature = null;
int closestDistance = 0;
for (int f = 0; f < features.length; f++) {
if (surrounding) {
if (probes[p].start() <= features[f].location().end() && probes[p].end() >= features[f].location().start()) {
bestFeature = features[f];
closestDistance = 0;
// Once we've found an overlapping feature we quit.
break;
}
}
if (downstream) {
// Check if the feature is downstream
// Get the distance to the start
int d = 0;
if (features[f].location().strand() == Location.FORWARD) {
d = features[f].location().start() - probes[p].end();
} else {
d = probes[p].start() - features[f].location().end();
}
if (d >= 0) {
if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
continue;
}
// See if this is the closest feature we have so far...
if (bestFeature == null || d < closestDistance) {
bestFeature = features[f];
closestDistance = d;
}
continue;
}
}
if (upstream) {
// Check if the feature is upstream
// Get the distance to the start
int d = 0;
if (features[f].location().strand() == Location.FORWARD) {
d = probes[p].start() - features[f].location().end();
} else {
d = features[f].location().start() - probes[p].end();
}
if (d >= 0) {
if (d > distanceLimit || (bestFeature != null && d > closestDistance)) {
continue;
}
// See if this is the closest feature we have so far...
if (bestFeature == null || d < closestDistance) {
bestFeature = features[f];
closestDistance = d;
}
continue;
}
}
}
if (bestFeature != null) {
group.addFeature(bestFeature);
}
}
// and do any necessary annotation.
if (group.numberOfProbes() > 0) {
// We need to do the annotation for this last group (if we have to)
if (!annotateIndividualProbes) {
annotateGroup(group, justEnclosed, annotationTypeValue);
}
if (includeAll || group.featureNames().length() > 0) {
// System.err.println("Adding group to keepers");
annotations.add(group);
}
}
}
DataStore[] stores = new DataStore[0];
if (((String) data.getSelectedItem()).equals("Include")) {
stores = storesToAnnotate;
}
TableModel model = new AnnotationTableModel(annotations.toArray(new ProbeGroupAnnotation[0]), stores);
reportComplete(model);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.
the class ChromosomeViewReport method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
int upstreamContext = 0;
if (upstreamContextField.getText().length() > 0) {
upstreamContext = Integer.parseInt(upstreamContextField.getText());
}
int downstreamContext = 0;
if (downstreamContextField.getText().length() > 0) {
downstreamContext = Integer.parseInt(downstreamContextField.getText());
}
// We need an HTML file we're going to make into an index
JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getSaveLocation());
chooser.setMultiSelectionEnabled(false);
chooser.addChoosableFileFilter(new HTMLFileFilter());
int result = chooser.showSaveDialog(SeqMonkApplication.getInstance());
if (result == JFileChooser.CANCEL_OPTION)
return;
File file = chooser.getSelectedFile();
SeqMonkPreferences.getInstance().setLastUsedSaveLocation(file);
if (file.isDirectory())
return;
if (!(file.getPath().toLowerCase().endsWith(".html") || file.getPath().toLowerCase().endsWith(".html"))) {
file = new File(file.getPath() + ".html");
}
// Check if we're stepping on anyone's toes...
if (file.exists()) {
int answer = JOptionPane.showOptionDialog(SeqMonkApplication.getInstance(), file.getName() + " exists. Do you want to overwrite the existing file?", "Overwrite file?", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Overwrite and Save", "Cancel" }, "Overwrite and Save");
if (answer > 0) {
return;
}
}
// We need to make a folder from the file
File outputFolder = new File(file.getAbsolutePath().replace(".html", "_files"));
outputFolder.mkdir();
System.err.println("Output file is " + file.getAbsolutePath() + " output folder is " + outputFolder.getAbsolutePath());
Chromosome[] chrs = collection.genome().getAllChromosomes();
// Save the starting location so we can go back there after we're done
Chromosome currentChr = DisplayPreferences.getInstance().getCurrentChromosome();
long currentLocation = DisplayPreferences.getInstance().getCurrentLocation();
for (int c = 0; c < chrs.length; c++) {
progressUpdated("Processing Chr" + chrs[c].name(), c, chrs.length);
Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(chrs[c]);
// We can now step through the probes making the image for each one
for (int p = 0; p < probes.length; p++) {
if (cancel) {
progressCancelled();
return;
}
int start = probes[p].start();
int end = probes[p].end();
start -= upstreamContext;
end += downstreamContext;
if (start < 1)
start = 1;
if (end < 1)
end = 1;
if (start > chrs[c].length())
start = chrs[c].length();
if (end > chrs[c].length())
end = chrs[c].length();
if (end - start < 100) {
progressWarningReceived(new SeqMonkException("View for " + probes[p].name() + " was too small to export"));
continue;
}
DisplayPreferences.getInstance().setLocation(chrs[c], SequenceRead.packPosition(start, end, Location.FORWARD));
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
File saveFile = new File(outputFolder.getAbsolutePath() + "/" + probes[p].name() + ".png");
try {
ImageSaver.savePNG(SeqMonkApplication.getInstance().chromosomeViewer(), saveFile);
} catch (IOException e) {
progressExceptionReceived(e);
return;
}
}
}
DisplayPreferences.getInstance().setLocation(currentChr, currentLocation);
reportComplete(null);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.
the class FeatureReport method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
String annotationTypeValue = (String) annotationType.getSelectedItem();
// Check what to do with unannotated features
boolean includeAll = true;
if (((String) excludes.getSelectedItem()).equals("Exclude")) {
includeAll = false;
}
// Check if we're only interested in exact overlaps
boolean onlyExactOverlaps = false;
if (((String) (overlapType.getSelectedItem())).equals("exactly overlapping")) {
onlyExactOverlaps = true;
}
Vector<FeatureAnnotation> annotations = new Vector<FeatureAnnotation>();
// Since we're going to be making the annotations on the
// basis of position we should go through all probes one
// chromosome at a time.
Chromosome[] chrs = dataCollection().genome().getAllChromosomes();
for (int c = 0; c < chrs.length; c++) {
progressUpdated("Processing Chr" + chrs[c].name(), c, chrs.length);
Probe[] probes = collection.probeSet().getActiveList().getProbesForChromosome(chrs[c]);
Feature[] features = collection.genome().annotationCollection().getFeaturesForType(chrs[c], annotationTypeValue);
FeatureAnnotation[] thisChrFeatureAnnotations = new FeatureAnnotation[features.length];
for (int f = 0; f < features.length; f++) {
if (cancel) {
progressCancelled();
return;
}
thisChrFeatureAnnotations[f] = new FeatureAnnotation(features[f]);
}
// We can now step through the probes looking for a match
for (int p = 0; p < probes.length; p++) {
for (int f = 0; f < thisChrFeatureAnnotations.length; f++) {
if (cancel) {
progressCancelled();
return;
}
// This method will silently reject any probes it doesn't like
thisChrFeatureAnnotations[f].addProbe(probes[p], onlyExactOverlaps);
}
}
// Now we add the features we want to keep to the overall collection
for (int f = 0; f < thisChrFeatureAnnotations.length; f++) {
if (includeAll || thisChrFeatureAnnotations[f].numberOfProbes() > 0) {
annotations.add(thisChrFeatureAnnotations[f]);
}
}
}
TableModel model = new AnnotationTableModel(annotations.toArray(new FeatureAnnotation[0]), storesToAnnotate);
reportComplete(model);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Chromosome in project SeqMonk by s-andrews.
the class EvenCoverageProbeGenerator method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
Chromosome[] chromosomes = collection.genome().getAllChromosomes();
Vector<Probe> newProbes = new Vector<Probe>();
for (int c = 0; c < chromosomes.length; c++) {
// Time for an update
updateGenerationProgress("Processed " + c + " chromosomes", c, chromosomes.length);
// We'll merge together the reads for all of the selected DataStores and
// compute a single set of probes which covers all of them.
ReadsWithCounts[] v = new ReadsWithCounts[selectedStores.length];
for (int s = 0; s < selectedStores.length; s++) {
v[s] = selectedStores[s].getReadsForChromosome(chromosomes[c]);
}
ReadsWithCounts reads = new ReadsWithCounts(v);
v = null;
if (reads.totalCount() == 0) {
// System.err.println("Skipping strand "+strandsToTry[strand]+" on chr "+chromosomes[c]);
continue;
}
int strandForNewProbes = Location.UNKNOWN;
int start = SequenceRead.start(reads.reads[0]);
int end = SequenceRead.end(reads.reads[0]);
int count = reads.counts[0];
for (int r = 1; r < reads.reads.length; r++) {
// See if we need to quit
if (cancel) {
generationCancelled();
return;
}
if (count > 0 && ((maxSize > 0 && (SequenceRead.end(reads.reads[r]) - start) + 1 > maxSize)) || (count + reads.counts[r] > targetReadCount)) {
// Make a probe out of what we have and start a new one with the
// read we're currently looking at
Probe p = new Probe(chromosomes[c], start, end, strandForNewProbes);
newProbes.add(p);
start = end + 1;
end = Math.max(end + 2, SequenceRead.end(reads.reads[r]));
count = SequenceRead.end(reads.counts[r]);
} else {
count += reads.counts[r];
if (SequenceRead.end(reads.reads[r]) > end) {
end = SequenceRead.end(reads.reads[r]);
}
}
}
if (count > 0) {
Probe p = new Probe(chromosomes[c], start, end, strandForNewProbes);
newProbes.add(p);
}
}
Probe[] finalList = newProbes.toArray(new Probe[0]);
newProbes.clear();
ProbeSet finalSet = new ProbeSet(getDescription(), finalList);
generationComplete(finalSet);
}
Aggregations