#!/bin/bash
# -*- coding: utf-8 -*-

##get the present directory
WD=$(pwd)


# loop over all fas files in the current directory
for file in *.fas
do
  outfile_name=$(echo $file | sed 's/algn\.fas/algn02\.fasta/g')
  #echo the modified filename to screen
  #echo "$outfile_name"
  #remove any previous versions of the outputfile
  rm "$WD"/"$outfile_name"
  #write a new output file to write to
  touch "$WD"/"$outfile_name"
  iconv -f UTF-8 -t UTF-8 "$WD"/"$outfile_name"

	# read individual fas file line by line
	while read line
	do
		if [[ $line == ">"* ]] # if line starts with '>'
			then
				echo "$line" | \
					#replace in the date at the end of each sequence name line
					#The elements in parenteses are possible to retain in the 'sed' function
					#this is done with '\1' and '\2'
					sed -E 's/(.)_([0-9]+)-([0-9]+)-([0-9]+)$/\1_\2/g' >> "$WD"/"$outfile_name"
		else # if line doesn't start wsith >, then write the line, but modify it with sed
			echo "$line" | \
				#replace the Us to Ts
				sed 's/U/T/g' >> "$WD"/"$outfile_name"
	  fi
	done  < ${file}
	#use head to check how the file ends up being modified
	head -12 "$WD"/"$outfile_name" | cut -c1-80
done


	
	
##