Your IP : 216.73.216.40


Current Path : /var/www/html/samantasintu/
Upload File :
Current File : /var/www/html/samantasintu/app_Backend.py

######################################################################################
######################################################################################


#to download data 
import os,requests,time
#os.chdir(r"C:/Users/seven/OneDrive/Desktop/SEM4")
#print(os.getcwd())
# def req():
#     try:
#         c = requests.get("https://rest.uniprot.org/uniprotkb/stream?fields=accession%2Corganism_name%2Corganism_id%2Creviewed%2Clineage%2Clineage_ids%2Cgene_names%2Cid%2Cgene_oln%2Cgene_orf%2Cgene_primary%2Cgene_synonym%2Cprotein_name%2Cxref_proteomes%2Cmass%2Clength%2Ccc_alternative_products%2Cft_non_std%2Cft_var_seq%2Cft_non_ter%2Ccc_rna_editing%2Ccc_polymorphism%2Csequence%2Corganelle%2Ccc_mass_spectrometry%2Cft_conflict%2Csequence_version%2Cft_variant%2Cft_non_cons%2Cft_act_site%2Cft_binding%2Ccc_catalytic_activity%2Ccc_cofactor%2Cft_dna_bind%2Cec%2Ccc_activity_regulation%2Ccc_function%2Crhea%2Ckinetics%2Ccc_pathway%2Cph_dependence%2Cannotation_score%2Ccc_miscellaneous%2Ckeyword%2Cprotein_existence%2Cfeature_count%2Ccomment_count%2Ctools%2Ccc_interaction%2Ccc_subunit%2Ccc_developmental_stage%2Ccc_tissue_specificity%2Cgo_p%2Cgo%2Cgo_id%2Cgo_c%2Cgo_f%2Ccc_biotechnology%2Ccc_disease%2Cft_mutagen%2Ccc_toxic_dose%2Ccc_pharmaceutical%2Cft_intramem%2Cft_topo_dom%2Ccc_subcellular_location%2Cft_transmem%2Cft_chain%2Cft_disulfid%2Cft_carbohyd%2Cft_crosslnk%2Cft_lipid%2Cft_mod_res%2Cft_peptide%2Ccc_ptm%2Cft_signal%2Cstructure_3d%2Cft_strand%2Cft_helix%2Cft_turn%2Clit_pubmed_id%2Cversion%2Cft_coiled%2Cft_compbias%2Ccc_domain%2Cft_domain%2Cft_motif%2Cprotein_families%2Cft_region%2Cft_repeat%2Cft_zn_fing%2Cxref_pdb%2Cxref_pdbsum%2Cxref_intact%2Cxref_string%2Cxref_mint%2Cxref_dip%2Cxref_biogrid%2Cxref_bindingdb%2Cxref_chembl%2Cxref_drugbank%2Cxref_imgt_gene-db%2Cxref_dbsnp%2Cxref_geneid%2Cxref_kegg%2Cxref_ensembl%2Cxref_genetree%2Cxref_brenda%2Cxref_biocyc%2Cxref_rnact%2Cxref_expressionatlas%2Cxref_panther%2Cxref_prosite%2Cxref_interpro%2Cxref_embl%2Cxref_ccds%2Cxref_refseq&format=tsv&query=(keyword:KW-0929)").text
#         return c
#     except:
#         print("Network Error : No connection, retrying")
#         time.sleep(0.011)
#         h = req()
#         if h:
#             print('Network Resumption : Connection Successful')
#             return h

# print("Updating main Dataset")

# with open('main_dataset.tsv','w',encoding="utf-8") as f:
#     f.write(req().replace("/"," ").replace("'",'`').replace('"','`').replace(',',';'))
# print("Updated main Dataset")

# time.sleep(2)


# ######################################################################################
# ######################################################################################


# #to format the main_dataset
# import pandas as pd

# f = open('main_dataset.tsv','r',encoding="utf-8").readlines()
# a = [i for i in range(1,len(f))]
# b = ["mrsa_"+str(i) for i in range(1,len(f))]

# print('Writing TSV Data')
        
# #import pandas as pd
# tsv_file = "main_dataset.tsv"
# csv_table = pd.read_table(tsv_file,sep='\t',dtype=object)
# csv_table.insert(0,column='Serial_No',value=a)
# csv_table.insert(1,column='mrsa_No_',value=b)
# csv_table.to_csv('pre-normal.tsv',sep='\t',index=False)

# with open('normal.tsv','w') as f:
#     for i in open('pre-normal.tsv').readlines():
#         for k in range(25):
#             i = i.replace('\t\t','\t Nil \t')
#         a = i.split('\t')[-1]
#         #print(a)
#         if a == '\n':
#             #print('Eta')
#             _ = f.write(i.strip()+'\t Nil \n')
#         else:
#             _ = f.write(i)
            
# print('Data formatting complete')
# #exit()


# ######################################################################################
# ######################################################################################


# #Sai
# #to compute features of downloaded data
# print('Data processing/computation')
# from peptides import Peptide as pept
# import Bio.SeqUtils.ProtParam as proparm
# file = open('main_dataset.tsv').readlines()
# seq_col = [i for i in range(len(file[0].split('\t'))) if file[0].split('\t')[i]=='Sequence'][0]
# with open('compute.tsv','w') as f:
#     f.write('UniProt_acc\tAliphatic Index\tInstability Index\tHydrophobicity (GRAVY)\tHydrophobic Moment\tIsoelectric Point\tMolecular Weight\tCharge (at pH 7)\tCounts\t\
# Frequencies\tBLOSUM1\tBLOSUM2\tBLOSUM3\tBLOSUM4\tBLOSUM5\tBLOSUM6\tBLOSUM7\tBLOSUM8\tBLOSUM9\tBLOSUM10\tPP1\tPP2\tPP3\tF1\tF2\tF3\tF4\tF5\tF6\tKF1\tKF2\
# \tKF3\tKF4\tKF5\tKF6\tKF7\tKF8\tKF9\tKF10\tMSWHIM1\tMSWHIM2\tMSWHIM3\tE1\tE2\tE3\tE4\tE5\tProtFP1\tProtFP2\tProtFP3\tProtFP4\tProtFP5\tProtFP6\tProtFP7\
# \tProtFP8\tSV1\tSV2\tSV3\tSV4\tST1\tST2\tST3\tST4\tST5\tST6\tST7\tST8\tT1\tT2\tT3\tT4\tT5\tVHSE1\tVHSE2\tVHSE3\tVHSE4\tVHSE5\tVHSE6\tVHSE7\tVHSE8\tZ1\tZ2\
# \tZ3\tZ4\tZ5\tSecondary Structure Fraction\tAromaticity\tMolar Extinction Coefficient (cysteine|cystine)\tFlexibility\n')
#     #descriptor computation,extraction and printing
#     for i in file[1:]:
#         j = i.split('\t')
#         pep = pept(j[int(seq_col)])
#         f.write(j[0]+'\t')
#         #Peptide feature calculation
#         prop = (str(pep.aliphatic_index())+'\t'+str(pep.instability_index())+'\t'+str(pep.hydrophobicity())+'\t'+str(pep.hydrophobic_moment())+'\t'+
#         str(pep.isoelectric_point())+'\t'+str(pep.molecular_weight())+'\t'+str(pep.charge())+'\t'+str(pep.counts())+'\t'+str(pep.frequencies())+'\t')
#         f.write(prop)
#         #Peptide.descriptor computation
#         dict1 = pep.descriptors()
#         for i in dict1.keys():
#             f.write(str(dict1[i]) + '\t')
#         #Protparam feature calculation
#         propar = proparm.ProteinAnalysis(j[int(seq_col)])
#         f.write(str(propar.secondary_structure_fraction())+'\t'+str(propar.aromaticity())+'\t'+str(propar.molar_extinction_coefficient())+'\t')
#         try:
#             f.write(str(propar.flexibility())+'\t')
#         except:
#             f.write('Cannot be computed for peptide with non-standard amino acid residues\t')
#         f.write('\n')

# print('Data processing/computation complete')
######################################################################################
######################################################################################
print('Data upload to backend')

#to upload the data
#to connect to mysql
import mysql.connector as mysql

config = {
	'user':'root',
	'password':'',
	'host':'127.0.0.1',
	'use_pure':True,
	'raise_on_warnings':True,
	}
connect2= mysql.connect(**config)
cursor = connect2.cursor()
try:
    cursor.execute("CREATE SCHEMA IF NOT EXISTS sintu")
except:
    print('db exists')
try:
    cursor.execute("USE `sintu`")
except:
    print('Not Using sintu')
try:
    cursor.execute("DROP TABLE IF EXISTS `data`")	
except:
    print("Dropping not possible")

#to store and process column names (normal Table) or the sql and php parts

sql1,sql2 = '',''

with open('data.txt') as f:
  names = f.readline().strip().split('\t')

for i in names:
  sql1 += '`'+i+'` text DEFAULT NULL,'
  sql2 += '`'+i+'`,'

  
Table = '''CREATE TABLE IF NOT EXISTS `data` (
  '''+sql1[:-1]+'''
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED'''
try:
    cursor.execute(Table)
except:
    print('Table exists')
try:
    cursor.execute("ALTER TABLE `data` ADD PRIMARY KEY (`Entry`);\
               ALTER TABLE `data` ROW_FORMAT=DYNAMIC;",multi=True)
except:
    print("Primary Key Assignment issue")
connect2.commit()


print("Uploading normal")
for i in open('data.txt').readlines()[1:]:
    j = 'INSERT INTO `data`('+sql2[:-1]+') VALUES\
("'+i.strip().replace('\t','","')+'")'
    print(i.split('\t')[1])
    print(len(i.split('\t')[1]))
    print(j)
    cursor.execute(j)
    connect2.commit()
print("Uploaded normal")

#to store and process column names (Compute Table) or the sql and php parts

# sql1,sql2 = '',''

# with open('compute.tsv') as f:
#   names = f.readline().strip().split('\t')

# for i in names:
#   sql1 += '`'+i+'` text DEFAULT NULL,'
#   sql2 += '`'+i+'`,'

  
# Table = '''CREATE TABLE IF NOT EXISTS `compute` (
#   '''+sql1[:-1]+'''
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED'''
# try:
#     cursor.execute(Table)
# except:
#     print('Table exists')
# try:
#     cursor.execute("ALTER TABLE `compute` ADD PRIMARY KEY (`Entry`);\
#                ALTER TABLE `compute` ROW_FORMAT=DYNAMIC;",multi=True)
# except:
#     print("Primary Key Assignment issue")
connect2.commit()


# print("Uploading compute_dataset")
# for i in open('compute.tsv').readlines()[1:]:
#     j = 'INSERT INTO `compute`('+sql2[:-1]+') VALUES\
# ("'+i.strip().replace('\t','","')+'")'
#     print(i.split('\t')[1])
#     cursor.execute(j)
#     connect2.commit()
# print("Uploaded compute_dataset")

connect2.close()