use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature 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.Feature 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.Feature 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.Feature in project SeqMonk by s-andrews.
the class FeaturePercentileProbeGenerator 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);
Feature[] features = collection.genome().annotationCollection().getFeaturesForType(chromosomes[c], featureType);
for (int f = 0; f < features.length; f++) {
// See if we need to quit
if (cancel) {
generationCancelled();
return;
}
if (useSubfeatures && (features[f].location() instanceof SplitLocation)) {
SplitLocation location = (SplitLocation) features[f].location();
Location[] subLocations = location.subLocations();
for (int s = 0; s < subLocations.length; s++) {
makeProbes(features[f], chromosomes[c], subLocations[s], newProbes);
}
} else {
makeProbes(features[f], chromosomes[c], features[f].location(), newProbes);
}
}
}
Probe[] finalList = newProbes.toArray(new Probe[0]);
ProbeSet finalSet = new ProbeSet(getDescription(), finalList);
generationComplete(finalSet);
}
use of uk.ac.babraham.SeqMonk.DataTypes.Genome.Feature in project SeqMonk by s-andrews.
the class FeatureGroup method getSubLocations.
public Location[] getSubLocations() {
if (features.size() == 1) {
Location loc = features.elementAt(0).location();
if (loc instanceof SplitLocation) {
return ((SplitLocation) loc).subLocations();
} else {
return new Location[] { loc };
}
}
LongVector allLocs = new LongVector();
Enumeration<Feature> en = features.elements();
while (en.hasMoreElements()) {
Location loc = en.nextElement().location();
if (loc instanceof SplitLocation) {
Location[] subLocs = ((SplitLocation) loc).subLocations();
for (int s = 0; s < subLocs.length; s++) {
allLocs.add(subLocs[s].packedPosition());
}
} else {
allLocs.add(loc.packedPosition());
}
}
long[] locs = allLocs.toArray();
SequenceRead.sort(locs);
Vector<Location> mergedLocs = new Vector<Location>();
long current = locs[0];
for (int i = 1; i < locs.length; i++) {
// if (debug) {System.err.println("Looking at "+SequenceRead.start(locs[i])+"-"+SequenceRead.end(locs[i])+" current is "+SequenceRead.start(current)+"-"+SequenceRead.end(current));}
if (SequenceRead.overlaps(current, locs[i]) && SequenceRead.end(locs[i]) > SequenceRead.end(current)) {
// if (debug) {System.err.println("They overlap, extending...");}
current = SequenceRead.packPosition(SequenceRead.start(current), SequenceRead.end(locs[i]), SequenceRead.strand(current));
} else if (SequenceRead.end(locs[i]) <= SequenceRead.end(current)) {
// if (debug) {System.err.println("This is a subset, ignoring it");}
continue;
} else {
// if (debug) {System.err.println("They don't overlap, moving on...");}
mergedLocs.add(new Location(current));
current = locs[i];
}
}
mergedLocs.add(new Location(current));
return mergedLocs.toArray(new Location[0]);
}
Aggregations