#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Group together elements of arraylist


    Hello all,

    I'm trying to group elements of an array list but struggling to do so. I'm reading in a file called "Metabolites" and then using data from two of its columns. Those columns look something like this:

    cid=

    Code:
    CPD0255CPD1224CPD1227CPD0947CPD0149CPD1036CPD1064CPD0420CPD1035
    And cn=

    Code:
    AcetamideAcetamide[e]Acetamide[p]AcetateAcetate[e]Acetate[p]AcetoacetateAcetoacetate[e]Acetoacetate[p]AcetoacetylPhosphinothricin
    You can see than in cn, we have a compound name, followed by the same name[e] followed by same name[p].

    I want to take these groups of three and write their corresponding cids together. Note that not every element in cn is involved in such a group.

    The following is my attempt. I read the first cn and store it in an arraylist "test." Then I read the next cn. If it matches with the what I have in test, then I write those two cns and their ids together. If not, I clear the list and add the next cn. That should work right?

    Well, what am I doing wrong?


    Code:
    //cc.java
    
    
    
    import java.io.*;
    
    import java.util.Arrays;
    
    import java.util.ArrayList;
    
    import java.util.List;
    
    import java.util.regex.*;
    
    
    
    
    
    
    
    public class cc{
    
    
    
     public static void main(String[] arg) throws Exception {
    
    
    
    
    
    BufferedWriter b1 = 
    
    	new BufferedWriter(new FileWriter("cc.gms"));//This creates the GAMS file "cc"
    
    
    
    
    BufferedReader b2 = 
    
    	new BufferedReader(new FileReader("Metabolites.txt"));//This reads in information from the file "Metabolites.txt".
    
    
    ArrayList<String> test = new ArrayList<String>();
    
    
    
    String strRead; String strWrite;
    
    
    
    
    
    while ((strRead=b2.readLine())!=null){
    
     String splitarray[] = strRead.split("\t");//This breaks up the .csv file into columns.
    
     String cid = splitarray[0];//This defines "reactions" as everything in column 0 of "Reactions.csv".
    
     String cn = splitarray[2];
    
     String list="";
    
    
    
    for (String t : test)
    
    {
    
        list += t;
    
    }
    
    if(test.isEmpty()){
    
    test.add(cn);
    
    
    
    }
    
    
    if(test.contains(cn+"\\[e\\]"))
    
    {b1.write(cn+cid);b1.newLine();}
    
    else{
    
    test.clear();
    test.add(cn);}
    
    
    
    }
    
    b1.close();
    
    
    
    
    
    
    
    
    
     } //main()
    
    } //
    Thanks for your help!

    kmath
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post the current output from the program and also the desired output?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by NormR
    Can you post the current output from the program and also the desired output?

    The current output is (for the example section):


    Code:
    lumazine7 Methyl 3 oxo 6 octenoyl CoAAcetaldehydeAcetaldehyde[e]Acetaldehyde[p]AcetamideAcetamide[e]Acetamide[p]AcetateAcetate[e]Acetate[p]AcetoacetateAcetoacetate[e]Acetoacetate[p]Acetoacetyl
    The desired output is (for this same section):


    Code:
    AcetaldehydeAcetaldehyde[e]Acetaldehyde[p
    AcetamideAcetamide[e]Acetamide[p]
    AcetateAcetate[e]Acetate[p]
    AcetoacetateAcetoacetate[e]Acetoacetate[p]
    That is, I want elements listed only if there's a name, followed by that name[e], followed by that name[p]. Each group of three should be on its own line.

    Eventually I want the output to look like the above but instead of listing cns, as above, I want to print the cid of each cn. That part should be easy though I can can achieve the above output. So let's start with that.

    Does that make sense?

    Thanks
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post a version of the program for testing that has a small sample of data in the program instead of requiring separate files?
    Use the StringReader class inside the BufferedReader class:
    Code:
    BufferedReader br = new BufferedReader(new StringReader(<... input data here ...>));
    Use \n in the String in the StringReader constructor where the code has lineends.
    That will make the testing easier.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by NormR
    Can you post a version of the program for testing that has a small sample of data in the program instead of requiring separate files?
    Use the StringReader class inside the BufferedReader class:
    Code:
    BufferedReader br = new BufferedReader(new StringReader(<... input data here ...>));
    Use \n in the String in the StringReader constructor where the code has lineends.
    That will make the testing easier.
    How about this?

    Code:
    //cc.java
    
    
    
    
    
    import java.io.*;
    
    import java.util.Arrays;
    
    import java.util.ArrayList;
    
    import java.util.List;
    
    import java.util.regex.*;
    
    
    
    
    
    
    
    public class cc{
    
    
    
     public static void main(String[] arg) throws Exception {
    
    
    
    
    
    
    BufferedReader br = new BufferedReader(new StringReader("lumazine7\nMethyl\n3\noxo\n6\noctenoyl\nCoA\nAcetaldehyde\nAcetaldehyde[e]\nAcetaldehyde[p]\nAcetamide\nAcetamide[e]\nAcetamide[p]\nAcetate\nAcetate\nAcetamide\nAcetamide[e]\nAcetamide[p]\nAcetate\nAcetate[e]\nAcetate[p]\nAcetoacetate\nAcetoacetate[e]\nAcetoacetate\nAcetoacetate[e]\nAcetoacetate[p]\nAcetoacetyl"));
    
    
    
    
    
    
    
    
    ArrayList<String> test = new ArrayList<String>();
    
    
    
    String strRead; String strWrite;
    
    
    
    
    
    while ((strRead=br.readLine())!=null){
    
    
     String cn = "lumazine7\nMethyl\n3\noxo\n6\noctenoyl\nCoA\nAcetaldehyde\nAcetaldehyde[e]\nAcetaldehyde[p]\nAcetamide\nAcetamide[e]\nAcetamide[p]\nAcetate\nAcetate\nAcetamide\nAcetamide[e]\nAcetamide[p]\nAcetate\nAcetate[e]\nAcetate[p]\nAcetoacetate\nAcetoacetate[e]\nAcetoacetate\nAcetoacetate[e]\nAcetoacetate[p]\nAcetoacetyl";
    
     String list="";
    
    
    
    
    
    
    
    
    for (String t : test)
    
    {
    
        list += t;
    
    }
    
    
    
    if(test.isEmpty()){
    
    
    
    test.add(cn);
    
    
    
    
    
    
    
    }
    
    
    
    if(test.contains(cn+"\\[e\\]")){
    
    
    
    System.out.println(cn);}
    
    
    
    else{
    
    if(test.contains(cn+"\\[p\\]")){
    
    System.out.println(cn);}
    
    
    
    else{
    
    test.clear();
    
    test.add(cn);}
    
    
    
    
    
    }}
    
    
    
    b1.close();br.close();
    
    
    
    
    
    
    
    
    
     } //main()
    
    } //
    By the way, this code does not produce any output

    But the desired output for this string is the same as I have listed above.

    Thanks for your help!
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please explain what the problem is with the posted code.
    Is is supposed to produce any output?
    what is the program's output supposed to look like?

    Try debugging the code by adding some calls to the println() statement to print out the values of variables as they are changed and used.


    Also the posted code needs to be properly formatted.
    Nested statements should be indented
    }s should NOT be hidden at the end of a statement. They should be on a line by themself.
    Last edited by NormR; June 27th, 2013 at 01:05 PM.

IMN logo majestic logo threadwatch logo seochat tools logo