#!/bin/sh


# Counts and extracts Motifs A - X from an RNAfold output file.

rm -r --force Motif*

for ((i=0; i<1; i++)) ;

do
	egrep -E "^.{$((37-$i))}\(\(\(\(\(\(" $1 -B2 --no-group-separator > MotifA_$i
	awk 'NR % 3 !=0' MotifA_$i > MotifA_$i.fasta
	sed -i 's/U/T/g' MotifA_$i.fasta
	fastx_trimmer  -i MotifA_$i.fasta -f $((38-$i)) -l $((43-$i)) -o MotifA_$(($i))_trimmed.fasta
	echo "$i" `wc -l < MotifA_$i.fasta | awk '{print $1/2}'` >> MotifA_count.txt



	egrep -E "^.{$((37-$i))}\)\)\)\)\)\)" $1 -B2 --no-group-separator > MotifB_$i
	awk 'NR % 3 !=0' MotifB_$i > MotifB_$i.fasta
	sed -i 's/U/T/g' MotifB_$i.fasta
	fastx_trimmer  -i MotifB_$i.fasta -f $((38-$i)) -l $((43-$i)) -o MotifB_$(($i))_trimmed.fasta
	echo "$i" `wc -l < MotifB_$i.fasta | awk '{print $1/2}'` >> MotifB_count.txt
                              
done
	echo "MotifA" >> MotifAll_count.txt
	cat MotifA_count.txt >> MotifAll_count.txt

	echo "MotifB" >> MotifAll_count.txt
	cat MotifB_count.txt >> MotifAll_count.txt

for ((i=0; i<6; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\(\(\(" $1 -B2 --no-group-separator > MotifC_$i
	awk 'NR % 3 !=0' MotifC_$i > MotifC_$i.fasta
	sed -i 's/U/T/g' MotifC_$i.fasta
	fastx_trimmer  -i MotifC_$i.fasta -f $((40-$i)) -l $((46-$i)) -o MotifC_$(($i))_trimmed.fasta
	
	egrep -E "^.{$((39-$i))}\)\)\)\.\)\)\)" $1 -B2 --no-group-separator > MotifD_$i
	awk 'NR % 3 !=0' MotifD_$i > MotifD_$i.fasta
	sed -i 's/U/T/g' MotifD_$i.fasta
	fastx_trimmer  -i MotifD_$i.fasta -f $((40-$i)) -l $((46-$i)) -o MotifD_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifC_$i.fasta | awk '{print $1/2}'` >> MotifC_count.txt
	echo "$i" `wc -l < MotifD_$i.fasta | awk '{print $1/2}'` >> MotifD_count.txt

                                  
done

	echo "MotifC" >> MotifAll_count.txt
	cat MotifC_count.txt >> MotifAll_count.txt
	echo "MotifD" >> MotifAll_count.txt
	cat MotifD_count.txt >> MotifAll_count.txt


for ((i=0; i<7; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\(\(\(" $1 -B2 --no-group-separator > MotifE_$i
	awk 'NR % 3 !=0' MotifE_$i > MotifE_$i.fasta
	sed -i 's/U/T/g' MotifE_$i.fasta
	fastx_trimmer  -i MotifE_$i.fasta -f $((40-$i)) -l $((47-$i)) -o MotifE_$(($i))_trimmed.fasta
	
	egrep -E "^.{$((39-$i))}\)\)\)\.\.\)\)\)" $1 -B2 --no-group-separator > MotifF_$i
	awk 'NR % 3 !=0' MotifF_$i > MotifF_$i.fasta
	sed -i 's/U/T/g' MotifF_$i.fasta
	fastx_trimmer  -i MotifF_$i.fasta -f $((40-$i)) -l $((47-$i)) -o MotifF_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifE_$i.fasta | awk '{print $1/2}'` >> MotifE_count.txt
	echo "$i" `wc -l < MotifF_$i.fasta | awk '{print $1/2}'` >> MotifF_count.txt

                                  
done

	echo "MotifE" >> MotifAll_count.txt
	cat MotifE_count.txt >> MotifAll_count.txt
	echo "MotifF" >> MotifAll_count.txt
	cat MotifF_count.txt >> MotifAll_count.txt

for ((i=0; i<8; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\(\(\(" $1 -B2 --no-group-separator > MotifG_$i
	awk 'NR % 3 !=0' MotifG_$i > MotifG_$i.fasta
	sed -i 's/U/T/g' MotifG_$i.fasta
	fastx_trimmer  -i MotifG_$i.fasta -f $((40-$i)) -l $((48-$i)) -o MotifG_$(($i))_trimmed.fasta
	
	egrep -E "^.{$((39-$i))}\)\)\)\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifH_$i
	awk 'NR % 3 !=0' MotifH_$i > MotifH_$i.fasta
	sed -i 's/U/T/g' MotifH_$i.fasta
	fastx_trimmer  -i MotifH_$i.fasta -f $((40-$i)) -l $((48-$i)) -o MotifH_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifQ_$i
	awk 'NR % 3 !=0' MotifQ_$i > MotifQ_$i.fasta
	sed -i 's/U/T/g' MotifQ_$i.fasta
	fastx_trimmer  -i MotifQ_$i.fasta -f $((40-$i)) -l $((48-$i)) -o MotifQ_$(($i))_trimmed.fasta

	echo "$i" `wc -l < MotifH_$i.fasta | awk '{print $1/2}'` >> MotifH_count.txt
	echo "$i" `wc -l < MotifG_$i.fasta | awk '{print $1/2}'` >> MotifG_count.txt
	echo "$i" `wc -l < MotifQ_$i.fasta | awk '{print $1/2}'` >> MotifQ_count.txt

	                                  
done

	echo "MotifG" >> MotifAll_count.txt
	cat MotifG_count.txt >> MotifAll_count.txt
	echo "MotifH" >> MotifAll_count.txt
	cat MotifH_count.txt >> MotifAll_count.txt
	echo "MotifQ" >> MotifAll_count.txt
	cat MotifQ_count.txt >> MotifAll_count.txt

for ((i=0; i<9; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\(\(\(" $1 -B2 --no-group-separator > MotifI_$i
	awk 'NR % 3 !=0' MotifI_$i > MotifI_$i.fasta
	sed -i 's/U/T/g' MotifI_$i.fasta
	fastx_trimmer  -i MotifI_$i.fasta -f $((40-$i)) -l $((49-$i)) -o MotifI_$(($i))_trimmed.fasta

	egrep -E "^.{$((39-$i))}\)\)\)\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifJ_$i
	awk 'NR % 3 !=0' MotifJ_$i > MotifJ_$i.fasta
	sed -i 's/U/T/g' MotifJ_$i.fasta
	fastx_trimmer  -i MotifJ_$i.fasta -f $((40-$i)) -l $((49-$i)) -o MotifJ_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifR_$i
	awk 'NR % 3 !=0' MotifR_$i > MotifR_$i.fasta
	sed -i 's/U/T/g' MotifR_$i.fasta
	fastx_trimmer  -i MotifR_$i.fasta -f $((40-$i)) -l $((49-$i)) -o MotifR_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifI_$i.fasta | awk '{print $1/2}'` >> MotifI_count.txt
	echo "$i" `wc -l < MotifJ_$i.fasta | awk '{print $1/2}'` >> MotifJ_count.txt
	echo "$i" `wc -l < MotifR_$i.fasta | awk '{print $1/2}'` >> MotifR_count.txt

		                                  
done

	echo "MotifI" >> MotifAll_count.txt
	cat MotifI_count.txt >> MotifAll_count.txt
	echo "MotifJ" >> MotifAll_count.txt
	cat MotifJ_count.txt >> MotifAll_count.txt
	echo "MotifR" >> MotifAll_count.txt
	cat MotifR_count.txt >> MotifAll_count.txt

for ((i=0; i<10; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\(\(\(" $1 -B2 --no-group-separator > MotifK_$i
	awk 'NR % 3 !=0' MotifK_$i > MotifK_$i.fasta
	sed -i 's/U/T/g' MotifK_$i.fasta
	fastx_trimmer  -i MotifK_$i.fasta -f $((40-$i)) -l $((50-$i)) -o MotifK_$(($i))_trimmed.fasta

	egrep -E "^.{$((39-$i))}\)\)\)\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifL_$i
	awk 'NR % 3 !=0' MotifL_$i > MotifL_$i.fasta
	sed -i 's/U/T/g' MotifL_$i.fasta
	fastx_trimmer  -i MotifL_$i.fasta -f $((40-$i)) -l $((50-$i)) -o MotifL_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifS_$i
	awk 'NR % 3 !=0' MotifS_$i > MotifS_$i.fasta
	sed -i 's/U/T/g' MotifS_$i.fasta
	fastx_trimmer  -i MotifS_$i.fasta -f $((40-$i)) -l $((50-$i)) -o MotifS_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifK_$i.fasta | awk '{print $1/2}'` >> MotifK_count.txt
	echo "$i" `wc -l < MotifL_$i.fasta | awk '{print $1/2}'` >> MotifL_count.txt
	echo "$i" `wc -l < MotifS_$i.fasta | awk '{print $1/2}'` >> MotifS_count.txt	

	                                 
done

	echo "MotifK" >> MotifAll_count.txt
	cat MotifK_count.txt >> MotifAll_count.txt
	echo "MotifL" >> MotifAll_count.txt
	cat MotifL_count.txt >> MotifAll_count.txt
	echo "MotifS" >> MotifAll_count.txt
	cat MotifS_count.txt >> MotifAll_count.txt


for ((i=0; i<11; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\.\(\(\(" $1 -B2 --no-group-separator > MotifM_$i
	awk 'NR % 3 !=0' MotifM_$i > MotifM_$i.fasta
	sed -i 's/U/T/g' MotifM_$i.fasta
	fastx_trimmer  -i MotifM_$i.fasta -f $((40-$i)) -l $((51-$i)) -o MotifM_$(($i))_trimmed.fasta
	
	egrep -E "^.{$((39-$i))}\)\)\)\.\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifN_$i
	awk 'NR % 3 !=0' MotifN_$i > MotifN_$i.fasta
	sed -i 's/U/T/g' MotifN_$i.fasta
	fastx_trimmer  -i MotifN_$i.fasta -f $((40-$i)) -l $((51-$i)) -o MotifN_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifT_$i
	awk 'NR % 3 !=0' MotifT_$i > MotifT_$i.fasta
	sed -i 's/U/T/g' MotifT_$i.fasta
	fastx_trimmer  -i MotifT_$i.fasta -f $((40-$i)) -l $((51-$i)) -o MotifT_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifM_$i.fasta | awk '{print $1/2}'` >> MotifM_count.txt
	echo "$i" `wc -l < MotifN_$i.fasta | awk '{print $1/2}'` >> MotifN_count.txt
	echo "$i" `wc -l < MotifT_$i.fasta | awk '{print $1/2}'` >> MotifT_count.txt	

		                                                                    
done

	echo "MotifM" >> MotifAll_count.txt
	cat MotifM_count.txt >> MotifAll_count.txt
	echo "MotifN" >> MotifAll_count.txt
	cat MotifN_count.txt >> MotifAll_count.txt
	echo "MotifT" >> MotifAll_count.txt
	cat MotifT_count.txt >> MotifAll_count.txt

for ((i=0; i<12; i++)) ;

do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\.\.\(\(\(" $1 -B2 --no-group-separator > MotifO_$i
	awk 'NR % 3 !=0' MotifO_$i > MotifO_$i.fasta
	sed -i 's/U/T/g' MotifO_$i.fasta
	fastx_trimmer  -i MotifO_$i.fasta -f $((40-$i)) -l $((52-$i)) -o MotifO_$(($i))_trimmed.fasta


	egrep -E "^.{$((39-$i))}\)\)\)\.\.\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifP_$i
	awk 'NR % 3 !=0' MotifP_$i > MotifP_$i.fasta
	sed -i 's/U/T/g' MotifP_$i.fasta
	fastx_trimmer  -i MotifP_$i.fasta -f $((40-$i)) -l $((52-$i)) -o MotifP_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifU_$i
	awk 'NR % 3 !=0' MotifU_$i > MotifU_$i.fasta
	sed -i 's/U/T/g' MotifU_$i.fasta
	fastx_trimmer  -i MotifU_$i.fasta -f $((40-$i)) -l $((52-$i)) -o MotifU_$(($i))_trimmed.fasta
		
	echo "$i" `wc -l < MotifO_$i.fasta | awk '{print $1/2}'` >> MotifO_count.txt
	echo "$i" `wc -l < MotifP_$i.fasta | awk '{print $1/2}'` >> MotifP_count.txt
	echo "$i" `wc -l < MotifU_$i.fasta | awk '{print $1/2}'` >> MotifU_count.txt	

                              
done

	echo "MotifO" >> MotifAll_count.txt
	cat MotifO_count.txt >> MotifAll_count.txt
	echo "MotifP" >> MotifAll_count.txt
	cat MotifP_count.txt >> MotifAll_count.txt
	echo "MotifU" >> MotifAll_count.txt
	cat MotifU_count.txt >> MotifAll_count.txt	 


for ((i=0; i<13; i++)) ;
do
	egrep -E "^.{$((39-$i))}\(\(\(\.\.\.\.\.\.\.\.\)\)\)" $1 -B2 --no-group-separator > MotifV_$i
	awk 'NR % 3 !=0' MotifV_$i > MotifV_$i.fasta
	sed -i 's/U/T/g' MotifV_$i.fasta
	fastx_trimmer  -i MotifV_$i.fasta -f $((40-$i)) -l $((53-$i)) -o MotifV_$(($i))_trimmed.fasta
		
 	echo "$i" `wc -l < MotifV_$i.fasta | awk '{print $1/2}'` >> MotifV_count.txt			                                  
done	

for ((i=0; i<3; i++)) ;
do
	egrep -E "^.{$((39-$i))}\)\)\(\(" $1 -B2 --no-group-separator > MotifW_$i
	awk 'NR % 3 !=0' MotifW_$i > MotifW_$i.fasta
	sed -i 's/U/T/g' MotifW_$i.fasta
	fastx_trimmer  -i MotifW_$i.fasta -f $((40-$i)) -l $((43-$i)) -o MotifW_$(($i))_trimmed.fasta
	echo "$i" `wc -l < MotifW_$i.fasta | awk '{print $1/2}'` >> MotifW_count.txt			                                  
done

for ((i=0; i<5; i++)) ;
do
	egrep -E "^.{$((39-$i))}\(\(\.\(\(\." $1 -B2 --no-group-separator > MotifX_$i
	awk 'NR % 3 !=0' MotifX_$i > MotifX_$i.fasta
	sed -i 's/U/T/g' MotifX_$i.fasta
	fastx_trimmer  -i MotifX_$i.fasta -f $((40-$i)) -l $((45-$i)) -o MotifX_$(($i))_trimmed.fasta
		
	egrep -E "^.{$((39-$i))}\)\)\.\)\)\." $1 -B2 --no-group-separator > MotifY_$i
	awk 'NR % 3 !=0' MotifY_$i > MotifY_$i.fasta
	sed -i 's/U/T/g' MotifY_$i.fasta
	fastx_trimmer  -i MotifY_$i.fasta -f $((40-$i)) -l $((45-$i)) -o MotifY_$(($i))_trimmed.fasta
	
	echo "$i" `wc -l < MotifX_$i.fasta | awk '{print $1/2}'` >> MotifX_count.txt
	echo "$i" `wc -l < MotifY_$i.fasta | awk '{print $1/2}'` >> MotifY_count.txt			                                  
done		

	echo "MotifV" >> MotifAll_count.txt
	cat MotifV_count.txt >> MotifAll_count.txt
	echo "MotifW" >> MotifAll_count.txt
	cat MotifW_count.txt >> MotifAll_count.txt
	echo "MotifX" >> MotifAll_count.txt
	cat MotifX_count.txt >> MotifAll_count.txt	
	echo "MotifY" >> MotifAll_count.txt
	cat MotifY_count.txt >> MotifAll_count.txt




