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