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