I need to swap letters in a string with the following rules:
For example: ACGTA
should become TGCAT
What would be the best way to resolve this?
As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.
For swapping two strings from one location to another location, we use strcpy() function. An array of characters (or) collection of characters is called a string.
Method #2 : Using join() + replace() + split() The combination of above methods can also be used to perform this task. In this, the task of replace is same, but we use join() and split() with same parameter to perform task of list comprehension.
The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name.
Searching for java "A to T, T to A"
found this suggestion:
String sequence = "AATTTCTCGGTTTCAAT";
sequence = sequence.replace("A", "t")
.replace("T", "a")
.replace("C", "g")
.replace("G", "c")
.toUpperCase();
System.out.println(sequence);
This is a simple and concise solution that works for your specific situation and will have acceptable performance if your DNA strings are relatively short. For a more general solution for handling large amounts of data you should iterate over the characters one by one and build a new string. Or as polygenelubricants pointed out - consider a storage format that only uses 2 bits per base instead of 16.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With