Example usage for com.google.common.collect Range closed

List of usage examples for com.google.common.collect Range closed

Introduction

In this page you can find the example usage for com.google.common.collect Range closed.

Prototype

public static <C extends Comparable<?>> Range<C> closed(C lower, C upper) 

Source Link

Document

Returns a range that contains all values greater than or equal to lower and less than or equal to upper .

Usage

From source file:net.sf.mzmine.modules.peaklistmethods.msms.msmsscore.MSMSScoreCalculator.java

/**
 * Returns a calculated similarity score of
 *///from  w  ww.  j  a  v a  2  s . com
public static MSMSScore evaluateMSMS(IMolecularFormula parentFormula, Scan msmsScan, ParameterSet parameters) {

    MZTolerance msmsTolerance = parameters.getParameter(MSMSScoreParameters.msmsTolerance).getValue();
    String massListName = parameters.getParameter(MSMSScoreParameters.massList).getValue();

    MassList massList = msmsScan.getMassList(massListName);

    if (massList == null) {
        throw new IllegalArgumentException("Scan #" + msmsScan.getScanNumber()
                + " does not have a mass list called '" + massListName + "'");
    }

    DataPoint msmsIons[] = massList.getDataPoints();

    if (msmsIons == null) {
        throw new IllegalArgumentException(
                "Mass list " + massList + " does not contain data for scan #" + msmsScan.getScanNumber());
    }

    MolecularFormulaRange msmsElementRange = new MolecularFormulaRange();
    for (IIsotope isotope : parentFormula.isotopes()) {
        msmsElementRange.addIsotope(isotope, 0, parentFormula.getIsotopeCount(isotope));
    }

    int totalMSMSpeaks = 0, interpretedMSMSpeaks = 0;
    Map<DataPoint, String> msmsAnnotations = new Hashtable<DataPoint, String>();

    msmsCycle: for (DataPoint dp : msmsIons) {

        // Check if this is an isotope
        Range<Double> isotopeCheckRange = Range.closed(dp.getMZ() - 1.4, dp.getMZ() - 0.6);
        for (DataPoint dpCheck : msmsIons) {
            // If we have any MS/MS peak with 1 neutron mass smaller m/z
            // and higher intensity, it means the current peak is an
            // isotope and we should ignore it
            if (isotopeCheckRange.contains(dpCheck.getMZ()) && (dpCheck.getIntensity() > dp.getIntensity())) {
                continue msmsCycle;
            }
        }

        // If getPrecursorCharge() returns 0, it means charge is unknown. In
        // that case let's assume charge 1
        int precursorCharge = msmsScan.getPrecursorCharge();
        if (precursorCharge == 0)
            precursorCharge = 1;

        // We don't know the charge of the fragment, so we will simply
        // assume 1
        double neutralLoss = msmsScan.getPrecursorMZ() * precursorCharge - dp.getMZ();

        // Ignore negative neutral losses and parent ion, <5 may be a
        // good threshold
        if (neutralLoss < 5) {
            continue;
        }

        Range<Double> msmsTargetRange = msmsTolerance.getToleranceRange(neutralLoss);

        IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

        MolecularFormulaGenerator msmsEngine;
        try {
            msmsEngine = new MolecularFormulaGenerator(builder, msmsTargetRange.lowerEndpoint(),
                    msmsTargetRange.upperEndpoint(), msmsElementRange);
        } catch (CDKException e) {
            e.printStackTrace();
            return null;
        }

        IMolecularFormula formula = msmsEngine.getNextFormula();
        if (formula != null) {
            String formulaString = MolecularFormulaManipulator.getString(formula);
            msmsAnnotations.put(dp, formulaString);
            interpretedMSMSpeaks++;
        }

        totalMSMSpeaks++;

    }

    // If we did not evaluate any MS/MS peaks, we cannot calculate a score
    if (totalMSMSpeaks == 0) {
        return null;
    }

    double msmsScore = (double) interpretedMSMSpeaks / totalMSMSpeaks;

    MSMSScore result = new MSMSScore(msmsScore, msmsAnnotations);

    return result;

}

From source file:com.ethlo.geodata.repository.GeoRepository.java

public Map.Entry<Long, Range<Long>> mapIpRange(Map<String, Object> rs) {
    final Long id = MapUtils.getLong(rs, "geoname_id");
    final long lower = MapUtils.getLong(rs, "first");
    final long upper = MapUtils.getLong(rs, "last");
    return new AbstractMap.SimpleEntry<>(id, Range.closed(lower, upper));
}

From source file:io.github.mzmine.parameters.parametertypes.ranges.IntegerRangeEditor.java

@Override
public Range<Integer> getValue() {
    String minValueS = minTxtField.getText();
    String maxValueS = maxTxtField.getText();
    try {//from  w  w  w. j a  va  2 s  .  co  m
        Integer minValue = Integer.parseInt(minValueS);
        Integer maxValue = Integer.parseInt(maxValueS);
        return Range.closed(minValue, maxValue);
    } catch (NumberFormatException e) {
        return null;
    }
}

From source file:io.github.msdk.util.MZTolerance.java

/**
 * <p>/*from   w  w  w  . j a v  a  2 s.c  o m*/
 * getToleranceRange.
 * </p>
 *
 * @param mzValue
 *            a {@link java.lang.Double} object.
 * @return a {@link com.google.common.collect.Range} object.
 */
@SuppressWarnings("null")
public @Nonnull Range<Double> getToleranceRange(final @Nonnull Double mzValue) {
    final @Nonnull Double absoluteTolerance = Math.max(mzTolerance, mzValue / MILLION * ppmTolerance);
    return Range.closed(mzValue - absoluteTolerance, mzValue + absoluteTolerance);
}

From source file:org.opendaylight.controller.cluster.datastore.persisted.FrontendClientMetadata.java

public static FrontendClientMetadata readFrom(final DataInput in) throws IOException, ClassNotFoundException {
    final ClientIdentifier id = ClientIdentifier.readFrom(in);

    final int purgedSize = in.readInt();
    final Builder<UnsignedLong> b = ImmutableRangeSet.builder();
    for (int i = 0; i < purgedSize; ++i) {
        final byte header = WritableObjects.readLongHeader(in);
        final UnsignedLong lower = UnsignedLong.fromLongBits(WritableObjects.readFirstLong(in, header));
        final UnsignedLong upper = UnsignedLong.fromLongBits(WritableObjects.readSecondLong(in, header));

        b.add(Range.closed(lower, upper));
    }/*w w w.j a va2s .c om*/

    final int currentSize = in.readInt();
    final Collection<FrontendHistoryMetadata> currentHistories = new ArrayList<>(currentSize);
    for (int i = 0; i < currentSize; ++i) {
        currentHistories.add(FrontendHistoryMetadata.readFrom(in));
    }

    return new FrontendClientMetadata(id, b.build(), currentHistories);
}

From source file:dollar.api.types.DollarVoid.java

@NotNull
@Override/*from   w  w  w .java 2 s  . com*/
public Value $as(@NotNull Type type) {
    if (type.is(Type._BOOLEAN)) {
        return DollarStatic.$(false);
    } else if (type.is(Type._STRING)) {
        return DollarStatic.$("");
    } else if (type.is(Type._LIST)) {
        return DollarStatic.$(Collections.emptyList());
    } else if (type.is(Type._MAP)) {
        return DollarStatic.$("value", this);
    } else if (type.is(Type._DECIMAL)) {
        return DollarStatic.$(0.0d);
    } else if (type.is(Type._INTEGER)) {
        return DollarStatic.$(0);
    } else if (type.is(Type._VOID)) {
        return this;
    } else if (type.is(Type._RANGE)) {
        return DollarFactory.fromValue(Range.closed($get(DollarStatic.$(0)), $get(DollarStatic.$(0))));
    } else {
        return DollarFactory.failure(ErrorType.INVALID_CAST, type.toString(), false);
    }
}

From source file:io.github.mzmine.parameters.parametertypes.ranges.MZRangeEditor.java

@Override
public Range<Double> getValue() {
    String minValueS = minTxtField.getText();
    String maxValueS = maxTxtField.getText();
    try {//from   w w w . j  a v  a2 s  .  c o m
        double minValue = Double.parseDouble(minValueS);
        double maxValue = Double.parseDouble(maxValueS);
        return Range.closed(minValue, maxValue);
    } catch (NumberFormatException e) {
        return null;
    }
}

From source file:org.apache.aurora.scheduler.base.Numbers.java

/**
 * Convert between range types./* ww  w . ja  v a  2  s.c o  m*/
 *
 * @param range Range to convert.
 * @return A closed range from the first to last of {@code range}.
 */
public static Range<Integer> toRange(IRange range) {
    return Range.closed(range.getFirst(), range.getLast());
}

From source file:org.apache.kylin.storage.cache.TsConditionExtractor.java

private static Range<Long> extractTsConditionInternal(TupleFilter filter, TblColRef colRef) {
    if (filter == null) {
        return Range.all();
    }//from   w  w  w  .  j  a  v a 2 s  . c  om

    if (filter instanceof LogicalTupleFilter) {
        if (filter.getOperator() == TupleFilter.FilterOperatorEnum.AND) {
            Range<Long> ret = Range.all();
            for (TupleFilter child : filter.getChildren()) {
                Range childRange = extractTsConditionInternal(child, colRef);
                if (childRange != null) {
                    if (ret.isConnected(childRange) && !ret.intersection(childRange).isEmpty()) {
                        ret = ret.intersection(childRange);
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            }
            return ret.isEmpty() ? null : ret;
        } else {
            //for conditions like date > DATE'2000-11-11' OR date < DATE '1999-01-01'
            //we will use Ranges.all() rather than two ranges to represent them
            return Range.all();
        }
    }

    if (filter instanceof CompareTupleFilter) {
        CompareTupleFilter compareTupleFilter = (CompareTupleFilter) filter;
        if (compareTupleFilter.getColumn() == null)// column will be null at filters like " 1<>1"
            return Range.all();

        if (compareTupleFilter.getColumn().equals(colRef)) {
            Object firstValue = compareTupleFilter.getFirstValue();
            long t;
            switch (compareTupleFilter.getOperator()) {
            case EQ:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.closed(t, t);
            case LT:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.lessThan(t);
            case LTE:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.atMost(t);
            case GT:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.greaterThan(t);
            case GTE:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.atLeast(t);
            case NEQ:
            case IN://not handled for now
                break;
            default:
            }
        }
    }
    return Range.all();
}

From source file:org.apache.james.imap.api.message.UidRange.java

private static Range<MessageUid> mergeRanges(Range<MessageUid> range, Range<MessageUid> previous) {
    return Range.closed(previous.lowerEndpoint(), range.upperEndpoint());
}