2015-02-10 43 views
0

我必须从一个单独的字符串中创建单独的字符串。Java子串的关键字

例如,给定字符串:

.*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.* 

我要创建具有以下内容一个HashSet:

.*C.{0}A.{2}T.{0}T.* 
.*A.{2}T.{0}T.{0}T.* 
.*T.{0}T.{0}T.{2}T.* 
.*T.{0}T.{2}T.{0}G.* 
... 

的元件被通过取的条目的4从原始字符串形成并从他们创建一个更小的字符串。然后,您将一个条目沿原始字符串移动并重复。

我该怎么做?

谢谢!

+1

我没有看到输入和输出如何与 – njzk2 2015-02-10 19:24:24

+0

大家好输出信息是从输入。输入字符的第一个是C. {0} A. {2} T. {0} T和下一个字符从A. {2}开始,因此切割A. {2} T. {0} T. {0} T ..... – 2015-02-10 19:27:00

回答

1

你想要一个字符串,表示一个元素列表,并将它变成一组重叠的较短的元素列表。

private static final Pattern pattern = Pattern.compile("[ACGT]\\.\\{\\d+\\}"); 

public static List<String> extract(String input) { 
    Matcher matcher = pattern.matcher(input); 
    List<String> result = new ArrayList<String>(); 

    while (matcher.find()) { 
     result.add(matcher.group(0)); 
    } 

    return result; 
} 

public static Set<String> compose(List<String> elements, int window) { 
    Set<String> result = new HashSet<String>(); 

    for (int i = 0; i <= elements.size() - window; i++) { 
     StringBuilder builder = new StringBuilder(".*"); 
     for (int j = i; j < i + window; j++) { 
      builder.append(elements.get(j)); 
     } 
     // This strips the final quantifier turning: 
     // .*C.{0}A.{2}T.{0}T.{0} 
     // into 
     // .*C.{0}A.{2}T.{0}T 
     builder.delete(builder.lastIndexOf("."), builder.length()); 

     builder.append(".*"); 
     result.add(builder.toString()); 
    } 

    return result; 
} 

可以用下面的方法检查::

public static void main(String[] args) { 
    String input = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}"; 

    Set<String> result = compose(extract(input), 4); 

    // The result will contain 
    // ".*C.{0}A.{2}T.{0}T.*" 
    // etc 
} 
+0

非常感谢你,当我尝试调试你的代码时,我得到了结果* T。{0} T. {0} T. {2} T. {0}。* ..这应该是be。* T。{0} T. {0} T. {2} T. * – 2015-02-10 20:08:09

+0

我已经更新它来解决这个问题。 – 2015-02-10 20:08:28

+0

超级棒!谢谢:) – 2015-02-10 20:11:06

1
可以通过具有返回从列表中,其选择套元件以显示一个滑动窗口中的元素,然后在方法做到这一点

这里是一个可能的解决方案:

public class Main { 
public static void main(String[] args) { 
    String s = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*"; 
    String[] array = s.split("}"); 

    Set<String> result = new HashSet<String>(); 
    for (int i = 0 ; i < array.length-3 ; i++) { 
     String firstElement = array[i].startsWith(".*") ? array[i].substring(2) : array[i]; 
     String lastElement = array[i+2]+"}"+array[i+3].substring(0,1)+".*" ; 
     String element = ".*"+firstElement+"}"+array[i+1]+"}"+lastElement; 
     result.add(element); 
     System.out.println(element); 
    } 

    //Your result are in the Set result 
} 
} 
+0

您的字符串只有三个元素,但在他提供的示例中4个元素。例如,您生成的第一个结果是“。* C。{0} A. {2} T. {0}。*'当它应该是'。C。{0} A. {2} T. {0} T. *' – 2015-02-10 20:03:23

+0

我明白了,你是对的。我立即完成代码。 – szpetip 2015-02-10 20:04:51

+1

我按照你的建议修改了代码。 – szpetip 2015-02-10 20:10:32