2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.analysis;
23 import com.stevesoft.pat.Regex;
25 import jalview.datamodel.*;
27 public class ParseProperties
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.
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) -
41 * The alignment being operated on
43 private AlignmentI al = null;
46 * initialise a new property parser
50 public ParseProperties(AlignmentI al)
55 public int getScoresFromDescription(String ScoreName,
56 String ScoreDescriptions, String regex, boolean repeat)
58 return getScoresFromDescription(new String[]
59 { ScoreName }, new String[]
60 { ScoreDescriptions }, regex, repeat);
63 public int getScoresFromDescription(String[] ScoreNames,
64 String[] ScoreDescriptions, String regex, boolean repeat)
66 return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
67 ScoreDescriptions, regex, repeat);
71 * Extract scores for sequences by applying regex to description string.
74 * seuqences to extract annotation from.
76 * labels for each numeric field in regex match
77 * @param ScoreDescriptions
78 * description for each numeric field in regex match
80 * Regular Expression string for passing to
81 * <code>new com.stevesoft.patt.Regex(regex)</code>
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
87 public int getScoresFromDescription(SequenceI[] seqs,
88 String[] ScoreNames, String[] ScoreDescriptions, String regex,
92 Regex pattern = new Regex(regex);
93 if (pattern.numSubs() > ScoreNames.length)
95 // Check that we have enough labels and descriptions for any parsed
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];
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++)
111 ScoreNames[i] = base + "_" + i;
112 ScoreDescriptions[i] = descrbase + " (column " + i + ")";
115 for (int i = 0; i < seqs.length; i++)
117 String descr = seqs[i].getDescription();
121 boolean added = false;
123 while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
125 pos = pattern.matchedTo();
126 for (int cols = 0; cols < pattern.numSubs(); cols++)
128 String sstring = pattern.stringMatched(cols + 1);
129 double score = Double.NaN;
132 score = new Double(sstring).doubleValue();
133 } catch (Exception e)
135 // don't try very hard to parse if regex was wrong.
138 // add score to sequence annotation.
139 AlignmentAnnotation an = new AlignmentAnnotation(ScoreNames[cols]
140 + ((reps > 0) ? "_" + reps : ""),
141 ScoreDescriptions[cols], null);
143 System.out.println("Score: " + ScoreNames[cols] + "=" + score); // DEBUG
144 an.setSequenceRef(seqs[i]);
145 seqs[i].addAlignmentAnnotation(an);
146 al.addAnnotation(an);
149 reps++; // repeated matches
159 public static void main(String argv[])
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.");