use of org.sbolstandard.core2.Range in project metron by apache.
the class WindowProcessorTest method testRepeatWithInclusionExclusion.
@Test
public void testRepeatWithInclusionExclusion() throws ParseException {
Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago including holidays:us excluding weekends");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date now = sdf.parse("2017/12/26 12:00");
List<Range<Long>> intervals = w.toIntervals(now.getTime());
Assert.assertEquals(1, intervals.size());
}
use of org.sbolstandard.core2.Range in project metron by apache.
the class WindowProcessorTest method testRepeatWithWeekdayExclusion.
@Test
public void testRepeatWithWeekdayExclusion() throws ParseException {
Window w = WindowProcessor.process("30 minute window every 24 hours from 7 days ago excluding weekdays");
Date now = new Date();
// avoid DST impacts if near Midnight
now.setHours(6);
List<Range<Long>> intervals = w.toIntervals(now.getTime());
Assert.assertEquals(2, intervals.size());
}
use of org.sbolstandard.core2.Range in project libSBOLj by SynBioDex.
the class SBOLReader method parseSequenceAnnotationV1.
/**
* @param SBOLDoc
* @param sequenceAnnotation
* @param precedePairs
* @param parentURI
* @param sa_num
* @param instantiatedComponents
* @return
* @throws SBOLValidationException if either of the following conditions is satisfied:
* <ul>
* <li>if an SBOL validation rule violation occurred in any of the following constructors or methods:
* <ul>
* <li>{@link URIcompliance#createCompliantURI(String, String, String)}, </li>
* <li>{@link #parseDnaComponentV1(SBOLDocument, IdentifiableDocument)}, </li>
* <li>{@link Range#Range(URI, int, int)}, </li>
* <li>{@link Range#setDisplayId(String)}, </li>
* <li>{@link Range#setVersion(String)}, </li>
* <li>{@link GenericLocation#GenericLocation(URI)}, </li>
* <li>{@link SequenceAnnotation#SequenceAnnotation(URI, Set)}, </li>
* <li>{@link SequenceAnnotation#setDisplayId(String)}, </li>
* <li>{@link SequenceAnnotation#setVersion(String)}, </li>
* <li>{@link SequenceAnnotation#setWasDerivedFrom(URI)}, </li>
* <li>{@link SequenceAnnotation#setComponent(URI)}, or </li>
* <li>{@link SequenceAnnotation#setAnnotations(List)}; or</li>
* </ul>
* </li>
* <li>the following SBOL validation rule was violated: 11002.</li>
* </ul>
* @throws SBOLConversionException
*/
private static SequenceAnnotation parseSequenceAnnotationV1(SBOLDocument SBOLDoc, NestedDocument<QName> sequenceAnnotation, List<SBOLPair> precedePairs, String parentURI, int sa_num, Set<String> instantiatedComponents) throws SBOLValidationException, SBOLConversionException {
Integer start = null;
Integer end = null;
String strand = null;
URI componentURI = null;
URI identity = sequenceAnnotation.getIdentity();
String persIdentity = sequenceAnnotation.getIdentity().toString();
List<Annotation> annotations = new ArrayList<>();
if (URIPrefix != null) {
persIdentity = createCompliantURI(parentURI, "annotation" + sa_num, "").toString();
identity = createCompliantURI(parentURI, "annotation" + sa_num, version);
}
if (!sequenceAnnotation.getType().equals(Sbol1Terms.SequenceAnnotations.SequenceAnnotation)) {
throw new SBOLConversionException("QName has to be" + Sbol1Terms.SequenceAnnotations.SequenceAnnotation.toString());
}
for (NamedProperty<QName> namedProperty : sequenceAnnotation.getProperties()) {
if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.bioStart)) {
if (!(namedProperty.getValue() instanceof Literal) || start != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
throw new SBOLValidationException("sbol-11102", sequenceAnnotation.getIdentity());
}
String temp = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
start = Integer.parseInt(temp);
} else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.bioEnd)) {
if (!(namedProperty.getValue() instanceof Literal) || end != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
throw new SBOLValidationException("sbol-11103", sequenceAnnotation.getIdentity());
}
String temp2 = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
end = Integer.parseInt(temp2);
} else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.strand)) {
if (!(namedProperty.getValue() instanceof Literal) || strand != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
throw new SBOLValidationException("sbol-11002", sequenceAnnotation.getIdentity());
}
strand = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
} else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.subComponent)) {
if (componentURI != null) {
throw new SBOLValidationException("sbol-10904", sequenceAnnotation.getIdentity());
}
if (namedProperty.getValue() instanceof NestedDocument) {
componentURI = parseDnaComponentV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue()).getIdentity();
} else {
if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
throw new SBOLValidationException("sbol-10904", sequenceAnnotation.getIdentity());
}
componentURI = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
}
} else if (namedProperty.getName().equals(Sbol1Terms.SequenceAnnotations.precedes)) {
URI left = sequenceAnnotation.getIdentity();
URI right = null;
if (namedProperty.getValue() instanceof NestedDocument) {
// TODO: need to check if ++sa_num here okay
right = parseSequenceAnnotationV1(SBOLDoc, (NestedDocument<QName>) namedProperty.getValue(), precedePairs, parentURI, ++sa_num, instantiatedComponents).getIdentity();
} else {
if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
throw new SBOLValidationException("sbol-11404", sequenceAnnotation.getIdentity());
}
right = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
}
SBOLPair pair = new SBOLPair(left, right);
precedePairs.add(pair);
} else {
annotations.add(new Annotation(namedProperty));
}
}
String componentDisplayId = URIcompliance.extractDisplayId(componentURI);
String displayId = "annotation" + sa_num;
if (compliant && componentDisplayId != null && !instantiatedComponents.contains(componentDisplayId)) {
identity = createCompliantURI(parentURI, componentDisplayId + "_annotation", version);
persIdentity = createCompliantURI(parentURI, componentDisplayId + "_annotation", "").toString();
displayId = componentDisplayId + "_annotation";
}
// Note: Do not create a seqAnnotation if Location is empty
Location location = null;
if (// create SequenceAnnotation & Component
start != null && end != null) {
URI range_identity = createCompliantURI(persIdentity, "range", version);
location = new Range(range_identity, start, end);
if (!persIdentity.equals("")) {
location.setPersistentIdentity(createCompliantURI(persIdentity, "range", ""));
location.setDisplayId("range");
location.setVersion(version);
}
if (strand != null) {
if (strand.equals("+")) {
location.setOrientation(OrientationType.INLINE);
} else if (strand.equals("-")) {
location.setOrientation(OrientationType.REVERSECOMPLEMENT);
}
}
} else {
URI dummyGenericLoc_id = createCompliantURI(persIdentity, "genericLocation", version);
location = new GenericLocation(dummyGenericLoc_id);
if (!persIdentity.equals("")) {
location.setPersistentIdentity(createCompliantURI(persIdentity, "genericLocation", ""));
location.setDisplayId("genericLocation");
location.setVersion(version);
}
if (strand != null) {
if (strand.equals("+")) {
location.setOrientation(OrientationType.INLINE);
} else if (strand.equals("-")) {
location.setOrientation(OrientationType.REVERSECOMPLEMENT);
}
}
}
Set<Location> locations = new HashSet<>();
locations.add(location);
SequenceAnnotation s = new SequenceAnnotation(identity, locations);
if (!persIdentity.equals("")) {
s.setPersistentIdentity(URI.create(persIdentity));
s.setDisplayId(displayId);
s.setVersion(version);
}
if (identity != sequenceAnnotation.getIdentity())
s.addWasDerivedFrom(sequenceAnnotation.getIdentity());
if (componentURI != null)
s.setComponent(componentURI);
if (!annotations.isEmpty())
s.setAnnotations(annotations);
return s;
}
use of org.sbolstandard.core2.Range in project libSBOLj by SynBioDex.
the class ComponentDefinition method getImpliedNucleicAcidSequence.
/**
* Return the elements of a nucleic acid sequence implied by the hierarchically included components.
* <p>
* This method first tries to obtain the length of a nucleic acid sequence from the set of sequences referenced by this component definition
* that has an {@link Sequence#IUPAC_DNA} encoding. It then iterates through this component defintion's
* sequence annotations, and update the length with the ending locations that have a larger value than the current length. It then populates
* the elements with this length with unknown bases. This method iterates through this component defintion's sequence annotations to recursively
* search for bases implied by the hierarchically included components, and fills the elements with these known bases.
*
* @return the elements of a nucleic sequence implied by the hierarchically included components
*/
public String getImpliedNucleicAcidSequence() {
URI type = null;
if (this.getTypes().contains(ComponentDefinition.DNA)) {
type = ComponentDefinition.DNA;
} else if (this.getTypes().contains(ComponentDefinition.RNA)) {
type = ComponentDefinition.RNA;
} else {
return null;
}
String elements = "";
int length = 0;
if (this.getSequenceByEncoding(Sequence.IUPAC_DNA) != null) {
length = this.getSequenceByEncoding(Sequence.IUPAC_DNA).getElements().length();
}
for (SequenceAnnotation sequenceAnnotation : this.getSequenceAnnotations()) {
for (Location location : sequenceAnnotation.getLocations()) {
if (location instanceof Range) {
Range range = (Range) location;
if (range.getEnd() > length) {
length = range.getEnd();
}
}
}
}
for (int i = 0; i < length; i++) {
elements += "N";
}
char[] elementsArray = elements.toCharArray();
for (SequenceAnnotation sequenceAnnotation : this.getSequenceAnnotations()) {
String subElements = null;
if (!sequenceAnnotation.isSetComponent())
continue;
if (sequenceAnnotation.getComponent().getDefinition() != null) {
ComponentDefinition compDef = sequenceAnnotation.getComponent().getDefinition();
if (compDef.getSequenceByEncoding(Sequence.IUPAC_DNA) != null) {
subElements = compDef.getSequenceByEncoding(Sequence.IUPAC_DNA).getElements();
} else {
subElements = compDef.getImpliedNucleicAcidSequence();
}
for (Location location : sequenceAnnotation.getLocations()) {
if (location instanceof Range) {
Range range = (Range) location;
if (range.isSetOrientation() && range.getOrientation().equals(OrientationType.REVERSECOMPLEMENT)) {
subElements = Sequence.reverseComplement(subElements, type);
}
for (int i = 0; i < subElements.length(); i++) {
// TODO: need to deal with when start index out of range
elementsArray[(range.getStart() + i) - 1] = subElements.charAt(i);
}
}
}
}
}
elements = String.valueOf(elementsArray);
return elements;
}
use of org.sbolstandard.core2.Range in project libSBOLj by SynBioDex.
the class SequenceAnnotation method copy.
void copy(SequenceAnnotation sequenceAnnotation) throws SBOLValidationException {
((Identified) this).copy((Identified) sequenceAnnotation);
for (Location location : sequenceAnnotation.getLocations()) {
String displayId = URIcompliance.findDisplayId(location);
if (location instanceof Range) {
Range range = (Range) location;
Range newRange;
if (range.isSetOrientation()) {
newRange = this.addRange(displayId, range.getStart(), range.getEnd(), range.getOrientation());
} else {
newRange = this.addRange(displayId, range.getStart(), range.getEnd());
}
newRange.copy(range);
} else if (location instanceof Cut) {
Cut cut = (Cut) location;
Cut newCut;
if (cut.isSetOrientation()) {
newCut = this.addCut(displayId, cut.getAt(), cut.getOrientation());
} else {
newCut = this.addCut(displayId, cut.getAt());
}
newCut.copy(cut);
} else if (location instanceof GenericLocation) {
GenericLocation genericLocation = (GenericLocation) location;
GenericLocation newGenericLocation;
if (genericLocation.isSetOrientation()) {
newGenericLocation = this.addGenericLocation(displayId, genericLocation.getOrientation());
} else {
newGenericLocation = this.addGenericLocation(displayId);
}
newGenericLocation.copy(genericLocation);
}
}
Location location = this.getLocation("DUMMY__LOCATION");
if (location != null) {
this.removeLocation(location);
}
if (sequenceAnnotation.isSetComponent()) {
String componentDisplayId = URIcompliance.findDisplayId(sequenceAnnotation.getComponent());
this.setComponent(componentDisplayId);
}
this.roles = new HashSet<>();
for (URI role : sequenceAnnotation.getRoles()) {
this.addRole(URI.create(role.toString()));
}
}
Aggregations