This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Problem to Read my file : infinite loop

Hello,

I have an urgent problem thinking in python. I want to read my file line by line to assign to each various function but when I read it, it only reads the first line in a loop.

So I find myself in an infinite loop. How to read the whole file line by line and not just the first line? thank you

Code:

def creation_dico(dic_localisation_atlas,name_file,exome_colonne,cohorte_name,dico_transm_ensembl):

    dico_DTM={}
    f_in=open(name_file,"r")

    dico_file={}

    path="vcf_output/"+cohorte_name

    if os.path.isdir(path)==False:
        os.mkdir(path,0o755)

    for k in exome_colonne:

        dico_file[k]=open(path+"/"+k+".txt","w")
        dico_file[k].write("##CHROMOSOME\tN_SNP\tN_gene\t\tInfo\tSNP_Donneur\tSNP_Receveur\n")

    colonne7=[]
    colonne7bis=[]
    colonne7ter=[]  

    liste_SNP=[]
    variantsYN=[]

    chro=1
    indel=0

    buf_score={}
    bufs=0
    rf=f_in.readline
    read=rf()
    a=0

    while read:

        row=read.split("\t")
        print row

        if row[0][0]!="#":

            if chro!=row[0]:
                chro=row[0]
                print("chromosome",chro)

            liste_SNP.append(row[1])
            variantsYN.append(row[7])
            liste_g=liste_genes(variantsYN)
            liste_transcrit=f_liste_transcrit(variantsYN)
                if len(variantsYN)>0:
                    for k in exome_colonne:
                        Variants_communs=comptage_SNP(row[exome_colonne[k][1]],row[exome_colonne[k][0]],row[1],k,variantsYN,dico_DTM,dic_localisation_atlas,liste_g,dico_transm_ensembl,liste_transcrit)

    f_in.close()

    print("Paire de patient, score global,score transmembrannaire protein atlas, score secretion, score transmembrannaire Ensembl")

    for keys in exome_colonne:
        print(keys,exome_colonne[keys][2],exome_colonne[keys][3],exome_colonne[keys][4],exome_colonne[keys][5])  

    for k in exome_colonne:
        dico_file[k].close() 

if os.path.exists("vcf_output")==False:
    os.mkdir("vcf_output")

Paris_Cohorte={}
Paris_Cohorte["1"]=[31,11,0,0,0,0]
Paris_Cohorte["2"]=[41,13,0,0,0,0]
Paris_Cohorte["3"]=[40,33,0,0,0,0]

parser = argparse.ArgumentParser()
choice = parser.add_argument_group('Choix cohorte')
choice.add_argument('-c','-cohorte',type=str,choices=["vcf"],required=True)
args = parser.parse_args()
path_in="localisation_simplified.csv"
dic_localisation_atlas=open_localisation(path_in)
membrannaire="transmembranaire/"
dico_transm_ensembl=file_ensembl(membrannaire)
if args.c :
    if args.c=="vcf" :
        creation_dico(dic_localisation_atlas,"vcf.vcf",Paris_Cohorte,"Paris",dico_transm_ensembl)

File (little part) :

##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description=All filters passed>
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  ...
1   783071  rs142849724 C   T   .   PASS    ENSG00000228794;ENST00000624927|ENST00000623808|ENST00000445118|ENST00000448975|ENST00000610067|ENST00000608189|ENST00000609139|ENST00000449005|ENST00000416570|ENST00000623070|ENST00000609009|ENST00000622921 GT  C;  C;  T;|C;   C;  T;|C;   C;  C;  C;  C;  C;|T;   C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;  C;|T;   C;  C;  C;  C;|T;   C;  C;  C;

Terminal exit:

['##fileformat=VCFv4.3\n']
['##fileformat=VCFv4.3\n']
['##fileformat=VCFv4.3\n']

Thank you

vcf python

1 answer

Use a for loop rather than a while loop:

for line in open(myfile):
    #do stuff with line

Log in to answer this question.