栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Rosalind Java|Overlap Graphs

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Rosalind Java|Overlap Graphs

Rosalind编程问题之查找重叠区段。

Overlap Graphs

Problem:
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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号