
def trim_sim_file(filename_in, filename_out, filename_label, labels, labels_out):
	
	import sys	
	file_in = open(filename_in)
	labels_in = open(filename_label)
	file_out = open(filename_out, "w")
	labels_outfile = open(labels_out, "w")
	matrix_labels = []

	for line in labels_in:
		matrix_labels.append(line.strip())

	firstline = True
	line_no = 0
	rows = 0
	rows_passed = 0
	for line in file_in:
		#print "LINE: " + str(line_no)
		if line_no % 100 == 99:
			print "Reading row %s / %s" % (line_no + 1, rows )

		wrote = False
		if firstline:
			firstline = False
			file_out.write(line)
			tokens = line.strip().split(" ")
			rows = int(tokens[0])
			continue
		line = line.strip()
		if len(line) == 0:
			line_no += 1
			continue 
		tokens = line.split(" ")
		if len(tokens) % 2 != 0:
			print >> sys.stderr, "len(tokens) % 2 != 0, line:",line
			continue
		current_label = matrix_labels[line_no]
		if current_label not in labels:
			line_no += 1
			continue
		for tok_no in xrange(0, len(tokens), 2):
			id = int(tokens[tok_no])
			if matrix_labels[id] in labels:
				continue
			val = float(tokens[tok_no+1])
			file_out.write(str(id) + " " + str(val) + " " )
			wrote = True
		if wrote:
			rows_passed += 1			
			file_out.write("\n")
			labels_outfile.write(matrix_labels[line_no] + "\n")
		line_no +=1

	print str(rows_passed) + " passed."
	print "!!!!!!!!!! IMPORTANT:"
	print "Replace the row number in the result file (first number in the file) with this number."
	file_out.close()
		

if __name__ == "__main__":
	import sys
	print "CRS Label filter sterting...."
	
	simmatrix_file = sys.argv[1]
	outfile = sys.argv[2]
	matrix_labelfile = sys.argv[3]
	filter_labelfile = sys.argv[4]
	labels_outfile = sys.argv[5]
	
	if len(sys.argv) != 6:
		print "Usage: filter_crs_labels <matrix_crs> <output_crs> <matrix_rowlabels> <labels_to_keep> <output_rowlabels>"
		sys.exit(1)	

	labels = {}

	for line in open(filter_labelfile):
		labels[line.strip()] = 1

	trim_sim_file(simmatrix_file, outfile, matrix_labelfile, labels, labels_outfile)
	
