-1

I have this script that contains four different function. Function 2-4 have a common path, the for loop. I would like to get some help so I can sort that 'for loop' making it into one but depending on the function to get the data from the right file, fastqs, faster or xml.

> def One(): #gene extraction
    #do something
One()

def Two(path_to_folder_H*,something_else): # de novo assembly
    #do something with files

folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.fastqs)
    if folder exit:
       #do something
    elif:
        # do something else
   Two(path_to_folder_H*,something_else)

def Third(path_to_folder_H*, someting_else_2): #database+blast
    #do something

folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.fa)
    Third(path_to_folder_H*,something_else_2)

def Fouth(path_to_folder_H*,something_else_3): #parsing file
    #do someething
count=0
dict={}
folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.xml)
    if something:
           #do something
    elif:
   #something else
    Fourth(path_to_folder_H*,something_else_3)

This code is about 200 line long, and I just put a synopsis to abbreviate. The first function extract data from a file. The second function does the novo assembly with the fastqs file, and give a confing.fa. The third function uses the cotings.fa and a file obtained with the first function to do a database, and blast, giving an xml file. The fourth function parse the xml obtained with the third funcion.

Ana
  • 131
  • 1
  • 14
  • The question title doesn't appear to match the content. What exactly are you trying to do? – roganjosh Jul 12 '17 at 12:06
  • Answering the title: [Duplicate](https://stackoverflow.com/questions/19201290/how-to-save-a-dictionary-to-a-file). But I assume, you want to do something else? – P. Siehr Jul 12 '17 at 12:08
  • You are absolutely right. That is for another question I wanted to ask but I found the solution. I am going to change it. Thanks for letting me know. – Ana Jul 12 '17 at 12:09
  • So with the updated title, all you have to do is move some code around. Define your functions at the top, copy/paste the code that's currently across 3 `for` loops into a single `for folder in folders`. Did you try this? What is your blocker - do you get errors? – roganjosh Jul 12 '17 at 12:12

1 Answers1

1

With the latest edits of the question, I edited this answer as well. You can simply define the functions at the top of your Python-script. Using your code this would look like the following:

def One():
    # do one

def Two(path_to_folder_H*,something_else):
    #do something with files

def Third(path_to_folder_H*, someting_else_2):
    #do something with files

def Fouth(path_to_folder_H*,something_else_3):
    #do someething with files

# -- main -- 

One()
folders=glob.glob(path_to_folder_H*)

for folder in folders:

    my_file=glob.glob(file.fastqs)
    Two(path_to_folder_H*,something_else)

    my_file=glob.glob(file.fa)
    Third(path_to_folder_H*,something_else_2)

    my_file=glob.glob(file.xml)
    Fourth(path_to_folder_H*,something_else_3)
P. Siehr
  • 525
  • 1
  • 6
  • 22
  • I added some explanation to make it clear. Hope it helps. – Ana Jul 12 '17 at 12:30
  • Ok, I understand that they should be executed one after another. So why not use the third code part in this answer? – P. Siehr Jul 12 '17 at 12:32
  • P.Siehr, I tried what you suggested, which it is good but I have a few 'if and else' underneath my_files. I have updated the code for guidance, and understanding on what I am saying. – Ana Jul 12 '17 at 13:27
  • @Ana To be honest, I don't see any problem. Can you provide a [minimal working example](https://stackoverflow.com/help/mcve)? – P. Siehr Jul 12 '17 at 14:32
  • The error message I get is about that the second my_file does not match indentation. So I have to check the script, and see the problem. It must be about one space indentation. But I agree with you that you example code should work. – Ana Jul 12 '17 at 15:19