Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / examples / groovy / PIDmatrix.groovy
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
22 import jalview.analysis.scoremodels.ScoreModels
23 import jalview.analysis.scoremodels.SimilarityParams
24
25 // generate matrix for current selection using standard Jalview PID
26
27 printSimilarityMatrix(true,true,SimilarityParams.Jalview)
28
29 /** 
30  * this function prints a sequence similarity matrix in PHYLIP format. 
31  * printSimilarityMatrix(selected-only, include-ids, pidMethod)
32  * 
33  * Allowed values for pidMethod:
34  * 
35  * Jalview's Comparison.PID method includes matching gaps 
36  * and counts over the length of the shorter gapped sequence
37  * SimilarityParams.Jalview;
38  *
39  * 'SeqSpace' mode PCA calculation does not count matching 
40  * gaps but uses longest gapped sequence length
41  *  SimilarityParams.SeqSpace;
42  *
43  * PID calcs from the Raghava-Barton paper
44  * SimilarityParams.PID1: ignores gap-gap, does not score gap-residue,
45  * includes gap-residue in lengths, matches on longer of two sequences.
46  * 
47  * SimilarityParams.PID2: ignores gap-gap,ignores gap-residue, 
48  * matches on longer of two sequences
49  * 
50  * SimilarityParams.PID3: ignores gap-gap,ignores gap-residue, 
51  * matches on shorter of sequences only
52  * 
53  * SimilarityParams.PID4: ignores gap-gap,does not score gap-residue,
54  * includes gap-residue in lengths,matches on shorter of sequences only.
55  */
56
57 void printSimilarityMatrix(boolean selview=false, boolean includeids=true, SimilarityParams pidMethod) {
58
59   def currentAlignFrame = Jalview.getCurrentAlignFrame()
60
61   jalview.gui.AlignViewport av = currentAlignFrame.getCurrentView()
62
63   jalview.datamodel.AlignmentView seqStrings = av.getAlignmentView(selview)
64
65   if (!selview || av.getSelectionGroup()==null) {
66     start = 0
67     end = av.getAlignment().getWidth()
68     seqs = av.getAlignment().getSequencesArray()
69   } else {
70     start = av.getSelectionGroup().getStartRes()
71     end = av.getSelectionGroup().getEndRes() + 1
72     seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment())
73   }
74
75   distanceCalc = ScoreModels.getInstance().getScoreModel("PID",
76       (jalview.api.AlignmentViewPanel) currentAlignFrame.alignPanel)
77
78   def distance=distanceCalc.findSimilarities(
79       seqStrings.getSequenceStrings(jalview.util.Comparison.GAP_DASH),pidMethod)
80
81   // output the PHYLIP Matrix
82
83   print distance.width()+" "+distance.height()+"\n"
84
85   p = 0
86
87   for (v in 1..distance.height()) {
88
89     if (includeids) {
90       print seqs[p++].getDisplayId(false)+" "
91     }
92
93     for (r in 1..distance.width()) {
94       print distance.getValue(v-1,r-1)+" "
95     }
96
97     print "\n"
98   }
99 }