JAL-4159 sometimes the progress bar may not be available - null check
[jalview.git] / src / jalview / analysis / Connectivity.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 //import jalview.datamodel.AlignmentView;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.Desktop;
27 import jalview.gui.JvOptionPane;
28 import jalview.viewmodel.AlignmentViewport;
29
30 import java.util.Comparator;
31 import java.util.Hashtable;
32 import java.util.HashSet;
33 import java.util.TreeSet;
34
35 /**
36  * @Author MorellThomas
37  */
38
39 public class Connectivity
40 {
41
42   /**
43    * Returns the number of unique connections for each sequence
44    * only connections with a score of above 0 count
45    * 
46    * @param av sequences
47    * @param scores alignment scores
48    *
49    * @return connectivity
50    */
51   public static Hashtable<SequenceI, Integer> getConnectivity(AlignmentViewport av, float[][] scores, byte dim) throws RuntimeException
52   {
53     boolean isSelection = av.getSelectionGroup() != null && av.getSelectionGroup().getSize() > 0;
54     SequenceI[] sequences;
55     if (isSelection)
56     {
57       sequences = (SequenceI[]) av.getAlignmentView(isSelection).getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
58     } else {
59       sequences = av.getAlignment().getSequencesArray();
60     }
61
62     Hashtable<SequenceI, Integer> connectivity = new Hashtable<SequenceI, Integer>();
63     // for each unique connection
64     for (int i = 0; i < sequences.length; i++)
65     {
66       connectivity.putIfAbsent(sequences[i], 0);
67       for (int j = 0; j < i; j++)
68       {
69         connectivity.putIfAbsent(sequences[j], 0);
70         int iOld = connectivity.get(sequences[i]);
71         int jOld = connectivity.get(sequences[j]); 
72         // count the connection if its score is not NaN
73 //System.out.println(String.format("%s - %s : %f", sequences[i].getName(), sequences[j].getName(), scores[i][j]));
74         if (!Float.isNaN(scores[i][j]))
75         {
76           connectivity.put(sequences[i], ++iOld);
77           connectivity.put(sequences[j], ++jOld);
78         }
79       }
80     }
81
82     // if a sequence has too few connections, abort
83     connectivity.forEach((sequence, connection) ->
84     {
85       System.out.println(String.format("%s: %d", sequence.getName(), connection));
86       if (connection < dim)
87       {
88         JvOptionPane.showInternalMessageDialog(Desktop.desktop, String.format("Insufficient number of connections for %s (%d, should be %d or more)", sequence.getName(), connection, dim), "Connectivity Error", JvOptionPane.WARNING_MESSAGE);
89         throw new ConnectivityException(sequence.getName(), connection, dim);
90       }
91     } );
92
93     return connectivity;
94   }
95
96 }