lundi 9 novembre 2020

Python find function with two conditions not working

I have created a function but it does not perform as wanted. I want to read a fastq file with SeqIO and if two string are found in record.seq then print their positions in the sequences and their distance from each other. But the problem is I also get records.seq which does not contain index1 - I mean every position for index1 is 0:

from Bio import SeqIO

def dual_index_positions(index1,index2,file):
    count=0
    with open(file, "r") as Fastq:
        for record in SeqIO.parse(Fastq,'fastq'):
            if (index1 and index2) in record.seq:
                print(record.name)
                ind1_rec=record.seq.find(index1)
                ind2_rec=record.seq.find(index2)
                rp_ind1=ind1_rec+len(index1)
                dist=(ind2_rec)-(rp_ind1)
                print('Index1 and index2 positions are '+ str(ind1_rec+1) + ' and ' + str(ind2_rec+1) + ' respectively ' +
                  '; distance is: ' + str(dist))
                count+=1
    print('The total number of hits is: '+ str(count))

Suppose I have a record.seq that my indexes are inside the sequence the process should be as below:

main_seq = 'NNNATTACTCGNNNNNNAGGCGAAGNN'
dual_index_positions('ATTACTCG','AGGCGAAG','/Users/lbl/Desktop/merged_fastqs.fastq')

Output:

record.id
Index1 and index2 positions are 4 and 18 respectively; distance is: 6
The total number of hits is: 1

Can anyone help me with this? Thank you in advance

Aucun commentaire:

Enregistrer un commentaire