Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

calculating molecular weight in perl

I have a perl script but it calculate molecular weight only when sequence is given. However I want to calculate molecular weight of protein sequences which is in fasta file.

print "Enter the amino acid sequence:
";  
$a = < STDIN > ; 
chomp($a);
my @a = ();
my $a = '';
$x = length($a); 
print "Length of sequence is : $x";
@a = split('', $a); 
$b = 0; 
my %data = ( 
    A=>71.09,  R=>16.19,  D=>114.11,  N=>115.09, 
    C=>103.15,  E=>129.12,  Q=>128.14,  G=>57.05, 
    H=>137.14,  I=>113.16,  L=>113.16,  K=>128.17, 
    M=>131.19,  F=>147.18,  P=>97.12,  S=>87.08, 
    T=>101.11,  W=>186.12,  Y=>163.18,  V=>99.14 
); 
foreach $i(@a) { 
    $b += $data{$i}; 
} 
$c = $b - (18 * ($x - 1)); 
print "
The molecular weight of the sequence is $c";             
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

first of all u must tell us what format has .fasta files. As i know they looks like

>seq_ID_1 descriptions etc 
ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH
ASDSADGDASHDASHSAREWAWGDASHASGASGASGSDGASDGDSAHSHAS
SFASGDASGDSSDFDSFSDFSD

>seq_ID_2 descriptions etc
ASDGDSAHSAHASDFRHGSDHSDGEWTSHSDHDSHFSDGSGASGADGHHAH
ASDSADGDASHDASHSAREWAWGDASHASGASGASG

if we will make suggestion that your code works fine, and counts molecular weight all we need is to read fasta files, parse them and count weight by yours code. It's more easy that sounds like.

#!/usr/bin/perl

use strict;
use warnings;
use Encode;


for my $file (@ARGV) {
    open my $fh, '<:encoding(UTF-8)', $file;
    my $input = join q{}, <$fh>; 
    close $fh;
    while ( $input =~ /^(>.*?)$([^>]*)/smxg ) {
        my $name = $1;
        my $seq = $2;
        $seq =~ s/
//smxg;
        my $mass = calc_mass($seq);
        print "$name has mass $mass
";
    }
}

sub calc_mass {
    my $a = shift;
    my @a = ();
    my $x = length $a;
    @a = split q{}, $a;
    my $b = 0;
    my %data = (
        A=>71.09,  R=>16.19,  D=>114.11,  N=>115.09,
        C=>103.15,  E=>129.12,  Q=>128.14,  G=>57.05,
        H=>137.14,  I=>113.16,  L=>113.16,  K=>128.17,
        M=>131.19,  F=>147.18,  P=>97.12,  S=>87.08,
        T=>101.11,  W=>186.12,  Y=>163.18,  V=>99.14
    );
    for my $i( @a ) {
        $b += $data{$i};
    }
    my $c = $b - (18 * ($x - 1));
    return $c;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...