-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTileAndConc.py
More file actions
executable file
·91 lines (64 loc) · 2.33 KB
/
Copy pathaddTileAndConc.py
File metadata and controls
executable file
·91 lines (64 loc) · 2.33 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
#!/usr/bin/env python
"""
Add the tile number and concentration to a CPseries file
as columns at the end of the file.
This script requires you to already have quantified images with another pipeline.
Inputs:
CPseries file
Outputs:
CPseries file with tile number and concentration
Ben Ober-Reynolds, boberrey@stanford.edu
20160808
"""
import sys
import os
import argparse
import subprocess
import re
### MAIN ###
def main():
################ Parse input parameters ################
#set up command line argument parser
parser = argparse.ArgumentParser(description='adding tile number and concentration to a CPseries file')
group = parser.add_argument_group('required arguments for processing data')
group.add_argument('-f', '--CPseries_file', required=True,
help='a CPseries file')
group = parser.add_argument_group('optional arguments')
group.add_argument('-c','--conc', default="",
help='concentration to add to CPseries file')
group.add_argument('-nf','--new_file_flag', default="tc",
help='flag to add to new file')
if not len(sys.argv) > 1:
parser.print_help()
sys.exit()
#parse command line arguments
args = parser.parse_args()
filename = os.path.abspath(args.CPseries_file)
if not os.path.isfile(filename):
print "Error: file "+filename+" is not a valid file. Exiting..."
sys.exit()
tilenum = get_tile_number_from_filename(filename)
conc = args.conc
basename = os.path.basename(filename)
path = filename.strip(basename)
newfilename = path+args.new_file_flag+"_"+basename
subprocess.call("awk \'{{print $0 \"\t{}\t{}\"}}\'".format(tilenum, conc)+"<{} > {}".format(filename, newfilename), shell=True)
print "Made new file: "+newfilename
def get_tile_number_from_filename(inFilename):
"""
(from cpfiletools)
Extract the tile number from a provided filename based on the presence of
'tile###'
Input: filename (string)
Output: three digit tile number (string)
"""
# from CPlibs
(path,filename) = os.path.split(inFilename) #split the file into parts
(root,ext) = os.path.splitext(filename)
matches = re.findall('tile[0-9]{1,3}',root.lower())
tileNumber = ''
if matches != []:
tileNumber = '{:03}'.format(int(matches[-1][4:]))
return tileNumber
if __name__ == '__main__':
main()