Rosalind编程问题之查找重叠区段。
Overlap GraphsProblem:
A graph whose nodes have all been labeled can be represented by an adjacency list, in which each row of the list contains the two node labels corresponding to a unique edge.
A directed graph (or digraph) is a graph containing directed edges, each of which has an orientation. That is, a directed edge is represented by an arrow instead of a line segment; the starting and ending nodes of an edge form its tail and head, respectively. The directed edge with tail v and head w is represented by (v,w) (but not by (w,v)). A directed loop is a directed edge of the form (v,v).
For a collection of strings and a positive integer k, the overlap graph for the strings is a directed graph Ok in which each string is represented by a node, and string s is connected to string t with a directed edge when there is a length k suffix of s that matches a length k prefix of t, as long as s≠t; we demand s≠t to prevent directed loops in the overlap graph (although directed cycles may be present).
Given: A collection of DNA strings in FASTA format having total length at most 10 kbp.
Sample input:
Rosalind_0498
AAATAAA
Rosalind_2391
AAATTTT
Rosalind_2323
TTTTCCC
Rosalind_0442
AAATCCC
Rosalind_5013
GGGTGGG
Return: The adjacency list corresponding to O3. You may return edges in any order.
Sample output:
Rosalind_0498 Rosalind_2391
Rosalind_0498 Rosalind_0442
Rosalind_2391 Rosalind_2323
简单概述下题目大意:给出一系列碱基序列。当序列开头三个碱基等于另一个序列结尾三个碱基时,这两个序列就可以被看作是重叠序列。需要我们将这两个序列并排输出到屏幕上。
实现思路如下:
1.读取整条核苷酸序列及其tag标签。
2.分别提取序列前三个碱基和另一序列后三个碱基(迭代两个for循环实现)。
3.当某一序列前三(front)碱基和另一序列后三(back)碱基相等,且另一序列不为序列自己时,输出两个序列的tag。
下面是实现代码:
public class Overlap_Graphs {
public static void main(String[] args) {
ArrayList fasta = BufferedReader2("C:/Users/Administrator/Desktop/rosalind_grph.txt", "fasta");
ArrayList tag = BufferedReader2("C:/Users/Administrator/Desktop/rosalind_grph.txt", "tag");
for (int i = 0; i < fasta.size(); i++) {
String back = fasta.get(i).substring(fasta.get(i).length() - 3);
for (int j = 0; j < fasta.size(); j++) {
String front = fasta.get(j).substring(0, 3);
if (front.equals(back) && tag.get(i) != tag.get(j)) {//开头三碱基等于另一结尾三碱基,且不等于自己
System.out.println(tag.get(i).substring(1) + " " + tag.get(j).substring(1));
}
}
}
}
public static ArrayList BufferedReader2(String path, String choose) {//返回值类型是新建集合大类,此处是Set而非哈希。
BufferedReader reader;
ArrayList tag = new java.util.ArrayList();
ArrayList fasta = new java.util.ArrayList();
try {
reader = new BufferedReader(new FileReader(path));
String line = reader.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {//多次匹配带有“>”的行,w代表0—9A—Z_a—z,需要转义。W代表非0—9A—Z_a—z。
if (line.matches(">[\w*|\W*]*")) {
tag.add(line);
//定义字符串变量seq保存删除换行符的序列信息
if (sb.length() != 0) {
String seq = sb.toString();
fasta.add(seq);
sb.delete(0, sb.length());//清空StringBuilder中全部元素
}
} else {
sb.append(line);//重新向StringBuilder添加元素
}
// read next line
line = reader.readLine();
}
String seq = sb.toString();
fasta.add(seq);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
if (choose.equals("tag")) {
return tag;
}
return fasta;
}
}



