Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Adamo
    Member
    • Jun 2010
    • 28

    Getting sequences into a bam file - psl2sam.pl

    Hello,

    I have a psl file, and convert it into sam format using psl2sam.pl (samtools).
    I'd like to know if there is a way of getting the target sequence in the sam file in place of the "*" placeholder?
    pslx format doesn't work either.

    I have found the code of some script that should do the job, but it doesn't work. Here is the script, if someone wants to see it:

    Code:
    #!/usr/bin/perl -w
    
    # Contact: nlw
    #this script combines a sam file with the sequences from a fasta file.  originally designed as a second step when converting blat (psl) to sam.
    
    
    $| = 1;
    
    ## SJC: Generic debug variable and print.
    my $DEBUG = 1;
    sub kvetch{
        my $foo = shift || '';
        print STDERR $foo . "\n" if $DEBUG;
    }
    
    
    use strict;
    use warnings;
    use Getopt::Std;
    use File::Copy;
    use Switch;
    
    my $lines_read = 0;
    my $lines_written = 0;
    my @filearray;
    my $lines_in_file = 0;
    my $start = time();
    my @buffer;
    
    ($lines_read, $lines_written, $lines_in_file) = &bowtie2sam;
    my $stop = time();
    my $time = $stop-$start;
    print STDERR "Done. Processed in " . $time . " sec \n";
    
    exit;
    
    sub bowtie2sam {
      my %opts = ();
      die("Usage: tab_merge_sam.pl <seq> <sam>\n") if (@ARGV == 0 && -t STDIN);
      my $seq_file = $ARGV[0];
      my $sam_file = $ARGV[1];
    
      my $tmp_seq = $seq_file . ".tmp";
      my $tmp_sam = $sam_file . ".tmp";
      my $merged_sam = $sam_file . ".seq_merged";
    
      print STDERR "Sorting sequence file \"$seq_file\"... ";
      `grep -v sequence $seq_file | sort > $tmp_seq`;
     
      print STDERR "Done.\n";
    
      print STDERR "Sorting sam file \"$sam_file\"... ";
      `sort $sam_file > $tmp_sam`;
    
      print STDERR "Done.\n";
    
      open(SAM, $tmp_sam);
      open(SEQ, $tmp_seq);
    
    
      my $wc_seq = `wc -l $tmp_seq`;
      my $lines_in_seq = (split(" ", $wc_seq))[0];
      my $lines_read_sam = 0;
      my $lines_written = 0;
      my $read_not_found = 0;
      my $reads_matched = 0;
      my $lines_read_seq = 0;
    
      print STDERR "there are " . $lines_in_seq . " lines in the file \"" . $seq_file . "\"\n";
      print STDERR "Merging...\n";
      my $sam_id = "";
    
      #there may be multiple alignments reported
      #for each of the reads in the tab file, find them in the sam file, add the hit counter
    
      while (<SEQ>) {
          $lines_read_seq++;
          my $seq_line = $_;
          chomp($seq_line);
          $seq_line =~ s/\s+/\t/g;
          my ($seq_id, $read_sequence) = split(/\t/,$seq_line);
    
          my $seq_found = 0;
          my $too_big = 0;
          my $seq_hits = 0; #counter for number of alignments for each sequence
          #print STDERR "line $lines_read_seq seq is $seq_id\n";
          my $flag = 0;
          while (!$flag) {
              last if eof(SAM);
              my $sam_line = <SAM>;  
              next if ($sam_line =~ /^\s*$/);
              my $line_length = length($sam_line);
              chomp($sam_line);
              $sam_line =~ s/\s+/\t/g;
              my @sam = split(/\t/, $sam_line);
              my $sam_id = $sam[0];
              my $strand = ($sam[1] == 0);
              #if ($sam_line =~ m/\Q$seq_id/) {  
              $lines_read_sam++;
              #print STDERR "line $lines_read_sam sam is $sam_id\n";
              if ($sam_id eq $seq_id) {
                  #add sequence to sam array
                  #$sam[9] = $strand ? $read_sequence : reverse_complement($read_sequence) ;
                  if ($strand) {
                      $sam[9] = $read_sequence;
                  } else {
                      #print STDERR "rc for read $sam_id at line $lines_read_sam\n";
                      $sam[9] = reverse_complement($read_sequence);
                  }
                  $seq_hits++;  
                  $sam[11] .= " HI:i:" . $seq_hits;
                  $reads_matched++;
                  #print STDERR "matched $seq_id at line $lines_read_sam of sam file\n";
                  print STDOUT join("\t", @sam) . "\n";      
              } elsif ($sam_id gt $seq_id) {
                  $lines_read_sam--;
                  #backup
                  seek(SAM,-$line_length,1);
                  #print STDERR "backing up to line $lines_read_sam of sam file\n";
                  $flag = 1;
              } else {
                  print STDOUT join("\t", @sam) . "\n";      
                  die;
              }  
          }
      }
      print STDERR "++++++++++++++++++++++++++++++++++\n";
      print STDERR "SAM read: $lines_read_sam\n";
      print STDERR "reads matched: $reads_matched\n";
      print STDERR "reads not found: $read_not_found\n";
      print STDERR "++++++++++++++++++++++++++++++++++\n";
      close(SAM);
      close(SEQ);
      unlink ($tmp_sam);
      unlink ($tmp_seq);
    }
    
    sub reverse_complement {
        my ($sequence) = @_;
        my $rc_sequence = "";
        my @array = split(//, $sequence);
        foreach my $c (@array) {
            switch ($c) {
                case "A" {$c = "T"}
                case "T" {$c = "A"}
                case "G" {$c = "C"}
                case "C" {$c = "G"}
            }
            $rc_sequence = $c . $rc_sequence
        }
        return $rc_sequence;
    }
    Thanks in advance!

Latest Articles

Collapse

  • SEQadmin2
    Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
    by SEQadmin2



    Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
    ...
    07-09-2026, 11:10 AM
  • SEQadmin2
    Cancer Drug Resistance: The Lingering Barrier to Rising Survival
    by SEQadmin2



    Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

    There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
    07-08-2026, 05:17 AM
  • GATTACAT
    Reply to Nine Things a Sample Prep Scientist Thinks About Before Sequencing
    by GATTACAT
    Love this - good data definitely starts from good input, and poor input can only give relatively poor data. I particularly like the mention of Nanodrop/absorbance based methods for quantification. It's such a toss up if you'll get an accurate reading or what amounts to a randomly generated number, and a lot of library/sequencing related issues can be traced back to poor quant.
    07-01-2026, 11:43 AM

ad_right_rmr

Collapse

News

Collapse

Topics Statistics Last Post
Started by SEQadmin2, 07-13-2026, 10:26 AM
0 responses
18 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 07-09-2026, 10:04 AM
0 responses
30 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 07-08-2026, 10:08 AM
0 responses
16 views
0 reactions
Last Post SEQadmin2  
Started by SEQadmin2, 07-07-2026, 11:05 AM
0 responses
34 views
0 reactions
Last Post SEQadmin2  
Working...