annotate hilbertvis.sh @ 0:6f6859904b65 draft

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