X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FSeqsetUtils.java;h=e2ed9b451ca94bd64da7c0a675835ad4c1ab205c;hb=841485c28403b4a8e00c9bddae9caa9411667b8a;hp=a0d0f0030b3540f26ec603b86340fb4563482a09;hpb=153dd62dc91da13ae732600e6ea55ddbe15eab39;p=jalview.git diff --git a/src/jalview/analysis/SeqsetUtils.java b/src/jalview/analysis/SeqsetUtils.java index a0d0f00..e2ed9b4 100755 --- a/src/jalview/analysis/SeqsetUtils.java +++ b/src/jalview/analysis/SeqsetUtils.java @@ -1,81 +1,91 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6) - * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * * Jalview 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 General Public License for more details. * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.analysis; -import java.util.*; +import jalview.bin.Cache; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.HiddenMarkovModel; +import jalview.datamodel.PDBEntry; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceFeature; +import jalview.datamodel.SequenceI; -import jalview.datamodel.*; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Vector; +import static java.lang.String.format; -/** - *

- * Title: - *

- * - *

- * Description: - *

- * - *

- * Copyright: Copyright (c) 2004 - *

- * - *

- * Company: Dundee University - *

- * - * @author not attributable - * @version 1.0 - */ public class SeqsetUtils { + public static class SequenceInfo { + private String name; + private int start; + private int end; + private Optional description = Optional.empty(); + private Optional> features = Optional.empty(); + private Optional> pdbId = Optional.empty(); + private Optional dataset = Optional.empty(); + private Optional hmm = Optional.empty(); + private Optional searchScores = Optional.empty(); + + private SequenceInfo(String name, int start, int end) { + this.name = name; + this.start = start; + this.end = end; + } + } /** * Store essential properties of a sequence in a hashtable for later recovery - * Keys are Name, Start, End, SeqFeatures, PdbId + * Keys are Name, Start, End, SeqFeatures, PdbId, HMM * * @param seq * SequenceI * @return Hashtable */ - public static Hashtable SeqCharacterHash(SequenceI seq) + public static SequenceInfo SeqCharacterHash(SequenceI seq) { - Hashtable sqinfo = new Hashtable(); - sqinfo.put("Name", seq.getName()); - sqinfo.put("Start", new Integer(seq.getStart())); - sqinfo.put("End", new Integer(seq.getEnd())); - if (seq.getDescription() != null) + SequenceInfo sqinfo = new SequenceInfo(seq.getName(), seq.getStart(), seq.getEnd()); + sqinfo.description = Optional.ofNullable(seq.getDescription()); + sqinfo.dataset = Optional.ofNullable(seq.getDatasetSequence()); + if (!sqinfo.dataset.isPresent()) { - sqinfo.put("Description", seq.getDescription()); + ArrayList feats = new ArrayList<>( + seq.getFeatures().getAllFeatures()); + sqinfo.features = Optional.of(feats); + sqinfo.pdbId = Optional.of(Objects.requireNonNullElse( + seq.getAllPDBEntries(), new ArrayList<>())); } - Vector sfeat = new Vector(); - jalview.datamodel.SequenceFeature[] sfarray = seq.getSequenceFeatures(); - if (sfarray != null && sfarray.length > 0) + if (seq.hasHMMProfile()) { - for (int i = 0; i < sfarray.length; i++) - { - sfeat.addElement(sfarray[i]); - } + sqinfo.hmm = Optional.of(seq.getHMM()); } - sqinfo.put("SeqFeatures", sfeat); - sqinfo.put("PdbId", (seq.getPDBId() != null) ? seq.getPDBId() - : new Vector()); - sqinfo.put("datasetSequence", (seq.getDatasetSequence() != null) ? seq - .getDatasetSequence() : new Sequence("THISISAPLACEHOLDER", "")); + sqinfo.searchScores = Optional.ofNullable(seq.getAnnotation("Search Scores")); return sqinfo; } @@ -89,60 +99,44 @@ public class SeqsetUtils * Hashtable * @return boolean true if name was not updated from sqinfo Name entry */ - public static boolean SeqCharacterUnhash(SequenceI sq, Hashtable sqinfo) + public static boolean SeqCharacterUnhash(SequenceI sq, SequenceInfo sqinfo) { - boolean namePresent = true; if (sqinfo == null) { return false; } - String oldname = (String) sqinfo.get("Name"); - Integer start = (Integer) sqinfo.get("Start"); - Integer end = (Integer) sqinfo.get("End"); - Vector sfeatures = (Vector) sqinfo.get("SeqFeatures"); - Vector pdbid = (Vector) sqinfo.get("PdbId"); - String description = (String) sqinfo.get("Description"); - Sequence seqds = (Sequence) sqinfo.get("datasetSequence"); - if (oldname == null) + if (sqinfo.name != null) { - namePresent = false; + sq.setName(sqinfo.name); } - else + sq.setStart(sqinfo.start); + sq.setEnd(sqinfo.end); + if (sqinfo.pdbId.isPresent() && !sqinfo.pdbId.get().isEmpty()) + sq.setPDBId(new Vector<>(sqinfo.pdbId.get())); + if (sqinfo.features.isPresent() && !sqinfo.features.get().isEmpty()) + sq.setSequenceFeatures(sqinfo.features.get()); + if (sqinfo.description.isPresent()) + sq.setDescription(sqinfo.description.get()); + if (sqinfo.dataset.isPresent()) { - sq.setName(oldname); - } - if (pdbid != null && pdbid.size() > 0) - { - sq.setPDBId(pdbid); - } - - if ((start != null) && (end != null)) - { - sq.setStart(start.intValue()); - sq.setEnd(end.intValue()); - } - - if ((sfeatures != null) && (sfeatures.size() > 0)) - { - SequenceFeature[] sfarray = new SequenceFeature[sfeatures.size()]; - for (int is = 0, isize = sfeatures.size(); is < isize; is++) + if (sqinfo.features.isPresent()) { - sfarray[is] = (SequenceFeature) sfeatures.elementAt(is); + Cache.log.warn("Setting dataset sequence for a sequence which has " + + "sequence features. Dataset sequence features will not be visible."); + assert false; } - sq.setSequenceFeatures(sfarray); - } - if (description != null) - { - sq.setDescription(description); + sq.setDatasetSequence(sqinfo.dataset.get()); } - if ((seqds != null) - && !(seqds.getName().equals("THISISAPLACEHOLDER") && seqds - .getLength() == 0)) + if (sqinfo.hmm.isPresent()) + sq.setHMM(new HiddenMarkovModel(sqinfo.hmm.get(), sq)); + if (sqinfo.searchScores.isPresent()) { - sq.setDatasetSequence(seqds); + for (AlignmentAnnotation score : sqinfo.searchScores.get()) + { + sq.addAlignmentAnnotation(score); + } } - - return namePresent; + return sqinfo.name != null; } /** @@ -155,7 +149,7 @@ public class SeqsetUtils */ public static String unique_name(int i) { - return new String("Sequence" + i); + return String.format("Sequence%d", i); } /** @@ -172,12 +166,12 @@ public class SeqsetUtils * @see deuniquify to recover original names (and properties) for renamed * sequences */ - public static Hashtable uniquify(SequenceI[] sequences, + public static Map uniquify(SequenceI[] sequences, boolean write_names) { // Generate a safely named sequence set and a hash to recover the sequence // names - Hashtable map = new Hashtable(); + HashMap map = new HashMap<>(); // String[] un_names = new String[sequences.length]; for (int i = 0; i < sequences.length; i++) @@ -205,7 +199,8 @@ public class SeqsetUtils * SequenceI[] * @return boolean */ - public static boolean deuniquify(Hashtable map, SequenceI[] sequences) + public static boolean deuniquify(Map map, + SequenceI[] sequences) { return deuniquify(map, sequences, true); } @@ -224,26 +219,25 @@ public class SeqsetUtils * map. * @return boolean */ - public static boolean deuniquify(Hashtable map, SequenceI[] sequences, - boolean quiet) + public static boolean deuniquify(Map map, + SequenceI[] sequences, boolean quiet) { jalview.analysis.SequenceIdMatcher matcher = new SequenceIdMatcher( sequences); SequenceI msq = null; - Enumeration keys = map.keys(); - Vector unmatched = new Vector(); + Iterator keys = map.keySet().iterator(); + Vector unmatched = new Vector<>(); for (int i = 0, j = sequences.length; i < j; i++) { unmatched.addElement(sequences[i]); } - while (keys.hasMoreElements()) + while (keys.hasNext()) { - Object key = keys.nextElement(); - if (key instanceof String) - { + String key = keys.next(); + try { if ((msq = matcher.findIdMatch((String) key)) != null) { - Hashtable sqinfo = (Hashtable) map.get(key); + SequenceInfo sqinfo = map.get(key); unmatched.removeElement(msq); SeqCharacterUnhash(msq, sqinfo); } @@ -251,20 +245,27 @@ public class SeqsetUtils { if (!quiet) { - System.err.println("Can't find '" + ((String) key) - + "' in uniquified alignment"); + Cache.log.warn(format("Can't find '%s' in uniquified alignment", + key)); } } + } catch (ClassCastException ccastex) { + if (!quiet) + { + Cache.log.error("Unexpected object in SeqSet map : "+ key.getClass()); + } } } if (unmatched.size() > 0 && !quiet) { - System.err.println("Did not find matches for :"); - for (Enumeration i = unmatched.elements(); i.hasMoreElements(); System.out - .println(((SequenceI) i.nextElement()).getName())) + StringBuilder sb = new StringBuilder("Did not find match for sequences: "); + Enumeration i = unmatched.elements(); + sb.append(i.nextElement().getName()); + for (; i.hasMoreElements();) { - ; + sb.append(", " + i.nextElement().getName()); } + Cache.log.warn(sb.toString()); return false; } @@ -287,8 +288,8 @@ public class SeqsetUtils for (int i = 0, j = sequences.length; i < j; i++) { String tempseq = jalview.analysis.AlignSeq.extractGaps( - jalview.util.Comparison.GapChars, sequences[i] - .getSequenceAsString()); + jalview.util.Comparison.GapChars, + sequences[i].getSequenceAsString()); if (tempseq.length() == 0) {