JAL-1620 version bump and release notes
[jalview.git] / src / jalview / analysis / ParseProperties.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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 com.stevesoft.pat.Regex;
24
25 import jalview.datamodel.*;
26
27 public class ParseProperties
28 {
29   /**
30    * Methods for parsing free text properties on alignments and sequences. There
31    * are a number of ways we might want to do this: arbitrary regex. and an
32    * associated score name for the number that's extracted. Regex that provides
33    * both score and name.
34    * 
35    * We may also want to : - modify description to remove parsed numbers (this
36    * behaviour is dangerous since exporting the alignment would lose the
37    * original form then) -
38    * 
39    */
40   /**
41    * The alignment being operated on
42    */
43   private AlignmentI al = null;
44
45   /**
46    * initialise a new property parser
47    * 
48    * @param al
49    */
50   public ParseProperties(AlignmentI al)
51   {
52     this.al = al;
53   }
54
55   public int getScoresFromDescription(String ScoreName,
56           String ScoreDescriptions, String regex, boolean repeat)
57   {
58     return getScoresFromDescription(new String[]
59     { ScoreName }, new String[]
60     { ScoreDescriptions }, regex, repeat);
61   }
62
63   public int getScoresFromDescription(String[] ScoreNames,
64           String[] ScoreDescriptions, String regex, boolean repeat)
65   {
66     return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
67             ScoreDescriptions, regex, repeat);
68   }
69
70   /**
71    * Extract scores for sequences by applying regex to description string.
72    * 
73    * @param seqs
74    *          seuqences to extract annotation from.
75    * @param ScoreNames
76    *          labels for each numeric field in regex match
77    * @param ScoreDescriptions
78    *          description for each numeric field in regex match
79    * @param regex
80    *          Regular Expression string for passing to
81    *          <code>new com.stevesoft.patt.Regex(regex)</code>
82    * @param repeat
83    *          true means the regex will be applied multiple times along the
84    *          description string of each sequence
85    * @return total number of sequences that matched the regex
86    */
87   public int getScoresFromDescription(SequenceI[] seqs,
88           String[] ScoreNames, String[] ScoreDescriptions, String regex,
89           boolean repeat)
90   {
91     int count = 0;
92     Regex pattern = new Regex(regex);
93     if (pattern.numSubs() > ScoreNames.length)
94     {
95       // Check that we have enough labels and descriptions for any parsed
96       // scores.
97       int onamelen = ScoreNames.length;
98       String[] tnames = new String[pattern.numSubs() + 1];
99       System.arraycopy(ScoreNames, 0, tnames, 0, ScoreNames.length);
100       String base = tnames[ScoreNames.length - 1];
101       ScoreNames = tnames;
102       String descrbase = ScoreDescriptions[onamelen - 1];
103       if (descrbase == null)
104         descrbase = "Score parsed from (" + regex + ")";
105       tnames = new String[pattern.numSubs() + 1];
106       System.arraycopy(ScoreDescriptions, 0, tnames, 0,
107               ScoreDescriptions.length);
108       ScoreDescriptions = tnames;
109       for (int i = onamelen; i < ScoreNames.length; i++)
110       {
111         ScoreNames[i] = base + "_" + i;
112         ScoreDescriptions[i] = descrbase + " (column " + i + ")";
113       }
114     }
115     for (int i = 0; i < seqs.length; i++)
116     {
117       String descr = seqs[i].getDescription();
118       if (descr == null)
119         continue;
120       int pos = 0;
121       boolean added = false;
122       int reps = 0;
123       while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
124       {
125         pos = pattern.matchedTo();
126         for (int cols = 0; cols < pattern.numSubs(); cols++)
127         {
128           String sstring = pattern.stringMatched(cols + 1);
129           double score = Double.NaN;
130           try
131           {
132             score = new Double(sstring).doubleValue();
133           } catch (Exception e)
134           {
135             // don't try very hard to parse if regex was wrong.
136             continue;
137           }
138           // add score to sequence annotation.
139           AlignmentAnnotation an = new AlignmentAnnotation(ScoreNames[cols]
140                   + ((reps > 0) ? "_" + reps : ""),
141                   ScoreDescriptions[cols], null);
142           an.setScore(score);
143           System.out.println("Score: " + ScoreNames[cols] + "=" + score); // DEBUG
144           an.setSequenceRef(seqs[i]);
145           seqs[i].addAlignmentAnnotation(an);
146           al.addAnnotation(an);
147           added = true;
148         }
149         reps++; // repeated matches
150       }
151       if (added)
152       {
153         count++;
154       }
155     }
156     return count;
157   }
158
159   public static void main(String argv[])
160   {
161     SequenceI[] seqs = new SequenceI[]
162     { new Sequence("sq1", "THISISAPLACEHOLDER"),
163         new Sequence("sq2", "THISISAPLACEHOLDER"),
164         new Sequence("sq3", "THISISAPLACEHOLDER"),
165         new Sequence("sq4", "THISISAPLACEHOLDER") };
166     seqs[0].setDescription("1 mydescription1");
167     seqs[1].setDescription("mydescription2");
168     seqs[2].setDescription("2. 0.1 mydescription3");
169     seqs[3].setDescription("3 0.01 mydescription4");
170     // seqs[4].setDescription("5 mydescription5");
171     Alignment al = new Alignment(seqs);
172     ParseProperties pp = new ParseProperties(al);
173     String regex = ".*([-0-9.+]+)";
174     System.out.println("Matched "
175             + pp.getScoresFromDescription("my Score",
176                     "my Score Description", regex, true) + " for " + regex);
177     regex = ".*([-0-9.+]+).+([-0-9.+]+).*";
178     System.out.println("Matched "
179             + pp.getScoresFromDescription("my Score",
180                     "my Score Description", regex, true) + " for " + regex);
181     System.out.println("Finished.");
182   }
183 }