From 8040f53a1e176a874d944f8c7688f1f28a22c089 Mon Sep 17 00:00:00 2001 From: Fengchao Date: Tue, 30 Jan 2018 15:45:14 +0800 Subject: [PATCH] Considering overlapped theoretical fragment ion multiple times. This is what Comet and Xolik do. --- pom.xml | 5 +++ .../proteomics/Types/SparseBooleanVector.java | 38 +++++-------------- .../Types/SparseBooleanVectorTest.java | 30 ++------------- 3 files changed, 19 insertions(+), 54 deletions(-) diff --git a/pom.xml b/pom.xml index a91e7ea..363fa12 100644 --- a/pom.xml +++ b/pom.xml @@ -66,6 +66,11 @@ commons-math3 3.6.1 + + com.google.guava + guava + 23.6-jre + diff --git a/src/main/java/proteomics/Types/SparseBooleanVector.java b/src/main/java/proteomics/Types/SparseBooleanVector.java index b8cf7ff..0485551 100644 --- a/src/main/java/proteomics/Types/SparseBooleanVector.java +++ b/src/main/java/proteomics/Types/SparseBooleanVector.java @@ -1,14 +1,15 @@ package proteomics.Types; -import java.util.HashSet; -import java.util.Map; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; + import java.util.Set; public class SparseBooleanVector { - private Set sparse_vector = new HashSet<>(); + private Multiset sparse_vector = HashMultiset.create(); - SparseBooleanVector(Set sparse_vector) { + SparseBooleanVector(Multiset sparse_vector) { this.sparse_vector = sparse_vector; } @@ -18,43 +19,24 @@ public void put(int idx) { sparse_vector.add(idx); } - double norm2square() { - return sparse_vector.size(); - } - - public double dot(SparseVector other) { // caution: it will change the original Sparse Boolean Vector. + public double dot(SparseVector other) { double output = 0; - sparse_vector.retainAll(other.getIdxSet()); - for (int i : sparse_vector) { + for (Integer i : sparse_vector) { output += other.get(i); } return output; } - double dot(SparseBooleanVector other) { - Set intersectedKeys = new HashSet<>(sparse_vector); - intersectedKeys.retainAll(other.sparse_vector); - return intersectedKeys.size(); - } - - public int size() { - return sparse_vector.size(); - } - - public boolean contains(int v) { - return sparse_vector.contains(v); - } - public Set getIdxSet() { - return sparse_vector; + return sparse_vector.elementSet(); } public boolean equals(Object other) { if (other instanceof SparseBooleanVector) { SparseBooleanVector temp = (SparseBooleanVector) other; - if (temp.size() == sparse_vector.size()) { + if (temp.sparse_vector.size() == sparse_vector.size()) { for (int v : sparse_vector) { - if (!temp.contains(v)) { + if (!temp.sparse_vector.contains(v)) { return false; } } diff --git a/src/test/java/proteomics/Types/SparseBooleanVectorTest.java b/src/test/java/proteomics/Types/SparseBooleanVectorTest.java index b551a50..bdc0dbc 100644 --- a/src/test/java/proteomics/Types/SparseBooleanVectorTest.java +++ b/src/test/java/proteomics/Types/SparseBooleanVectorTest.java @@ -1,5 +1,7 @@ package proteomics.Types; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; import org.junit.Before; import org.junit.Test; @@ -14,7 +16,7 @@ public class SparseBooleanVectorTest { @Before public void setUp() throws Exception { - Set temp = new HashSet<>(); + Multiset temp = HashMultiset.create(); temp.add(1); temp.add(3); temp.add(23); @@ -23,7 +25,7 @@ public void setUp() throws Exception { @Test public void put() throws Exception { - Set temp = new HashSet<>(); + Multiset temp = HashMultiset.create(); temp.add(1); temp.add(3); temp.add(23); @@ -34,11 +36,6 @@ public void put() throws Exception { assertEquals(ground_truth, vector); } - @Test - public void norm2square() throws Exception { - assertEquals(3, vector.norm2square(), 0); - } - @Test public void dot() throws Exception { SparseVector other = new SparseVector(); @@ -47,25 +44,6 @@ public void dot() throws Exception { assertEquals(1.5f, vector.dot(other), 0); } - @Test - public void dot1() throws Exception { - SparseBooleanVector other = new SparseBooleanVector(); - other.put(1); - other.put(2); - assertEquals(1, vector.dot(other), 0); - } - - @Test - public void size() throws Exception { - assertEquals(3, vector.size()); - } - - @Test - public void contains() throws Exception { - assertTrue(vector.contains(1)); - assertFalse(vector.contains(2)); - } - @Test public void equals() throws Exception { SparseBooleanVector other_1 = new SparseBooleanVector();