Using Python to convert the two value CSV format to a matrix

Source: Internet
Author: User

#!/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.