comparison manipulate/minimal_kcf/minimal_kcf.py @ 0:89592faa2875 draft

Uploaded
author chrisb
date Wed, 23 Mar 2016 14:35:56 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:89592faa2875
1 __author__ = "Chris Barnett"
2 __version__ = "0.3"
3 __license__ = "MIT"
4
5
6 def read_meta_kcf(inputstream):
7 """
8 :param inputstream: the kcf file
9 read kcf file (which may contain multiple kcf entries) and only keep ENTRY, NODE and EDGE parts.
10 :return:
11 """
12 if inputstream is None or inputstream == [] or inputstream == "":
13 raise IOError("empty input stream")
14 list_of_kcf_paragraphs = []
15 kcfpara = None
16 for line in inputstream:
17 if "ENTRY" in line:
18 kcfpara = [line]
19 elif "NODE" in line:
20 _, totalnodes = line.split()
21 totalnodes = int(totalnodes)
22 kcfpara.append(line)
23 for inodes in range(0, totalnodes):
24 nodeline = inputstream.next()
25 kcfpara.append(nodeline)
26 elif "EDGE" in line:
27 _, totaledges = line.split()
28 kcfpara.append(line)
29 totaledges = int(totaledges)
30 for inodes in range(0, totaledges):
31 edgeline = inputstream.next()
32 kcfpara.append(edgeline)
33 elif "///" in line:
34 kcfpara.append(line)
35 list_of_kcf_paragraphs.append(kcfpara)
36 # . sometimes kcf has no /// or final kcf in many has no ////, so add it
37 if kcfpara not in list_of_kcf_paragraphs:
38 list_of_kcf_paragraphs.append(kcfpara)
39
40 return list_of_kcf_paragraphs # why this list. easier to deal with each glycan as an individual item in the list
41
42
43 def flatten_meta_kcf_list(metakcflist):
44 """
45
46 :param metakcflist: a list containing lists of strings
47 :return: combined kcfs as a large string for saving to file
48 """
49 import itertools
50
51 return "".join(list(itertools.chain(*metakcflist)))
52
53
54 if __name__ == "__main__":
55 from optparse import OptionParser
56
57 usage = "usage: python %prog [options]\n"
58 parser = OptionParser(usage=usage)
59 parser.add_option("-i", action="store", type="string", dest="i", default="input",
60 help="input kcf file (input)")
61 parser.add_option("-o", action="store", type="string", dest="o", default="output",
62 help="output kcf file (output)")
63 (options, args) = parser.parse_args()
64
65 try:
66 inputname = options.i
67 outputname = options.o
68 except Exception as e:
69 raise Exception(e, "Please pass an input (kcf) and output filename as arguments")
70 instream = file(inputname, 'r')
71 try:
72 convertedkcf = read_meta_kcf(instream)
73 with open(outputname, "w") as f:
74 f.write(flatten_meta_kcf_list(convertedkcf))
75 except Exception as e:
76 raise e