#!/usr/bin/python

import os
import sys
import shutil
import re
import optparse
import os.path
import glob
import commands

fileList = glob.glob('bicoh_out*.txt')
#print fileList

peaks = []
peak = ''
matlink = ''

linksFile = open('links.txt','r')
links = linksFile.readlines()
linksFile.close()

outFile = open('rawPeakList.txt','w')

for line in fileList:
	flObj = open(line,'r')
	fl = flObj.readlines()
	#print line
	matlink = ''
	for i, item in enumerate(fl):
		if item.find("Found final QPC candidate") != -1:
			peak = fl[i+1].strip('\n')
			peak = peak.split('...')
			bicohFile = line
			try:
				if float(peak[0])== 0 or float(peak[1])== 0:
					print 'f1: ' + peak[0] + ' | f2: ' + peak[1] + '        ' + peak[2] + peak[3] + '       ' + peak[4] + peak[5] + '       ' + peak[6] + peak[7] + ' [WARNING: coupled with 0!]'
                                        outFile.write('f1: ' + peak[0] + ' | f2: ' + peak[1] + '        ' + peak[2] + peak[3] + '       ' + peak[4] + peak[5] + '       ' + peak[6] + peak[7] + ' [WARNING: coupled with 0!]\n')
				else:
					print 'f1: ' + peak[0] + ' | f2: ' + peak[1] + '	' + peak[2] + peak[3] + '	' + peak[4] + peak[5] + '	' + peak[6] + peak[7]
					outFile.write('f1: ' + peak[0] + ' | f2: ' + peak[1] + '        ' + peak[2] + peak[3] + '       ' + peak[4] + peak[5] + '       ' + peak[6] + peak[7] + '\n')
			except:
				try:
					if float(peak[0])== 0 or float(peak[1])== 0:
						print 'f1: ' + peak[0] + ' | f2: ' + peak[1] + ' [WARNING: coupled with 0!]'
                                                outFile.write('f1: ' + peak[0] + ' | f2: ' + peak[1] + ' [WARNING: coupled with 0!]\n')
					else:
						print 'f1: ' + peak[0] + ' | f2: ' + peak[1]
						outFile.write('f1: ' + peak[0] + ' | f2: ' + peak[1] + '\n')
				except:
					print 'Badline!'
			for linkItem in links:
				#print line
				filename = line.split('.')
				filename = filename[0].strip('\n')
				#print filename
				if linkItem.find(filename) != -1:
					matlink = linkItem.strip('\n')
	if matlink != '':
		matlink = matlink.split()
		matlink = matlink[1]
		print matlink
		print bicohFile
		print '****************************'
		outFile.write(matlink + '\n') 
                outFile.write(bicohFile + '\n')
                outFile.write('****************************' + '\n')
	flObj.close()


