Commit 74c44899 by Andrea Mancini

Update create_dataset.py

parent 6f18e6f3
......@@ -8,14 +8,11 @@ from pathlib import Path
def get_sequences_from_fasta(header_list, fasta_path):
f = open(fasta_path, 'r')
with open(fasta_path, 'r') as f:
fasta_txt = f.read()
data = [(seq.split()[0],"".join(seq.split('\n')[1:])) for seq in fasta_txt.split('>')[1:]]
fasta_txt = f.read()
data = [(seq.split()[0],"".join(seq.split('\n')[1:])) for seq in fasta_txt.split('>')[1:]]
f.close()
founded_seqs = ([],[])
founded_seqs = [[],[]]
for head_in_file, seq in data:
if head_in_file in header_list:
......@@ -24,7 +21,10 @@ def get_sequences_from_fasta(header_list, fasta_path):
return founded_seqs
def get_nodes_at_depth(node, level, node_list=[], curr_level=0):
def get_nodes_at_depth(node, level, node_list=None, curr_level=0):
if node_list is None:
node_list = []
if(curr_level == level):
node_list.append(node)
......@@ -52,7 +52,12 @@ def define_classes_at_depth(fasta_path, tree: Tree, depth):
def define_classes_from_list(fasta_path, tree: Tree, node_ids_list, class_ids=None):
node_list = [tree.search_nodes(name=id)[0] for id in node_ids_list]
node_list = []
for id in node_ids_list:
nodes = tree.search_nodes(name=id)
if not nodes:
raise ValueError(f"Node {id} not found in tree")
node_list.append(nodes[0])
len_nodes = len(node_list)
......@@ -98,7 +103,7 @@ def main(args):
if depth is None and len(node_list) == 0:
raise Exception("Please one between depth and node list is required")
raise Exception("Either depth or node_list must be provided")
elif depth and len(node_list) > 0:
......@@ -107,13 +112,13 @@ def main(args):
tree = Tree(args.tree)
if depth:
if depthi is not None:
class2seqs = define_classes_at_depth(args.ifpath, tree, depth)
elif len(node_list) > 0:
class2seqs = define_classes_from_list(args.ifpath, tree, depth, args.node_list, args.class_ids)
class2seqs = define_classes_from_list(args.ifpath, tree, node_list, args.class_ids)
save_to_csv(class2seqs, args.ofpath)
......@@ -135,10 +140,10 @@ if __name__ == "__main__":
"--depth", dest="depth", type=int, default=None, help="Depth where cutting subtree (clusters) - defining class"
)
parser.add_argument(
"--nodes", dest="node_list", type=str, default=[], help="Internal Node IDs list of subtrees"
"--nodes", dest="node_list", nargs="+", default=[], help="Internal Node IDs list of subtrees"
)
parser.add_argument(
"--class", dest="class_ids", type=str, default=None, help="Class ids associated at same order with nodes (only if passing a node list)"
"--class", dest="class_ids", nargs="+", default=None, help="Class ids associated at same order with nodes (only if passing a node list)"
)
options = parser.parse_args()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment