大家好我正在尝试阅读基因组序列并搜索出现的任何10个字符重复。我想到的解决方案分为三个步骤:
我遇到的麻烦是如何从较旧和较大的字符串生成新字符串。如果我的基因组序列是AAAAGGGGGAAAATTTCCCC,那么我的前十个字符序列将是AAAAGGGGGA,下一个将是AAAGGGGGAA。我将如何在java中进行此操作?
这是我到目前为止所做的:
import java.util.List;
import java.util.ArrayList;
public class Solution
{
public ArrayList<String> findRepeatedDnaSequences(String s)
{
ArrayList<String> sequence = new ArrayList<String>();
int matches;
ArrayList<String> matchedSequence = new ArrayList<String>();
for(int i = 0; i < s.length(); i++)
{
if (i + 9 > s.length())
{
sequence.add(s.substring(i, i + 9));
}
}
for(int i = 0; i < sequence.size(); i++)
{
matches = 0;
for (int j = 1; j < sequence.size(); j++)
{
if(sequence.get(i) == sequence.get(i))
{
matches++;
System.out.print(matches);
matchedSequence.add(sequence.get(i));
}
}
}
return matchedSequence;
}
}
答案 0 :(得分:0)
{{1}}
答案 1 :(得分:0)
如果您使用的是Java 8,则可以使用流来完成。遗憾的是,Stream API中缺少许多方法,这些方法存在于其他编程语言中,但我们仍然可以自己实现它们。所以使用这个答案的方法sliding
:
How can I convert a Stream of Strings to Stream of String pairs?
您可以这样做:
String gseq = "AAAAACCCCCAAAAACCCCC";
Map<String, Long> count = StreamUtils.sliding(10, gseq.chars().boxed())
.map(l -> new String(l.stream().mapToInt(n -> n).toArray(), 0, l.size()))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
这将生成一个地图,其中包含每个长度为10的子字符串的计数。
答案 2 :(得分:0)
Following is the complete class that you are looking for. The code is pretty self explanatory.
package source;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.zip.InflaterInputStream;
public class PatternFinding {
//function to find the patterns
public static List<String> stringMatcher(String str,int len){
String string="";
int count=1;
List<String> list=new ArrayList<String>();
for(int i=0;i+len<=str.length();i++){
System.out.print(i);
string="";
count=1;
char ch=str.charAt(i);
string+=String.valueOf(ch);
for(int j=i+1;j<str.length() && j<i+len;j++){
System.out.println(" "+j);
if(ch==str.charAt(j)){
count++;
string+=String.valueOf(str.charAt(j));
}else{
break;
}
}
System.out.println(string);
if(count==len){
list.add(string);
}
}
return list;
}
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String text=br.readLine();
//pass the length of your pattern as second arguement
List<String> list=stringMatcher(text,5);
//sorting the list
Collections.sort(list);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//counting occurances
for(int i=0;i<list.size();){
String str=list.get(i);
int lastIndex=list.lastIndexOf(str);
System.out.println(str+" happens "+ (lastIndex-i+1)+" times");
i=lastIndex+1;
}
}
}