Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with substitution 1

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
Hi! i am trying to replace text from one file in another and am not able to get it done. please suggest a solution.

thanks in advance

here is what i have done sofar
#!/usr/bin/perl -w

my ($line, $name, $sequence, $intron, $sequences, $line2);
my (%categories);
open(FILER1, "insertion-details.txt");
open(FILEW1, ">insertion-marked.txt");
;
while(!eof(FILER1)){
$line=<FILER1>;
if($line=~/\w/){
($name, $sequence, $intron) = split(/\t/, $line);


$sequences = $sequence.$intron;

$categories{$sequence} = $name;

print FILEW1 "$sequences\n";

open(FH2, "target.txt");
while(!eof(FH2)){
$line2 = <FH2>;
if($line2=~/$name/){
$line2 =~ s/$categories{$sequence}/$sequences/;
print FILEW1 "$line2\n";

}
else {
print FILEW1 "no match\n";
}

}
}
}

here are my input files and the expected output file. one of the entries has substitution at two places

insertion details as "insertion-details.txt"
data in both the input files is tabdelimited.

ATC51G4 VDNKFFNVAL (0)
ATC51G4 LIPKSRKRLP (1)
ATA51G1 IIPRSKKRLP (1)
CTC51G5 LIPKSRKRLP (1)
ATS51G2 IIPRSKKRLP (1)


target file as "target.txt"
Each entry will be on a seperate line
ATC51G4 MDVDNKFFNVALLIVATVVVAKLISALLIPKSRKRLPPTVKGF
ATA51G1 MDVXXKTFNAXFLLVATLLVAKLISALIIPRSKKRLPPTIKAF
CTC51G5 MDVDNKFFNVALLIVATVVVAKLISALLIPKSRKRLPPTVKAF
ATS51G2 MDVXXKAFNAXFLLVATLLVAKLISALIIPRSKKRLPPTIKTF


desired output as "insertion-marked.txt"
ATC51G4 MDVDNKFFNVAL(0)LIVATVVVAKLISALLIPKSRKRLP(1)PTVKGF
ATA51G1 MDVXXKTFNAXFLLVATLLVAKLISALIIPRSKKRLP(1)PTIKAF
CTC51G5 MDVDNKFFNVALLIVATVVVAKLISALLIPKSRKRLP(1)PTVKAF
ATS51G2 MDVXXKAFNAXFLLVATLLVAKLISALIIPRSKKRLP(1)PTIKTF


 
Well, I don't have any suggestions concerning the supposed flaw in your logic. You haven't told us anything about what you're trying to do to be able to tell you where your code is incorrect. However, here is how I would clean up your current code:

Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$catfile[/blue] = [red]"[/red][purple]insertion-details.txt[/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]$infile[/blue]  = [red]"[/red][purple]target.txt[/purple][red]"[/red][red];[/red]
[black][b]my[/b][/black] [blue]$outfile[/blue] = [red]"[/red][purple]insertion-marked.txt[/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]%categories[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]CAT, [blue]$catfile[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$catfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<CAT>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] ! [red]/[/red][purple][purple][b]\w[/b][/purple][/purple][red]/[/red][red];[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [blue]$sequence[/blue], [blue]$intron[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$categories[/blue][red]{[/red][blue]$name[/blue][red]}[/red][red]}[/red], [red][[/red][blue]$sequence[/blue], [blue]$intron[/blue][red]][/red][red];[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]CAT[red])[/red][red];[/red]

[black][b]open[/b][/black][red]([/red]INFH, [red]'[/red][purple]<[/purple][red]'[/red], [blue]$infile[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$infile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red]OUTFH, [red]'[/red][purple]>[/purple][red]'[/red], [blue]$outfile[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<INFH>[red])[/red] [red]{[/red]
	[black][b]chomp[/b][/black][red];[/red]
	[black][b]my[/b][/black] [red]([/red][blue]$name[/blue], [blue]$string[/blue][red])[/red] = [black][b]split[/b][/black] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red];[/red]

	[olive][b]if[/b][/olive] [red]([/red][blue]$categories[/blue][red]{[/red][blue]$name[/blue][red]}[/red][red])[/red] [red]{[/red]
		[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$data[/blue] [red]([/red][blue]@[/blue][red]{[/red][blue]$categories[/blue][red]{[/red][blue]$name[/blue][red]}[/red][red]}[/red][red])[/red] [red]{[/red]
			[black][b]my[/b][/black] [red]([/red][blue]$sequence[/blue], [blue]$intron[/blue][red])[/red] = [blue]@$data[/blue][red];[/red]
			[blue]$string[/blue] =~ [red]s/[/red][purple][purple][b]\Q[/b][/purple][blue]$sequence[/blue][purple][b]\E[/b][/purple][/purple][red]/[/red][purple][blue]$sequence[/blue][blue]$intron[/blue][/purple][red]/[/red][red]g[/red][red];[/red]
		[red]}[/red]

	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]No match for: [blue]$name[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]

	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUTFH [red]"[/red][purple][blue]$name[/blue][purple][b]\t[/b][/purple][blue]$string[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]close[/b][/black][red]([/red]INFH[red])[/red][red];[/red]
[black][b]close[/b][/black][red]([/red]OUTFH[red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul][/tt]

- Miller
 

Many thanks Milller, a big star for this. it works right for me. sorry i thought i had enough details. these are protein sequences and i am trying to insert introns (gene gaps) at certain positions and i got off track.


 
Your welcome.

Although amusingly enough, after all that I replied using the wrong account :)

Darn.
- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top