Extracting information from String (regex, Beanshell)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mokita
    New Member
    • Mar 2007
    • 11

    #1

    Extracting information from String (regex, Beanshell)

    Hello,

    I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a string, separated by newline. For this i am using regex.

    The String is given:

    P48534
    EXP value is: e-10
    Q0543
    EXP value is: 4e-07


    My script look like this in Beanshell:

    [CODE=java] import java.util.regex .Matcher;
    import java.util.regex .Pattern;

    Pattern pGI = Pattern.compile ("(^.*?$)");
    Pattern pEvaLue = Pattern.compile ("is: (.*)$");
    Matcher mGI;
    Matcher mEvalue;
    StringBuffer temp = new StringBuffer();
    String [] line = BlastReport.spl it("\n");
    int arraysize = line.length;

    for (int i=0; i<(arraysize-1); i+=2){
    String sGI = line[i];
    String sEvalue = line[i+1];
    mGI = pGI.matcher(sGI );
    mEvalue = pEvalue.matcher (sEvalue);
    String gi="";

    if (mGI.find()){
    gi =mGI.group(1);
    }
    if (mEvalue.find() ){
    String eval = mEvalue.group(1 );
    if(eval.startsW ith("e")){
    eval= "1".concat(eval );
    }
    Double d = new Double (eval);
    double Evalue = d.doubleValue() ;
    if (Evalue<=0.02){
    temp.append(gi + "\n");
    }
    }
    }

    String result = temp.toString() .trim();[/CODE]


    The error message is: "Attempt to resolve method: matcher() on undefined variable or class name: pEvalue: at Line: 16: pEvalue .matcher(sEvalu e)"


    Can someone tell me why is giving me this error and how can i fix it.

    Thank you in advance,

    Mokita
    Last edited by prometheuzz; Aug 6 '07, 03:30 PM. Reason: Wrapped some code-tags around it.
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Originally posted by Mokita
    Hello,

    I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a string, separated by newline. For this i am using regex.

    The String is given:

    P48534
    EXP value is: e-10
    Q0543
    EXP value is: 4e-07

    ...
    It looks like you're trying to capture the text after the is:, right?
    Try this:
    [CODE=java] String text = "P48534\nEX P value is: e-10\nQ0543\nEXP value is: 4e-07";
    Pattern pattern = Pattern.compile ("(?<=is:\\s )[^\\n]+");
    Matcher matcher = pattern.matcher (text);

    while(matcher.f ind()) {
    System.out.prin tln(matcher.gro up());
    }[/CODE]

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Which one is your line 16?

      Comment

      • Mokita
        New Member
        • Mar 2007
        • 11

        #4
        My line 16 is:
        mEvalue = pEvalue.matcher (sEvalue);

        Mokita

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Mokita
          My line 16 is:
          mEvalue = pEvalue.matcher (sEvalue);

          Mokita
          Java is case sensitive.
          Now give yourself a kick.

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by Mokita
            My line 16 is:
            mEvalue = pEvalue.matcher (sEvalue);

            Mokita
            That splitting of your String and increasing with 2 in your for loop looks dangerous. Perhaps I (or someone else) can suggest a better approach. But then you first need to explain what is you're trying to do.

            So given the String: [CODE=java]"P48534\nEX P value is: e-10\nQ0543\nEXP value is: 4e-07"[/CODE]what is it you're trying to extract and/or group?

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #7
              Originally posted by Mokita
              My line 16 is:
              mEvalue = pEvalue.matcher (sEvalue);

              Mokita
              Try this:
              [CODE=java]String text = "P48534\nEX P value is: e-10\nQ0543\nEXP value is: 4e-07";
              Pattern pattern = Pattern.compile ("([A-Z]\\d+).*\\n?.*(( ?<=is:\\s)[^\\n]+)");
              Matcher matcher = pattern.matcher (text);

              System.out.prin tln(text+"\n");

              while(matcher.f ind()) {
              String id = matcher.group(1 );
              String sVal = matcher.group(2 );
              sVal = sVal.startsWith ("e") ? 1+sVal : sVal;
              double dVal = Double.parseDou ble(sVal);
              System.out.prin tln(id+"\t"+dVa l);
              }[/CODE]

              Comment

              • Mokita
                New Member
                • Mar 2007
                • 11

                #8
                Originally posted by prometheuzz
                That splitting of your String and increasing with 2 in your for loop looks dangerous. Perhaps I (or someone else) can suggest a better approach. But then you first need to explain what is you're trying to do.

                So given the String: [CODE=java]"P48534\nEX P value is: e-10\nQ0543\nEXP value is: 4e-07"[/CODE]what is it you're trying to extract and/or group?

                I am trying to do a workflow with taverna, which has a beanshell, where i can write a script in it.
                In my workflow i will have the output of a blast search, GI number, which are the P23234 or Q12344 or A12443 or only numbers and also a E-value, which is the EXP vaule is: e-10.
                From that output i want to extract the GI numbers which have an e-value<= 0.02. The way i thought i could extract was with regular expressions in java, but the way i wrote the script it is not working.

                I think it is more clear what i want to do, but if you need more explanation please ask.

                Mokita

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by Mokita
                  I am trying to do a workflow with taverna, which has a beanshell, where i can write a script in it.
                  In my workflow i will have the output of a blast search, GI number, which are the P23234 or Q12344 or A12443 or only numbers and also a E-value, which is the EXP vaule is: e-10.
                  From that output i want to extract the GI numbers which have an e-value<= 0.02. The way i thought i could extract was with regular expressions in java, but the way i wrote the script it is not working.

                  I think it is more clear what i want to do, but if you need more explanation please ask.

                  Mokita
                  I don't think regex is best for this (I could be wrong of course). You are not searching for a pattern (which is where I mostly use my regex) but you are searching for numbers within some range.
                  P.S I hope you managed to correct the spelling mistake for that variable.

                  Comment

                  • Mokita
                    New Member
                    • Mar 2007
                    • 11

                    #10
                    Hello

                    I want to thank you for your help, it is working. I also want to ask you a quick question.
                    How can i change group 1 ([A-Z]\\d+) to catch: P00DF3 or 1234653 or Q5647GJD or A4658DF
                    It is not catching the ones with letters in the midle.

                    Thank you again,

                    Mokita

                    Comment

                    • prometheuzz
                      Recognized Expert New Member
                      • Apr 2007
                      • 197

                      #11
                      Originally posted by Mokita
                      ...
                      How can i change group 1 ([A-Z]\\d+) to catch: P00DF3 or 1234653 or Q5647GJD or A4658DF
                      It is not catching the ones with letters in the midle.

                      Thank you again,

                      Mokita
                      Try replacing "([A-Z]\\d+)" with "(\\w+)"

                      Comment

                      Working...