forked from enormandeau/Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correspondance_sort.py
executable file
·166 lines (133 loc) · 4.39 KB
/
correspondance_sort.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Put 2 files (each with 2 columns: ID and value) in the same order
# Using a third file giving the correspondances (2 columns)
__authors__ = "Eric Normandeau"
__program_name__ = "correspondance_sort"
__version_info__ = ('0', '0', '1')
__version__ = '.'.join(__version_info__)
__copyright__ = "Copyright (c) 2011 Eric Normandeau"
__license__ = "GPLv3"
__revision_date__ = "2011-03-02"
# Importing modules
import os
import sys
import getopt
import platform
# Function definitions
def help():
"""Help attained by typing './program_name.py -h'
"""
_plateform = platform.system()
name = __program_name__
text = """
%s(1) User Commands %s(1)
\033[1mNAME\033[0m
\t%s - Sort the content of 2 files
\033[1mSYNOPSIS\033[0m
\t\033[1mpython %s.py \033[0m[\033[4mOPTION\033[0m] [\033[4mFILE\033[0m]...
\033[1mDESCRIPTION\033[0m
\tSort 2 files according to a third containing correspondances
\t%s Takes 3 files in. 2 contain IDs and values and a third
\tcontains correspondance for the IDs of the 2 first files. It builds
\ta new table with all the data and outputs it in a new file
\033[1mOPTIONS\033[0m
\t\033[1m-h, --help\033[0m
\t\tDisplay the help of this program
\t\033[1m-d, --data1\033[0m
\t\tFirst data file
\t\t2 columns, tab separated (ID, value)
\t\033[1m-D, --data2\033[0m
\t\tSecond data file
\t\t2 columns, tab separated (ID, value)
\t\033[1m-c, --correspondance\033[0m
\t\tCorrespondance file
\t\t2 columns, tab separated (ID1, ID2)
\t\033[1m-o, --output\033[0m
\t\tOutput file
\033[1mAUTHORS\033[0m
\t%s
%s %s %s %s(1)
"""%(name, name, name, name, name, __authors__, name, __version__, \
__revision_date__, name)
if _plateform != 'Windows' and "this is great news":
print text
else:
__Windows__ = "This is an abomination"
remove = ["\033[1m","\033[0m","\033[4m"]
for i in remove:
text = text.replace(i, "")
print text
del(__Windows__) # If only we could...
# Describe the program's goal in a haiku
# --------------------------------------
# Correspondance_sort
# Does its very best to help
# At a borring task
# The program itself
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "hd:D:c:o:", ["help",
"data1=", "data2=", "correspondance=", "output="])
except getopt.GetoptError, e:
print "Input error. Use -h for help"
sys.exit(0)
for option, value in opts:
if option in ('-h', '--help'):
help()
sys.exit(0)
elif option in ('-d', '--data1'):
input_data1 = value
elif option in ('-D', '--data2'):
input_data2 = value
elif option in ('-c', '--correspondance'):
input_corr = value
elif option in ('-o', '--output'):
output_file = value
try:
with open(input_data1) as test:
pass
with open(input_data2) as test:
pass
with open(input_corr) as test:
pass
except:
print "Input Error: One of the input files could not be found"
print "Use -h for help."
sys.exit(0)
try:
with open(output_file, "w") as test:
pass
except:
print "Output Error: No output file specified or incorect path."
print "Use -h for help."
sys.exit(0)
print "Using version:", __version__, "of", __program_name__
print "Last revision:", __revision_date__
print "Authors:", __authors__
print __copyright__
print "Licence:", __license__
print "Hello, program tester!"
data = {}
with open(input_data1) as f:
for line in f:
l = line.strip()
if l != "":
l = l.split("\t")
data[l[0]] = l[1]
with open(input_data2) as f:
for line in f:
l = line.strip()
if l != "":
l = l.split("\t")
data[l[0]] = l[1]
with open(input_corr) as f:
with open(output_file, "w") as out_f:
for line in f:
l = line.strip()
if l != "":
l = l.split("\t")
try:
out_f.write("\t".join([l[0], data[l[0]], l[1], data[l[1]]]) + "\n")
except:
print "Found a missing corresponcance combination"