/**
 * Mutinack mutation detection program.
 * Copyright (C) 2014-2016 Olivier Cinquin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package uk.org.cinquin.mutinack.output;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import org.eclipse.jdt.annotation.NonNull;

import uk.org.cinquin.mutinack.DuplexDisagreement;
import uk.org.cinquin.mutinack.Parameters;
import uk.org.cinquin.mutinack.SequenceLocation;
import uk.org.cinquin.mutinack.candidate_sequences.CandidateSequence;
import uk.org.cinquin.mutinack.misc_util.Pair;
import uk.org.cinquin.mutinack.misc_util.Util;
import uk.org.cinquin.mutinack.qualities.Quality;

@PersistenceCapable
public class RunResult implements Serializable {
	private static final long serialVersionUID = -5856926265963435703L;

	public @Persistent @Column(length = 10_000) String mutinackVersion;
	public @Persistent Parameters parameters;
	public @Persistent List<ParedDownMutinack> samples;

	public Stream<Entry<@NonNull SequenceLocation, LocationAnalysis>> extractLocationAnalyses() {
		return samples.stream().
			flatMap(sample -> sample.stats.stream()).
			flatMap(stats -> stats.detections.entrySet().stream());
	}

	public Stream<CandidateSequence> extractDetections() {
		return extractLocationAnalyses().
			flatMap(e -> e.getValue().candidates.stream());
	}

	public Stream<Pair<SequenceLocation, DuplexDisagreement>> extractDisagreements() {
		return extractLocationAnalyses().
			flatMap(e -> e.getValue().disagreements.stream().
				map(dis -> new Pair<>(e.getKey(), dis)));
	}

	public SortedSet<Map.Entry<DuplexDisagreement, Long>> getQ2DisagreementCounts() {
		@SuppressWarnings({ "unchecked", "rawtypes" })
		SortedSet<Map.Entry<DuplexDisagreement, Long>> sortedSet =
			new TreeSet(Util.mapEntryByValueSorter);
		sortedSet.addAll(extractDisagreements().
			filter(pair -> pair.snd.quality.atLeast(Quality.GOOD)).
			collect(Collectors.groupingBy(p -> p.snd, Collectors.counting())).
			entrySet());
		return sortedSet;
	}
}

@@ -786,58 +786,8 @@
 st;%0A
-import java.util.Map;%0Aimport java.util.Map.Entry;%0A
 impo
@@ -1755,21 +1755,20 @@
  Stream%3C
-Entry
+Pair
 %3C@NonNul
@@ -1955,32 +1955,83 @@
 ySet().stream())
+.%0A%09%09%09map(e -%3E new Pair%3C%3E(e.getKey(), e.getValue()))
 ;%0A%09%7D%0A%0A%09public St
@@ -2130,26 +2130,19 @@
 (e -%3E e.
-getValue()
+snd
 .candida
@@ -2180,16 +2180,25 @@
 am%3CPair%3C
+@NonNull 
 Sequence
@@ -2206,16 +2206,25 @@
 ocation,
+ @NonNull
  DuplexD
@@ -2316,26 +2316,19 @@
 (e -%3E e.
-getValue()
+snd
 .disagre
@@ -2347,17 +2347,16 @@
 m().%0A%09%09%09
-%09
 map(dis 
@@ -2371,24 +2371,19 @@
 air%3C%3E(e.
-getKey()
+fst
 , dis)))
@@ -2380,32 +2380,59 @@
 st, dis)));%0A%09%7D%0A%0A
+%09@SuppressWarnings(%22null%22)%0A
 %09public SortedSe
@@ -2429,33 +2429,28 @@
 c SortedSet%3C
-Map.Entry
+Pair
 %3CDuplexDisag
@@ -2558,17 +2558,12 @@
 Set%3C
-Map.Entry
+Pair
 %3CDup
@@ -2648,25 +2648,8 @@
 ;%0A%09%09
-sortedSet.addAll(
 extr
@@ -2810,16 +2810,77 @@
 trySet()
+.stream().%0A%09%09%09map(Pair::fromEntry).%0A%09%09%09forEach(sortedSet::add
 );%0A%09%09ret

Stop using map entries because of the potential problems they can cause.