2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
20 import com.stevesoft.pat.Regex;
22 import jalview.datamodel.*;
24 public class ParseProperties
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.
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) -
38 * The alignment being operated on
40 private AlignmentI al = null;
43 * initialise a new property parser
47 public ParseProperties(AlignmentI al)
52 public int getScoresFromDescription(String ScoreName,
53 String ScoreDescriptions, String regex, boolean repeat)
55 return getScoresFromDescription(new String[]
56 { ScoreName }, new String[]
57 { ScoreDescriptions }, regex, repeat);
60 public int getScoresFromDescription(String[] ScoreNames,
61 String[] ScoreDescriptions, String regex, boolean repeat)
63 return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
64 ScoreDescriptions, regex, repeat);
68 * Extract scores for sequences by applying regex to description string.
71 * seuqences to extract annotation from.
73 * labels for each numeric field in regex match
74 * @param ScoreDescriptions
75 * description for each numeric field in regex match
77 * Regular Expression string for passing to
78 * <code>new com.stevesoft.patt.Regex(regex)</code>
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
84 public int getScoresFromDescription(SequenceI[] seqs,
85 String[] ScoreNames, String[] ScoreDescriptions, String regex,
89 Regex pattern = new Regex(regex);
90 if (pattern.numSubs() > ScoreNames.length)
92 // Check that we have enough labels and descriptions for any parsed
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];
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++)
108 ScoreNames[i] = base + "_" + i;
109 ScoreDescriptions[i] = descrbase + " (column " + i + ")";
112 for (int i = 0; i < seqs.length; i++)
114 String descr = seqs[i].getDescription();
118 boolean added = false;
120 while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
122 pos = pattern.matchedTo();
123 for (int cols = 0; cols < pattern.numSubs(); cols++)
125 String sstring = pattern.stringMatched(cols + 1);
126 double score = Double.NaN;
129 score = new Double(sstring).doubleValue();
130 } catch (Exception e)
132 // don't try very hard to parse if regex was wrong.
135 // add score to sequence annotation.
136 AlignmentAnnotation an = new AlignmentAnnotation(ScoreNames[cols]
137 + ((reps > 0) ? "_" + reps : ""),
138 ScoreDescriptions[cols], null);
140 System.out.println("Score: " + ScoreNames[cols] + "=" + score); // DEBUG
141 an.setSequenceRef(seqs[i]);
142 seqs[i].addAlignmentAnnotation(an);
143 al.addAnnotation(an);
146 reps++; // repeated matches
156 public static void main(String argv[])
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.");