#!/usr/local/bin/python
#/usr/bin/python

import sys
import string
from string import *

#######################
def readClipInfoAndOutput(fp_in, fp_out):
	lines = fp_in.readlines()
	if not lines:
		print "No inut in the input file"
		sys.exit(0)  # if no input, exit
	i = 0   # the line index

	clip_str = "Clip"

	#jump to the start line
	i = line_num_string_head(lines, i, clip_str)

	#i = 264520
	while i < len(lines):
		print i
		line = get_line(lines, i)
		tokens = split(line)
		seqName = tokens[1]
		leftClip = tokens[4]
		leftClip = ridOfComma(leftClip)   # rid of the comma
		rightClip = tokens[7]
		rightClip = ridOfComma(rightClip)   # rid of the comma
		length = tokens[9]
		length = ridOfComma(length)   # rid of the comma
		rightSize = tokens[12]
		outputToFile(fp_out, seqName, leftClip, rightClip, length, rightSize)
		i = line_num_string_head(lines, i+1, clip_str)


def outputToFile(fp_out, arg1, arg2, arg3, arg4, arg5):
	t = "\t"
	str = arg1 + t + arg2 + t + arg3 + t + arg4 + t + arg5 + "\n"
	fp_out.write(str)

def ridOfComma(str):
	if str[len(str)-1] == ",":
		return str[0:len(str)-1]
	return str

def get_line(lines, index):
	line = lines[index]
	line = line[0:len(line)-1]
	return line

def line_num_string_head(lines, i, str):
	while 1:
		if i >= len(lines):
			return len(lines)
		line = get_line(lines, i)
		if line[0:len(str)] == str:
			break
		i = i + 1
	return i


########################
#main

#ask for input file name
s = raw_input("Please input the Clip input file name : ")
input_filename = s

#ask for output file name
s = raw_input("Please input the Clip output file name : ")
output_filename = s

if input_filename == output_filename:
	print "Error: input filename is the same as the output filename"
	sys.exit(0)

#open file handlers
fp_in = open(input_filename, "rb")
fp_out = open(output_filename, "wb")

#read the clip info and output it
readClipInfoAndOutput(fp_in, fp_out)

#close file handlers
fp_in.close()
fp_out.close()



