annotate extract_data_single_galaxy.pl @ 7:b3934ae8e58f draft

Deleted selected files
author grau
date Wed, 13 Nov 2013 04:12:09 -0500
parents 5130880b8e0a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5130880b8e0a Uploaded
grau
parents:
diff changeset
1 use strict;
5130880b8e0a Uploaded
grau
parents:
diff changeset
2
5130880b8e0a Uploaded
grau
parents:
diff changeset
3 if(@ARGV == 0){
5130880b8e0a Uploaded
grau
parents:
diff changeset
4 die <<USAGE
5130880b8e0a Uploaded
grau
parents:
diff changeset
5 usage:
5130880b8e0a Uploaded
grau
parents:
diff changeset
6 perl extract_data.pl <chromFa> <bedfile> <chromcol> <startcol> <seccolm> <secondcol> <width> <statcol> <outfile>
5130880b8e0a Uploaded
grau
parents:
diff changeset
7
5130880b8e0a Uploaded
grau
parents:
diff changeset
8 <chromFa>: the chromosome FastA containing all chromosome sequences
5130880b8e0a Uploaded
grau
parents:
diff changeset
9 <bedfile>: the file containing the peaks in tabular format,
5130880b8e0a Uploaded
grau
parents:
diff changeset
10 e.g., bed, gff, narrowPeak
5130880b8e0a Uploaded
grau
parents:
diff changeset
11 <chromcol>: the column of <bedfile> containing the chromosome
5130880b8e0a Uploaded
grau
parents:
diff changeset
12 <startcol>: the column of <bedfile> containing the start position relative to
5130880b8e0a Uploaded
grau
parents:
diff changeset
13 the chromosome start
5130880b8e0a Uploaded
grau
parents:
diff changeset
14 <seccolm>: center: "Center of peak (relative to start)", end: "End of peak (global coordinates)"
5130880b8e0a Uploaded
grau
parents:
diff changeset
15 <secondcol>: the column of <bedfile> containing the peak center position (center) relative to
5130880b8e0a Uploaded
grau
parents:
diff changeset
16 <startcol> or the column of <bedfile> containing the end position (end)
5130880b8e0a Uploaded
grau
parents:
diff changeset
17 <width>: fixed width of all regions
5130880b8e0a Uploaded
grau
parents:
diff changeset
18 <statcol>: the column of <bedfile> containing the peak statistic
5130880b8e0a Uploaded
grau
parents:
diff changeset
19 or a similar measure of confidence
5130880b8e0a Uploaded
grau
parents:
diff changeset
20 <outfile>: the path to the output file, written as FastA
5130880b8e0a Uploaded
grau
parents:
diff changeset
21 USAGE
5130880b8e0a Uploaded
grau
parents:
diff changeset
22 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
23
5130880b8e0a Uploaded
grau
parents:
diff changeset
24
5130880b8e0a Uploaded
grau
parents:
diff changeset
25 my $chromFa = $ARGV[0];
5130880b8e0a Uploaded
grau
parents:
diff changeset
26 my $bed = $ARGV[1];
5130880b8e0a Uploaded
grau
parents:
diff changeset
27 my $chromcol = $ARGV[2]-1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
28 my $startcol = $ARGV[3]-1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
29 my $seccolm = $ARGV[4];
5130880b8e0a Uploaded
grau
parents:
diff changeset
30 my $seccol = $ARGV[5]-1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
31 my $width = $ARGV[6];
5130880b8e0a Uploaded
grau
parents:
diff changeset
32 my $statcol = $ARGV[7]-1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
33 my $outfile = $ARGV[8];
5130880b8e0a Uploaded
grau
parents:
diff changeset
34
5130880b8e0a Uploaded
grau
parents:
diff changeset
35 my $sort = 1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
36
5130880b8e0a Uploaded
grau
parents:
diff changeset
37
5130880b8e0a Uploaded
grau
parents:
diff changeset
38 sub loadSeq{
5130880b8e0a Uploaded
grau
parents:
diff changeset
39 my $prefix = shift;
5130880b8e0a Uploaded
grau
parents:
diff changeset
40 print $prefix," ";
5130880b8e0a Uploaded
grau
parents:
diff changeset
41 open(FA,$chromFa);
5130880b8e0a Uploaded
grau
parents:
diff changeset
42 my $head = "";
5130880b8e0a Uploaded
grau
parents:
diff changeset
43 my @lines = ();
5130880b8e0a Uploaded
grau
parents:
diff changeset
44 while(<FA>){
5130880b8e0a Uploaded
grau
parents:
diff changeset
45 chomp();
5130880b8e0a Uploaded
grau
parents:
diff changeset
46 if(/^>/){
5130880b8e0a Uploaded
grau
parents:
diff changeset
47 if($head){
5130880b8e0a Uploaded
grau
parents:
diff changeset
48 last;
5130880b8e0a Uploaded
grau
parents:
diff changeset
49 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
50 if(/^>\s*(${prefix}|chr${prefix})(\s.*$|$)/i){
5130880b8e0a Uploaded
grau
parents:
diff changeset
51 $head = $_;
5130880b8e0a Uploaded
grau
parents:
diff changeset
52 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
53 }elsif($head){
5130880b8e0a Uploaded
grau
parents:
diff changeset
54 push(@lines,lc($_));
5130880b8e0a Uploaded
grau
parents:
diff changeset
55 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
56 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
57 my $str = join("",@lines);
5130880b8e0a Uploaded
grau
parents:
diff changeset
58 print "loaded\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
59 return $str;
5130880b8e0a Uploaded
grau
parents:
diff changeset
60 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
61
5130880b8e0a Uploaded
grau
parents:
diff changeset
62
5130880b8e0a Uploaded
grau
parents:
diff changeset
63
5130880b8e0a Uploaded
grau
parents:
diff changeset
64 open(IN,$ARGV[1]);
5130880b8e0a Uploaded
grau
parents:
diff changeset
65
5130880b8e0a Uploaded
grau
parents:
diff changeset
66 my @lines = ();
5130880b8e0a Uploaded
grau
parents:
diff changeset
67
5130880b8e0a Uploaded
grau
parents:
diff changeset
68 while(<IN>){
5130880b8e0a Uploaded
grau
parents:
diff changeset
69 chomp();
5130880b8e0a Uploaded
grau
parents:
diff changeset
70 my @parts = split("\t",$_);
5130880b8e0a Uploaded
grau
parents:
diff changeset
71 $parts[$chromcol] =~ s/chr0/chr/g;
5130880b8e0a Uploaded
grau
parents:
diff changeset
72 my @vals = ();
5130880b8e0a Uploaded
grau
parents:
diff changeset
73 if($seccolm eq "center"){
5130880b8e0a Uploaded
grau
parents:
diff changeset
74 @vals = ($parts[$chromcol],$parts[$startcol]+$parts[$seccol],$parts[$statcol]);
5130880b8e0a Uploaded
grau
parents:
diff changeset
75 }else{
5130880b8e0a Uploaded
grau
parents:
diff changeset
76 @vals = ($parts[$chromcol],int(($parts[$startcol]+$parts[$seccol])/2),$parts[$statcol]);
5130880b8e0a Uploaded
grau
parents:
diff changeset
77 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
78 push(@vals,$width);
5130880b8e0a Uploaded
grau
parents:
diff changeset
79 push(@lines,\@vals);
5130880b8e0a Uploaded
grau
parents:
diff changeset
80 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
81
5130880b8e0a Uploaded
grau
parents:
diff changeset
82 close(IN);
5130880b8e0a Uploaded
grau
parents:
diff changeset
83 #print "Read input file ".$bed."\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
84
5130880b8e0a Uploaded
grau
parents:
diff changeset
85
5130880b8e0a Uploaded
grau
parents:
diff changeset
86 if($sort){
5130880b8e0a Uploaded
grau
parents:
diff changeset
87
5130880b8e0a Uploaded
grau
parents:
diff changeset
88 @lines = sort { ${$a}[0] cmp ${$b}[0] } @lines;
5130880b8e0a Uploaded
grau
parents:
diff changeset
89
5130880b8e0a Uploaded
grau
parents:
diff changeset
90 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
91
5130880b8e0a Uploaded
grau
parents:
diff changeset
92 open(OUT,">".$outfile);
5130880b8e0a Uploaded
grau
parents:
diff changeset
93
5130880b8e0a Uploaded
grau
parents:
diff changeset
94 print "Extracting sequences...\n\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
95
5130880b8e0a Uploaded
grau
parents:
diff changeset
96 my $oldchr = "";
5130880b8e0a Uploaded
grau
parents:
diff changeset
97 my $sequence = "";
5130880b8e0a Uploaded
grau
parents:
diff changeset
98 for my $line (@lines){
5130880b8e0a Uploaded
grau
parents:
diff changeset
99 my @ar = @{$line};
5130880b8e0a Uploaded
grau
parents:
diff changeset
100 my $chr = $ar[0];
5130880b8e0a Uploaded
grau
parents:
diff changeset
101 unless($chr eq $oldchr){
5130880b8e0a Uploaded
grau
parents:
diff changeset
102 $sequence = loadSeq($chr);
5130880b8e0a Uploaded
grau
parents:
diff changeset
103 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
104 $oldchr = $chr;
5130880b8e0a Uploaded
grau
parents:
diff changeset
105 my $w = $ar[3];
5130880b8e0a Uploaded
grau
parents:
diff changeset
106 if($w <= 0){
5130880b8e0a Uploaded
grau
parents:
diff changeset
107 print $w," -> next\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
108 next;
5130880b8e0a Uploaded
grau
parents:
diff changeset
109 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
110 if($w % 2 == 0){
5130880b8e0a Uploaded
grau
parents:
diff changeset
111 $w = $w/2;
5130880b8e0a Uploaded
grau
parents:
diff changeset
112 }else{
5130880b8e0a Uploaded
grau
parents:
diff changeset
113 $w = ($w-1)/2;
5130880b8e0a Uploaded
grau
parents:
diff changeset
114 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
115
5130880b8e0a Uploaded
grau
parents:
diff changeset
116 my $start = $ar[1]-$w-1;
5130880b8e0a Uploaded
grau
parents:
diff changeset
117
5130880b8e0a Uploaded
grau
parents:
diff changeset
118 my $head = "> chr: ".$chr."; start: ".$start."; peak: ".($ar[1]-$start)."; signal: ".$ar[2]."\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
119 my $curr = substr($sequence,$start,$ar[3]);
5130880b8e0a Uploaded
grau
parents:
diff changeset
120 if($curr =~ /[^ACGTacgt]/){
5130880b8e0a Uploaded
grau
parents:
diff changeset
121 print "Sequence for\n\t",substr($head,1),"omitted due to ambiguous nucleotides.\n\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
122 }else{
5130880b8e0a Uploaded
grau
parents:
diff changeset
123 print OUT $head,$curr,"\n";
5130880b8e0a Uploaded
grau
parents:
diff changeset
124 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
125 }
5130880b8e0a Uploaded
grau
parents:
diff changeset
126
5130880b8e0a Uploaded
grau
parents:
diff changeset
127 close(OUT);
5130880b8e0a Uploaded
grau
parents:
diff changeset
128 print "\nDone.\n";