[med-svn] [Git][med-team/jebl2][upstream] New upstream version 0.1+git20200723.3a2ead5

Pierre Gruet gitlab at salsa.debian.org
Thu Jul 23 21:16:03 BST 2020



Pierre Gruet pushed to branch upstream at Debian Med / jebl2


Commits:
79f5e87a by Pierre Gruet at 2020-07-23T17:57:08+02:00
New upstream version 0.1+git20200723.3a2ead5
- - - - -


6 changed files:

- src/jebl/evolution/io/NewickExporter.java
- src/jebl/evolution/io/NexusExporter.java
- src/jebl/evolution/io/PHYLIPExporter.java
- src/jebl/evolution/io/TreeExporter.java
- src/jebl/evolution/sequences/Sequence.java
- src/jebl/evolution/trees/RootedSubtree.java


Changes:

=====================================
src/jebl/evolution/io/NewickExporter.java
=====================================
@@ -4,6 +4,7 @@ import jebl.evolution.trees.Tree;
 import jebl.evolution.trees.Utils;
 import jebl.evolution.trees.RootedTree;
 
+import java.io.PrintWriter;
 import java.io.Writer;
 import java.io.IOException;
 import java.io.BufferedWriter;
@@ -16,7 +17,7 @@ import java.util.Collection;
  */
 public class NewickExporter implements TreeExporter {
     public NewickExporter(Writer writer) {
-        this.writer = writer;
+        this.writer = new PrintWriter(writer);
     }
 
     /**
@@ -25,7 +26,7 @@ public class NewickExporter implements TreeExporter {
      * @param tree
      * @throws java.io.IOException
      */
-    public void exportTree(Tree tree) throws IOException {
+    public void exportTree(Tree tree) {
         writeTree(tree);
     }
 
@@ -35,15 +36,20 @@ public class NewickExporter implements TreeExporter {
      * @param trees
      * @throws java.io.IOException
      */
-    public void exportTrees(Collection<? extends Tree> trees) throws IOException {
+    public void exportTrees(Collection<? extends Tree> trees) {
         for (Tree tree : trees) {
             writeTree(tree);
         }
     }
 
-    private void writeTree(Tree tree) throws IOException {
-        writer.write(Utils.toNewick(Utils.rootTheTree(tree)));
+    @Override
+    public void close() {
+        writer.close();
     }
 
-    private final Writer writer;
+    private void writeTree(Tree tree) {
+        writer.println(Utils.toNewick(Utils.rootTheTree(tree)));
+    }
+
+    private final PrintWriter writer;
 }


=====================================
src/jebl/evolution/io/NexusExporter.java
=====================================
@@ -30,16 +30,16 @@ import java.util.List;
 
 public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeExporter {
 
-	public NexusExporter(Writer writer) {
-		this(writer, true);
-	}
+    public NexusExporter(Writer writer) {
+        this(writer, true);
+    }
 
-	/**
+    /**
      *
      * @param writer where export text goes
      */
     public NexusExporter(Writer writer, boolean writeMetaComments) {
-		this(writer, writeMetaComments, false);
+        this(writer, writeMetaComments, false);
     }
 
     /**
@@ -47,7 +47,7 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
      * @param writer where export text goes
      */
     public NexusExporter(Writer writer, boolean writeMetaComments, boolean interleave) {
-		this.writeMetaComments = writeMetaComments;
+        this.writeMetaComments = writeMetaComments;
         this.interleave = interleave;
         this.writer = new PrintWriter(writer);
         this.writer.println("#NEXUS");
@@ -56,14 +56,18 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
     /**
      * exportAlignment.
      */
-    public void exportAlignment(Alignment alignment) throws IOException {
-    	exportSequences(alignment.getSequences());
+    public void exportAlignment(Alignment alignment) {
+        exportSequences(alignment.getSequences());
     }
 
     /**
      * export alignment.
      */
-    public void exportSequences(Collection<? extends Sequence> sequences) throws IOException, IllegalArgumentException {
+    public void exportSequences(Collection<? extends Sequence> sequences) throws IllegalArgumentException {
+
+        if (treesBlockOpen) {
+            endWriteTrees();
+        }
 
         establishSequenceTaxa(sequences);
 
@@ -77,7 +81,7 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
             if (seqType == null) {
                 seqType = sequence.getSequenceType();
             } else if( seqType != sequence.getSequenceType() ) {
-               throw new IllegalArgumentException("All seqeuences must have the same type");
+                throw new IllegalArgumentException("All seqeuences must have the same type");
             }
         }
 
@@ -113,58 +117,25 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
         }
     }
 
+    private boolean treesBlockOpen = false;
+
     /**
      * Export a single tree
      *
      * @param tree
      * @throws java.io.IOException
      */
-    public void exportTree(Tree tree) throws IOException {
+    public void exportTree(Tree tree) {
         List<Tree> trees = new ArrayList<Tree>();
         trees.add(tree);
         exportTrees(trees);
     }
 
-    private void writeTrees(Collection<? extends Tree> trees, boolean checkTaxa) throws IOException {
-        int nt = 0;
-        for( Tree t : trees ) {
-            if( checkTaxa && !establishTreeTaxa(t) ) {
-                throw new IllegalArgumentException();
-            }
-            final boolean isRooted = t instanceof RootedTree;
-            final RootedTree rtree = isRooted ? (RootedTree)t : Utils.rootTheTree(t);
-
-            final Object name = t.getAttribute(treeNameAttributeKey);
-
-            ++nt;
-            final String treeName = (name != null) ? NexusImporter.makeIntoAllowableIdentifier(name.toString()) : "tree_" + nt;
-
-            StringBuilder builder = new StringBuilder("\ttree ");
-
-            builder.append(treeName);
-            builder.append(" = ");
-
-            // TREE & UTREE are depreciated in the NEXUS format in favour of a metacomment
-            // [&U] or [&R] after the TREE command. Andrew.
-            // TT: The [&U], [&R] should actually come *after* the " = " and be uppercase, see
-            // e.g. tree_rest in http://www.cs.nmsu.edu/~epontell/nexus/nexus_grammar .
-            // Before 2008-05-05 we incorrectly inserted it before the treeName.
-            builder.append(isRooted && !rtree.conceptuallyUnrooted() ? "[&R] " : "[&U] ");
-
-            appendAttributes(rtree, exportExcludeKeys, builder);
-
-            appendTree(rtree, rtree.getRootNode(), builder);
-            builder.append(";");
-
-            writer.println(builder);
-        }
-    }
-
-    public void exportTrees(Collection<? extends Tree> trees) throws IOException {
+    public void exportTrees(Collection<? extends Tree> trees) {
         exportTrees(trees, false);
     }
 
-    public void exportTrees(Collection<? extends Tree> trees, boolean writeTaxa) throws IOException {
+    public void exportTrees(Collection<? extends Tree> trees, boolean writeTaxa) {
         if (writeTaxa) {
             TreeSet<Taxon> taxa = new TreeSet<Taxon>();
             for (Tree tree : trees) {
@@ -174,26 +145,88 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
             establishTaxa(taxa);
         }
 
-        writer.println("begin trees;");
+        if (!treesBlockOpen) {
+            startWriteTrees(null);
+        }
+        writeTrees(trees, false);
+    }
+
+    public void exportTreesWithTranslation(Collection<? extends Tree> trees, Map<String, String> translationMap) throws IOException {
+        if (!treesBlockOpen) {
+            startWriteTrees(translationMap);
+        }
         writeTrees(trees, false);
-        writer.println("end;");
     }
 
-    public void exportTreesWithTranslation(Collection<? extends Tree> trees, Map<String, String> t) throws IOException {
+    public void close() {
+        if (treesBlockOpen) {
+            endWriteTrees();
+        }
+        writer.close();
+    }
+
+    private void startWriteTrees(Map<String, String> translationMap) {
         writer.println("begin trees;");
-        writer.println("\ttranslate");
-        boolean first = true;
-        for( Map.Entry<String, String> e : t.entrySet() ) {
-            writer.print((first ? "" : ",\n") + "\t\t" + safeName(e.getKey()) + " " + safeName(e.getValue()));
-            first = false;
+        if (translationMap != null) {
+            writer.println("\ttranslate");
+            boolean first = true;
+            for (Map.Entry<String, String> e : translationMap.entrySet()) {
+                writer.print((first ? "" : ",\n") + "\t\t" + safeName(e.getKey()) + " " + safeName(e.getValue()));
+                first = false;
+            }
+            writer.println("\n\t;");
         }
-        writer.println("\n\t;");
+        treesBlockOpen = true;
+    }
 
-        writeTrees(trees, false);
+    private void endWriteTrees() {
         writer.println("end;");
     }
 
+    private void writeTrees(Collection<? extends Tree> trees, boolean checkTaxa) {
+        for (Tree tree : trees) {
+            writeTree(tree, checkTaxa);
+        }
+    }
+
+    private void writeTree(Tree tree, boolean checkTaxa) {
+        int nt = 0;
+        if( checkTaxa && !establishTreeTaxa(tree) ) {
+            throw new IllegalArgumentException();
+        }
+        final boolean isRooted = tree instanceof RootedTree;
+        final RootedTree rtree = isRooted ? (RootedTree)tree : Utils.rootTheTree(tree);
+
+        final Object name = tree.getAttribute(treeNameAttributeKey);
+
+        ++nt;
+        final String treeName = (name != null) ? NexusImporter.makeIntoAllowableIdentifier(name.toString()) : "tree_" + nt;
+
+        StringBuilder builder = new StringBuilder("\ttree ");
+
+        builder.append(treeName);
+        builder.append(" = ");
+
+        // TREE & UTREE are depreciated in the NEXUS format in favour of a metacomment
+        // [&U] or [&R] after the TREE command. Andrew.
+        // TT: The [&U], [&R] should actually come *after* the " = " and be uppercase, see
+        // e.g. tree_rest in http://www.cs.nmsu.edu/~epontell/nexus/nexus_grammar .
+        // Before 2008-05-05 we incorrectly inserted it before the treeName.
+        builder.append(isRooted && !rtree.conceptuallyUnrooted() ? "[&R] " : "[&U] ");
+
+        appendAttributes(rtree, exportExcludeKeys, builder);
+
+        appendTree(rtree, rtree.getRootNode(), builder);
+        builder.append(";");
+
+        writer.println(builder);
+    }
+
     public void exportMatrix(final DistanceMatrix distanceMatrix) {
+        if (treesBlockOpen) {
+            endWriteTrees();
+        }
+
         final List<Taxon> taxa = distanceMatrix.getTaxa();
         establishTaxa(taxa);
         writer.println("begin distances;");
@@ -365,9 +398,9 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
     }
 
     private StringBuilder appendAttributes(Attributable item, String[] excludeKeys, StringBuilder builder) {
-	    if (!writeMetaComments) {
-		    return builder;
-	    }
+        if (!writeMetaComments) {
+            return builder;
+        }
 
         boolean first = true;
         for( String key : item.getAttributeNames() ) {
@@ -445,7 +478,7 @@ public class NexusExporter implements AlignmentExporter, SequenceExporter, TreeE
 
     private Set<Taxon> taxa = null;
     protected final PrintWriter writer;
-	private boolean writeMetaComments;
+    private boolean writeMetaComments;
     private boolean interleave;
     public static final int MAX_ROW_LENGTH = 60;
 }


=====================================
src/jebl/evolution/io/PHYLIPExporter.java
=====================================
@@ -104,15 +104,20 @@ public class PHYLIPExporter implements AlignmentExporter, TreeExporter {
 
     // Should call those only after the alignment
 
-    public void exportTree(Tree tree) throws IOException {
+    public void exportTree(Tree tree) {
         final RootedTree rtree = Utils.rootTheTree(tree);
         writer.print(Utils.toNewick(rtree));
         writer.println(";");
     }
 
-    public void exportTrees(Collection<? extends Tree> trees) throws IOException {
+    public void exportTrees(Collection<? extends Tree> trees) {
        for( Tree t : trees ) {
            exportTree(t);
        }
     }
+
+    @Override
+    public void close() {
+        writer.close();
+    }
 }


=====================================
src/jebl/evolution/io/TreeExporter.java
=====================================
@@ -19,12 +19,14 @@ public interface TreeExporter {
      * @param tree
      * @throws IOException
      */
-    void exportTree(Tree tree) throws IOException;
+    void exportTree(Tree tree);
 
     /**
      * Export a collection of trees
      * @param trees
      * @throws IOException
      */
-    void exportTrees(Collection<? extends Tree> trees) throws IOException;
+    void exportTrees(Collection<? extends Tree> trees);
+
+    void close();
 }


=====================================
src/jebl/evolution/sequences/Sequence.java
=====================================
@@ -95,18 +95,17 @@ public interface Sequence extends Attributable, Comparable {
 		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(), states);
 	}
 
-	static Sequence trimSequence(Sequence sequence, State[] trimStates) {
-		Set<State> trimSet = new HashSet<>(Arrays.asList(trimStates));
+	static Sequence trimSequence(Sequence sequence, List<State> trimStates) {
 		State[] sourceStates = sequence.getStates();
 		int i = 0;
-		while (i < sourceStates.length && trimSet.contains(sourceStates[i])) {
+		while (i < sourceStates.length && trimStates.contains(sourceStates[i])) {
 			i++;
 		}
 		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])) {
+			while (i > 0 && trimStates.contains(sourceStates[i])) {
 				i--;
 			}
 			return getSubSequence(sequence1, 0, i);
@@ -132,7 +131,8 @@ public interface Sequence extends Attributable, Comparable {
 	 * @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));
+		return new BasicSequence(sequence.getSequenceType(), sequence.getTaxon(),
+				jebl.evolution.sequences.Utils.stripStates(sequence.getStates(), stripStates));
 	}
 
 	/**


=====================================
src/jebl/evolution/trees/RootedSubtree.java
=====================================
@@ -52,23 +52,26 @@ final public class RootedSubtree implements RootedTree {
                     children.add(newChild);
                 }
             }
-            if (children.size() >= 2) {
+            if (children.size() > 1) {
                 newNode = createInternalNode(children);
 
-                for( Map.Entry<String, Object> e : node.getAttributeMap().entrySet() ) {
-                    newNode.setAttribute(e.getKey(), e.getValue());
-                }
-
-                setHeight(newNode, tree.getHeight(node));
             } else if (children.size() == 1) {
 
-                newNode = children.get(0);
+                // just return the single child
+                return children.get(0);
             } else {
 
                 newNode = null;
             }
         }
 
+        if (newNode != null) {
+            for( Map.Entry<String, Object> e : node.getAttributeMap().entrySet() ) {
+                newNode.setAttribute(e.getKey(), e.getValue());
+            }
+            setHeight(newNode, tree.getHeight(node));
+        }
+
         return newNode;
     }
 



View it on GitLab: https://salsa.debian.org/med-team/jebl2/-/commit/79f5e87ae9f3d5f4c9e766b63d3fa018719210e9

-- 
View it on GitLab: https://salsa.debian.org/med-team/jebl2/-/commit/79f5e87ae9f3d5f4c9e766b63d3fa018719210e9
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/20200723/a3fd1c6d/attachment-0001.html>


More information about the debian-med-commit mailing list