Friday, March 8, 2013

python

This function written is python shortens the string of a .txt file on every other line and makes a copy in a new file.  This is very useful in bioinformatics.  My problem was I had a Phred file from Illumina sequencing where both ends of the sequence was N.  If I map this using bowtie and allow two low quality reads per sequence a lot of the data would be thrown out. 

Simple example:
    +
    Shorten
    Keep
    Shorten
    Keep

    ----------------
Example output
    +
    horte
    Keep
    horte
    Keep
------------------
def every_other_line_shorten(starting_file, ending_file):
# open the files
    from_file = open(starting_file, 'r') # read from file

    to_file = open(ending_file, 'w') # write to_file

    line = from_file.readline()
    while line != "":
   
        # keeps line
        to_file.write(line)
        line = from_file.readline()
       
       # cuts line
         to_file.write(line[1:-2] + '\n')
        line = from_file.readline()

    print('done')
   
    to_file.close()
    from_file.close()