Find nucleotides in DNA sequence with perl

泪湿孤枕 提交于 2019-12-10 18:37:36

问题


I have the sequence DNA and I want to find nucleotide of the sequence at the position which was chosed by people. Below is the example:

Enter the sequence DNA: ACTAAAAATACAAAAATTAGCCAGGCGTGGTGGCAC (the length of sequence is 33) Enter the position: (12)

I hope the result is the position number 12 the nucleotides are AAA.

I have no problem finding the amino acid of the position. Below is the current code I have.

print "ENTER THE FILENAME OF THE DNA SEQUENCE:= ";
$DNAfilename = <STDIN>;
chomp $DNAfilename;
unless ( open(DNAFILE, $DNAfilename) ) {
  print "Cannot open file \"$DNAfilename\"\n\n";
}
@DNA = <DNAFILE>;
close DNAFILE;
$DNA = join( '', @DNA);
print " \nThe original DNA file is:\n$DNA \n";
$DNA =~ s/\s//g;

print" enter the number ";
$po=<STDIN>;

@pos=$DNA;
if ($po>length($DNA)) 
{
  print" no data";
}

else 
{
  print " @pos\n\n";
}

Please advice how can I find the position at the DNA sequence.


回答1:


my $nucleotide = substr $DNA, $po, 3;

This will take the 3 nucleotides from positions $po upto $po+2 and assign it to $nucleotide.




回答2:


That'll be something like this:

use strict;
use warnings;

print 'ENTER THE FILENAME OF THE DNA SEQUENCE:= ';
my $DNA_filename = <STDIN>;
chomp $DNA_filename;
unless (open(DNAFILE, $DNA_filename))
{
    die 'Cannot open file "' . $DNA_filename . '"' . "\n\n";
}

my @DNA = <DNAFILE>;
close DNAFILE;

my $DNA_string = join('', @DNA);
print "\n" . 'The original DNA file is:' . "\n" . $DNA_string . "\n";
$DNA_string =~ s/\s//g;

print ' enter the number ';
my $pos = <STDIN>;

if ($pos > length($DNA_string)) 
{
    print ' no data';
}
else
{
    print ' ' . substr($DNA_string, $pos, 3) . "\n\n";
}

Some comments:

  1. Always use strict and use warnings - it'll help you to write better and bug-free code.
  2. I personally don't like using interpolation in double quoted strings, hence those concatenations.
  3. Result's position is starting with 0 - if you want, you may change last if's condition and else.

Edit: I've misread part of question about nucleotides, as @hexcoder wrote, you want substr($DNA_string, $pos, 3).



来源:https://stackoverflow.com/questions/7090371/find-nucleotides-in-dna-sequence-with-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!