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