-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitTimefilesToDirectories.py
More file actions
executable file
·121 lines (85 loc) · 3.57 KB
/
Copy pathsplitTimefilesToDirectories.py
File metadata and controls
executable file
·121 lines (85 loc) · 3.57 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
#!/usr/bin/env python
""" Split a folder of images into multiple directories
for use in classic quantification pipeline
Currently will assume that you have as the same number
of images for each tile.
Inputs:
Directory with image files(.tif/.tiff files)
Outputs:
Image files moved into new directories such that
each new directory has only one image per tile
Ben Ober-Reynolds, boberrey@stanford.edu
Started 20160802, last changed 20160830
"""
import sys
import os
import argparse
import cpfiletools
def main():
################ Parse input parameters ################
#set up command line argument parser
parser = argparse.ArgumentParser(description='Script for splitting a directory of timed files (e.g. images or CPfluors) into multiple directories')
group = parser.add_argument_group('required arguments for processing data')
group.add_argument('-fd', '--file_directory', required=True,
help='directory that holds files to be split')
group.add_argument('-ext', '--extension', required=True,
help='extension of files to be split')
group = parser.add_argument_group('optional arguments for processing data')
group.add_argument('-p','--prefix', default="set",
help='prefix for new directories. default = set')
group.add_argument('-od','--output_directory', default='file_directory',
help='directory in which new directories will be made')
group.add_argument('-a','--action', default='l',
help='what to do with the images (m = move, l = symbolic link). Default is to link.')
if not len(sys.argv) > 1:
parser.print_help()
sys.exit()
#parse command line arguments
args = parser.parse_args()
if args.action != "m" and args.action != "l":
print "Error: action must be either 'm' (move) or 'l' (link)!"
sys.exit()
# Gather the files in the provided image directory
print "Finding files in directory {}...".format(args.file_directory)
imageFiles = cpfiletools.find_files_in_directory(args.file_directory, [args.extension])
if len(imageFiles) < 1:
print "Error: no files found in directory {} matching extension {}. Exiting...".format(args.file_directory, args.extension)
sys.exit()
# Make a dictionary of all the image files keyed by tile number
imageDirectory = os.path.abspath(args.file_directory)
imageDict = cpfiletools.make_tile_dict_multiple(imageFiles, imageDirectory)
tileList = imageDict.keys()
numImagesPerTile = len(imageDict[tileList[0]])
# now make new directories to hold split images:
if args.output_directory == 'file_directory':
outputPath = args.file_directory
else:
outputPath = args.output_directory
if not os.path.exists(outputPath):
print "Error: directory {} does not exist!".format(outputPath)
newDirList = []
for n in range(numImagesPerTile):
dirname = outputPath+args.prefix+"{:02}".format(n+1)
os.mkdir(dirname)
newDirList.append(dirname)
print "made directory: {}".format(dirname)
# Now that directories are made, move images into those directories (or link)
count = 0
while count < numImagesPerTile:
for tile in tileList:
fullFileName = imageDict[tile].pop(0)
prevPath, fileName = os.path.split(fullFileName)
if args.action == "m":
os.rename(fullFileName, newDirList[count]+"/"+fileName)
if args.action == "l":
os.symlink(fullFileName, newDirList[count]+"/"+fileName)
count += 1
print "Files split successfully"
def printDict(d):
for key in d.keys():
images = d[key]
print "tile {}".format(key)
for image in images:
print "\t" + image
if __name__ == '__main__':
main()