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)
|
3
|
78 items = line.split(",")
|
9
|
79 if len(items) != 29:
|
|
80 accumulated_msgs = add_error_msg(accumulated_msgs, "Line %d contains %s columns, (must be 29)." % (i, len(items)))
|
2
|
81 stop_error(accumulated_msgs)
|
0
|
82 # Required and validated.
|
9
|
83 date_entered_db = items[0]
|
4
|
84 accumulated_msgs = validate_date_string(i, date_entered_db, accumulated_msgs)
|
0
|
85 # Required.
|
9
|
86 user_specimen_id = items[1]
|
0
|
87 if len(user_specimen_id) == 0:
|
|
88 accumulated_msgs = empty_value(i, "user_specimen_id", accumulated_msgs)
|
|
89 # Optional.
|
9
|
90 field_call = items[2]
|
0
|
91 # Optional.
|
9
|
92 bcoral_genet_id = items[3]
|
0
|
93 # Optional.
|
9
|
94 bsym_genet_id = items[4]
|
0
|
95 # Required.
|
9
|
96 reef = items[5]
|
0
|
97 if len(reef) == 0:
|
|
98 accumulated_msgs = empty_value(i, "reef", accumulated_msgs)
|
|
99 # Required.
|
9
|
100 region = items[6]
|
0
|
101 if len(region) == 0:
|
|
102 accumulated_msgs = empty_value(i, "region", accumulated_msgs)
|
|
103 # Required and validated.
|
9
|
104 latitude = items[7]
|
0
|
105 accumulated_msgs = validate_decimal(i, latitude, accumulated_msgs)
|
|
106 # Required and validated.
|
9
|
107 longitude = items[8]
|
0
|
108 accumulated_msgs = validate_decimal(i, longitude, accumulated_msgs)
|
|
109 # Optional.
|
9
|
110 geographic_origin = items[9]
|
|
111 # Optional.
|
|
112 sample_location = items[10]
|
0
|
113 # Optional.
|
9
|
114 latitude_outplant = items[11]
|
0
|
115 # Optional.
|
9
|
116 longitude_outplant = items[12]
|
0
|
117 # Optional.
|
9
|
118 depth = items[13]
|
0
|
119 # Optional.
|
9
|
120 dist_shore = items[14]
|
0
|
121 # Optional.
|
9
|
122 disease_resist = items[15]
|
0
|
123 # Optional.
|
9
|
124 bleach_resist = items[16]
|
0
|
125 # Optional.
|
9
|
126 mortality = items[17]
|
0
|
127 # Optional.
|
9
|
128 tle = items[18]
|
0
|
129 # Optional.
|
9
|
130 spawning = string_as_boolean_string(items[19])
|
|
131 # Required.
|
|
132 collector_last_name = items[21]
|
|
133 if len(collector_last_name) == 0:
|
|
134 accumulated_msgs = empty_value(i, "collector_last_name", accumulated_msgs)
|
0
|
135 # Required.
|
9
|
136 collector_first_name = items[22]
|
|
137 if len(collector_first_name) == 0:
|
|
138 accumulated_msgs = empty_value(i, "collector_first_name", accumulated_msgs)
|
0
|
139 # Required.
|
9
|
140 org = items[23]
|
0
|
141 if len(org) == 0:
|
|
142 accumulated_msgs = empty_value(i, "org", accumulated_msgs)
|
|
143 # Required and validated.
|
9
|
144 collection_date = items[24]
|
4
|
145 accumulated_msgs = validate_date_string(i, date_entered_db, accumulated_msgs)
|
0
|
146 # Required and validated.
|
9
|
147 contact_email = items[25]
|
5
|
148 accumulated_msgs = validate_email(i, contact_email, accumulated_msgs)
|
0
|
149 # Required.
|
9
|
150 seq_facility = items[26]
|
0
|
151 if len(seq_facility) == 0:
|
|
152 accumulated_msgs = empty_value(i, "seq_facility", accumulated_msgs)
|
|
153 # Optional.
|
9
|
154 array_version = items[27]
|
0
|
155 # Optional.
|
9
|
156 public = string_as_boolean_string(items[28])
|
0
|
157 # Optional.
|
11
|
158 public_after_date = items[29]
|
9
|
159 accumulated_msga = validate_date_string(public_after_date)
|
0
|
160
|
6
|
161 if len(accumulated_msgs) > 0:
|
0
|
162 stop_error(accumulated_msgs)
|
|
163
|
8
|
164 shutil.copyfile(args.input, args.output)
|