#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    23
    Rep Power
    0

    How to redirect output in different files


    Hi all,


    I am using the python script that takes two input files (goodProteins.fasta and list.txt)
    and save result in gene.fasta output files.


    Code:
    from Bio import SeqIO
    
    fasta_file = "goodProteins.fasta" # First input
    wanted_file = "list.txt" # Second input
    result_file = "gene.fasta" # Output 
    
    wanted = set()
    with open(wanted_file) as f:
        for line in f:
            line = line.strip()
            if line != "":
                wanted.add(line)
    
    fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
    with open(result_file, "w") as f:
        for seq in fasta_sequences:
            if seq.id in wanted:
                SeqIO.write([seq], f, "fasta")

    But now I have several list files (list1.txt, list100.txt etc). I want to take each list file one at a time, proceed through the script and generates the corresponding output files
    (gene1.fasta, gene100.fasta).

    Any help??
    Last edited by utpalmtbi; June 17th, 2013 at 05:21 AM. Reason: simplify the problem
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Originally Posted by utpalmtbi
    Hi all,


    I am using the python script that takes two input files (goodProteins.fasta and list.txt)
    and save result in gene.fasta output files.





    But now I have several list files (list1.txt, list100.txt etc) that are present in the subdirectory 'outfile'
    within the current directory. I want to take each list file one at a time, proceed through the script and generates the corresponding output files
    (gene1.fasta, gene100.fasta) and saved them in a separate sub directory 'result'.

    Any help??
    First get a list of the input files:
    Code:
    import glob
    infiles=glob.glob('outfile/list*.txt')
    Next loop through those files and create the output file name accordingly:
    Code:
    for f in infiles:
        inf=open("outfile/"+f)
        outf=open("outfile/gene"+f[4:-4]+".rasta","w");#assuming all the input files are "textnnnn.txt"
        {insert your code}
        inf.close()
        outf.close()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    20
    Rep Power
    0

    Unhappy


    I modify the code as your suggestion as follows:

    Code:
    from Bio import SeqIO
    import glob
    
    
    fasta_file = "goodProteins.fasta" 
    wanted_file=glob.glob('outfile/list*.txt')
    
    for f in wanted_file:
        inf=open("outfile/"+f)
        result_file=open("outfile/gene"+f[4:-4]+".fasta","w");#assuming all the input files are "textnnnn.txt"
    
    
    wanted = set()
    with open(wanted_file) as f:
    for line in f:
    line = line.strip()
    if line != "":
    wanted.add(line)
    
    fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
    with open(result_file, "w") as f:
    for seq in fasta_sequences:
    if seq.id in wanted:
    SeqIO.write([seq], f, "fasta")
    
    inf.close()
    outf.close()

    But it gives following IndentationError:

    File "script.py", line 15
    for line in f:
    ^
    IndentationError: expected an indented block

    I have submitted my input files here: https://sites.google.com/site/iicbbioinformatics/share

    (note that list files should be under subdirectory 'outfile')..

    plz help
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Put your code between code tags not quote tags.
    This way we can help you better. Also give full trace back error message.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    20
    Rep Power
    0
    It gives following error:
    Code:
    Traceback (most recent call last):
      File "script.py", line 12, in <module>
        result_file=open("outfile/gene"+f[4:-4]+".fasta","w");#assuming all the inpu
    t files are "textnnnn.txt"
    FileNotFoundError: [Errno 2] No such file or directory: 'outfile/geneile\\list1.
    fasta'

IMN logo majestic logo threadwatch logo seochat tools logo