perl函数substr
来源:互联网 发布:成都正规java培训机构 编辑:程序博客网 时间:2024/05/01 02:24
把DNA序列经过整合成一行,然后去除所有的空白字符以后,又把$DNA通过split函数变成了数组,然后进行统计,那有没有更好的办法呢?
其实perl里有一个函数,substr。
我们先来看一看这个函数的用法,substr是针对一个大字符串的操作符(The substr function works with only a part of a larger string )言外之意就是对一个很长的字符串,进行片段化处理,取其中的一部分。我们这里用到的就是这个特性。
$little_string =substr($large_string,$start_position,$length)
$小片段=substr($大片段,$你要截取的小片段的起始位置,$你要截取的长度)
我们这里为了统计DNA中各种碱基的数量,所以要处理的字符串是一个碱基,所以我们要把$length设置为1。这样才能够满足我们的需求。
下面我们把修改过的代码写下:
- #下面的程序是用来计算一段DNA序列中ATGC的数量的
- #首先定义四种碱基的数量为0
- $count_A=0;
- $count_T=0;
- $count_C=0;
- $count_G=0;
- #首先要先把序列进行合并成一行
- #先确定所要处理的文件的路径及文件名(在windows系统下面要按照这样的例子写
- #f:\\perl\\data.txt
- print "please input the Path just like this f:\\\\perl\\\\data.txt\n";
- chomp($dna_filename=<STDIN>);
- #打开文件
- open(DNAFILENAME,$dna_filename)||die("can not open the file!");
- #将文件赋予一个数组
- @DNA=<DNAFILENAME>;
- #以下两步要把所有的行合并成一行,然后去掉所有的空白符
- $DNA=join('',@DNA);
- $DNA=~s/\s//g;
- #然后依次读取字符串的元素,并对四种碱基的数量进行统计
- for ($position=0;$position<length $DNA;++$position)
- {
- $base=substr($DNA,$position,1);
- if ($base eq 'A')
- {
- $count_A=$count_A+1;
- }
- elsif ($base eq 'T')
- {
- $count_T=$count_T+1;
- }
- elsif ($base eq 'C')
- {
- $count_C=$count_C+1;
- }
- elsif ($base eq 'G')
- {
- $count_G=$count_G+1;
- }
- else
- {
- print "error\n"
- }
- }
- #输出最后的结果
- print "A=$count_A\n";
- print "T=$count_T\n";
- print "C=$count_C\n";
- print "G=$count_G\n";
得到的结果如下:
- F:\>perl\a.pl
- please input the Path just like this f:\\perl\\data.txt
- f:\\perl\\data.txt
- error
- A=40
- T=17
- C=27
- G=24
- F:\>
0 0
- perl函数substr
- Perl 字符串截取函数substr
- Perl 字符串截取函数substr
- shell、perl 常用函数---substr、chmod
- Perl语言substr应用
- substr函数
- substr() 函数
- substr函数
- substr 函数
- SUBSTR()函数
- substr 函数
- substr函数
- substr函数
- substr函数
- substr函数
- perl 中substr应用详解
- perl中substr应用详解
- perl对于utf8编码文件的substr
- myBatis的SQL注入问题
- 无废话WCF入门教程四[WCF的配置文件]
- C语言高级语法概述笔记
- Css浮动
- 无废话WCF入门教程五[WCF的通信模式]
- perl函数substr
- Activity的四种加载模式
- Manacher算法:求解最长回文字符串,时间复杂度为O(N)
- INI文件操作
- 在xcode中编译APUE代码
- android gradle初探
- 8.去掉const属性
- Raspberry Pi-2 启动自动挂载USB设备
- Android开源项目SlidingMenu深入剖析