#!/usr/bin/python

"""
   Read the bulk converted file (not trifiltered)
   and the triplicate file to identify technical replicates
   and collect VAF for each mutation common in the replicates
"""

import sys

triplicates = dict()
trifile = open(sys.argv[1])
for i in trifile:
	fields = i.rstrip().split()
	repname = fields[0]
	for name in fields[1:]:
		triplicates[name] = repname
trifile.close()

ratiofieldnum = 69 # But for some version it is different

result = dict()

for i in open(sys.argv[2]):
	if i.startswith("#"):
		if i.startswith("#Anal"):
			colnames = i.rstrip().split("\t")
			for j in range(len(colnames)):
				if colnames[j] == "PM-Tum":
					ratiofieldnum = j
					break
		continue
	fields = i.rstrip().split("\t")
	mtype  = fields[15]
	if mtype != "Sub":
		continue
	sample = fields[1]
	chrx   = fields[4]
	pos    = fields[5]
	alt    = fields[7]
	ratio  = fields[ratiofieldnum]
	rname  = triplicates[sample]
	key    = rname + "\t" + chrx + "_" + pos + "_" + alt
	if key not in result:
		result[key] = list()
	result[key].append(ratio)

for r in result:
	if len(result[r]) < 2:
		continue
	print r + "\t" + "\t".join(result[r][:2])
