Mercurial > repos > guerler > springsuite
annotate spring_minz.py @ 11:21a7dd67b483 draft
"planemo upload commit f208fb81425711754738fc8e612ee2d5cc26377f"
author | guerler |
---|---|
date | Fri, 25 Sep 2020 19:07:14 +0000 |
parents | 4ac5d5a9b21c |
children | 4a4888bf0338 |
rev | line source |
---|---|
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
1 #! /usr/bin/env python3 |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
2 import argparse |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
3 import os |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
4 |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
5 def main(args): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
6 names = [] |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
7 with open(args.list) as file: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
8 for index, line in enumerate(file): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
9 names.append(line.strip()) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
10 print ("Loaded %s names from `%s`." % (len(names), args.list)) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
11 crossreference = {} |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
12 with open(args.crossreference) as file: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
13 for index, line in enumerate(file): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
14 columns = line.split() |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
15 core = columns[0] |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
16 partner = columns[2] |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
17 if core not in crossreference: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
18 crossreference[core] = [] |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
19 crossreference[core].append(partner) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
20 print ("Loaded cross reference from `%s`." % args.crossreference) |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
21 targets = get_template_scores(args.target, args.minscore, args.idx) |
8
f2f38991c36f
"planemo upload commit ab5e686c62d07a0d45216b474d435b855745222d-dirty"
guerler
parents:
0
diff
changeset
|
22 interactions = [] |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
23 if not targets: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
24 print("No targets found `%s`" % args.target) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
25 else: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
26 print ("Loaded target scores from `%s`." % args.target) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
27 for name in names: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
28 input_directory = args.inputs.rstrip("/") |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
29 input_file = "%s/%s" % (input_directory, name) |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
30 templates = get_template_scores(input_file, args.minscore, args.idx) |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
31 minz = 0 |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
32 for t in targets: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
33 if t in crossreference: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
34 partners = crossreference[t] |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
35 for p in partners: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
36 if p in templates: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
37 score = min(targets[t], templates[p]) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
38 if score > minz: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
39 minz = score |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
40 if minz > args.minscore: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
41 interactions.append((name, minz)) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
42 print("Predicting: %s, min-Z: %s" % (name, minz)) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
43 interactions.sort(key=lambda tup: tup[1], reverse=True) |
11
21a7dd67b483
"planemo upload commit f208fb81425711754738fc8e612ee2d5cc26377f"
guerler
parents:
9
diff
changeset
|
44 with open(args.output, 'a+') as output_file: |
8
f2f38991c36f
"planemo upload commit ab5e686c62d07a0d45216b474d435b855745222d-dirty"
guerler
parents:
0
diff
changeset
|
45 for i in interactions: |
11
21a7dd67b483
"planemo upload commit f208fb81425711754738fc8e612ee2d5cc26377f"
guerler
parents:
9
diff
changeset
|
46 output_file.write("%s %s %s\n" % (args.name, i[0], i[1])) |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
47 |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
48 def get_template_scores(hhr_file, min_score, identifier_length): |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
49 result = {} |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
50 identifier_length = identifier_length + 4 |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
51 if os.path.isfile(hhr_file): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
52 with open(hhr_file) as file: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
53 for index, line in enumerate(file): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
54 if index > 8: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
55 if not line.strip(): |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
56 break |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
57 template_id = line[4:identifier_length] |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
58 template_score = float(line[57:63]) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
59 if template_score > min_score: |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
60 result[template_id] = template_score |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
61 return result |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
62 |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
63 if __name__ == "__main__": |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
64 parser = argparse.ArgumentParser(description='This script identifies interactions by detecting matching HH-search results.') |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
65 parser.add_argument('-t', '--target', help='HHR target file result', required=True) |
11
21a7dd67b483
"planemo upload commit f208fb81425711754738fc8e612ee2d5cc26377f"
guerler
parents:
9
diff
changeset
|
66 parser.add_argument('-n', '--name', help='HHR target name', required=True) |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
67 parser.add_argument('-c', '--crossreference', help='Cross Reference index file', required=True) |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
68 parser.add_argument('-x', '--idx', help='Length of identifier', type=int, default=6) |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
69 parser.add_argument('-l', '--list', help='Text file containing identifiers.', required=True) |
9
4ac5d5a9b21c
"planemo upload commit f465445ebca0307953f59938494a2244ca8ea23c"
guerler
parents:
8
diff
changeset
|
70 parser.add_argument('-i', '--inputs', help='Directory containing `hhr` files', required=True) |
0
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
71 parser.add_argument('-o', '--output', help='Output file containing minZ-scores`', required=True) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
72 parser.add_argument('-m', '--minscore', help='min-Z score threshold', type=int, default=10) |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
73 args = parser.parse_args() |
d30785e31577
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
guerler
parents:
diff
changeset
|
74 main(args) |