Rosalind编程问题之计算GC含量。

Computing GC Content

Problem
The GC-content of a DNA string is given by the percentage of symbols in the string that are ‘C’ or ‘G’. For example, the GC-content of “AGCTATAG” is 37.5%. Note that the reverse complement of any DNA string has the same GC-content.

DNA strings must be labeled when they are consolidated into a database. A commonly used method of string labeling is called FASTA format. In this format, the string is introduced by a line that begins with ‘>’, followed by some labeling information. Subsequent lines contain the string itself; the first line to begin with ‘>’ indicates the label of the next string.

In Rosalind’s implementation, a string in FASTA format will be labeled by the ID “Rosalind_xxxx”, where “xxxx” denotes a four-digit code between 0000 and 9999.

Given: At most 10 DNA strings in FASTA format (of length at most 1 kbp each).
Sample input

>Rosalind_6404
CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC
TCCCACTAATAATTCTGAGG
>Rosalind_5959
CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT
ATATCCATTTGTCAGCAGACACGC
>Rosalind_0808
CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC
TGGGAACCTGCGGGCAGTAGGTGGAAT

Return: The ID of the string having the highest GC-content, followed by the GC-content of that string. Rosalind allows for a default error of 0.001 in all decimal answers unless otherwise stated; please see the note on absolute error below.
Sample output

Rosalind_0808
60.919540


GC含量是在所研究的对象的全基因组中,鸟嘌呤(Guanine)和胞嘧啶(Cytosine)在全部碱基中所占的比例。能够决定DNA的稳定性。本道题需要我们读取含有多序列的fasta文件,并且挨个计算GC含量,最终输出GC含量最高的序列开头注释信息以及其GC含量值。** 解题思路如下:

  • 1.逐行读取fasta文件,消掉序列信息中的换行符。
  • 2.计算每条序列的GC含量。
  • 3.各条序列的GC含量进行对比,并获得最大GC含量。
  • 4.输出GC含量最高的序列及其标签

因此,小编分别定义了三个子方法:

  • 子方法1BufferedReader用来读取fasta文件并且去掉其中的序列换行符。详情请见:解决Java逐行读取带有行缩进的fasta文件
  • 子方法2FindMaxCount获取GC含量最高值。
  • 子方法3FindIndex获取最大的GC含量序列引索值,用以输出其对应的开头注释信息。以下是全部代码。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;public class Computing_GC_Content {public static void main(String[] args) {//1.逐行读取fasta文件ArrayList<String> fasta = BufferedReader("C:/Users/Administrator/Desktop/rosalind_gc.txt", "fasta");//设置返回值是碱基序列。ArrayList<String> tag = BufferedReader("C:/Users/Administrator/Desktop/rosalind_gc.txt", "tag");//设置返回值是标签名称。//2.计算每条序列的GC含量float[] GCratio = new float[fasta.size()];//定义数组存储每条fasta序列的GC比例int[] GCcount = new int[fasta.size()];//定义数组存储每条fasta序列的GC含量for (int i = 0; i < fasta.size(); i++) {for (int j = 0; j < fasta.get(i).length(); j++) {if (fasta.get(i).charAt(j) == 'G' || fasta.get(i).charAt(j) == 'C') {//fasta.get(i).charAt(j)=='G'||'C' Java不支持此类型的判断语句GCcount[i]++;}}GCratio[i] = (float) GCcount[i] / fasta.get(i).length();}//3.各条序列的GC含量进行对比,并获得最大GC含量float maxcount = (float) (Math.round(FindMaxCount(GCratio) * 100000000)) / 1000000;int maxIndex = FindIndex(GCratio);//4.输出GC含量最高的序列及其标签System.out.println(tag.get(maxIndex));System.out.println(maxcount);}//子方法1.读取fasta文件并且分别存储到fasta集合和tag集合中。public static ArrayList<String> BufferedReader(String path,String choose) {//返回值类型是新建集合大类,此处是Set而非哈希。BufferedReader reader;ArrayList<String> tag = new java.util.ArrayList<String>();ArrayList<String> fasta = new java.util.ArrayList<String>();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);//第一个循环开始时StringBuilder为空,需要添加判断以排除此特例。if (sb.length()!=0){String seq = sb.toString();//定义字符串变量seq保存删除换行符的序列信息fasta.add(seq);sb.delete(0, sb.length());//清空StringBuilder中全部元素}}else{sb.append(line);//重新向StringBuilder添加元素}// read next lineline = reader.readLine();}String seq = sb.toString();fasta.add(seq);//循环结束还要再次输出序列,否则会丢失一条序列。reader.close();} catch (IOException e) {e.printStackTrace();}if (choose.equals("tag")){return tag;}return fasta;}//子方法2:获取最大的GC含量数字public static float FindMaxCount(float[] arr) {float max = arr[0];for (int x = 1; x < arr.length; x++) {if (max < arr[x]) {max = arr[x];}}return max;}//子方法3:获取最大的GC含量序列引索public static int FindIndex(float[] arr) {float max = arr[0];int maxValIndex = 0;for (int x = 1; x < arr.length; x++) {if (max < arr[x]) {max = arr[x];maxValIndex = x;}}return maxValIndex;}}

要留意哈,运行完全部代码并不代表着结束。你会发现输出的结果中">Rosalind_0808"是带着“>”的(示例如下),如果在上传Rosalind网站提交答案时带上了“>”,系统会自动判定为错误。请大家格外注意细节!最后手动删除“>”后再上传即可。

>Rosalind_0808
60.919540

Rosalind Java| Computing GC Content相关推荐

  1. 【Rosalind】Computing GC Content

    问题描述 Problem The GC-content of a DNA string is given by the percentage of symbols in the string that ...

  2. Rosalind Java|Matching Random Motifs

    Rosalind编程问题之计算随机序列出现并匹配待比对序列的概率. 跟Rosalind Java|Introduction to Random Strings有异曲同工之妙. Matching Ran ...

  3. Jvm 系列(六):Java 服务 GC 参数调优案例

    本文介绍了一次生产环境的JVM GC相关参数的调优过程,通过参数的调整避免了GC卡顿对JAVA服务成功率的影响. 这段时间在整理jvm系列的文章,无意中发现本文,作者思路清晰通过步步分析最终解决问题. ...

  4. java中gc是啥_java的gc是什么

    一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率,才能提高整个应用程序 ...

  5. java gc 可以对方法区进行回收_浅谈 Java 之 GC

    阅读本文假设你对java内存模型已有一些了解. 1.Java虚拟机中哪些内存需要回收? 先来看看jvm内存模型,如下图 线程隔离的区域随线程而生,随线程而灭:程序计数器可保存着虚拟机字节码指令的地址( ...

  6. gc java root_聊聊Java的GC机制

    原标题:聊聊Java的GC机制 近日,MIUI在小米全球社区发布公告,表示MIUI将在全球市场销售的手机中预装谷歌拨号及谷歌消息应用程序(中国.印度.印度尼西亚等市场除外).小米表示,小米9T Pro ...

  7. Java的GC机制及算法

    转载自   Java的GC机制及算法 GC的阶段  对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finaliz ...

  8. java超出gc开销_通过这5个简单的技巧减少GC开销

    java超出gc开销 编写代码的五种简单方法,可以提高内存效率,而无需花费更多时间或降低代码可读性 垃圾回收会为您的应用程序增加多少开销? 您可能不知道确切的数字,但您确实知道总有改进的余地. 尽管自 ...

  9. java 打开gc日志_在运行时打开GC日志记录

    java 打开gc日志 总是有下一个JVM表现不佳. 而且,您内心深知,如果您只有少数启动选项可以公开一些有关正在发生的事情的信息,那么您可能就有机会真正修复该死的东西. 但是不,您需要的标志( -X ...

最新文章

  1. Linux C编程--网络编程1--字节顺序和字节处理函数
  2. w7计算机的工具栏爱那里,Win7系统如何在任务栏中添加爱心图标图文教程
  3. 如何在fluid中添加自定义控件
  4. centos桥接模式网络配置
  5. how to deal with Demodex
  6. IDEA模块(module)的概念和使用_对比Eclipse
  7. Git 的 .gitignore 配置
  8. SpringMVC+Spring3.2+Hibernate4整合实例
  9. ​​2021快手母婴行业数据价值报告
  10. android的蓝牙和数据库的开源项目地址
  11. node.weiChat
  12. javascript高级程序设计pdf_JavaScript八张思维导图
  13. Layui自定义表单校验规则
  14. 你看得懂的CSMA介质访问控制原理
  15. 图解 Python 编程(27) | 时间和日期(附要点速查表·完结)
  16. Java实现将阿拉伯数字转换为中文数字123=》一二三
  17. windows CMD 下 长ping 加时间戳,亲测有效
  18. 计算机桌面去方格子,win7桌面office图标变成白色方格图标的原因和解法
  19. error: Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools,亲测100%安装
  20. 如何用java POI在excel中画线_Java中使用POI在Excel单元格中画斜线—XLS格式

热门文章

  1. 用kNN算法诊断乳腺癌--基于R语言
  2. java.lang.IllegalStateException: Already resumed, but proposed with update xxxx
  3. R语言 观测异常值并改进
  4. HashMap集合(高级)
  5. UI设计初学者应该如何入门?
  6. 微信公众号支付 使用基于thinkphp 使用微信官网的sdk
  7. 人像分割之ExtremeC3Net
  8. 哪五种人不适合学编程?
  9. 陈艾盐:春燕百集访谈节目第二十集
  10. 【洛谷P1903】数颜色