annotate name_changer.py @ 0:58ad7b512590 draft default tip

Uploaded
author brenninc
date Thu, 12 May 2016 09:48:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
1 #!/usr/bin/env python
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
2
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
3 import optparse
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
4 import os.path
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
5
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
6
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
7 def fix_header_line(start_header, header_line, new_names):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
8 header_parts = header_line.split("\t")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
9 if len(header_parts) <= len(start_header):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
10 raise Exception("Only found {0} columns in second (header) line expected at least {1}.".format(len(header_parts), (len(start_header) + 1)))
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
11 data_headers = header_parts[:len(start_header)]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
12 if data_headers != start_header:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
13 raise Exception("Unexpected start to second (header) line Found: ")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
14 new_header = "\t".join(start_header)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
15 file_headers = header_parts[len(start_header):]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
16 if len(file_headers) != len(new_names):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
17 raise Exception("Found {0} file columns in header line, but {1} new_name paramters provided.".format(len(file_headers), len(new_names)))
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
18 for i in range(len(file_headers)):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
19 new_header += "\t"
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
20 new_header += new_names[i]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
21 new_header += "\n"
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
22 return new_header
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
23
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
24
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
25 def clean_names(prefix, old_names):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
26 if len(old_names) > 1:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
27 shared_start = old_names[0].strip()
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
28 shared_ends = old_names[0].strip()
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
29 for name in old_names:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
30 clean = name.strip()
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
31 while len(shared_start) > 0 and (not clean.startswith(shared_start)):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
32 shared_start = shared_start[:-1]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
33 while len(shared_ends) > 0 and (not clean.endswith(shared_ends)):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
34 shared_ends = shared_ends[1:]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
35 start = len(shared_start)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
36 end = 0 - len(shared_ends)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
37 else:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
38 start = 0
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
39 end = 0
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
40 new_names = []
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
41 if end < 0:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
42 for name in old_names:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
43 new_names.append(prefix + name.strip()[start:end])
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
44 else:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
45 for name in old_names:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
46 new_names.append(prefix + name.strip()[start:])
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
47 return new_names
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
48
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
49
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
50 def main():
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
51 #Parse Command Line
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
52 parser = optparse.OptionParser()
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
53 parser.add_option("--raw_count_file", action="store", type="string", default=None, help="path to file original with the counts")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
54 parser.add_option("--fixed_count_file", action="store", type="string", default=None, help="new path for renamaned counts file")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
55 parser.add_option("--raw_summary_file", action="store", type="string", default=None, help="path to file original with the summary")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
56 parser.add_option("--fixed_summary_file", action="store", type="string", default=None, help="new path for renamaned summary file")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
57 parser.add_option("--names_file", action="store", type="string", default=None, help="path to file which contains the names.")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
58 parser.add_option("--new_name", action="append", type="string", default=None,
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
59 help="Names to be used. Must be the same length as in the raw_count_file")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
60 parser.add_option("--names_prefix", action="store", type="string", default="", help="Prefix to add in from of every name.")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
61
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
62 (options, args) = parser.parse_args()
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
63
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
64 if not os.path.exists(options.raw_count_file):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
65 parser.error("Unable to find raw_count_file {0}.".format(options.raw_count_file))
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
66 if options.names_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
67 if options.new_name:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
68 parser.error("names_file parameter clashes with new_names paramter(s)")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
69 if not os.path.exists(options.names_file):
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
70 parser.error("Unable to find names_file {0}.".format(options.names_file))
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
71 new_names = []
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
72 with open(options.names_file, "r") as names_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
73 for line in names_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
74 new_names.append(line.strip())
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
75 new_names = clean_names(options.names_prefix, new_names)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
76 else:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
77 if not options.new_name:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
78 parser.error("No names_file or new_name paraters provided.")
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
79 new_names = options.new_name
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
80
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
81 print "Changing column names to ", new_names
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
82
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
83 with open(options.raw_count_file, "r") as input_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
84 with open(options.fixed_count_file, "w") as output_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
85 input_file.readline() # job line
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
86 start_header = ["Geneid", "Chr", "Start", "End", "Strand", "Length"]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
87 header_line = fix_header_line(start_header, input_file.readline(), new_names)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
88 output_file.write(header_line)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
89 for line in input_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
90 output_file.write(line)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
91
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
92 with open(options.raw_summary_file, "r") as input_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
93 with open(options.fixed_summary_file, "w") as output_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
94 start_header = ["Status"]
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
95 header_line = fix_header_line(start_header, input_file.readline(), new_names)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
96 output_file.write(header_line)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
97 for line in input_file:
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
98 output_file.write(line)
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
99
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
100
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
101 if __name__ == "__main__":
58ad7b512590 Uploaded
brenninc
parents:
diff changeset
102 main()