X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FParseProperties.java;h=3af6c240e614b583e78b4684441e5165224895de;hb=9d67f04acb1c0ae097b918b09a997c5561592880;hp=79c1325aab21b6efed5177d827e1b47bf0b25dba;hpb=f683f4d6c8af43be0ffeb96513b52572619efed5;p=jalview.git diff --git a/src/jalview/analysis/ParseProperties.java b/src/jalview/analysis/ParseProperties.java index 79c1325..3af6c24 100644 --- a/src/jalview/analysis/ParseProperties.java +++ b/src/jalview/analysis/ParseProperties.java @@ -1,3 +1,20 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6) + * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + */ package jalview.analysis; import com.stevesoft.pat.Regex; @@ -7,102 +24,124 @@ 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. + * 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) - * - + * 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; - + private AlignmentI al = null; + /** * initialise a new property parser + * * @param al */ - public ParseProperties(AlignmentI al) { + public ParseProperties(AlignmentI al) + { this.al = al; } - public int getScoresFromDescription(String ScoreName, String ScoreDescriptions, String regex, boolean repeat) + public int getScoresFromDescription(String ScoreName, + String ScoreDescriptions, String regex, boolean repeat) { - return getScoresFromDescription(new String[] { ScoreName }, new String[] { ScoreDescriptions}, regex, repeat); + return getScoresFromDescription(new String[] + { ScoreName }, new String[] + { ScoreDescriptions }, regex, repeat); } - public int getScoresFromDescription(String[] ScoreNames, String[] ScoreDescriptions, String regex, boolean repeat) + public int getScoresFromDescription(String[] ScoreNames, + String[] ScoreDescriptions, String regex, boolean repeat) { - return getScoresFromDescription(al.getSequencesArray(), ScoreNames, ScoreDescriptions, regex, repeat); + return getScoresFromDescription(al.getSequencesArray(), ScoreNames, + ScoreDescriptions, regex, repeat); } + /** * 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) - * @param repeat true means the regex will be applied multiple times along the description string of each sequence + * + * @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) + * @param repeat + * true means the regex will be applied multiple times along the + * description string of each sequence * @return total number of sequences that matched the regex */ - public int getScoresFromDescription(SequenceI[] seqs, String[] ScoreNames, String[] ScoreDescriptions, String regex, boolean repeat) + public int getScoresFromDescription(SequenceI[] seqs, + String[] ScoreNames, String[] ScoreDescriptions, String regex, + boolean repeat) { - int count=0; + int count = 0; Regex pattern = new Regex(regex); - if (pattern.numSubs()>ScoreNames.length) + if (pattern.numSubs() > ScoreNames.length) { - // Check that we have enough labels and descriptions for any parsed scores. + // Check that we have enough labels and descriptions for any parsed + // scores. int onamelen = ScoreNames.length; - String[] tnames = new String[pattern.numSubs()+1]; + String[] tnames = new String[pattern.numSubs() + 1]; System.arraycopy(ScoreNames, 0, tnames, 0, ScoreNames.length); - String base = tnames[ScoreNames.length-1]; + String base = tnames[ScoreNames.length - 1]; ScoreNames = tnames; - String descrbase = ScoreDescriptions[onamelen-1]; + 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); + 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; i0) ? "_"+reps : ""), ScoreDescriptions[cols], null); + AlignmentAnnotation an = new AlignmentAnnotation(ScoreNames[cols] + + ((reps > 0) ? "_" + reps : ""), + ScoreDescriptions[cols], null); an.setScore(score); - System.out.println("Score: "+ScoreNames[cols]+"="+score); // DEBUG + System.out.println("Score: " + ScoreNames[cols] + "=" + score); // DEBUG an.setSequenceRef(seqs[i]); seqs[i].addAlignmentAnnotation(an); al.addAnnotation(an); - added=true; + added = true; } reps++; // repeated matches } @@ -111,24 +150,31 @@ public class ParseProperties count++; } } - return count; + return count; } - public static void main(String argv[]) { - SequenceI[] seqs = new SequenceI[] { new Sequence("sq1","THISISAPLACEHOLDER"), - new Sequence("sq2","THISISAPLACEHOLDER"), - new Sequence("sq3","THISISAPLACEHOLDER"), - new Sequence("sq4","THISISAPLACEHOLDER")}; + + public static void main(String argv[]) + { + SequenceI[] seqs = new SequenceI[] + { new Sequence("sq1", "THISISAPLACEHOLDER"), + new Sequence("sq2", "THISISAPLACEHOLDER"), + new Sequence("sq3", "THISISAPLACEHOLDER"), + new Sequence("sq4", "THISISAPLACEHOLDER") }; seqs[0].setDescription("1 mydescription1"); seqs[1].setDescription("mydescription2"); seqs[2].setDescription("2. 0.1 mydescription3"); seqs[3].setDescription("3 0.01 mydescription4"); - //seqs[4].setDescription("5 mydescription5"); + // seqs[4].setDescription("5 mydescription5"); Alignment al = new Alignment(seqs); ParseProperties pp = new ParseProperties(al); String regex = ".*([-0-9.+]+)"; - System.out.println("Matched "+pp.getScoresFromDescription("my Score", "my Score Description",regex, true)+" for "+regex); + System.out.println("Matched " + + pp.getScoresFromDescription("my Score", + "my Score Description", regex, true) + " for " + regex); regex = ".*([-0-9.+]+).+([-0-9.+]+).*"; - System.out.println("Matched "+pp.getScoresFromDescription("my Score", "my Score Description",regex, true)+" for "+regex); + System.out.println("Matched " + + pp.getScoresFromDescription("my Score", + "my Score Description", regex, true) + " for " + regex); System.out.println("Finished."); } }