This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Use Query List To Extra Sequeces

Question:

A database containing sequences as follows:

>leaf_1
AAGACCATTCGAGCTTATCTCTTC
>leaf_2
ATGGAGAAGGAAATGAAGAGCAGT
>leaf_3
TGGCTGTAAGTCATACCTGTCA
>leaf_4
CGCGGAGTAGATCAGTTTGGTA
>leaf_5
AGTAACGGCTTTACAAGAATCAAA
......

Now I have a query file (inquiry.txt), which looks like:

>leaf_2
>leaf_4
>leaf_5

Need an output file (result.txt) looks like:

>leaf_2
ATGGAGAAGGAAATGAAGAGCAGT
>leaf_4
CGCGGAGTAGATCAGTTTGGTA
>leaf_5
AGTAACGGCTTTACAAGAATCAAA

Could anyone help with this question? Many thanks.

data list

2 answers

Try?

$ perl below-script.pl all-sequences.txt inquiry.txt

#!/usr/bin/perl

open (INPUT, $ARGV[0]) or die $1;
open (QUERY, $ARGV[1]) or die $1;
open (OUTPUT, ">result.txt");

chomp (my @array=<QUERY>);

while (<INPUT>) {
    foreach my $temp (@array){
    if ($_ =~ $temp) {
    $nextline = <INPUT>;
    print OUTPUT "$_$nextline";
    }
    }
}

close (OUTPUT);
close (QUERY);
close (INPUT);

You may also try the following Perl script... and this works for fasta format input files!

  use strict;
  use warnings;

  my @genes;
  open my $list, '<file2.list';
  while (my $line = <$list>) {
      push (@genes, $1) if $line =~ /[^>]+>([^|]+)/;
  }
  my $input;
  close $list;
  {
       local $/ = undef;
       open my $fasta, '<file1.fasta';
       $input = <$fasta>;
       close $fasta;
  }
 my @lines = split(/>/,$input);
 foreach my $l (@lines) {
      foreach my $reg (@genes) {
              print ">$l" if $l =~ /$reg\|/;
      }
}

File 2 will your query file and File1, the fasta sequence file in this case!

Log in to answer this question.