<jupyter_start><jupyter_text>Practical 1<jupyter_code># AA composition calculation
def AA_freq(peptide):
          
    aa_freq = {'A':0, 'C':0, 'D':0, 'E':0, 'F':0, 'G':0, 'H':0, 'I':0, 'K':0, 'L':0, 'M':0, 'N' :0, 'P':0, 'Q':0, 'R':0, 'S':0, 'T':0, 'V':0, 
               'W':0, 'Y':0}
    for i in peptide: 
        aa_freq[i] += 1
    for i in aa_freq:
        aa_freq[i]= aa_freq[i]/len(peptide)*100
      
    print ("{:<8} {:<8}".format('AA','% composition'))
    for key, value in aa_freq.items():
       print ("{:<8} {:<8}".format(key, value))
    

# Parse fasta file
def read_fasta(fp):
        name, seq = None, []
        for line in fp:
            line = line.rstrip()
            if line.startswith(">"):
                if name: yield (name, ''.join(seq))
                name, seq = line, []
            else:
                seq.append(line)
        if name: yield (name, ''.join(seq))<jupyter_output><empty_output><jupyter_text>Q5<jupyter_code>#Q5 AA composition for overall TMH

fasta_file = "alphanr40.txt"
identifiersTMH =[]
sequencesTMH= []

with open(fasta_file) as fp:
    for name, seq in read_fasta(fp):
        seq= seq.replace(" ", "")   #Remove space
        seq= seq.replace("U", "")   #Remove 'U'
                  
        identifiersTMH.append(name)
        sequencesTMH.append(seq)
        
TMH_overall = ''.join(map(str, sequencesTMH))
AA_freq(TMH_overall)
#Q5 AA composition for overall TMB

fasta_file = "betanr40.txt"
identifiersTMB =[]
sequencesTMB= []

with open(fasta_file) as fp:
    for name, seq in read_fasta(fp):
        seq= seq.replace(" ", "")   #Remove space
        seq= seq.replace("U", "")   #Remove 'U'
                  
        identifiersTMB.append(name)
        sequencesTMB.append(seq)
        
TMB_overall = ''.join(map(str, sequencesTMB))
AA_freq(TMB_overall)<jupyter_output>AA       % composition
A        7.965135223199739
C        0.4024112088628217
D        6.7595307917888565
E        4.617139133268165
F        4.221244705115673
G        9.501466275659824
H        1.9582926034538939
I        4.11208862821766
K        4.622026718800912
L        7.893450635386119
M        1.5868361029651352
N        6.133919843597263
P        3.704789833822092
Q        4.351580319322255
R        5.047246660149886
S        7.642554578038449
T        6.712284131638971
V        6.112740306288694
W        1.6568914956011729
Y        4.998370804822418<jupyter_text>--- Q6<jupyter_code># AA composition
def AA_freq(peptide):
    aa_freq = {'A':0, 'C':0, 'D':0, 'E':0, 'F':0, 'G':0, 'H':0, 'I':0, 'K':0,
               'L':0, 'M':0, 'N' :0, 'P':0, 'Q':0, 'R':0, 'S':0, 'T':0, 'V':0, 
               'W':0, 'Y':0}
    for i in peptide: 
        aa_freq[i] += 1
    for i in aa_freq:
        aa_freq[i]= aa_freq[i]/len(peptide)*100
      
    return aa_freq
#Q6(a) Composition for each sequence in TMH: Filename: ‘TMHseq_composition.csv’

import csv
# name of csv file  
filenameA = "TMHseq_composition.csv"

# field names  
fields = ['Seq','A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q',
 'R', 'S', 'T', 'V', 'W', 'Y' ]

TMHdict=[]

for i in range(0, len(sequencesTMH)):
    seq= sequencesTMH[i]
    aa_freq_peptide= AA_freq(seq)
    aa_freq_peptide['Seq']= identifiersTMH[i]
    TMHdict.append(aa_freq_peptide)
    
    
with open(filenameA, 'w') as csvfile:  
    writer = csv.DictWriter(csvfile, fieldnames = fields)  
    writer.writeheader()  # field names 
    writer.writerows(TMHdict) # data rows
    
# Q6 (b)(c)(d)(e)(f)
TMHcomp = {'A':8.511, 'C':1.285, 'D': 4.017, 'E':4.798, 'F':5.628, 'G':7.145, 'H':2.248, 
           'I':6.784, 'K':4.499, 'L':11.628, 'M':2.861, 'N' :3.651, 'P':4.429, 'Q':3.337, 
           'R':4.391, 'S':6.678, 'T':5.423, 'V':7.465, 'W':1.809, 'Y':3.409}

TMBcomp = {'A':7.965, 'C':0.402, 'D':6.759, 'E':4.617, 'F':4.221, 'G':9.501, 'H':1.958, 
           'I':4.112, 'K':4.622, 'L':7.893, 'M':1.587, 'N' :6.134, 'P':3.705, 'Q':4.352, 
           'R':5.047, 'S':7.642, 'T':6.712, 'V':6.113, 'W':1.657, 'Y':4.998}

TruePositives = 0
FalseNegatives = 0
TrueNegatives = 0
FalsePositives = 0

for seq_comp in TMHdict:
    dev_A=0
    dev_B=0
    
    for i in TMHcomp.keys():
        dev_A = dev_A + abs(seq_comp[i]- TMHcomp[i])
        dev_B = dev_B + abs(seq_comp[i]- TMBcomp[i])
        
    if dev_A < dev_B:
        TruePositives = TruePositives + 1
    else:
        FalseNegatives = FalseNegatives + 1   

print(TruePositives)
print(FalseNegatives)<jupyter_output>1058
96<jupyter_text>Q7<jupyter_code>#Q7 Composition for each sequence in TMB: Filename: ‘TMBseq_composition.csv’

filenameB = "TMBseq_composition.csv"
# field names  
fields = ['Seq','A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y' ]

TMBdict=[]

for i in range(0, len(sequencesTMB)):
    seq= sequencesTMB[i]
    aa_freq_peptide= AA_freq(seq)
    aa_freq_peptide['Seq']= identifiersTMB[i]
    TMBdict.append(aa_freq_peptide)
    
    
with open(filenameB, 'w') as csvfile:  
    writer = csv.DictWriter(csvfile, fieldnames = fields)  
    writer.writeheader()   # field names 
    writer.writerows(TMBdict) # data rows  

for seq_comp in TMBdict:
    dev_A=0
    dev_B=0
    
    for i in TMBcomp.keys():
        dev_A = dev_A + abs(seq_comp[i]- TMHcomp[i])
        dev_B = dev_B + abs(seq_comp[i]- TMBcomp[i])
        
    if dev_A > dev_B:
        TrueNegatives = TrueNegatives + 1
    else:
        FalsePositives = FalsePositives + 1   
print(TrueNegatives)
print(FalsePositives)<jupyter_output>121
12<jupyter_text>Q8<jupyter_code># Using 100% Non-redundant dataset as training 
TP = TruePositives
FN = FalseNegatives
TN = TrueNegatives
FP= FalsePositives

Sensitivity = TP/(TP+FN)
Specificity = TN/(TN+FP)
Accuracy = (TP+TN)/(TP+TN+FP+FN)

print('Sensitivity using 100% dataset as training= ', Sensitivity)
print('Specificity using 100% dataset as training= ', Specificity)
print('Accuracy using 100% dataset as training= ', Accuracy)<jupyter_output>Sensitivity using 100% dataset as training=  0.9168110918544194
Specificity using 100% dataset as training=  0.9097744360902256
Accuracy using 100% dataset as training=  0.916083916083916<jupyter_text>Q9<jupyter_code># Include a deviation (TMH + 0.5) to estimate the sensitivity, specificity and accuracy
TruePositives = 0
FalseNegatives = 0

for seq_comp in TMHdict:
    dev_A=0
    dev_B=0
    
    for i in TMHcomp.keys():
        dev_A = dev_A + abs(seq_comp[i]- TMHcomp[i]) 
        dev_B = dev_B + abs(seq_comp[i]- TMBcomp[i])
        
    if dev_A + 0.5 < dev_B:      # Deviation (TMH + 0.5) 
        TruePositives = TruePositives + 1
    else:
        FalseNegatives = FalseNegatives + 1

print(TruePositives)
print(FalseNegatives)
# Include a deviation (TMH + 0.5) to estimate the sensitivity, specificity and accuracy
TP = TruePositives
FN = FalseNegatives
TN = TrueNegatives
FP= FalsePositives

Sensitivity = TP/(TP+FN)
Specificity = TN/(TN+FP)
Accuracy = (TP+TN)/(TP+TN+FP+FN)

print('Sensitivity using 100% dataset as training and deviation 0.5 to TMH= ', Sensitivity)
print('Specificity using 100% dataset as training and deviation 0.5 to TMH = ', Specificity)
print('Accuracy using 100% dataset as training and deviation 0.5 to TMH= ', Accuracy)<jupyter_output>Sensitivity using 100% dataset as training and deviation 0.5 to TMH=  0.9064124783362218
Specificity using 100% dataset as training and deviation 0.5 to TMH =  0.9097744360902256
Accuracy using 100% dataset as training and deviation 0.5 to TMH=  0.9067599067599068<jupyter_text>Q10<jupyter_code># Parse all non-reduntant TMH        
fasta_file = "alphanr40.txt"
identifiersTMH =[]  
sequencesTMH= []    

with open(fasta_file) as fp:
    for name, seq in read_fasta(fp):
        seq= seq.replace(" ", "")   #Remove space
        seq= seq.replace("U", "")   #Remove 'U'
                  
        identifiersTMH.append(name)
        sequencesTMH.append(seq)   

# Parse all non-reduntant TMB
fasta_file = "betanr40.txt"
identifiersTMB =[]
sequencesTMB= []

with open(fasta_file) as fp:
    for name, seq in read_fasta(fp):
        seq= seq.replace(" ", "")   #Remove space
        seq= seq.replace("U", "")   #Remove 'U'
                  
        identifiersTMB.append(name)
        sequencesTMB.append(seq)
        

# Considering 70% of TMH and 70% of TMB to compute the composition
# using 70% dataset as training and 30% dataset as test
import sklearn       
from sklearn.model_selection import train_test_split

trainTMH70, testTMH70 = sklearn.model_selection.train_test_split(sequencesTMH, train_size=0.7, test_size=0.3, random_state=9)
trainTMB70, testTMB70 = sklearn.model_selection.train_test_split(sequencesTMB, train_size=0.7, test_size=0.3, random_state=9)

trainTMH70_overall = ''.join(map(str, trainTMH70))
trainTMH70comp= AA_freq(trainTMH70_overall)        #TMH comp using 70% Training

trainTMB70_overall = ''.join(map(str, trainTMB70))
trainTMB70comp= AA_freq(trainTMB70_overall)        #TMB comp using 70% Training

TP70 = 0
FN70 = 0
TN70 = 0
FP70 = 0

for seq in testTMH70:
    aa_comp_test= AA_freq(seq)
    dev_A70=0
    dev_B70=0
    
    for i in trainTMH70comp.keys():
        dev_A70 = dev_A70 + abs(aa_comp_test[i]- trainTMH70comp[i])
        dev_B70 = dev_B70 + abs(aa_comp_test[i]- trainTMB70comp[i])
        
    if dev_A70 < dev_B70:
        TP70 = TP70 + 1
    else:
        FN70 = FN70 + 1   

for seq in testTMB70:
    aa_comp_test= AA_freq(seq)
    dev_A70=0
    dev_B70=0
    
    for i in trainTMB70comp.keys():
        dev_A70 = dev_A70 + abs(aa_comp_test[i]- trainTMH70comp[i])
        dev_B70 = dev_B70 + abs(aa_comp_test[i]- trainTMB70comp[i])
        
    if dev_A70 > dev_B70:
        TN70 = TN70 + 1
    else:
        FP70 = FP70 + 1
    
Sensitivity70 = TP70/(TP70+FN70)
Specificity70 = TN70/(TN70+FP70)
Accuracy70 = (TP70+TN70)/(TP70+TN70+FP70+FN70)

print('Sensitivity using 70% dataset as training and 30% dataset as test= ', Sensitivity70)
print('Specificity using 70% dataset as training and 30% dataset as test= ', Specificity70)
print('Accuracy using 70% dataset as training and 30% dataset as test = ', Accuracy70)
# Considering 60% of TMH and 60% of TMB to compute the composition
# using 60% dataset as training and 40% dataset as test
import sklearn       
from sklearn.model_selection import train_test_split

trainTMH60, testTMH60 = sklearn.model_selection.train_test_split(sequencesTMH, train_size=0.6, test_size=0.4, random_state=9)
trainTMB60, testTMB60 = sklearn.model_selection.train_test_split(sequencesTMB, train_size=0.6, test_size=0.4, random_state=9)

trainTMH60_overall = ''.join(map(str, trainTMH60))
trainTMH60comp= AA_freq(trainTMH60_overall)        #TMH comp using 60% Training

trainTMB60_overall = ''.join(map(str, trainTMB60))
trainTMB60comp= AA_freq(trainTMB60_overall)        #TMB comp using 60% Training

TP60 = 0
FN60 = 0
TN60 = 0
FP60 = 0

for seq in testTMH60:
    aa_comp_test= AA_freq(seq)
    dev_A60=0
    dev_B60=0
    
    for i in trainTMH60comp.keys():
        dev_A60 = dev_A60 + abs(aa_comp_test[i]- trainTMH60comp[i])
        dev_B60 = dev_B60 + abs(aa_comp_test[i]- trainTMB60comp[i])
        
    if dev_A60 < dev_B60:
        TP60 = TP60 + 1
    else:
        FN60 = FN60 + 1   

for seq in testTMB60:
    aa_comp_test= AA_freq(seq)
    dev_A60=0
    dev_B60=0
    
    for i in trainTMB60comp.keys():
        dev_A60 = dev_A60 + abs(aa_comp_test[i]- trainTMH60comp[i])
        dev_B60 = dev_B60 + abs(aa_comp_test[i]- trainTMB60comp[i])
        
    if dev_A60 > dev_B60:
        TN60 = TN60 + 1
    else:
        FP60 = FP60 + 1
    
Sensitivity60 = TP60/(TP60+FN60)
Specificity60 = TN60/(TN60+FP60)
Accuracy60 = (TP60+TN60)/(TP60+TN60+FP60+FN60)

print('Sensitivity using 60% dataset as training and 40% dataset as test= ', Sensitivity60)
print('Specificity using 60% dataset as training and 40% dataset as test= ', Specificity60)
print('Accuracy using 60% dataset as training and 40% dataset as test = ', Accuracy60)
# Considering 50% of TMH and 50% of TMB to compute the composition
# using 50% dataset as training and 50% dataset as test
import sklearn       
from sklearn.model_selection import train_test_split

trainTMH50, testTMH50 = sklearn.model_selection.train_test_split(sequencesTMH, train_size=0.5, test_size=0.5, random_state=9)
trainTMB50, testTMB50 = sklearn.model_selection.train_test_split(sequencesTMB, train_size=0.5, test_size=0.5, random_state=9)

trainTMH50_overall = ''.join(map(str, trainTMH50))
trainTMH50comp= AA_freq(trainTMH50_overall)        #TMH comp using 50% Training

trainTMB50_overall = ''.join(map(str, trainTMB50))
trainTMB50comp= AA_freq(trainTMB50_overall)        #TMB comp using 50% Training

TP50 = 0
FN50 = 0
TN50 = 0
FP50 = 0

for seq in testTMH50:
    aa_comp_test= AA_freq(seq)
    dev_A50=0
    dev_B50=0
    
    for i in trainTMH50comp.keys():
        dev_A50 = dev_A50 + abs(aa_comp_test[i]- trainTMH50comp[i])
        dev_B50 = dev_B50 + abs(aa_comp_test[i]- trainTMB50comp[i])
        
    if dev_A50 < dev_B50:
        TP50 = TP50 + 1
    else:
        FN50 = FN50 + 1   

for seq in testTMB50:
    aa_comp_test= AA_freq(seq)
    dev_A50=0
    dev_B50=0
    
    for i in trainTMB50comp.keys():
        dev_A50 = dev_A50 + abs(aa_comp_test[i]- trainTMH50comp[i])
        dev_B50 = dev_B50 + abs(aa_comp_test[i]- trainTMB50comp[i])
        
    if dev_A50 > dev_B50:
        TN50 = TN50 + 1
    else:
        FP50 = FP50 + 1
    
Sensitivity50 = TP50/(TP50+FN50)
Specificity50 = TN50/(TN50+FP50)
Accuracy50 = (TP50+TN50)/(TP50+TN50+FP50+FN50)

print('Sensitivity using 50% dataset as training and 50% dataset as test = ', Sensitivity50)
print('Specificity using 50% dataset as training and 50% dataset as test = ', Specificity50)
print('Accuracy using 50% dataset as training and 50% dataset as test= ', Accuracy50)
# Considering 40% of TMH and 40% of TMB to compute the composition
# using 40% dataset as training and 60% dataset as test
import sklearn       
from sklearn.model_selection import train_test_split

trainTMH40, testTMH40 = sklearn.model_selection.train_test_split(sequencesTMH, train_size=0.4, test_size=0.6, random_state=9)
trainTMB40, testTMB40 = sklearn.model_selection.train_test_split(sequencesTMB, train_size=0.4, test_size=0.6, random_state=9)

trainTMH40_overall = ''.join(map(str, trainTMH40))
trainTMH40comp= AA_freq(trainTMH40_overall)        #TMH comp using 40% Training

trainTMB40_overall = ''.join(map(str, trainTMB40))
trainTMB40comp= AA_freq(trainTMB40_overall)        #TMB comp using 40% Training

TP40 = 0
FN40 = 0
TN40 = 0
FP40 = 0

for seq in testTMH40:
    aa_comp_test= AA_freq(seq)
    dev_A40=0
    dev_B40=0
    
    for i in trainTMH40comp.keys():
        dev_A40 = dev_A40 + abs(aa_comp_test[i]- trainTMH40comp[i])
        dev_B40 = dev_B40 + abs(aa_comp_test[i]- trainTMB40comp[i])
        
    if dev_A40 < dev_B40:
        TP40 = TP40 + 1
    else:
        FN40 = FN40 + 1   

for seq in testTMB40:
    aa_comp_test= AA_freq(seq)
    dev_A40=0
    dev_B40=0
    
    for i in trainTMB40comp.keys():
        dev_A40 = dev_A40 + abs(aa_comp_test[i]- trainTMH40comp[i])
        dev_B40 = dev_B40 + abs(aa_comp_test[i]- trainTMB40comp[i])
        
    if dev_A40 > dev_B40:
        TN40 = TN40 + 1
    else:
        FP40 = FP40 + 1
    
Sensitivity40 = TP40/(TP40+FN40)
Specificity40 = TN40/(TN40+FP40)
Accuracy40 = (TP40+TN40)/(TP40+TN40+FP40+FN40)

print('Sensitivity using 40% dataset as training and 60% dataset as test= ', Sensitivity40)
print('Specificity using 40% dataset as training and 60% dataset as test= ', Specificity40)
print('Accuracy using 40% dataset as training and 60% dataset as test = ', Accuracy40)
# Considering 30% of TMH and 30% of TMB to compute the composition
# using 30% dataset as training and 70% dataset as test
import sklearn       
from sklearn.model_selection import train_test_split

trainTMH30, testTMH30 = sklearn.model_selection.train_test_split(sequencesTMH, train_size=0.3, test_size=0.7, random_state=9)
trainTMB30, testTMB30 = sklearn.model_selection.train_test_split(sequencesTMB, train_size=0.3, test_size=0.7, random_state=9)

trainTMH30_overall = ''.join(map(str, trainTMH30))
trainTMH30comp= AA_freq(trainTMH30_overall)        #TMH comp using 30% Training

trainTMB30_overall = ''.join(map(str, trainTMB30))
trainTMB30comp= AA_freq(trainTMB30_overall)        #TMB comp using 30% Training

TP30 = 0
FN30 = 0
TN30 = 0
FP30 = 0

for seq in testTMH30:
    aa_comp_test= AA_freq(seq)
    dev_A30=0
    dev_B30=0
    
    for i in trainTMH30comp.keys():
        dev_A30 = dev_A30 + abs(aa_comp_test[i]- trainTMH30comp[i])
        dev_B30 = dev_B30 + abs(aa_comp_test[i]- trainTMB30comp[i])
        
    if dev_A30 < dev_B30:
        TP30 = TP30 + 1
    else:
        FN30 = FN30 + 1   

for seq in testTMB30:
    aa_comp_test= AA_freq(seq)
    dev_A30=0
    dev_B30=0
    
    for i in trainTMB30comp.keys():
        dev_A30 = dev_A30 + abs(aa_comp_test[i]- trainTMH30comp[i])
        dev_B30 = dev_B30 + abs(aa_comp_test[i]- trainTMB30comp[i])
        
    if dev_A30 > dev_B30:
        TN30 = TN30 + 1
    else:
        FP30 = FP30 + 1
    
Sensitivity30 = TP30/(TP30+FN30)
Specificity30 = TN30/(TN30+FP30)
Accuracy30 = (TP30+TN30)/(TP30+TN30+FP30+FN30)

print('Sensitivity using 30% dataset as training and 70% dataset as test= ', Sensitivity30)
print('Specificity using 30% dataset as training and 70% dataset as test= ', Specificity30)
print('Accuracy using 30% dataset as training and 70% dataset as test= ', Accuracy30)<jupyter_output>Sensitivity using 30% dataset as training and 70% dataset as test=  0.8910891089108911
Specificity using 30% dataset as training and 70% dataset as test=  0.9148936170212766
Accuracy using 30% dataset as training and 70% dataset as test=  0.893569844789357
{'fraction_non_alphanumeric': 0.12349634626194492, 'fraction_numerical': 0.09561551433389545, 'mean_word_length': 3.3098352713178296, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '190883', 'license': 'MIT', 'n_tokens_mistral': 8081, 'n_tokens_neox': 6815, 'n_words': 1625, 'path': 'P1_TMH_TMB_Seq_Composition/BE17B009_Practical1.ipynb', 'repo_name': 'Roshni1999/Computational-Biology-BT4110', 'size': '17790'}