整理代码,方便自己查看复习,无参考价值,勿喷
通用代码
- 定义外部参数
my ($infile,$outfile);
GetOptions(
"i|infile:s" => \$infile,
"o|outfile:s" => \$outfile
);
- 打开关闭文件
open F,"$infile"or die $!;
close F;
- 打开关闭写入文件
open OUT,">$outfile" or die $!;
close OUT;
- 注意while,if,foreach使用:
- 子程序建立哈希(ex2)
- 调用子程序
my %hash = read_lis($infile);
常看的四题代码
- 提取文件三列
my ($infile,$outfile);
33 GetOptions(
34 "i|infile:s" => \$infile,
35 "o|outfile:s" => \$outfile
36 );
37
38 open F,"$infile"or die $!;
39 open OUT,">$outfile" or die $!;
40 while (my $line=<F>){
41 chomp $line;
42 my @a=split("\t",$line);
43 if ($a[2] eq "transcript"){
44 my $length =abs($a[4]-$a[3])+1;
45 my $id0 =$a[8];
46 my @id1 = split(";",$id0);
47 my $id =$id1[0];
48 my $chr=$a[0];
49 print OUT "$chr\t$length\t$id\n";
50
51 }
52 }
53 close F;
54 close OUT;
32 my $infile;
33 GetOptions(
34 "i|infile:s"=> \$infile,
35 # "o|outfile"=> \$outfile,
36 );
37
38 #建立哈希
39 my %hash = read_lis($infile);
40 #计算所有value的sum;
41 use List::Util qw/sum/;
42 my @c = values %hash;
43 my $sum = sum @c;
44 #计算特定key对应value的sum1,if循环;
45 my $sum1=0;
46 foreach (keys %hash){
47 if (($_>=18)and($_<=30)){
48 $sum1 += $hash{$_};
49 print "$sum1\n";
- ex1
my ($infile1,$infile2,$outfile);
33 GetOptions(
34 "ifa|infile1:s" => \$infile1,
35 "id|infile2:s" => \$infile2,
36 "o|outfile:s" => \$outfile,
37
38 );
39
40 my $testid =read_id ($infile2);
41 my @testid2=split /-/,$testid;
42 my %hash2 = read_fa($infile1);
43 #主程序:判断并输出
44 open OUT,">$outfile" or die $!;
45 my $seq2;
46 foreach (@testid2){
47 if (exists $hash2{$_}){
48 $seq2 =$hash2{$_};
49 print OUT "$_\n$seq2\n";
50 }
51 }
52 #close F;
53 close OUT;
54
55 #将test.fa创建为哈希
56 sub read_fa{
57 my %hash;
58 open F1,$infile1 or die $!;
#这个子程序已经定义了参数,也没有必要写为子程序
59 my ($id,$gene, $seq);
60 while(my $line=<F1>){
61
62 if ($line =~/^>/){
63 ($id,$gene)=split" ",$line;
64 $id=~ s/>//;
65 $seq='';
66 }
67 else{
68 $seq.=$line;
69 }
70 $hash{$id}=$seq;
71 }
72 close F1;
73 return %hash;
74 }
75
76 #读取文件test.id,将其转变为数组
77 sub read_id{
78 open F,$infile2 or die $!;
79 my ($a,$testid);
80 while(my $line1=<F>){
81 chomp $line1;
82 $a=$line1;
83 $testid .="$a-";
84 }
85 close F;
86 return $testid;
87 }
- ex2
33 my ($infile,$inchr,$sta_end,$outfile);
34 GetOptions(
35 "it|infile:s"=>\$infile,
36 "chr|inchr:i"=>\$inchr,
37 "se|sta_end:s"=>\$sta_end,
38 "o|outfile:s"=>\$outfile,
39 );
40 #定义输入文件chr-seq为哈希%testhash,需要调用> 子程序并输入参数
41 my %testhash=read_fafile($infile);
42 #赋值$defseq为%testhash中key为输入参数(inchr )的value值
43 my $defseq=$testhash{$inchr};
44
45 #主程序
46 #调用外部输入参数$sta_end,定义其差值,若大于0 输出%defhash,小于0,输出反向互补的%defseq,
47 open OUT,">$outfile"or die "$outfile\n";
48 my ($s,$e) = split "-",$sta_end;
49 my $dse=$e-$s;
50 if ($dse>0){
51 my $locseq=substr($defseq,$s-1,$e-$s+ 1);
52 print OUT "$inchr\n $locseq\n";
53 }else{
54 my $cseq=reverse $defseq;
55 $cseq =~ tr/ATCGacgt/TAGCtgca/;
56 my $relocseq=substr($cseq,$e-1,$s-$e+ 1);
57 print OUT "$inchr\n$relocseq\n";
58 }
59 close OUT;
60 #子程序1:读取文件,将fa文件chr-seq建立哈希,> 返回哈希,
61 sub read_fafile{
62 my $filename = shift;
63 open F,$filename or die $!;
64 #my $filename = $(@_)[0];还没有见过这种写
法,不知道对不对
65 #my $filename = $_[0];
66 my (%hash,$chr,$seq);
67 while (my $line=<F>){
68 # my ($chr,$seq);
69 $line =~s/[\r\n]//g;
70 if ($line=~/^>/){
71 $chr = $line;
72 $chr =~s/^>//;
73 $seq ='';
74 # print "$chr\n";
75 }
76 else{
77 $seq .=$line;
78 }
79 #print "err-$seq\n";
80 $hash{$chr}=$seq;
81 }
82 return %hash;
83 }
- 第三题
- 按照序列数分fq1与fq2文件
my ($infile1,$infile2,$defseqc);
GetOptions(
"ifq1|infile1:s"=>\ $infile1,
"ifq2|infile2:s"=> \$infile2,
"c|defseqc:i"=> \$defseqc,
);
cut_seqcount($infile1);
cut_seqcount($infile2);
sub cut_seqcount{
$/ = "@";
my ($b,$a);
$a =0;
$b=0;
my $filena =shift;
my $filename=$filena.$b;
open OUT,">$filename" or die $!;
print OUT "@";
open F,$filena or die $!;
my $line;
while (my $file =<F>){
#chomp $file;
my $line = $file;
if ($a< $defseqc){
print OUT $line or die $!;
$a =$a+1;
}
else {
close OUT;
$a = 0;
$b=$b+1;
$filename = $filena.$b;
open OUT,">$filename" or die $!;
#my $line2 = $_
print OUT "@";
print OUT "$line" or die $!;
$a =$a+1;
}
}
close OUT;
close F;
}
- 按照文件数目分fq1,fq2文件
my ($infile1,$infile2,$deffilec);
GetOptions(
"ifq1|infile1:s"=>\ $infile1,
"ifq2|infile2:s"=>\ $infile2,
"c|deffilec:i"=> \$deffilec,
);
cut_fileco($infile1);
cut_fileco($infile2);
sub cut_fileco{
$/ = "@";
my $filenam =shift;
open F,$filenam or die $!;
##创建并切换目录:
#my $di = "outfile_$b";
#my $dir ="/($di)";
#mkdir ($dir) ,0755 or die "无法创建目录,$!";
#chdir ($dir) or die "无法切换目录,$!";
#open F,$infile1 or die $!;
my ($b,$a);
$a =0;#对应外部定义序列数的计数器
$b=0;#对应文件名的计数器
my $cseq =0;#对应读取文件line的计数器
my $all_line =`wc -l $infile1`;
my $all_seq_count = ($all_line)/4;
my $filename=$filenam.$b;
open OUT,">$filename" or die $!;
print OUT "@";
my $line;
my $defseqc=int($all_seq_count/$deffilec);
print $defseqc;
while (my $file =<F>){
#chomp $file;
$cseq +=1;
# my $defseqc=int($all_seq_count/$deffilec);
#my $defseqc =("%.0f",$all_seq_count/$deffilec );
#print $defseqc;
my $line = $file;
#print "$file-";
if ($a< $defseqc or $cseq > $deffilec * $defseqc ){
#$line .=$_;
print OUT $line or die $!;
$a =$a+1;
}
else {
close OUT;
$a = 0;
$b=$b+1;
$filename =$filenam.$b;
open OUT,">$filename" or die $!;
#my $line2 = $_
print OUT "@";
print OUT "$line" or die $!;
$a =$a+1;
}
}
close OUT;
close F;
}
网友评论