Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 9th, 2006, 11:16 AM   #1
BungalowBill
Programmer
 
Join Date: Dec 2005
Posts: 40
Rep Power: 0 BungalowBill is on a distinguished road
ArrayLists

Im writing a program to find out if a protein sequence has a turn in it (don't ask) and this is what i want to do.

I need to enter a sequence, at the terminal, i.e. CAENBVWGKL or something then i need to convert this string into an array to separate it all up and give each letter its own place in the array. The rest becomes very complicated biology stuff so i won't bore you with it. I just need to crack this part.

ArrayList sequence = new ArrayList

//hopefully, eventually a list like this will be made somewhere.

sequence[] = ["C","A","E","N" etc.]

So if i tried to get sequence[2] i would get "A" back.

It seems like it should be pretty straightforward but i can't figure it out.

Does anyone know of any good resources where i can see examples of this kind of thing? I feel like i'm going round in circles with google searching for "converting strings to arrayList" and various similar conbinations.

Any help?
BungalowBill is offline   Reply With Quote
Old Nov 9th, 2006, 11:34 AM   #2
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
Do you really need an ArrayList? Is sequence actually going to modified once it has been read? If not, it seems like a waste to use an ArrayList. If all you need is to be able to look up an index in the sequence you can the String class's charAt(index) method on your input string.
Kaja Fumei is offline   Reply With Quote
Old Nov 9th, 2006, 12:55 PM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
you could also look into ToCharArray() function that will convert a string into a character array. Wich seems to be exactly what you need.

It doesn't apear like you need an arraylist.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 9th, 2006, 2:03 PM   #4
Iftikhar
Programmer
 
Iftikhar's Avatar
 
Join Date: Oct 2006
Location: London
Posts: 40
Rep Power: 0 Iftikhar is on a distinguished road
Send a message via MSN to Iftikhar
You can convert striung to charcters array as suggested by xavier like this
[code]
char [] chrArray = strBiologyStuff.ToCharArray();
[\code]
Then still you want each chracter in arrayList then you can loop throught this array list using
foreach(char chr in chrArray)
 arrList.Add(chr);
__________________
Iftikhar Ahmed Khan
For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak
Iftikhar is offline   Reply With Quote
Old Nov 9th, 2006, 5:52 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I would use Python rather than Java for this sort of task.

In Java, however, you should only use an ArrayList over a char[] array if you will be adding/removing elements after you initially create the collection.

@Iftikhar: You miscapitalized "ToCharArray" in your code (it should be "toCharArray"). As for your enhanced for loop, the keyword is still "for", not "foreach", and the separator is a colon, not "in". Also, the add method for an ArrayList is "add", not "Add". Next time please ensure that your code compiles and runs successfully before posting.

// The original sequence as a String
String sequence = "CAENBVWGKL";

// Convert it to an array of char
char[] seqChars = sequence.toCharArray();

// Now you can easily access each character
System.out.println(seqChars[0]);

// If you want an ArrayList instead
ArrayList<Character> seqList = new ArrayList<Character>();
for (char c : seqChars)
    seqList.add(c); // Add each character to the list
titaniumdecoy is offline   Reply With Quote
Old Nov 9th, 2006, 11:24 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by titaniumdecoy View Post
@Iftikhar: ...
Damn that C#, eh? :beard:

On subject, you could either do it with the ArrayList or a char[] as suggested, but if you eventually want to look at substrings as well (e.g. see if ABAB occurred), you might consider keeping it as a string and using charAt(). That way you can use substring() for finding the substrings, rather than selecting a subset of an array[list].
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Nov 10th, 2006, 6:55 AM   #7
BungalowBill
Programmer
 
Join Date: Dec 2005
Posts: 40
Rep Power: 0 BungalowBill is on a distinguished road
Alright thanks for that guys. The reason i was going to use an arraylist was that later on im gonna have to make similar lists for indivdual amino acids which include ints and i thought it would be better to use arraylists which could handle both strings and integers. For example:-

the amino acid Alanine, just A for short would have a list like this.

A[142,83,66,0.06,0.076,0.035,0.058]

(If you're interested about what those numbers are, the first one is the P(a) which is the propensity of this particular ammino acid to participate in alpha helices. The next is P(b), beta sheets and it goes on like that.

I plan to have 20 of these lists, one for each of the most common amino acids which i want to mantipulate to pick out certain values for formulas.

Could i just declare each list at the start of the file? ie

ArrayList A = new ArrayList
ArrayList R = new ArrayList
etc..

A[142,83,66,0.06,0.076,0.035,0.058]
R[98,93,95,0.07,0.106,0.099,0.085]
etc..


then access the different values as i wish with if statements?

if(i.equals("A")) {
  x = A[1]; 
}

which would hopefully set the x value to 142, to be used in a later calulation.
What do you guys think?
BungalowBill is offline   Reply With Quote
Old Nov 10th, 2006, 2:58 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You could use a map to associate each letter with an array of numbers, as demonstrated below. This way there is no need to use if statements to extract values. You might also consider reading the values from a file rather than hard-coding them to make the program more flexible.

HashMap<Character, double[]> map = new HashMap<Character, double[]>();

map.put('A', new double[] { 142,83,66,0.06,0.076,0.035,0.058 });
map.put('R', new double[] { 98,93,95,0.07,0.106,0.099,0.085 });

double x = map.get('A')[0]; // Sets x to the first value in A (142)
double y = map.get('R')[2]; // Sets y to the third value in R (95)
titaniumdecoy is offline   Reply With Quote
Old Nov 11th, 2006, 7:08 AM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,036
Rep Power: 5 lectricpharaoh will become famous soon enough
Another thing you could do is write a class to represent amino acids. It would have methods to get the values associated with a particular amino acid (or you could just expose them as public final constants), an enum definition for the various acids (so that you could not construct an object except for a valid amino acid), and a variable storing the enum value for this particular instance. Assuming you implement the proper interfaces, such as Comparable, you could then create an array of the suckers, and sort, search, etc the array.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Nov 11th, 2006, 12:19 PM   #10
Iftikhar
Programmer
 
Iftikhar's Avatar
 
Join Date: Oct 2006
Location: London
Posts: 40
Rep Power: 0 Iftikhar is on a distinguished road
Send a message via MSN to Iftikhar
I think some one already the language I used is C Sharp. Pretty much like Java . That is a good thing also.
__________________
Iftikhar Ahmed Khan
For doing an experiment on programmer's mood please visit http://uxisfyp1.brunel.ac.uk/cspgiak
Iftikhar is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC