2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
21 import com.stevesoft.pat.Regex;
23 import jalview.datamodel.*;
25 public class ParseProperties
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.
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) -
39 * The alignment being operated on
41 private AlignmentI al = null;
44 * initialise a new property parser
48 public ParseProperties(AlignmentI al)
53 public int getScoresFromDescription(String ScoreName,
54 String ScoreDescriptions, String regex, boolean repeat)
56 return getScoresFromDescription(new String[]
57 { ScoreName }, new String[]
58 { ScoreDescriptions }, regex, repeat);
61 public int getScoresFromDescription(String[] ScoreNames,
62 String[] ScoreDescriptions, String regex, boolean repeat)
64 return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
65 ScoreDescriptions, regex, repeat);
69 * Extract scores for sequences by applying regex to description string.
72 * seuqences to extract annotation from.
74 * labels for each numeric field in regex match
75 * @param ScoreDescriptions
76 * description for each numeric field in regex match
78 * Regular Expression string for passing to
79 * <code>new com.stevesoft.patt.Regex(regex)</code>
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
85 public int getScoresFromDescription(SequenceI[] seqs,
86 String[] ScoreNames, String[] ScoreDescriptions, String regex,
90 Regex pattern = new Regex(regex);
91 if (pattern.numSubs() > ScoreNames.length)
93 // Check that we have enough labels and descriptions for any parsed
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];
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++)
109 ScoreNames[i] = base + "_" + i;
110 ScoreDescriptions[i] = descrbase + " (column " + i + ")";
113 for (int i = 0; i < seqs.length; i++)
115 String descr = seqs[i].getDescription();
119 boolean added = false;
121 while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
123 pos = pattern.matchedTo();
124 for (int cols = 0; cols < pattern.numSubs(); cols++)
126 String sstring = pattern.stringMatched(cols + 1);
127 double score = Double.NaN;
130 score = new Double(sstring).doubleValue();
131 } catch (Exception e)
133 // don't try very hard to parse if regex was wrong.
136 // add score to sequence annotation.
137 AlignmentAnnotation an = new AlignmentAnnotation(ScoreNames[cols]
138 + ((reps > 0) ? "_" + reps : ""),
139 ScoreDescriptions[cols], null);
141 System.out.println("Score: " + ScoreNames[cols] + "=" + score); // DEBUG
142 an.setSequenceRef(seqs[i]);
143 seqs[i].addAlignmentAnnotation(an);
144 al.addAnnotation(an);
147 reps++; // repeated matches
157 public static void main(String argv[])
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.");