Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jim829

Thread: Can this be improved?

  1. #1
    jazzmasterkc is offline Member
    Join Date
    Sep 2013
    Location
    Lincoln, NE
    Posts
    5
    Rep Power
    0

    Default Can this be improved?

    Hello - I have completed an assignment in my CS class that the goal was to read a txt file and output the amino acid of DNA. I'm in CS 2, or a basic class so all that is really cared about in this assignment is to read the txt file and make a correct output. However, I would like to know if I can improve this in anyway, as it doesn't seem very efficient.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    
    public class DnaSequencer {
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		File f = new File(args[0]);		
    		String txt = new Scanner(f).useDelimiter("X").next();
    		String txt2 = txt.replaceAll("T", "U");
    		
    		List<String> listTxt = new ArrayList<String>();
    		int count = 0;
    		while (count < txt2.length()) {
    			listTxt.add(txt2.substring(count, Math.min(count+3, txt2.length())));
    			count += 3;
    		}
    		
    		String[] arrayTxt = new String[listTxt.size()];
    		listTxt.toArray(arrayTxt);
    		
    		char[] arrayAcid = new char[listTxt.size()];
    		
    		count = 0;
    		for(String i : listTxt) {
    			if (i == "UAA" || i == "UAG" || i == "UGA") {
    				System.out.println(arrayAcid);
    			} else if(i.equals("GCU") || i.equals("GCC") || i.equals("GCA") || i.equals("GCG")) {
    				arrayAcid[count] = 'A';
    			} else if(i.equals("UGU") || i.equals("UGC")) {
    				arrayAcid[count] = 'C';
    			} else if(i.equals("GAU") || i.equals("GAC")) {
    				arrayAcid[count] = 'D';
    			} else if(i.equals("GAA") || i.equals("GAG")) {
    				arrayAcid[count] = 'E';
    			} else if(i.equals("UUU") || i.equals("UUC")) {
    				arrayAcid[count] = 'F';
    			} else if(i.equals("GGU") || i.equals("GGC") || i.equals("GGA") || i.equals("GGG")) {
    				arrayAcid[count] = 'G';
    			} else if(i.equals("CAU") || i.equals("CAC")) {
    				arrayAcid[count] = 'H';
    			} else if(i.equals("AUU") || i.equals("AUC") || i.equals("AUA")) {
    				arrayAcid[count] = 'I';
    			} else if(i.equals("AAA") || i.equals("AAG")) {
    				arrayAcid[count] = 'K';
    			} else if(i.equals("UUA") || i.equals("UUG") || i.equals("CUU") || i.equals("CUC") || i.equals("CUA") || i.equals("CUG")) {
    				arrayAcid[count] = 'L';
    			} else if(i.equals("AUG")) {
    				arrayAcid[count] = 'M';
    			} else if(i.equals("AAU") || i.equals("AAC")) {
    				arrayAcid[count] = 'N';
    			} else if(i.equals("CCU") || i.equals("CCC") || i.equals("CCA") || i.equals("CCG")) {
    				arrayAcid[count] = 'P';
    			} else if(i.equals("CAA") || i.equals("CAG")) {
    				arrayAcid[count] = 'Q';
    			} else if(i.equals("CGU") || i.equals("CGC") || i.equals("CGA") || i.equals("CGG") || i.equals("AGA") || i.equals("AGG")) {
    				arrayAcid[count] = 'R';
    			} else if(i.equals("UCU") || i.equals("UCC") || i.equals("UCA") || i.equals("UCG") || i.equals("AGU") || i.equals("AGC")) {
    				arrayAcid[count] = 'S';
    			} else if(i.equals("ACU") || i.equals("ACC") || i.equals("ACA") || i.equals("ACG")) {
    				arrayAcid[count] = 'T';
    			} else if(i.equals("GUU") || i.equals("GUC") || i.equals("GUA") || i.equals("GUG")) { 
    				arrayAcid[count] = 'V';
    			} else if(i.equals("UGG")) {
    				arrayAcid[count] = 'W';
    			} else if(i.equals("UAU") || i.equals("UAC")) {
    				arrayAcid[count] = 'Y';
    			}
    			count++;
    		}
    		
    		String acid = new String(arrayAcid);
    		System.out.println(acid);
    		
    	}
    }
    From what I understand 'multi dementional arrays' dont exist in java as they are 1-d 1-d arrays... unless I missed on how to map this better..
    Thanks

    Edit: a few notes about the program, the file name is argument from command line, and the txt contains letters like "AAAAAAAAAAAAAAA" and so forth.. What I'm more concerned about is if I could of structured this better...
    Last edited by jazzmasterkc; 09-09-2013 at 10:35 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Can this be improved?

    For a small "example" program, it looks okay. To make a "complete" program, each of those acid types ('A', etc) should probably be a class with a (lets say) "matches" method that takes the three character sequence as an argument, and the reading of the file and structuring of the array should be in its own method, but for such an "example/quick-and-dirty" program, its fine.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Can this be improved?

    As long as it works and is done, why change it? But for reference you could do the following:

    I assume based on my scan of the program that all your trigraphs are unique. Therefore, I would hard code them in a map where the trigraphs are the key and they point to the acid. Better still would be to read them in from an initialization file to initialize the map.

    You could also have used a switch statement and switch on the trigraphs with fall thrus on the groupings.

    Regards,
    Jim
    Last edited by jim829; 09-09-2013 at 08:50 PM.
    gimbal2 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Can this be improved?

    Agreed on the usage of a lookup map loaded from a file, that would make the code a lot easier on the eyes and the matrix easier to manage.

    I see some beginnings of business rules in this code. If this would become any form of complex it would enter the territory of for example JBoss Drools. But that is -very- advanced material.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jazzmasterkc is offline Member
    Join Date
    Sep 2013
    Location
    Lincoln, NE
    Posts
    5
    Rep Power
    0

    Default Re: Can this be improved?

    Ok, thanks for the advice. We were limited to being able to use only one class though. And wow, JBoss Drools looks like weeks looking at documentation haha.

    And Jim, using switch statement was my first thought but then I remembered how much my professor hates switch statements.. :p

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: Can this be improved?

    Did the professor say why s/he hates switch statements? I would guess they are fairly efficient since the JVM has special opcodes to handle them.

    Regards,
    Jim
    Last edited by jim829; 09-09-2013 at 08:48 PM. Reason: YAT
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can this be improved?

    Quote Originally Posted by jim829 View Post
    Did the professor say why he s/he hates switch statements?
    Because it's a sort of tinny word; 'break' however, is nice and woody: break! break! break! it's woody but switch, switch, switch; it's so tinny ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Can this be improved?

    Right-o, Professor Jos
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can this be improved?

    Quote Originally Posted by DarrylBurke View Post
    Right-o, Professor Jos
    "Right-o" ... "Right-o" ... another tinny word ...

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jazzmasterkc is offline Member
    Join Date
    Sep 2013
    Location
    Lincoln, NE
    Posts
    5
    Rep Power
    0

    Default Re: Can this be improved?

    Quote Originally Posted by jim829 View Post
    Did the professor say why s/he hates switch statements? I would guess they are fairly efficient since the JVM has special opcodes to handle them.

    Regards,
    Jim
    No he didn't :p

Similar Threads

  1. Replies: 0
    Last Post: 06-05-2013, 12:00 PM
  2. Replies: 0
    Last Post: 01-24-2012, 01:58 PM
  3. Improved version --MenuBar , menus & actions
    By Agrata in forum New To Java
    Replies: 3
    Last Post: 01-06-2012, 05:14 AM
  4. OK Guys! My new and improved code.
    By Meta in forum New To Java
    Replies: 4
    Last Post: 04-07-2010, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •