use of uk.ac.babraham.SeqMonk.DataTypes.Probes.Probe 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.Probes.Probe 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.Probes.Probe 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.Probes.Probe 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.Probes.Probe in project SeqMonk by s-andrews.
the class PercentileNormalisationQuantitation method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
if (!isReady()) {
progressExceptionReceived(new SeqMonkException("Options weren't set correctly"));
}
Probe[] allProbes = application.dataCollection().probeSet().getAllProbes();
Probe[] calculateProbes = ((ProbeList) calculateFromProbeList.getSelectedItem()).getAllProbes();
float[][] percentileValues = new float[data.length][];
float[] minValues = new float[data.length];
// Work out the value at the appropriate percentile
for (int d = 0; d < data.length; d++) {
// Otherwise we'll calculate for each percentage (0-100)
if (autoPercentileBox.isSelected()) {
percentileValues[d] = new float[101];
} else {
percentileValues[d] = new float[1];
}
progressUpdated("Calculating correction for " + data[d].name(), d, data.length);
float[] theseValues = new float[calculateProbes.length];
for (int p = 0; p < calculateProbes.length; p++) {
try {
theseValues[p] = data[d].getValueForProbe(calculateProbes[p]);
} catch (SeqMonkException e) {
progressExceptionReceived(e);
}
}
Arrays.sort(theseValues);
if (autoPercentileBox.isSelected()) {
for (int i = 0; i <= 100; i++) {
// percentileValues[d][i] = theseValues[(int)((theseValues.length-1)*i)/100];
percentileValues[d][i] = getPercentileValue(theseValues, i);
}
} else {
// percentileValues[d][0] = theseValues[(int)((theseValues.length-1)*percentile)/100];
percentileValues[d][0] = getPercentileValue(theseValues, percentile);
}
minValues[d] = theseValues[0];
}
float[] maxPercentiles = new float[percentileValues[0].length];
for (int i = 0; i < percentileValues.length; i++) {
for (int j = 0; j < maxPercentiles.length; j++) {
if (i == 0 || percentileValues[i][j] > maxPercentiles[j]) {
maxPercentiles[j] = percentileValues[i][j];
}
}
}
for (int d = 0; d < data.length; d++) {
// See if we need to quit
if (cancel) {
progressCancelled();
return;
}
progressUpdated(d, data.length);
// Get the correction value
float[] correctionFactors = new float[percentileValues[0].length];
if (correctionAction == ADD) {
for (int i = 0; i < correctionFactors.length; i++) {
correctionFactors[i] = maxPercentiles[i] - percentileValues[d][i];
}
} else if (correctionAction == MULTIPLY) {
for (int i = 0; i < correctionFactors.length; i++) {
correctionFactors[i] = (maxPercentiles[i] - minValues[d]) / (percentileValues[d][i] - minValues[d]);
}
}
// Now we work out the correction factor we're actually going to use
float correctionFactor = SimpleStats.median(correctionFactors);
// Apply the correction to all probes
try {
for (int p = 0; p < allProbes.length; p++) {
// See if we need to quit
if (cancel) {
progressCancelled();
return;
}
if (correctionAction == ADD) {
data[d].setValueForProbe(allProbes[p], data[d].getValueForProbe(allProbes[p]) + correctionFactor);
} else if (correctionAction == MULTIPLY) {
data[d].setValueForProbe(allProbes[p], minValues[d] + ((data[d].getValueForProbe(allProbes[p]) - minValues[d]) * correctionFactor));
}
}
} catch (SeqMonkException e) {
progressExceptionReceived(e);
}
}
quantitatonComplete();
}
Aggregations