Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.analysis

File ParseProperties.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
54% of files have more coverage

Code metrics

18
46
4
1
164
97
16
0.35
11.5
4
4

Classes

Class Line # Actions
ParseProperties 29 46 16
0.00%
 

Contributing tests

No tests hitting this source file were found.

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   
27    import com.stevesoft.pat.Regex;
28   
 
29    public class ParseProperties
30    {
31    /**
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.
36    *
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) -
40    *
41    */
42    /**
43    * The alignment being operated on
44    */
45    private AlignmentI al = null;
46   
47    /**
48    * initialise a new property parser
49    *
50    * @param al
51    */
 
52  0 toggle public ParseProperties(AlignmentI al)
53    {
54  0 this.al = al;
55    }
56   
 
57  0 toggle public int getScoresFromDescription(String ScoreName,
58    String ScoreDescriptions, String regex, boolean repeat)
59    {
60  0 return getScoresFromDescription(new String[] { ScoreName },
61    new String[]
62    { ScoreDescriptions }, regex, repeat);
63    }
64   
 
65  0 toggle public int getScoresFromDescription(String[] ScoreNames,
66    String[] ScoreDescriptions, String regex, boolean repeat)
67    {
68  0 return getScoresFromDescription(al.getSequencesArray(), ScoreNames,
69    ScoreDescriptions, regex, repeat);
70    }
71   
72    /**
73    * Extract scores for sequences by applying regex to description string.
74    *
75    * @param seqs
76    * seuqences to extract annotation from.
77    * @param ScoreNames
78    * labels for each numeric field in regex match
79    * @param ScoreDescriptions
80    * description for each numeric field in regex match
81    * @param regex
82    * Regular Expression string for passing to
83    * <code>new com.stevesoft.patt.Regex(regex)</code>
84    * @param repeat
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
88    */
 
89  0 toggle public int getScoresFromDescription(SequenceI[] seqs, String[] ScoreNames,
90    String[] ScoreDescriptions, String regex, boolean repeat)
91    {
92  0 int count = 0;
93  0 Regex pattern = new Regex(regex);
94  0 if (pattern.numSubs() > ScoreNames.length)
95    {
96    // Check that we have enough labels and descriptions for any parsed
97    // scores.
98  0 int onamelen = ScoreNames.length;
99  0 String[] tnames = new String[pattern.numSubs() + 1];
100  0 System.arraycopy(ScoreNames, 0, tnames, 0, ScoreNames.length);
101  0 String base = tnames[ScoreNames.length - 1];
102  0 ScoreNames = tnames;
103  0 String descrbase = ScoreDescriptions[onamelen - 1];
104  0 if (descrbase == null)
105    {
106  0 descrbase = "Score parsed from (" + regex + ")";
107    }
108  0 tnames = new String[pattern.numSubs() + 1];
109  0 System.arraycopy(ScoreDescriptions, 0, tnames, 0,
110    ScoreDescriptions.length);
111  0 ScoreDescriptions = tnames;
112  0 for (int i = onamelen; i < ScoreNames.length; i++)
113    {
114  0 ScoreNames[i] = base + "_" + i;
115  0 ScoreDescriptions[i] = descrbase + " (column " + i + ")";
116    }
117    }
118  0 for (int i = 0; i < seqs.length; i++)
119    {
120  0 String descr = seqs[i].getDescription();
121  0 if (descr == null)
122    {
123  0 continue;
124    }
125  0 int pos = 0;
126  0 boolean added = false;
127  0 int reps = 0;
128  0 while ((repeat || pos == 0) && pattern.searchFrom(descr, pos))
129    {
130  0 pos = pattern.matchedTo();
131  0 for (int cols = 0; cols < pattern.numSubs(); cols++)
132    {
133  0 String sstring = pattern.stringMatched(cols + 1);
134  0 double score = Double.NaN;
135  0 try
136    {
137  0 score = Double.valueOf(sstring).doubleValue();
138    } catch (Exception e)
139    {
140    // don't try very hard to parse if regex was wrong.
141  0 continue;
142    }
143    // add score to sequence annotation.
144  0 AlignmentAnnotation an = new AlignmentAnnotation(
145  0 ScoreNames[cols] + ((reps > 0) ? "_" + reps : ""),
146    ScoreDescriptions[cols], null);
147  0 an.setScore(score);
148  0 jalview.bin.Console.outPrintln(seqs[i].getName() + " score: '"
149    + ScoreNames[cols] + "' = " + score); // DEBUG
150  0 an.setSequenceRef(seqs[i]);
151  0 seqs[i].addAlignmentAnnotation(an);
152  0 al.addAnnotation(an);
153  0 added = true;
154    }
155  0 reps++; // repeated matches
156    }
157  0 if (added)
158    {
159  0 count++;
160    }
161    }
162  0 return count;
163    }
164    }