Fix PaSiMap on selected sequences
[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.viewmodel.AlignmentViewport;
27
28 import java.util.Hashtable;
29
30 /**
31  * @Author MorellThomas
32  */
33
34 public class Connectivity
35 {
36
37   /**
38    * Returns the number of unique connections for each sequence
39    * only connections with a score of above 0 count
40    * 
41    * @param av sequences
42    * @param scores alignment scores
43    *
44    * @return connectivity
45    */
46   public static Hashtable<SequenceI, Integer> getConnectivity(AlignmentViewport av, float[][] scores, byte dim) throws RuntimeException
47   {
48     boolean isSelection = av.getSelectionGroup() != null && av.getSelectionGroup().getSize() > 0;
49     SequenceI[] sequences;
50     if (isSelection)
51     {
52       sequences = (SequenceI[]) av.getAlignmentView(isSelection).getAlignmentAndHiddenColumns(av.getGapCharacter())[0];
53     } else {
54       sequences = av.getAlignment().getSequencesArray();
55     }
56
57     Hashtable<SequenceI, Integer> connectivity = new Hashtable<SequenceI, Integer>();
58     // for each unique connection
59     for (int i = 0; i < sequences.length; i++)
60     {
61       connectivity.putIfAbsent(sequences[i], 0);
62       for (int j = 0; j < i; j++)
63       {
64         connectivity.putIfAbsent(sequences[j], 0);
65         int iOld = connectivity.get(sequences[i]);
66         int jOld = connectivity.get(sequences[j]); 
67         // count the connection if its score is not NaN
68         if (!Float.isNaN(scores[i][j]))
69         {
70           connectivity.put(sequences[i], ++iOld);
71           connectivity.put(sequences[j], ++jOld);
72         }
73       }
74     }
75
76     // if a sequence has too few connections, abort
77     connectivity.forEach((sequence, connection) ->
78     {
79       System.out.println(String.format("%s: %d", sequence.getName(), connection));
80       if (connection < dim)
81       {
82         // a popup saying that it failed would be nice
83         throw new ConnectivityException(sequence.getName(), connection, dim);
84       }
85     } );
86
87     return connectivity;
88   }
89
90 }