-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreference_add_snp.pl
More file actions
116 lines (103 loc) · 2.6 KB
/
reference_add_snp.pl
File metadata and controls
116 lines (103 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use FindBin qw($Bin $Script);
use File::Basename qw(basename dirname);
require "$Bin/snp.pm";
require "$Bin/common.pm";
my $BEGIN_TIME=time();
my $version="1.0.0";
#######################################################################################
# ------------------------------------------------------------------
# GetOptions
# ------------------------------------------------------------------
my ($fref, $fvcf,$fkey,$outdir);
GetOptions(
"help|?" =>\&USAGE,
"ir:s"=>\$fref,
"iv:s"=>\$fvcf,
"k:s"=>\$fkey,
"od:s"=>\$outdir,
) or &USAGE;
&USAGE unless ($fref and $fvcf and $fkey);
$outdir||="./";
`mkdir $outdir` unless (-d $outdir);
$outdir=AbsolutePath("dir",$outdir);
&SHOW_TIME("read reference file");
my @chr;
my %seq;
open(R, $fref) or die $!;
$/=">";
while(<R>){
chomp;
next if(/^$/);
my ($head, @seq)=split /\n/, $_;
my ($id)=split /\s+/, $head;
print $id,"...\n";
push @chr, $id;
$id=~s/chr//;
my $x = join("", @seq);
@{$seq{$id}} = split //, $x;
}
close(R);
&SHOW_TIME("read in vcf file and add snp info");
if($fvcf=~/.gz/){
open(V, "zcat $fvcf|") or die $!;
}else{
open(V, $fvcf) or die $!;
}
$/="\n";
while(<V>){
chomp;
next if(/^$/ || /^#/);
my ($c, $p, undef, $ref, $alt)=split /\s+/, $_;
$c=~s/chr//;
next if(!exists $seq{$c});
&sequence_convert_snp($seq{$c}, $p, $ref, $alt);
}
close(V);
my $L=60;
open(O, ">$outdir/$fkey.add_snp.fasta") or die $!;
foreach my $c (@chr){
print O ">$c\n";
$c=~s/chr//;
my $len = scalar @{$seq{$c}};
for(my $i=0; $i<$len; $i+=$L){
my $e = $i+$L-1;
$e= $e >($len-1)? $len-1: $e;
# for(my $j=$i; $j<=$e;$j++){
# if(!defined $seq{$c}->[$j]){
# print join("\t",$i,$e,$j),"\n";
# print join("", @{$seq{$c}}[$i..$e]),"\n";
# die;
# }
#
# }
print O join("", @{$seq{$c}}[$i..$e]),"\n";
}
}
close(O);
#######################################################################################
print STDOUT "\nDone. Total elapsed time : ",time()-$BEGIN_TIME,"s\n";
#######################################################################################
# ------------------------------------------------------------------
# sub function
# ------------------------------------------------------------------
sub USAGE {#
my $usage=<<"USAGE";
Program:
Version: $version
Contact:zeng huaping<huaping.zeng\@genetalks.com>
Usage:
Options:
-ir <file> Input reference file, forced
-iv <file> Input vcf gz file, forced
-k <str> Key of output file, forced
-od <dir> Dir of output file, default ./
-h Help
USAGE
print $usage;
exit;
}