annotate scrappie_raw.py @ 3:632c50fe83ba draft default tip

planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
author jdv
date Tue, 03 Oct 2017 21:50:49 -0400
parents 30c2b84b4117
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
1 #!/usr/bin/env python3
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
2
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
3 import sys, os
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
4 import glob
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
5 import subprocess
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
6 import shutil
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
7 import h5py
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
8 import numpy as np
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
9
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
10 def main():
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
11 tar_file = sys.argv[1]
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
12 out_file = sys.argv[2]
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
13 threads = sys.argv[3]
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
14
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
15 extract_fast5(tar_file)
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
16 with open(out_file, "w") as outfile:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
17 subprocess.call(["scrappie",
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
18 "raw",
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
19 "--threads", threads,
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
20 "--outformat", "fasta",
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
21 "in_dir" ],
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
22 stdout=outfile )
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
23
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
24 def extract_fast5(fn):
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
25
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
26 try:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
27 in_dir = "in_dir"
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
28 if not os.path.exists(in_dir):
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
29 os.makedirs(in_dir)
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
30
3
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
31 # python's tarfile interface does not sanitize file paths within
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
32 # tarballs, which can be a big security risk. GNU tar does sanitize by
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
33 # default, so it's easier/safer here just to call the system tar
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
34 subprocess.call([
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
35 "tar",
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
36 "-xf",
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
37 fn,
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
38 "-C",
632c50fe83ba planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit d8cc434bd1704b2834f89b7d91370f356e3ac85a
jdv
parents: 0
diff changeset
39 in_dir])
0
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
40
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
41 files = glob.glob(
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
42 os.path.join(in_dir, "**", "*.fast5"),
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
43 recursive=True
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
44 )
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
45 if len(files) < 1:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
46 raise ValueError('No FAST5 files found')
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
47 for f in files:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
48 shutil.copy(f, in_dir)
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
49
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
50 except OSError as e:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
51 print("Unexpected error:", e.strerror)
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
52 raise
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
53
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
54 except:
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
55 print("Unexpected error:", sys.exc_info()[0])
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
56 raise
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
57
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
58 if __name__ == "__main__" :
30c2b84b4117 planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/scrappie commit bf5788ad5a3293446a50a3246b44ba09174c9b71
jdv
parents:
diff changeset
59 main()