-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomline.py
More file actions
289 lines (280 loc) · 9.88 KB
/
Copy pathcomline.py
File metadata and controls
289 lines (280 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import argparse
import os.path
#import distutils.util
from pathlib import Path
class ComLine():
'Class for implementing command line options'
def __init__(self, args):
parser = argparse.ArgumentParser()
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
filtering = parser.add_argument_group('filtering arguments')
optional = parser.add_argument_group('optional arguments')
conversion = parser.add_argument_group('conversion arguments')
colony = parser.add_argument_group('colony format arguments')
structure = parser.add_argument_group('structure format arguments')
snppit = parser.add_argument_group('snppit format arguments')
required.add_argument("-x", "--infile",
dest='infile',
required=True,
help="Specify either an Excel file (xlsx) or a csv file for input."
)
filtering.add_argument("-D", "--dups",
dest='dups',
action='store_true',
help="Detect duplicate genotypes (can take a while on large files; default = False)."
)
filtering.add_argument("-i", "--pmissind",
dest='pmissind',
type=float,
default=0.2,
help="Enter the maximum allowable proportion of missing data for an individual (default = 0.2)."
)
filtering.add_argument("-I", "--ifi",
dest='ifi',
type=float,
default=2.5,
help="Enter the maximum allowable IFI score for a genotype (default = 2.5)."
)
filtering.add_argument("-k", "--keepdups",
dest='keepdups',
type=str,
default='none',
choices={'all','first','second','none'},
help="Methods for keeping duplicates. 'all' = keep all duplicates; 'first' = keep first encountered; 'second' = keep second; 'none' = keep none (default)"
)
filtering.add_argument("-l", "--pmissloc",
dest='pmissloc',
type=float,
default=0.1,
help="Enter the maximum allowable proportion of missing data for a locus (default = 0.1)."
)
filtering.add_argument("-m", "--monomorphic",
dest='monomorphic',
action='store_true',
help="Turn on filter to remove monomorphic loci."
)
filtering.add_argument("-r", "--removeinds",
dest='removeinds',
help="Specify a list of individuals to remove from the converted files."
)
filtering.add_argument("-R", "--removeloci",
dest='removeloci',
help="Specify a list of individuals to remove from the converted files."
)
filtering.add_argument("-T", "--dupthresh",
dest='dupthresh',
type=int,
default=3,
help="Maximum number of allelic mismatches for identifying duplicate individuals (default = 3)."
)
optional.add_argument("-d", "--sexid",
dest='sexid',
help="Specify a list of loci that are sex-identifying SNPs."
)
optional.add_argument("-o", "--order",
dest='order',
type=str,
default='loci',
choices={'loci','individuals'},
help="Specify whether missing data filters will first be applied to loci or individuals (options: 'loci' = loci filtered first (default), 'individuals' = individuals filtered first)."
)
optional.add_argument("-P", "--keeppops",
dest='keeppops',
help="Provide a text file of populations to retain. One population per line. Populations must match data in 'Population ID' column of your input Excel file."
)
optional.add_argument("-s", "--species",
dest='species',
help="Specify a list of loci that are species identification SNPs."
)
optional.add_argument("-Q", "--identquit",
dest='identquit',
action='store_false',
help="Turn off option to quit the program if identical sample names detected. Program will attempt to force unique names upon these samples."
)
colony.add_argument("-B", "--inbreed",
dest='inbreed',
type=int,
default=0,
choices={0,1},
help="0 = inbreeding absent; 1 = inbreeding present (default = 0)."
)
colony.add_argument("-e", "--droperr",
dest='droperr',
type=float,
default=0.0005,
help="Enter the assumed allelic dropout rate (default = 0.0005)."
)
colony.add_argument("-E", "--genoerr",
dest='genoerr',
type=float,
default=0.0005,
help="Enter the assumed genotyping error rate (default = 0.0005). If the -f/--genoerrfile option is used, the value of -E/--genoerr will override error values < -E/--genoerr."
)
colony.add_argument("-f", "--genoerrfile",
dest='genoerrfile',
help='Specify a list of marker-specific genotyping error rates (optional).'
)
colony.add_argument("-F", "--pfemale",
dest='pfemale',
type=float,
default=0.5,
help="Enter the assumed probability of mother being among candidate parents (default = 0.5). Value is ignored if no candidate mothers provided in the dataset."
)
colony.add_argument("-L", "--runlength",
dest='runlength',
type=int,
default=2,
choices={1,2,3,4},
help="1/2/3/4 = Short/Medium/Long/VeryLong run (default = 2)."
)
colony.add_argument("-M", "--pmale",
dest='pmale',
type=float,
default=0.5,
help="Enter the assumed probability of father being among candidate parents (default = 0.5). Value is ignored if no candidate fathers provided in the dataset."
)
colony.add_argument("-N", "--runname",
dest='runname',
type=str,
default='gtSeqConvert',
help="Run name for colony file format (default = 'gtSeqConvert')"
)
colony.add_argument("-y", "--mpoly",
dest='mpoly',
type=int,
default=0,
choices={0,1},
help="0/1 = Polygamy/Monogamy for males [default = 0 (polygamy)]."
)
colony.add_argument("-Y", "--fpoly",
dest='fpoly',
type=int,
default=0,
choices={0,1},
help="0/1 = Polygamy/Monogamy for females [default = 0 (polygamy)]."
)
structure.add_argument("-H", "--header",
dest='header',
action='store_false',
help="Turn off printing of header line with locus names for Structure output"
)
structure.add_argument("-t", "--twoline",
dest='twoline',
action='store_true',
help="Turn on twoline format version for Structure output"
)
conversion.add_argument("-a", "--allelematch",
dest='allelematch',
action='store_true',
help="Write allelematch format file."
)
conversion.add_argument("-b", "--binary",
dest='binary',
action='store_true',
help="Write binary format file."
)
conversion.add_argument("-c", "--coancestry",
dest='coancestry',
action='store_true',
help="Write coancestry format file."
)
conversion.add_argument("-C", "--colony",
dest='colony',
action='store_true',
help="Write colony format file."
)
conversion.add_argument("-g", "--genepop",
dest='genepop',
action='store_true',
help="Write genepop format file."
)
conversion.add_argument("-G", "--grandma",
dest='grandma',
action='store_true',
help="Write gRandma format file."
)
conversion.add_argument("-n", "--newhybrids",
dest='newhybrids',
action='store_true',
help="Write NewHybrids format file."
)
conversion.add_argument("-p", "--plink",
dest='plink',
action='store_true',
help="Write Plink format file."
)
conversion.add_argument("-q", "--sequoia",
dest='sequoia',
action='store_true',
help="Write Sequoia format file."
)
conversion.add_argument("-S", "--structure",
dest='structure',
action='store_true',
help="Write Structure format file."
)
conversion.add_argument("-u", "--rubias",
dest='rubias',
action='store_true',
help="Write rubias format file."
)
conversion.add_argument("-X", "--xlsx",
dest='xlsx',
action='store_true',
help="Write filtered Excel format file."
)
conversion.add_argument("-z", "--snppit",
dest='snppit',
action='store_true',
help="Write SNPPIT format file."
)
snppit.add_argument("-Z", "--snppitmap",
dest='snppitmap',
help="Provide a tab-delimited file specifying POP and OFFSPRING groups for SNPPIT format. Required if converting a SNPPIT file."
)
self.args = parser.parse_args()
#check if at least one conversion option was used.
if not [x for x in (self.args.allelematch, self.args.binary, self.args.coancestry, self.args.colony, self.args.genepop, self.args.grandma, self.args.newhybrids, self.args.plink, self.args.sequoia, self.args.structure, self.args.rubias, self.args.snppit, self.args.xlsx) if x is True]:
print("")
print("No format conversion options were selected.")
print("You must choose at least one file format for output.")
print("")
raise SystemExit
#check if input file ends with .xlsx
fileSuffix = Path(self.args.infile).suffix.lower()
if fileSuffix not in [".xlsx", ".csv"]:
print("ERROR: Input file " + self.args.infile + " does not end with .csv or .xlsx file extension.")
print("Is this a valid input file?")
print("Exiting Program...")
print("")
raise SystemExit
#check if files exist
self.exists( self.args.infile )
if self.args.species:
self.exists(self.args.species)
if self.args.sexid:
self.exists(self.args.sexid)
if self.args.removeinds:
self.exists(self.args.removeinds)
if self.args.removeloci:
self.exists(self.args.removeloci)
if self.args.keeppops:
self.exists(self.args.keeppops)
if self.args.genoerrfile:
self.exists(self.args.genoerrfile)
if self.args.snppit == True:
if self.args.snppitmap is None:
print("")
print("If doing a snppit conversion you must also specify a POP and OFFSPRING groups file using the -Z option.")
print("")
raise SystemExit
else:
self.exists(self.args.snppitmap)
def exists(self, filename):
if( os.path.isfile(filename) != True ):
print("")
print(filename, "does not exist")
print("Exiting program...")
print("")
raise SystemExit