UPDATE: glad it worked :) If you 'd like to have more functionality added to the program, let me know. For now: you 're welcome & have a nice day Lisa :)
PS: if you 'd like to mail me the written report when it's done, my email is anthony_ve*hotm*ail*com (replace the 3 *'s first though ;), but you shouldn't feel obliged to do so, of course. Im glad i could help.
EDIT3: System.out is the default "outputstream", i.e. to write to the command prompt & System.in is the default "inputstream", i.e. to read from the command prompt. The Scanner class is just a good help to get a line (i.e. until the user presses enter) from a File, or inputstream (in this case: the default inputstream, i.e. command prompt input). Ok, & now im off to bed :)
EDIT2: Ah, i think i understand the problem now :) I 've made the inputting "interactive", so that the user has to input it at run-time. While you probably prefer to just put it into the code & then run the program? If so, look for this line in the main method: "String userInput = getStrandFromUser();" & replace it with something like: String userInput = "AGCTTCAGAT";
Hope this helps. Have a nice day :)
EDIT: i don't know if you use an editor or IDE or just the command prompt to run the program. But e.g. in the command prompt, it should give you a line saying "Please enter your strand: ". Then just start typing there. Or in the Eclipse IDE, there is a window on the bottom, called "Console", which acts much the same way as the command prompt. If you 're still unsure about where to input it, just send me a message or leave another comment here. Or if you 'd like, i could add a method to the program, which would allow you to read the input from a file, so that you would just have to create a simple text file, run the program, & get a text file containing the frequency table as a result. But it's 10:30pm here, so that 'd be something for tomorrow after i get back from work.
About the last thing: i surely wouldn't mind you doing that. In fact, that 'd be very kind of you Lisa. Btw, when you 're done with the research, would it be possible to get to read it maybe? :)
UPDATE: hello again Lisa. Below is an updated class. I 've added a way to export the frequency table to a file & put some things u might want to change in a section called "SETTINGS". There are 2 ways for input: let the pc generate a random strand or let the user input a strand. In the main method, i 've just put some example code to show what 's possible with the actual code. In the declaration of DEFAULT_OUTPUT_FILE, i 've had to include a space because the line got too long. Normally this shouldn't give problems, but if it gives you an error on that line, just remove the space :) If there are things in the code that are not clear, don't hesitate to ask. Hope this helps. (PS: the name 's Anthony :)
import java.io.*;
import java.text.NumberFormat;
import java.util.*;
import javax.swing.filechooser.FileSystemView;
public class Main {
/************************* MAIN *************************/
public static void main(String[] args) {
String generatedInput = generateRandomStrand(100);
System.out.println("Generated input: "+generatedInput);
printFreqTable( generateFreqTable(generatedInput, 2));
String userInput = getStrandFromUser();
System.out.println("User input: "+userInput);
saveFreqTable( generateFreqTable(userInput, 15), DEFAULT_OUTPUT_FILE);
}
/************************* SETTINGS *************************/
/**
* A File object that can be used to save a frequency table
* = "freqTable.txt", on your desktop
*/
private static final File DEFAULT_OUTPUT_FILE = new File( FileSystemView .getFileSystemView().getHomeDirectory(), "freqTable.txt");
private static final String EXPORT_FIELD_SEPARATOR = " - ";
private static final boolean EXPORT_AMOUNTS = true;
private static final boolean EXPORT_PERCENTAGES = true;
private static final int EXPORT_PERCENTAGE_FRACTION_DIGITS = 2;
/************************* INPUT *************************/
/**
* Lets the user input a strand
*/
public static String getStrandFromUser() {
System.out.print("Please enter your strand: ");
return new Scanner(System.in).nextLine();
}
/**
* Generates a random strand of the given length
*/
public static String generateRandomStrand(int length) {
StringBuilder builder = new StringBuilder(length);
for(int i=0;i
int element = (int) (Math.random()*4);
switch(element) {
case 0 : builder.append('A'); break;
case 1 : builder.append('C'); break;
case 2 : builder.append('G'); break;
case 3 : builder.append('T'); break;
}
}
return builder.toString();
}
/************************* PROCESSING *************************/
public static Map
generateFreqTable(String input, int partLength) {
if(partLength > input.length())
throw new IllegalArgumentException();
SortedMap result = new TreeMap();
for(int i=0;i
String part = input.substring(i, i+partLength);
int currentFreq = (result.get(part) == null) ? 0 : result.get(part);
result.put(part, currentFreq+1);
}
return result;
}
/************************* OUTPUT *************************/
/**
* Prints the given frequency table to the default output (command prompt)
*/
public static void printFreqTable(Map freqTable) {
saveFreqTable(freqTable, System.out);
}
/**
* Saves the given frequency table to the given file
*/
public static void saveFreqTable(Map freqTable, File output) {
try {
saveFreqTable(freqTable, new FileOutputStream(output));
} catch(FileNotFoundException e) {
System.err.println("Saving failed: "+e.getMessage());
}
}
/**
* Helps the other output methods
*/
private static void saveFreqTable(Map freqTable, OutputStream out) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(out, "UTF-8"), true);
for(String key : freqTable.keySet()) {
String line = key;
if(EXPORT_AMOUNTS)
line += EXPORT_FIELD_SEPARATOR +freqTable.get(key);
if(EXPORT_PERCENTAGES)
line += EXPORT_FIELD_SEPARATOR +getPct(freqTable.get(key), freqTable);
pw.println(line);
}
} catch(IOException e) {
System.err.println("Saving failed: "+e.getMessage());
} finally {
if(pw!=null && out!=System.out)
pw.close();
}
}
/**
* Gets the percentage as a String
*/
private static String getPct(Integer value, Map freqTable) {
int totalNbSets = 0;
for(String key : freqTable.keySet())
totalNbSets += freqTable.get(key);
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setMinimumFractionDigits( EXPORT_PERCENTAGE_FRACTION_DIGITS);
return fmt.format(1.0*value/totalNbSets);
}
}
EDIT6 (please do read my other edits below too, if you don't mind :)): this is a class i 've written which solves your problem.
import java.util.*;
public class Main {
/*Main method*/
public static void main(String[] args) {
String input = generateRandomStrand(100);
System.out.println("Used input: "+input);
printFreqTable( generateFreqTable(input, 2));
printFreqTable( generateFreqTable(input, 3));
}
/*This method will generate a random strand of the given length*/
public static String generateRandomStrand(int length) {
StringBuilder builder = new StringBuilder(length);
for(int i=0;i
int element = (int) (Math.random()*4);
switch(element) {
case 0 : builder.append('A'); break;
case 1 : builder.append('C'); break;
case 2 : builder.append('G'); break;
case 3 : builder.append('T'); break;
}
}
return builder.toString();
}
/*The following method will generate a frequency table*/
public static Map generateFreqTable(String input, int partLength) {
if(partLength > input.length())
throw new IllegalArgumentException();
SortedMap result = new TreeMap();
for(int i=0;i
String part = input.substring(i, i+partLength);
int currentFreq = (result.get(part) == null) ? 0 : result.get(part);
result.put(part, currentFreq+1);
}
return result;
}
/*The following method will print a frequency table*/
public static void printFreqTable(Map freqTable) {
for(String key : freqTable.keySet())
System.out.println(key+" - "+freqTable.get(key));
}
}
EDIT5bis: it's possible to encode a strand of length 4 with 1 byte. This encoding takes 8 times less memory & makes it very easy to read from & write to files.
EDIT5: what would be the maximum input length that should be handled? one million, one billion...?
& what 's the maximum "set length" that should be handled? 15?
EDIT4: where does the input strand come from? from a file or from input of the user?
EDIT3: what is the main goal of your program? is it:
1) a simple input-output program
input = a huge string + the length of the parts
output = a frequency table
2) a rather complex program, i.e.: this is just one of the many things your program should be able to do. You want it to be easily extensible, so that later on you can add things like "check if 2 strands can be paired", "concatenate two strands" etc...
EDIT2: so if you 'd have a String of length 30, & would want to have it split into sets of 15, you would end up with 16 sets, right?
EDIT: now i don't mean to be annoying, but: you say you 're dealing with RNA strands, but doe