|
1 | 1 | """preparation of proteins specified in configfile""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import tempfile |
4 | 5 | from Bio.PDB import PDBParser, PDBIO |
5 | 6 |
|
| 7 | +# Redirect all stdout/stderr to the log file |
| 8 | +sys.stdout = open(snakemake.log[0], 'w', buffering=1) # line buffering |
| 9 | +sys.stderr = sys.stdout |
6 | 10 |
|
7 | 11 | def removeChains(model, chainlist): |
8 | 12 | """ |
@@ -39,18 +43,31 @@ def prepareRec(inputfile, outputfile, target): |
39 | 43 | """ |
40 | 44 | select chains to delete depending on config definition |
41 | 45 | """ |
42 | | - print(target) |
| 46 | + print(f"Preparing target: {target}") |
| 47 | + # target might be a gzipped file |
| 48 | + if inputfile.endswith(".gz"): |
| 49 | + import gzip |
| 50 | + import shutil |
| 51 | + # the unzipped file needs to be temporary |
| 52 | + with tempfile.NamedTemporaryFile(delete=False) as f: |
| 53 | + print(f" Unzipping {inputfile} to temporary file.") |
| 54 | + ungzipped = f.name |
| 55 | + with gzip.open(inputfile, "rb") as f_in: |
| 56 | + with open(ungzipped, "wb") as f_out: |
| 57 | + shutil.copyfileobj(f_in, f_out) |
| 58 | + inputfile = ungzipped |
43 | 59 | ID = target.split(",") |
44 | 60 | chains = ID[1].split(" ") |
45 | 61 | parser = PDBParser() # MMCIFParser() |
46 | 62 | structure = parser.get_structure(ID[0], inputfile) |
47 | 63 | model = structure[0] |
| 64 | + print(f" Removing chains not in: {chains}") |
48 | 65 | removeChains(model, chains) |
49 | 66 |
|
50 | 67 | io = PDBIO() |
51 | 68 | io.set_structure(structure) |
52 | 69 | out = outputfile |
53 | | - print("printing outfile") |
| 70 | + print(f" Printing outfile: {out}") |
54 | 71 | io.save(out) |
55 | 72 |
|
56 | 73 |
|
|
0 commit comments