[med-svn] [Git][med-team/jebl2][upstream] 2 commits: New upstream version 0.1+git20190308

Pierre Gruet gitlab at salsa.debian.org
Fri Apr 24 16:46:11 BST 2020



Pierre Gruet pushed to branch upstream at Debian Med / jebl2


Commits:
d7039deb by Pierre Gruet at 2020-04-23T21:57:42+02:00
New upstream version 0.1+git20190308
- - - - -
a8569cac by Pierre Gruet at 2020-04-23T22:38:54+02:00
New upstream version 0.1+git20190308.a98448e
- - - - -


11 changed files:

- build.xml
- src/jebl/evolution/io/NexusExporter.java
- src/jebl/evolution/sequences/AminoAcidState.java
- src/jebl/evolution/sequences/BasicSequence.java
- src/jebl/evolution/sequences/CodonState.java
- src/jebl/evolution/sequences/Codons.java
- src/jebl/evolution/sequences/NucleotideState.java
- src/jebl/evolution/sequences/Nucleotides.java
- src/jebl/evolution/sequences/Sequence.java
- src/jebl/evolution/sequences/State.java
- src/jebl/evolution/sequences/Utils.java


Changes:

=====================================
build.xml
=====================================
@@ -30,7 +30,7 @@
 
     <target name="compile-jebl" depends="init">
         <!-- Compile the java code from ${src} into ${build} -->
-        <javac source="1.6" srcdir="${src}" destdir="${build}" debug="true" target="1.6">
+        <javac source="1.8" srcdir="${src}" destdir="${build}" debug="true" target="1.8">
             <include name="jebl/**/*"/>
         </javac>
         <copy todir="${build}">


=====================================
src/jebl/evolution/io/NexusExporter.java
=====================================
@@ -317,6 +317,15 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
      * @param builder
      */
     private void appendTree(RootedTree tree, Node node, StringBuilder builder) {
+
+        Node p = tree.getParent(node);
+        while (p != null) {
+            if (p == node) {
+                System.err.println("Circular reference in tree structure");
+            }
+            p = tree.getParent(p);
+        }
+
         if (tree.isExternal(node)) {
             appendTaxonName(tree.getTaxon(node), builder);
 


=====================================
src/jebl/evolution/sequences/AminoAcidState.java
=====================================
@@ -51,4 +51,6 @@ public final class AminoAcidState extends State {
 	public boolean isStop() {
 		return this == AminoAcids.STOP_STATE;
 	}
+
+    public SequenceType getType() { return SequenceType.AMINO_ACID; }
 }


=====================================
src/jebl/evolution/sequences/BasicSequence.java
=====================================
@@ -111,7 +111,11 @@ public class BasicSequence implements Sequence {
      * @return an array of state objects.
      */
     public State[] getStates() {
-        return sequenceType.toStateArray(getStateIndices());
+        if (sequenceType == SequenceType.CODON) {
+            return sequenceType.toStateArray(getString());
+        } else {
+            return sequenceType.toStateArray(getStateIndices());
+        }
     }
 
     public byte[] getStateIndices() {


=====================================
src/jebl/evolution/sequences/CodonState.java
=====================================
@@ -29,4 +29,6 @@ public final class CodonState extends State {
     public boolean isGap() {
 		return this == Codons.GAP_STATE;
 	}
+
+    public SequenceType getType() { return SequenceType.CODON; }
 }


=====================================
src/jebl/evolution/sequences/Codons.java
=====================================
@@ -89,7 +89,17 @@ public final class Codons {
 
 	public static boolean isGap(CodonState state) { return state == GAP_STATE; }
 
-	public static NucleotideState[] toNucleotides(CodonState state) {
+    public static NucleotideState[] toNucleotides(State[] states) {
+        NucleotideState[] nucs = new NucleotideState[states.length * 3];
+        int i = 0;
+        for (State state : states) {
+            System.arraycopy(toNucleotides((CodonState)state), 0, nucs, i, 3);
+            i += 3;
+        }
+        return nucs;
+    }
+
+    public static NucleotideState[] toNucleotides(CodonState state) {
 		NucleotideState[] nucs = new NucleotideState[3];
 		String code = state.getCode();
 		nucs[0] = Nucleotides.getState(code.charAt(0));


=====================================
src/jebl/evolution/sequences/NucleotideState.java
=====================================
@@ -35,6 +35,6 @@ public final class NucleotideState extends State {
 		return this == Nucleotides.GAP_STATE;
 	}
 
-
+    public SequenceType getType() { return SequenceType.NUCLEOTIDE; }
 
 }


=====================================
src/jebl/evolution/sequences/Nucleotides.java
=====================================
@@ -259,6 +259,39 @@ public final class Nucleotides {
 	    return seq;
 	}
 
+    /**
+     * Convert an array of nucleotide states into an array of codon states
+     * @param states the nucleotide states
+     * @param readingFrame the reading frame (1 to 3)
+     * @return the codon states
+     */
+    public static CodonState[] toCodons(final State[] states, int readingFrame) {
+        if (states == null) throw new NullPointerException("States array is null");
+        if (states.length == 0) return new CodonState[0];
+
+        if (readingFrame < 1 || readingFrame > 3) {
+            throw new IllegalArgumentException("Reading frame should be between 1 and 3");
+        }
+
+        if (states[0] instanceof NucleotideState) {
+            int offset = readingFrame - 1;
+            int length = states.length - offset;
+
+            if (length == 0) return new CodonState[0];
+
+            CodonState[] conversion = new CodonState[length / 3];
+            for (int i = 0; i < conversion.length; i++) {
+                conversion[i] = Codons.getState(
+                        (NucleotideState)states[i * 3 + offset],
+                        (NucleotideState)states[(i * 3) + offset + 1],
+                        (NucleotideState)states[(i * 3) + offset + 2]);
+            }
+            return conversion;
+        } else {
+            throw new IllegalArgumentException("Given states are not nucleotides so cannot be converted");
+        }
+    }
+
     private static final NucleotideState[] statesByCode;
 
     static {


=====================================
src/jebl/evolution/sequences/Sequence.java
=====================================
@@ -11,10 +11,7 @@ package jebl.evolution.sequences;
 import jebl.evolution.taxa.Taxon;
 import jebl.util.Attributable;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.*;
 
 /**
  * A biomolecular sequence.
@@ -69,7 +66,7 @@ public interface Sequence extends Attributable, Comparable {
 	 * @param sequence2
 	 * @return
 	 */
-	public static Sequence appendSequences(Sequence sequence1, Sequence sequence2) {
+	static Sequence appendSequences(Sequence sequence1, Sequence sequence2) {
 		if (sequence1.getSequenceType() != sequence2.getSequenceType()) {
 			throw new IllegalArgumentException("sequences to be appended not of the same type");
 		}
@@ -86,7 +83,7 @@ public interface Sequence extends Attributable, Comparable {
 	 * @param to
 	 * @return
 	 */
-	public static Sequence getSubSequence(Sequence sequence, int from, int to) {
+	static Sequence getSubSequence(Sequence sequence, int from, int to) {
 		if (from > to) {
 			throw new IllegalArgumentException("subsequence from is greater than to");
 		}
@@ -98,24 +95,74 @@ public interface Sequence extends Attributable, Comparable {
 		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), states);
 	}
 
-	public static Sequence trimSequence(Sequence sequence, State[] trimStates) {
+	static Sequence trimSequence(Sequence sequence, State[] trimStates) {
 		Set<State> trimSet = new HashSet<>(Arrays.asList(trimStates));
 		State[] sourceStates = sequence.getStates();
 		int i = 0;
 		while (i < sourceStates.length && trimSet.contains(sourceStates[i])) {
 			i++;
 		}
-		if (i == sourceStates.length) {
-			return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), new State[] {} );
+		if (i < sourceStates.length) {
+			Sequence sequence1 = getSubSequence(sequence, i, sourceStates.length - 1);
+			sourceStates = sequence1.getStates();
+			i = sourceStates.length - 1;
+			while (i > 0 && trimSet.contains(sourceStates[i])) {
+				i--;
+			}
+			return getSubSequence(sequence1, 0, i);
+		} else {
+			return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), new State[] {});
 		}
+	}
+
+	/**
+	 * Strips a sequence of gaps
+	 * @param sequence the sequence
+	 * @return the stripped sequence
+	 */
+	static Sequence stripGaps(final Sequence sequence) {
+		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), jebl.evolution.sequences.Utils.stripGaps(sequence.getStates()));
+	}
+
+
+	/**
+	 * Strips a sequence of any states given
+	 * @param sequence the sequence
+	 * @param stripStates the states to strip
+	 * @return an array of states
+	 */
+	static Sequence stripStates(final Sequence sequence, final List<State> stripStates) {
+		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), jebl.evolution.sequences.Utils.stripStates(sequence.getStates(), stripStates));
+	}
 
-		Sequence sequence1 = getSubSequence(sequence, i, sourceStates.length - 1);
-		sourceStates = sequence1.getStates();
-		i = sourceStates.length - 1;
-		while (i > 0 && trimSet.contains(sourceStates[i])) {
-			i--;
+	/**
+	 * Searchers and replaces a sequence of any states given
+	 * @param sequence the sequence
+	 * @param searchStates the states to search for
+	 * @return an array of states
+	 */
+	static Sequence replaceStates(final Sequence sequence, final List<State> searchStates, State replaceState) {
+		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(),
+				jebl.evolution.sequences.Utils.replaceStates(sequence.getStates(), searchStates, replaceState));
+	}
+
+
+	/**
+	 * Counts the number of occurances of a state
+	 * @param sequence the sequence string to count
+	 * @param state the state
+	 * @return the number of occurances
+	 */
+	static int getStateCount(final Sequence sequence, final State state) {
+
+		int count = 0;
+		for (State s : sequence.getStates()) {
+			if (s.equals(state)) {
+				count ++;
+			}
 		}
-		return getSubSequence(sequence1, 0, i);
 
+		return count;
 	}
+
 }


=====================================
src/jebl/evolution/sequences/State.java
=====================================
@@ -89,7 +89,9 @@ public abstract class State implements Comparable {
 
     public abstract boolean isGap();
 
-	private String stateCode;
+    public abstract SequenceType getType();
+
+    private String stateCode;
     private String name;
     private Set<State> ambiguities;
     private int index;


=====================================
src/jebl/evolution/sequences/Utils.java
=====================================
@@ -10,6 +10,10 @@ package jebl.evolution.sequences;
 
 import jebl.evolution.taxa.Taxon;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 /**
  * @author Andrew Rambaut
  * @author Alexei Drummond
@@ -215,24 +219,53 @@ public class Utils {
         return translateCharSequence(nucleotideSequence, geneticCode);
     }
 
+    /**
+     * Strips a sequence of gaps
+     * @param sequence the sequence
+     * @return the stripped sequence
+     */
     public static State[] stripGaps(final State[] sequence) {
-        int count = 0;
+        if (sequence.length == 0) {
+            return new State[] {};
+        }
+
+        return stripStates(sequence, Collections.singletonList(sequence[0].getType().getGapState()));
+    }
+
+    /**
+     * Strips a sequence of any states given
+     * @param sequence the sequence
+     * @param stripStates the states to strip
+     * @return an array of states
+     */
+    public static State[] stripStates(final State[] sequence, final List<State> stripStates) {
+        List<State> stripped = new ArrayList<State>();
         for (State state : sequence) {
-            if (!state.isGap()) {
-                count++;
+            if (!stripStates.contains(state)) {
+                stripped.add(state);
             }
         }
 
-        State[] stripped = new State[count];
-        int index = 0;
+        return stripped.toArray(new State[0]);
+    }
+
+    /**
+     * Searchers and replaces a sequence of any states given
+     * @param sequence the sequence
+     * @param searchStates the states to search for
+     * @return an array of states
+     */
+    public static State[] replaceStates(final State[] sequence, final List<State> searchStates, State replaceState) {
+        List<State> stripped = new ArrayList<State>();
         for (State state : sequence) {
-            if (!state.isGap()) {
-                stripped[index] = state;
-                index += 1;
+            if (searchStates.contains(state)) {
+                stripped.add(replaceState);
+            } else {
+                stripped.add(state);
             }
         }
 
-        return stripped;
+        return stripped.toArray(new State[0]);
     }
 
     public static State[] reverse(final State[] sequence) {



View it on GitLab: https://salsa.debian.org/med-team/jebl2/-/compare/26097f2fc3174ef02f67d6625acf126203e196e6...a8569cac54e78618085ff4cb02895a881c498bcc

-- 
View it on GitLab: https://salsa.debian.org/med-team/jebl2/-/compare/26097f2fc3174ef02f67d6625acf126203e196e6...a8569cac54e78618085ff4cb02895a881c498bcc
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/debian-med-commit/attachments/20200424/6b6c1665/attachment-0001.html>


More information about the debian-med-commit mailing list