Reading gzipped text file line-by-line for processing in python 3.2.6
Question: I'm a complete newbie when it comes to python, but I've been tasked with
trying to get a piece of code running on a machine which has a different
version of python (3.2.6) than that which the code was originally built for.

I've come across an issue with reading in a gzipped-text file line-by-line
(and processing it depending on the first character). The code (which
obviously is written in python > 3.2.6) is

    
    
    for line in gzip.open(input[0], 'rt'):
        if line[:1] != '>':
            out.write(line)
            continue
    
        chromname = match2chrom(line[1:-1])
        seqname = line[1:].split()[0]
    
        print('>{}'.format(chromname), file=out)
        print('{}\t{}'.format(seqname, chromname), file=mappingout)
    

(for those who know, this strips gzipped FASTA genome files into headers (with
">" at start) and sequences, and processes the lines into two different files
depending on this)

I have found <https://bugs.python.org/issue13989>, which states that mode 'rt'
cannot be used for gzip.open in python-3.2 and to use something along the
lines of:

    
    
    import io
    
    with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:
         for line in fin:
             if line[:1] != '>':
                 out.write(line)
                 continue
    
             chromname = match2chrom(line[1:-1])
             seqname = line[1:].split()[0]
    
             print('>{}'.format(chromname), file=out)
             print('{}\t{}'.format(seqname, chromname), file=mappingout)
    

but the above code does not work:

    
    
    UnsupportedOperation in line <4> of /path/to/python_file.py:
    read1
    

How can I rewrite this routine to give out exactly what I want - reading the
gzip file line-by-line into the variable "line" and processing based on the
first character?

EDIT: traceback from the first version of this routine is (python 3.2.6):

    
    
    Mode rt not supported  
    File "/path/to/python_file.py", line 79, in __process_genome_sequences  
    File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 46, in open  
    File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 157, in __init__
    

Traceback from the second version is:

    
    
    UnsupportedOperation in line 81 of /path/to/python_file.py:
    read1
    File "/path/to/python_file.py", line 81, in __process_genome_sequences
    

with no further traceback (the extra two lines in the line count are the
`import io` and `with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:`
lines


Answer: I have actually appeared to have solved the problem.

In the end I had to use `shell("gunzip {input[0]}")` to ensure that the
gunzipped file could be read in in text mode, and then read in the resulting
file using

    
    
    for line in open(' *< resulting file >* ','r'):
        if line[:1] != '>':
            out.write(line)
            continue
    
        chromname = match2chrom(line[1:-1])
        seqname = line[1:].split()[0]
    
        print('>{}'.format(chromname), file=out)
        print('{}\t{}'.format(seqname, chromname), file=mappingout)  
    

