comparison w4mjoinpn.sh @ 0:948bac693947 draft

planemo upload for repository https://github.com/HegemanLab/w4mjoinpn_galaxy_wrapper/tree/master commit cedf2e01903099ef5f1bbe624afe4c2845d6bf23
author eschen42
date Sun, 29 Oct 2017 10:05:05 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:948bac693947
1 #!/bin/bash
2 # join positive and negative ionization-mode XCMS datasets for a common set of samples
3 # summary:
4 # - parse and validate arguments (or abort)
5 # - check that the same samples are present in the same order in both the positive and negative mode data matrices (or abort)
6
7 # Parse arguments
8 # ref: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146
9 POSITIONAL=()
10 while [[ $# -gt 0 ]]; do
11 key="$1"
12 case $key in
13 dmpos)
14 DMPOS="$2"
15 shift # past argument
16 shift # past value
17 ;;
18 dmneg)
19 DMNEG="$2"
20 shift # past argument
21 shift # past value
22 ;;
23 dmout)
24 DMOUT="$2"
25 shift # past argument
26 shift # past value
27 ;;
28 smpos)
29 SMPOS="$2"
30 shift # past argument
31 shift # past value
32 ;;
33 smneg)
34 SMNEG="$2"
35 shift # past argument
36 shift # past value
37 ;;
38 smout)
39 SMOUT="$2"
40 shift # past argument
41 shift # past value
42 ;;
43 vmpos)
44 VMPOS="$2"
45 shift # past argument
46 shift # past value
47 ;;
48 vmneg)
49 VMNEG="$2"
50 shift # past argument
51 shift # past value
52 ;;
53 vmout)
54 VMOUT="$2"
55 shift # past argument
56 shift # past value
57 ;;
58 *) # unknown option
59 POSITIONAL+=("$1") # save it in an array for later
60 shift # past argument
61 ;;
62 esac
63 done
64 set -- "${POSITIONAL[@]}" # restore positional parameters
65 if [[ -n $1 ]]; then
66 echo "unexpected argument $1"
67 echo "arguments supplied: $@"
68 exit 1
69 fi
70
71 # Validate that we got the expected args
72 set -- ${DMPOS} ${DMNEG} ${DMOUT} ${SMPOS} ${SMNEG} ${SMOUT} ${VMPOS} ${VMNEG} ${VMOUT}
73 if [[ ! -n $9 ]]; then
74 echo "expecting nine arguments"
75 echo "parsed arguments: $@"
76 exit 1
77 fi
78
79 # Show them what we got
80 echo "dataMatrix positive_mode ${DMPOS}"
81 echo "dataMatrix negative_mode ${DMNEG}"
82 echo "dataMatrix joined_modes ${DMOUT}"
83 echo "sampleMetadata positive_mode ${SMPOS}"
84 echo "sampleMetadata negative_mode ${SMNEG}"
85 echo "sampleMetadata joined_modes ${SMOUT}"
86 echo "variableMetadata positive_mode ${VMPOS}"
87 echo "variableMetadata negative_mode ${VMNEG}"
88 echo "variableMetadata joined_modes ${VMOUT}"
89
90 # Check that sample names are the same, in the same order, for the dataMatrix in both datasets
91 if [ "$( head -n 1 ${DMPOS} )" != "$( head -n 1 ${DMNEG} )" ]; then echo sample names in dataMatrix files differ; exit 1; fi
92 # Check that sample names are the same, in the same order, for the sampleMetadata in both datasets
93 if [ "$( cut -f 1 ${SMPOS} )" != "$( cut -f 1 ${SMNEG} )" ]; then echo sample names in sampleMetadata files differ; exit 1; fi
94
95 # Concatenate variableMetadata datasets to respective output file
96 cat <( head -n 1 ${VMNEG} ) <( sed -n -e '1 d; s/^/N/; p;' ${VMNEG} ) <( sed -n -e '1 d; s/^/P/; p;' ${VMPOS} ) > ${VMOUT}
97
98 # Concatenate dataMatrix datasets to respective output file
99 cat <( head -n 1 ${DMNEG} ) <( sed -n -e '1 d; s/^/N/; p;' ${DMNEG} ) <( sed -n -e '1 d; s/^/P/; p;' ${DMPOS} ) > ${DMOUT}
100
101 # Determine whether negative ionization-mode sampleMetadata file's column three is titled "polarity"
102
103 # find the ordinal number of the first column named "polarity" of the negative ionization-mode sampleMetadata file, if any
104 set -- `head -n 1 ${SMNEG}`
105 POLARITY=0
106 MAXCOUNT=0
107 while [[ $# -gt 0 ]]; do
108 MAXCOUNT=$(( MAXCOUNT + 1 ))
109 key="$1"
110 case $key in
111 polarity)
112 if [ $POLARITY -eq 0 ]; then POLARITY=${MAXCOUNT}; fi
113 shift # past argument
114 ;;
115 *) # unknown option
116 shift # past argument
117 ;;
118 esac
119 done
120 echo "Polarity is in column $POLARITY of ${SMNEG}"
121 echo "There are $MAXCOUNT columns in ${SMNEG}"
122
123 # Copy sampleMetadata from negative ionization-mode to output file, replacing polarity if possible
124 if [ ${POLARITY} -gt 1 ]; then
125 COLBEFORE=$(( POLARITY - 1 ))
126 COLAFTER=$(( POLARITY + 1 ))
127 # Replace all entries in column three of negative ionization-mode sampleMetadata file with "posneg" in respective output file
128 if [ ${POLARITY} -lt ${MAXCOUNT} ]; then
129 # Handle the case where polarity is not in the last column
130 paste <( cut -f 1-${COLBEFORE} ${SMNEG} ) <( cut -f ${POLARITY} ${SMNEG} | sed -n -e '2,$ s/.*/posneg/; p;' ) <( cut -f ${COLAFTER}- ${SMNEG} ) > ${SMOUT}
131 else
132 # Handle the case where polarity is in the last column
133 paste <( cut -f 1-${COLBEFORE} ${SMNEG} ) <( cut -f ${POLARITY} ${SMNEG} | sed -n -e '2,$ s/.*/posneg/; p;' ) > ${SMOUT}
134 fi
135 else
136 # Handle the case where polarity was not found: Copy negative ionization-mode sampleMetadata file to the respective output file
137 cp ${SMNEG} ${SMOUT}
138 fi
139
140 exit 0