1 |
########################################
|
2 |
## author: fmoscato
|
3 |
## script that take an input file containing a list of
|
4 |
## site:dataset
|
5 |
## and group by site
|
6 |
## input are input_file out_dir
|
7 |
##############################################
|
8 |
|
9 |
|
10 |
#!/bin/python
|
11 |
import sys
|
12 |
|
13 |
Usage = """
|
14 |
loadData.py
|
15 |
Options:
|
16 |
--input_file file that contains list of the form site:dataset
|
17 |
--out_dir output directory where print the group by result
|
18 |
"""
|
19 |
|
20 |
|
21 |
|
22 |
def loadData(input_file,out_dir):
|
23 |
|
24 |
fs=open(input_file,"r")
|
25 |
sites={}
|
26 |
lines =fs.readlines()
|
27 |
fs.close()
|
28 |
|
29 |
for f in lines:
|
30 |
ar=f.split(":")
|
31 |
if len(ar)<2:
|
32 |
print f
|
33 |
sys.exit(1)
|
34 |
if ar[0]=="":
|
35 |
ar[0]="EMPTY_MSS"
|
36 |
if ar[0] not in sites:
|
37 |
sites[ar[0]]=[]
|
38 |
sites[ar[0]].append(ar[1])
|
39 |
|
40 |
for i,lst in sites.iteritems():
|
41 |
t=i.replace(" ","")
|
42 |
if t=="": continue
|
43 |
f=open("%s/%s"%(out_dir,t),"w")
|
44 |
for item in lst:
|
45 |
f.write(item)
|
46 |
|
47 |
|
48 |
|
49 |
def main(opts, args):
|
50 |
|
51 |
input_file=''
|
52 |
out_dir=''
|
53 |
|
54 |
for opt, val in opts:
|
55 |
if opt=='--input_file':
|
56 |
input_file = val
|
57 |
if opt=='--out_dir':
|
58 |
out_dir = val
|
59 |
|
60 |
if not input_file or not out_dir:
|
61 |
print Usage
|
62 |
sys.exit(1)
|
63 |
|
64 |
loadData(input_file,out_dir)
|
65 |
|
66 |
if __name__ == '__main__':
|
67 |
import sys
|
68 |
import getopt
|
69 |
|
70 |
try:
|
71 |
opts, args = getopt.getopt(sys.argv[1:], "", ["input_file=","out_dir="])
|
72 |
except getopt.GetoptError, msg:
|
73 |
print
|
74 |
print msg
|
75 |
print
|
76 |
print Usage
|
77 |
sys.exit(1)
|
78 |
|
79 |
if len(opts) <1 :
|
80 |
print
|
81 |
print Usage
|
82 |
sys.exit(1)
|
83 |
|
84 |
|
85 |
main( opts, args)
|
86 |
|