I have a text file with something like this in it:
>ENSG00321 Gene_ch19 . . . .
AATAGCGGTTAACATATTAATTAAACTAATGTATAGATATTATCCGCTTCCAGATTTATTATTTTGTAAA
ATAGGCACAGTTCTTATCTGGTGTCACTTTATGCTTTTTTAATAGACGCCCAGCTCTACTCATGGCCAAG
.
.
>ENSG00531 Gene_ch5 . . . .
AAAGAAGCGTAGGCCCGCCATGCAGCGTAAAAAGGATAATGCGGAGCAGCGGTCCCCAAAGACTGATTTA
TCAAAACTGTTTTCGCAGCAATTAACACACCCACCGACCAGTAACACCGCTGCAAGCAAGGCCACGGGCA
.
.
I have some headers like (ENSG00321) too.
I want to search in text files with header and then get characters(AGCT, characters line start from below of the header line) from 5 to 100 of that header and copy to a new text file.
How can I programming this? In MATLAB or PERL or C++.

Dani AI

Generated

As explained you already have a list of headers plus a start and length for each — the robust way is to treat the file as FASTA-like (header lines begin with > and the sequence is all following lines until the next header), concatenate the sequence lines for a header, then extract the substring using 1-based coordinates (convert to 0-based for code). Processing header-by-header avoids holding the whole file in memory: read a header, accumulate its sequence, extract any requested slices, write them out, then discard the sequence before moving to the next header. was right that compiled code works fine, but Perl is often the shortest, easiest-to-run choice for this task.

Example Perl script (streaming per header). Save a mapping file with lines like header_name start length (whitespace separated). Run as perl extract.pl sequences.fasta map.txt out.fasta:

#!/usr/bin/perl
use strict;
use warnings;

my ($fasta, $map, $out) = @ARGV;
open my $mh, '<', $map or die "map: $!";
my %targets;
while (<$mh>) {
  chomp; next if /^\s*$/;
  my ($h,$s,$len) = split;
  push @{ $targets{$h} }, [$s, $len];
}
open my $fh, '<', $fasta or die "fasta: $!";
open my $of, '>', $out or die "out: $!";

my ($hdr, $seq) = ('', '');
while (<$fh>) {
  chomp;
  if (/^>(\S+)/) {
    process($hdr,$seq,\%targets,$of) if $hdr;
    ($hdr, $seq) = ($1, '');
    next;
  }
  s/\s+//g;
  $seq .= uc($_);
}
process($hdr,$seq,\%targets,$of) if $hdr;

sub process {
  my ($h,$sref,$tref,$ofh) = @_;
  return unless $h && exists $tref->{$h};
  for my $pair (@{ $tref->{$h} }) {
    my ($start,$len) = @$pair;
    my $pos = $start - 1;
    if ($pos >= length($sref)) { warn "Start beyond end: $h\n"; next }
    my $sub = substr($sref, $pos, $len);
    print $ofh ">$h:$start-".($start+length($sub)-1)."\n$sub\n";
  }
}

Notes and gotchas: confirm whether your start positions are 1-based (most bio formats use 1-based). Strip whitespace and uppercase the sequence before counting. If many headers are needed but each extraction is small, the streaming approach above keeps RAM low. For very large numbers of random extractions you can build an index (or use existing FASTA index libraries) and use random access in C++ or MATLAB, but that adds complexity.

Recommended Answers

All 2 Replies

You just want to copy lines 5-100 out of one file and into another file? You could use c++ for that - look into fstream and cin.getline()

not always from 5th character. it can be different. i want find Header and then read characters from special character like 5th or 41th or etc and then read special number of characters start from 5th(ex, 100 or 102 or etc) and then copy and write them to another file. I have the address of headers and start character, and number of characters for each headers that must be read and copy from start character.
like this:
5th char "start to read 112 character after 5th character" so, end of string that will be copy must be 117th character.
Also there are a lot of headers and their characters in text file. i have the address of headers and their characters, that should be copy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.