Python 3 codes for loops

September 5, 2019 107 views
Python

In my python script, there are 7 rows (0 to 6) in a list of lists (alignments). The SeqIDs of the first 2 rows, the next 2 rows and the last 3 rows are similar. Now, while appending the rows using the following codes

uniques = []
ap = open(tempparse, ‘w’)
for row in range(len(alignments)):
ap.write('row ’+str(row)+’\n’)

for compare in range(row+1,len(alignments)):
......
......
.......

the last row (i. e. the last one of the last 3 rows in the list ) is not appended because it does not have any subsequent row to compare, and therefore, the six rows are appended except for the last one.

How can I resolve this issue?

1 Answer

Maybe just try adding additional if statement and if there are no subsequent row to compare just add a default value.

If you wish you could share some of the data that you are running this against so that I could test it as well as it is very hard to just imagine the result without seeing any data.

  • Thanks a lot. Below, please find the referred codes in my referred script (indentation to be ignored as cut-pasted):

    f = open(tempblastn, ‘r’)
    reader = csv.reader(f)
    alignments = list(reader)
    f.close()

    uniques = []
    ap = open(tempparse, 'w’)
    for row in range(len(alignments)):
    ap.write('row ’+str(row)+’\n’)

        for compare in range(row+1,len(alignments)):
    
            if alignments[row][0] != alignments[compare][0]:
                uniques.append(alignments[row])
                ap.write('\tadding row '+str(row)+' to unique list\n')
                break
    
        else:
            ap.write('\tcomparing to '+str(compare)+'\n')
    
            if int(alignments[row][3]) <= int(alignments[compare][3]) and int(alignments[row][4]) <= int(alignments[compare][3]):                    
                uniques.append(alignments[row])
                ap.write('\tadding row '+str(row)+' to unique list\n')
                break            
    
            elif int(alignments[row][3]) <= int(alignments[compare][3]) and int(alignments[row][4]) >= int(alignments[compare][4]):
                ap.write('\tsstart and send of row '+str(compare)+' is not unique\n')
                break
    
            elif int(alignments[row][3]) <= int(alignments[compare][3]) and int(alignments[row][4]) <= int(alignments[compare][4]):
                ap.write('\tsstart and send of row '+str(compare)+' is not unique\n')
                break
    

    ap.close()

    The test alignment data (csv-formatted) being parsed are as follows: Row starts.

    AWWV01005960.1,729,1377,170,821,minus,0.01,652
    AWWV01005960.1,567,730,2133,2296,minus,0.01,164
    AWWV01006281.1,567,730,5784,5947,plus,0.01,164
    AWWV01006281.1,729,1377,7258,7906,plus,0.0,649
    AWWV01006679.1,1378,1572,21394,21590,minus,0.02,197
    AWWV01006679.1,729,1377,22748,23396,minus,0.0,649
    AWWV01006679.1,567,730,24704,24867,minus,0.01,164

    Row ends. There is a problem with the referred codes as mentioned earlier, if there is an odd number of matches at the end of the list, as in the above data, i. e. seqid 'AWWV01006679.1’ has three matches. Obviously, if there is an even number of matches at the end, it appends all the rows correctly (as printed in the 'tempparse’ output .txt file). If I add the following within the 'for compare....’ loop, the last row is appended; however, my intuition tells me it’s a hack, and will compromise the script when thousands of seqids will be processed in our study alignments.

    if row == (len(alignments)-1):
    uniques.append(alignments[row])
    ap.write(’\tadding row ’+str(row)+’ to unique list\n’)

    Kindest regards.
Have another answer? Share your knowledge.