#!/usr/bin/env python#Coding:utf-8#Import pandas as pd, NumPy as np;" "Convert the CSV file to the corresponding adjacency matrix Mat" " fromNumPyImport*;defProtein_complexes_trans (): File= Open ('Protein_complexes.csv'); Filepro= Open ('complexes','a'); Filetarget= Open ('targets (complexes)','a'); Fileinter= Open ('Protein_complexes_interaction_matrix','a'); Proteins= []; Targets= []; while1: line=File.readline (); if notline : break; Token= Line.split (';'); Targ= Token[1].split (','); forIinchrange (0,len (targ)): targets.append (targ[i]); Proteins.append (token[0]); File.seek (0); #reset the pointer back to the first rowProarr= Unique (array (proteins)). tolist ();#go to the heavy and delete the last item (title)Tararr = unique (array (targets)). tolist ();#to delete the last#mat for adjacency matrixMat = Zeros (len (proarr), len (tararr)), dtype =int16); while1: line=File.readline (); if notline : break; Token= Line.split (';'); Targ= Token[1].split (','); Row=Proarr.index (token[0]); forIinchrange (0, Len (targ)): col=Tararr.index (targ[i]); mat[row][col]= 1; #List of output drugs and targeting forIinchProArr:filePro.write ('%s\n'%i); forIinchTarArr:fileTarget.write ('%s\n'%i); Print "%d&%d"%(len (proarr), Len (tararr)); Intermat=mat.tolist (); #writing a matrix to a file forIinchrange (0, len (proarr)): fileinter.write ('%s\n'%(str (intermat[i))); return0;defDrugs_targets_trans ():#print ' Hello World ';File = Open ('Drugs_targets_sum'); Filedrug= Open ('Drugs','a'); Filetarget= Open ('Targets','a'); Fileinter= Open ('Drugs_targets_interaction_matrix','a'); Drugs= []; Targets= []; while1: line=File.readline (); if notline : break; Token= Line.split (';'); Targ= Token[1].split (','); forIinchrange (0,len (targ)): targets.append (targ[i]); Drugs.append (token[0]); File.seek (0); #reset the pointer back to the first rowDrugarr= Unique (array (drugs)). tolist ();#go to the heavy and delete the last item (title)Tararr = unique (array (targets)). tolist ();#to delete the last#mat for adjacency matrixMat = Zeros (len (drugarr), len (tararr)), dtype =int16); while1: line=File.readline (); if notline : break; Token= Line.split (';'); Targ= Token[1].split (','); Row=Drugarr.index (token[0]); forIinchrange (0, Len (targ)): col=Tararr.index (targ[i]); mat[row][col]= 1; #List of output drugs and targeting forIinchDrugArr:fileDrug.write ('%s\n'%i); forIinchTarArr:fileTarget.write ('%s\n'%i); Intermat=mat.tolist (); forIinchrange (0, len (drugarr)): fileinter.write ('%s\n'%(str (intermat[i )));#Protein_complexes_trans ();Drugs_targets_trans ();
Using Python to convert the two value CSV format to a matrix