Deleting sequences based on character frequency


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting sequences based on character frequency
# 1  
Old 06-17-2010
Deleting sequences based on character frequency

This is what I would like to accomplish, I have an input file (file A) that consist of thousands of sequence elements with the same number of characters (length), each headed by a free text header starting with the chevron ‘>' character followed by the ID (all different IDs with different lenghts) and a number (prevalence). Something like this:
Quote:
> ID 1 Prev 1
C-TGCTAGCTACGTCGTACGT
> ID 2 Prev 31
A-TGCTAGCTACGTCGTACGT
> ID 3 Prev 30
A-TGCTAGCTACGTCGTTCGT
> ID 4 Prev 30
A-TGCTAGCTACGTCGTACGA
> ID 5 Prev 2
A-TGCTAGCTACGTCG-----
> ID 6 Prev 2
A-TGCTAGCTACNTCGTACGT
> ID 7 Prev 2
A-CGCTAGCTACGTCGTACGT
> ID 8 Prev 2
A-TGCTAGCTA-GTCGTACGT
> ID 9 Prev 1
AGTGCTAGCTACGTCGTACGT
I need to calculate the frequency of A,G,C,T,- and N in each position. Then, I need to evaluate the first position in the first sequence and if the frequency of the character in that position does not reach 5% the entire sequence along with the ID should be removed. If the frequency is higher then I need to determine the frequency of the character in the second position so on and so forth till the entire sequence has been scanned. Then, I should do the same thing for each and every sequence in the file.
Thus, in my example above Seq ID 1 should be removed since "C" accounts for only 1% (Prev=1) in that column (the ID is not considered in the analysis). As result of this process, the output file (File B) should only contained Sequences 2, 3 and 4 since the frequency of each character in every position along the entire sequence in the three entries is higher than 5%. The output file should have the same format (FASTA) as the input file:
Quote:
> ID 2 Prev 31
A-TGCTAGCTACGTCGTACGT
> ID 3 Prev 30
A-TGCTAGCTACGTCGTTCGT
> ID 4 Prev 30
A-TGCTAGCTACGTCGTACGA
Any help will be greatly appreciated.
# 2  
Old 06-17-2010
Hi, try this:
Code:
awk '$1==">"&&$5>=5{print; getline; print}' infile

or more cryptic:
Code:
awk '/^>/{p=($5>=5)?1:0}p' infile


Last edited by Scrutinizer; 06-17-2010 at 04:39 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-17-2010
Amazing! It worked like a charm!

Would it be possible to couple the pre-processing step? This is what I would like to do:

My input file (file A) contains the following information:
Quote:
> ID 1
ATGCTAGCTACGTCGTACGT
> ID 2
ACGCTGACGTAGCTA
> ID 3
ACTC
> ID 4
ACGCTGACGTAGCTA
> ID 5
ATGCTAGCTACGTCGTAC
> ID 6
AT
> ID 7
ATGCTAGCTACGTCGTAC
> ID 8
ATGCTAGCTACGTCGTAC
> ID 9
ATGCTAGCTACGTCGTAC
> ID 10
ACGCTGACGTAGCTA
> ID 11
ACGCTGACGTAGCTA
Now, I would like to create a file (file B) containing only the sequences with 5 or more characters but less than 18 with their corresponding ID an frequency, form my example above:
Quote:
> ID 2 Freq 3
ACGCTGACGTAGCTA
> ID 5 Freq 4
ATGCTAGCTACGTCGTAC
Then couple it with the code you mentioned above.
Would that be possible?
Thanks!
# 4  
Old 06-17-2010
Hi, this should do it:
Code:
WHINY_USERS=1 awk '/^>/{id=$0;next}length>=5&&length<=18{F[$1]++;if (!I[$1]) I[$1]=id} 
                   END{for(i in I)printf "%s Freq %s\n%s\n",I[i],F[i],i}' infile

Combining these two:
Code:
WHINY_USERS=1 awk '/^>/{id=$0;next}length>=5&&length<=18{F[$1]++;if (!I[$1]) I[$1]=id} 
                   END{for(i in I)if (F[i]>=5) printf "%s Freq %s\n%s\n",I[i],F[i],i}' infile

# 5  
Old 06-17-2010
A problem

If we use File A.fas contain the following sequences
Quote:
> Seq1 Freq 1
ATC
> Seq2 Freq 33
ACG
> Seq3 Freq 33
TTG
> Seq4 Freq 33
TCC
ANd then used your code
Code:
awk '/^>/{p=($5>=5)?1:0}p' A.fas > Unique.fas

As result I get Seq2, 3 and 4 but not Seq1. Now, if we determine the frequency of each character in Seq1, the values are always higher than 5% (34%), therefore it should also be kept. Thus, the code is not calculating the frequency of each character in each position (column) to decide whether to keep or remove the sequence. It is only considering the prevalence, if higher than 5% then the sequence will be kept.
I have also tried your last code:
Code:
$ awk '/^>/{id=$0;next}length>=5&&length<=18{F[$1]++;if (!I[$1]) I[$1]=id}END {for(i in I)printf "%s Freq %s\n%s\n",I[i],F[i],i}' A.fas > Unique.fas

But the Unique.fas file is empty.

Last edited by Xterra; 06-17-2010 at 06:22 AM..
# 6  
Old 06-17-2010
I think then that I do not fully understand your requirement. I took it that the number behind prev or Freq or whatever is the frequency or prevalence of the string itself. You seem to be suggesting that the script should look for the frequency of a particular character in that string?
# 7  
Old 06-17-2010
Frequency of each character

My bad, l guess I did not express myself correctly. From my previous example:
Quote:
> Seq1 Freq 1
ATC
> Seq2 Freq 33
ACG
> Seq3 Freq 33
TTG
> Seq4 Freq 33
TCC
The first character in the first sequence is A, and the frequency of that sequence is 1%. However, A is also present in the same position (1) in sequence #2 and the frequency of that second sequence is 33%. Therefore, the 'global' frequency of A in the data set in position 1 is = 34%, hence it should be kept. When you do the same for the second and thrid position you can see that the global frequency for T (second character) and C (third character) from Sequence 1, are =34%, and therfore that should be enought to keep the entire sequence.
I hope I have better explain what I would like to accomplish.
Thanks one more time!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting sequences based on length with sed

I have this file: >ID1 AA >ID2 TTTTTT >ID-3 AAAAAAAAA >ID4 TTTTTTGGAGATCAGTAGCAGATGACAG-GGGGG-TGCACCCC Add I am trying to use this script to output sequences longer than 15 characters: sed -r '/^>/N;{/^.{,15}$/d}' The desire output would be this: >ID4... (8 Replies)
Discussion started by: Xterra
8 Replies

2. UNIX for Dummies Questions & Answers

Filling positions based on frequency

I have files with hundreds of sequences with frequency values reported as "Freq X" and missing characters represented by a dash ("-"), something like this >39sample Freq 4 TAGATGTGCCCGTGGGTTTCCCGTCAACACCGGATAGTAGCAGCACTA >22sample Freq 15 T-GATGTCGTGGGTTTCCCGTCAACACCGGCAAATAGTAGCAGCACTA... (12 Replies)
Discussion started by: Xterra
12 Replies

3. Shell Programming and Scripting

Print sequences from file2 based on match to, AND in same order as, file1

I have a list of IDs in file1 and a list of sequences in file2. I can print sequences from file2, but I'm asking for help in printing the sequences in the same order as the IDs appear in file1. file1: EN_comp12952_c0_seq3:367-1668 ES_comp17168_c1_seq6:1-864 EN_comp13395_c3_seq14:231-1088... (5 Replies)
Discussion started by: pathunkathunk
5 Replies

4. Shell Programming and Scripting

Eliminating sequences based on Distances

I have to remove sequences from a file based on the distance value. I am attaching the file containing the distances (Distance.xls) The second file looks something like this: Sequences.txt >Sample1 Freq 59 ggatatgatgatgaactggt >Sample1 Freq 54 ggatatgatgttgaactggt >Sample1 Freq 44... (2 Replies)
Discussion started by: Xterra
2 Replies

5. Shell Programming and Scripting

Selecting sequences based on scores

I have two files with thousands of sequences of different lengths. infile1 contains the actual sequences and infile2 the scores for each A, T, G and C in infile1. Something like this: infile1: >HZVJKYI01ECH5R TTGATGTGCCAGCTGCCGTTGGTGTGCCAA >HZVJKYI01AQWJ8 GGATATGATGATGAACTGGTTTGGCACACC... (4 Replies)
Discussion started by: Xterra
4 Replies

6. Shell Programming and Scripting

Extract sequences based on the list

Hi, I have a file with more than 28000 records and it looks like below.. >mm10_refflat_ABCD range=chr1:1234567-2345678 tgtgcacactacacatgactagtacatgactagac....so on >mm10_refflat_BCD range=chr1:3234567-4545678... tgtgcacactacacatgactagtatgtgcacactacacatgactagta . . . . . so on ... (2 Replies)
Discussion started by: Diya123
2 Replies

7. Shell Programming and Scripting

Trimming sequences based on Reference

My file looks something like this Wnat I need is to look for the Reference sequence (">Reference1") and based on the length of that sequence trim all the entries in that file. So, the rersulting file will contain all sequences with the same length, like this Thus, all sequences will keep... (5 Replies)
Discussion started by: Xterra
5 Replies

8. Shell Programming and Scripting

Removing low frequency sequences

If I have a file with the following information And I would like to remove all the sequences with Freq less than 3, so I end up having the following file: I am currently using awk to accomplish this task but I am not getting the results I actually want. Any help will be greatly appreciated. (3 Replies)
Discussion started by: Xterra
3 Replies

9. Shell Programming and Scripting

Trimming sequences based on specific pattern

My files look like this And I need to cut the sequences at the last "A" found in the following 'pattern' -highlighted for easier identification, the pattern is the actual file is not highlighted. The expected result should look like this Thus, all the sequences would end with AGCCCTA... (2 Replies)
Discussion started by: Xterra
2 Replies

10. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies
Login or Register to Ask a Question