简体   繁体   中英

How do I map one set of letters to another set in perl?

I have a dna string like

tcatttcaaatcatctggaccaaaagagtcaggaagtaactcttctatcgttttcatatcataacccccgt
cgtcatgaaacatataaacatttatatccttcgaaaattcacgaattacttgacgacaaataccacagggc
gtaactcttcccttggcactcatcactccaatagccataaatttagtgtaacccatgctcactgcttttgt
aattgctactcgttcggcacagatgcaattgccatatgatgcgttttcaacattagctccataaatatatg
tatttttatcgtcggatactacgcatgcacctactgcgaagttggagtagggacaatatgaatattgtaaa
ctctcttttacttcttgaaataacttctcaatatcttctttttccat

(except as one long strong with no breaks)

Now I want to replace all a to t , t to a , c to g , g to c how can I do using a regular expression?

You could use a subroutine:

sub complement {

    my ($seq) = @_;

    $seq =~ tr/ACGTacgt/TGCAtgca/;

    return $seq;
}

And call it like:

my $complemented_string = complement($string);

You should present your problem as plainly as possible. Asking for a solution that uses a regular expression misleads everyone.

Ever since version 14 of Perl 5, you could use a /r modifier on tr/// to return a modified string instead of altering the value in-place. It is probably the best solution if you want to keep both the original and the inverted DNA sequences.

This short program shows an example. Note that tr/// doe not use regular expressions.

use strict;
use warnings;
use 5.014;

my $seq = 'tcatttcaaatcatctggaccaaaagagtcaggaagt';
my $inv = $seq =~ tr/atcg/tagc/r;

say $seq;
say $inv;

output

tcatttcaaatcatctggaccaaaagagtcaggaagt
agtaaagtttagtagacctggttttctcagtccttca

如果您想尝试,直接使用shell命令tr

tr "atcg" "tagc" < file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM