use of java.util.function.BinaryOperator in project jvarkit by lindenb.
the class CombineVcfFisher method doWork.
@Override
public int doWork(final List<String> args) {
try {
this.pedigree = new PedigreeParser().parse(this.pedigreeFile);
final Map<Path, Optional<GeneGenotypes>> path2genegenotype = new HashMap<>(50_000);
final Function<Path, GeneGenotypes> geneMapper = PATH -> {
if (this.do_buffer_vcf && path2genegenotype.containsKey(PATH)) {
return path2genegenotype.get(PATH).orElse(null);
}
final GeneGenotypes value;
try {
value = loadGeneGenotypes(PATH);
} catch (final IOException err) {
throw new RuntimeIOException(err);
}
if (this.do_buffer_vcf) {
path2genegenotype.put(PATH, Optional.ofNullable(value));
}
return value;
};
final BinaryOperator<GeneGenotypes> merger = (A, B) -> {
final GeneGenotypes ggt = new GeneGenotypes(A.geneid + ":" + B.geneid);
ggt.genotypes.or(A.genotypes);
ggt.genotypes.or(B.genotypes);
ggt.nVariants = A.nVariants + B.nVariants;
return ggt;
};
final List<Path> geneList = new ArrayList<>();
final String input = oneAndOnlyOneFile(args);
try (PrintWriter pw = super.openPathOrStdoutAsPrintWriter(this.outputFile)) {
pw.println("#genes\tn-variants\tcases-R\tcases-A\tcontrols-R\tcontrols-A\tfisher");
try (BufferedReader br = super.openBufferedReader(input)) {
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("#") || StringUtils.isBlank(line))
continue;
final String[] tokens = CharSplitter.TAB.split(line);
if (!use_line_mode) {
if (tokens.length != 1) {
LOG.error("inconsistent number of column in " + line + " exepcted one column but got " + tokens.length);
return -1;
}
geneList.add(Paths.get(tokens[0]));
} else // line mode
{
final GeneGenotypes ggt = Arrays.stream(tokens).map(S -> Paths.get(S)).map(geneMapper).filter(X -> X != null).reduce(merger).orElse(null);
if (ggt != null)
runFisher(pw, ggt);
}
}
}
if (!use_line_mode) {
Collections.sort(geneList, (A, B) -> A.getFileName().compareTo(B.getFileName()));
switch(recursion) {
case 1:
{
for (int x = 0; x < geneList.size(); x++) {
final GeneGenotypes ggtx = geneMapper.apply(geneList.get(x));
if (ggtx == null)
continue;
runFisher(pw, ggtx);
}
break;
}
case 2:
{
for (int x = 0; x + 1 < geneList.size(); x++) {
final GeneGenotypes ggtx = geneMapper.apply(geneList.get(x));
if (ggtx == null)
continue;
for (int y = x + 1; y < geneList.size(); y++) {
final GeneGenotypes ggty = geneMapper.apply(geneList.get(y));
if (ggty == null)
continue;
runFisher(pw, merger.apply(ggtx, ggty));
}
}
break;
}
case 3:
{
for (int x = 0; x + 2 < geneList.size(); x++) {
final GeneGenotypes ggtx = geneMapper.apply(geneList.get(x));
if (ggtx == null)
continue;
for (int y = x + 1; y + 1 < geneList.size(); y++) {
final GeneGenotypes ggty = geneMapper.apply(geneList.get(y));
if (ggty == null)
continue;
final GeneGenotypes ggtxy = merger.apply(ggtx, ggty);
for (int z = y + 1; z < geneList.size(); z++) {
final GeneGenotypes ggtz = geneMapper.apply(geneList.get(z));
if (ggtz == null)
continue;
runFisher(pw, merger.apply(ggtxy, ggtz));
}
}
}
break;
}
default:
{
LOG.error("Cannot compute recursion>3 or <1");
return -1;
}
}
}
pw.flush();
}
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
}
}
use of java.util.function.BinaryOperator in project parent by Daytime-Don-t-Know-Dark-Night.
the class StreamReduce1 method main.
public static void main(String[] args) {
/**
* @param a 表达式执行结果的缓存
* @param b stream中的每一个元素
*/
Integer sum1 = Stream.of(1, 2, 3, 4, 5).reduce(new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer a, Integer b) {
System.out.printf("a = %d, b = %d %n", a, b);
return a + b;
}
}).get();
Integer sum2 = Stream.of(1, 2, 3, 4, 5).reduce(0, (a, b) -> a + b);
// 前两种计算结果的类型必须和stream中的元素类型相同,
/**
* @param 初始值, 初始值为什么类型, 最后返回的结果就是什么类型
* @param a, a的类型与初始值及返回结果一致
* @param b, 中间量的类型, 也就是stream中元素的类型
* @param expression
*/
ArrayList<String> sum3 = Stream.of(1, 2, 3, 4, 5).reduce(new ArrayList<String>(), (ArrayList<String> a, Integer b) -> {
a.add("*" + b);
return a;
}, (a, b) -> b);
HttpClientContext httpContext = HttpClientContext.create();
String url = "http://www.boluo.com";
String body = "&type=1";
Document d1 = historyRequest(httpContext, url, body);
Stream<CompletableFuture<Document>> d2 = Stream.iterate(2, i -> i + 1).limit(20).map(i -> CompletableFuture.supplyAsync(() -> historyRequest(httpContext, url, body + "&page=" + i)));
/* 为保证线程start之后全部执行完成再往下执行, 可以先collect全部join
Stream<Document> s2 = d2.collect(Collectors.toList()).stream().map(CompletableFuture::join);
Stream.concat(Stream.of(d1), s2).flatMap(tr -> {
Elements head = tr.select("");
return head.stream().map(i -> {
return i.text();
});
}); */
// 关键在于 CompletableFuture<Document> -> Stream<Document>
CompletableFuture<Stream<Document>> d3 = d2.reduce(CompletableFuture.completedFuture(Stream.of()), (CompletableFuture<Stream<Document>> a, CompletableFuture<Document> b) -> {
CompletableFuture<Stream<Document>> res = a.thenCombine(b, (ra, rb) -> {
return Stream.concat(ra, Stream.of(rb));
});
return res;
}, (a, b) -> b);
// 此时:
Stream<Document> pageContext = Stream.concat(Stream.of(d1), d3.join());
// *************************************************************************************************************
/* 另一种方式
Stream<CompletableFuture<Document>> s2 = Stream.iterate(2, i -> i + 1)
.limit(20)
.map(i -> CompletableFuture.supplyAsync(() -> {
return historyRequest(httpContext, url, body + "&page=" + i);
})); */
Stream<CompletableFuture<Stream<Document>>> s2 = Stream.iterate(2, i -> i + 1).limit(20).map(i -> CompletableFuture.supplyAsync(() -> {
Document doc = historyRequest(httpContext, url, body + "&page=" + i);
return Stream.of(doc);
}));
// 这里可以直接用第二种签名, 此时a, b类型一致, 为: CompletableFuture<Stream<Document>>
CompletableFuture<Stream<Document>> s3 = s2.reduce(CompletableFuture.completedFuture(Stream.of()), (CompletableFuture<Stream<Document>> a, CompletableFuture<Stream<Document>> b) -> {
CompletableFuture<Stream<Document>> res = a.thenCombine(b, (ra, rb) -> {
return Stream.concat(ra, rb);
});
return res;
});
Stream.concat(Stream.of(d1), s3.join());
}
use of java.util.function.BinaryOperator in project sw360portal by sw360.
the class XhtmlGeneratorTest method findLicenses.
private String findLicenses(Document document, String releaseNameString) {
List list = document.selectNodes("//*[local-name()='li'][@id='" + releaseNameString + "']/*[local-name()='ul'][@class='licenseEntries']");
StringBuffer result = new StringBuffer();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Element element = (Element) iter.next();
for (Object liObject : element.content()) {
Element liElement = (Element) liObject;
String licenseEntryId = liElement.attribute("id").getValue();
String licenseTextId = licenseEntryId.replace("licenseEntry", "licenseText");
List licenseTexts = document.selectNodes("//*[local-name()='pre'][@id='" + licenseTextId + "']/text()");
Object licenseText = licenseTexts.stream().map(l -> ((Text) l).getStringValue()).reduce("", (BinaryOperator<String>) (l1, l2) -> (String) (l1 + l2));
result.append(((String) licenseText).trim());
result.append("\n");
}
}
return result.toString();
}
use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.
the class RandomCutForest method getDynamicSimulatedScore.
/**
* Similar to above but now the scoring takes in a function of Bounding Box to
* probabilities (vector over the dimensions); and produces a score af-if the
* tree were built using that function (when in reality the tree is an RCF).
* Changing the defaultRCFgVec function to some other function f() will provide
* a mechanism of dynamic scoring for trees that are built using f() which is
* the purpose of TransductiveScalarScore visitor. Note that the answer is an
* MCMC simulation and is not normalized (because the scoring functions are
* flexible and unknown) and over a small number of trees the errors can be
* large specially if vecSep is very far from defaultRCFgVec
*
* Given the large number of possible sources of distortion, ignoreLeafThreshold
* is not supported.
*
* @param point point to be scored
* @param seen the score function for seen point
* @param unseen score function for unseen points
* @param damp dampening the score for duplicates
* @param vecSep the function of (BoundingBox) -> array of probabilities
* @return the simuated score
*/
public double getDynamicSimulatedScore(float[] point, BiFunction<Double, Double, Double> seen, BiFunction<Double, Double, Double> unseen, BiFunction<Double, Double, Double> damp, Function<IBoundingBoxView, double[]> vecSep) {
if (!isOutputReady()) {
return 0.0;
}
VisitorFactory<Double> visitorFactory = new VisitorFactory<>((tree, y) -> new SimulatedTransductiveScalarScoreVisitor(tree.projectToTree(y), tree.getMass(), seen, unseen, damp, CommonUtils::defaultRCFgVecFunction, vecSep));
BinaryOperator<Double> accumulator = Double::sum;
Function<Double, Double> finisher = sum -> sum / numberOfTrees;
return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
use of java.util.function.BinaryOperator in project random-cut-forest-by-aws by aws.
the class RandomCutForest method getDynamicScore.
/**
* Score a point using the given scoring functions.
*
* @param point input point being scored
* @param ignoreLeafMassThreshold said threshold
* @param seen the function that applies if input is equal to
* a previously seen sample in a leaf
* @param unseen if the input does not have a match in the
* leaves
* @param damp damping function based on the duplicity of the
* previously seen samples
* @return anomaly score
*/
public double getDynamicScore(float[] point, int ignoreLeafMassThreshold, BiFunction<Double, Double, Double> seen, BiFunction<Double, Double, Double> unseen, BiFunction<Double, Double, Double> damp) {
checkArgument(ignoreLeafMassThreshold >= 0, "ignoreLeafMassThreshold should be greater than or equal to 0");
if (!isOutputReady()) {
return 0.0;
}
VisitorFactory<Double> visitorFactory = new VisitorFactory<>((tree, y) -> new DynamicScoreVisitor(tree.projectToTree(y), tree.getMass(), ignoreLeafMassThreshold, seen, unseen, damp));
BinaryOperator<Double> accumulator = Double::sum;
Function<Double, Double> finisher = sum -> sum / numberOfTrees;
return traverseForest(transformToShingledPoint(point), visitorFactory, accumulator, finisher);
}
Aggregations