#! /usr/bin/env python """ # ----------------------------------------------------------------------- # encoding: utf-8 # parse_annotations.py # Kevin Specht # Last Modified: 2017-04-27 # This code takes a network and a number and outputs a set of that # number of random genes # ----------------------------------------------------------------------- """ # ----------------------------------------------------- # Checking for input from the command line: # ----------------------------------------------------- # # [1] file with the seed genes (if table contains more than one # column they must be tab-separated; the first column will be # used only) # # [2] file providing the network in the form of an edgelist # (tab-separated table, columns 1 & 2 will be used) # # [3] number of desired iterations # # [4] name for the results file import sys import time import copy import csv import random """ Gets the set of seed genes """ def get_seed_genes(): f = open(sys.argv[1],'r') seedList=[] for line in f: seedList.append(line.rstrip()) f.close() return seedList """ Gets the links from the network """ def get_links(list): f=open(list,'r') linkList=[] for line in f: linkList.append(line.rstrip()) f.close() return linkList """ Gets all eligible disease genes in the network (not seed genes) """ def get_all_genes(list, seed_genes): all_genes=[] for line in list: genes=line.split() if genes[0] not in all_genes: if genes[0] not in seed_genes: all_genes.append(genes[0]) if genes[1] not in all_genes: if genes[1] not in seed_genes: all_genes.append(genes[1]) return all_genes """ Main program """ seedList=get_seed_genes() #get inputs network=get_links(sys.argv[2]) geneList=get_all_genes(network, seedList) randomList=[] #list of randomly selected genes for i in range(int(sys.argv[3])): #loop through specified iterations choice=random.choice(geneList) #choose a random gene from the network randomList.append(choice) #add gene to list of random genes geneList.remove(choice) #remove gene from the network f=open(sys.argv[4],'w') #write to outfile for rand in randomList: f.write(rand) f.write("\n") f.close()