Printing in the same line in python
Question: I am quite new in python and I need your help.

I have a file like this:

    
    
    >chr14_Gap_2
    ACCGCGATGAAAGAGTCGGTGGTGGGCTCGTTCCGACGCGCATCCCCTGGAAGTCCTGCTCAATCAGGTGCCGGATGAAGGTGGT
    GCTCCTCCAGGGGGCAGCAGCTTCTGCGCGTACAGCTGCCACAGCCCCTAGGACACCGTCTGGAAGAGCTCCGGCTCCTTCTTG
    acacccaggactgatctcctttaggatggactggctggatcttcttgcagtccaaggggctctcaagagt
    ………..
    >chr14_Gap_3
    ACCGCGATGAAAGAGTCGGTGGTGGGCTCGTTCCGACGCGCATCCCCTGGAAGTCCTGCTCAATCAGGTGCCGGATGAAGGTGGT
    GCTCCTCCAGGGGGCAGCAGCTTCTGCGCGTACAGCTGCCACAGCCCCTAGGACACCGTCTGGAAGAGCTCCGGCTCCTTCTTG
    acacccaggactgatctcctttaggatggactggctggatcttcttgcagtccaaggggctctcaagagt
    ………..
    

One string as a tag and one string the dna sequence. I want to calculate the
number of the N letters and the number of the lower case letters and take the
percentage. I wrote the following script which works but I have a problem in
printing.

    
    
    #!/usr/bin/python
    
    import sys
    
    
    if len (sys.argv) != 2 :
      print "Usage: If you want to run this python script  you have to put the fasta file that     includes the desert area's sequences as arument"
      sys.exit (1)
    
      fasta_file = sys.argv[1]
    
    #This script reads the sequences of the desert areas (fasta files) and calculates the   persentage of the Ns and the repeats.
    
     fasta_file = sys.argv[1]
     f = open(fasta_file, 'r')
    
    content = f.readlines()
    x = len(content)
    #print x
    for i in range(0,len(content)):
          if (i%2 == 0):
                content[i].strip()
                name = content[i].split(">")[1]
                print name,  #the "," makes the print command to avoid to print a new line
         else:
                content[i].strip()
                numberOfN = content[i].count('N')
                #print numberOfN
                allChar =  len(content[i])
                lowerChars = sum(1 for c in content[i] if c.islower())
                Ns_persentage = 100 * (numberOfN/float(allChar))
                lower_persentage = 100 * (lowerChars/float(allChar))
                waste = Ns_persentage + lower_persentage
                print ("The waste persentage is: %s" % (round(waste)))
                #print ("The persentage of Ns is: %s and the persentage of repeats is: %s" %   (Ns_persentage,lower_persentage))
    
        #print (name + waste)
    

The thing is that it can print the tag in the first line and the waste
variable in the second one like this:

    
    
    chr10_Gap_18759
    The waste persentage is: 52.0
    

How can I print it in the same line, tab separated?

eg

    
    
    chr10_Gap_18759      52.0 
    chr10_Gap_19000      78.0 
    …….
    

Thank you very much.


Answer: You can print it with:

    
    
    print name, "\t", round(waste)
    

If you are using python 2.X I would make some modification to your code. There
is the [argparse](https://docs.python.org/2.7/library/argparse.html) module of
python to manage the arguments from the command line. I would do something
like this:

    
    
    #!/usr/bin/python
    
    import argparse
    # To use the arguments 
    parser = argparse.ArgumentParser()
    parser.add_argument("fasta_file", help = "The fasta file to be processed ", type=str)
    args = parser.parse_args()
    
    f= open(args.fasta_file, "r")
    content = f.readlines()
    f.close()
    
    x = len(content)
    for i in range(x):
          line = content[i].strip()
          if (i%2 == 0):
              #The first time it will fail, for the next occasions it will be printed as you wish
                try:
                    print bname, "\t", round(waste)
                except:
                    pass
                name = line.split(">")[1]
         else:
                numberOfN = line.count('N')
                allChar =  len(line)
                lowerChars = sum(1 for c in content[i] if c.islower())
                Ns_persentage = 100 * (numberOfN/float(allChar))
                lower_persentage = 100 * (lowerChars/float(allChar))
                waste = Ns_persentage + lower_persentage
    # To print the last case you need to do it outside the loop
    print name, "\t", round(waste)
    

You can also print it like the other answer with `print("{}\t{}".format(name,
round(waste)))`

I am not sure about the use of `i%2`, Note that if the sequence uses and odd
number of lines you'll will not get the name of the next sequence until the
same event occurs. I would check if the line begin with ">" then use store the
name, and sum the characters of the next line.

