2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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 jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
27 import com.stevesoft.pat.Regex;
29 public class ParseProperties
32 * Methods for parsing free text properties on alignments and sequences. There
33 * are a number of ways we might want to do this: arbitrary regex. and an
34 * associated score name for the number that's extracted. Regex that provides
35 * both score and name.
37 * We may also want to : - modify description to remove parsed numbers (this
38 * behaviour is dangerous since exporting the alignment would lose the
39 * original form then) -
43 * The alignment being operated on
45 private AlignmentI al = null;
48 * initialise a new property parser
52 public ParseProperties(AlignmentI al)
57 public int getScoresFromDescription(String ScoreName,
58 String ScoreDescriptions, String regex, boolean repeat)
60 return getScoresFromDescription(new String[] { ScoreName },
62 { ScoreDescriptions }, regex, repeat);
65 public int getScoresFromDescription(String[] ScoreNames,
66 String[] ScoreDescriptions, String regex, boolean repeat)
68 return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
69 ScoreDescriptions, regex, repeat);
73 * Extract scores for sequences by applying regex to description string.
76 * seuqences to extract annotation from.
78 * labels for each numeric field in regex match
79 * @param ScoreDescriptions
80 * description for each numeric field in regex match
82 * Regular Expression string for passing to
83 * <code>new com.stevesoft.patt.Regex(regex)</code>
85 * true means the regex will be applied multiple times along the
86 * description string of each sequence
87 * @return total number of sequences that matched the regex
89 public int getScoresFromDescription(SequenceI[] seqs, String[] ScoreNames,
90 String[] ScoreDescriptions, String regex, boolean repeat)
93 Regex pattern = new Regex(regex);
94 if (pattern.numSubs() > ScoreNames.length)
96 // Check that we have enough labels and descriptions for any parsed
98 int onamelen = ScoreNames.length;
99 String[] tnames = new String[pattern.numSubs() + 1];
100 System.arraycopy(ScoreNames, 0, tnames, 0, ScoreNames.length);
101 String base = tnames[ScoreNames.length - 1];
103 String descrbase = ScoreDescriptions[onamelen - 1];
104 if (descrbase == null)
106 descrbase = "Score parsed from (" + regex + ")";
108 tnames = new String[pattern.numSubs() + 1];
109 System.arraycopy(ScoreDescriptions, 0, tnames, 0,
110 ScoreDescriptions.length);
111 ScoreDescriptions = tnames;
112 for (int i = onamelen; i < ScoreNames.length; i++)
114 ScoreNames[i] = base + "_" + i;
115 ScoreDescriptions[i] = descrbase + " (column " + i + ")";
118 for (int i = 0; i < seqs.length; i++)
120 String descr = seqs[i].getDescription();
126 boolean added = false;
128 while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
130 pos = pattern.matchedTo();
131 for (int cols = 0; cols < pattern.numSubs(); cols++)
133 String sstring = pattern.stringMatched(cols + 1);
134 double score = Double.NaN;
137 score = Double.valueOf(sstring).doubleValue();
138 } catch (Exception e)
140 // don't try very hard to parse if regex was wrong.
143 // add score to sequence annotation.
144 AlignmentAnnotation an = new AlignmentAnnotation(
145 ScoreNames[cols] + ((reps > 0) ? "_" + reps : ""),
146 ScoreDescriptions[cols], null);
148 System.out.println(seqs[i].getName() + " score: '"
149 + ScoreNames[cols] + "' = " + score); // DEBUG
150 an.setSequenceRef(seqs[i]);
151 seqs[i].addAlignmentAnnotation(an);
152 al.addAnnotation(an);
155 reps++; // repeated matches