0
|
1 #!/usr/bin/env python
|
|
2 """
|
|
3 Validate the metadata file associated with Affymetrix 96 well plate data.
|
|
4 """
|
|
5 import argparse
|
|
6 import datetime
|
|
7 import decimal
|
|
8 import re
|
|
9 import shutil
|
|
10 import sys
|
|
11
|
|
12 parser = argparse.ArgumentParser()
|
|
13 parser.add_argument('--input', dest='input', help='Metadata file for Affymetrix 96 well plate data')
|
|
14 parser.add_argument('--output', dest='output', help='Output dataset'),
|
|
15 args = parser.parse_args()
|
|
16
|
|
17 EMAIL_MAX_LEN = 255
|
|
18 VALID_EMAIL_RE = re.compile("[^@]+@[^@]+\.[^@]+")
|
|
19
|
|
20
|
|
21 def add_error_msg(accumulated_msgs, msg):
|
|
22 return "%s\n%s" % (accumulated_msgs, msg)
|
|
23
|
|
24
|
|
25 def empty_value(line_no, label, accumulated_msgs):
|
|
26 return add_error_msg(accumulated_msgs, "The required %s value is missing on line %d." % (label, line_no))
|
|
27
|
|
28
|
|
29 def stop_error(msg):
|
|
30 sys.exit(msg)
|
|
31
|
|
32
|
9
|
33 def string_as_boolean_string(string):
|
|
34 if str(string).lower() in ['true', 'yes', 'on', '1']:
|
|
35 return 'True'
|
|
36 else:
|
|
37 return 'False'
|
|
38
|
|
39
|
0
|
40 def validate_date_string(line_no, date_string, accumulated_msgs):
|
9
|
41 if len(date_string) == 0:
|
|
42 return accumulated_msgs
|
0
|
43 try:
|
9
|
44 datetime.datetime.strptime(date_string, '%Y-%m-%d')
|
0
|
45 return accumulated_msgs
|
|
46 except ValueError:
|
9
|
47 return add_error_msg(accumulated_msgs, "Line %d contains an incorrect date format (%s must be YYYY-MM-DD)." % (line_no, date_string))
|
0
|
48
|
|
49
|
|
50 def validate_decimal(line_no, decimal_string, accumulated_msgs):
|
|
51 try:
|
|
52 decimal.Decimal(decimal_string)
|
|
53 return accumulated_msgs
|
|
54 except Exception:
|
|
55 return add_error_msg(accumulated_msgs, "Line %d contains an incorrect decimal value (%s)." % (line_no, decimal_string))
|
|
56
|
|
57
|
|
58 def validate_email(line_no, email, accumulated_msgs):
|
|
59 if not (VALID_EMAIL_RE.match(email)):
|
|
60 return add_error_msg(accumulated_msgs, "Line %d contains an invalid email address (%s). " % (line_no, email))
|
|
61 elif len(email) > EMAIL_MAX_LEN:
|
|
62 return add_error_msg(accumulated_msgs, "Line %d contains an email address (%) that is longer than the maximum length, %d characters." % (line_no, email))
|
|
63 return accumulated_msgs
|
|
64
|
|
65
|
|
66 accumulated_msgs = ""
|
|
67 # Parse the input file, skipping the header, and validating
|
|
68 # that each data line consists of 31 comma-separated items.
|
|
69 with open(args.input, "r") as ih:
|
1
|
70 for i, line in enumerate(ih):
|
0
|
71 if i == 0:
|
|
72 # Skip the header.
|
|
73 continue
|
|
74 line = line.rstrip("\r\n")
|
|
75 if i > 97:
|
9
|
76 accumulated_msgs = add_error_msg(accumulated_msgs, "The input file contains more than 97 lines (must be 1 header line and no more than 96 data lines).")
|
0
|
77 stop_error(accumulated_msgs)
|
17
|
78 items = line.split("\t")
|
23
|
79 if len(items) != 32:
|
21
|
80 accumulated_msgs = add_error_msg(accumulated_msgs, "Line %d contains %s columns, (must be 30)." % (i, len(items)))
|
2
|
81 stop_error(accumulated_msgs)
|
0
|
82 # Required and validated.
|
|
83 # Required.
|
18
|
84 user_specimen_id = items[0]
|
0
|
85 if len(user_specimen_id) == 0:
|
|
86 accumulated_msgs = empty_value(i, "user_specimen_id", accumulated_msgs)
|
|
87 # Optional.
|
18
|
88 field_call = items[1]
|
0
|
89 # Optional.
|
18
|
90 bcoral_genet_id = items[2]
|
0
|
91 # Optional.
|
18
|
92 bsym_genet_id = items[3]
|
0
|
93 # Required.
|
18
|
94 reef = items[4]
|
0
|
95 if len(reef) == 0:
|
|
96 accumulated_msgs = empty_value(i, "reef", accumulated_msgs)
|
|
97 # Required.
|
18
|
98 region = items[5]
|
0
|
99 if len(region) == 0:
|
|
100 accumulated_msgs = empty_value(i, "region", accumulated_msgs)
|
|
101 # Required and validated.
|
18
|
102 latitude = items[6]
|
0
|
103 accumulated_msgs = validate_decimal(i, latitude, accumulated_msgs)
|
|
104 # Required and validated.
|
18
|
105 longitude = items[7]
|
0
|
106 accumulated_msgs = validate_decimal(i, longitude, accumulated_msgs)
|
|
107 # Optional.
|
18
|
108 geographic_origin = items[8]
|
9
|
109 # Optional.
|
18
|
110 sample_location = items[9]
|
0
|
111 # Optional.
|
18
|
112 latitude_outplant = items[10]
|
0
|
113 # Optional.
|
18
|
114 longitude_outplant = items[11]
|
0
|
115 # Optional.
|
18
|
116 depth = items[12]
|
0
|
117 # Optional.
|
18
|
118 dist_shore = items[13]
|
0
|
119 # Optional.
|
18
|
120 disease_resist = items[14]
|
0
|
121 # Optional.
|
18
|
122 bleach_resist = items[15]
|
0
|
123 # Optional.
|
18
|
124 mortality = items[16]
|
0
|
125 # Optional.
|
18
|
126 tle = items[17]
|
0
|
127 # Optional.
|
18
|
128 spawning = string_as_boolean_string(items[18])
|
9
|
129 # Required.
|
18
|
130 collector_last_name = items[19]
|
9
|
131 if len(collector_last_name) == 0:
|
|
132 accumulated_msgs = empty_value(i, "collector_last_name", accumulated_msgs)
|
0
|
133 # Required.
|
18
|
134 collector_first_name = items[20]
|
9
|
135 if len(collector_first_name) == 0:
|
|
136 accumulated_msgs = empty_value(i, "collector_first_name", accumulated_msgs)
|
0
|
137 # Required.
|
18
|
138 org = items[21]
|
0
|
139 if len(org) == 0:
|
|
140 accumulated_msgs = empty_value(i, "org", accumulated_msgs)
|
|
141 # Required and validated.
|
18
|
142 collection_date = items[22]
|
|
143 accumulated_msgs = validate_date_string(i, collection_date, accumulated_msgs)
|
0
|
144 # Required and validated.
|
18
|
145 contact_email = items[23]
|
5
|
146 accumulated_msgs = validate_email(i, contact_email, accumulated_msgs)
|
0
|
147 # Required.
|
18
|
148 seq_facility = items[24]
|
0
|
149 if len(seq_facility) == 0:
|
|
150 accumulated_msgs = empty_value(i, "seq_facility", accumulated_msgs)
|
|
151 # Optional.
|
18
|
152 array_version = items[25]
|
0
|
153 # Optional.
|
18
|
154 public = string_as_boolean_string(items[26])
|
0
|
155 # Optional.
|
18
|
156 public_after_date = items[27]
|
13
|
157 accumulated_msga = validate_date_string(i, public_after_date, accumulated_msgs)
|
21
|
158 # Required and validated.
|
|
159 sperm_motility = items[28]
|
|
160 accumulated_msgs = validate_decimal(i, sperm_motility, accumulated_msgs)
|
|
161 # Required and validated.
|
|
162 healing_time = items[29]
|
|
163 accumulated_msgs = validate_decimal(i, healing_time, accumulated_msgs)
|
23
|
164 # Optional.
|
|
165 dna_extraction_method = items[9]
|
|
166 # Optional.
|
|
167 dna_concentration = items[31]
|
|
168 # If dna_concentration has a value, then it must be decimal.
|
|
169 if len(dna_concentration) > 0:
|
|
170 accumulated_msgs = validate_decimal(i, dna_concentration, accumulated_msgs)
|
|
171
|
0
|
172
|
6
|
173 if len(accumulated_msgs) > 0:
|
0
|
174 stop_error(accumulated_msgs)
|
|
175
|
8
|
176 shutil.copyfile(args.input, args.output)
|