-
Notifications
You must be signed in to change notification settings - Fork 3
/
dirSplit.py
executable file
·371 lines (284 loc) · 8.59 KB
/
dirSplit.py
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
# !/bin/sh
from collections import defaultdict
import re
import os
import textwrap
import argparse
import urllib.request
import ssl
import sys
# TODO: in this version, I made pfam cross-validation optional
def ribosome(seq):
NTs = ['T', 'C', 'A', 'G']
stopCodons = ['TAA', 'TAG', 'TGA']
Codons = []
for i in range(4):
for j in range(4):
for k in range(4):
codon = NTs[i] + NTs[j] + NTs[k]
# if not codon in stopCodons:
Codons.append(codon)
CodonTable = {}
AAz = "FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG"
AAs = list(AAz)
k = 0
for base1 in NTs:
for base2 in NTs:
for base3 in NTs:
codon = base1 + base2 + base3
CodonTable[codon] = AAs[k]
k += 1
prot = []
for j in range(0, len(seq), 3):
codon = seq[j:j + 3]
try:
prot.append(CodonTable[codon])
except KeyError:
prot.append("X")
protein = ("".join(prot))
return protein
def reverseComplement(seq):
out = []
for i in range(len(seq)-1, -1, -1):
nucleotide = seq[i]
if nucleotide == "C":
nucleotide = "G"
elif nucleotide == "G":
nucleotide = "C"
elif nucleotide == "T":
nucleotide = "A"
elif nucleotide == "A":
nucleotide = "T"
out.append(nucleotide)
outString = "".join(out)
return outString
def cluster(data, maxgap):
'''Arrange data into groups where successive elements
differ by no more than *maxgap*
#->>> cluster([1, 6, 9, 100, 102, 105, 109, 134, 139], maxgap=10)
[[1, 6, 9], [100, 102, 105, 109], [134, 139]]
#->>> cluster([1, 6, 9, 99, 100, 102, 105, 134, 139, 141], maxgap=10)
[[1, 6, 9], [99, 100, 102, 105], [134, 139, 141]]
'''
# data = sorted(data)
data.sort(key=int)
groups = [[data[0]]]
for x in data[1:]:
if abs(x - groups[-1][-1]) <= maxgap:
groups[-1].append(x)
else:
groups.append([x])
return groups
def lastItem(ls):
x = ''
for i in ls:
x = i
return x
def RemoveDuplicates(ls):
empLS = []
counter = 0
for i in ls:
if i not in empLS:
empLS.append(i)
else:
pass
return empLS
def allButTheLast(iterable, delim):
x = []
count = 0
length = len(iterable.split(delim))
for i in range(0, length):
count += 1
x.append(iterable.split(delim)[i])
if count == length:
x.append(delim)
return delim.join(x[0:len(x)-2])
def secondToLastItem(ls):
x = ''
for i in ls[0:len(ls)-1]:
x = i
return x
def pull(item, one, two):
ls = []
counter = 0
for i in item:
if counter == 0:
if i != one:
pass
else:
counter += 1
ls.append(i)
else:
if i != two:
ls.append(i)
else:
ls.append(i)
counter = 0
outstr = "".join(ls)
return outstr
def stabilityCounter(int):
if len(str(int)) == 1:
string = (str(0) + str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 2:
string = (str(0) + str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 3:
string = (str(0) + str(0) + str(int))
return (string)
if len(str(int)) == 4:
string = (str(0) + str(int))
return (string)
def replace(stringOrlist, list, item):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
emptyList.append(item)
outString = "".join(emptyList)
return outString
def remove(stringOrlist, list):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
pass
outString = "".join(emptyList)
return outString
def removeLS(stringOrlist, list):
emptyList = []
for i in stringOrlist:
if i not in list:
emptyList.append(i)
else:
pass
return emptyList
def fasta(fasta_file):
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
header = header.split(" ")[0]
seq = ''
else:
header = i[1:]
header = header.split(" ")[0]
seq = ''
else:
seq += i
Dict[header] = seq
# print(count)
return Dict
def filter(list, items):
outLS = []
for i in list:
if i not in items:
outLS.append(i)
return outLS
def delim(line):
ls = []
string = ''
for i in line:
if i != " ":
string += i
else:
ls.append(string)
string = ''
ls = filter(ls, [""])
return ls
parser = argparse.ArgumentParser(
prog="dirSplit.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
*******************************************************
Script for splitting a directory into multiple directories, each containing a subset of files
Developed by Arkadiy Garber: [email protected]
*******************************************************
'''))
parser.add_argument('-d', type=str, help='directory to split')
parser.add_argument('-b', type=str, help='number of batches to split into', default=2)
parser.add_argument('-f', type=str, help='comma-separated list of filename extensions to look for')
parser.add_argument('--gz', type=str,
help="files are gzipped", const=True, nargs="?")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
DIR = os.listdir(args.d)
extensions = args.f
extensionsList = extensions.split(",")
if args.gz:
count = 0
for i in DIR:
file = allButTheLast(i, ".")
if lastItem(file.split(".")) == extensionsList[0]:
count += 1
print("total files with the %s extension: %s" % (extensionsList[0], str(count)))
batches = count / int(args.b)
batchSize = int(round(batches, 0))
os.system("sleep 1")
print("splitting into %s batches, each with about %s files" % (args.b, str(batchSize)))
os.system("sleep 1\n")
count = 1
DIRdict = defaultdict(list)
for i in DIR:
file = allButTheLast(i, ".")
extension = lastItem(file.split("."))
if len(DIRdict[count]) == batchSize:
print("batch_%s filled" % str(count))
count += 1
if extension == extensionsList[0]:
DIRdict[count].append(i)
else:
if extension == extensionsList[0]:
DIRdict[count].append(i)
print("")
for i in DIRdict:
os.system("mkdir batch_%s" % str(i))
print(i)
for j in DIRdict[i]:
file = allButTheLast(j, ".")
basename = allButTheLast(file, ".")
for k in extensionsList:
outfile = basename + "." + k + ".gz"
os.system("mv %s/%s batch_%s/%s > /dev/null 2>&1" % (args.d, outfile, i, outfile))
else:
count = 0
for i in DIR:
if lastItem(i.split(".")) == extensionsList[0]:
count += 1
print("total files with the %s extension: %s" % (extensionsList[0], str(count)))
batches = count/int(args.b)
batchSize = int(round(batches, 0))
os.system("sleep 1")
print("splitting into %s batches, each with about %s files" % (args.b, str(batchSize)))
os.system("sleep 1\n")
count = 1
DIRdict = defaultdict(list)
for i in DIR:
extension = lastItem(i.split("."))
if len(DIRdict[count]) == batchSize:
print("batch_%s filled" % str(count))
count += 1
if extension == extensionsList[0]:
DIRdict[count].append(i)
else:
if extension == extensionsList[0]:
DIRdict[count].append(i)
print("")
for i in DIRdict:
os.system("mkdir batch_%s" % str(i))
print(i)
for j in DIRdict[i]:
basename = allButTheLast(j, ".")
for k in extensionsList:
file = basename + "." + k
os.system("mv %s/%s batch_%s/%s > /dev/null 2>&1" % (args.d, file, i, file))