Can't get my program to run -- GC calculator?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't get my program to run -- GC calculator?
# 1  
Old 03-11-2016
Can't get my program to run -- GC calculator?

have been trying for a few weeks not to get this program running. I am newer to programming and it has definitely been a challenge. I think my problem arises with my if statement. I can get it to append the name to the new file, but it simply appends the whole sequence to the file rather than counting it. I am working with a fasta file that contains multiple sequences, the name starting with '>' and the sequence on one line below it. Here is my code. Please help, and thank you so much in advance!!

Code:
#! /bin/bash

#exit program with error if user does not specify input on command line

if [ $# != 1 ]; then
        echo "Please specify fasta input on command line and rerun"
        exit
        else echo "Begining count"
fi

#collect input from user each time they run the program
input=`cat $1`

#seperate the sequence from the sequence name
name=`grep '>' $1`
sequence=`grep -v '>' $1`


#if name, if sequence
IFS=$'\n'
set -f
for i in $(cat "$1"); 
do
	if [ $i=">" ]; then
		echo "$i" >> GCcontent.txt
		else 
		#count number of occurence of motif ATGC in fasta sequence
		countG=`echo $i | grep -o "G" | wc -l`
		countC=`echo $i | grep -o "C" | wc -l`
		total=`echo $i | wc -m`
		count=`echo "scale=2" ; ($countG+$countC) | bc`

		#calculate percent over total divided by 3bp
		percent=`echo "scale=2 ; ($count/$total*100)" | bc`
		
		#print output name and percent to file
		echo "$percent" >> GCcontent.txt
	fi
done

echo "Exiting"

exit

Edit:

The input file has multiple sequence within it, all with respective titles. they look something like this:
Code:
>gi|226451773|gb|FJ846591.1
CATTATAGACTGCGTGGTCCGTATTCCCAAGGAGCAGGGAGTTCTGTCCTTCTGGCGCGGTAACCTGGCCAATGTCATCAGATACTTCCCCACCCAGGCTCTTAACTTCGCCTTCAAAGATAAATACAAGCAGATCTTCCTAGGTGGTGTGGACAAGAGGACCCAGTTTTGGCGCTACTTTGCAGGGAATCTGGCATCAGGTGGTGCCGCAGGGGCCACATCCCTGTGTTTTGTGTACCCTCTTGATTTTGCCCGTACCCGTCTAGCAGCTGATGTGGGTAAAGCTGGAGCTGAAAGGGAATTCCGAGGCCTCGGTGACTGCCTGGTTAAGATCTACAAATCTGATGGGATTAAGGGCCTGTACCAAGGCTTTAACGTGTCTGTGCAGGGTATTATCATCTACCGAGCCGCCTACTTCGGTATCTATGACACTGCAAAGGGTAAGTTTGCTGTGGGCTTTAAAGTTGTGTTCTTAGGAGACAATTTAAAAGAGCGTTGTACCAACCTAACATTCCAAGAGCTAGAGAGTTTTTTTAATTGCTGAAGGAAGCCAAGATCATCCAGTGCGACCCTCATGCACAGATGACATGTTTAGGGGATGTGGGGAAAGGAAGTCAGTAAAACTCTACTTTTTGGTAAAAGCATCTCTTTCCTATTCCCAGGAATGCTTCCGGATCCCAAAAACACTCACATCGTCATCAGCTGGATGATCGCACAGACTGTCACTGCTGTTGCTGGGTTGACTTCCTATCCATTTGA

The output i would like to contain the name and the percent of Gs an Cs (totaled together)
Code:
>Name of the file 
  percent of GCs

My idea for the program was to have the user input the file, then the loop either append the line that contains the title to the file GCcontent.txt or to run through the counter i have set up and append it to the file GCcontent.txt
Moderator's Comments:
Mod Comment An opening CODE tag looks like [CODE]; not [/CODE]. And please display sample input, sample output, and code segments in CODE tags; not just code segments.

Last edited by Don Cragun; 03-12-2016 at 05:09 AM.. Reason: Fix CODE tags on code segments; add CODE tags on sample input and output.
# 2  
Old 03-11-2016
The test or [ command needs a space around the = sign.

Not sure I entirely understand what you're after. An input sample, the desired output and a description of the logics connecting the two would definitely help.
# 3  
Old 03-11-2016
I updated it for you
# 4  
Old 03-12-2016
For ME?

You did not correct the erroneous [ statement. And, outputtting the lines starting with > doesn't seem to match your output sample >Name of the file

---------- Post updated at 12:23 ---------- Previous update was at 11:32 ----------

Would this do what you want?
Code:
awk '/^>/ {print; next} {printf "GC percent: %.2f\n", gsub (/[GC]/, "&")/length*100}' file
>gi|226451773|gb|FJ846591.1
GC percent: 48.09

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run a program as another user

I have a bash script that launches another problem. I need to run that program under a certain user account. The script itself is run using 'sudo <scriptname>'. #!/bin/bash myprogram=... exec "$myprogram" How would this be done? (1 Reply)
Discussion started by: Carson Dyle
1 Replies

2. UNIX for Dummies Questions & Answers

Help to run this socket program in C

i have created two files named server and client then when i run the server program it says the server is waiting(./server 5555) then when i run the client program it says "client error:connection refused" can u plz help me to run it?:( (7 Replies)
Discussion started by: kedah160
7 Replies

3. UNIX for Dummies Questions & Answers

Log in, run program

Hey, im editing the passwd file so that when the user ben logs in it runs my assign program. I changed the last column from to Then when i log in i get... There is no problem with the program because it runs fine when i open it normally. Any help much appreciated. (8 Replies)
Discussion started by: RAFC_99
8 Replies

4. Shell Programming and Scripting

Run shell program in perl

Hello , I want to run some shell scripts in my perl script. I need to read the script's name from a file ( this file includes the name of all the scripts) and run the script one by one.. Please let me know how to go .. Thanks in advance, Radha (5 Replies)
Discussion started by: s123.radha
5 Replies

5. UNIX for Dummies Questions & Answers

cannot run program

Hi, I have a program in /opt/local/bin, my path in my .profile is export PATH=/opt/local/bin:/opt/local/sbin:$PATH however when i type the program name it cannot find it, however i know the program is in /opt/local/bin? Thanks (2 Replies)
Discussion started by: c19h28O2
2 Replies

6. Shell Programming and Scripting

calculator program..

Hey can anyone tell me the korn script code to implement an interactive integer calculator using the shell's built in arithemetic expression evaluation (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

7. Shell Programming and Scripting

how can i run java program from any where

Hello all sorry didnt know where to post it i compiled simple program that using "System.getProperty("user.dir");" now i compiled the program under directory foo and and its running great when i do "java myapp" i included this directory in the $PATH env variable so i could see it fro any where... (1 Reply)
Discussion started by: umen
1 Replies

8. UNIX for Dummies Questions & Answers

How do i run a program while in Unix?....

Im new and wanted to know if im in the unix terminal and lets say i want to run microsoft word for example. i go in and go into the HD and then keep going and i type ls and see that microsoft word.app is there. how do i run it from that? (4 Replies)
Discussion started by: Corrail
4 Replies

9. UNIX for Dummies Questions & Answers

Getting a program to run

Please help, I have tried to run this program countless times and still nothing. Please tell me what I'm doing wrong. $ cat>test count=1 while do echo "5" read number echo $5 let count=count+ 1 done exit 0 ^C$ ksh test $ ^C $ ksh test $ $ nothing happend (6 Replies)
Discussion started by: ctiamaria
6 Replies

10. Programming

how to run debugging on c program

Can someone help me debug a c program I am running? It gives me segmentation fault. I want to turn on debugging. Can some one give the command to turn it on? Below is the error I get: Segmentation Fault (core dumped) (3 Replies)
Discussion started by: ibeg
3 Replies
Login or Register to Ask a Question