Skip to content

Instantly share code, notes, and snippets.

@ShujiaHuang
Last active August 29, 2015 14:05
Show Gist options
  • Save ShujiaHuang/9c77bf979eeebc51cc4d to your computer and use it in GitHub Desktop.
Save ShujiaHuang/9c77bf979eeebc51cc4d to your computer and use it in GitHub Desktop.
读存Fa文件
// C++ read fa-sequence
void ReadFaSeq(const char *file, map<string, string> &fa) {
ifstream I(file);
if (!I){
cerr << "Cannot open file : " << file << endl;
exit(1);
}
string tmp, refId;
while (1) {
I >> tmp;
if (I.eof()) break;
if (tmp[0] != '>') {
fa[refId] += tmp;
} else {
refId.assign (tmp, 1, string::npos);
fa[refId].clear();
}
getline(I, tmp, '\n');
}
I.close();
}
void OutputFa(string id, int lineLength, string &seq) {
cout << id << endl;
for (size_t i(0); i < seq.size(); ++i) {
cout << seq[i];
if ((i + 1) % lineLength == 0) cout << endl;
}
if (seq.length() % lineLength > 0) cout << endl;
return;
}
### Read Fa sequence ###
sub ReadFaSeq {
my ( $file, $fa ) = @_;
my ( $refId, $seq );
open I, $file or die "Cannot open file : $file\n";
$/ = ">"; <I>; $/ = "\n";
while ( <I> ) {
chomp;
$refId = ( split /\s+/ )[0];
$/ = ">"; chomp( $seq = <I> ); $/ = "\n";
$seq =~ s/\s+//g;
$$fa{$refId} = $seq;
}
close I;
}
### Output Fa Sequence ####
sub OutputFa {
my ( $id, $lineLength, $seq ) = @_;
my $i= 0;
print "$id\n";
for (; $i < length( $$seq ); $i += $lineLength ) {
print substr($$seq, $i, $lineLength );
print "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment