Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.analysis

File ParseProperties.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

18
46
4
1
165
98
16
0.35
11.5
4
4

Classes

Class Line # Actions
ParseProperties 30 46 16
0.941176594.1%
 

Contributing tests

This file is covered by 3 tests. .

Source view

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