annotate toolbox/ftools.py @ 1:57f1260ca94e draft

"planemo upload commit fec9dc76b3dd17b14b02c2f04be9d30f71eba1ae"
author watsocam
date Fri, 11 Mar 2022 23:40:51 +0000
parents 99308601eaa6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
1 from os.path import *
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
2 from os import listdir, makedirs, remove
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
3 import pickle
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
4 import shutil
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
5
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
6 def fileparts(path): # path = file path
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
7 [p,f] = split(path)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
8 [n,e] = splitext(f)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
9 return [p,n,e]
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
10
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
11 def listfiles(path,token): # path = folder path
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
12 l = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
13 for f in listdir(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
14 fullPath = join(path,f)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
15 if isfile(fullPath) and token in f:
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
16 l.append(fullPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
17 l.sort()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
18 return l
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
19
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
20 def listsubdirs(path): # path = folder path
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
21 l = []
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
22 for f in listdir(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
23 fullPath = join(path,f)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
24 if isdir(fullPath):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
25 l.append(fullPath)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
26 l.sort()
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
27 return l
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
28
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
29 def pathjoin(p,ne): # '/path/to/folder', 'name.extension' (or a subfolder)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
30 return join(p,ne)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
31
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
32 def saveData(data,path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
33 print('saving data')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
34 dataFile = open(path, 'wb')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
35 pickle.dump(data, dataFile)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
36
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
37 def loadData(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
38 print('loading data')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
39 dataFile = open(path, 'rb')
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
40 return pickle.load(dataFile)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
41
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
42 def createFolderIfNonExistent(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
43 if not exists(path): # from os.path
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
44 makedirs(path)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
45
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
46 def moveFile(fullPathSource,folderPathDestination):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
47 [p,n,e] = fileparts(fullPathSource)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
48 shutil.move(fullPathSource,pathjoin(folderPathDestination,n+e))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
49
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
50 def copyFile(fullPathSource,folderPathDestination):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
51 [p,n,e] = fileparts(fullPathSource)
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
52 shutil.copy(fullPathSource,pathjoin(folderPathDestination,n+e))
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
53
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
54 def removeFile(path):
99308601eaa6 "planemo upload for repository https://github.com/ohsu-comp-bio/UNetCoreograph commit fb90660a1805b3f68fcff80d525b5459c3f7dfd6-dirty"
perssond
parents:
diff changeset
55 remove(path)