import os
mypath='/home/sb/bioinfo/test/'
AACharge = {"C":-.045,"D":-.999,"E":-.998,
            "H":.091,"K":1,"R":1,"Y":-.001}
for x in os.listdir(mypath):
    if os.path.splitext(x)[1]=='.fas':
        fh = open(os.path.join(mypath,x),'U')
        name = fh.readline()[1:-1]
        seq = ""
        for line in fh:
            seq = seq + line[:-1].upper()
        fh.close()
        charge = -0.002
        for aa in seq:
            charge += AACharge.get(aa,0)
        fh = open(os.path.join(mypath,'netvalue.txt'),'a')
        fh.write("%s,%s\n"%(name,charge))
        fh.close()

This code is part of the book "Python for Bioinformatics", by Sebastian Bassi (sbassi@genesdigitales.com). Return to home page.