2
$\begingroup$

enter image description here

enter image description here

Hi, I am trying to create a small repository by entering three things as a list of string, namely the asp, sequence and description. The trouble that I am facing is that while I am manually running the first green block by manually changing the value of the integer-character in asp, things are running fine and I am able to use ReadLines[file] properly.

However, when I try to modify a previous asp by running the second green block of the code and using the modifiedasp, things go haywire and the output seems strange. By strange I mean that any new value that I add (now running the first block of code) after modifying a previous value (second block of code) gets incorporated together into a single list of strings. The output is shown in the second figure. I modified two values and therefore two new additions somehow merged with the modified values. Can anyone suggest me what I am doing wrong? Thanks !

(*Add a new entry*)

file = "C:\\Users\\Ali\\Desktop\\database.csv";
str = OpenAppend[file];

asp = StringJoin["ASP", "8"];
sequence = "CGTCAATGGCATGATCGAT";
description = "the foursome are combined to make 16some";

Catch[
  If[MemberQ[ReadList[file][[All, 1]], asp], 
    Throw["The construct is already part of the list"], 
    Write[str, List[asp, sequence, description]]]]
Close[file];

(*Modify an existing entry*)

data = ReadList[file];
ind = 4;
existingEntry = data[[ind]];

modifiedasp = "ASP6";
modifiedSEQ = "CGTCGATCCGTATGGC";
modifiedDescription = "the dinucleosomes are combined to form 4some";

modifiedEntry = {modifiedasp, modifiedSEQ, modifiedDescription};
data[[ind]] = modifiedEntry;
Export[file, data, "List"];

Print["the previous entry ", 
  ToString @ existingEntry, " has been replaced by ", 
  ToString @ modifiedEntry]

(*Read the entire file and Print in Tableform*)

TableForm[ReadList[file], TableSpacing -> {1, 5}]
Print["There are ", 
  ToString @ Length[ReadList[file]], " entries in the database"]
$\endgroup$
7
  • $\begingroup$ Please, post code, not images; try to keep it simple by posting a minimal example that illustrates the problems you are encountering and last, but not least, format your code properly -- more on that in the help centre. $\endgroup$
    – Sektor
    Sep 11 '15 at 10:46
  • $\begingroup$ code added in the question. Thanks for the feedback! $\endgroup$
    – Ali Hashmi
    Sep 11 '15 at 10:51
  • $\begingroup$ any help on the issue will be highly appreciated! $\endgroup$
    – Ali Hashmi
    Sep 11 '15 at 12:36
  • 1
    $\begingroup$ The code you posted throws errors and does not generally behave as you indicated. Could you check it? You might also want to include a small sample database as well. $\endgroup$
    – MarcoB
    Sep 11 '15 at 13:20
  • $\begingroup$ basically first I created an empty .csv file (provided in the filename) and then I ran the first half of the code (i.e. before (Modify an existing entry) for three times by pressing shift enter on the block of code and during those three instances I manually change the integer-character from 1,2 and 3. Then I move to the second block of code i.e. after (modify an existing entry) and before (Read the entire file and Print in TableForm); I changed the ind to modify i.e. ind = 3 (its 4 in the code but I changed to 3 in my sample code) and run it once. $\endgroup$
    – Ali Hashmi
    Sep 11 '15 at 13:55
2
$\begingroup$
(*add a new entry*)
file = "C:\\Users\\Ali\\Desktop\\database.csv";
str = OpenAppend[file];
asp = StringJoin["ASP", "9"];
sequence = "CGTCAATGGCATGATCGAT";
description = "the foursome are combined to make 16some";

Catch[If[MemberQ[ReadList[file][[All, 1]], asp], 
Throw["The construct is already part of the list"], 
Put[{asp, sequence, description}, str]]]
Close[str];

(*modify an existing entry*)
data = ReadList[file];
ind = 1; 
existingEntry = data[[ind]];
str = OpenWrite[file];
modifiedasp = "ASP8";
modifiedSEQ = "CGTCGATCCGTATGGC";
modifiedDescription = "the dinucleosomes are combined to form 4some";

modifiedEntry = {modifiedasp, modifiedSEQ, modifiedDescription};
data[[ind]] = modifiedEntry;
Put[#, str] & /@ data;
Close[str];

Print["the previous entry ", ToString@existingEntry, " has been replaced by ", ToString@modifiedEntry]

(*read the entire file*)
TableForm[ReadList[file], TableSpacing -> {1, 5}]
Print["There are ", ToString@Length[ReadList[file]], " entries in the database"]

After deliberating for sometime, I found the a modification of the above code that solves the problem. What I ended up doing was replacing the Export command with Put in the second block of code i.e. (modify an existing entry)

$\endgroup$
1
  • $\begingroup$ Ali I am glad to see that you were able to solve your problem. Thank you for posting a self-answer with your solution! (+1) $\endgroup$
    – MarcoB
    Sep 14 '15 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.