|
1 | 1 | """sorting and outputting the N>1 best results from a docking run""" |
| 2 | + |
2 | 3 | import subprocess |
3 | 4 | import gzip |
4 | 5 | import os |
5 | 6 | from math import ceil |
| 7 | + |
6 | 8 | inPath = snakemake.input[0] |
7 | 9 | outFile = snakemake.output[0] |
8 | 10 | num = float(snakemake.config["RESULT_NUMBER"]) |
9 | 11 |
|
10 | | -count=0 |
11 | | -lineN=0 |
12 | | -ids=0 |
13 | | -swapFlg=False |
14 | | -scoreList=[] |
15 | | -strucList=[] |
16 | | -tempList=[] |
| 12 | +count = 0 |
| 13 | +lineN = 0 |
| 14 | +ids = 0 |
| 15 | +swapFlg = False |
| 16 | +scoreList = [] |
| 17 | +strucList = [] |
| 18 | +tempList = [] |
17 | 19 |
|
18 | 20 | getLigNum = "zgrep -c 'REMARK RECEPTOR' " + inPath |
19 | 21 | ligand_num = int(subprocess.check_output(getLigNum, shell=True)) |
20 | 22 |
|
21 | 23 | if num > 1: |
22 | 24 | listSize = num |
23 | 25 | else: |
24 | | - total=float(ligand_num) |
25 | | - listSize = ceil(num*total) |
| 26 | + total = float(ligand_num) |
| 27 | + listSize = ceil(num * total) |
26 | 28 |
|
27 | | -with gzip.open(inPath, 'rt', encoding='utf-8') as inFile: |
| 29 | +with gzip.open(inPath, "rt", encoding="utf-8") as inFile: |
28 | 30 | for line in inFile: |
29 | 31 | if "REMARK RECEPTOR" in line: |
30 | | - lineN=0 |
31 | | - if count>0: |
32 | | - if count<=listSize: |
| 32 | + lineN = 0 |
| 33 | + if count > 0: |
| 34 | + if count <= listSize: |
33 | 35 | strucList.append(tempList) |
34 | 36 | elif swapFlg: |
35 | | - strucList[ids]=tempList |
36 | | - swapFlg=False |
37 | | - count=count+1 |
38 | | - tempList=[] |
39 | | - if lineN==3: |
40 | | - strs=line.split() |
41 | | - curValue=float(strs[3]) |
42 | | - if count<=listSize: |
| 37 | + strucList[ids] = tempList |
| 38 | + swapFlg = False |
| 39 | + count = count + 1 |
| 40 | + tempList = [] |
| 41 | + if lineN == 3: |
| 42 | + strs = line.split() |
| 43 | + curValue = float(strs[3]) |
| 44 | + if count <= listSize: |
43 | 45 | scoreList.append(curValue) |
44 | 46 | else: |
45 | | - maxValue=max(scoreList) |
46 | | - if curValue<maxValue: |
47 | | - swapFlg=True |
48 | | - ids=scoreList.index(maxValue) |
49 | | - scoreList[ids]=curValue |
| 47 | + maxValue = max(scoreList) |
| 48 | + if curValue < maxValue: |
| 49 | + swapFlg = True |
| 50 | + ids = scoreList.index(maxValue) |
| 51 | + scoreList[ids] = curValue |
50 | 52 | tempList.append(line) |
51 | | - lineN=lineN+1 |
52 | | - scoreDict={} |
| 53 | + lineN = lineN + 1 |
| 54 | + scoreDict = {} |
53 | 55 |
|
54 | 56 | for index, item in enumerate(scoreList): |
55 | | - scoreDict[index]=item |
| 57 | + scoreDict[index] = item |
56 | 58 |
|
57 | | - sortList=sorted(scoreDict, key=scoreDict.get) |
58 | | - with open(outFile, 'w', encoding='utf-8') as outF: |
| 59 | + sortList = sorted(scoreDict, key=scoreDict.get) |
| 60 | + with open(outFile, "w", encoding="utf-8") as outF: |
59 | 61 | for index in sortList: |
60 | 62 | for line in strucList[index]: |
61 | 63 | outF.write(line) |
62 | | -os.chmod(outFile, 0o400) ## TODO: remove after included in Snakemake |
| 64 | +os.chmod(outFile, 0o400) ## TODO: remove after included in Snakemake |
0 commit comments