package org.unipept.tools.commandline;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;

/**
 * This script calculates all lowest common ancestors
 *
 * @author Bart Mesuere
 *
 */
public class LCACalculator {

    private static int[][] taxonomy;

    public static void buildTaxonomy(String file) throws FileNotFoundException {
        HashMap<Integer, int[]> taxonomyMap = new HashMap<>();
        InputStream is = new FileInputStream(new File(file));
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        br.lines()
                .skip(1)
                .forEach((line) -> {
                    String[] elements = line.split(",");

                    int key = Integer.parseInt(elements[0]);
                    int[] lineage = Arrays.stream(elements)
                            .mapToInt((s) -> s.equals("NULL") ? 0 : Integer.parseInt(s))
                            .toArray();

                    taxonomyMap.put(key, lineage);
                });

        int max = taxonomyMap.keySet().stream().mapToInt(x -> x).max().getAsInt();
        taxonomy = new int[max + 1][];
        taxonomyMap.keySet().stream().forEach(key -> taxonomy[key] = taxonomyMap.get(key));
    }
}

@@ -67,19 +67,21 @@
 ava.
-util.Arrays
+sql.Timestamp
 ;%0Aim
@@ -95,23 +95,17 @@
 va.util.
-HashMap
+*
 ;%0A%0A/**%0A 
@@ -614,14 +614,12 @@
 ach(
-(
 line
-)
  -%3E 
@@ -664,22 +664,28 @@
 s = 
-line
+SEPARATOR
 .split(
-%22,%22
+line
 );%0A%0A
@@ -843,19 +843,17 @@
 apToInt(
-(s)
+s
  -%3E s.eq
@@ -1059,36 +1059,30 @@
 ).ma
-pToInt(x -%3E x).max().getAsIn
+x(Integer::compare).ge
 t();
@@ -1218,11 +1218,1853 @@
 );%0A    %7D
+%0A%0A    public static void calculateLCAs(String file) throws FileNotFoundException %7B%0A        Scanner sc = new Scanner(new File(file));%0A        sc.nextLine(); // skip header%0A%0A        int count = 0;%0A%0A        int currentSequence = -1;%0A        Collection%3CInteger%3E taxa = new ArrayList%3C%3E();%0A        while (sc.hasNext()) %7B%0A            count++;%0A            if (count %25 1000000 == 0) %7B%0A                System.err.println(new Timestamp(System.currentTimeMillis()) + %22: %22 + count);%0A            %7D%0A            String%5B%5D split = SEPARATOR.split(sc.nextLine());%0A            int sequenceId = Integer.parseInt(split%5B0%5D);%0A            int taxonId = Integer.parseInt(split%5B1%5D);%0A%0A            if (sequenceId != currentSequence) %7B%0A                if (currentSequence != -1) %7B%0A                    int lca = 1;%0A                    int%5B%5D%5B%5D lineages = taxa.stream()%0A                            .map(t -%3E taxonomy%5Bt%5D)%0A                            .filter(l -%3E l != null)%0A                            .toArray(int%5B%5D%5B%5D::new);%0A                    for (int rank = 0; rank %3C lineages%5B0%5D.length; rank++) %7B%0A                        final int finalRank = rank;%0A                        int%5B%5D current = Arrays.stream(lineages)%0A                                .mapToInt(l -%3E l%5BfinalRank%5D)%0A                                .filter(i -%3E i %3E 0)%0A                                .toArray();%0A                        if (current.length %3E 1) %7B%0A                            break;%0A                        %7D%0A                        if (current.length %3E 0 && current%5B0%5D != 0) %7B%0A                            lca = current%5B0%5D;%0A                        %7D%0A                    %7D%0A                    //System.out.println(currentSequence + %22,%22 + lca);%0A                %7D%0A%0A                currentSequence = sequenceId;%0A                taxa.clear();%0A            %7D%0A%0A            taxa.add(taxonId);%0A        %7D%0A    %7D
 %0A%7D%0A

calculate LCA