package jalview.analysis;
import com.stevesoft.pat.Regex;
import jalview.datamodel.*;
public class ParseProperties
{
/**
* Methods for parsing free text properties on alignments and sequences.
* There are a number of ways we might want to do this:
* arbitrary regex. and an associated score name for the number that's extracted.
* Regex that provides both score and name.
*
* We may also want to :
* - modify description to remove parsed numbers (this behaviour is dangerous since exporting the alignment would lose the original form then)
* -
*
*/
/**
* The alignment being operated on
*/
private AlignmentI al=null;
/**
* initialise a new property parser
* @param al
*/
public ParseProperties(AlignmentI al) {
this.al = al;
}
public int getScoresFromDescription(String ScoreName, String ScoreDescriptions, String regex)
{
return getScoresFromDescription(new String[] { ScoreName }, new String[] { ScoreDescriptions}, regex);
}
public int getScoresFromDescription(String[] ScoreNames, String[] ScoreDescriptions, String regex)
{
return getScoresFromDescription(al.getSequencesArray(), ScoreNames, ScoreDescriptions, regex);
}
/**
* Extract scores for sequences by applying regex to description string.
* @param seqs seuqences to extract annotation from.
* @param ScoreNames labels for each numeric field in regex match
* @param ScoreDescriptions description for each numeric field in regex match
* @param regex Regular Expression string for passing to new com.stevesoft.patt.Regex(regex)
* @return total number of sequences that matched the regex
*/
public int getScoresFromDescription(SequenceI[] seqs, String[] ScoreNames, String[] ScoreDescriptions, String regex)
{
int count=0;
Regex pattern = new Regex(regex);
if (pattern.numSubs()>ScoreNames.length)
{
// Check that we have enough labels and descriptions for any parsed scores.
int onamelen = ScoreNames.length;
String[] tnames = new String[pattern.numSubs()+1];
System.arraycopy(ScoreNames, 0, tnames, 0, ScoreNames.length);
String base = tnames[ScoreNames.length-1];
ScoreNames = tnames;
String descrbase = ScoreDescriptions[onamelen-1];
if (descrbase == null)
descrbase = "Score parsed from ("+regex+")";
tnames = new String[pattern.numSubs()+1];
System.arraycopy(ScoreDescriptions, 0, tnames, 0, ScoreDescriptions.length);
ScoreDescriptions = tnames;
for (int i=onamelen; i