annotate tools/human_genome_variation/hilbertvis.sh @ 2:c2a356708570

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:42 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env bash
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 input_file="$1"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 output_file="$2"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 chromInfo_file="$3"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 chrom="$4"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 score_col="$5"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 hilbert_curve_level="$6"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 summarization_mode="$7"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 chrom_col="$8"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 start_col="$9"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 end_col="${10}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 strand_col="${11}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 ## use first sequence if chrom filed is empty
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 if [ -z "$chrom" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 chrom=$( head -n 1 "$input_file" | cut -f$chrom_col )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 ## get sequence length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 if [ ! -r "$chromInfo_file" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 echo "Unable to read chromInfo_file $chromInfo_file" 1>&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 chrom_len=$( awk '$1 == chrom {print $2}' chrom=$chrom $chromInfo_file )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 ## error if we can't find the chrom_len
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 if [ -z "$chrom_len" ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 echo "Can't find length for sequence \"$chrom\" in chromInfo_file $chromInfo_file" 1>&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 ## make sure chrom_len is positive
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if [ $chrom_len -le 0 ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 echo "sequence \"$chrom\" length $chrom_len <= 0" 1>&2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 exit 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 ## modify R script depending on the inclusion of a score column, strand information
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 input_cols="\$${start_col}, \$${end_col}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 col_types='beg=0, end=0, strand=""'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 # if strand_col == 0 (strandCol metadata is not set), assume everything's on the plus strand
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if [ $strand_col -ne 0 ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 input_cols="${input_cols}, \$${strand_col}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 else
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 input_cols="${input_cols}, \\\"+\\\""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 # set plot value (either from data or use a constant value)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if [ $score_col -eq -1 ]; then
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 value=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 else
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 input_cols="${input_cols}, \$${score_col}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 col_types="${col_types}, score=0"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 value='chunk$score[i]'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 fi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 R --vanilla &> /dev/null <<endOfCode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 library(HilbertVis);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 chrom_len <- ${chrom_len};
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 chunk_size <- 1000;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 interval_count <- 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 invalid_strand <- 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 awk_cmd <- paste(
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 "awk 'BEGIN{FS=\"\t\";OFS=\"\t\"}",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 "\$${chrom_col} == \"${chrom}\"",
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 "{print ${input_cols}}' ${input_file}"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 );
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 col_types <- list(${col_types});
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 vec <- vector(mode="numeric", length=chrom_len);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 conn <- pipe(description=awk_cmd, open="r");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 repeat {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 chunk <- scan(file=conn, what=col_types, sep="\t", nlines=chunk_size, quiet=TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 if ((rows <- length(chunk\$beg)) == 0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 break;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 interval_count <- interval_count + rows;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 for (i in 1:rows) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 if (chunk\$strand[i] == '+') {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 start <- chunk\$beg[i] + 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 stop <- chunk\$end[i];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 } else if (chunk\$strand[i] == '-') {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 start <- chrom_len - chunk\$end[i] - 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 stop <- chrom_len - chunk\$beg[i];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 invalid_strand <- invalid_strand + 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 interval_count <- interval_count - 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 next;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 vec[start:stop] <- ${value};
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 close(conn);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 hMat <- hilbertImage(vec, level=$hilbert_curve_level, mode="$summarization_mode");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 pdf(file="$output_file", onefile=TRUE, width=8, height=10.5, paper="letter");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 showHilbertImage(hMat);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 dev.off();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 endOfCode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109