2015-02-10 75 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